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 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hci.c" 39ab2c6ae4SMatthias Ringwald 401713bceaSmatthias.ringwald /* 411f504dbdSmatthias.ringwald * hci.c 421f504dbdSmatthias.ringwald * 431f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 441f504dbdSmatthias.ringwald * 451f504dbdSmatthias.ringwald */ 461f504dbdSmatthias.ringwald 477907f069SMatthias Ringwald #include "btstack_config.h" 4828171530Smatthias.ringwald 497f2435e6Smatthias.ringwald 5006b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 51aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 528f2a52f4SMatthias Ringwald #include "btstack_run_loop_embedded.h" 53a484130cSMatthias Ringwald #endif 5406b9e820SMatthias Ringwald #endif 55a484130cSMatthias Ringwald 564a3574a1SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 57d0b87befSMatthias Ringwald #include "../port/ios/src/btstack_control_iphone.h" 584a3574a1SMatthias Ringwald #endif 594a3574a1SMatthias Ringwald 60a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 61472a5742SMatthias Ringwald #include "gap.h" 6221debf25SMatthias Ringwald #include "ble/le_device_db.h" 6345c102fdSMatthias Ringwald #endif 6445c102fdSMatthias Ringwald 6593b8dc03Smatthias.ringwald #include <stdarg.h> 6693b8dc03Smatthias.ringwald #include <string.h> 675838a2edSMatthias Ringwald #include <inttypes.h> 687f2435e6Smatthias.ringwald 6916ece135SMatthias Ringwald #include "btstack_debug.h" 700e2df43fSMatthias Ringwald #include "btstack_event.h" 714a3574a1SMatthias Ringwald #include "btstack_linked_list.h" 724a3574a1SMatthias Ringwald #include "btstack_memory.h" 7361f37892SMatthias Ringwald #include "bluetooth_company_id.h" 741cfb383eSMatthias Ringwald #include "bluetooth_data_types.h" 754a3574a1SMatthias Ringwald #include "gap.h" 764a3574a1SMatthias Ringwald #include "hci.h" 774a3574a1SMatthias Ringwald #include "hci_cmd.h" 78d8905019Smatthias.ringwald #include "hci_dump.h" 791cfb383eSMatthias Ringwald #include "ad_parser.h" 8093b8dc03Smatthias.ringwald 812b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 822b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 832b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 842b838201SMatthias Ringwald #endif 852b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 862b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 872b838201SMatthias Ringwald #endif 882b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 892b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 902b838201SMatthias Ringwald #endif 912b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 922b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 932b838201SMatthias Ringwald #endif 942b838201SMatthias Ringwald #endif 951b0e3922Smatthias.ringwald 96aa81e641SMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) && defined(ENABLE_SCO_OVER_PCM) 97aa81e641SMatthias Ringwald #error "SCO data can either be routed over HCI or over PCM, but not over both. Please only enable ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM." 98aa81e641SMatthias Ringwald #endif 99aa81e641SMatthias Ringwald 1001e20a53eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) && defined(HAVE_SCO_TRANSPORT) 1011e20a53eSMatthias Ringwald #error "SCO data can either be routed over HCI or over PCM, but not over both. Please only enable ENABLE_SCO_OVER_HCI or HAVE_SCO_TRANSPORT." 1021e20a53eSMatthias Ringwald #endif 1031e20a53eSMatthias Ringwald 104169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 10562473419SMatthias Ringwald 10662473419SMatthias Ringwald #ifndef HCI_RESET_RESEND_TIMEOUT_MS 107659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 10862473419SMatthias Ringwald #endif 109ee091cf1Smatthias.ringwald 1101cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 1111cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 1121cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 1131cfb383eSMatthias Ringwald #endif 1141cfb383eSMatthias Ringwald 115f5875de5SMatthias Ringwald // GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested 116f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MIN 0x01 117f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MAX 0x30 118beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_IDLE 0x00 119beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W4_ACTIVE 0x80 120beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_ACTIVE 0x81 121beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W2_CANCEL 0x82 122beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W4_CANCELLED 0x83 123f5875de5SMatthias Ringwald 124b7f1ee76SMatthias Ringwald // GAP Remote Name Request 125b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_IDLE 0 126b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W2_SEND 1 127b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W4_COMPLETE 2 128b7f1ee76SMatthias Ringwald 1290a51f88bSMatthias Ringwald // GAP Pairing 1300a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_IDLE 0 1310a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN 1 1320a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE 2 1330a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY 3 1340a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE 4 1350a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION 5 1360a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE 6 1370a51f88bSMatthias Ringwald 1380a51f88bSMatthias Ringwald 139b83d5eabSMatthias Ringwald // prototypes 14035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 141758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 14235454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 14335454696SMatthias Ringwald static int hci_local_ssp_activated(void); 14435454696SMatthias Ringwald static int hci_remote_ssp_supported(hci_con_handle_t con_handle); 14567aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection); 14635454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 14735454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 148a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 14935454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 150ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 15196a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 15252db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 1539784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size); 15452db98b2SMatthias Ringwald #endif 1551cfb383eSMatthias Ringwald 1567586ee35S[email protected] static int hci_power_control_on(void); 1577586ee35S[email protected] static void hci_power_control_off(void); 1586da48142SSean Wilson static void hci_state_reset(void); 159fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void); 160fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 161b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 162b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 163b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 164b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 165b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 16695d71764SMatthias Ringwald static void hci_run(void); 16795d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 16895d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type( bd_addr_type_t address_type); 1696a5ffed8SMatthias Ringwald 1706a5ffed8SMatthias Ringwald #ifdef ENABLE_CLASSIC 171f234b250SMatthias Ringwald static int hci_have_usb_transport(void); 1726a5ffed8SMatthias Ringwald #endif 1735d509858SMatthias Ringwald 174a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 175e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 17639677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 177384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size); 178667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address); 1799c77c9dbSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void); 1805d509858SMatthias Ringwald #endif 181d70217a2SMatthias Ringwald #endif 182758b46ceSmatthias.ringwald 18306b35ec0Smatthias.ringwald // the STACK is here 1843a9fb326S[email protected] #ifndef HAVE_MALLOC 1853a9fb326S[email protected] static hci_stack_t hci_stack_static; 1863a9fb326S[email protected] #endif 1873a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 18816833f0aSmatthias.ringwald 1891c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 19063168530SMatthias Ringwald // default name 19163168530SMatthias Ringwald static const char * default_classic_name = "BTstack 00:00:00:00:00:00"; 19263168530SMatthias Ringwald 19366fb9560S[email protected] // test helper 19466fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 1951c9e5e9dSMatthias Ringwald #endif 19666fb9560S[email protected] 19796a45072S[email protected] /** 19896a45072S[email protected] * create connection for given address 19996a45072S[email protected] * 20096a45072S[email protected] * @return connection OR NULL, if no memory left 20196a45072S[email protected] */ 202667ba9d1SMatthias Ringwald static hci_connection_t * create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 2031a06f663S[email protected] log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 204bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 20596a45072S[email protected] if (!conn) return NULL; 206058e3d6bSMatthias Ringwald bd_addr_copy(conn->address, addr); 2072dceb1d6SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 20896a45072S[email protected] conn->address_type = addr_type; 20996a45072S[email protected] conn->con_handle = 0xffff; 21096a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 21196a45072S[email protected] conn->bonding_flags = 0; 21296a45072S[email protected] conn->requested_security_level = LEVEL_0; 21352db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 21488a03c8dSMatthias Ringwald conn->request_role = HCI_ROLE_INVALID; 215140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = 0xffff; 21691a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 21791a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 21896a45072S[email protected] hci_connection_timestamp(conn); 21952db98b2SMatthias Ringwald #endif 22096a45072S[email protected] conn->acl_recombination_length = 0; 22196a45072S[email protected] conn->acl_recombination_pos = 0; 222ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 223760b20efSMatthias Ringwald 224da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 225b90f6e0aSMatthias Ringwald #ifdef ENABLE_BLE 226b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = 0xff; 227b90f6e0aSMatthias Ringwald #endif 2280f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 2290f3b27c5SMatthias Ringwald conn->le_max_tx_octets = 27; 2300f3b27c5SMatthias Ringwald #endif 231665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 23296a45072S[email protected] return conn; 23396a45072S[email protected] } 23466fb9560S[email protected] 235da886c03S[email protected] 236da886c03S[email protected] /** 237da886c03S[email protected] * get le connection parameter range 238da886c03S[email protected] * 239da886c03S[email protected] * @return le connection parameter range struct 240da886c03S[email protected] */ 2414ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 2424ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 243da886c03S[email protected] } 244da886c03S[email protected] 245da886c03S[email protected] /** 246da886c03S[email protected] * set le connection parameter range 247da886c03S[email protected] * 248da886c03S[email protected] */ 249da886c03S[email protected] 2504ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 2514ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 252da886c03S[email protected] } 253da886c03S[email protected] 254da886c03S[email protected] /** 25573cd8a2aSMatthias Ringwald * @brief Test if connection parameters are inside in existing rage 25673cd8a2aSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 25773cd8a2aSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 25873cd8a2aSMatthias Ringwald * @param conn_latency 25973cd8a2aSMatthias Ringwald * @param supervision_timeout (unit: 10ms) 26073cd8a2aSMatthias Ringwald * @returns 1 if included 26173cd8a2aSMatthias Ringwald */ 26273cd8a2aSMatthias Ringwald int gap_connection_parameter_range_included(le_connection_parameter_range_t * existing_range, uint16_t le_conn_interval_min, uint16_t le_conn_interval_max, uint16_t le_conn_latency, uint16_t le_supervision_timeout){ 26373cd8a2aSMatthias Ringwald if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0; 26473cd8a2aSMatthias Ringwald if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0; 26573cd8a2aSMatthias Ringwald 26673cd8a2aSMatthias Ringwald if (le_conn_latency < existing_range->le_conn_latency_min) return 0; 26773cd8a2aSMatthias Ringwald if (le_conn_latency > existing_range->le_conn_latency_max) return 0; 26873cd8a2aSMatthias Ringwald 26973cd8a2aSMatthias Ringwald if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0; 27073cd8a2aSMatthias Ringwald if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0; 27173cd8a2aSMatthias Ringwald 27273cd8a2aSMatthias Ringwald return 1; 27373cd8a2aSMatthias Ringwald } 27473cd8a2aSMatthias Ringwald 27573cd8a2aSMatthias Ringwald /** 2762b6ab3e6SMatthias Ringwald * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 2772b6ab3e6SMatthias Ringwald * @note: default: 1 2782b6ab3e6SMatthias Ringwald * @param max_peripheral_connections 2792b6ab3e6SMatthias Ringwald */ 280d4e4907bSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 2812b6ab3e6SMatthias Ringwald void gap_set_max_number_peripheral_connections(int max_peripheral_connections){ 2822b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = max_peripheral_connections; 2832b6ab3e6SMatthias Ringwald } 284d4e4907bSMatthias Ringwald #endif 2852b6ab3e6SMatthias Ringwald 2862b6ab3e6SMatthias Ringwald /** 287da886c03S[email protected] * get hci connections iterator 288da886c03S[email protected] * 289da886c03S[email protected] * @return hci connections iterator 290da886c03S[email protected] */ 291da886c03S[email protected] 292665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 293665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 294da886c03S[email protected] } 295da886c03S[email protected] 29697addcc5Smatthias.ringwald /** 297ee091cf1Smatthias.ringwald * get connection for a given handle 298ee091cf1Smatthias.ringwald * 299ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 300ee091cf1Smatthias.ringwald */ 3015061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 302665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 303665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 304665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 305665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 3063ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 307da886c03S[email protected] return item; 308ee091cf1Smatthias.ringwald } 309ee091cf1Smatthias.ringwald } 310ee091cf1Smatthias.ringwald return NULL; 311ee091cf1Smatthias.ringwald } 312ee091cf1Smatthias.ringwald 31396a45072S[email protected] /** 31496a45072S[email protected] * get connection for given address 31596a45072S[email protected] * 31696a45072S[email protected] * @return connection OR NULL, if not found 31796a45072S[email protected] */ 318667ba9d1SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 319665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 320665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 321665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 322665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 32396a45072S[email protected] if (connection->address_type != addr_type) continue; 32496a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 32562bda3fbS[email protected] return connection; 32662bda3fbS[email protected] } 32762bda3fbS[email protected] return NULL; 32862bda3fbS[email protected] } 32962bda3fbS[email protected] 3303e5e0926SMatthias Ringwald inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 3313e5e0926SMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 3323e5e0926SMatthias Ringwald } 33352db98b2SMatthias Ringwald 334228e430cSMatthias Ringwald inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 335228e430cSMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 336228e430cSMatthias Ringwald } 337228e430cSMatthias Ringwald 33852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 33952db98b2SMatthias Ringwald 340ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 341ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 342ee752bb8SMatthias Ringwald int connections = 0; 343ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 344ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 345ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 346ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 347ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 348ee752bb8SMatthias Ringwald connections++; 349ee752bb8SMatthias Ringwald } 350ee752bb8SMatthias Ringwald return connections; 351ee752bb8SMatthias Ringwald } 352ee752bb8SMatthias Ringwald #endif 353ee752bb8SMatthias Ringwald 354ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 35591a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 356aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 357528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 358c785ef68Smatthias.ringwald // connections might be timed out 359c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 360c785ef68Smatthias.ringwald } 361f316a845SMatthias Ringwald #else 362c1ab6cc1SMatthias Ringwald if (btstack_run_loop_get_time_ms() > (connection->timestamp + HCI_CONNECTION_TIMEOUT_MS)){ 3635f26aadcSMatthias Ringwald // connections might be timed out 3645f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 3655f26aadcSMatthias Ringwald } 3665f26aadcSMatthias Ringwald #endif 367c785ef68Smatthias.ringwald } 368ee091cf1Smatthias.ringwald 369ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 370aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 371528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 372f316a845SMatthias Ringwald #else 373528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 3745f26aadcSMatthias Ringwald #endif 375ee091cf1Smatthias.ringwald } 376ee091cf1Smatthias.ringwald 37743bfb1bdSmatthias.ringwald /** 37880ca58a0Smatthias.ringwald * add authentication flags and reset timer 37996a45072S[email protected] * @note: assumes classic connection 3802e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 3817fde4af9Smatthias.ringwald */ 3827fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 3837fde4af9Smatthias.ringwald bd_addr_t addr; 384724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 385f16129ceSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3867fde4af9Smatthias.ringwald if (conn) { 38728ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 38880ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 3897fde4af9Smatthias.ringwald } 3907fde4af9Smatthias.ringwald } 3917fde4af9Smatthias.ringwald 39280ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 3935061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 39480ca58a0Smatthias.ringwald if (!conn) return 0; 3956724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 3966724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 3976724cd9eS[email protected] return 0; 39880ca58a0Smatthias.ringwald } 39980ca58a0Smatthias.ringwald 40015a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 40155597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 4022bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 403a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 404c12e46e7Smatthias.ringwald } 40555597469SMatthias Ringwald 40655597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 40755597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 4082bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 40955597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 410c12e46e7Smatthias.ringwald } 4111b6fb31bSMatthias Ringwald 412e8ad470fSMatthias Ringwald bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type){ 413e8ad470fSMatthias Ringwald if (!hci_stack->link_key_db) return false; 41476b0318eSMatthias Ringwald int result = hci_stack->link_key_db->get_link_key(addr, link_key, type) != 0; 41576b0318eSMatthias Ringwald log_info("link key for %s available %u, type %u", bd_addr_to_str(addr), result, (int) *type); 41676b0318eSMatthias Ringwald return result; 417e8ad470fSMatthias Ringwald } 418e8ad470fSMatthias Ringwald 419ceecb9d9SMatthias Ringwald void gap_delete_all_link_keys(void){ 420ceecb9d9SMatthias Ringwald bd_addr_t addr; 421ceecb9d9SMatthias Ringwald link_key_t link_key; 422ceecb9d9SMatthias Ringwald link_key_type_t type; 423ceecb9d9SMatthias Ringwald btstack_link_key_iterator_t it; 424ceecb9d9SMatthias Ringwald int ok = gap_link_key_iterator_init(&it); 425ceecb9d9SMatthias Ringwald if (!ok) { 426ceecb9d9SMatthias Ringwald log_error("could not initialize iterator"); 427ceecb9d9SMatthias Ringwald return; 428ceecb9d9SMatthias Ringwald } 429ceecb9d9SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 430ceecb9d9SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 431ceecb9d9SMatthias Ringwald } 432ceecb9d9SMatthias Ringwald gap_link_key_iterator_done(&it); 433ceecb9d9SMatthias Ringwald } 434ceecb9d9SMatthias Ringwald 4351b6fb31bSMatthias Ringwald int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){ 4361b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 4371b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db->iterator_init) return 0; 4381b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_init(it); 4391b6fb31bSMatthias Ringwald } 4401b6fb31bSMatthias Ringwald int gap_link_key_iterator_get_next(btstack_link_key_iterator_t * it, bd_addr_t bd_addr, link_key_t link_key, link_key_type_t * type){ 4411b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 4421b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type); 4431b6fb31bSMatthias Ringwald } 4441b6fb31bSMatthias Ringwald void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){ 4451b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return; 4461b6fb31bSMatthias Ringwald hci_stack->link_key_db->iterator_done(it); 4471b6fb31bSMatthias Ringwald } 44835454696SMatthias Ringwald #endif 449c12e46e7Smatthias.ringwald 450eb8076ddSMatthias Ringwald static bool hci_is_le_connection_type(bd_addr_type_t address_type){ 451eb8076ddSMatthias Ringwald switch (address_type){ 452ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 453ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 454ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_PUBLIC: 455ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_RANDOM: 456eb8076ddSMatthias Ringwald return true; 457ce41473eSMatthias Ringwald default: 458eb8076ddSMatthias Ringwald return false; 459ce41473eSMatthias Ringwald } 4600bf6344aS[email protected] } 4610bf6344aS[email protected] 462eb8076ddSMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 463eb8076ddSMatthias Ringwald return hci_is_le_connection_type(connection->address_type); 464eb8076ddSMatthias Ringwald } 465eb8076ddSMatthias Ringwald 4667fde4af9Smatthias.ringwald /** 46743bfb1bdSmatthias.ringwald * count connections 46843bfb1bdSmatthias.ringwald */ 46940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 47056c253c9Smatthias.ringwald int count = 0; 471665d90f2SMatthias Ringwald btstack_linked_item_t *it; 472a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL ; it = it->next){ 47315a27967SMatthias Ringwald count++; 47415a27967SMatthias Ringwald } 47543bfb1bdSmatthias.ringwald return count; 47643bfb1bdSmatthias.ringwald } 477c8e4258aSmatthias.ringwald 47895d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 479ee303eddS[email protected] 480f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 481f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 482ee303eddS[email protected] 483665d90f2SMatthias Ringwald btstack_linked_item_t *it; 484a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 485998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 486ce41473eSMatthias Ringwald if (hci_is_le_connection(connection)){ 487ce41473eSMatthias Ringwald num_packets_sent_le += connection->num_packets_sent; 488ce41473eSMatthias Ringwald } 489f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 490ce41473eSMatthias Ringwald num_packets_sent_classic += connection->num_packets_sent; 491ee303eddS[email protected] } 492ee303eddS[email protected] } 493d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 494ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 495ee303eddS[email protected] int free_slots_le = 0; 496ee303eddS[email protected] 497ee303eddS[email protected] if (free_slots_classic < 0){ 4989da54300S[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); 499998906cdSmatthias.ringwald return 0; 500998906cdSmatthias.ringwald } 501ee303eddS[email protected] 502ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 503ee303eddS[email protected] // if we have LE slots, they are used 504ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 505ee303eddS[email protected] if (free_slots_le < 0){ 5069da54300S[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); 507ee303eddS[email protected] return 0; 508998906cdSmatthias.ringwald } 509ee303eddS[email protected] } else { 510ee303eddS[email protected] // otherwise, classic slots are used for LE, too 511ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 512ee303eddS[email protected] if (free_slots_classic < 0){ 5139da54300S[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); 514ee303eddS[email protected] return 0; 515ee303eddS[email protected] } 516ee303eddS[email protected] } 517ee303eddS[email protected] 518ee303eddS[email protected] switch (address_type){ 519ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 5202125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 521ee303eddS[email protected] return 0; 522ee303eddS[email protected] 523f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 524ee303eddS[email protected] return free_slots_classic; 525ee303eddS[email protected] 526ee303eddS[email protected] default: 527cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 528ee303eddS[email protected] return free_slots_le; 529ee303eddS[email protected] } 530cb00d3aaS[email protected] return free_slots_classic; 531cb00d3aaS[email protected] } 532998906cdSmatthias.ringwald } 533998906cdSmatthias.ringwald 5342125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 5352125de09SMatthias Ringwald // get connection type 5362125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5372125de09SMatthias Ringwald if (!connection){ 5382125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 5392125de09SMatthias Ringwald return 0; 5402125de09SMatthias Ringwald } 5412125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 5422125de09SMatthias Ringwald } 5432125de09SMatthias Ringwald 54435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 545701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 546f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 547665d90f2SMatthias Ringwald btstack_linked_item_t *it; 548760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 549760b20efSMatthias Ringwald // explicit flow control 550665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 551e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 552ce41473eSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 553ce41473eSMatthias Ringwald num_sco_packets_sent += connection->num_packets_sent; 554e35edcc1S[email protected] } 555e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 556701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 55744d0e3d5S[email protected] return 0; 55844d0e3d5S[email protected] } 559e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 56049205f5dSMatthias Ringwald } else { 56149205f5dSMatthias Ringwald // implicit flow control -- TODO 5626f28d2eeSMatthias Ringwald int num_ready = 0; 5636f28d2eeSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 5646f28d2eeSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 5656f28d2eeSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 5666f28d2eeSMatthias Ringwald if (connection->sco_tx_ready == 0) continue; 5676f28d2eeSMatthias Ringwald num_ready++; 56849205f5dSMatthias Ringwald } 5696f28d2eeSMatthias Ringwald return num_ready; 5706f28d2eeSMatthias Ringwald } 571e35edcc1S[email protected] } 57235454696SMatthias Ringwald #endif 57344d0e3d5S[email protected] 5742b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 5752b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 576ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 577ac928cc2S[email protected] 578ac928cc2S[email protected] // check for async hci transport implementations 579ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 580ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 581ac928cc2S[email protected] return 0; 582ac928cc2S[email protected] } 583ac928cc2S[email protected] } 5842b838201SMatthias Ringwald return 1; 5852b838201SMatthias Ringwald } 586ac928cc2S[email protected] 5872b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 5882b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 5892b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 5904ea43905SMatthias Ringwald return hci_stack->num_cmd_packets > 0u; 591ac928cc2S[email protected] } 592ac928cc2S[email protected] 5939d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 594ac928cc2S[email protected] // check for async hci transport implementations 5959d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 5969d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 597ac928cc2S[email protected] } 5989d04d3a7SMatthias Ringwald 5999d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 6009d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 6019d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 602ac928cc2S[email protected] } 6039d04d3a7SMatthias Ringwald 6049d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 6059d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 6069d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 6079d04d3a7SMatthias Ringwald } 6089d04d3a7SMatthias Ringwald 6099d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 6109d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 611e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 612ac928cc2S[email protected] } 613ac928cc2S[email protected] 614ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 615ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 616ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 6176b4af23dS[email protected] } 6186b4af23dS[email protected] 61935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 62035454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 62135454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 622f16129ceSMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_ACL); 62335454696SMatthias Ringwald } 62435454696SMatthias Ringwald 625701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 626d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 627f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 628f234b250SMatthias Ringwald return hci_stack->sco_can_send_now; 629f234b250SMatthias Ringwald } else { 630701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 63144d0e3d5S[email protected] } 632f234b250SMatthias Ringwald } 63344d0e3d5S[email protected] 634701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 635d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 636701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 63744d0e3d5S[email protected] } 63844d0e3d5S[email protected] 639d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 640d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 641d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 642d057580eSMatthias Ringwald } 64335454696SMatthias Ringwald #endif 644d057580eSMatthias Ringwald 64595d71764SMatthias Ringwald // used for internal checks in l2cap.c 646c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 647c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 648c8b9416aS[email protected] } 649c8b9416aS[email protected] 6506b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 6516b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 6529d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 6539d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 6549d14b626S[email protected] return 0; 6559d14b626S[email protected] } 6566b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 6576b4af23dS[email protected] return 1; 6586b4af23dS[email protected] } 6596b4af23dS[email protected] 66068a0fcf7S[email protected] void hci_release_packet_buffer(void){ 66168a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 66268a0fcf7S[email protected] } 66368a0fcf7S[email protected] 6646b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 6657f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 6666b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 6676b4af23dS[email protected] } 6686b4af23dS[email protected] 669452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 670452cf3bbS[email protected] 671452cf3bbS[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); 672452cf3bbS[email protected] 673452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 674452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 6754ea43905SMatthias Ringwald if (hci_is_le_connection(connection) && (hci_stack->le_data_packets_length > 0u)){ 676452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 677452cf3bbS[email protected] } 678452cf3bbS[email protected] 6790f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 680fcf88f47SMatthias Ringwald if (hci_is_le_connection(connection) && (connection->le_max_tx_octets < max_acl_data_packet_length)){ 6810f3b27c5SMatthias Ringwald max_acl_data_packet_length = connection->le_max_tx_octets; 6820f3b27c5SMatthias Ringwald } 6830f3b27c5SMatthias Ringwald #endif 684452cf3bbS[email protected] 685d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 686d999b54eSMatthias Ringwald 687452cf3bbS[email protected] int err; 688452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 689ff3cc4a5SMatthias Ringwald while (true){ 690452cf3bbS[email protected] 691d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 692d999b54eSMatthias Ringwald 693452cf3bbS[email protected] // get current data 6944ea43905SMatthias Ringwald const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u; 695452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 6961979f09cSMatthias Ringwald bool more_fragments = false; 697452cf3bbS[email protected] 698452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 699452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 7001979f09cSMatthias Ringwald more_fragments = true; 701452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 702452cf3bbS[email protected] } 703452cf3bbS[email protected] 704452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 7054ea43905SMatthias Ringwald if (acl_header_pos > 0u){ 706f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 7074ea43905SMatthias Ringwald handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u); 708f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 709452cf3bbS[email protected] } 710452cf3bbS[email protected] 711452cf3bbS[email protected] // update header len 7124ea43905SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length); 713452cf3bbS[email protected] 714452cf3bbS[email protected] // count packet 715ce41473eSMatthias Ringwald connection->num_packets_sent++; 7161979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments); 717d999b54eSMatthias Ringwald 718d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 719d999b54eSMatthias Ringwald if (more_fragments){ 720d999b54eSMatthias Ringwald // update start of next fragment to send 721d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 722d999b54eSMatthias Ringwald } else { 723d999b54eSMatthias Ringwald // done 724d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 725d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 726d999b54eSMatthias Ringwald } 727452cf3bbS[email protected] 728452cf3bbS[email protected] // send packet 729452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 730452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 7315bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 73281d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 1; 733452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 734452cf3bbS[email protected] 7351979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments); 736d999b54eSMatthias Ringwald 737452cf3bbS[email protected] // done yet? 738452cf3bbS[email protected] if (!more_fragments) break; 739452cf3bbS[email protected] 740452cf3bbS[email protected] // can send more? 741452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 742452cf3bbS[email protected] } 743452cf3bbS[email protected] 744d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 745452cf3bbS[email protected] 746d051460cS[email protected] // release buffer now for synchronous transport 747203bace6S[email protected] if (hci_transport_synchronous()){ 74881d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 749452cf3bbS[email protected] hci_release_packet_buffer(); 750fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 751452cf3bbS[email protected] } 752452cf3bbS[email protected] 753452cf3bbS[email protected] return err; 754452cf3bbS[email protected] } 755452cf3bbS[email protected] 756826f7347S[email protected] // pre: caller has reserved the packet buffer 757826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 7587856c818Smatthias.ringwald 759452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 760452cf3bbS[email protected] 761826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 762826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 763826f7347S[email protected] return 0; 764826f7347S[email protected] } 765826f7347S[email protected] 766d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 767d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 768d713a683S[email protected] 769826f7347S[email protected] // check for free places on Bluetooth module 770d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 771826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 77297b61c7bS[email protected] hci_release_packet_buffer(); 773068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 77497b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 77597b61c7bS[email protected] } 7766218e6f1Smatthias.ringwald 7775061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 77897b61c7bS[email protected] if (!connection) { 7795fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 78097b61c7bS[email protected] hci_release_packet_buffer(); 781068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 78297b61c7bS[email protected] return 0; 78397b61c7bS[email protected] } 78452db98b2SMatthias Ringwald 78552db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 78656cf178bSmatthias.ringwald hci_connection_timestamp(connection); 78752db98b2SMatthias Ringwald #endif 78856cf178bSmatthias.ringwald 789452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 7907856c818Smatthias.ringwald 791452cf3bbS[email protected] // setup data 792452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 793452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 7946218e6f1Smatthias.ringwald 795452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 796ee091cf1Smatthias.ringwald } 797ee091cf1Smatthias.ringwald 79835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 79944d0e3d5S[email protected] // pre: caller has reserved the packet buffer 80044d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 80144d0e3d5S[email protected] 80244d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 80344d0e3d5S[email protected] 80444d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 80544d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 80644d0e3d5S[email protected] return 0; 80744d0e3d5S[email protected] } 80844d0e3d5S[email protected] 80944d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 8104b3e1e19SMatthias Ringwald 8114b3e1e19SMatthias Ringwald // skip checks in loopback mode 8124b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 81344d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 81444d0e3d5S[email protected] 81544d0e3d5S[email protected] // check for free places on Bluetooth module 816701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 817cbf638a9SMatthias Ringwald log_error("hci_send_sco_packet_buffer called but no free SCO buffers on controller"); 81844d0e3d5S[email protected] hci_release_packet_buffer(); 819068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 82044d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 82144d0e3d5S[email protected] } 82244d0e3d5S[email protected] 823e35edcc1S[email protected] // track send packet in connection struct 824e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 825e35edcc1S[email protected] if (!connection) { 826e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 827e35edcc1S[email protected] hci_release_packet_buffer(); 828068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 829e35edcc1S[email protected] return 0; 830e35edcc1S[email protected] } 831f234b250SMatthias Ringwald 832f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 833f234b250SMatthias Ringwald // token used 834f234b250SMatthias Ringwald hci_stack->sco_can_send_now = 0; 835f234b250SMatthias Ringwald } else { 836760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 837ce41473eSMatthias Ringwald connection->num_packets_sent++; 8386f28d2eeSMatthias Ringwald } else { 839e4157653SMatthias Ringwald connection->sco_tx_ready--; 840760b20efSMatthias Ringwald } 8414b3e1e19SMatthias Ringwald } 842f234b250SMatthias Ringwald } 84344d0e3d5S[email protected] 84444d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 845543e835cSMatthias Ringwald 84643149fc9SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 84743149fc9SMatthias Ringwald hci_stack->sco_transport->send_packet(packet, size); 84843149fc9SMatthias Ringwald hci_release_packet_buffer(); 84943149fc9SMatthias Ringwald hci_emit_transport_packet_sent(); 85043149fc9SMatthias Ringwald 85143149fc9SMatthias Ringwald return 0; 85243149fc9SMatthias Ringwald #else 85343149fc9SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 854543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 855543e835cSMatthias Ringwald hci_release_packet_buffer(); 856fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 857543e835cSMatthias Ringwald } 858543e835cSMatthias Ringwald 859543e835cSMatthias Ringwald return err; 86043149fc9SMatthias Ringwald #endif 86144d0e3d5S[email protected] } 86235454696SMatthias Ringwald #endif 86344d0e3d5S[email protected] 864c3b46f5aSMatthias Ringwald static void acl_handler(uint8_t *packet, uint16_t size){ 865e76a89eeS[email protected] 8667856c818Smatthias.ringwald // get info 8677856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 8685061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 8697856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 8707856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 8717856c818Smatthias.ringwald 8727856c818Smatthias.ringwald // ignore non-registered handle 8737856c818Smatthias.ringwald if (!conn){ 874c3b46f5aSMatthias Ringwald log_error("acl_handler called with non-registered handle %u!" , con_handle); 8757856c818Smatthias.ringwald return; 8767856c818Smatthias.ringwald } 8777856c818Smatthias.ringwald 878e76a89eeS[email protected] // assert packet is complete 8794ea43905SMatthias Ringwald if ((acl_length + 4u) != size){ 880c3b46f5aSMatthias Ringwald log_error("acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 881e76a89eeS[email protected] return; 882e76a89eeS[email protected] } 883e76a89eeS[email protected] 88452db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 8857856c818Smatthias.ringwald // update idle timestamp 8867856c818Smatthias.ringwald hci_connection_timestamp(conn); 88752db98b2SMatthias Ringwald #endif 8887856c818Smatthias.ringwald 8892b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 8902b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 8912b838201SMatthias Ringwald conn->num_packets_completed++; 8922b838201SMatthias Ringwald #endif 8932b838201SMatthias Ringwald 8947856c818Smatthias.ringwald // handle different packet types 8954ea43905SMatthias Ringwald switch (acl_flags & 0x03u) { 8967856c818Smatthias.ringwald 8977856c818Smatthias.ringwald case 0x01: // continuation fragment 8987856c818Smatthias.ringwald 8990ca847afS[email protected] // sanity checks 9004ea43905SMatthias Ringwald if (conn->acl_recombination_pos == 0u) { 9019da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 9027856c818Smatthias.ringwald return; 9037856c818Smatthias.ringwald } 9044ea43905SMatthias Ringwald if ((conn->acl_recombination_pos + acl_length) > (4u + HCI_ACL_BUFFER_SIZE)){ 9050ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 9060ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 9070ca847afS[email protected] conn->acl_recombination_pos = 0; 9080ca847afS[email protected] return; 9090ca847afS[email protected] } 9107856c818Smatthias.ringwald 9117856c818Smatthias.ringwald // append fragment payload (header already stored) 9126535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], 9136535961aSMatthias Ringwald &packet[4], acl_length); 9147856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 9157856c818Smatthias.ringwald 9167856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 9174ea43905SMatthias Ringwald if (conn->acl_recombination_pos >= (conn->acl_recombination_length + 4u + 4u)){ // pos already incl. ACL header 918d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 9197856c818Smatthias.ringwald // reset recombination buffer 9207856c818Smatthias.ringwald conn->acl_recombination_length = 0; 9217856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 9227856c818Smatthias.ringwald } 9237856c818Smatthias.ringwald break; 9247856c818Smatthias.ringwald 9257856c818Smatthias.ringwald case 0x02: { // first fragment 9267856c818Smatthias.ringwald 92723a77e1aS[email protected] // sanity check 92823a77e1aS[email protected] if (conn->acl_recombination_pos) { 92923a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 93023a77e1aS[email protected] conn->acl_recombination_pos = 0; 93123a77e1aS[email protected] } 93223a77e1aS[email protected] 9337856c818Smatthias.ringwald // peek into L2CAP packet! 9347856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 9357856c818Smatthias.ringwald 9367856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 9374ea43905SMatthias Ringwald if (acl_length >= (l2cap_length + 4u)){ 9387856c818Smatthias.ringwald // forward fragment as L2CAP packet 9394ea43905SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4u); 9407856c818Smatthias.ringwald } else { 9410ca847afS[email protected] 9420ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 9430ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 9440ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 9450ca847afS[email protected] return; 9460ca847afS[email protected] } 9470ca847afS[email protected] 9487856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 9496535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 9504ea43905SMatthias Ringwald packet, acl_length + 4u); 9514ea43905SMatthias Ringwald conn->acl_recombination_pos = acl_length + 4u; 9527856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 9534ea43905SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2u, l2cap_length +4u); 9547856c818Smatthias.ringwald } 9557856c818Smatthias.ringwald break; 9567856c818Smatthias.ringwald 9577856c818Smatthias.ringwald } 9587856c818Smatthias.ringwald default: 959c3b46f5aSMatthias Ringwald log_error( "acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 9607856c818Smatthias.ringwald return; 9617856c818Smatthias.ringwald } 96294ab26f8Smatthias.ringwald 96394ab26f8Smatthias.ringwald // execute main loop 96494ab26f8Smatthias.ringwald hci_run(); 96516833f0aSmatthias.ringwald } 96622909952Smatthias.ringwald 96767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 9689da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 9693c4d4b90Smatthias.ringwald 970b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 971cb70c5abSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(HAVE_SCO_TRANSPORT) 972cb70c5abSMatthias Ringwald bd_addr_type_t addr_type = conn->address_type; 973cb70c5abSMatthias Ringwald #endif 974cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 975cb70c5abSMatthias Ringwald hci_con_handle_t con_handle = conn->con_handle; 976ee752bb8SMatthias Ringwald #endif 977b3264428SMatthias Ringwald #endif 978ee752bb8SMatthias Ringwald 979528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 980c785ef68Smatthias.ringwald 981665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 982a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 9833c4d4b90Smatthias.ringwald 9843c4d4b90Smatthias.ringwald // now it's gone 985c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 986ee752bb8SMatthias Ringwald 987b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 988034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 989ee752bb8SMatthias Ringwald // update SCO 990cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->hci_transport != NULL) && (hci_stack->hci_transport->set_sco_config != NULL)){ 991ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 992ee752bb8SMatthias Ringwald } 993034e9b53SMatthias Ringwald #endif 994cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 995cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->sco_transport != NULL)){ 996cb70c5abSMatthias Ringwald hci_stack->sco_transport->close(con_handle); 997cb70c5abSMatthias Ringwald } 998cb70c5abSMatthias Ringwald #endif 999b3264428SMatthias Ringwald #endif 1000c7e0c5f6Smatthias.ringwald } 1001c7e0c5f6Smatthias.ringwald 100235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 100335454696SMatthias Ringwald 10040c042179S[email protected] static const uint16_t packet_type_sizes[] = { 10058f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 10068f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 10078f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 10088f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 10098f8108aaSmatthias.ringwald }; 101065389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 101165389bfcS[email protected] 0, // 3 slot packets 101265389bfcS[email protected] 1, // 5 slot packets 101365389bfcS[email protected] 25, // EDR 2 mpbs 101465389bfcS[email protected] 26, // EDR 3 mbps 101565389bfcS[email protected] 39, // 3 slot EDR packts 101665389bfcS[email protected] 40, // 5 slot EDR packet 101765389bfcS[email protected] }; 101865389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 101965389bfcS[email protected] 0x0f00, // 3 slot packets 102065389bfcS[email protected] 0xf000, // 5 slot packets 102165389bfcS[email protected] 0x1102, // EDR 2 mpbs 102265389bfcS[email protected] 0x2204, // EDR 3 mbps 102365389bfcS[email protected] 0x0300, // 3 slot EDR packts 102465389bfcS[email protected] 0x3000, // 5 slot EDR packet 102565389bfcS[email protected] }; 10268f8108aaSmatthias.ringwald 102765389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 102865389bfcS[email protected] // enable packet types based on size 10298f8108aaSmatthias.ringwald uint16_t packet_types = 0; 1030f16a69bbS[email protected] unsigned int i; 10318f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 10328f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 10338f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 10348f8108aaSmatthias.ringwald packet_types |= 1 << i; 10358f8108aaSmatthias.ringwald } 10368f8108aaSmatthias.ringwald } 103765389bfcS[email protected] // disable packet types due to missing local supported features 103865389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 1039f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 104065389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 104165389bfcS[email protected] if (feature_set) continue; 104265389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 104365389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 104465389bfcS[email protected] } 10458f8108aaSmatthias.ringwald // flip bits for "may not be used" 10468f8108aaSmatthias.ringwald packet_types ^= 0x3306; 10478f8108aaSmatthias.ringwald return packet_types; 10488f8108aaSmatthias.ringwald } 10498f8108aaSmatthias.ringwald 10508f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 10513a9fb326S[email protected] return hci_stack->packet_types; 10528f8108aaSmatthias.ringwald } 105335454696SMatthias Ringwald #endif 10548f8108aaSmatthias.ringwald 1055facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 10567dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 10573a9fb326S[email protected] return hci_stack->hci_packet_buffer; 10587dc17943Smatthias.ringwald } 10597dc17943Smatthias.ringwald 1060f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 10613a9fb326S[email protected] return hci_stack->acl_data_packet_length; 10627dc17943Smatthias.ringwald } 10637dc17943Smatthias.ringwald 106406b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 10653e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 10663e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 10673e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 10683e68d23dSMatthias Ringwald } 106906b9e820SMatthias Ringwald #endif 10703e68d23dSMatthias Ringwald 10716ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 10726ac9a97eS[email protected] // No. 54, byte 6, bit 6 10734ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 6u)) != 0u; 10746ac9a97eS[email protected] } 10756ac9a97eS[email protected] 107615a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 10776ac9a97eS[email protected] // No. 51, byte 6, bit 3 10784ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 3u)) != 0u; 1079f5d8d141S[email protected] } 1080f5d8d141S[email protected] 10817f02f414SMatthias Ringwald static int hci_classic_supported(void){ 108235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 10836ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 10843a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 108535454696SMatthias Ringwald #else 108635454696SMatthias Ringwald return 0; 108735454696SMatthias Ringwald #endif 1088f5d8d141S[email protected] } 1089f5d8d141S[email protected] 10907f02f414SMatthias Ringwald static int hci_le_supported(void){ 1091a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 10926ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 10934ea43905SMatthias Ringwald return (hci_stack->local_supported_features[4u] & (1u << 6u)) != 0u; 1094f5d8d141S[email protected] #else 1095f5d8d141S[email protected] return 0; 1096f5d8d141S[email protected] #endif 1097f5d8d141S[email protected] } 1098f5d8d141S[email protected] 1099b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 1100b95a5a35SMatthias Ringwald 1101b95a5a35SMatthias Ringwald /** 1102b95a5a35SMatthias Ringwald * @brief Get addr type and address used for LE in Advertisements, Scan Responses, 1103b95a5a35SMatthias Ringwald */ 1104b95a5a35SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 1105b95a5a35SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 1106b95a5a35SMatthias Ringwald if (hci_stack->le_own_addr_type){ 11076535961aSMatthias Ringwald (void)memcpy(addr, hci_stack->le_random_address, 6); 110869a97523S[email protected] } else { 11096535961aSMatthias Ringwald (void)memcpy(addr, hci_stack->local_bd_addr, 6); 111069a97523S[email protected] } 111169a97523S[email protected] } 111269a97523S[email protected] 1113e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1114384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size){ 11159ec2630cSMatthias Ringwald 1116d1dc057bS[email protected] int offset = 3; 1117d1dc057bS[email protected] int num_reports = packet[offset]; 1118d1dc057bS[email protected] offset += 1; 1119d1dc057bS[email protected] 112057c9da5bS[email protected] int i; 11214f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 112203fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1123a1df452eSMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 112433e6948bSMatthias Ringwald // sanity checks on data_length: 112533e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 112633e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 11274ea43905SMatthias Ringwald if ((offset + 9u + data_length + 1u) > size) return; 112833e6948bSMatthias Ringwald // setup event 11294ea43905SMatthias Ringwald uint8_t event_size = 10u + data_length; 1130d1dc057bS[email protected] int pos = 0; 1131045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 113257c9da5bS[email protected] event[pos++] = event_size; 11336535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address 1134d1dc057bS[email protected] offset += 8; 1135d1dc057bS[email protected] pos += 8; 1136d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 113733e6948bSMatthias Ringwald event[pos++] = data_length; 113833e6948bSMatthias Ringwald offset++; 11396535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], data_length); 114057c9da5bS[email protected] pos += data_length; 11414ea43905SMatthias Ringwald offset += data_length + 1u; // rssi 1142d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 114357c9da5bS[email protected] } 114457c9da5bS[email protected] } 1145b2f949feS[email protected] #endif 1146e8c8828eSMatthias Ringwald #endif 114757c9da5bS[email protected] 11482b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 11492b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1150bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1151bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_enabled){ 11522b6ab3e6SMatthias Ringwald // get number of active le slave connections 11532b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 11542b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 11552b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 11562b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11572b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 11582b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 11592b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 11602b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 11612b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 11622b6ab3e6SMatthias Ringwald num_slave_connections++; 11632b6ab3e6SMatthias Ringwald } 11642b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1165bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1166bbc366e6SMatthias Ringwald } else { 1167bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 11682b6ab3e6SMatthias Ringwald } 11692b6ab3e6SMatthias Ringwald } 11702b6ab3e6SMatthias Ringwald #endif 11712b6ab3e6SMatthias Ringwald #endif 11722b6ab3e6SMatthias Ringwald 1173cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 117406b9e820SMatthias Ringwald 117596b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 117696b53536SMatthias Ringwald if (!hci_stack->config) return 0; 11779796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 117896b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1179a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 118096b53536SMatthias Ringwald baud_rate = 3000000; 118196b53536SMatthias Ringwald } 118296b53536SMatthias Ringwald return baud_rate; 118396b53536SMatthias Ringwald } 118496b53536SMatthias Ringwald 1185ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 11869ec2630cSMatthias Ringwald UNUSED(ds); 11879ec2630cSMatthias Ringwald 11880305bdeaSMatthias Ringwald switch (hci_stack->substate){ 11890305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 11907b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 11910305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 11927b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 11930305bdeaSMatthias Ringwald hci_run(); 11940305bdeaSMatthias Ringwald break; 11959f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 11969f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 11979f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 11989f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 11999f007422SMatthias Ringwald } 1200cf373d3aSMatthias Ringwald 1201cf373d3aSMatthias Ringwald /* fall through */ 1202cf373d3aSMatthias Ringwald 1203e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1204e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1205e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1206e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1207e47e68c7SMatthias Ringwald hci_run(); 1208688c2635SMatthias Ringwald break; 12097224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 12107224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 121196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1212cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 12137dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 12147224be7eSMatthias Ringwald } 1215834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 121661f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1217834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1218834bce8cSMatthias Ringwald log_info("Link Reset"); 1219834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1220834bce8cSMatthias Ringwald } 1221772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1222772a36d3SMatthias Ringwald hci_run(); 1223772a36d3SMatthias Ringwald } 12244696bddbSMatthias Ringwald break; 1225559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1226559961d0SMatthias Ringwald // otherwise continue 1227559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1228559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1229559961d0SMatthias Ringwald break; 12300305bdeaSMatthias Ringwald default: 12310305bdeaSMatthias Ringwald break; 12320305bdeaSMatthias Ringwald } 12330305bdeaSMatthias Ringwald } 123406b9e820SMatthias Ringwald #endif 12350305bdeaSMatthias Ringwald 123671de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 123774b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 123874b323a9SMatthias Ringwald } 123974b323a9SMatthias Ringwald 124074b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 124171de195eSMatthias Ringwald static void hci_initializing_run(void){ 1242148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 124374b323a9SMatthias Ringwald switch (hci_stack->substate){ 124474b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 124574b323a9SMatthias Ringwald hci_state_reset(); 1246a0cf2f3fSMatthias Ringwald 124706b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 12480305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1249659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1250528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1251528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1252a0cf2f3fSMatthias Ringwald #endif 12530305bdeaSMatthias Ringwald // send command 125474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 12550305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 125674b323a9SMatthias Ringwald break; 125776fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 125876fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 125976fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 126076fcb19bSMatthias Ringwald break; 1261e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1262e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1263e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1264e90bae01SMatthias Ringwald break; 126506b9e820SMatthias Ringwald 126606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1267e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1268e47e68c7SMatthias Ringwald hci_state_reset(); 1269e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1270659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1271528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1272528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1273e47e68c7SMatthias Ringwald // send command 1274e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1275e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1276e47e68c7SMatthias Ringwald break; 12778d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 12788d29070eSMatthias Ringwald hci_state_reset(); 12798d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 12808d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 12818d29070eSMatthias Ringwald break; 1282fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 128396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 12843fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1285f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 128674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 12874ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 12884696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 12894696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 129061f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1291659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1292528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 12934696bddbSMatthias Ringwald } 129474b323a9SMatthias Ringwald break; 1295fab26ab3SMatthias Ringwald } 1296fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 129796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 12983fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1299f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1300eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 13014ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1302eb3a5314SMatthias Ringwald break; 1303fab26ab3SMatthias Ringwald } 130474b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 130574b323a9SMatthias Ringwald // Custom initialization 13063fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1307ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 13081979f09cSMatthias Ringwald bool send_cmd = false; 1309ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1310f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 13111979f09cSMatthias Ringwald send_cmd = true; 1312e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1313e47e68c7SMatthias Ringwald break; 1314f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 13151979f09cSMatthias Ringwald send_cmd = true; 1316f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1317e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1318659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1319528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1320528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1321a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1322772a36d3SMatthias Ringwald && hci_stack->config 13233fb36a29SMatthias Ringwald && hci_stack->chipset 13243fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1325772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 13262caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1327772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1328772a36d3SMatthias Ringwald } else { 13299f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1330772a36d3SMatthias Ringwald } 1331e47e68c7SMatthias Ringwald break; 1332f41911edSMatthias Ringwald default: 1333f41911edSMatthias Ringwald break; 1334e47e68c7SMatthias Ringwald } 1335ee720f3aSMatthias Ringwald 1336ee720f3aSMatthias Ringwald if (send_cmd){ 13374ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1338ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1339ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 13400305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 134174b323a9SMatthias Ringwald break; 134274b323a9SMatthias Ringwald } 1343148ca237SMatthias Ringwald log_info("Init script done"); 134492a0d36dSMatthias Ringwald 1345559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1346ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1347a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1348a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1349e021ff1eSMatthias Ringwald 1350559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 135192a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 13523fb36a29SMatthias Ringwald && hci_stack->chipset 13533fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 135492a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 13559796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 135692a0d36dSMatthias Ringwald if (need_baud_change) { 13579796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1358cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 135992a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 136092a0d36dSMatthias Ringwald } 1361559961d0SMatthias Ringwald 1362f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1363f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1364f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1365f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1366559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1367f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1368559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1369559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1370559961d0SMatthias Ringwald break; 137192a0d36dSMatthias Ringwald } 137274b323a9SMatthias Ringwald } 137374b323a9SMatthias Ringwald // otherwise continue 1374a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1375a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1376a828a756SMatthias Ringwald break; 137753860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 137853860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 13793fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1380f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 138153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 13824ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 138353860077SMatthias Ringwald break; 138406b9e820SMatthias Ringwald #endif 138506b9e820SMatthias Ringwald 138606b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 138706b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 138806b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 138906b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 139006b9e820SMatthias Ringwald break; 139153860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 139253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 139353860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 139453860077SMatthias Ringwald break; 139574b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 139674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 13970305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 139874b323a9SMatthias Ringwald break; 139953860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 140053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 14010305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 140274b323a9SMatthias Ringwald break; 14032b838201SMatthias Ringwald 14042b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 14052b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 14062b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 14072b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 14082b838201SMatthias Ringwald break; 14092b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 14102b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 14112b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 14122b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 14132b838201SMatthias Ringwald break; 14142b838201SMatthias Ringwald #endif 14152b838201SMatthias Ringwald 141674b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 14170305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 141874b323a9SMatthias Ringwald if (hci_le_supported()){ 141974b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 142074b323a9SMatthias Ringwald } else { 142174b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 142274b323a9SMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 142374b323a9SMatthias Ringwald } 142474b323a9SMatthias Ringwald break; 14252b838201SMatthias Ringwald 142635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 142774b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 142874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 14290305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 143074b323a9SMatthias Ringwald break; 143174b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 143274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 14330305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 143474b323a9SMatthias Ringwald break; 1435c33e56d3SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_LINK_POLICY_SETTING: 1436c33e56d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_LINK_POLICY_SETTING; 1437c33e56d3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1438c33e56d3SMatthias Ringwald break; 143974b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 144074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 14410305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 144274b323a9SMatthias Ringwald break; 14431c2a62f5SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: { 14440305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 14451c2a62f5SMatthias Ringwald hci_reserve_packet_buffer(); 14461c2a62f5SMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 14471c2a62f5SMatthias Ringwald // construct HCI Command and send 14481c2a62f5SMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 14491c2a62f5SMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 14501c2a62f5SMatthias Ringwald packet[0] = opcode & 0xff; 14511c2a62f5SMatthias Ringwald packet[1] = opcode >> 8; 14521c2a62f5SMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 14531c2a62f5SMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 145479186815SMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 145579186815SMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 145679186815SMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 145779186815SMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 14581c2a62f5SMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 14593c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 14601c2a62f5SMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 146174b323a9SMatthias Ringwald break; 14621c2a62f5SMatthias Ringwald } 146329af07f3SMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: { 14648a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 146529af07f3SMatthias Ringwald hci_reserve_packet_buffer(); 146629af07f3SMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 146763873210SMatthias Ringwald // construct HCI Command in-place and send 146829af07f3SMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 146929af07f3SMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 147063873210SMatthias Ringwald uint16_t offset = 0; 147163873210SMatthias Ringwald packet[offset++] = opcode & 0xff; 147263873210SMatthias Ringwald packet[offset++] = opcode >> 8; 147363873210SMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 147463873210SMatthias Ringwald packet[offset++] = 0; // FEC not required 147563873210SMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 1476ad0d1108SMatthias Ringwald if (hci_stack->eir_data){ 147763873210SMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 147863873210SMatthias Ringwald ad_context_t context; 147963873210SMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 148063873210SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 148163873210SMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 148263873210SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 148363873210SMatthias Ringwald // copy item 148463873210SMatthias Ringwald packet[offset++] = size + 1; 148563873210SMatthias Ringwald packet[offset++] = data_type; 148663873210SMatthias Ringwald memcpy(&packet[offset], data, size); 148763873210SMatthias Ringwald // update name item 148863873210SMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 14893c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 149063873210SMatthias Ringwald } 149163873210SMatthias Ringwald offset += size; 149263873210SMatthias Ringwald } 1493ad0d1108SMatthias Ringwald } else { 149479186815SMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 149579186815SMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 149663873210SMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 149763873210SMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 149879186815SMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 149929af07f3SMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 15003c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 150163873210SMatthias Ringwald } 1502a8c4e5adSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 1503ff00ed1cSMatthias Ringwald break; 150429af07f3SMatthias Ringwald } 1505f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1506f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 15078a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1508f6858d14SMatthias Ringwald break; 15095d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 15105d23aae8SMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 1511c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 15125d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 15135d23aae8SMatthias Ringwald break; 151474b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 151574b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 151674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 151774b323a9SMatthias Ringwald break; 1518483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1519729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1520729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1521729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1522729ed62eSMatthias Ringwald break; 1523483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1524483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1525483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1526483c5078SMatthias Ringwald break; 15278051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 1528a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1529a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 15308051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1531a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1532a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 15338051253fSMatthias Ringwald #endif 15348051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 15358051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 15361d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 15371d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 15381d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 15391d2bbd54SMatthias Ringwald #else 15401d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 15411d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 15421d2bbd54SMatthias Ringwald #endif 15438051253fSMatthias Ringwald #endif 1544a42798c3SMatthias Ringwald break; 15454e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 15464e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 15474e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 15484e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 15491d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 15501d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 15511d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 15521d2bbd54SMatthias Ringwald #else 15531d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 15541d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 15551d2bbd54SMatthias Ringwald #endif 15564e821764SMatthias Ringwald break; 155735454696SMatthias Ringwald #endif 15584e821764SMatthias Ringwald #endif 15594e821764SMatthias Ringwald 1560a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 156174b323a9SMatthias Ringwald // LE INIT 156274b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 156374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 15640305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 156574b323a9SMatthias Ringwald break; 1566daabb8b8SMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 1567daabb8b8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 1568729710c0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0x809FF, 0x0); // bits 0-8, 11, 19 1569daabb8b8SMatthias Ringwald break; 157074b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 157174b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 157274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 15730305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 157474b323a9SMatthias Ringwald break; 1575b435e062SMatthias Ringwald #endif 1576b435e062SMatthias Ringwald 1577b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1578dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 1579dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 1580dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 1581dcd678baSMatthias Ringwald break; 1582dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 1583dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 1584b435e062SMatthias Ringwald hci_send_cmd(&hci_le_write_suggested_default_data_length, hci_stack->le_supported_max_tx_octets, hci_stack->le_supported_max_tx_time); 1585dcd678baSMatthias Ringwald break; 1586b435e062SMatthias Ringwald #endif 1587b435e062SMatthias Ringwald 1588b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 15893b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 15903b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 15913b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 15923b6d4121SMatthias Ringwald break; 159374b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 159474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 15958b69e4c7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 159674b323a9SMatthias Ringwald break; 159774b323a9SMatthias Ringwald #endif 159874b323a9SMatthias Ringwald default: 159974b323a9SMatthias Ringwald return; 160074b323a9SMatthias Ringwald } 160155975f88SMatthias Ringwald } 160255975f88SMatthias Ringwald 1603a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1604a650ba4dSMatthias Ringwald // done. tell the app 1605a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1606a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1607a650ba4dSMatthias Ringwald hci_emit_state(); 1608a650ba4dSMatthias Ringwald hci_run(); 1609a650ba4dSMatthias Ringwald } 1610a650ba4dSMatthias Ringwald 161107fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 161207fd2f31SMatthias Ringwald bool command_completed = false; 16130e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1614f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 16156155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 161607fd2f31SMatthias Ringwald command_completed = true; 1617148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 16186155b3d3S[email protected] } else { 1619d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 16206155b3d3S[email protected] } 16216155b3d3S[email protected] } 16220f97eae7SMatthias Ringwald 16230e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 16246155b3d3S[email protected] uint8_t status = packet[2]; 1625f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 16266155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 16276155b3d3S[email protected] if (status){ 162807fd2f31SMatthias Ringwald command_completed = true; 1629148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 16306155b3d3S[email protected] } else { 16316155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 16326155b3d3S[email protected] } 16336155b3d3S[email protected] } else { 1634148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 16356155b3d3S[email protected] } 16366155b3d3S[email protected] } 163706b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1638e47e68c7SMatthias Ringwald // Vendor == CSR 16390e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 1640e47e68c7SMatthias Ringwald // TODO: track actual command 164107fd2f31SMatthias Ringwald command_completed = true; 1642e47e68c7SMatthias Ringwald } 1643a2481739S[email protected] 16444e9daa6fSMatthias Ringwald // Vendor == Toshiba 16450e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 16464e9daa6fSMatthias Ringwald // TODO: track actual command 164707fd2f31SMatthias Ringwald command_completed = true; 1648004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 1649004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 16504e9daa6fSMatthias Ringwald } 165107fd2f31SMatthias Ringwald #endif 165207fd2f31SMatthias Ringwald 165307fd2f31SMatthias Ringwald return command_completed; 165407fd2f31SMatthias Ringwald } 165507fd2f31SMatthias Ringwald 165607fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 165707fd2f31SMatthias Ringwald 165807fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 165907fd2f31SMatthias Ringwald 166007fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 166107fd2f31SMatthias Ringwald 166207fd2f31SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 16634e9daa6fSMatthias Ringwald 16640f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 16650f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 16660f97eae7SMatthias Ringwald // 16670f97eae7SMatthias Ringwald // HCI Reset 16680f97eae7SMatthias Ringwald // Timeout 100 ms 16690f97eae7SMatthias Ringwald // HCI Reset 16700f97eae7SMatthias Ringwald // Command Complete Reset 16710f97eae7SMatthias Ringwald // HCI Read Local Version Information 16720f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 16730f97eae7SMatthias Ringwald // hang... 16740f97eae7SMatthias Ringwald // 16750f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 16760f97eae7SMatthias Ringwald if (!command_completed 1677a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 16780e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 16790f97eae7SMatthias Ringwald 1680f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 16810f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 16820f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 16830f97eae7SMatthias Ringwald return; 16840f97eae7SMatthias Ringwald } 16850f97eae7SMatthias Ringwald } 16860f97eae7SMatthias Ringwald 16879f007422SMatthias Ringwald // CSR & H5 16889f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 16899f007422SMatthias Ringwald if (!command_completed 1690a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 16910e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 16929f007422SMatthias Ringwald 16939f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 16949f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 16959f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 16969f007422SMatthias Ringwald return; 16979f007422SMatthias Ringwald } 16989f007422SMatthias Ringwald } 16999f007422SMatthias Ringwald 17009f007422SMatthias 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 17019f007422SMatthias Ringwald // fix: Correct substate and behave as command below 17029f007422SMatthias Ringwald if (command_completed){ 17039f007422SMatthias Ringwald switch (hci_stack->substate){ 17049f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 17059f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 17069f007422SMatthias Ringwald break; 17079f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 17089f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 17099f007422SMatthias Ringwald break; 17109f007422SMatthias Ringwald default: 17119f007422SMatthias Ringwald break; 17129f007422SMatthias Ringwald } 17139f007422SMatthias Ringwald } 17140f97eae7SMatthias Ringwald 171506b9e820SMatthias Ringwald #endif 17160f97eae7SMatthias Ringwald 1717a2481739S[email protected] if (!command_completed) return; 1718a2481739S[email protected] 171907fd2f31SMatthias Ringwald bool need_baud_change = false; 172007fd2f31SMatthias Ringwald bool need_addr_change = false; 172106b9e820SMatthias Ringwald 172206b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 172306b9e820SMatthias Ringwald need_baud_change = hci_stack->config 17243fb36a29SMatthias Ringwald && hci_stack->chipset 17253fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1726db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 17279796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1728db8bc6ffSMatthias Ringwald 172906b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 17303fb36a29SMatthias Ringwald && hci_stack->chipset 17313fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 173206b9e820SMatthias Ringwald #endif 1733a80162e9SMatthias Ringwald 17345c363727SMatthias Ringwald switch(hci_stack->substate){ 173506b9e820SMatthias Ringwald 173606b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 17379f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1738d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 17399f007422SMatthias Ringwald // fix: just correct substate and behave as command below 17409f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 17419f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 17429f007422SMatthias Ringwald break; 174374b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1744528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 174576fcb19bSMatthias Ringwald break; 1746e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 174707fd2f31SMatthias Ringwald log_info("Received local name, need baud change %d", (int) need_baud_change); 17482f48d920SMatthias Ringwald if (need_baud_change){ 17492f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 17502f48d920SMatthias Ringwald return; 17512f48d920SMatthias Ringwald } 175253860077SMatthias Ringwald // skip baud change 175374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 175474b323a9SMatthias Ringwald return; 1755a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 17564696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1757fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 175861f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 175996b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1760cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 1761fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 17624696bddbSMatthias Ringwald } 176374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 176474b323a9SMatthias Ringwald return; 1765a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1766528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1767a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1768a80162e9SMatthias Ringwald return; 176974b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 177074b323a9SMatthias Ringwald // repeat custom init 177174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 177274b323a9SMatthias Ringwald return; 177306b9e820SMatthias Ringwald #else 177406b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 177506b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 177606b9e820SMatthias Ringwald return ; 177706b9e820SMatthias Ringwald #endif 177806b9e820SMatthias Ringwald 1779a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 17800e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1781efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 1782efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 1783eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1784eb3a5314SMatthias Ringwald return; 1785eb3a5314SMatthias Ringwald } 178653860077SMatthias Ringwald if (need_addr_change){ 178753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 178853860077SMatthias Ringwald return; 178953860077SMatthias Ringwald } 179053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 179153860077SMatthias Ringwald return; 179206b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 17937224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 17947224be7eSMatthias Ringwald if (need_baud_change){ 179596b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1796cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 1797fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 17987224be7eSMatthias Ringwald } 1799eb3a5314SMatthias Ringwald if (need_addr_change){ 1800eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1801eb3a5314SMatthias Ringwald return; 1802eb3a5314SMatthias Ringwald } 1803eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1804eb3a5314SMatthias Ringwald return; 180553860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 18066ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 18076ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 18086ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 180953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 181053860077SMatthias Ringwald return; 181153860077SMatthias Ringwald } 181253860077SMatthias Ringwald // skipping st warm boot 181353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 181453860077SMatthias Ringwald return; 181553860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 181653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 181753860077SMatthias Ringwald return; 181806b9e820SMatthias Ringwald #endif 181953860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 182053860077SMatthias Ringwald // only read buffer size if supported 18214ea43905SMatthias Ringwald if (hci_stack->local_supported_commands[0u] & 0x01u) { 182253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 182353860077SMatthias Ringwald return; 182453860077SMatthias Ringwald } 182553860077SMatthias Ringwald // skipping read buffer size 182653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1827a828a756SMatthias Ringwald return; 182874b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 18296155b3d3S[email protected] // skip Classic init commands for LE only chipsets 18306155b3d3S[email protected] if (!hci_classic_supported()){ 18312c68f164SMatthias Ringwald #ifdef ENABLE_BLE 18326155b3d3S[email protected] if (hci_le_supported()){ 183374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 183474b323a9SMatthias Ringwald return; 18352c68f164SMatthias Ringwald } 18362c68f164SMatthias Ringwald #endif 18376155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1838a650ba4dSMatthias Ringwald hci_init_done(); 183974b323a9SMatthias Ringwald return; 18406155b3d3S[email protected] } 184115a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 18425c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 184374b323a9SMatthias Ringwald return; 18446155b3d3S[email protected] } 18456155b3d3S[email protected] break; 1846903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1847a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1848a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 18494ea43905SMatthias Ringwald if (hci_stack->local_supported_commands[0u] & 0x02u) break; 1850daabb8b8SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_EVENT_MASK; 1851daabb8b8SMatthias Ringwald return; 1852daabb8b8SMatthias Ringwald 1853b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1854b435e062SMatthias Ringwald case HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED: 18556fab74dbSMatthias Ringwald log_info("Supported commands %x", hci_stack->local_supported_commands[0] & 0x30); 18564ea43905SMatthias Ringwald if ((hci_stack->local_supported_commands[0u] & 0x30u) == 0x30u){ 185780d7d618SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_EVENT_MASK; 1858b435e062SMatthias Ringwald return; 1859b435e062SMatthias Ringwald } 1860b435e062SMatthias Ringwald // explicit fall through to reduce repetitions 1861b435e062SMatthias Ringwald 1862b435e062SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1863b435e062SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1864903ea03aSMatthias Ringwald #else 1865903ea03aSMatthias Ringwald hci_init_done(); 1866903ea03aSMatthias Ringwald #endif 1867a828a756SMatthias Ringwald return; 186806337f4fSMatthias Ringwald #endif /* ENABLE_LE_DATA_LENGTH_EXTENSION */ 186906337f4fSMatthias Ringwald 187006337f4fSMatthias Ringwald #endif /* ENABLE_BLE */ 1871b435e062SMatthias Ringwald 18725d23aae8SMatthias Ringwald case HCI_INIT_W4_WRITE_INQUIRY_MODE: 18735d23aae8SMatthias Ringwald // skip write secure connections host support if not supported or disabled 18744ea43905SMatthias Ringwald if (!hci_stack->secure_connections_enable || (hci_stack->local_supported_commands[1u] & 0x02u) == 0u) { 18755d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; 18765d23aae8SMatthias Ringwald return; 18775d23aae8SMatthias Ringwald } 18785d23aae8SMatthias Ringwald break; 18795d23aae8SMatthias Ringwald 188047bc3ebbSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 18813b65523cSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 18823905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 18833905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 18843905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1885cf373d3aSMatthias Ringwald 1886cf373d3aSMatthias Ringwald /* fall through */ 18873905afbfSMatthias Ringwald 1888729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 18893905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 18903905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 18913905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1892cf373d3aSMatthias Ringwald 1893cf373d3aSMatthias Ringwald /* fall through */ 18943905afbfSMatthias Ringwald 1895f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1896a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 189761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 18980168d82eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 1899cf373d3aSMatthias Ringwald 1900cf373d3aSMatthias Ringwald /* fall through */ 1901a42798c3SMatthias Ringwald 1902a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1903729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1904729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1905a650ba4dSMatthias Ringwald hci_init_done(); 1906729ed62eSMatthias Ringwald return; 1907729ed62eSMatthias Ringwald } 1908afe9bf34SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 1909729ed62eSMatthias Ringwald break; 1910f3b012f9SMatthias Ringwald 1911f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1912f3b012f9SMatthias Ringwald 191374b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 19148051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 19158051253fSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) { 19168051253fSMatthias Ringwald hci_stack->substate = HCI_INIT_BCM_WRITE_SCO_PCM_INT; 19178051253fSMatthias Ringwald return; 19188051253fSMatthias Ringwald } 19198051253fSMatthias Ringwald #endif 19208051253fSMatthias Ringwald /* fall through */ 19218051253fSMatthias Ringwald 19224e821764SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 1923f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1924f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1925f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 192674b323a9SMatthias Ringwald return; 19276155b3d3S[email protected] } 1928729ed62eSMatthias Ringwald #endif 1929f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1930f3b012f9SMatthias Ringwald hci_init_done(); 1931f3b012f9SMatthias Ringwald return; 1932f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1933f3b012f9SMatthias Ringwald 1934a4e96e78SMatthias Ringwald // avoid compile error due to duplicate cases: HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT == HCI_INIT_DONE-1 1935a4e96e78SMatthias Ringwald #if defined(ENABLE_BLE) || defined(ENABLE_LE_DATA_LENGTH_EXTENSION) || defined(ENABLE_LE_CENTRAL) 1936a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1937a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1938a650ba4dSMatthias Ringwald hci_init_done(); 1939a650ba4dSMatthias Ringwald return; 1940a4e96e78SMatthias Ringwald #endif 1941a650ba4dSMatthias Ringwald 19426155b3d3S[email protected] default: 194374b323a9SMatthias Ringwald break; 19446155b3d3S[email protected] } 194555975f88SMatthias Ringwald hci_initializing_next_state(); 19466155b3d3S[email protected] } 19476155b3d3S[email protected] 19480bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 1949229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 19500bbba85bSMatthias Ringwald bd_addr_t bd_address; 19516535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 19520bbba85bSMatthias Ringwald 19536bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 19546bc9fa5eSMatthias Ringwald // cache needed data 19556bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 19566bc9fa5eSMatthias Ringwald #endif 19576bc9fa5eSMatthias Ringwald 19580bbba85bSMatthias Ringwald // connection failed, remove entry 19590bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 19600bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 19610bbba85bSMatthias Ringwald 19626bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 19630bbba85bSMatthias Ringwald // notify client if dedicated bonding 19640bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 19650bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 19660bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 19670bbba85bSMatthias Ringwald } 19680bbba85bSMatthias Ringwald 19690bbba85bSMatthias Ringwald // if authentication error, also delete link key 19700bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 19710bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 19720bbba85bSMatthias Ringwald } 19731c79b5e3SMatthias Ringwald #else 19741c79b5e3SMatthias Ringwald UNUSED(status); 19756bc9fa5eSMatthias Ringwald #endif 19760bbba85bSMatthias Ringwald } 19770bbba85bSMatthias Ringwald 1978be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 19792f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 19802f5c44baSMatthias Ringwald // SSP Controller 19812f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 19822f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 19832f5c44baSMatthias Ringwald } 19842f5c44baSMatthias Ringwald // eSCO 19852f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 19862f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 19872f5c44baSMatthias Ringwald } 19882f5c44baSMatthias Ringwald // Extended features 19892f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 19902f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 19912f5c44baSMatthias Ringwald } 19922f5c44baSMatthias Ringwald } 19932f5c44baSMatthias Ringwald 19942f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 19952f5c44baSMatthias Ringwald // SSP Host 19962f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 19972f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 19982f5c44baSMatthias Ringwald } 199950c51a77SMatthias Ringwald // SC Host 200050c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 200150c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 200250c51a77SMatthias Ringwald } 200350c51a77SMatthias Ringwald } 200450c51a77SMatthias Ringwald 200550c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 200650c51a77SMatthias Ringwald // SC Controller 200750c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 200850c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 200950c51a77SMatthias Ringwald } 20102f5c44baSMatthias Ringwald } 20112f5c44baSMatthias Ringwald 2012de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2013de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 201449bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2015de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2016de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2017de0df013SMatthias Ringwald } 2018de0df013SMatthias Ringwald } 2019be500194SMatthias Ringwald #endif 2020de0df013SMatthias Ringwald 202167c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 202267c6c9dcSMatthias Ringwald // handle BT initialization 202367c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 202467c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 202567c6c9dcSMatthias Ringwald } 202667c6c9dcSMatthias Ringwald 202767c6c9dcSMatthias Ringwald // help with BT sleep 202867c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 202967c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 203067c6c9dcSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)) { 203167c6c9dcSMatthias Ringwald hci_initializing_next_state(); 203267c6c9dcSMatthias Ringwald } 203367c6c9dcSMatthias Ringwald } 203467c6c9dcSMatthias Ringwald 20359866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 20369866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 20379866fdc7SMatthias Ringwald conn->authentication_flags |= CONNECTION_ENCRYPTED; 20389866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2039abdad579SMatthias Ringwald 2040abdad579SMatthias Ringwald if ((conn->authentication_flags & CONNECTION_AUTHENTICATED) != 0) { 20419866fdc7SMatthias Ringwald hci_emit_security_level(conn->con_handle, gap_security_level_for_connection(conn)); 2042abdad579SMatthias Ringwald return; 2043abdad579SMatthias Ringwald } 2044abdad579SMatthias Ringwald 2045abdad579SMatthias Ringwald // Request Authentication if not already done 2046abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2047abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 20489866fdc7SMatthias Ringwald } 20499866fdc7SMatthias Ringwald #endif 20509866fdc7SMatthias Ringwald 20514f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2052b08371a9SMilanka Ringwald UNUSED(size); 2053e76a89eeS[email protected] 20549cbd2215SMatthias Ringwald uint16_t manufacturer; 20559cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2056fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 20571f7b95a1Smatthias.ringwald hci_connection_t * conn; 2058645b7c25SMatthias Ringwald uint8_t status; 20599cbd2215SMatthias Ringwald #endif 20606bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 20616bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 20627ec5eeaaSmatthias.ringwald 2063645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2064645b7c25SMatthias Ringwald switch (opcode){ 2065645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2066645b7c25SMatthias Ringwald if (packet[5]) break; 20679e263bd7SMatthias Ringwald // terminate, name 248 chars 20689e263bd7SMatthias Ringwald packet[6+248] = 0; 20699e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2070645b7c25SMatthias Ringwald break; 2071645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 20721d279b20Smatthias.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" 2073c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2074429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2075429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2076429122ccSMatthias Ringwald 2077429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2078429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2079429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2080429122ccSMatthias Ringwald 2081f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 2082f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 2083a8b12447S[email protected] 2084429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2085429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 20861a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2087c3b46f5aSMatthias Ringwald } 2088645b7c25SMatthias Ringwald break; 2089645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2090645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2091891b9fc2SMatthias Ringwald uint8_t event[5]; 2092891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2093891b9fc2SMatthias Ringwald event[1] = 3; 20946535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2095891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2096891b9fc2SMatthias Ringwald } 2097645b7c25SMatthias Ringwald break; 2098a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2099645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2100f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2101ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 21026c26b087S[email protected] // determine usable ACL payload size 21036c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 21046c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 21056c26b087S[email protected] } 21069da54300S[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); 2107645b7c25SMatthias Ringwald break; 2108b435e062SMatthias Ringwald #endif 2109b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2110645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2111dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2112dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2113dcd678baSMatthias Ringwald log_info("hci_le_read_maximum_data_length: tx octets %u, tx time %u us", hci_stack->le_supported_max_tx_octets, hci_stack->le_supported_max_tx_time); 2114645b7c25SMatthias Ringwald break; 2115b435e062SMatthias Ringwald #endif 2116d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2117645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2118e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 211915d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2120645b7c25SMatthias Ringwald break; 212165a46ef3S[email protected] #endif 2122645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2123645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2124645b7c25SMatthias Ringwald log_info("Local Address, Status: 0x%02x: Addr: %s", packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 212533373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 21261624665aSMatthias Ringwald if (hci_stack->link_key_db){ 21271624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 21281624665aSMatthias Ringwald } 212933373e40SMatthias Ringwald #endif 2130645b7c25SMatthias Ringwald break; 213135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2132645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 21333a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2134645b7c25SMatthias Ringwald break; 2135645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 2136f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2137f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2138f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2139f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2140f5875de5SMatthias Ringwald } 2141645b7c25SMatthias Ringwald break; 214235454696SMatthias Ringwald #endif 2143645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 21444f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 214565389bfcS[email protected] 214635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2147a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2148a5a23fc2S[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]); 21498b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 215035454696SMatthias Ringwald #endif 2151f5d8d141S[email protected] // Classic/LE 2152f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2153645b7c25SMatthias Ringwald break; 2154645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2155645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2156c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2157c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2158c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2159c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2160c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2161c21d89f3SMatthias Ringwald } 2162c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 21634696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2164645b7c25SMatthias Ringwald break; 2165645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 2166a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 21674ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+14u] & 0x80u) >> 7u) | // bit 0 = Octet 14, bit 7 / Read Buffer Size 21684ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+24u] & 0x40u) >> 5u) | // bit 1 = Octet 24, bit 6 / Write Le Host Supported 21694ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+10u] & 0x10u) >> 2u) | // bit 2 = Octet 10, bit 4 / Write Synchronous Flow Control Enable 21704ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+18u] & 0x08u) ) | // bit 3 = Octet 18, bit 3 / Write Default Erroneous Data Reporting 21714ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+34u] & 0x01u) << 4u) | // bit 4 = Octet 34, bit 0 / LE Write Suggested Default Data Length 21724ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+35u] & 0x08u) << 2u) | // bit 5 = Octet 35, bit 3 / LE Read Maximum Data Length 21734ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+35u] & 0x20u) << 1u) | // bit 6 = Octet 35, bit 5 / LE Set Default PHY 21744ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+20u] & 0x10u) << 3u); // bit 7 = Octet 20, bit 4 / Read Encryption Key Size 21758d2d8af1SMatthias Ringwald hci_stack->local_supported_commands[1] = 21764ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+ 2u] & 0x40u) >> 6u) | // bit 8 = Octet 2, bit 6 / Read Remote Extended Features 217799b14827SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+32u] & 0x08u) >> 2u) | // bit 9 = Octet 32, bit 3 / Write Secure Connections Host 2178f21849cbSMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+35u] & 0x02u) << 1u) | // bit 10 = Octet 35, bit 1 / LE Set Address Resolution Enable 2179f21849cbSMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+32u] & 0x02u) << 2u) | // bit 11 = Octet 32, bit 1 / Remote OOB Extended Data Request Reply 2180f21849cbSMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+32u] & 0x40u) >> 2u); // bit 12 = Octet 32, bit 6 / Read Local OOB Extended Data command 21818d2d8af1SMatthias Ringwald log_info("Local supported commands summary %02x - %02x", hci_stack->local_supported_commands[0], hci_stack->local_supported_commands[1]); 2182645b7c25SMatthias Ringwald break; 2183e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2184645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 21854f781026SMatthias Ringwald if (packet[5]) return; 21865b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2187645b7c25SMatthias Ringwald break; 2188645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2189645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2190573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2191573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2192c3b46f5aSMatthias Ringwald if (conn != NULL) { 21939866fdc7SMatthias Ringwald uint8_t key_size = 0; 2194573897a0SMatthias Ringwald if (status == 0){ 21959866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 21964a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2197170fafaeSMatthias Ringwald } else { 21989866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2199573897a0SMatthias Ringwald } 22009866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2201c3b46f5aSMatthias Ringwald } 2202645b7c25SMatthias Ringwald break; 220375a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 220475a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 220575a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 220675a8e4faSMatthias Ringwald uint8_t event[67]; 220775a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 220875a8e4faSMatthias Ringwald event[1] = 65; 220975a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 221075a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 221175a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 221275a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 221375a8e4faSMatthias Ringwald event[2] = 3; 221475a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 221575a8e4faSMatthias Ringwald } else { 221675a8e4faSMatthias Ringwald event[2] = 1; 221775a8e4faSMatthias Ringwald } 221875a8e4faSMatthias Ringwald } 221975a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 222075a8e4faSMatthias Ringwald break; 222175a8e4faSMatthias Ringwald } 2222e8c8828eSMatthias Ringwald #endif 222375a8e4faSMatthias Ringwald #endif 22246f35bb46SMatthias Ringwald default: 22256f35bb46SMatthias Ringwald break; 22264f781026SMatthias Ringwald } 2227645b7c25SMatthias Ringwald } 22284f781026SMatthias Ringwald 22290ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 22300ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 22310ce3f217SMatthias Ringwald bd_addr_t addr; 22320ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 22330ce3f217SMatthias Ringwald hci_connection_t * conn; 22340ce3f217SMatthias Ringwald 22350ce3f217SMatthias Ringwald // Connection management 22360ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 22370ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 22380ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 22390ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 22400ce3f217SMatthias Ringwald 22410ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 22420ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 22430ce3f217SMatthias Ringwald if (packet[3]){ 22440ce3f217SMatthias Ringwald 22450ce3f217SMatthias Ringwald // handle cancelled outgoing connection 22460ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 22470ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 22480ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 22490ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 22500ce3f217SMatthias Ringwald // whitelist connect 22510ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 22520ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 22530ce3f217SMatthias Ringwald } 22540ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 22550ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 22560ce3f217SMatthias Ringwald } 22570ce3f217SMatthias Ringwald 22580ce3f217SMatthias Ringwald // outgoing le connection establishment is done 22590ce3f217SMatthias Ringwald if (conn){ 22600ce3f217SMatthias Ringwald // remove entry 22610ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 22620ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 22630ce3f217SMatthias Ringwald } 22640ce3f217SMatthias Ringwald return; 22650ce3f217SMatthias Ringwald } 22660ce3f217SMatthias Ringwald #endif 22670ce3f217SMatthias Ringwald 22680ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 22690ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 22700ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 22710ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 22720ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 22730ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 22740ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 22750ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 22760ce3f217SMatthias Ringwald } 22770ce3f217SMatthias Ringwald #endif 22780ce3f217SMatthias Ringwald } else { 22790ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 22800ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 22810ce3f217SMatthias Ringwald hci_stack->le_advertisements_active = false; 22820ce3f217SMatthias Ringwald #endif 22830ce3f217SMatthias Ringwald } 22840ce3f217SMatthias Ringwald 22850ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 22860ce3f217SMatthias Ringwald if (!conn){ 22870ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 22880ce3f217SMatthias Ringwald } 22890ce3f217SMatthias Ringwald 22900ce3f217SMatthias Ringwald // no memory, sorry. 22910ce3f217SMatthias Ringwald if (!conn){ 22920ce3f217SMatthias Ringwald return; 22930ce3f217SMatthias Ringwald } 22940ce3f217SMatthias Ringwald 22950ce3f217SMatthias Ringwald conn->state = OPEN; 22960ce3f217SMatthias Ringwald conn->role = packet[6]; 22970ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 22980ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 22990ce3f217SMatthias Ringwald 23000ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 23010ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 23020ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 23030ce3f217SMatthias Ringwald } 23040ce3f217SMatthias Ringwald #endif 23050ce3f217SMatthias Ringwald 2306dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 2307dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 2308dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 2309dde9ff1eSMatthias Ringwald 23100ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 23110ce3f217SMatthias Ringwald 23120ce3f217SMatthias Ringwald // restart timer 23130ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 23140ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 23150ce3f217SMatthias Ringwald 23160ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 23170ce3f217SMatthias Ringwald 23180ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 23190ce3f217SMatthias Ringwald } 23200ce3f217SMatthias Ringwald #endif 23210ce3f217SMatthias Ringwald 2322c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 23234f781026SMatthias Ringwald 23244f781026SMatthias Ringwald uint16_t event_length = packet[1]; 23254f781026SMatthias Ringwald 23264f781026SMatthias Ringwald // assert packet is complete 23274ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 23284f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 23294f781026SMatthias Ringwald return; 23304f781026SMatthias Ringwald } 23314f781026SMatthias Ringwald 23324f781026SMatthias Ringwald bd_addr_type_t addr_type; 23334f781026SMatthias Ringwald hci_con_handle_t handle; 23344f781026SMatthias Ringwald hci_connection_t * conn; 23354f781026SMatthias Ringwald int i; 23364f781026SMatthias Ringwald int create_connection_cmd; 23374f781026SMatthias Ringwald 23384f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 23395e91d96cSMatthias Ringwald hci_link_type_t link_type; 234048f33f37SMatthias Ringwald bd_addr_t addr; 23414f781026SMatthias Ringwald #endif 23424f781026SMatthias Ringwald 23434f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 23444f781026SMatthias Ringwald 23454f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 23464f781026SMatthias Ringwald 23474f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 23484f781026SMatthias Ringwald handle_command_complete_event(packet, size); 234956cf178bSmatthias.ringwald break; 235056cf178bSmatthias.ringwald 23517ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 23526bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 23536bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2354229331c6SMatthias Ringwald 2355229331c6SMatthias Ringwald // check command status to detected failed outgoing connections 2356c57fa566SMatthias Ringwald create_connection_cmd = 0; 2357c57fa566SMatthias Ringwald #ifdef ENABLE_CLASSIC 2358c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){ 2359c57fa566SMatthias Ringwald create_connection_cmd = 1; 2360c57fa566SMatthias Ringwald } 2361c57fa566SMatthias Ringwald #endif 2362c57fa566SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2363c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_le_create_connection)){ 2364c57fa566SMatthias Ringwald create_connection_cmd = 1; 2365c57fa566SMatthias Ringwald } 2366c57fa566SMatthias Ringwald #endif 2367c57fa566SMatthias Ringwald if (create_connection_cmd) { 2368229331c6SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 23698da98cbeSMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 23706ea9315cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 23716ea9315cSMatthias Ringwald log_info("command status (create connection), status %x, connection %p, addr %s, type %x", status, conn, bd_addr_to_str(hci_stack->outgoing_addr), addr_type); 2372229331c6SMatthias Ringwald 2373229331c6SMatthias Ringwald // reset outgoing address info 2374229331c6SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2375229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2376229331c6SMatthias Ringwald 23776ea9315cSMatthias Ringwald // on error 23786ea9315cSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2379f75e06adSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 23806ea9315cSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 23816ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 23826ea9315cSMatthias Ringwald } 2383f75e06adSMatthias Ringwald #endif 2384229331c6SMatthias Ringwald // error => outgoing connection failed 23856ea9315cSMatthias Ringwald if (conn != NULL){ 2386229331c6SMatthias Ringwald hci_handle_connection_failed(conn, status); 2387229331c6SMatthias Ringwald } 2388229331c6SMatthias Ringwald } 23896ea9315cSMatthias Ringwald } 2390beb3c81dSMatthias Ringwald 2391beb3c81dSMatthias Ringwald #ifdef ENABLE_CLASSIC 2392beb3c81dSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_inquiry)) { 2393beb3c81dSMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2394beb3c81dSMatthias Ringwald log_info("command status (inquiry), status %x", status); 2395beb3c81dSMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 2396beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2397beb3c81dSMatthias Ringwald } else { 2398beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2399beb3c81dSMatthias Ringwald } 2400beb3c81dSMatthias Ringwald } 2401beb3c81dSMatthias Ringwald #endif 24027ec5eeaaSmatthias.ringwald break; 24037ec5eeaaSmatthias.ringwald 24042e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 24059784dac1SMatthias Ringwald if (size < 3) return; 24069784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 24074ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 24089784dac1SMatthias Ringwald uint16_t offset = 3; 24099784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 24104ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 24114ea43905SMatthias Ringwald offset += 2u; 2412f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 24134ea43905SMatthias Ringwald offset += 2u; 24142e440c8aS[email protected] 24155061f3afS[email protected] conn = hci_connection_for_handle(handle); 241656cf178bSmatthias.ringwald if (!conn){ 24179da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 241856cf178bSmatthias.ringwald continue; 241956cf178bSmatthias.ringwald } 242023bed257S[email protected] 2421ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets){ 2422ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 2423e35edcc1S[email protected] } else { 2424ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 2425ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 2426e35edcc1S[email protected] } 2427ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 2428760b20efSMatthias Ringwald 2429760b20efSMatthias Ringwald #ifdef ENABLE_CLASSIC 2430760b20efSMatthias Ringwald // For SCO, we do the can_send_now_check here 2431760b20efSMatthias Ringwald hci_notify_if_sco_can_send_now(); 2432760b20efSMatthias Ringwald #endif 243356cf178bSmatthias.ringwald } 24346772a24cSmatthias.ringwald break; 24352e440c8aS[email protected] } 243635454696SMatthias Ringwald 243735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2438f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 2439f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 2440f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2441f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2442f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2443f5875de5SMatthias Ringwald } 2444f5875de5SMatthias Ringwald break; 2445b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 2446b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 2447b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 2448b7f1ee76SMatthias Ringwald } 2449b7f1ee76SMatthias Ringwald break; 24501f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 2451724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 24525e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 245307e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 24545e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 24554536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 245607e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 245707e010b6SMilanka Ringwald break; 245807e010b6SMilanka Ringwald } 245907e010b6SMilanka Ringwald } 246007e010b6SMilanka Ringwald 246137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 24625e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 24635e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 24642e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 24651f7b95a1Smatthias.ringwald if (!conn) { 24665293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 24671f7b95a1Smatthias.ringwald } 2468ce4c8fabSmatthias.ringwald if (!conn) { 2469ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 24704536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 2471058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 2472ce4c8fabSmatthias.ringwald break; 2473ce4c8fabSmatthias.ringwald } 24745cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 247532ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 2476f3a16b9aSMatthias Ringwald // store info about eSCO 24775e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 247876ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 2479f3a16b9aSMatthias Ringwald } 248032ab9390Smatthias.ringwald hci_run(); 24811f7b95a1Smatthias.ringwald break; 24821f7b95a1Smatthias.ringwald 24836772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 2484fe1ed1b8Smatthias.ringwald // Connection management 2485724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 24869da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 2487f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 24882e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2489fe1ed1b8Smatthias.ringwald if (conn) { 2490b448a0e7Smatthias.ringwald if (!packet[2]){ 2491c8e4258aSmatthias.ringwald conn->state = OPEN; 2492f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 24936909f064SMatthias Ringwald 24946909f064SMatthias Ringwald // queue get remote feature 2495ba018746SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 2496ee091cf1Smatthias.ringwald 2497e6c51921SMatthias Ringwald // queue set supervision timeout if we're master 2498d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 24996909f064SMatthias Ringwald connectionSetAuthenticationFlags(conn, WRITE_SUPERVISION_TIMEOUT); 25006909f064SMatthias Ringwald } 25016909f064SMatthias Ringwald 2502c785ef68Smatthias.ringwald // restart timer 2503528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2504528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 2505c785ef68Smatthias.ringwald 25069da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 250743bfb1bdSmatthias.ringwald 250843bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 2509b448a0e7Smatthias.ringwald } else { 25100bbba85bSMatthias Ringwald // connection failed 25110bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 2512fe1ed1b8Smatthias.ringwald } 2513fe1ed1b8Smatthias.ringwald } 25146772a24cSmatthias.ringwald break; 2515fe1ed1b8Smatthias.ringwald 251644d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 2517724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 251844d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 25191a06f663S[email protected] if (packet[2]){ 252044d0e3d5S[email protected] // connection failed 252144d0e3d5S[email protected] break; 252244d0e3d5S[email protected] } 25232e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2524e35edcc1S[email protected] if (!conn) { 2525e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2526e35edcc1S[email protected] } 2527e35edcc1S[email protected] if (!conn) { 2528e35edcc1S[email protected] break; 2529e35edcc1S[email protected] } 25301a06f663S[email protected] conn->state = OPEN; 2531f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2532ee752bb8SMatthias Ringwald 2533ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2534ee752bb8SMatthias Ringwald // update SCO 2535ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 2536ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 2537ee752bb8SMatthias Ringwald } 2538f234b250SMatthias Ringwald // trigger can send now 2539f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 2540f234b250SMatthias Ringwald hci_stack->sco_can_send_now = 1; 2541f234b250SMatthias Ringwald } 2542ee752bb8SMatthias Ringwald #endif 2543cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 2544cb70c5abSMatthias Ringwald // configure sco transport 2545cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 2546cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 2547cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 2548cb70c5abSMatthias Ringwald } 2549cb70c5abSMatthias Ringwald #endif 255044d0e3d5S[email protected] break; 255144d0e3d5S[email protected] 2552afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 2553f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2554afd4e962S[email protected] conn = hci_connection_for_handle(handle); 2555afd4e962S[email protected] if (!conn) break; 2556afd4e962S[email protected] if (!packet[2]){ 25572f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 25582f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 25592f5c44baSMatthias Ringwald 25605ccef624SMatthias Ringwald // read extended features if possible 25615ccef624SMatthias Ringwald if (((hci_stack->local_supported_commands[1] & 1) != 0) && ((conn->remote_supported_features[0] & 2) != 0)) { 25625ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 25635ccef624SMatthias Ringwald break; 25645ccef624SMatthias Ringwald } 25655ccef624SMatthias Ringwald } 25665ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 25675ccef624SMatthias Ringwald break; 25685ccef624SMatthias Ringwald 25695ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 25705ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 25715ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 25725ccef624SMatthias Ringwald if (!conn) break; 25735ccef624SMatthias Ringwald // status = ok, page = 1 257450c51a77SMatthias Ringwald if (!packet[2]) { 257550c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 257650c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 25775ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 257850c51a77SMatthias Ringwald bool done = false; 257950c51a77SMatthias Ringwald switch (page_number){ 258050c51a77SMatthias Ringwald case 1: 25812f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 258250c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 258350c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 258450c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 258550c51a77SMatthias Ringwald } else { 258650c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 258750c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 258850c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 258950c51a77SMatthias Ringwald } 259050c51a77SMatthias Ringwald done = true; 259150c51a77SMatthias Ringwald } 259250c51a77SMatthias Ringwald break; 259350c51a77SMatthias Ringwald case 2: 259450c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 259550c51a77SMatthias Ringwald done = true; 259650c51a77SMatthias Ringwald break; 259750c51a77SMatthias Ringwald default: 259850c51a77SMatthias Ringwald break; 259950c51a77SMatthias Ringwald } 260050c51a77SMatthias Ringwald if (!done) break; 2601afd4e962S[email protected] } 2602de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 2603afd4e962S[email protected] break; 2604afd4e962S[email protected] 26057fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 26069da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 26077fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 2608608f51bbSMatthias Ringwald // request handled by hci_run() 260932ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 2610608f51bbSMatthias Ringwald break; 26117fde4af9Smatthias.ringwald 26129ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 2613724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 2614f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 26159ab95c90S[email protected] if (!conn) break; 26169ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 26177bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 26189ab95c90S[email protected] // Change Connection Encryption keeps link key type 26199ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 26209ab95c90S[email protected] conn->link_key_type = link_key_type; 26219ab95c90S[email protected] } 2622bec5f683SMatthias Ringwald // only store link key: 2623bec5f683SMatthias Ringwald // - if bondable enabled 2624bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 2625bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 2626bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 2627bec5f683SMatthias Ringwald uint8_t auth_req_ignoring_mitm = conn->io_cap_response_auth_req & 0xfe; 2628bec5f683SMatthias Ringwald if (auth_req_ignoring_mitm == SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_NO_BONDING){ 2629bec5f683SMatthias Ringwald break; 2630bec5f683SMatthias Ringwald } 2631bec5f683SMatthias Ringwald } 263255597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 263329d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 26347fde4af9Smatthias.ringwald break; 26359ab95c90S[email protected] } 26367fde4af9Smatthias.ringwald 26377fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 26386724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 26394c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 26403a9fb326S[email protected] if (!hci_stack->bondable){ 2641899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 2642f8fb5f6eS[email protected] hci_run(); 2643f8fb5f6eS[email protected] return; 26444c57c146S[email protected] } 2645d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 2646a98592bcSMatthias Ringwald if (!hci_stack->link_key_db) break; 2647a6ef64baSMilanka Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 2648a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 26497fde4af9Smatthias.ringwald break; 26507fde4af9Smatthias.ringwald 265150d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 265250d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 265350d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 265450d7398cSMatthias Ringwald if (!conn) break; 265550d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 265650d7398cSMatthias Ringwald break; 265750d7398cSMatthias Ringwald 26581d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 26591d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 266011b03efaSMatthias Ringwald log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 266144565b0cSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 26628dbd6159SMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 2663dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 266411b03efaSMatthias Ringwald } else { 266511b03efaSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 266611b03efaSMatthias Ringwald } 266744565b0cSMatthias Ringwald #endif 2668dbe1a790S[email protected] break; 2669dbe1a790S[email protected] 26701849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 26711849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 26721849becdSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 26731849becdSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_REMOTE_OOB_DATA_REPLY); 26741849becdSMatthias Ringwald break; 26751849becdSMatthias Ringwald #endif 26761849becdSMatthias Ringwald 2677dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 26786724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 26793a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 2680dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 2681dbe1a790S[email protected] break; 2682dbe1a790S[email protected] 2683dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 26846724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 26853a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 2686dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 26871d6b20aeS[email protected] break; 26881849becdSMatthias Ringwald 26893dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 26903dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 26913dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 26923dce6128SMatthias Ringwald if (!conn) break; 26933dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 26943dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 26953dce6128SMatthias Ringwald break; 269635454696SMatthias Ringwald #endif 26971d6b20aeS[email protected] 2698f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 2699254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 2700f0944df2S[email protected] conn = hci_connection_for_handle(handle); 2701f0944df2S[email protected] if (!conn) break; 27024ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 2703254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 2704254b78eeSMatthias Ringwald if (encryption_enabled){ 2705573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 2706573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 2707f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 2708573897a0SMatthias Ringwald } 2709573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 2710573897a0SMatthias Ringwald else { 2711254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 2712254b78eeSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type) != 0; 2713254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 2714edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 2715254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 27169ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 2717254b78eeSMatthias Ringwald break; 2718254b78eeSMatthias Ringwald } 2719254b78eeSMatthias Ringwald 27204a659b0eSMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 2721378d29b4SMatthias Ringwald if (connected_uses_aes_ccm){ 27224a659b0eSMatthias Ringwald conn->authentication_flags |= CONNECTION_AUTHENTICATED; 2723378d29b4SMatthias Ringwald } 27244a659b0eSMatthias Ringwald 27256e058d3fSMatthias Ringwald if ((hci_stack->local_supported_commands[0] & 0x80) != 0){ 27266e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 2727573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 27286e058d3fSMatthias Ringwald } else { 27296e058d3fSMatthias Ringwald // if not, pretend everything is perfect 27309866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 27316e058d3fSMatthias Ringwald } 2732573897a0SMatthias Ringwald } 2733573897a0SMatthias Ringwald #endif 2734f0944df2S[email protected] } else { 2735536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 2736f0944df2S[email protected] } 2737ad83dc6aS[email protected] } 2738573897a0SMatthias Ringwald 2739f0944df2S[email protected] break; 2740f0944df2S[email protected] 274135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 27421eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 2743abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 27441eb2563eS[email protected] conn = hci_connection_for_handle(handle); 27451eb2563eS[email protected] if (!conn) break; 2746ad83dc6aS[email protected] 274763fc2b30SMatthias Ringwald // ignore authentication event if we didn't request it 274863fc2b30SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) == 0) break; 274963fc2b30SMatthias Ringwald 2750ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 2751ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 2752ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 2753ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 27541bd5283dS[email protected] conn->bonding_status = packet[2]; 2755ad83dc6aS[email protected] break; 2756ad83dc6aS[email protected] } 2757ad83dc6aS[email protected] 2758abdad579SMatthias Ringwald // authenticated only if auth status == 0 2759abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 2760abdad579SMatthias Ringwald // authenticated 2761abdad579SMatthias Ringwald conn->authentication_flags |= CONNECTION_AUTHENTICATED; 2762abdad579SMatthias Ringwald 2763abdad579SMatthias Ringwald // If link key sufficient for requested security and not already encrypted, start encryption 2764abdad579SMatthias Ringwald if (((gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level)) && 2765abdad579SMatthias Ringwald ((conn->authentication_flags & CONNECTION_ENCRYPTED) == 0)){ 27661eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2767ad83dc6aS[email protected] break; 2768ad83dc6aS[email protected] } 2769abdad579SMatthias Ringwald } 2770abdad579SMatthias Ringwald 2771abdad579SMatthias Ringwald // emit updated security level 27721eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 27731eb2563eS[email protected] break; 277435454696SMatthias Ringwald #endif 277534d2123cS[email protected] 277686805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 2777ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 2778ccda6e14S[email protected] // see end of function, too. 2779a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 2780a4f30ec0S[email protected] if (packet[2]) break; // status != 0 2781f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 278281d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 27834ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 2784c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 27854ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 278681d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 2787c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 2788c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 278981d2bdb2SMatthias Ringwald if (release_buffer){ 279081d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 279181d2bdb2SMatthias Ringwald } 2792c6a37cfdSMatthias Ringwald } 2793c6a37cfdSMatthias Ringwald } 279435454696SMatthias Ringwald 2795c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 2796c6a37cfdSMatthias Ringwald if (!conn) break; 2797046ec007SMatthias Ringwald // mark connection for shutdown 2798a70e17d4SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 2799046ec007SMatthias Ringwald 2800046ec007SMatthias Ringwald // emit dedicatd bonding event 2801046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 2802046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 2803046ec007SMatthias Ringwald } 2804046ec007SMatthias Ringwald 280535454696SMatthias Ringwald #ifdef ENABLE_BLE 2806d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 2807046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 28082b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 2809bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 28109a2e4658SMatthias Ringwald } 281135454696SMatthias Ringwald #endif 2812d70217a2SMatthias Ringwald #endif 2813ccda6e14S[email protected] break; 28146772a24cSmatthias.ringwald 2815c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 2816313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 2817d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2818c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 28197586ee35S[email protected] } else { 28207586ee35S[email protected] // if no special requests, just reboot stack 28217586ee35S[email protected] hci_power_control_off(); 28227586ee35S[email protected] hci_power_control_on(); 2823c68bdf90Smatthias.ringwald } 2824c68bdf90Smatthias.ringwald break; 2825c68bdf90Smatthias.ringwald 28260e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 28275cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 28285cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2829c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 2830f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 2831c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2832c4c88f1bSJakob Krantz if (!conn) break; 28335cf766e8SMatthias Ringwald conn->role = packet[9]; 28345cf766e8SMatthias Ringwald break; 28350e6f3837SMatthias Ringwald #endif 28365cf766e8SMatthias Ringwald 283763fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2838d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 28394fa24b5fS[email protected] if (hci_transport_synchronous()) { 284063fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 28414fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 28424fa24b5fS[email protected] } 284381d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 2844d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2845d051460cS[email protected] hci_release_packet_buffer(); 2846701e3307SMatthias Ringwald 284763fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 284863fa3374SMatthias Ringwald 284935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 285063fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2851701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 285235454696SMatthias Ringwald #endif 28536b4af23dS[email protected] break; 28546b4af23dS[email protected] 285535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 285663fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 285763fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2858f234b250SMatthias Ringwald hci_stack->sco_can_send_now = 1; 285963fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 286063fa3374SMatthias Ringwald return; 28611cfb383eSMatthias Ringwald 28621cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 28631cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 28641cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 28651cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 28669784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 28671cfb383eSMatthias Ringwald break; 286835454696SMatthias Ringwald #endif 286963fa3374SMatthias Ringwald 2870a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 28715909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 28725909f7f2Smatthias.ringwald switch (packet[2]){ 2873d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 287457c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 28754f4e0224SMatthias Ringwald // log_info("advertising report received"); 28766fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 287757c9da5bS[email protected] le_handle_advertisement_report(packet, size); 28787bdc6798S[email protected] break; 2879d70217a2SMatthias Ringwald #endif 28805909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 28810ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 28825909f7f2Smatthias.ringwald break; 28835909f7f2Smatthias.ringwald 2884f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 2885c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 2886c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 2887c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 2888c9db5c21SMilanka Ringwald if (!conn) break; 2889c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 2890c9db5c21SMilanka Ringwald break; 289165a46ef3S[email protected] 289273cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 289373cd8a2aSMatthias Ringwald // connection 289473cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 289573cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 289673cd8a2aSMatthias Ringwald if (conn) { 289773cd8a2aSMatthias Ringwald // read arguments 289873cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 289973cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 290073cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 290173cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 290273cd8a2aSMatthias Ringwald 290373cd8a2aSMatthias Ringwald // validate against current connection parameter range 290473cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 290573cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 290673cd8a2aSMatthias Ringwald int update_parameter = gap_connection_parameter_range_included(&existing_range, le_conn_interval_min, le_conn_interval_max, le_conn_latency, le_supervision_timeout); 290773cd8a2aSMatthias Ringwald if (update_parameter){ 290873cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 290973cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 291073cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 291173cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 291273cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 291373cd8a2aSMatthias Ringwald } else { 2914c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 291573cd8a2aSMatthias Ringwald } 291673cd8a2aSMatthias Ringwald } 291773cd8a2aSMatthias Ringwald break; 29180f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 29190f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 29200f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 29210f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 29220f3b27c5SMatthias Ringwald if (conn) { 29230f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 29240f3b27c5SMatthias Ringwald } 29250f3b27c5SMatthias Ringwald break; 29260f3b27c5SMatthias Ringwald #endif 29275909f7f2Smatthias.ringwald default: 29285909f7f2Smatthias.ringwald break; 29295909f7f2Smatthias.ringwald } 29305909f7f2Smatthias.ringwald break; 29315909f7f2Smatthias.ringwald #endif 29320b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 29330b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 29340b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 29350b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 29360b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 29370b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 29380b36101dSMatthias Ringwald break; 29390b36101dSMatthias Ringwald default: 29400b36101dSMatthias Ringwald break; 29410b36101dSMatthias Ringwald } 29420b36101dSMatthias Ringwald break; 29436772a24cSmatthias.ringwald default: 29446772a24cSmatthias.ringwald break; 2945fe1ed1b8Smatthias.ringwald } 2946fe1ed1b8Smatthias.ringwald 294767c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 294889db417bSmatthias.ringwald 294986805605S[email protected] // notify upper stack 2950d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 295194ab26f8Smatthias.ringwald 295286805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 2953797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 2954f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 295505ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 2956046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 2957797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 295805ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 2959b0136355SMatthias Ringwald } 296086805605S[email protected] } 296186805605S[email protected] 296294ab26f8Smatthias.ringwald // execute main loop 296394ab26f8Smatthias.ringwald hci_run(); 296416833f0aSmatthias.ringwald } 296516833f0aSmatthias.ringwald 296635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 29676f28d2eeSMatthias Ringwald 29686be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 29696f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 29706f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 29716f28d2eeSMatthias Ringwald 29726f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 297359c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 29745a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 29756f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 29766f28d2eeSMatthias Ringwald if (!conn) return; 29776f28d2eeSMatthias Ringwald 2978afaddd67SMatthias Ringwald // trigger send 2979afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 2980e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 2981a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 2982e4157653SMatthias Ringwald conn->sco_tx_ready++; 2983e4157653SMatthias Ringwald } 2984afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 29856f28d2eeSMatthias Ringwald } 29866f28d2eeSMatthias Ringwald 298759c33575SMatthias Ringwald 298859c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 298959c33575SMatthias Ringwald 29906f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 299159c33575SMatthias Ringwald 29926f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 299359c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 299459c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 299559c33575SMatthias Ringwald 299659c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 299759c33575SMatthias Ringwald 299859c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 299959c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 300059c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 300159c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 300259c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 30036f28d2eeSMatthias Ringwald } 30046be3cf7fSMatthias Ringwald #endif 30056f28d2eeSMatthias Ringwald 3006c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 30070b3f95dfSMatthias Ringwald // lookup connection struct 30082b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 30092b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 30100b3f95dfSMatthias Ringwald if (!conn) return; 30110b3f95dfSMatthias Ringwald 30126be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3013760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 3014760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 3015a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 3016760b20efSMatthias Ringwald packet[2] = 0x3c; 3017760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 3018760b20efSMatthias Ringwald size = 63; 30190b3f95dfSMatthias Ringwald } 30200b3f95dfSMatthias Ringwald } 3021760b20efSMatthias Ringwald 3022f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 3023f234b250SMatthias Ringwald // Nothing to do 3024f234b250SMatthias Ringwald } else { 302549205f5dSMatthias Ringwald // log_debug("sco flow %u, handle 0x%04x, packets sent %u, bytes send %u", hci_stack->synchronous_flow_control_enabled, (int) con_handle, conn->num_packets_sent, conn->num_sco_bytes_sent); 3026760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 30276f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 302859c33575SMatthias Ringwald 302959c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 303059c33575SMatthias Ringwald // ignore first 10 packets 30316f28d2eeSMatthias Ringwald conn->sco_rx_count++; 303259c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 303359c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 303459c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 30356f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 30366f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 303759c33575SMatthias Ringwald conn->sco_rx_count = 0; 303859c33575SMatthias Ringwald sco_schedule_tx(conn); 303959c33575SMatthias Ringwald } 304059c33575SMatthias Ringwald } else { 304159c33575SMatthias Ringwald // track expected arrival timme 304259c33575SMatthias Ringwald conn->sco_rx_count++; 304359c33575SMatthias Ringwald conn->sco_rx_ms += 7; 304459c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 304559c33575SMatthias Ringwald if (delta > 0){ 304659c33575SMatthias Ringwald conn->sco_rx_ms++; 304759c33575SMatthias Ringwald } 304859c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 30496f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 30506f28d2eeSMatthias Ringwald } 3051760b20efSMatthias Ringwald } 3052f234b250SMatthias Ringwald } 30536be3cf7fSMatthias Ringwald #endif 30546be3cf7fSMatthias Ringwald 30550b3f95dfSMatthias Ringwald // deliver to app 30560b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 30570b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 30580b3f95dfSMatthias Ringwald } 30590b3f95dfSMatthias Ringwald 3060ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3061ed325439SMatthias Ringwald // We can send one packet for each received packet 3062ed325439SMatthias Ringwald conn->sco_tx_ready++; 3063ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 3064ed325439SMatthias Ringwald #endif 3065ed325439SMatthias Ringwald 30660b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 30672b838201SMatthias Ringwald conn->num_packets_completed++; 30682b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 30692b838201SMatthias Ringwald hci_run(); 30702b838201SMatthias Ringwald #endif 3071c91d150bS[email protected] } 307235454696SMatthias Ringwald #endif 3073c91d150bS[email protected] 30740a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 30755bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 307610e830c9Smatthias.ringwald switch (packet_type) { 307710e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 307810e830c9Smatthias.ringwald event_handler(packet, size); 307910e830c9Smatthias.ringwald break; 308010e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 308110e830c9Smatthias.ringwald acl_handler(packet, size); 308210e830c9Smatthias.ringwald break; 308335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3084c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 3085c91d150bS[email protected] sco_handler(packet, size); 3086202c8a4cSMatthias Ringwald break; 308735454696SMatthias Ringwald #endif 308810e830c9Smatthias.ringwald default: 308910e830c9Smatthias.ringwald break; 309010e830c9Smatthias.ringwald } 309110e830c9Smatthias.ringwald } 309210e830c9Smatthias.ringwald 3093d6b06661SMatthias Ringwald /** 3094d6b06661SMatthias Ringwald * @brief Add event packet handler. 3095d6b06661SMatthias Ringwald */ 3096d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 3097d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 3098d6b06661SMatthias Ringwald } 3099d6b06661SMatthias Ringwald 3100d6b06661SMatthias Ringwald 3101fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 31023d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 3103fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 310416833f0aSmatthias.ringwald } 310516833f0aSmatthias.ringwald 310635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 31078abbe8b5SMatthias Ringwald /** 31088abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 31098abbe8b5SMatthias Ringwald */ 31103d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 31118abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 31128abbe8b5SMatthias Ringwald } 311335454696SMatthias Ringwald #endif 31148abbe8b5SMatthias Ringwald 311571de195eSMatthias Ringwald static void hci_state_reset(void){ 3116595bdbfbS[email protected] // no connections yet 3117595bdbfbS[email protected] hci_stack->connections = NULL; 311874308b23Smatthias.ringwald 311974308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 312074308b23Smatthias.ringwald // hci_stack->discoverable = 0; 312174308b23Smatthias.ringwald // hci_stack->connectable = 0; 312274308b23Smatthias.ringwald // hci_stack->bondable = 1; 3123b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 3124595bdbfbS[email protected] 312544935e40S[email protected] // buffer is free 312644935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 312744935e40S[email protected] 3128595bdbfbS[email protected] // no pending cmds 3129595bdbfbS[email protected] hci_stack->decline_reason = 0; 3130595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 3131595bdbfbS[email protected] 3132c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 3133c214d65bSMatthias Ringwald 3134bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 3135bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = 0xffff; 3136bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = 0xffff; 3137bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = 0xff; 3138bea424a5SMatthias Ringwald #endif 3139bea424a5SMatthias Ringwald 3140cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3141cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 3142cf01e888SMatthias Ringwald #endif 3143cf01e888SMatthias Ringwald 3144595bdbfbS[email protected] // LE 3145b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 3146b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 3147b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 3148d70217a2SMatthias Ringwald #endif 3149d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 31506fab74dbSMatthias Ringwald hci_stack->le_scanning_active = 0; 3151b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3152d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3153e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 3154d70217a2SMatthias Ringwald #endif 3155595bdbfbS[email protected] } 3156595bdbfbS[email protected] 315735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 31582d5e09d6SMatthias Ringwald /** 31592d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 31602d5e09d6SMatthias Ringwald */ 31612d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 31622d5e09d6SMatthias Ringwald // store and open remote device db 31632d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 31642d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 31652d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 31662d5e09d6SMatthias Ringwald } 31672d5e09d6SMatthias Ringwald } 316835454696SMatthias Ringwald #endif 31692d5e09d6SMatthias Ringwald 31702d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 3171475c8125Smatthias.ringwald 31723a9fb326S[email protected] #ifdef HAVE_MALLOC 31733a9fb326S[email protected] if (!hci_stack) { 31743a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 31753a9fb326S[email protected] } 31763a9fb326S[email protected] #else 31773a9fb326S[email protected] hci_stack = &hci_stack_static; 31783a9fb326S[email protected] #endif 317966fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 31803a9fb326S[email protected] 3181475c8125Smatthias.ringwald // reference to use transport layer implementation 31823a9fb326S[email protected] hci_stack->hci_transport = transport; 3183475c8125Smatthias.ringwald 318411e23e5fSmatthias.ringwald // reference to used config 31853a9fb326S[email protected] hci_stack->config = config; 318611e23e5fSmatthias.ringwald 318726a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 318826a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 318926a9b6daSMatthias Ringwald 31908fcba05dSmatthias.ringwald // max acl payload size defined in config.h 31913a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 31928fcba05dSmatthias.ringwald 319316833f0aSmatthias.ringwald // register packet handlers with transport 319410e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 3195f5454fc6Smatthias.ringwald 31963a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 3197e2386ba1S[email protected] 3198e2386ba1S[email protected] // class of device 31993a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 3200a45d6b9fS[email protected] 3201f20168b8Smatthias.ringwald // bondable by default 3202f20168b8Smatthias.ringwald hci_stack->bondable = 1; 3203f20168b8Smatthias.ringwald 3204e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 320563168530SMatthias Ringwald // classic name 320663168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 3207c4c88f1bSJakob Krantz 3208c4c88f1bSJakob Krantz // Master slave policy 3209c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 3210170fafaeSMatthias Ringwald 3211b4eb4420SMatthias Ringwald // Allow Role Switch 3212b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 3213b4eb4420SMatthias Ringwald 321478315a58SMatthias Ringwald // Default / minimum security level = 2 321578315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 321678315a58SMatthias Ringwald 3217cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 3218cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 3219d821984bSMatthias Ringwald 3220d821984bSMatthias Ringwald // Link Supervision Timeout 3221d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 3222d821984bSMatthias Ringwald 3223e9f343c8SMatthias Ringwald #endif 322463168530SMatthias Ringwald 322563048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 32263a9fb326S[email protected] hci_stack->ssp_enable = 1; 32273a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 32283a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 32293a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 323069a97523S[email protected] 32315d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 32325d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 32335d23aae8SMatthias Ringwald 3234fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 3235fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 3236d950d659SMatthias Ringwald 3237831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3238831711daSMatthias Ringwald // connection parameter to use for outgoing connections 3239cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 3240cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 3241831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 3242831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 3243831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 3244831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 3245831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 3246831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 32477261e5d8SMatthias Ringwald 32487261e5d8SMatthias Ringwald // default LE Scanning 32498b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 32508b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 32518b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 3252831711daSMatthias Ringwald #endif 3253831711daSMatthias Ringwald 32542b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 32552b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 32562b6ab3e6SMatthias Ringwald #endif 32572b6ab3e6SMatthias Ringwald 3258831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 3259831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 3260831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 3261831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 3262831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 3263831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 3264831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 3265831711daSMatthias Ringwald 3266595bdbfbS[email protected] hci_state_reset(); 3267475c8125Smatthias.ringwald } 3268475c8125Smatthias.ringwald 32694688f216SMatthias Ringwald void hci_deinit(void){ 32704688f216SMatthias Ringwald #ifdef HAVE_MALLOC 32714688f216SMatthias Ringwald if (hci_stack) { 32724688f216SMatthias Ringwald free(hci_stack); 32734688f216SMatthias Ringwald } 32744688f216SMatthias Ringwald #endif 32754688f216SMatthias Ringwald hci_stack = NULL; 3276b5bbcbf4SMatthias Ringwald 3277b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 32784688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 3279b5bbcbf4SMatthias Ringwald #endif 32804688f216SMatthias Ringwald } 32814688f216SMatthias Ringwald 32823fb36a29SMatthias Ringwald /** 32833fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 32843fb36a29SMatthias Ringwald */ 32853fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 32863fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 32873fb36a29SMatthias Ringwald 32883fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 32893fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 32903fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 32913fb36a29SMatthias Ringwald } 32923fb36a29SMatthias Ringwald } 32933fb36a29SMatthias Ringwald 3294fb55bd0aSMatthias Ringwald /** 3295d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 3296fb55bd0aSMatthias Ringwald */ 3297fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 3298fb55bd0aSMatthias Ringwald // references to used control implementation 3299fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 3300d0b87befSMatthias Ringwald // init with transport config 3301d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 3302fb55bd0aSMatthias Ringwald } 3303fb55bd0aSMatthias Ringwald 330471de195eSMatthias Ringwald void hci_close(void){ 3305404843c1Smatthias.ringwald // close remote device db 3306a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 3307a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 3308404843c1Smatthias.ringwald } 33097224be7eSMatthias Ringwald 33107224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 33117224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 33127224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 33137224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 33147224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 33157224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 33167224be7eSMatthias Ringwald hci_shutdown_connection(connection); 3317f5454fc6Smatthias.ringwald } 33187224be7eSMatthias Ringwald 3319f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 33203a9fb326S[email protected] 33213a9fb326S[email protected] #ifdef HAVE_MALLOC 33223a9fb326S[email protected] free(hci_stack); 33233a9fb326S[email protected] #endif 33243a9fb326S[email protected] hci_stack = NULL; 3325404843c1Smatthias.ringwald } 3326404843c1Smatthias.ringwald 3327cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3328cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 3329cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 3330cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 3331cb70c5abSMatthias Ringwald } 3332cb70c5abSMatthias Ringwald #endif 3333cb70c5abSMatthias Ringwald 333435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3335170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 3336170fafaeSMatthias Ringwald // validate ranage and set 3337170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 3338170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 3339170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 3340170fafaeSMatthias Ringwald } 334178315a58SMatthias Ringwald 334278315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 334378315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 334478315a58SMatthias Ringwald } 334578315a58SMatthias Ringwald 334678315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 334778315a58SMatthias Ringwald return hci_stack->gap_security_level; 334878315a58SMatthias Ringwald } 3349170fafaeSMatthias Ringwald #endif 3350170fafaeSMatthias Ringwald 3351170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 335260b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 33539e61646fS[email protected] hci_stack->class_of_device = class_of_device; 33549e61646fS[email protected] } 335576f27cffSMatthias Ringwald 3356c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 3357c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 3358c33e56d3SMatthias Ringwald } 3359c33e56d3SMatthias Ringwald 3360b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 3361b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 3362b4eb4420SMatthias Ringwald } 3363b4eb4420SMatthias Ringwald 3364b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 3365b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 3366b4eb4420SMatthias Ringwald } 3367b4eb4420SMatthias Ringwald 33680c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 33690c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 33700c3eb48dSMatthias Ringwald } 33710c3eb48dSMatthias Ringwald 337276f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 337376f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 337476f27cffSMatthias Ringwald } 337535454696SMatthias Ringwald #endif 33769e61646fS[email protected] 337776f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 3378f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 3379f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 33806535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 3381f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 3382f456b2d0S[email protected] } 338376f27cffSMatthias Ringwald #endif 3384f456b2d0S[email protected] 33858d213e1aSmatthias.ringwald // State-Module-Driver overview 33868d213e1aSmatthias.ringwald // state module low-level 33878d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 33888d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 33898d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 33908d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 3391d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 3392d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 3393c7e0c5f6Smatthias.ringwald 339440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 33957301ad89Smatthias.ringwald 3396038bc64cSmatthias.ringwald // power on 3397f9a30166Smatthias.ringwald int err = 0; 33983a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 3399d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 3400f9a30166Smatthias.ringwald } 3401038bc64cSmatthias.ringwald if (err){ 34029da54300S[email protected] log_error( "POWER_ON failed"); 3403038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3404038bc64cSmatthias.ringwald return err; 3405038bc64cSmatthias.ringwald } 3406038bc64cSmatthias.ringwald 340724b3c629SMatthias Ringwald // int chipset driver 34083fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 34093fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 34103fb36a29SMatthias Ringwald } 34113fb36a29SMatthias Ringwald 341224b3c629SMatthias Ringwald // init transport 341324b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 341424b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 341524b3c629SMatthias Ringwald } 341624b3c629SMatthias Ringwald 341724b3c629SMatthias Ringwald // open transport 341824b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 3419038bc64cSmatthias.ringwald if (err){ 34209da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 34213a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3422d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3423f9a30166Smatthias.ringwald } 3424038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3425038bc64cSmatthias.ringwald return err; 3426038bc64cSmatthias.ringwald } 34278d213e1aSmatthias.ringwald return 0; 34288d213e1aSmatthias.ringwald } 3429038bc64cSmatthias.ringwald 343040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 34318d213e1aSmatthias.ringwald 34329da54300S[email protected] log_info("hci_power_control_off"); 34339418f9c9Smatthias.ringwald 34348d213e1aSmatthias.ringwald // close low-level device 343524b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 34368d213e1aSmatthias.ringwald 34379da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 34389418f9c9Smatthias.ringwald 34398d213e1aSmatthias.ringwald // power off 34403a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3441d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 34428d213e1aSmatthias.ringwald } 34439418f9c9Smatthias.ringwald 34449da54300S[email protected] log_info("hci_power_control_off - control closed"); 34459418f9c9Smatthias.ringwald 34463a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 344772ea5239Smatthias.ringwald } 344872ea5239Smatthias.ringwald 344940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 345072ea5239Smatthias.ringwald 34519da54300S[email protected] log_info("hci_power_control_sleep"); 34523144bce4Smatthias.ringwald 3453b429b9b7Smatthias.ringwald #if 0 3454b429b9b7Smatthias.ringwald // don't close serial port during sleep 3455b429b9b7Smatthias.ringwald 345672ea5239Smatthias.ringwald // close low-level device 34573a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 3458b429b9b7Smatthias.ringwald #endif 345972ea5239Smatthias.ringwald 346072ea5239Smatthias.ringwald // sleep mode 34613a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 3462d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 346372ea5239Smatthias.ringwald } 3464b429b9b7Smatthias.ringwald 34653a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 34668d213e1aSmatthias.ringwald } 34678d213e1aSmatthias.ringwald 346840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 3469b429b9b7Smatthias.ringwald 34709da54300S[email protected] log_info("hci_power_control_wake"); 3471b429b9b7Smatthias.ringwald 3472b429b9b7Smatthias.ringwald // wake on 34733a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 3474d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 3475b429b9b7Smatthias.ringwald } 3476b429b9b7Smatthias.ringwald 3477b429b9b7Smatthias.ringwald #if 0 3478b429b9b7Smatthias.ringwald // open low-level device 34793a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 3480b429b9b7Smatthias.ringwald if (err){ 34819da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 34823a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3483d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3484b429b9b7Smatthias.ringwald } 3485b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 3486b429b9b7Smatthias.ringwald return err; 3487b429b9b7Smatthias.ringwald } 3488b429b9b7Smatthias.ringwald #endif 3489b429b9b7Smatthias.ringwald 3490b429b9b7Smatthias.ringwald return 0; 3491b429b9b7Smatthias.ringwald } 3492b429b9b7Smatthias.ringwald 349344935e40S[email protected] static void hci_power_transition_to_initializing(void){ 349444935e40S[email protected] // set up state machine 349544935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 349644935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 349744935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 34985c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 349944935e40S[email protected] } 3500b429b9b7Smatthias.ringwald 350142bd3d77SMatthias Ringwald // returns error 350242bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 350342bd3d77SMatthias Ringwald int err; 35048d213e1aSmatthias.ringwald switch (power_mode){ 35058d213e1aSmatthias.ringwald case HCI_POWER_ON: 35068d213e1aSmatthias.ringwald err = hci_power_control_on(); 350742bd3d77SMatthias Ringwald if (err != 0) { 3508f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 350997b61c7bS[email protected] return err; 351097b61c7bS[email protected] } 351144935e40S[email protected] hci_power_transition_to_initializing(); 35128d213e1aSmatthias.ringwald break; 35138d213e1aSmatthias.ringwald case HCI_POWER_OFF: 35148d213e1aSmatthias.ringwald // do nothing 35158d213e1aSmatthias.ringwald break; 35168d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3517b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 35188d213e1aSmatthias.ringwald break; 35197bbeb3adSMilanka Ringwald default: 35207bbeb3adSMilanka Ringwald btstack_assert(false); 35217bbeb3adSMilanka Ringwald break; 35228d213e1aSmatthias.ringwald } 352342bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 352442bd3d77SMatthias Ringwald } 35257301ad89Smatthias.ringwald 352642bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 35278d213e1aSmatthias.ringwald switch (power_mode){ 35288d213e1aSmatthias.ringwald case HCI_POWER_ON: 35298d213e1aSmatthias.ringwald // do nothing 35308d213e1aSmatthias.ringwald break; 35318d213e1aSmatthias.ringwald case HCI_POWER_OFF: 35328d213e1aSmatthias.ringwald // no connections yet, just turn it off 35338d213e1aSmatthias.ringwald hci_power_control_off(); 35348d213e1aSmatthias.ringwald break; 35358d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3536b546ac54Smatthias.ringwald // no connections yet, just turn it off 353772ea5239Smatthias.ringwald hci_power_control_sleep(); 35388d213e1aSmatthias.ringwald break; 35397bbeb3adSMilanka Ringwald default: 35407bbeb3adSMilanka Ringwald btstack_assert(false); 35417bbeb3adSMilanka Ringwald break; 35428d213e1aSmatthias.ringwald } 354342bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 354442bd3d77SMatthias Ringwald } 35457301ad89Smatthias.ringwald 354642bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 35478d213e1aSmatthias.ringwald switch (power_mode){ 35488d213e1aSmatthias.ringwald case HCI_POWER_ON: 35498d213e1aSmatthias.ringwald // do nothing 35508d213e1aSmatthias.ringwald break; 35518d213e1aSmatthias.ringwald case HCI_POWER_OFF: 3552c7e0c5f6Smatthias.ringwald // see hci_run 35533a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3554beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 35558d213e1aSmatthias.ringwald break; 35568d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3557b546ac54Smatthias.ringwald // see hci_run 35583a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 355974b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 35608d213e1aSmatthias.ringwald break; 35617bbeb3adSMilanka Ringwald default: 35627bbeb3adSMilanka Ringwald btstack_assert(false); 35637bbeb3adSMilanka Ringwald break; 35648d213e1aSmatthias.ringwald } 356542bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 356642bd3d77SMatthias Ringwald } 35677301ad89Smatthias.ringwald 356842bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 35698d213e1aSmatthias.ringwald switch (power_mode){ 35708d213e1aSmatthias.ringwald case HCI_POWER_ON: 357144935e40S[email protected] hci_power_transition_to_initializing(); 35728d213e1aSmatthias.ringwald break; 35738d213e1aSmatthias.ringwald case HCI_POWER_OFF: 35748d213e1aSmatthias.ringwald // do nothing 35758d213e1aSmatthias.ringwald break; 35768d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3577b546ac54Smatthias.ringwald // see hci_run 35783a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 357974b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 35808d213e1aSmatthias.ringwald break; 35817bbeb3adSMilanka Ringwald default: 35827bbeb3adSMilanka Ringwald btstack_assert(false); 35837bbeb3adSMilanka Ringwald break; 35848d213e1aSmatthias.ringwald } 358500278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 358642bd3d77SMatthias Ringwald } 35878d213e1aSmatthias.ringwald 358842bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 35898d213e1aSmatthias.ringwald switch (power_mode){ 35908d213e1aSmatthias.ringwald case HCI_POWER_ON: 359128171530Smatthias.ringwald 3592423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 359328171530Smatthias.ringwald // nothing to do, if H4 supports power management 3594d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 35953a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 359674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 359728171530Smatthias.ringwald break; 359828171530Smatthias.ringwald } 359928171530Smatthias.ringwald #endif 360044935e40S[email protected] hci_power_transition_to_initializing(); 36018d213e1aSmatthias.ringwald break; 36028d213e1aSmatthias.ringwald case HCI_POWER_OFF: 3603b546ac54Smatthias.ringwald // see hci_run 36043a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3605beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 36068d213e1aSmatthias.ringwald break; 36078d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3608b546ac54Smatthias.ringwald // do nothing 36098d213e1aSmatthias.ringwald break; 36107bbeb3adSMilanka Ringwald default: 36117bbeb3adSMilanka Ringwald btstack_assert(false); 36127bbeb3adSMilanka Ringwald break; 36138d213e1aSmatthias.ringwald } 361442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 361542bd3d77SMatthias Ringwald } 36168d213e1aSmatthias.ringwald 361742bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 361842bd3d77SMatthias Ringwald int err; 36198d213e1aSmatthias.ringwald switch (power_mode){ 36208d213e1aSmatthias.ringwald case HCI_POWER_ON: 3621423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 362228171530Smatthias.ringwald // nothing to do, if H4 supports power management 3623d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 36243a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 36255c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 3626758b46ceSmatthias.ringwald hci_update_scan_enable(); 362728171530Smatthias.ringwald break; 362828171530Smatthias.ringwald } 362928171530Smatthias.ringwald #endif 36303144bce4Smatthias.ringwald err = hci_power_control_wake(); 36313144bce4Smatthias.ringwald if (err) return err; 363244935e40S[email protected] hci_power_transition_to_initializing(); 36338d213e1aSmatthias.ringwald break; 36348d213e1aSmatthias.ringwald case HCI_POWER_OFF: 36353a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3636beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 36378d213e1aSmatthias.ringwald break; 36388d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3639b546ac54Smatthias.ringwald // do nothing 36408d213e1aSmatthias.ringwald break; 36417bbeb3adSMilanka Ringwald default: 36427bbeb3adSMilanka Ringwald btstack_assert(false); 36437bbeb3adSMilanka Ringwald break; 36448d213e1aSmatthias.ringwald } 364542bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 364642bd3d77SMatthias Ringwald } 36477bbeb3adSMilanka Ringwald 364842bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 364942bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 365024f87726SMatthias Ringwald int err = 0; 365142bd3d77SMatthias Ringwald switch (hci_stack->state){ 365242bd3d77SMatthias Ringwald case HCI_STATE_OFF: 365342bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 365442bd3d77SMatthias Ringwald break; 365542bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 365642bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 365742bd3d77SMatthias Ringwald break; 365842bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 365942bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 366042bd3d77SMatthias Ringwald break; 366142bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 366242bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 366342bd3d77SMatthias Ringwald break; 366442bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 366542bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 366642bd3d77SMatthias Ringwald break; 366742bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 366842bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 366942bd3d77SMatthias Ringwald break; 36707bbeb3adSMilanka Ringwald default: 36717bbeb3adSMilanka Ringwald btstack_assert(false); 36727bbeb3adSMilanka Ringwald break; 367311e23e5fSmatthias.ringwald } 367424f87726SMatthias Ringwald if (err != 0){ 367542bd3d77SMatthias Ringwald return err; 367642bd3d77SMatthias Ringwald } 367768d92d03Smatthias.ringwald 3678038bc64cSmatthias.ringwald // create internal event 3679ee8bf225Smatthias.ringwald hci_emit_state(); 3680ee8bf225Smatthias.ringwald 368168d92d03Smatthias.ringwald // trigger next/first action 368268d92d03Smatthias.ringwald hci_run(); 368368d92d03Smatthias.ringwald 3684475c8125Smatthias.ringwald return 0; 3685475c8125Smatthias.ringwald } 3686475c8125Smatthias.ringwald 368735454696SMatthias Ringwald 368835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 368935454696SMatthias Ringwald 3690758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 3691758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 3692a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 3693758b46ceSmatthias.ringwald hci_run(); 3694758b46ceSmatthias.ringwald } 3695758b46ceSmatthias.ringwald 369615a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 3697381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 3698381fbed8Smatthias.ringwald 36993a9fb326S[email protected] if (hci_stack->discoverable == enable){ 37003a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 3701381fbed8Smatthias.ringwald return; 3702381fbed8Smatthias.ringwald } 3703381fbed8Smatthias.ringwald 37043a9fb326S[email protected] hci_stack->discoverable = enable; 3705758b46ceSmatthias.ringwald hci_update_scan_enable(); 3706758b46ceSmatthias.ringwald } 3707b031bebbSmatthias.ringwald 370815a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 3709758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 3710758b46ceSmatthias.ringwald 3711758b46ceSmatthias.ringwald // don't emit event 37123a9fb326S[email protected] if (hci_stack->connectable == enable) return; 3713758b46ceSmatthias.ringwald 37143a9fb326S[email protected] hci_stack->connectable = enable; 3715758b46ceSmatthias.ringwald hci_update_scan_enable(); 3716381fbed8Smatthias.ringwald } 371735454696SMatthias Ringwald #endif 3718381fbed8Smatthias.ringwald 371915a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 37206535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 37215061f3afS[email protected] } 37225061f3afS[email protected] 37232b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 37242b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 37252b838201SMatthias Ringwald 37262b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 37272b838201SMatthias Ringwald hci_reserve_packet_buffer(); 37282b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 37292b838201SMatthias Ringwald 37302b838201SMatthias Ringwald uint16_t size = 0; 37312b838201SMatthias Ringwald uint16_t num_handles = 0; 37322b838201SMatthias Ringwald packet[size++] = 0x35; 37332b838201SMatthias Ringwald packet[size++] = 0x0c; 37342b838201SMatthias Ringwald size++; // skip param len 37352b838201SMatthias Ringwald size++; // skip num handles 37362b838201SMatthias Ringwald 37372b838201SMatthias Ringwald // add { handle, packets } entries 37382b838201SMatthias Ringwald btstack_linked_item_t * it; 37392b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 37402b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 37412b838201SMatthias Ringwald if (connection->num_packets_completed){ 37422b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 37432b838201SMatthias Ringwald size += 2; 37442b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 37452b838201SMatthias Ringwald size += 2; 37462b838201SMatthias Ringwald // 37472b838201SMatthias Ringwald num_handles++; 37482b838201SMatthias Ringwald connection->num_packets_completed = 0; 37492b838201SMatthias Ringwald } 37502b838201SMatthias Ringwald } 37512b838201SMatthias Ringwald 37522b838201SMatthias Ringwald packet[2] = size - 3; 37532b838201SMatthias Ringwald packet[3] = num_handles; 37542b838201SMatthias Ringwald 37552b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 37562b838201SMatthias Ringwald 37572b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 37582b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 37592b838201SMatthias Ringwald 37602b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 37612b838201SMatthias Ringwald if (hci_transport_synchronous()){ 3762e2d22487SMatthias Ringwald hci_release_packet_buffer(); 3763068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 37642b838201SMatthias Ringwald } 37652b838201SMatthias Ringwald } 37662b838201SMatthias Ringwald #endif 37672b838201SMatthias Ringwald 376826fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 376926fe9592SMatthias Ringwald UNUSED(ds); 377026fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 3771beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 3772beceeddeSMatthias Ringwald hci_emit_state(); 377326fe9592SMatthias Ringwald hci_run(); 377426fe9592SMatthias Ringwald } 377526fe9592SMatthias Ringwald 3776f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 37774ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3778b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 3779b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 3780b5d8b22bS[email protected] if (connection) { 378128a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 3782b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 3783f30077b7SMatthias Ringwald return true; 3784b5d8b22bS[email protected] } 378528a0332dSMatthias Ringwald } else { 3786b5d8b22bS[email protected] // connection gone -> discard further fragments 378728a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 3788b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 3789b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 3790b5d8b22bS[email protected] } 3791b5d8b22bS[email protected] } 3792f30077b7SMatthias Ringwald return false; 37932b838201SMatthias Ringwald } 3794b031bebbSmatthias.ringwald 379535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3796f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 3797f30077b7SMatthias Ringwald 3798b031bebbSmatthias.ringwald // decline incoming connections 37993a9fb326S[email protected] if (hci_stack->decline_reason){ 38003a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 38013a9fb326S[email protected] hci_stack->decline_reason = 0; 38023a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 3803f30077b7SMatthias Ringwald return true; 3804ce4c8fabSmatthias.ringwald } 3805bea424a5SMatthias Ringwald // write page scan activity 3806bea424a5SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && (hci_stack->new_page_scan_interval != 0xffff) && hci_classic_supported()){ 3807bea424a5SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 3808bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = 0xffff; 3809bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = 0xffff; 3810bea424a5SMatthias Ringwald return true; 3811bea424a5SMatthias Ringwald } 3812bea424a5SMatthias Ringwald // write page scan type 3813bea424a5SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && (hci_stack->new_page_scan_type != 0xff) && hci_classic_supported()){ 3814bea424a5SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 3815bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = 0xff; 3816bea424a5SMatthias Ringwald return true; 3817bea424a5SMatthias Ringwald } 3818b031bebbSmatthias.ringwald // send scan enable 38190e588213SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && (hci_stack->new_scan_enable_value != 0xff) && hci_classic_supported()){ 38203a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 38213a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 3822f30077b7SMatthias Ringwald return true; 3823b031bebbSmatthias.ringwald } 3824f5875de5SMatthias Ringwald // start/stop inquiry 3825a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 3826f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 3827beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 38288aee7be2SMilanka Ringwald hci_send_cmd(&hci_inquiry, GAP_IAC_GENERAL_INQUIRY, duration, 0); 3829f30077b7SMatthias Ringwald return true; 3830f5875de5SMatthias Ringwald } 3831f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 3832f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 3833f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 3834f30077b7SMatthias Ringwald return true; 3835f5875de5SMatthias Ringwald } 3836b7f1ee76SMatthias Ringwald // remote name request 3837b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 3838b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 3839b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 3840ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 3841f30077b7SMatthias Ringwald return true; 3842b7f1ee76SMatthias Ringwald } 3843cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3844cf01e888SMatthias Ringwald // Local OOB data 3845cf01e888SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && hci_stack->classic_read_local_oob_data){ 3846cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 3847cf01e888SMatthias Ringwald if (hci_stack->local_supported_commands[1] & 0x10u){ 3848cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 3849cf01e888SMatthias Ringwald } else { 3850cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 3851cf01e888SMatthias Ringwald } 3852cf01e888SMatthias Ringwald } 3853cf01e888SMatthias Ringwald #endif 38540a51f88bSMatthias Ringwald // pairing 38550a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 38560a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 38570a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 38583f659ee4SMilanka Ringwald uint8_t pin_code[16]; 38590a51f88bSMatthias Ringwald switch (state){ 38600a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 38613f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 38623f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 38633f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 38640a51f88bSMatthias Ringwald break; 38650a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 38660a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 38670a51f88bSMatthias Ringwald break; 38680a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 3869d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 38700a51f88bSMatthias Ringwald break; 38710a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 38720a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 38730a51f88bSMatthias Ringwald break; 38740a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 38750a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 38760a51f88bSMatthias Ringwald break; 38770a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 38780a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 38790a51f88bSMatthias Ringwald break; 38800a51f88bSMatthias Ringwald default: 38810a51f88bSMatthias Ringwald break; 38820a51f88bSMatthias Ringwald } 3883f30077b7SMatthias Ringwald return true; 3884f30077b7SMatthias Ringwald } 3885f30077b7SMatthias Ringwald return false; 38860a51f88bSMatthias Ringwald } 388735454696SMatthias Ringwald #endif 3888b031bebbSmatthias.ringwald 3889a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 3890f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 3891f30077b7SMatthias Ringwald 3892f30077b7SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 3893f30077b7SMatthias Ringwald 3894f30077b7SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 38954ea43905SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 3896d70217a2SMatthias Ringwald 3897a41310b7SMatthias Ringwald 3898a41310b7SMatthias Ringwald // Phase 1: collect what to stop 3899a41310b7SMatthias Ringwald 3900a41310b7SMatthias Ringwald bool scanning_stop = false; 3901a41310b7SMatthias Ringwald bool connecting_stop = false; 3902a41310b7SMatthias Ringwald bool advertising_stop = false; 3903a41310b7SMatthias Ringwald 3904a41310b7SMatthias Ringwald #ifndef ENABLE_LE_CENTRAL 3905a41310b7SMatthias Ringwald UNUSED(scanning_stop); 39060abd9f64SMatthias Ringwald UNUSED(connecting_stop); 3907a41310b7SMatthias Ringwald #endif 3908a41310b7SMatthias Ringwald #ifndef ENABLE_LE_PERIPHERAL 3909a41310b7SMatthias Ringwald UNUSED(advertising_stop); 3910a41310b7SMatthias Ringwald #endif 3911a41310b7SMatthias Ringwald 391229c24bebSMatthias Ringwald // check if whitelist needs modification 391329c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 391429c24bebSMatthias Ringwald btstack_linked_list_iterator_t lit; 391529c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 391629c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 391729c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 391829c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 391929c24bebSMatthias Ringwald whitelist_modification_pending = true; 392029c24bebSMatthias Ringwald break; 392129c24bebSMatthias Ringwald } 392229c24bebSMatthias Ringwald } 392321debf25SMatthias Ringwald // check if resolving list needs modification 392421debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 392521debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 3926ea151974SMatthias Ringwald bool resolving_list_supported = (hci_stack->local_supported_commands[1] & (1 << 2)) != 0; 3927ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 392821debf25SMatthias Ringwald resolving_list_modification_pending = true; 392921debf25SMatthias Ringwald } 393021debf25SMatthias Ringwald #endif 393129c24bebSMatthias Ringwald 3932d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3933fde725feSMatthias Ringwald // scanning control 39343251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 393529c24bebSMatthias Ringwald // stop if: 393629c24bebSMatthias Ringwald // - parameter change required 393729c24bebSMatthias Ringwald // - it's disabled 393829c24bebSMatthias Ringwald // - whitelist change required but used for scanning 393921debf25SMatthias Ringwald // - resolving list modified 394029c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 394121debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 394221debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 394321debf25SMatthias Ringwald scanning_uses_whitelist || 394421debf25SMatthias Ringwald resolving_list_modification_pending){ 394521debf25SMatthias Ringwald 39462d5c2a27SMatthias Ringwald scanning_stop = true; 3947fde725feSMatthias Ringwald } 3948fde725feSMatthias Ringwald } 394929c24bebSMatthias Ringwald #endif 3950fde725feSMatthias Ringwald 395129c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 395229c24bebSMatthias Ringwald // connecting control 3953f496d06eSMatthias Ringwald bool connecting_with_whitelist; 3954f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 3955f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 3956f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 3957af64f147SMatthias Ringwald // stop connecting if: 3958af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 3959af64f147SMatthias Ringwald // - if it got disabled 396021debf25SMatthias Ringwald // - resolving list modified 3961f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 3962f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 396321debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 396421debf25SMatthias Ringwald resolving_list_modification_pending) { 396521debf25SMatthias Ringwald 396629c24bebSMatthias Ringwald connecting_stop = true; 396729c24bebSMatthias Ringwald } 3968f496d06eSMatthias Ringwald break; 3969f496d06eSMatthias Ringwald default: 3970f496d06eSMatthias Ringwald break; 397129c24bebSMatthias Ringwald } 3972d70217a2SMatthias Ringwald #endif 397329c24bebSMatthias Ringwald 3974d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 397545c102fdSMatthias Ringwald // le advertisement control 3976bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_active){ 397729c24bebSMatthias Ringwald // stop if: 397829c24bebSMatthias Ringwald // - parameter change required 397929c24bebSMatthias Ringwald // - it's disabled 3980ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 398121debf25SMatthias Ringwald // - resolving list modified 398229c24bebSMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy > 0; 398321debf25SMatthias Ringwald if ((hci_stack->le_advertisements_todo != 0) || 398421debf25SMatthias Ringwald !hci_stack->le_advertisements_enabled_for_current_roles || 398521debf25SMatthias Ringwald (advertising_uses_whitelist & whitelist_modification_pending) || 398621debf25SMatthias Ringwald resolving_list_modification_pending) { 398721debf25SMatthias Ringwald 3988fde725feSMatthias Ringwald advertising_stop = true; 3989fde725feSMatthias Ringwald } 3990fde725feSMatthias Ringwald } 3991a41310b7SMatthias Ringwald #endif 3992fde725feSMatthias Ringwald 3993a41310b7SMatthias Ringwald 3994a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 3995a41310b7SMatthias Ringwald 3996a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3997a41310b7SMatthias Ringwald if (scanning_stop){ 39985226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 3999a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 4000a41310b7SMatthias Ringwald return true; 4001a41310b7SMatthias Ringwald } 4002a41310b7SMatthias Ringwald #endif 4003a41310b7SMatthias Ringwald 4004a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4005a41310b7SMatthias Ringwald if (connecting_stop){ 4006a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 4007a41310b7SMatthias Ringwald return true; 4008a41310b7SMatthias Ringwald } 4009a41310b7SMatthias Ringwald #endif 4010a41310b7SMatthias Ringwald 4011a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4012fde725feSMatthias Ringwald if (advertising_stop){ 40135226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = false; 401445c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 4015f30077b7SMatthias Ringwald return true; 401645c102fdSMatthias Ringwald } 4017a41310b7SMatthias Ringwald #endif 4018fde725feSMatthias Ringwald 4019a41310b7SMatthias Ringwald // Phase 3: modify 4020a41310b7SMatthias Ringwald 4021a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4022a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 4023a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 4024a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 4025a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 4026a41310b7SMatthias Ringwald return true; 4027a41310b7SMatthias Ringwald } 4028a41310b7SMatthias Ringwald #endif 4029a41310b7SMatthias Ringwald 4030a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 403145c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 403245c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 403345c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 403445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 403545c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 403645c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 4037b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, 403845c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 403945c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 404045c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 404145c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 4042f30077b7SMatthias Ringwald return true; 404345c102fdSMatthias Ringwald } 4044501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 4045501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 40467a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 40477a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 40486535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 40496535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 40503c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 40517a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 4052f30077b7SMatthias Ringwald return true; 405345c102fdSMatthias Ringwald } 4054501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 4055501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 4056f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 4057f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 40586535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 40596535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 40603c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 4061214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 4062f30077b7SMatthias Ringwald return true; 4063501f56b3SMatthias Ringwald } 4064d70217a2SMatthias Ringwald #endif 40659956955bSMatthias Ringwald 406613eb2a2eSMatthias Ringwald 4067057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4068a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 4069a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 4070057ab60cSMatthias Ringwald #endif 4071057ab60cSMatthias Ringwald 4072a41310b7SMatthias Ringwald // LE Whitelist Management 4073a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 40749956955bSMatthias Ringwald // add/remove entries 4075665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4076665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4077665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4078453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 4079453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4080453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 4081453459ddSMatthias Ringwald return true; 4082453459ddSMatthias Ringwald } 40839956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 4084453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 4085453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 40869956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 4087f30077b7SMatthias Ringwald return true; 40889956955bSMatthias Ringwald } 4089453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 4090665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 40919956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 40929956955bSMatthias Ringwald } 40939956955bSMatthias Ringwald } 409491915b0bSMatthias Ringwald } 40959956955bSMatthias Ringwald 409621debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 409721debf25SMatthias Ringwald // LE Resolving List Management 4098ea151974SMatthias Ringwald if (resolving_list_supported) { 409921debf25SMatthias Ringwald uint16_t i; 410021debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 410121debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 410221debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 410321debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 410421debf25SMatthias Ringwald return true; 410521debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 410621debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 410721debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 410821debf25SMatthias Ringwald return true; 410921debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 411002b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 4111ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 4112ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 4113ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 4114ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 411521debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 411621debf25SMatthias Ringwald return true; 411702b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 411802b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 411902b02cffSMatthias Ringwald uint8_t offset = i >> 3; 412002b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 412102b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 412202b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 412302b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 412402b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 412502b02cffSMatthias Ringwald sm_key_t peer_irk; 412602b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 412702b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 4128f5228c62SMatthias Ringwald 4129f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 4130f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 4131f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4132f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 4133f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 4134f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 4135f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 4136f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 4137f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 4138f5228c62SMatthias Ringwald } 4139f5228c62SMatthias Ringwald #endif 4140f5228c62SMatthias Ringwald 4141ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 4142ea151974SMatthias Ringwald peer_identity_addreses); 414302b02cffSMatthias Ringwald return true; 414402b02cffSMatthias Ringwald } 414502b02cffSMatthias Ringwald 414602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 414702b02cffSMatthias Ringwald 414802b02cffSMatthias Ringwald /* fall through */ 414902b02cffSMatthias Ringwald 415021debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 415121debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 415221debf25SMatthias Ringwald uint8_t offset = i >> 3; 415321debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 415402b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 415502b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 415621debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 415721debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 415821debf25SMatthias Ringwald sm_key_t peer_irk; 415921debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 416021debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 416121debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 416221debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 416321debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 416421debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 416521debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 416621debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 4167ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 4168ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 416921debf25SMatthias Ringwald return true; 417021debf25SMatthias Ringwald } 417102b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 417221debf25SMatthias Ringwald break; 417302b02cffSMatthias Ringwald 417421debf25SMatthias Ringwald default: 417521debf25SMatthias Ringwald break; 417621debf25SMatthias Ringwald } 4177ea151974SMatthias Ringwald } 417821debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 417921debf25SMatthias Ringwald #endif 4180a41310b7SMatthias Ringwald 4181a41310b7SMatthias Ringwald // Phase 4: restore state 418229c24bebSMatthias Ringwald 418329c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4184af64f147SMatthias Ringwald // re-start scanning 418529c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 418629c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 418729c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 418829c24bebSMatthias Ringwald return true; 418929c24bebSMatthias Ringwald } 419029c24bebSMatthias Ringwald #endif 419129c24bebSMatthias Ringwald 419213eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4193af64f147SMatthias Ringwald // re-start connecting 4194af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 41959956955bSMatthias Ringwald bd_addr_t null_addr; 41969956955bSMatthias Ringwald memset(null_addr, 0, 6); 41979956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 4198cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 4199cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 42009956955bSMatthias Ringwald 1, // use whitelist 42019956955bSMatthias Ringwald 0, // peer address type 42029956955bSMatthias Ringwald null_addr, // peer bd addr 4203b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 420473044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 420573044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 420673044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 420773044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 420873044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 420973044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 42109956955bSMatthias Ringwald ); 4211f30077b7SMatthias Ringwald return true; 42129956955bSMatthias Ringwald } 4213d70217a2SMatthias Ringwald #endif 4214a41310b7SMatthias Ringwald 4215a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4216a41310b7SMatthias Ringwald // re-start advertising 42178978dcf1SMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && !hci_stack->le_advertisements_active){ 4218a41310b7SMatthias Ringwald // check if advertisements should be enabled given 42195226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = true; 4220a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 4221a41310b7SMatthias Ringwald return true; 4222a41310b7SMatthias Ringwald } 4223a41310b7SMatthias Ringwald #endif 4224a41310b7SMatthias Ringwald 4225f30077b7SMatthias Ringwald return false; 42267bdc6798S[email protected] } 4227b2f949feS[email protected] #endif 42287bdc6798S[email protected] 422988a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 4230f30077b7SMatthias Ringwald btstack_linked_item_t * it; 4231a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 423205ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 423332ab9390Smatthias.ringwald 42340bf6344aS[email protected] switch(connection->state){ 42350bf6344aS[email protected] case SEND_CREATE_CONNECTION: 42364f3229d8S[email protected] switch(connection->address_type){ 423735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4238f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 42399da54300S[email protected] log_info("sending hci_create_connection"); 4240b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 42414f3229d8S[email protected] break; 424235454696SMatthias Ringwald #endif 42434f3229d8S[email protected] default: 4244a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4245d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 42469da54300S[email protected] log_info("sending hci_le_create_connection"); 42474f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 4248cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 4249cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 42504f3229d8S[email protected] 0, // don't use whitelist 42514f3229d8S[email protected] connection->address_type, // peer address type 42524f3229d8S[email protected] connection->address, // peer bd addr 4253b95a5a35SMatthias Ringwald hci_stack->le_own_addr_type, // our addr type: 425473044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 425573044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 425673044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 425773044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 425873044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 425973044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 42604f3229d8S[email protected] ); 42614f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 4262b2f949feS[email protected] #endif 4263d70217a2SMatthias Ringwald #endif 42644f3229d8S[email protected] break; 42654f3229d8S[email protected] } 4266f30077b7SMatthias Ringwald return true; 4267ad83dc6aS[email protected] 426835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 42690bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 42705cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 4271f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 427276ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 4273895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 4274c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 4275e35edcc1S[email protected] } 4276f30077b7SMatthias Ringwald return true; 427735454696SMatthias Ringwald #endif 42780bf6344aS[email protected] 4279a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4280d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 42810bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 42820bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 42830bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 4284f30077b7SMatthias Ringwald return true; 4285a6725849S[email protected] #endif 4286d70217a2SMatthias Ringwald #endif 42870bf6344aS[email protected] case SEND_DISCONNECT: 42880bf6344aS[email protected] connection->state = SENT_DISCONNECT; 42896ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4290f30077b7SMatthias Ringwald return true; 42910bf6344aS[email protected] 42920bf6344aS[email protected] default: 42930bf6344aS[email protected] break; 4294c7e0c5f6Smatthias.ringwald } 4295c7e0c5f6Smatthias.ringwald 4296cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 4297cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 4298cabf004eSMatthias Ringwald 4299891b9fc2SMatthias Ringwald if (connection->authentication_flags & READ_RSSI){ 4300891b9fc2SMatthias Ringwald connectionClearAuthenticationFlags(connection, READ_RSSI); 4301891b9fc2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 4302f30077b7SMatthias Ringwald return true; 4303891b9fc2SMatthias Ringwald } 4304891b9fc2SMatthias Ringwald 430594418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 430694418890SMatthias Ringwald 43076909f064SMatthias Ringwald if (connection->authentication_flags & WRITE_SUPERVISION_TIMEOUT){ 43086909f064SMatthias Ringwald connectionClearAuthenticationFlags(connection, WRITE_SUPERVISION_TIMEOUT); 43096909f064SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 4310f30077b7SMatthias Ringwald return true; 43116909f064SMatthias Ringwald } 43126909f064SMatthias Ringwald 431332ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 4314608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 431534d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 431630e72d78SMatthias Ringwald 431732ab9390Smatthias.ringwald link_key_t link_key; 4318c77e8838S[email protected] link_key_type_t link_key_type; 431930e72d78SMatthias Ringwald bool have_link_key = hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(connection->address, link_key, &link_key_type); 432030e72d78SMatthias Ringwald 432130e72d78SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 432230e72d78SMatthias Ringwald bool sc_enabled_remote = (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 432330e72d78SMatthias Ringwald bool sc_downgrade = have_link_key && (gap_secure_connection_for_link_key_type(link_key_type) == 1) && !sc_enabled_remote; 432430e72d78SMatthias Ringwald if (sc_downgrade){ 432530e72d78SMatthias Ringwald log_info("Link key based on SC, but remote does not support SC -> disconnect"); 432630e72d78SMatthias Ringwald connection->state = SENT_DISCONNECT; 432730e72d78SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 432830e72d78SMatthias Ringwald return true; 432930e72d78SMatthias Ringwald } 433030e72d78SMatthias Ringwald 433130e72d78SMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level); 433230e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 43339ab95c90S[email protected] connection->link_key_type = link_key_type; 433432ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 433532ab9390Smatthias.ringwald } else { 433632ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 433732ab9390Smatthias.ringwald } 4338f30077b7SMatthias Ringwald return true; 433932ab9390Smatthias.ringwald } 43401d6b20aeS[email protected] 4341899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 43429da54300S[email protected] log_info("denying to pin request"); 4343899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 434434d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 4345f30077b7SMatthias Ringwald return true; 43464c57c146S[email protected] } 43474c57c146S[email protected] 4348dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 434934d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 4350a8d20135SMatthias Ringwald // set authentication requirements: 4351a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 4352a8d20135SMatthias Ringwald // - BONDING MODE: Dedicated if requested, otherwise bondable flag 4353a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 43549faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 43559faad3abS[email protected] authreq |= 1; 4356106d6d11S[email protected] } 4357a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 4358a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 4359a8d20135SMatthias Ringwald } else if (hci_stack->bondable){ 4360a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 4361a8d20135SMatthias Ringwald } 43621849becdSMatthias Ringwald uint8_t have_oob_data = 0; 43631849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 43641849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 43651849becdSMatthias Ringwald have_oob_data |= 1; 43661849becdSMatthias Ringwald } 43671849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 43681849becdSMatthias Ringwald have_oob_data |= 2; 43691849becdSMatthias Ringwald } 43701849becdSMatthias Ringwald #endif 43711849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 437211b03efaSMatthias Ringwald return true; 4373f8fb5f6eS[email protected] } 437411b03efaSMatthias Ringwald 437511b03efaSMatthias Ringwald if (connection->authentication_flags & SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 437611b03efaSMatthias Ringwald connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 437711b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 4378f30077b7SMatthias Ringwald return true; 437932ab9390Smatthias.ringwald } 438032ab9390Smatthias.ringwald 43811849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 43821849becdSMatthias Ringwald if (connection->authentication_flags & SEND_REMOTE_OOB_DATA_REPLY){ 43831849becdSMatthias Ringwald connectionClearAuthenticationFlags(connection, SEND_REMOTE_OOB_DATA_REPLY); 43841849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 43851849becdSMatthias Ringwald const uint8_t * r_192 = zero; 43861849becdSMatthias Ringwald const uint8_t * c_192 = zero; 43871849becdSMatthias Ringwald const uint8_t * r_256 = zero; 43881849becdSMatthias Ringwald const uint8_t * c_256 = zero; 43891849becdSMatthias Ringwald // verify P-256 OOB 43901849becdSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && ((hci_stack->local_supported_commands[1] & 0x08u) != 0)) { 43911849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 43921849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 43931849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 43941849becdSMatthias Ringwald } 43951849becdSMatthias Ringwald } 43961849becdSMatthias Ringwald // verify P-192 OOB 43971849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 43981849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 43991849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 44001849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 44011849becdSMatthias Ringwald } 44021849becdSMatthias Ringwald } 44031849becdSMatthias Ringwald // Reply 44041849becdSMatthias Ringwald if (c_256 != zero) { 44051849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 44061849becdSMatthias Ringwald } else if (c_192 != zero){ 44071849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 44081849becdSMatthias Ringwald } else { 44091849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 44101849becdSMatthias Ringwald } 44111849becdSMatthias Ringwald return true; 44121849becdSMatthias Ringwald } 44131849becdSMatthias Ringwald #endif 44141849becdSMatthias Ringwald 4415dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 4416dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 441734d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 4418f30077b7SMatthias Ringwald return true; 4419dbe1a790S[email protected] } 4420dbe1a790S[email protected] 4421dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 4422dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 442334d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 4424f30077b7SMatthias Ringwald return true; 4425dbe1a790S[email protected] } 4426afd4e962S[email protected] 4427ba018746SMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 4428ba018746SMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 442934d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 4430f30077b7SMatthias Ringwald return true; 44312bd8b7e7S[email protected] } 44322bd8b7e7S[email protected] 44335ccef624SMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 44345ccef624SMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 44355ccef624SMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 44365ccef624SMatthias Ringwald return true; 44375ccef624SMatthias Ringwald } 44385ccef624SMatthias Ringwald 443982721f83SMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 444082721f83SMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 444182721f83SMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 444282721f83SMatthias Ringwald return true; 444382721f83SMatthias Ringwald } 444482721f83SMatthias Ringwald 4445ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 4446ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 44471bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 44486ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 44496ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4450f30077b7SMatthias Ringwald return true; 4451ad83dc6aS[email protected] } 445276f27cffSMatthias Ringwald 445334d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 445434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 4455abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 445634d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 4457f30077b7SMatthias Ringwald return true; 4458afd4e962S[email protected] } 445976f27cffSMatthias Ringwald 4460dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 4461dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 4462dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 4463f30077b7SMatthias Ringwald return true; 4464dce78009S[email protected] } 4465573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 4466573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 4467573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 4468f30077b7SMatthias Ringwald return true; 4469573897a0SMatthias Ringwald } 447076f27cffSMatthias Ringwald #endif 447176f27cffSMatthias Ringwald 447276f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 447376f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 44746ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 44756ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 44766ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 4477f30077b7SMatthias Ringwald return true; 447876f27cffSMatthias Ringwald } 44796ef3696aSMatthias Ringwald } 4480da886c03S[email protected] 44816cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 4482f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 4483f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 4484f8ee3071SMatthias Ringwald case 0: 4485f8ee3071SMatthias Ringwald break; 4486f8ee3071SMatthias Ringwald case 0xffff: 4487f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 4488f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 4489f30077b7SMatthias Ringwald return true; 4490f8ee3071SMatthias Ringwald default: 4491f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 4492f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 4493f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 4494f30077b7SMatthias Ringwald return true; 4495f8ee3071SMatthias Ringwald } 449688a03c8dSMatthias Ringwald 4497140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 4498140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 4499140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 4500140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 4501140c0557SMatthias Ringwald return true; 4502140c0557SMatthias Ringwald } 4503140c0557SMatthias Ringwald 450488a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 450588a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 450688a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 450788a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 450888a03c8dSMatthias Ringwald return true; 450988a03c8dSMatthias Ringwald } 45106cdc2862SMatthias Ringwald #endif 4511f8ee3071SMatthias Ringwald 4512a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 451373cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 451473cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 451573cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 4516da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 451773cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 4518c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 4519c37a3166S[email protected] 0x0000, 0xffff); 4520f30077b7SMatthias Ringwald return true; 452173cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 452273cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 452373cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 452473cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 452573cd8a2aSMatthias Ringwald 0x0000, 0xffff); 4526f30077b7SMatthias Ringwald return true; 452773cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 452873cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 452973cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE); 4530f30077b7SMatthias Ringwald return true; 453173cd8a2aSMatthias Ringwald default: 453273cd8a2aSMatthias Ringwald break; 4533c37a3166S[email protected] } 45344ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 4535b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 4536b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 4537b90f6e0aSMatthias Ringwald hci_send_cmd(&hci_le_set_phy, connection->con_handle, all_phys, connection->le_phy_update_tx_phys, connection->le_phy_update_rx_phys, connection->le_phy_update_phy_options); 4538f30077b7SMatthias Ringwald return true; 4539b90f6e0aSMatthias Ringwald } 4540c37a3166S[email protected] #endif 4541dbe1a790S[email protected] } 4542f30077b7SMatthias Ringwald return false; 4543f30077b7SMatthias Ringwald } 4544c7e0c5f6Smatthias.ringwald 4545f30077b7SMatthias Ringwald static void hci_run(void){ 4546f30077b7SMatthias Ringwald 4547f30077b7SMatthias Ringwald bool done; 4548f30077b7SMatthias Ringwald 4549f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 4550f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 4551f30077b7SMatthias Ringwald if (done) return; 4552f30077b7SMatthias Ringwald 4553f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 4554f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 4555f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 4556f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 4557f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 4558f30077b7SMatthias Ringwald return; 4559f30077b7SMatthias Ringwald } 4560f30077b7SMatthias Ringwald #endif 4561f30077b7SMatthias Ringwald 4562f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4563f30077b7SMatthias Ringwald 4564f30077b7SMatthias Ringwald // global/non-connection oriented commands 4565f30077b7SMatthias Ringwald 4566f30077b7SMatthias Ringwald 4567f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 4568f30077b7SMatthias Ringwald // general gap classic 4569f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 4570f30077b7SMatthias Ringwald if (done) return; 4571f30077b7SMatthias Ringwald #endif 4572f30077b7SMatthias Ringwald 4573f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 4574f30077b7SMatthias Ringwald // general gap le 4575f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 4576f30077b7SMatthias Ringwald if (done) return; 4577f30077b7SMatthias Ringwald #endif 4578f30077b7SMatthias Ringwald 4579f30077b7SMatthias Ringwald // send pending HCI commands 458088a03c8dSMatthias Ringwald done = hci_run_general_pending_commands(); 4581f30077b7SMatthias Ringwald if (done) return; 4582f30077b7SMatthias Ringwald 4583f30077b7SMatthias Ringwald // stack state sub statemachines 458405ae8de3SMatthias Ringwald hci_connection_t * connection; 45853a9fb326S[email protected] switch (hci_stack->state){ 45863429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 458774b323a9SMatthias Ringwald hci_initializing_run(); 45883429f56bSmatthias.ringwald break; 4589c7e0c5f6Smatthias.ringwald 4590c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 4591c7e0c5f6Smatthias.ringwald 459226fe9592SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 459326fe9592SMatthias Ringwald switch (hci_stack->substate){ 4594beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 4595beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_TIMER: 4596beceeddeSMatthias Ringwald 4597a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4598d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4599beceeddeSMatthias Ringwald // free whitelist entries 46009956955bSMatthias Ringwald { 4601665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 4602665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4603665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4604665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4605665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 46069956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 46079956955bSMatthias Ringwald } 46089956955bSMatthias Ringwald } 46099956955bSMatthias Ringwald #endif 4610d70217a2SMatthias Ringwald #endif 4611c7e0c5f6Smatthias.ringwald // close all open connections 46123a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 4613c7e0c5f6Smatthias.ringwald if (connection){ 4614711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 4615d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 461632ab9390Smatthias.ringwald 46178fca890eSMatthias Ringwald // check state 46188fca890eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 46198fca890eSMatthias Ringwald connection->state = SENT_DISCONNECT; 46208fca890eSMatthias Ringwald 46218837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 4622c7e0c5f6Smatthias.ringwald 46238837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 46248837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 46258837e9efSMatthias Ringwald 46268837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 4627c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 46288837e9efSMatthias Ringwald 46298837e9efSMatthias Ringwald // finally, send the disconnect command 46306ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4631c7e0c5f6Smatthias.ringwald return; 4632c7e0c5f6Smatthias.ringwald } 463326fe9592SMatthias Ringwald 4634beceeddeSMatthias Ringwald if (hci_stack->substate == HCI_HALTING_DISCONNECT_ALL_TIMER){ 4635beceeddeSMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 4636beceeddeSMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 463726fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_TIMER; 4638beceeddeSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 463926fe9592SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 464026fe9592SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 464126fe9592SMatthias Ringwald break; 4642beceeddeSMatthias Ringwald } 464326fe9592SMatthias Ringwald 4644cf373d3aSMatthias Ringwald /* fall through */ 464526fe9592SMatthias Ringwald 464626fe9592SMatthias Ringwald case HCI_HALTING_CLOSE: 46479da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 4648c7e0c5f6Smatthias.ringwald 464972ea5239Smatthias.ringwald // switch mode 4650c7e0c5f6Smatthias.ringwald hci_power_control_off(); 46519418f9c9Smatthias.ringwald 46529da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 465372ea5239Smatthias.ringwald hci_emit_state(); 46549da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 465572ea5239Smatthias.ringwald break; 4656beceeddeSMatthias Ringwald 4657beceeddeSMatthias Ringwald case HCI_HALTING_W4_TIMER: 4658beceeddeSMatthias Ringwald // keep waiting 4659beceeddeSMatthias Ringwald 4660beceeddeSMatthias Ringwald break; 466126fe9592SMatthias Ringwald default: 466226fe9592SMatthias Ringwald break; 466326fe9592SMatthias Ringwald } 466426fe9592SMatthias Ringwald 466526fe9592SMatthias Ringwald break; 4666c7e0c5f6Smatthias.ringwald 466772ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 46683a9fb326S[email protected] switch(hci_stack->substate) { 466974b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 46709da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 467172ea5239Smatthias.ringwald // close all open connections 46723a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 467366da7044Smatthias.ringwald 4674423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 467566da7044Smatthias.ringwald // don't close connections, if H4 supports power management 4676d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 467766da7044Smatthias.ringwald connection = NULL; 467866da7044Smatthias.ringwald } 467966da7044Smatthias.ringwald #endif 468072ea5239Smatthias.ringwald if (connection){ 468132ab9390Smatthias.ringwald 468272ea5239Smatthias.ringwald // send disconnect 4683d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 468432ab9390Smatthias.ringwald 46859da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 46866ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 468772ea5239Smatthias.ringwald 468872ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 468972ea5239Smatthias.ringwald hci_shutdown_connection(connection); 469072ea5239Smatthias.ringwald return; 469172ea5239Smatthias.ringwald } 469272ea5239Smatthias.ringwald 469392368cd3S[email protected] if (hci_classic_supported()){ 469489db417bSmatthias.ringwald // disable page and inquiry scan 4695d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 469632ab9390Smatthias.ringwald 46979da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 46983a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 469989db417bSmatthias.ringwald 470089db417bSmatthias.ringwald // continue in next sub state 470174b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 470289db417bSmatthias.ringwald break; 470392368cd3S[email protected] } 4704cf373d3aSMatthias Ringwald 4705cf373d3aSMatthias Ringwald /* fall through */ 470692368cd3S[email protected] 470774b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 47089da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 4709423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 471028171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 4711d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 471228171530Smatthias.ringwald // SLEEP MODE reached 47133a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 471428171530Smatthias.ringwald hci_emit_state(); 471528171530Smatthias.ringwald break; 471628171530Smatthias.ringwald } 471728171530Smatthias.ringwald #endif 471872ea5239Smatthias.ringwald // switch mode 47193a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 4720c7e0c5f6Smatthias.ringwald hci_emit_state(); 472128171530Smatthias.ringwald break; 472228171530Smatthias.ringwald 472389db417bSmatthias.ringwald default: 472489db417bSmatthias.ringwald break; 472589db417bSmatthias.ringwald } 4726c7e0c5f6Smatthias.ringwald break; 4727c7e0c5f6Smatthias.ringwald 47283429f56bSmatthias.ringwald default: 47293429f56bSmatthias.ringwald break; 47301f504dbdSmatthias.ringwald } 47313429f56bSmatthias.ringwald } 473216833f0aSmatthias.ringwald 473331452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 473435454696SMatthias Ringwald // house-keeping 473535454696SMatthias Ringwald 473635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4737c8e4258aSmatthias.ringwald bd_addr_t addr; 4738c8e4258aSmatthias.ringwald hci_connection_t * conn; 4739c123d999SMatthias Ringwald #endif 4740c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 47416f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 47429cbd2215SMatthias Ringwald #endif 4743c8e4258aSmatthias.ringwald 47446f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 47456f35bb46SMatthias Ringwald switch (opcode) { 47469cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 47479cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 47489cbd2215SMatthias Ringwald break; 47499cbd2215SMatthias Ringwald 47509cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 47516f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 4752724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 47539da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 4754c8e4258aSmatthias.ringwald 4755f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 4756ad83dc6aS[email protected] if (!conn) { 4757f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 475817f1ba2aSmatthias.ringwald if (!conn) { 475917f1ba2aSmatthias.ringwald // notify client that alloc failed 47602deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 4761f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 476217f1ba2aSmatthias.ringwald } 4763ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 4764f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 4765ad83dc6aS[email protected] } 4766ad83dc6aS[email protected] log_info("conn state %u", conn->state); 4767ad83dc6aS[email protected] switch (conn->state) { 4768ad83dc6aS[email protected] // if connection active exists 4769ad83dc6aS[email protected] case OPEN: 4770f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 4771274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 4772f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 4773672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 4774672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 4775672e36abSMatthias Ringwald break; 4776ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 4777532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 4778532f91a5SMatthias Ringwald break; 4779ad83dc6aS[email protected] default: 4780ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 4781f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 4782ad83dc6aS[email protected] } 4783c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 4784229331c6SMatthias Ringwald 4785229331c6SMatthias Ringwald // track outgoing connection 4786f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 47876535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 47886f35bb46SMatthias Ringwald break; 47896f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LINK_KEY_REQUEST_REPLY: 47907fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 47916f35bb46SMatthias Ringwald break; 47926f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LINK_KEY_REQUEST_NEGATIVE_REPLY: 47937fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 47946f35bb46SMatthias Ringwald break; 47956f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_DELETE_STORED_LINK_KEY: 4796a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 4797724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 4798a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 47998ef73945Smatthias.ringwald } 48006f35bb46SMatthias Ringwald break; 48016f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_NEGATIVE_REPLY: 48026f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_REPLY: 4803724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 4804f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 48056724cd9eS[email protected] if (conn) { 48066724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 48076724cd9eS[email protected] } 48086f35bb46SMatthias Ringwald break; 48096f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: 48106f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_REPLY: 48116f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_NEGATIVE_REPLY: 48126f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_REPLY: 4813724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 4814f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 48156724cd9eS[email protected] if (conn) { 48166724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 48176724cd9eS[email protected] } 48186f35bb46SMatthias Ringwald break; 4819ee752bb8SMatthias Ringwald 48205b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 48216f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 4822ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 4823ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 4824ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 48256f35bb46SMatthias Ringwald break; 48266f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 4827ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 4828ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 4829ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 48306f35bb46SMatthias Ringwald break; 4831ee752bb8SMatthias Ringwald #endif 483235454696SMatthias Ringwald #endif 48334b3e1e19SMatthias Ringwald 4834a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 48356f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_RANDOM_ADDRESS: 4836b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 4837b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 48386f35bb46SMatthias Ringwald break; 48397e8d8b1dSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 48406f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_ADVERTISE_ENABLE: 48415226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = packet[3] != 0; 48426f35bb46SMatthias Ringwald break; 4843d70217a2SMatthias Ringwald #endif 4844d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 48456f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 4846b04dfa37SMatthias Ringwald // white list used? 48476f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 4848b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 4849b04dfa37SMatthias Ringwald case 0: 4850b04dfa37SMatthias Ringwald // whitelist not used 4851b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 4852b04dfa37SMatthias Ringwald break; 4853b04dfa37SMatthias Ringwald case 1: 4854b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 4855b04dfa37SMatthias Ringwald break; 4856b04dfa37SMatthias Ringwald default: 4857b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 4858b04dfa37SMatthias Ringwald break; 4859b04dfa37SMatthias Ringwald } 4860c163146eSMatthias Ringwald // track outgoing connection 486105002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 4862c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 48636f35bb46SMatthias Ringwald break; 48646f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 48656ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 48666f35bb46SMatthias Ringwald break; 48679cbd2215SMatthias Ringwald #endif 48689cbd2215SMatthias Ringwald #endif 48696f35bb46SMatthias Ringwald default: 48706f35bb46SMatthias Ringwald break; 4871b04dfa37SMatthias Ringwald } 487269a97523S[email protected] 48733a9fb326S[email protected] hci_stack->num_cmd_packets--; 48745bb5bc3eS[email protected] 48755bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 4876bfea0222SMatthias Ringwald return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 487731452debSmatthias.ringwald } 48788adf0ddaSmatthias.ringwald 48792bd8b7e7S[email protected] // disconnect because of security block 48802bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 48812bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 48822bd8b7e7S[email protected] if (!connection) return; 48832bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 48842bd8b7e7S[email protected] } 48852bd8b7e7S[email protected] 48862bd8b7e7S[email protected] 4887dbe1a790S[email protected] // Configure Secure Simple Pairing 4888dbe1a790S[email protected] 488935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 489035454696SMatthias Ringwald 4891dbe1a790S[email protected] // enable will enable SSP during init 489215a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 48933a9fb326S[email protected] hci_stack->ssp_enable = enable; 4894dbe1a790S[email protected] } 4895dbe1a790S[email protected] 489695d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 489715a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 48982bd8b7e7S[email protected] } 48992bd8b7e7S[email protected] 4900dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 490115a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 49023a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 4903dbe1a790S[email protected] } 490415a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 49053a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 4906dbe1a790S[email protected] } 4907dbe1a790S[email protected] 4908dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 490915a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 49103a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 4911dbe1a790S[email protected] } 49125d23aae8SMatthias Ringwald 49135d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 49145d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 49155d23aae8SMatthias Ringwald } 49165d23aae8SMatthias Ringwald 491735454696SMatthias Ringwald #endif 4918dbe1a790S[email protected] 491994be1a66SMatthias Ringwald // va_list part of hci_send_cmd 492094be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t *cmd, va_list argptr){ 4921d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 49229d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 49239d14b626S[email protected] return 0; 49249d14b626S[email protected] } 49259d14b626S[email protected] 49265127cc62S[email protected] // for HCI INITIALIZATION 49279da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 49285127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 49295127cc62S[email protected] 49309d14b626S[email protected] hci_reserve_packet_buffer(); 49319d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 493294be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 4933bfea0222SMatthias Ringwald int err = hci_send_cmd_packet(packet, size); 4934bfea0222SMatthias Ringwald 4935593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 4936593702caSMatthias Ringwald if ((err < 0) || hci_transport_synchronous()){ 4937e2d22487SMatthias Ringwald hci_release_packet_buffer(); 4938068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 4939bfea0222SMatthias Ringwald } 4940bfea0222SMatthias Ringwald 4941bfea0222SMatthias Ringwald return err; 494294be1a66SMatthias Ringwald } 49439d14b626S[email protected] 494494be1a66SMatthias Ringwald /** 494594be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 494694be1a66SMatthias Ringwald */ 494794be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 49481cd208adSmatthias.ringwald va_list argptr; 49491cd208adSmatthias.ringwald va_start(argptr, cmd); 495094be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 49511cd208adSmatthias.ringwald va_end(argptr); 495294be1a66SMatthias Ringwald return res; 495393b8dc03Smatthias.ringwald } 4954c8e4258aSmatthias.ringwald 4955ee091cf1Smatthias.ringwald // Create various non-HCI events. 4956ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 4957ee091cf1Smatthias.ringwald 4958d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 4959fb37a842SMatthias Ringwald // dump packet 4960d6b06661SMatthias Ringwald if (dump) { 4961300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 4962d6b06661SMatthias Ringwald } 49631ef6bb52SMatthias Ringwald 4964fb37a842SMatthias Ringwald // dispatch to all event handlers 49651ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 49661ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 49671ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 49681ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 4969d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 49701ef6bb52SMatthias Ringwald } 4971d6b06661SMatthias Ringwald } 4972d6b06661SMatthias Ringwald 4973d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 4974fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 49753d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 4976d6b06661SMatthias Ringwald } 4977d6b06661SMatthias Ringwald 497835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4979701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 49803bc639ceSMatthias Ringwald // notify SCO sender if waiting 4981701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 4982701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 49833bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 4984701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 4985701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 49863d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 49873bc639ceSMatthias Ringwald } 49883bc639ceSMatthias Ringwald } 49891cfb383eSMatthias Ringwald 49901cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 49919784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 4992e4f5dd75SMatthias Ringwald uint8_t event[19+GAP_INQUIRY_MAX_NAME_LEN]; 49931cfb383eSMatthias Ringwald 49941cfb383eSMatthias Ringwald uint8_t * eir_data; 49951cfb383eSMatthias Ringwald ad_context_t context; 49961cfb383eSMatthias Ringwald const uint8_t * name; 49971cfb383eSMatthias Ringwald uint8_t name_len; 49981cfb383eSMatthias Ringwald 49999784dac1SMatthias Ringwald if (size < 3) return; 50009784dac1SMatthias Ringwald 50011cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 5002a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 50031cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 50041cfb383eSMatthias Ringwald 50059784dac1SMatthias Ringwald switch (event_type){ 50069784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 50079784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 50089784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 50099784dac1SMatthias Ringwald break; 50109784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 50119784dac1SMatthias Ringwald if (size != 257) return; 50129784dac1SMatthias Ringwald if (num_responses != 1) return; 50139784dac1SMatthias Ringwald break; 50149784dac1SMatthias Ringwald default: 50159784dac1SMatthias Ringwald return; 50169784dac1SMatthias Ringwald } 50179784dac1SMatthias Ringwald 50181cfb383eSMatthias Ringwald // event[1] is set at the end 50191cfb383eSMatthias Ringwald int i; 50201cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 50211cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 50221cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 50231cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 50241cfb383eSMatthias Ringwald 50256535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 50260e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 50276535961aSMatthias Ringwald (void)memcpy(&event[9], 50286535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 50296535961aSMatthias Ringwald 3); // class of device 50306535961aSMatthias Ringwald (void)memcpy(&event[12], 50316535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 50326535961aSMatthias Ringwald 2); // clock offset 50331cfb383eSMatthias Ringwald 50341cfb383eSMatthias Ringwald switch (event_type){ 50351cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 50361cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 50371cfb383eSMatthias Ringwald break; 50381cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 50391cfb383eSMatthias Ringwald event[14] = 1; 5040a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 50411cfb383eSMatthias Ringwald // 16,17 = 0, size 18 50421cfb383eSMatthias Ringwald break; 50431cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 50441cfb383eSMatthias Ringwald event[14] = 1; 5045a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 504679186815SMatthias Ringwald // EIR packets only contain a single inquiry response 50471cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 50481cfb383eSMatthias Ringwald name = NULL; 5049a8c4e5adSMatthias Ringwald // Iterate over EIR data 5050a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 50511cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 50521cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 50531cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 50541cfb383eSMatthias Ringwald // Prefer Complete Local Name over Shortend Local Name 50551cfb383eSMatthias Ringwald switch (data_type){ 50561cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 50571cfb383eSMatthias Ringwald if (name) continue; 5058cf373d3aSMatthias Ringwald /* fall through */ 50591cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 50601cfb383eSMatthias Ringwald name = data; 50611cfb383eSMatthias Ringwald name_len = data_size; 50621cfb383eSMatthias Ringwald break; 50631cfb383eSMatthias Ringwald default: 50641cfb383eSMatthias Ringwald break; 50651cfb383eSMatthias Ringwald } 50661cfb383eSMatthias Ringwald } 50671cfb383eSMatthias Ringwald if (name){ 50681cfb383eSMatthias Ringwald event[16] = 1; 50691cfb383eSMatthias Ringwald // truncate name if needed 50701cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 50711cfb383eSMatthias Ringwald event[17] = len; 50726535961aSMatthias Ringwald (void)memcpy(&event[18], name, len); 50731cfb383eSMatthias Ringwald event_size += len; 50741cfb383eSMatthias Ringwald } 50751cfb383eSMatthias Ringwald break; 50767bbeb3adSMilanka Ringwald default: 50777bbeb3adSMilanka Ringwald return; 50781cfb383eSMatthias Ringwald } 50791cfb383eSMatthias Ringwald event[1] = event_size - 2; 50801cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 50811cfb383eSMatthias Ringwald } 50821cfb383eSMatthias Ringwald } 508335454696SMatthias Ringwald #endif 50843bc639ceSMatthias Ringwald 508571de195eSMatthias Ringwald void hci_emit_state(void){ 50863a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 5087425d1371Smatthias.ringwald uint8_t event[3]; 508880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 50894ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 50903a9fb326S[email protected] event[2] = hci_stack->state; 5091d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5092c8e4258aSmatthias.ringwald } 5093c8e4258aSmatthias.ringwald 509435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 50952deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 5096425d1371Smatthias.ringwald uint8_t event[13]; 5097c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5098425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 509917f1ba2aSmatthias.ringwald event[2] = status; 51002deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 51012deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 5102c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 5103c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 5104d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5105c8e4258aSmatthias.ringwald } 510652db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 510752db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 510852db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 510952db98b2SMatthias Ringwald uint8_t event[4]; 511052db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 511152db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 511252db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 511352db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 511452db98b2SMatthias Ringwald } 511535454696SMatthias Ringwald #endif 5116c8e4258aSmatthias.ringwald 511735454696SMatthias Ringwald #ifdef ENABLE_BLE 5118d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5119667ba9d1SMatthias Ringwald static void hci_emit_le_connection_complete(uint8_t address_type, const bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 51204f3229d8S[email protected] uint8_t event[21]; 51214f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 51224ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 51234f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 51244f3229d8S[email protected] event[3] = status; 5125fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 51264f3229d8S[email protected] event[6] = 0; // TODO: role 51276e2e9a6bS[email protected] event[7] = address_type; 5128724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 5129f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 5130f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 5131f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 51324f3229d8S[email protected] event[20] = 0; // master clock accuracy 5133d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 51344f3229d8S[email protected] } 513535454696SMatthias Ringwald #endif 5136d70217a2SMatthias Ringwald #endif 51374f3229d8S[email protected] 5138fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 5139fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 5140fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 5141fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 5142fd43c0e0SMatthias Ringwald } 5143fd43c0e0SMatthias Ringwald 5144fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 5145425d1371Smatthias.ringwald uint8_t event[6]; 51463c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 51474ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 51483c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 5149fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 51503c4d4b90Smatthias.ringwald event[5] = reason; 5151d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 51523c4d4b90Smatthias.ringwald } 51533c4d4b90Smatthias.ringwald 5154b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 5155e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 5156425d1371Smatthias.ringwald uint8_t event[3]; 515780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 51584ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 515943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 5160d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 516143bfb1bdSmatthias.ringwald } 5162038bc64cSmatthias.ringwald 5163b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 5164e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 5165425d1371Smatthias.ringwald uint8_t event[2]; 516680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 51674ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 5168d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5169038bc64cSmatthias.ringwald } 51701b0e3922Smatthias.ringwald 517135454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 517235454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 517335454696SMatthias Ringwald uint8_t event[9]; 517435454696SMatthias Ringwald int pos = 0; 517535454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 51764ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 517735454696SMatthias Ringwald event[pos++] = status; 517835454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 5179d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5180381fbed8Smatthias.ringwald } 5181458bf4e8S[email protected] 518235454696SMatthias Ringwald 518335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 518435454696SMatthias Ringwald 5185b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 5186df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 5187a00031e2S[email protected] uint8_t event[5]; 5188e00caf9cS[email protected] int pos = 0; 51895611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 5190e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 5191f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 5192e00caf9cS[email protected] pos += 2; 5193e00caf9cS[email protected] event[pos++] = level; 5194d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5195e00caf9cS[email protected] } 5196e00caf9cS[email protected] 519735454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 519835454696SMatthias Ringwald if (!connection) return LEVEL_0; 519935454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 5200abdad579SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 5201170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 52024051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 5203170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 52040e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 5205170fafaeSMatthias Ringwald security_level = LEVEL_3; 5206170fafaeSMatthias Ringwald } 5207170fafaeSMatthias Ringwald return security_level; 520835454696SMatthias Ringwald } 520935454696SMatthias Ringwald 521035454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 521135454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 521235454696SMatthias Ringwald uint8_t event[3]; 521335454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 521435454696SMatthias Ringwald event[1] = sizeof(event) - 2; 521535454696SMatthias Ringwald event[2] = enabled; 5216d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5217ad83dc6aS[email protected] } 5218ad83dc6aS[email protected] 521998a2fd1cSMatthias Ringwald // query if remote side supports eSCO 5220073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 522198a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 522298a2fd1cSMatthias Ringwald if (!connection) return 0; 522376ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 522498a2fd1cSMatthias Ringwald } 522598a2fd1cSMatthias Ringwald 522667aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 522767aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 522867aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 522967aae551SMatthias Ringwald } 523067aae551SMatthias Ringwald 52312bd8b7e7S[email protected] // query if remote side supports SSP 52322bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 52332bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 52342bd8b7e7S[email protected] if (!connection) return 0; 523567aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 52362bd8b7e7S[email protected] } 52372bd8b7e7S[email protected] 523815a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 5239df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 5240df3354fcS[email protected] } 5241df3354fcS[email protected] 5242458bf4e8S[email protected] // GAP API 5243458bf4e8S[email protected] /** 5244458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 5245458bf4e8S[email protected] * @praram enabled 5246458bf4e8S[email protected] */ 52474c57c146S[email protected] void gap_set_bondable_mode(int enable){ 52483a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 5249458bf4e8S[email protected] } 52504ef6443cSMatthias Ringwald /** 52514ef6443cSMatthias Ringwald * @brief Get bondable mode. 52524ef6443cSMatthias Ringwald * @return 1 if bondable 52534ef6443cSMatthias Ringwald */ 52544ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 52554ef6443cSMatthias Ringwald return hci_stack->bondable; 52564ef6443cSMatthias Ringwald } 5257cb230b9dS[email protected] 5258cb230b9dS[email protected] /** 525934d2123cS[email protected] * @brief map link keys to security levels 5260cb230b9dS[email protected] */ 526134d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 526234d2123cS[email protected] switch (link_key_type){ 52633c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 52643c68dfa9S[email protected] return LEVEL_4; 52653c68dfa9S[email protected] case COMBINATION_KEY: 52663c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 52673c68dfa9S[email protected] return LEVEL_3; 52683c68dfa9S[email protected] default: 52693c68dfa9S[email protected] return LEVEL_2; 52703c68dfa9S[email protected] } 5271cb230b9dS[email protected] } 5272cb230b9dS[email protected] 52738b35e16aSMatthias Ringwald /** 52748b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 52758b35e16aSMatthias Ringwald */ 52768b35e16aSMatthias Ringwald int gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 52778b35e16aSMatthias Ringwald switch (link_key_type){ 52788b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 52798b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 52808b35e16aSMatthias Ringwald return 1; 52818b35e16aSMatthias Ringwald default: 52828b35e16aSMatthias Ringwald return 0; 52838b35e16aSMatthias Ringwald } 52848b35e16aSMatthias Ringwald } 52858b35e16aSMatthias Ringwald 52868b35e16aSMatthias Ringwald /** 52878b35e16aSMatthias Ringwald * @brief map link keys to authenticated 52888b35e16aSMatthias Ringwald */ 52898b35e16aSMatthias Ringwald int gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 52908b35e16aSMatthias Ringwald switch (link_key_type){ 52918b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 52928b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 52938b35e16aSMatthias Ringwald return 1; 52948b35e16aSMatthias Ringwald default: 52958b35e16aSMatthias Ringwald return 0; 52968b35e16aSMatthias Ringwald } 52978b35e16aSMatthias Ringwald } 52988b35e16aSMatthias Ringwald 5299106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 53005127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 5301106d6d11S[email protected] return level > LEVEL_2; 5302106d6d11S[email protected] } 5303106d6d11S[email protected] 530434d2123cS[email protected] /** 530534d2123cS[email protected] * @brief get current security level 530634d2123cS[email protected] */ 530734d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 530834d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 530934d2123cS[email protected] if (!connection) return LEVEL_0; 531034d2123cS[email protected] return gap_security_level_for_connection(connection); 531134d2123cS[email protected] } 531234d2123cS[email protected] 5313cb230b9dS[email protected] /** 5314cb230b9dS[email protected] * @brief request connection to device to 5315cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 5316cb230b9dS[email protected] */ 531734d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 531834d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 531934d2123cS[email protected] if (!connection){ 5320a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 532134d2123cS[email protected] return; 532234d2123cS[email protected] } 5323defbf200SMatthias Ringwald 5324defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 5325defbf200SMatthias Ringwald 532634d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 532783d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 532883d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 532983d08d7cSMatthias Ringwald 533083d08d7cSMatthias Ringwald // assumption: earlier requested security higher than current level => security request is active 533183d08d7cSMatthias Ringwald if (current_level < connection->requested_security_level){ 533283d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 533383d08d7cSMatthias Ringwald // increase requested level as new level is higher 533483d08d7cSMatthias Ringwald 533583d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 533683d08d7cSMatthias Ringwald 533783d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 533883d08d7cSMatthias Ringwald } 533983d08d7cSMatthias Ringwald return; 534083d08d7cSMatthias Ringwald } 534183d08d7cSMatthias Ringwald 534283d08d7cSMatthias Ringwald // no request active, notify if security sufficient 534383d08d7cSMatthias Ringwald if (requested_level <= current_level){ 5344a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 534534d2123cS[email protected] return; 534634d2123cS[email protected] } 5347a00031e2S[email protected] 5348e060c07dSMatthias Ringwald // store request 534934d2123cS[email protected] connection->requested_security_level = requested_level; 5350a00031e2S[email protected] 5351e060c07dSMatthias Ringwald // start to authenticate connection if authentication not already active 5352abdad579SMatthias Ringwald if ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 53531eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 5354e80b2cf9S[email protected] hci_run(); 5355e00caf9cS[email protected] } 5356ad83dc6aS[email protected] 5357ad83dc6aS[email protected] /** 5358ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 5359ad83dc6aS[email protected] * @param device 5360ad83dc6aS[email protected] * @param request MITM protection 5361ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 5362ad83dc6aS[email protected] */ 5363ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 5364ad83dc6aS[email protected] 5365ad83dc6aS[email protected] // create connection state machine 5366f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 5367ad83dc6aS[email protected] 5368ad83dc6aS[email protected] if (!connection){ 5369ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 5370ad83dc6aS[email protected] } 5371ad83dc6aS[email protected] 5372ad83dc6aS[email protected] // delete linkn key 537315a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 5374ad83dc6aS[email protected] 5375ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 5376ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 5377ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 5378f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 5379ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 5380ad83dc6aS[email protected] 5381ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 5382ad83dc6aS[email protected] 5383ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 5384ad83dc6aS[email protected] // handle: authentication failure 5385ad83dc6aS[email protected] // handle: disconnect on done 5386ad83dc6aS[email protected] 5387ad83dc6aS[email protected] hci_run(); 5388ad83dc6aS[email protected] 5389ad83dc6aS[email protected] return 0; 5390ad83dc6aS[email protected] } 539135454696SMatthias Ringwald #endif 53928e618f72S[email protected] 53938e618f72S[email protected] void gap_set_local_name(const char * local_name){ 53948e618f72S[email protected] hci_stack->local_name = local_name; 53958e618f72S[email protected] } 53968e618f72S[email protected] 539735454696SMatthias Ringwald 539835454696SMatthias Ringwald #ifdef ENABLE_BLE 539935454696SMatthias Ringwald 5400d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5401d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 5402fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 54037bdc6798S[email protected] hci_run(); 54047bdc6798S[email protected] } 54058e618f72S[email protected] 5406d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 5407fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 54087bdc6798S[email protected] hci_run(); 54097bdc6798S[email protected] } 54104f3229d8S[email protected] 5411a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 5412ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 5413a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 5414ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 5415ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 54168b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 5417ef11999fSmatthias.ringwald hci_run(); 5418ef11999fSmatthias.ringwald } 54194f3229d8S[email protected] 5420a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 5421a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 5422a7a719e9SMatthias Ringwald } 5423a7a719e9SMatthias Ringwald 5424667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 54254f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 54264f3229d8S[email protected] if (!conn){ 5427d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 5428d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 5429d32b3f05SMatthias Ringwald log_error("le connection already active"); 5430d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 5431d32b3f05SMatthias Ringwald } 5432d32b3f05SMatthias Ringwald 5433d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 54342e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 54354f3229d8S[email protected] if (!conn){ 54364f3229d8S[email protected] // notify client that alloc failed 54376e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 5438d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 5439472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 54404f3229d8S[email protected] } 5441d5b1a89eSMatthias Ringwald 5442d5b1a89eSMatthias Ringwald // set le connecting state 5443d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 5444d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 5445d5b1a89eSMatthias Ringwald } 5446d5b1a89eSMatthias Ringwald 54474f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 5448d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 5449564fca32S[email protected] hci_run(); 5450b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 54514f3229d8S[email protected] } 54520bf6344aS[email protected] 54530bf6344aS[email protected] if (!hci_is_le_connection(conn) || 5454a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 54550e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 54562e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 5457d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 5458616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 54590bf6344aS[email protected] } 54600bf6344aS[email protected] 5461b0136355SMatthias Ringwald // check if connection was just disconnected 5462b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 5463b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 5464b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 5465b0136355SMatthias Ringwald hci_run(); 5466b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 5467b0136355SMatthias Ringwald } 5468b0136355SMatthias Ringwald 5469d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 5470d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 54714f3229d8S[email protected] hci_run(); 5472b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 54734f3229d8S[email protected] } 54744f3229d8S[email protected] 54757851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 5476d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 5477665d90f2SMatthias Ringwald btstack_linked_item_t *it; 5478a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 54790bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 54800bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 54810bf6344aS[email protected] switch (conn->state){ 5482a6725849S[email protected] case SEND_CREATE_CONNECTION: 54837851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 54849c77c9dbSMatthias Ringwald case SENT_CANCEL_CONNECTION: 54857851196eSmatthias.ringwald return conn; 54867851196eSmatthias.ringwald default: 54877851196eSmatthias.ringwald break; 54887851196eSmatthias.ringwald }; 54897851196eSmatthias.ringwald } 54907851196eSmatthias.ringwald return NULL; 54917851196eSmatthias.ringwald } 54927851196eSmatthias.ringwald 5493d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 5494d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 5495616edd56SMatthias Ringwald if (!conn) return 0; 54967851196eSmatthias.ringwald switch (conn->state){ 54977851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 54987851196eSmatthias.ringwald // skip sending create connection and emit event instead 54992e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 5500665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 55017851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 55020bf6344aS[email protected] break; 5503a6725849S[email protected] case SENT_CREATE_CONNECTION: 55047851196eSmatthias.ringwald // request to send cancel connection 55050bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 55060bf6344aS[email protected] hci_run(); 55070bf6344aS[email protected] break; 55080bf6344aS[email protected] default: 55090bf6344aS[email protected] break; 55100bf6344aS[email protected] } 5511616edd56SMatthias Ringwald return 0; 5512e31f89a7S[email protected] } 5513d70217a2SMatthias Ringwald #endif 55144f3229d8S[email protected] 551513e645d2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5516c37a3166S[email protected] /** 55176012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 5518cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 5519cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 55206012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 55216012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 55226012052bSMatthias Ringwald * @param conn_latency, default: 4 55236012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 55246012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 55256012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 55266012052bSMatthias Ringwald */ 55276012052bSMatthias Ringwald 5528cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 5529cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 5530cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 5531cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 5532cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 55336012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 55346012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 55356012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 55366012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 55376012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 55386012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 55396012052bSMatthias Ringwald } 554013e645d2SMatthias Ringwald #endif 55416012052bSMatthias Ringwald 55426012052bSMatthias Ringwald /** 5543c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 5544c37a3166S[email protected] * @param handle 5545c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 5546c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 5547c37a3166S[email protected] * @param conn_latency 5548c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 5549c37a3166S[email protected] * @returns 0 if ok 5550c37a3166S[email protected] */ 5551c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 5552c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 5553c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 5554c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5555c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 5556c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 5557c37a3166S[email protected] connection->le_conn_latency = conn_latency; 5558c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 555984cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 5560cfc59f1bSMatthias Ringwald hci_run(); 5561c37a3166S[email protected] return 0; 5562c37a3166S[email protected] } 5563c37a3166S[email protected] 556445c102fdSMatthias Ringwald /** 5565b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 5566b68d7bc3SMatthias Ringwald * @param handle 5567b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 5568b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 5569b68d7bc3SMatthias Ringwald * @param conn_latency 5570b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 5571b68d7bc3SMatthias Ringwald * @returns 0 if ok 5572b68d7bc3SMatthias Ringwald */ 5573b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 5574b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 5575b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5576b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5577b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 5578b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 5579b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 5580b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 5581b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 558209c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 558309c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 5584b68d7bc3SMatthias Ringwald return 0; 5585b68d7bc3SMatthias Ringwald } 5586b68d7bc3SMatthias Ringwald 5587d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5588d70217a2SMatthias Ringwald 5589b68d7bc3SMatthias Ringwald /** 559045c102fdSMatthias Ringwald * @brief Set Advertisement Data 559145c102fdSMatthias Ringwald * @param advertising_data_length 559245c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 559345c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 559445c102fdSMatthias Ringwald */ 559545c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 559645c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 559745c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 5598501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 5599bbc366e6SMatthias Ringwald hci_run(); 560045c102fdSMatthias Ringwald } 5601501f56b3SMatthias Ringwald 5602501f56b3SMatthias Ringwald /** 5603501f56b3SMatthias Ringwald * @brief Set Scan Response Data 5604501f56b3SMatthias Ringwald * @param advertising_data_length 5605501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 5606501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 5607501f56b3SMatthias Ringwald */ 5608501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 5609501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 5610501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 5611501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5612bbc366e6SMatthias Ringwald hci_run(); 561345c102fdSMatthias Ringwald } 561445c102fdSMatthias Ringwald 561545c102fdSMatthias Ringwald /** 561645c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 561745c102fdSMatthias Ringwald * @param adv_int_min 561845c102fdSMatthias Ringwald * @param adv_int_max 561945c102fdSMatthias Ringwald * @param adv_type 562045c102fdSMatthias Ringwald * @param direct_address_type 562145c102fdSMatthias Ringwald * @param direct_address 562245c102fdSMatthias Ringwald * @param channel_map 562345c102fdSMatthias Ringwald * @param filter_policy 562445c102fdSMatthias Ringwald * 562545c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 562645c102fdSMatthias Ringwald */ 562745c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 5628b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 562945c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 563045c102fdSMatthias Ringwald 563145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 563245c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 563345c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 563445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 563545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 563645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 56376535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 56386535961aSMatthias Ringwald 6); 563945c102fdSMatthias Ringwald 564045c102fdSMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 5641bbc366e6SMatthias Ringwald hci_run(); 564245c102fdSMatthias Ringwald } 564345c102fdSMatthias Ringwald 564445c102fdSMatthias Ringwald /** 564545c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 564645c102fdSMatthias Ringwald * @param enabled 564745c102fdSMatthias Ringwald */ 564845c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 56495226d7f2SMatthias Ringwald hci_stack->le_advertisements_enabled = enabled != 0; 5650bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 5651cfc59f1bSMatthias Ringwald hci_run(); 565245c102fdSMatthias Ringwald } 565345c102fdSMatthias Ringwald 565435454696SMatthias Ringwald #endif 565506e5cf96SMatthias Ringwald 565606e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 565706e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 565806e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 565906e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 566006e5cf96SMatthias Ringwald 566164068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 566206e5cf96SMatthias Ringwald // update advertisement parameters, too 566306e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 5664bbc366e6SMatthias Ringwald hci_run(); 566564068776SMatthias Ringwald #endif 566664068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 566706e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 566864068776SMatthias Ringwald #endif 566906e5cf96SMatthias Ringwald } 567006e5cf96SMatthias Ringwald 5671d70217a2SMatthias Ringwald #endif 567245c102fdSMatthias Ringwald 5673616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 56745917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 56755917a5c5S[email protected] if (!conn){ 56767851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 5677616edd56SMatthias Ringwald return 0; 56785917a5c5S[email protected] } 56797fd7aa6fSMatthias Ringwald // ignore if already disconnected 56807fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 56817fd7aa6fSMatthias Ringwald return 0; 56827fd7aa6fSMatthias Ringwald } 56835917a5c5S[email protected] conn->state = SEND_DISCONNECT; 56845917a5c5S[email protected] hci_run(); 5685616edd56SMatthias Ringwald return 0; 56864f3229d8S[email protected] } 568704a6ef8cSmatthias.ringwald 5688228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 5689228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 5690228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 5691228e430cSMatthias Ringwald connectionSetAuthenticationFlags(hci_connection, READ_RSSI); 5692228e430cSMatthias Ringwald hci_run(); 5693228e430cSMatthias Ringwald return 1; 5694228e430cSMatthias Ringwald } 5695228e430cSMatthias Ringwald 5696a1bf5ae7SMatthias Ringwald /** 5697a1bf5ae7SMatthias Ringwald * @brief Get connection type 5698a1bf5ae7SMatthias Ringwald * @param con_handle 5699a1bf5ae7SMatthias Ringwald * @result connection_type 5700a1bf5ae7SMatthias Ringwald */ 5701a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 5702a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 5703a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 5704a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 5705a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 5706a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 5707a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 5708a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 5709a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 5710f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 5711a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 5712a1bf5ae7SMatthias Ringwald default: 5713a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 5714a1bf5ae7SMatthias Ringwald } 5715a1bf5ae7SMatthias Ringwald } 5716a1bf5ae7SMatthias Ringwald 57172dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 57182dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 57192dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 57202dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 57212dceb1d6SMatthias Ringwald } 57222dceb1d6SMatthias Ringwald 57232dceb1d6SMatthias Ringwald 572444f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 5725667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 572688a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 572788a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 572888a03c8dSMatthias Ringwald conn->request_role = role; 572988a03c8dSMatthias Ringwald hci_run(); 5730d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 573188a03c8dSMatthias Ringwald } 573244f858f3SMatthias Ringwald #endif 573388a03c8dSMatthias Ringwald 5734a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 57354f551432SMatthias Ringwald 5736b90f6e0aSMatthias Ringwald uint8_t gap_le_set_phy(hci_con_handle_t connection_handle, uint8_t all_phys, uint8_t tx_phys, uint8_t rx_phys, uint8_t phy_options){ 5737b90f6e0aSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 5738b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5739b90f6e0aSMatthias Ringwald 5740b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 5741b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 5742b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 5743b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 5744b90f6e0aSMatthias Ringwald 5745b90f6e0aSMatthias Ringwald hci_run(); 5746b90f6e0aSMatthias Ringwald 5747b90f6e0aSMatthias Ringwald return 0; 5748b90f6e0aSMatthias Ringwald } 5749b90f6e0aSMatthias Ringwald 5750667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 57517a92a9dbSMatthias Ringwald // check if already in list 57527a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 57537a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 57547a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 57557a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 57567a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 57577a92a9dbSMatthias Ringwald continue; 57587a92a9dbSMatthias Ringwald } 57597a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 57607a92a9dbSMatthias Ringwald continue; 57617a92a9dbSMatthias Ringwald } 5762287379ccSMatthias Ringwald // disallow if already scheduled to add 5763287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 57647a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 57657a92a9dbSMatthias Ringwald } 5766287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 5767287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 5768287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 5769287379ccSMatthias Ringwald } 57707a92a9dbSMatthias Ringwald // alloc and add to list 5771e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 5772e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 5773e83201bcSMatthias Ringwald entry->address_type = address_type; 57746535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 5775e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 5776665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 5777226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 5778ac9c45e0SMatthias Ringwald } 5779ac9c45e0SMatthias Ringwald 5780667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 5781665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 5782665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 5783665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5784665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 57857a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 57867a92a9dbSMatthias Ringwald continue; 57877a92a9dbSMatthias Ringwald } 57887a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 57897a92a9dbSMatthias Ringwald continue; 57907a92a9dbSMatthias Ringwald } 5791e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 5792e83201bcSMatthias Ringwald // remove from controller if already present 5793e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 5794a3b69fdeSMatthias Ringwald } else { 5795226db5efSMatthias Ringwald // directly remove entry from whitelist 5796665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 5797e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 5798e83201bcSMatthias Ringwald } 5799a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 5800a3b69fdeSMatthias Ringwald } 5801a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 580242ff5ba1SMatthias Ringwald } 580342ff5ba1SMatthias Ringwald 5804226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 5805665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 5806665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 5807665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5808665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 5809e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 5810e83201bcSMatthias Ringwald // remove from controller if already present 5811e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 5812e83201bcSMatthias Ringwald continue; 5813e83201bcSMatthias Ringwald } 581491915b0bSMatthias Ringwald // directly remove entry from whitelist 5815665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 5816e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 5817e83201bcSMatthias Ringwald } 5818226db5efSMatthias Ringwald } 5819226db5efSMatthias Ringwald 5820a3b69fdeSMatthias Ringwald /** 5821a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 5822a3b69fdeSMatthias Ringwald * @returns 0 if ok 5823a3b69fdeSMatthias Ringwald */ 5824a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 5825a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 5826a3b69fdeSMatthias Ringwald hci_run(); 5827a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 5828a3b69fdeSMatthias Ringwald } 5829a3b69fdeSMatthias Ringwald 5830a3b69fdeSMatthias Ringwald /** 5831a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 5832a3b69fdeSMatthias Ringwald * @param address_typ 5833a3b69fdeSMatthias Ringwald * @param address 5834a3b69fdeSMatthias Ringwald * @returns 0 if ok 5835a3b69fdeSMatthias Ringwald */ 5836667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 5837a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 5838a3b69fdeSMatthias Ringwald if (status){ 5839a3b69fdeSMatthias Ringwald return status; 5840a3b69fdeSMatthias Ringwald } 5841a3b69fdeSMatthias Ringwald hci_run(); 5842a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 5843a3b69fdeSMatthias Ringwald } 5844a3b69fdeSMatthias Ringwald 5845a3b69fdeSMatthias Ringwald /** 5846a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 5847a3b69fdeSMatthias Ringwald * @param address_typ 5848a3b69fdeSMatthias Ringwald * @param address 5849a3b69fdeSMatthias Ringwald * @returns 0 if ok 5850a3b69fdeSMatthias Ringwald */ 5851667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 5852a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 5853a3b69fdeSMatthias Ringwald if (status){ 5854a3b69fdeSMatthias Ringwald return status; 5855a3b69fdeSMatthias Ringwald } 5856a3b69fdeSMatthias Ringwald hci_run(); 5857a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 5858a3b69fdeSMatthias Ringwald } 5859a3b69fdeSMatthias Ringwald 5860226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5861226db5efSMatthias Ringwald /** 586295e257d9SMatthias Ringwald * @brief Connect with Whitelist 586395e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 586495e257d9SMatthias Ringwald * @returns - if ok 586595e257d9SMatthias Ringwald */ 586695e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 586795e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 586895e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 586995e257d9SMatthias Ringwald } 587095e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 587195e257d9SMatthias Ringwald hci_run(); 587295e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 587395e257d9SMatthias Ringwald } 587495e257d9SMatthias Ringwald 587595e257d9SMatthias Ringwald /** 5876226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 5877226db5efSMatthias Ringwald * @param address_typ 5878226db5efSMatthias Ringwald * @param address 5879226db5efSMatthias Ringwald * @returns 0 if ok 5880226db5efSMatthias Ringwald */ 5881667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 588295e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 5883226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 5884226db5efSMatthias Ringwald } 5885226db5efSMatthias Ringwald 5886226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 588763f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 5888226db5efSMatthias Ringwald return status; 5889226db5efSMatthias Ringwald } 5890226db5efSMatthias Ringwald 5891226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 5892226db5efSMatthias Ringwald 5893226db5efSMatthias Ringwald hci_run(); 589495e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 5895226db5efSMatthias Ringwald } 5896226db5efSMatthias Ringwald 5897226db5efSMatthias Ringwald /** 5898226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 5899226db5efSMatthias Ringwald * @param address_typ 5900226db5efSMatthias Ringwald * @param address 5901226db5efSMatthias Ringwald * @returns 0 if ok 5902226db5efSMatthias Ringwald */ 5903667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 590495e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 590595e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 590695e257d9SMatthias Ringwald } 590795e257d9SMatthias Ringwald 5908226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 590995e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 591095e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 591195e257d9SMatthias Ringwald } 5912226db5efSMatthias Ringwald hci_run(); 5913226db5efSMatthias Ringwald return 0; 5914226db5efSMatthias Ringwald } 5915226db5efSMatthias Ringwald 5916226db5efSMatthias Ringwald /** 5917226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 5918226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 5919226db5efSMatthias Ringwald */ 592095e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 592195e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 592295e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 592395e257d9SMatthias Ringwald } 5924226db5efSMatthias Ringwald hci_whitelist_clear(); 5925226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 5926e83201bcSMatthias Ringwald hci_run(); 592795e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 5928ac9c45e0SMatthias Ringwald } 5929c9db5c21SMilanka Ringwald 5930c9db5c21SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t connection_handle){ 5931c9db5c21SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 5932c9db5c21SMilanka Ringwald if (!conn) return 0; 5933c9db5c21SMilanka Ringwald return conn->le_connection_interval; 5934c9db5c21SMilanka Ringwald } 5935d70217a2SMatthias Ringwald #endif 59364f551432SMatthias Ringwald #endif 59374f551432SMatthias Ringwald 593835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5939ac9c45e0SMatthias Ringwald /** 5940ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 5941a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 5942ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 5943ff00ed1cSMatthias Ringwald */ 5944ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 5945ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 5946ff00ed1cSMatthias Ringwald } 5947ff00ed1cSMatthias Ringwald 5948ff00ed1cSMatthias Ringwald /** 5949f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 5950f5875de5SMatthias Ringwald * @param duration in 1.28s units 5951f5875de5SMatthias Ringwald * @return 0 if ok 5952f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 5953f5875de5SMatthias Ringwald */ 5954f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 595599449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 5956f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 5957a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 5958f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 5959f5875de5SMatthias Ringwald } 5960f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 5961f5875de5SMatthias Ringwald hci_run(); 5962f5875de5SMatthias Ringwald return 0; 5963f5875de5SMatthias Ringwald } 5964f5875de5SMatthias Ringwald 5965f5875de5SMatthias Ringwald /** 5966f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 5967f5875de5SMatthias Ringwald * @returns 0 if ok 5968f5875de5SMatthias Ringwald */ 5969f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 5970a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 5971f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 5972f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 5973f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5974f5875de5SMatthias Ringwald return 0; 5975f5875de5SMatthias Ringwald } 5976f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 5977f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 5978f5875de5SMatthias Ringwald hci_run(); 5979f5875de5SMatthias Ringwald return 0; 5980f5875de5SMatthias Ringwald } 5981f5875de5SMatthias Ringwald 5982b7f1ee76SMatthias Ringwald 5983b7f1ee76SMatthias Ringwald /** 5984b7f1ee76SMatthias Ringwald * @brief Remote Name Request 5985b7f1ee76SMatthias Ringwald * @param addr 5986b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 5987b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 5988b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 5989b7f1ee76SMatthias Ringwald */ 5990667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 5991b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 59926535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 5993b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 5994b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 5995b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 5996b7f1ee76SMatthias Ringwald hci_run(); 5997b7f1ee76SMatthias Ringwald return 0; 5998b7f1ee76SMatthias Ringwald } 5999b7f1ee76SMatthias Ringwald 6000667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 60010a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 60026535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 60030a51f88bSMatthias Ringwald hci_run(); 60040a51f88bSMatthias Ringwald return 0; 60050a51f88bSMatthias Ringwald } 60060a51f88bSMatthias Ringwald 60070a51f88bSMatthias Ringwald /** 6008aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 6009aad97216SMatthias Ringwald * @param addr 6010aad97216SMatthias Ringwald * @param pin_data 6011aad97216SMatthias Ringwald * @param pin_len 6012aad97216SMatthias Ringwald * @return 0 if ok 6013aad97216SMatthias Ringwald */ 6014667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 6015aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6016aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 6017aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 6018aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 6019aad97216SMatthias Ringwald } 6020aad97216SMatthias Ringwald 6021aad97216SMatthias Ringwald /** 60220a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 60230a51f88bSMatthias Ringwald * @param addr 60240a51f88bSMatthias Ringwald * @param pin 60250a51f88bSMatthias Ringwald * @return 0 if ok 60260a51f88bSMatthias Ringwald */ 6027667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 6028aad97216SMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, strlen(pin)); 60290a51f88bSMatthias Ringwald } 60300a51f88bSMatthias Ringwald 60310a51f88bSMatthias Ringwald /** 60320a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 60330a51f88bSMatthias Ringwald * @param addr 60340a51f88bSMatthias Ringwald * @param pin 60350a51f88bSMatthias Ringwald * @return 0 if ok 60360a51f88bSMatthias Ringwald */ 60370a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 6038cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 60390a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 60400a51f88bSMatthias Ringwald } 60410a51f88bSMatthias Ringwald 60420a51f88bSMatthias Ringwald /** 60430a51f88bSMatthias Ringwald * @brief SSP Passkey Response 60440a51f88bSMatthias Ringwald * @param addr 60450a51f88bSMatthias Ringwald * @param passkey 60460a51f88bSMatthias Ringwald * @return 0 if ok 60470a51f88bSMatthias Ringwald */ 6048667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 6049cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6050d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 60510a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 60520a51f88bSMatthias Ringwald } 60530a51f88bSMatthias Ringwald 60540a51f88bSMatthias Ringwald /** 60550a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 60560a51f88bSMatthias Ringwald * @param addr 60570a51f88bSMatthias Ringwald * @param pin 60580a51f88bSMatthias Ringwald * @return 0 if ok 60590a51f88bSMatthias Ringwald */ 6060667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 6061cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 60620a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 60630a51f88bSMatthias Ringwald } 60640a51f88bSMatthias Ringwald 60650a51f88bSMatthias Ringwald /** 60660a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 60670a51f88bSMatthias Ringwald * @param addr 60680a51f88bSMatthias Ringwald * @param passkey 60690a51f88bSMatthias Ringwald * @return 0 if ok 60700a51f88bSMatthias Ringwald */ 6071667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 6072cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 60730a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 60740a51f88bSMatthias Ringwald } 60750a51f88bSMatthias Ringwald 60760a51f88bSMatthias Ringwald /** 60770a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 60780a51f88bSMatthias Ringwald * @param addr 60790a51f88bSMatthias Ringwald * @param pin 60800a51f88bSMatthias Ringwald * @return 0 if ok 60810a51f88bSMatthias Ringwald */ 6082667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 6083cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 60840a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 60850a51f88bSMatthias Ringwald } 60860a51f88bSMatthias Ringwald 608744565b0cSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 608844565b0cSMatthias Ringwald 608944565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 609044565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 609144565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 609244565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 609344565b0cSMatthias Ringwald hci_run(); 609444565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 609544565b0cSMatthias Ringwald } 609644565b0cSMatthias Ringwald 609744565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 609844565b0cSMatthias Ringwald return gap_set_auth_flag_and_run(addr, SEND_IO_CAPABILITIES_REPLY); 609944565b0cSMatthias Ringwald } 610044565b0cSMatthias Ringwald 610144565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 610244565b0cSMatthias Ringwald return gap_set_auth_flag_and_run(addr, SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 610344565b0cSMatthias Ringwald } 610444565b0cSMatthias Ringwald #endif 610544565b0cSMatthias Ringwald 61061849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 61071849becdSMatthias Ringwald /** 61081849becdSMatthias Ringwald * @brief Report Remote OOB Data 61091849becdSMatthias Ringwald * @param bd_addr 61101849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 61111849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 61121849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 61131849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 61141849becdSMatthias Ringwald */ 61151849becdSMatthias Ringwald uint8_t gap_ssp_remote_oob_data(const bd_addr_t addr, const uint8_t * c_192, const uint8_t * r_192, const uint8_t * c_256, const uint8_t * r_256){ 61161849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 61171849becdSMatthias Ringwald if (connection == NULL) { 61181849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 61191849becdSMatthias Ringwald } 61201849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 61211849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 61221849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 61231849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 61241849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 61251849becdSMatthias Ringwald } 6126cf01e888SMatthias Ringwald /** 6127cf01e888SMatthias Ringwald * @brief Generate new OOB data 6128cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 6129cf01e888SMatthias Ringwald */ 6130cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 6131cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 6132cf01e888SMatthias Ringwald hci_run(); 6133cf01e888SMatthias Ringwald } 6134cf01e888SMatthias Ringwald 61351849becdSMatthias Ringwald #endif 61361849becdSMatthias Ringwald 6137f5875de5SMatthias Ringwald /** 6138f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 6139f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 6140f6858d14SMatthias Ringwald */ 6141f6858d14SMatthias Ringwald void hci_set_inquiry_mode(inquiry_mode_t mode){ 6142f6858d14SMatthias Ringwald hci_stack->inquiry_mode = mode; 6143f6858d14SMatthias Ringwald } 6144f6858d14SMatthias Ringwald 6145f6858d14SMatthias Ringwald /** 6146d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 6147d950d659SMatthias Ringwald */ 6148d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 6149d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 6150d950d659SMatthias Ringwald } 6151d950d659SMatthias Ringwald 6152d950d659SMatthias Ringwald /** 6153d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 6154d950d659SMatthias Ringwald * @return current voice setting 6155d950d659SMatthias Ringwald */ 61560cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 6157d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 6158d950d659SMatthias Ringwald } 6159d950d659SMatthias Ringwald 6160400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 6161400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 6162400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 6163400ff5abSMatthias Ringwald if (!transport_name) return 0; 6164400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 6165400ff5abSMatthias Ringwald } 6166400ff5abSMatthias Ringwald 6167b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 6168b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 6169b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 6170b3aad8daSMatthias Ringwald */ 6171b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 6172a3069afeSMatthias Ringwald int sco_packet_length = 0; 6173cf119f3bSMatthias Ringwald 61741e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 61756f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 61761431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 6177cf119f3bSMatthias Ringwald 6178400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 6179400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 6180cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 618152e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 618252e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 6183400ff5abSMatthias Ringwald } else { 6184400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 6185400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 6186d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 6187d966a453SMatthias Ringwald if (sco_packet_length > hci_stack->sco_data_packet_length){ 6188d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 6189d966a453SMatthias Ringwald } 6190400ff5abSMatthias Ringwald } 6191a3069afeSMatthias Ringwald #endif 61925b7087c7SMatthias Ringwald 61935b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 61945b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 61951e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 61965b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 61975b7087c7SMatthias Ringwald #endif 6198a3069afeSMatthias Ringwald return sco_packet_length; 6199b3aad8daSMatthias Ringwald } 6200b3aad8daSMatthias Ringwald 6201c4c88f1bSJakob Krantz /** 6202c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 6203c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 6204c4c88f1bSJakob Krantz */ 6205c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 6206c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 6207c4c88f1bSJakob Krantz } 6208c4c88f1bSJakob Krantz 6209c4c88f1bSJakob Krantz #endif 6210ec111c8bSMatthias Ringwald 6211ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 6212ec111c8bSMatthias Ringwald return hci_stack->state; 6213ec111c8bSMatthias Ringwald } 6214ec111c8bSMatthias Ringwald 621507e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 62165e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 621707e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 621807e010b6SMilanka Ringwald } 621907e010b6SMilanka Ringwald #endif 6220ec111c8bSMatthias Ringwald 6221d950d659SMatthias Ringwald /** 6222d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 6223d23838ecSMatthias Ringwald */ 6224c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 6225d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 6226d23838ecSMatthias Ringwald } 6227d23838ecSMatthias Ringwald 622871de195eSMatthias Ringwald void hci_disconnect_all(void){ 6229665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6230665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 6231665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6232665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 623304a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 623404a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 623504a6ef8cSmatthias.ringwald } 6236d31fba26S[email protected] hci_run(); 623704a6ef8cSmatthias.ringwald } 623833373e40SMatthias Ringwald 623933373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 624033373e40SMatthias Ringwald return hci_stack->manufacturer; 624133373e40SMatthias Ringwald } 62429c6e867eSMatthias Ringwald 62433e329ddfSandryblack #ifdef ENABLE_BLE 62443e329ddfSandryblack 62459c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 62469c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 62479c6e867eSMatthias Ringwald if (!hci_con) return NULL; 62489c6e867eSMatthias Ringwald return &hci_con->sm_connection; 62499c6e867eSMatthias Ringwald } 62509c6e867eSMatthias Ringwald 62519c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 62529c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 62539c6e867eSMatthias Ringwald 62549c6e867eSMatthias Ringwald int gap_encryption_key_size(hci_con_handle_t con_handle){ 6255c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6256c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 6257c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 6258c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 6259c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 62609c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 62619c6e867eSMatthias Ringwald } 6262c5fb5ca4SMatthias Ringwald } 6263c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 6264c5fb5ca4SMatthias Ringwald else { 6265c5fb5ca4SMatthias Ringwald if ((hci_connection->authentication_flags & CONNECTION_ENCRYPTED)){ 6266c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 6267c5fb5ca4SMatthias Ringwald } 6268c5fb5ca4SMatthias Ringwald } 6269c5fb5ca4SMatthias Ringwald #endif 6270c5fb5ca4SMatthias Ringwald return 0; 6271c5fb5ca4SMatthias Ringwald } 62729c6e867eSMatthias Ringwald 62739c6e867eSMatthias Ringwald int gap_authenticated(hci_con_handle_t con_handle){ 6274c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6275c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 62765f3981bfSMatthias Ringwald 6277c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 62785f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 62795f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6280c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 6281c5fb5ca4SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated; 628259a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 62835f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6284f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6285c5fb5ca4SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 628659a1a47aSMatthias Ringwald #endif 62875f3981bfSMatthias Ringwald default: 62885f3981bfSMatthias Ringwald return 0; 62895f3981bfSMatthias Ringwald } 62909c6e867eSMatthias Ringwald } 62919c6e867eSMatthias Ringwald 6292f1dfbe18SMatthias Ringwald int gap_secure_connection(hci_con_handle_t con_handle){ 6293c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6294c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 62958b35e16aSMatthias Ringwald 6296c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 62978b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 62988b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6299c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 6300c5fb5ca4SMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc; 630159a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 63028b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6303f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6304c5fb5ca4SMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 630559a1a47aSMatthias Ringwald #endif 63068b35e16aSMatthias Ringwald default: 63078b35e16aSMatthias Ringwald return 0; 63088b35e16aSMatthias Ringwald } 6309f1dfbe18SMatthias Ringwald } 6310f1dfbe18SMatthias Ringwald 63111e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 63121e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 63131e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 63141e122704SMatthias Ringwald 631548f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 63161e122704SMatthias Ringwald link_key_t link_key; 63171e122704SMatthias Ringwald link_key_type_t link_key_type; 631848f33f37SMatthias Ringwald #endif 63191e122704SMatthias Ringwald switch (hci_connection->address_type){ 63201e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 63211e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 63221e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 63231e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 63241e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 63251e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 63261e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 63271e122704SMatthias Ringwald #endif 63281e122704SMatthias Ringwald default: 63291e122704SMatthias Ringwald return false; 63301e122704SMatthias Ringwald } 63311e122704SMatthias Ringwald } 63321e122704SMatthias Ringwald 63331e122704SMatthias Ringwald 63349c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 63359c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 63369c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 63379c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 63389c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 63399c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 63409c6e867eSMatthias Ringwald } 63419c6e867eSMatthias Ringwald #endif 6342f8ee3071SMatthias Ringwald 6343f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 6344f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_enter(hci_con_handle_t con_handle, uint16_t sniff_min_interval, uint16_t sniff_max_interval, uint16_t sniff_attempt, uint16_t sniff_timeout){ 6345f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6346f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6347f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 6348f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 6349f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 6350f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 6351f8ee3071SMatthias Ringwald hci_run(); 6352f8ee3071SMatthias Ringwald return 0; 6353f8ee3071SMatthias Ringwald } 6354f8ee3071SMatthias Ringwald 6355f8ee3071SMatthias Ringwald /** 6356f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 6357f8ee3071SMatthias Ringwald * @param con_handle 6358f8ee3071SMatthias Ringwald @ @return 0 if ok 6359f8ee3071SMatthias Ringwald */ 6360f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 6361f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6362f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6363f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 6364f8ee3071SMatthias Ringwald hci_run(); 6365f8ee3071SMatthias Ringwald return 0; 6366f8ee3071SMatthias Ringwald } 6367bea424a5SMatthias Ringwald 6368140c0557SMatthias Ringwald uint8_t gap_sniff_subrating_configure(hci_con_handle_t con_handle, uint16_t max_latency, uint16_t min_remote_timeout, uint16_t min_local_timeout){ 6369140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6370140c0557SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6371140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 6372140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 6373140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 6374140c0557SMatthias Ringwald hci_run(); 6375*dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 6376140c0557SMatthias Ringwald } 6377140c0557SMatthias Ringwald 6378bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 6379bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 6380bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 6381bea424a5SMatthias Ringwald hci_run(); 6382bea424a5SMatthias Ringwald } 6383bea424a5SMatthias Ringwald 6384bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 6385bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 6386bea424a5SMatthias Ringwald hci_run(); 6387bea424a5SMatthias Ringwald } 6388bea424a5SMatthias Ringwald 6389f8ee3071SMatthias Ringwald #endif 6390beceeddeSMatthias Ringwald 6391beceeddeSMatthias Ringwald void hci_halting_defer(void){ 6392beceeddeSMatthias Ringwald if (hci_stack->state != HCI_STATE_HALTING) return; 6393beceeddeSMatthias Ringwald switch (hci_stack->substate){ 6394beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 6395beceeddeSMatthias Ringwald case HCI_HALTING_CLOSE: 6396beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_TIMER; 6397beceeddeSMatthias Ringwald break; 6398beceeddeSMatthias Ringwald default: 6399beceeddeSMatthias Ringwald break; 6400beceeddeSMatthias Ringwald } 6401beceeddeSMatthias Ringwald } 6402eddac615SMatthias Ringwald 640321debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 640421debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 640521debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 640621debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 640721debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 640821debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 640902b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 641021debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 641102b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 641202b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 641302b02cffSMatthias Ringwald } 641402b02cffSMatthias Ringwald } 641502b02cffSMatthias Ringwald 641602b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 641702b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 641802b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 641902b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 642002b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 642102b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 642202b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 642302b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 642421debf25SMatthias Ringwald } 642521debf25SMatthias Ringwald } 6426cf38a091SMatthias Ringwald 6427cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 6428cf38a091SMatthias Ringwald if ((hci_stack->local_supported_commands[1] & (1 << 2)) == 0) { 6429cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 6430cf38a091SMatthias Ringwald } 6431cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 6432cf38a091SMatthias Ringwald // restart le resolving list update 6433cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 6434cf38a091SMatthias Ringwald } 6435cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 6436cf38a091SMatthias Ringwald } 643721debf25SMatthias Ringwald #endif 643821debf25SMatthias Ringwald 6439eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 644018976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 644118976c74SMatthias Ringwald hci_connection_t * conn; 644218976c74SMatthias Ringwald 644318976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 644418976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 644518976c74SMatthias Ringwald 64467d33cb26SMilanka Ringwald // setup Controller info 64477d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 64487d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 64497d33cb26SMilanka Ringwald 645018976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 645118976c74SMatthias Ringwald addr[5] = 0x01; 645218976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 645318976c74SMatthias Ringwald conn->con_handle = addr[5]; 645418976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 645518976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 64567d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 645718976c74SMatthias Ringwald 645818976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 645918976c74SMatthias Ringwald addr[5] = 0x02; 646018976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 646118976c74SMatthias Ringwald conn->con_handle = addr[5]; 646218976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 646318976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 64647d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 646518976c74SMatthias Ringwald 646618976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 646718976c74SMatthias Ringwald addr[5] = 0x03; 646818976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 646918976c74SMatthias Ringwald conn->con_handle = addr[5]; 647018976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 647118976c74SMatthias Ringwald conn->state = OPEN; 64727d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 647318976c74SMatthias Ringwald 647418976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 647518976c74SMatthias Ringwald addr[5] = 0x04; 647618976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 647718976c74SMatthias Ringwald conn->con_handle = addr[5]; 647818976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 647918976c74SMatthias Ringwald conn->state = OPEN; 64807d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 648118976c74SMatthias Ringwald 648218976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 648318976c74SMatthias Ringwald addr[5] = 0x05; 648418976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 648518976c74SMatthias Ringwald conn->con_handle = addr[5]; 648618976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 648718976c74SMatthias Ringwald conn->state = OPEN; 64887d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 64898046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 649018976c74SMatthias Ringwald } 649118976c74SMatthias Ringwald 6492eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 6493eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 6494eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 6495eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6496eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 6497eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6498eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 6499eddac615SMatthias Ringwald } 6500eddac615SMatthias Ringwald } 65011470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 65021470db0cSMatthias Ringwald hci_init_done(); 65031470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 65041470db0cSMatthias Ringwald } 6505eddac615SMatthias Ringwald #endif 6506