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; 216965a4ccfSMatthias Ringwald conn->qos_service_type = HCI_SERVICE_TYPE_INVALID; 217e9f98c4aSMatthias Ringwald conn->link_key_type = INVALID_LINK_KEY; 21891a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 21991a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 22096a45072S[email protected] hci_connection_timestamp(conn); 22152db98b2SMatthias Ringwald #endif 22296a45072S[email protected] conn->acl_recombination_length = 0; 22396a45072S[email protected] conn->acl_recombination_pos = 0; 224ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 225760b20efSMatthias Ringwald 226da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 227b90f6e0aSMatthias Ringwald #ifdef ENABLE_BLE 228b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = 0xff; 229b90f6e0aSMatthias Ringwald #endif 2300f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 2310f3b27c5SMatthias Ringwald conn->le_max_tx_octets = 27; 2320f3b27c5SMatthias Ringwald #endif 233665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 23496a45072S[email protected] return conn; 23596a45072S[email protected] } 23666fb9560S[email protected] 237da886c03S[email protected] 238da886c03S[email protected] /** 239da886c03S[email protected] * get le connection parameter range 240da886c03S[email protected] * 241da886c03S[email protected] * @return le connection parameter range struct 242da886c03S[email protected] */ 2434ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 2444ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 245da886c03S[email protected] } 246da886c03S[email protected] 247da886c03S[email protected] /** 248da886c03S[email protected] * set le connection parameter range 249da886c03S[email protected] * 250da886c03S[email protected] */ 251da886c03S[email protected] 2524ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 2534ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 254da886c03S[email protected] } 255da886c03S[email protected] 256da886c03S[email protected] /** 25773cd8a2aSMatthias Ringwald * @brief Test if connection parameters are inside in existing rage 25873cd8a2aSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 25973cd8a2aSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 26073cd8a2aSMatthias Ringwald * @param conn_latency 26173cd8a2aSMatthias Ringwald * @param supervision_timeout (unit: 10ms) 26273cd8a2aSMatthias Ringwald * @returns 1 if included 26373cd8a2aSMatthias Ringwald */ 26473cd8a2aSMatthias 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){ 26573cd8a2aSMatthias Ringwald if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0; 26673cd8a2aSMatthias Ringwald if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0; 26773cd8a2aSMatthias Ringwald 26873cd8a2aSMatthias Ringwald if (le_conn_latency < existing_range->le_conn_latency_min) return 0; 26973cd8a2aSMatthias Ringwald if (le_conn_latency > existing_range->le_conn_latency_max) return 0; 27073cd8a2aSMatthias Ringwald 27173cd8a2aSMatthias Ringwald if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0; 27273cd8a2aSMatthias Ringwald if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0; 27373cd8a2aSMatthias Ringwald 27473cd8a2aSMatthias Ringwald return 1; 27573cd8a2aSMatthias Ringwald } 27673cd8a2aSMatthias Ringwald 27773cd8a2aSMatthias Ringwald /** 2782b6ab3e6SMatthias Ringwald * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 2792b6ab3e6SMatthias Ringwald * @note: default: 1 2802b6ab3e6SMatthias Ringwald * @param max_peripheral_connections 2812b6ab3e6SMatthias Ringwald */ 282d4e4907bSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 2832b6ab3e6SMatthias Ringwald void gap_set_max_number_peripheral_connections(int max_peripheral_connections){ 2842b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = max_peripheral_connections; 2852b6ab3e6SMatthias Ringwald } 286d4e4907bSMatthias Ringwald #endif 2872b6ab3e6SMatthias Ringwald 2882b6ab3e6SMatthias Ringwald /** 289da886c03S[email protected] * get hci connections iterator 290da886c03S[email protected] * 291da886c03S[email protected] * @return hci connections iterator 292da886c03S[email protected] */ 293da886c03S[email protected] 294665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 295665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 296da886c03S[email protected] } 297da886c03S[email protected] 29897addcc5Smatthias.ringwald /** 299ee091cf1Smatthias.ringwald * get connection for a given handle 300ee091cf1Smatthias.ringwald * 301ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 302ee091cf1Smatthias.ringwald */ 3035061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 304665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 305665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 306665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 307665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 3083ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 309da886c03S[email protected] return item; 310ee091cf1Smatthias.ringwald } 311ee091cf1Smatthias.ringwald } 312ee091cf1Smatthias.ringwald return NULL; 313ee091cf1Smatthias.ringwald } 314ee091cf1Smatthias.ringwald 31596a45072S[email protected] /** 31696a45072S[email protected] * get connection for given address 31796a45072S[email protected] * 31896a45072S[email protected] * @return connection OR NULL, if not found 31996a45072S[email protected] */ 320667ba9d1SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 321665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 322665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 323665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 324665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 32596a45072S[email protected] if (connection->address_type != addr_type) continue; 32696a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 32762bda3fbS[email protected] return connection; 32862bda3fbS[email protected] } 32962bda3fbS[email protected] return NULL; 33062bda3fbS[email protected] } 33162bda3fbS[email protected] 3323e5e0926SMatthias Ringwald inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 3333e5e0926SMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 3343e5e0926SMatthias Ringwald } 33552db98b2SMatthias Ringwald 336228e430cSMatthias Ringwald inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 337228e430cSMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 338228e430cSMatthias Ringwald } 339228e430cSMatthias Ringwald 34052db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 34152db98b2SMatthias Ringwald 342ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 343ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 344ee752bb8SMatthias Ringwald int connections = 0; 345ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 346ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 347ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 348ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 349ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 350ee752bb8SMatthias Ringwald connections++; 351ee752bb8SMatthias Ringwald } 352ee752bb8SMatthias Ringwald return connections; 353ee752bb8SMatthias Ringwald } 354ee752bb8SMatthias Ringwald #endif 355ee752bb8SMatthias Ringwald 356ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 35791a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 358aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 359528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 360c785ef68Smatthias.ringwald // connections might be timed out 361c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 362c785ef68Smatthias.ringwald } 363f316a845SMatthias Ringwald #else 364c1ab6cc1SMatthias Ringwald if (btstack_run_loop_get_time_ms() > (connection->timestamp + HCI_CONNECTION_TIMEOUT_MS)){ 3655f26aadcSMatthias Ringwald // connections might be timed out 3665f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 3675f26aadcSMatthias Ringwald } 3685f26aadcSMatthias Ringwald #endif 369c785ef68Smatthias.ringwald } 370ee091cf1Smatthias.ringwald 371ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 372aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 373528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 374f316a845SMatthias Ringwald #else 375528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 3765f26aadcSMatthias Ringwald #endif 377ee091cf1Smatthias.ringwald } 378ee091cf1Smatthias.ringwald 37943bfb1bdSmatthias.ringwald /** 38080ca58a0Smatthias.ringwald * add authentication flags and reset timer 38196a45072S[email protected] * @note: assumes classic connection 3822e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 3837fde4af9Smatthias.ringwald */ 3847fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 3857fde4af9Smatthias.ringwald bd_addr_t addr; 386724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 387f16129ceSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3887fde4af9Smatthias.ringwald if (conn) { 38928ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 39080ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 3917fde4af9Smatthias.ringwald } 3927fde4af9Smatthias.ringwald } 3937fde4af9Smatthias.ringwald 3941714cbbdSMatthias Ringwald static bool hci_pairing_active(hci_connection_t * hci_connection){ 3951714cbbdSMatthias Ringwald return (hci_connection->authentication_flags & PAIRING_ACTIVE_MASK) != 0; 3961714cbbdSMatthias Ringwald } 3971714cbbdSMatthias Ringwald 3981714cbbdSMatthias Ringwald static void hci_pairing_started(hci_connection_t * hci_connection, bool ssp){ 3991714cbbdSMatthias Ringwald if (hci_pairing_active(hci_connection)) return; 4001714cbbdSMatthias Ringwald if (ssp){ 4011714cbbdSMatthias Ringwald hci_connection->authentication_flags |= SSP_PAIRING_ACTIVE; 4021714cbbdSMatthias Ringwald } else { 4031714cbbdSMatthias Ringwald hci_connection->authentication_flags |= LEGACY_PAIRING_ACTIVE; 4041714cbbdSMatthias Ringwald } 4051714cbbdSMatthias Ringwald // if we are initiator, we have sent an HCI Authenticate Request 4061714cbbdSMatthias Ringwald bool initiator = (hci_connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0; 4071714cbbdSMatthias Ringwald 4081714cbbdSMatthias Ringwald log_info("pairing started, ssp %u, initiator %u", (int) ssp, (int) initiator); 409*77208d90SMatthias Ringwald 410*77208d90SMatthias Ringwald uint8_t event[12]; 411*77208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_STARTED; 412*77208d90SMatthias Ringwald event[1] = 10; 413*77208d90SMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[2]); 414*77208d90SMatthias Ringwald little_endian_store_16(event, 8, (uint16_t) hci_connection->con_handle); 415*77208d90SMatthias Ringwald event[10] = (uint8_t) ssp; 416*77208d90SMatthias Ringwald event[11] = (uint8_t) initiator; 417*77208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4181714cbbdSMatthias Ringwald } 4191714cbbdSMatthias Ringwald 4201714cbbdSMatthias Ringwald static void hci_pairing_complete(hci_connection_t * hci_connection, uint8_t status){ 4211714cbbdSMatthias Ringwald if (!hci_pairing_active(hci_connection)) return; 4221714cbbdSMatthias Ringwald hci_connection->authentication_flags &= ~PAIRING_ACTIVE_MASK; 4231714cbbdSMatthias Ringwald log_info("pairing complete, status %02x", status); 424*77208d90SMatthias Ringwald 425*77208d90SMatthias Ringwald uint8_t event[12]; 426*77208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_COMPLETE; 427*77208d90SMatthias Ringwald event[1] = 9; 428*77208d90SMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[2]); 429*77208d90SMatthias Ringwald little_endian_store_16(event, 8, (uint16_t) hci_connection->con_handle); 430*77208d90SMatthias Ringwald event[10] = status; 431*77208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4321714cbbdSMatthias Ringwald } 4331714cbbdSMatthias Ringwald 43480ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 4355061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 43680ca58a0Smatthias.ringwald if (!conn) return 0; 4371714cbbdSMatthias Ringwald return (int) hci_pairing_active(conn); 43880ca58a0Smatthias.ringwald } 43980ca58a0Smatthias.ringwald 44015a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 44155597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 4422bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 443a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 444c12e46e7Smatthias.ringwald } 44555597469SMatthias Ringwald 44655597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 44755597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 4482bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 44955597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 450c12e46e7Smatthias.ringwald } 4511b6fb31bSMatthias Ringwald 452e8ad470fSMatthias Ringwald bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type){ 453e8ad470fSMatthias Ringwald if (!hci_stack->link_key_db) return false; 45476b0318eSMatthias Ringwald int result = hci_stack->link_key_db->get_link_key(addr, link_key, type) != 0; 45576b0318eSMatthias Ringwald log_info("link key for %s available %u, type %u", bd_addr_to_str(addr), result, (int) *type); 45676b0318eSMatthias Ringwald return result; 457e8ad470fSMatthias Ringwald } 458e8ad470fSMatthias Ringwald 459ceecb9d9SMatthias Ringwald void gap_delete_all_link_keys(void){ 460ceecb9d9SMatthias Ringwald bd_addr_t addr; 461ceecb9d9SMatthias Ringwald link_key_t link_key; 462ceecb9d9SMatthias Ringwald link_key_type_t type; 463ceecb9d9SMatthias Ringwald btstack_link_key_iterator_t it; 464ceecb9d9SMatthias Ringwald int ok = gap_link_key_iterator_init(&it); 465ceecb9d9SMatthias Ringwald if (!ok) { 466ceecb9d9SMatthias Ringwald log_error("could not initialize iterator"); 467ceecb9d9SMatthias Ringwald return; 468ceecb9d9SMatthias Ringwald } 469ceecb9d9SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 470ceecb9d9SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 471ceecb9d9SMatthias Ringwald } 472ceecb9d9SMatthias Ringwald gap_link_key_iterator_done(&it); 473ceecb9d9SMatthias Ringwald } 474ceecb9d9SMatthias Ringwald 4751b6fb31bSMatthias Ringwald int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){ 4761b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 4771b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db->iterator_init) return 0; 4781b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_init(it); 4791b6fb31bSMatthias Ringwald } 4801b6fb31bSMatthias 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){ 4811b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 4821b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type); 4831b6fb31bSMatthias Ringwald } 4841b6fb31bSMatthias Ringwald void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){ 4851b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return; 4861b6fb31bSMatthias Ringwald hci_stack->link_key_db->iterator_done(it); 4871b6fb31bSMatthias Ringwald } 48835454696SMatthias Ringwald #endif 489c12e46e7Smatthias.ringwald 490eb8076ddSMatthias Ringwald static bool hci_is_le_connection_type(bd_addr_type_t address_type){ 491eb8076ddSMatthias Ringwald switch (address_type){ 492ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 493ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 494ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_PUBLIC: 495ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_RANDOM: 496eb8076ddSMatthias Ringwald return true; 497ce41473eSMatthias Ringwald default: 498eb8076ddSMatthias Ringwald return false; 499ce41473eSMatthias Ringwald } 5000bf6344aS[email protected] } 5010bf6344aS[email protected] 502eb8076ddSMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 503eb8076ddSMatthias Ringwald return hci_is_le_connection_type(connection->address_type); 504eb8076ddSMatthias Ringwald } 505eb8076ddSMatthias Ringwald 5067fde4af9Smatthias.ringwald /** 50743bfb1bdSmatthias.ringwald * count connections 50843bfb1bdSmatthias.ringwald */ 50940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 51056c253c9Smatthias.ringwald int count = 0; 511665d90f2SMatthias Ringwald btstack_linked_item_t *it; 512a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL ; it = it->next){ 51315a27967SMatthias Ringwald count++; 51415a27967SMatthias Ringwald } 51543bfb1bdSmatthias.ringwald return count; 51643bfb1bdSmatthias.ringwald } 517c8e4258aSmatthias.ringwald 51895d71764SMatthias Ringwald static int hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 519ee303eddS[email protected] 520f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 521f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 522ee303eddS[email protected] 523665d90f2SMatthias Ringwald btstack_linked_item_t *it; 524a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 525998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 526ce41473eSMatthias Ringwald if (hci_is_le_connection(connection)){ 527ce41473eSMatthias Ringwald num_packets_sent_le += connection->num_packets_sent; 528ce41473eSMatthias Ringwald } 529f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 530ce41473eSMatthias Ringwald num_packets_sent_classic += connection->num_packets_sent; 531ee303eddS[email protected] } 532ee303eddS[email protected] } 533d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 534ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 535ee303eddS[email protected] int free_slots_le = 0; 536ee303eddS[email protected] 537ee303eddS[email protected] if (free_slots_classic < 0){ 5389da54300S[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); 539998906cdSmatthias.ringwald return 0; 540998906cdSmatthias.ringwald } 541ee303eddS[email protected] 542ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 543ee303eddS[email protected] // if we have LE slots, they are used 544ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 545ee303eddS[email protected] if (free_slots_le < 0){ 5469da54300S[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); 547ee303eddS[email protected] return 0; 548998906cdSmatthias.ringwald } 549ee303eddS[email protected] } else { 550ee303eddS[email protected] // otherwise, classic slots are used for LE, too 551ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 552ee303eddS[email protected] if (free_slots_classic < 0){ 5539da54300S[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); 554ee303eddS[email protected] return 0; 555ee303eddS[email protected] } 556ee303eddS[email protected] } 557ee303eddS[email protected] 558ee303eddS[email protected] switch (address_type){ 559ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 5602125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 561ee303eddS[email protected] return 0; 562ee303eddS[email protected] 563f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 564ee303eddS[email protected] return free_slots_classic; 565ee303eddS[email protected] 566ee303eddS[email protected] default: 567cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 568ee303eddS[email protected] return free_slots_le; 569ee303eddS[email protected] } 570cb00d3aaS[email protected] return free_slots_classic; 571cb00d3aaS[email protected] } 572998906cdSmatthias.ringwald } 573998906cdSmatthias.ringwald 5742125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 5752125de09SMatthias Ringwald // get connection type 5762125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5772125de09SMatthias Ringwald if (!connection){ 5782125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 5792125de09SMatthias Ringwald return 0; 5802125de09SMatthias Ringwald } 5812125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 5822125de09SMatthias Ringwald } 5832125de09SMatthias Ringwald 58435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 585701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 586f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 587665d90f2SMatthias Ringwald btstack_linked_item_t *it; 588760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 589760b20efSMatthias Ringwald // explicit flow control 590665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 591e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 592ce41473eSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 593ce41473eSMatthias Ringwald num_sco_packets_sent += connection->num_packets_sent; 594e35edcc1S[email protected] } 595e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 596701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 59744d0e3d5S[email protected] return 0; 59844d0e3d5S[email protected] } 599e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 60049205f5dSMatthias Ringwald } else { 60149205f5dSMatthias Ringwald // implicit flow control -- TODO 6026f28d2eeSMatthias Ringwald int num_ready = 0; 6036f28d2eeSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 6046f28d2eeSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 6056f28d2eeSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 6066f28d2eeSMatthias Ringwald if (connection->sco_tx_ready == 0) continue; 6076f28d2eeSMatthias Ringwald num_ready++; 60849205f5dSMatthias Ringwald } 6096f28d2eeSMatthias Ringwald return num_ready; 6106f28d2eeSMatthias Ringwald } 611e35edcc1S[email protected] } 61235454696SMatthias Ringwald #endif 61344d0e3d5S[email protected] 6142b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 6152b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 616ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 617ac928cc2S[email protected] 618ac928cc2S[email protected] // check for async hci transport implementations 619ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 620ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 621ac928cc2S[email protected] return 0; 622ac928cc2S[email protected] } 623ac928cc2S[email protected] } 6242b838201SMatthias Ringwald return 1; 6252b838201SMatthias Ringwald } 626ac928cc2S[email protected] 6272b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 6282b838201SMatthias Ringwald int hci_can_send_command_packet_now(void){ 6292b838201SMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return 0; 6304ea43905SMatthias Ringwald return hci_stack->num_cmd_packets > 0u; 631ac928cc2S[email protected] } 632ac928cc2S[email protected] 6339d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 634ac928cc2S[email protected] // check for async hci transport implementations 6359d04d3a7SMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return 1; 6369d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 637ac928cc2S[email protected] } 6389d04d3a7SMatthias Ringwald 6399d04d3a7SMatthias Ringwald static int hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 6409d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 6419d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 642ac928cc2S[email protected] } 6439d04d3a7SMatthias Ringwald 6449d04d3a7SMatthias Ringwald int hci_can_send_acl_le_packet_now(void){ 6459d04d3a7SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 6469d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 6479d04d3a7SMatthias Ringwald } 6489d04d3a7SMatthias Ringwald 6499d04d3a7SMatthias Ringwald int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 6509d04d3a7SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return 0; 651e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 652ac928cc2S[email protected] } 653ac928cc2S[email protected] 654ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 655ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 656ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 6576b4af23dS[email protected] } 6586b4af23dS[email protected] 65935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 66035454696SMatthias Ringwald int hci_can_send_acl_classic_packet_now(void){ 66135454696SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 662f16129ceSMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_ACL); 66335454696SMatthias Ringwald } 66435454696SMatthias Ringwald 665701e3307SMatthias Ringwald int hci_can_send_prepared_sco_packet_now(void){ 666d057580eSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return 0; 667f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 668f234b250SMatthias Ringwald return hci_stack->sco_can_send_now; 669f234b250SMatthias Ringwald } else { 670701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 67144d0e3d5S[email protected] } 672f234b250SMatthias Ringwald } 67344d0e3d5S[email protected] 674701e3307SMatthias Ringwald int hci_can_send_sco_packet_now(void){ 675d057580eSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return 0; 676701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 67744d0e3d5S[email protected] } 67844d0e3d5S[email protected] 679d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 680d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 681d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 682d057580eSMatthias Ringwald } 68335454696SMatthias Ringwald #endif 684d057580eSMatthias Ringwald 68595d71764SMatthias Ringwald // used for internal checks in l2cap.c 686c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 687c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 688c8b9416aS[email protected] } 689c8b9416aS[email protected] 6906b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 6916b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 6929d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 6939d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 6949d14b626S[email protected] return 0; 6959d14b626S[email protected] } 6966b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 6976b4af23dS[email protected] return 1; 6986b4af23dS[email protected] } 6996b4af23dS[email protected] 70068a0fcf7S[email protected] void hci_release_packet_buffer(void){ 70168a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 70268a0fcf7S[email protected] } 70368a0fcf7S[email protected] 7046b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 7057f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 7066b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 7076b4af23dS[email protected] } 7086b4af23dS[email protected] 709452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 710452cf3bbS[email protected] 711452cf3bbS[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); 712452cf3bbS[email protected] 713452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 714452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 7154ea43905SMatthias Ringwald if (hci_is_le_connection(connection) && (hci_stack->le_data_packets_length > 0u)){ 716452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 717452cf3bbS[email protected] } 718452cf3bbS[email protected] 7190f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 720fcf88f47SMatthias Ringwald if (hci_is_le_connection(connection) && (connection->le_max_tx_octets < max_acl_data_packet_length)){ 7210f3b27c5SMatthias Ringwald max_acl_data_packet_length = connection->le_max_tx_octets; 7220f3b27c5SMatthias Ringwald } 7230f3b27c5SMatthias Ringwald #endif 724452cf3bbS[email protected] 725d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 726d999b54eSMatthias Ringwald 727452cf3bbS[email protected] int err; 728452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 729ff3cc4a5SMatthias Ringwald while (true){ 730452cf3bbS[email protected] 731d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 732d999b54eSMatthias Ringwald 733452cf3bbS[email protected] // get current data 7344ea43905SMatthias Ringwald const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u; 735452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 7361979f09cSMatthias Ringwald bool more_fragments = false; 737452cf3bbS[email protected] 738452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 739452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 7401979f09cSMatthias Ringwald more_fragments = true; 741452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 742452cf3bbS[email protected] } 743452cf3bbS[email protected] 744452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 7454ea43905SMatthias Ringwald if (acl_header_pos > 0u){ 746f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 7474ea43905SMatthias Ringwald handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u); 748f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 749452cf3bbS[email protected] } 750452cf3bbS[email protected] 751452cf3bbS[email protected] // update header len 7524ea43905SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length); 753452cf3bbS[email protected] 754452cf3bbS[email protected] // count packet 755ce41473eSMatthias Ringwald connection->num_packets_sent++; 7561979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments); 757d999b54eSMatthias Ringwald 758d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 759d999b54eSMatthias Ringwald if (more_fragments){ 760d999b54eSMatthias Ringwald // update start of next fragment to send 761d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 762d999b54eSMatthias Ringwald } else { 763d999b54eSMatthias Ringwald // done 764d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 765d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 766d999b54eSMatthias Ringwald } 767452cf3bbS[email protected] 768452cf3bbS[email protected] // send packet 769452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 770452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 7715bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 77281d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 1; 773452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 774452cf3bbS[email protected] 7751979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments); 776d999b54eSMatthias Ringwald 777452cf3bbS[email protected] // done yet? 778452cf3bbS[email protected] if (!more_fragments) break; 779452cf3bbS[email protected] 780452cf3bbS[email protected] // can send more? 781452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 782452cf3bbS[email protected] } 783452cf3bbS[email protected] 784d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 785452cf3bbS[email protected] 786d051460cS[email protected] // release buffer now for synchronous transport 787203bace6S[email protected] if (hci_transport_synchronous()){ 78881d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 789452cf3bbS[email protected] hci_release_packet_buffer(); 790fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 791452cf3bbS[email protected] } 792452cf3bbS[email protected] 793452cf3bbS[email protected] return err; 794452cf3bbS[email protected] } 795452cf3bbS[email protected] 796826f7347S[email protected] // pre: caller has reserved the packet buffer 797826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 7987856c818Smatthias.ringwald 799452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 800452cf3bbS[email protected] 801826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 802826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 803826f7347S[email protected] return 0; 804826f7347S[email protected] } 805826f7347S[email protected] 806d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 807d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 808d713a683S[email protected] 809826f7347S[email protected] // check for free places on Bluetooth module 810d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 811826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 81297b61c7bS[email protected] hci_release_packet_buffer(); 813068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 81497b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 81597b61c7bS[email protected] } 8166218e6f1Smatthias.ringwald 8175061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 81897b61c7bS[email protected] if (!connection) { 8195fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 82097b61c7bS[email protected] hci_release_packet_buffer(); 821068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 82297b61c7bS[email protected] return 0; 82397b61c7bS[email protected] } 82452db98b2SMatthias Ringwald 82552db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 82656cf178bSmatthias.ringwald hci_connection_timestamp(connection); 82752db98b2SMatthias Ringwald #endif 82856cf178bSmatthias.ringwald 829452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 8307856c818Smatthias.ringwald 831452cf3bbS[email protected] // setup data 832452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 833452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 8346218e6f1Smatthias.ringwald 835452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 836ee091cf1Smatthias.ringwald } 837ee091cf1Smatthias.ringwald 83835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 83944d0e3d5S[email protected] // pre: caller has reserved the packet buffer 84044d0e3d5S[email protected] int hci_send_sco_packet_buffer(int size){ 84144d0e3d5S[email protected] 84244d0e3d5S[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 84344d0e3d5S[email protected] 84444d0e3d5S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 84544d0e3d5S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 84644d0e3d5S[email protected] return 0; 84744d0e3d5S[email protected] } 84844d0e3d5S[email protected] 84944d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 8504b3e1e19SMatthias Ringwald 8514b3e1e19SMatthias Ringwald // skip checks in loopback mode 8524b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 85344d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 85444d0e3d5S[email protected] 85544d0e3d5S[email protected] // check for free places on Bluetooth module 856701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 857cbf638a9SMatthias Ringwald log_error("hci_send_sco_packet_buffer called but no free SCO buffers on controller"); 85844d0e3d5S[email protected] hci_release_packet_buffer(); 859068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 86044d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 86144d0e3d5S[email protected] } 86244d0e3d5S[email protected] 863e35edcc1S[email protected] // track send packet in connection struct 864e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 865e35edcc1S[email protected] if (!connection) { 866e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 867e35edcc1S[email protected] hci_release_packet_buffer(); 868068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 869e35edcc1S[email protected] return 0; 870e35edcc1S[email protected] } 871f234b250SMatthias Ringwald 872f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 873f234b250SMatthias Ringwald // token used 874f234b250SMatthias Ringwald hci_stack->sco_can_send_now = 0; 875f234b250SMatthias Ringwald } else { 876760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 877ce41473eSMatthias Ringwald connection->num_packets_sent++; 8786f28d2eeSMatthias Ringwald } else { 879e4157653SMatthias Ringwald connection->sco_tx_ready--; 880760b20efSMatthias Ringwald } 8814b3e1e19SMatthias Ringwald } 882f234b250SMatthias Ringwald } 88344d0e3d5S[email protected] 88444d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 885543e835cSMatthias Ringwald 88643149fc9SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 88743149fc9SMatthias Ringwald hci_stack->sco_transport->send_packet(packet, size); 88843149fc9SMatthias Ringwald hci_release_packet_buffer(); 88943149fc9SMatthias Ringwald hci_emit_transport_packet_sent(); 89043149fc9SMatthias Ringwald 89143149fc9SMatthias Ringwald return 0; 89243149fc9SMatthias Ringwald #else 89343149fc9SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 894543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 895543e835cSMatthias Ringwald hci_release_packet_buffer(); 896fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 897543e835cSMatthias Ringwald } 898543e835cSMatthias Ringwald 899543e835cSMatthias Ringwald return err; 90043149fc9SMatthias Ringwald #endif 90144d0e3d5S[email protected] } 90235454696SMatthias Ringwald #endif 90344d0e3d5S[email protected] 904c3b46f5aSMatthias Ringwald static void acl_handler(uint8_t *packet, uint16_t size){ 905e76a89eeS[email protected] 9067856c818Smatthias.ringwald // get info 9077856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 9085061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 9097856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 9107856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 9117856c818Smatthias.ringwald 9127856c818Smatthias.ringwald // ignore non-registered handle 9137856c818Smatthias.ringwald if (!conn){ 914c3b46f5aSMatthias Ringwald log_error("acl_handler called with non-registered handle %u!" , con_handle); 9157856c818Smatthias.ringwald return; 9167856c818Smatthias.ringwald } 9177856c818Smatthias.ringwald 918e76a89eeS[email protected] // assert packet is complete 9194ea43905SMatthias Ringwald if ((acl_length + 4u) != size){ 920c3b46f5aSMatthias Ringwald log_error("acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 921e76a89eeS[email protected] return; 922e76a89eeS[email protected] } 923e76a89eeS[email protected] 92452db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 9257856c818Smatthias.ringwald // update idle timestamp 9267856c818Smatthias.ringwald hci_connection_timestamp(conn); 92752db98b2SMatthias Ringwald #endif 9287856c818Smatthias.ringwald 9292b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 9302b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 9312b838201SMatthias Ringwald conn->num_packets_completed++; 9322b838201SMatthias Ringwald #endif 9332b838201SMatthias Ringwald 9347856c818Smatthias.ringwald // handle different packet types 9354ea43905SMatthias Ringwald switch (acl_flags & 0x03u) { 9367856c818Smatthias.ringwald 9377856c818Smatthias.ringwald case 0x01: // continuation fragment 9387856c818Smatthias.ringwald 9390ca847afS[email protected] // sanity checks 9404ea43905SMatthias Ringwald if (conn->acl_recombination_pos == 0u) { 9419da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 9427856c818Smatthias.ringwald return; 9437856c818Smatthias.ringwald } 9444ea43905SMatthias Ringwald if ((conn->acl_recombination_pos + acl_length) > (4u + HCI_ACL_BUFFER_SIZE)){ 9450ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 9460ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 9470ca847afS[email protected] conn->acl_recombination_pos = 0; 9480ca847afS[email protected] return; 9490ca847afS[email protected] } 9507856c818Smatthias.ringwald 9517856c818Smatthias.ringwald // append fragment payload (header already stored) 9526535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], 9536535961aSMatthias Ringwald &packet[4], acl_length); 9547856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 9557856c818Smatthias.ringwald 9567856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 9574ea43905SMatthias Ringwald if (conn->acl_recombination_pos >= (conn->acl_recombination_length + 4u + 4u)){ // pos already incl. ACL header 958d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 9597856c818Smatthias.ringwald // reset recombination buffer 9607856c818Smatthias.ringwald conn->acl_recombination_length = 0; 9617856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 9627856c818Smatthias.ringwald } 9637856c818Smatthias.ringwald break; 9647856c818Smatthias.ringwald 9657856c818Smatthias.ringwald case 0x02: { // first fragment 9667856c818Smatthias.ringwald 96723a77e1aS[email protected] // sanity check 96823a77e1aS[email protected] if (conn->acl_recombination_pos) { 96923a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 97023a77e1aS[email protected] conn->acl_recombination_pos = 0; 97123a77e1aS[email protected] } 97223a77e1aS[email protected] 9737856c818Smatthias.ringwald // peek into L2CAP packet! 9747856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 9757856c818Smatthias.ringwald 9767856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 9774ea43905SMatthias Ringwald if (acl_length >= (l2cap_length + 4u)){ 9787856c818Smatthias.ringwald // forward fragment as L2CAP packet 9794ea43905SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4u); 9807856c818Smatthias.ringwald } else { 9810ca847afS[email protected] 9820ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 9830ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 9840ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 9850ca847afS[email protected] return; 9860ca847afS[email protected] } 9870ca847afS[email protected] 9887856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 9896535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 9904ea43905SMatthias Ringwald packet, acl_length + 4u); 9914ea43905SMatthias Ringwald conn->acl_recombination_pos = acl_length + 4u; 9927856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 9934ea43905SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2u, l2cap_length +4u); 9947856c818Smatthias.ringwald } 9957856c818Smatthias.ringwald break; 9967856c818Smatthias.ringwald 9977856c818Smatthias.ringwald } 9987856c818Smatthias.ringwald default: 999c3b46f5aSMatthias Ringwald log_error( "acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 10007856c818Smatthias.ringwald return; 10017856c818Smatthias.ringwald } 100294ab26f8Smatthias.ringwald 100394ab26f8Smatthias.ringwald // execute main loop 100494ab26f8Smatthias.ringwald hci_run(); 100516833f0aSmatthias.ringwald } 100622909952Smatthias.ringwald 100767a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 10089da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 10093c4d4b90Smatthias.ringwald 1010b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1011cb70c5abSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(HAVE_SCO_TRANSPORT) 1012cb70c5abSMatthias Ringwald bd_addr_type_t addr_type = conn->address_type; 1013cb70c5abSMatthias Ringwald #endif 1014cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1015cb70c5abSMatthias Ringwald hci_con_handle_t con_handle = conn->con_handle; 1016ee752bb8SMatthias Ringwald #endif 1017b3264428SMatthias Ringwald #endif 1018ee752bb8SMatthias Ringwald 1019528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 1020c785ef68Smatthias.ringwald 1021665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1022a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 10233c4d4b90Smatthias.ringwald 10243c4d4b90Smatthias.ringwald // now it's gone 1025c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 1026ee752bb8SMatthias Ringwald 1027b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1028034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1029ee752bb8SMatthias Ringwald // update SCO 1030cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->hci_transport != NULL) && (hci_stack->hci_transport->set_sco_config != NULL)){ 1031ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1032ee752bb8SMatthias Ringwald } 1033034e9b53SMatthias Ringwald #endif 1034cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1035cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->sco_transport != NULL)){ 1036cb70c5abSMatthias Ringwald hci_stack->sco_transport->close(con_handle); 1037cb70c5abSMatthias Ringwald } 1038cb70c5abSMatthias Ringwald #endif 1039b3264428SMatthias Ringwald #endif 1040c7e0c5f6Smatthias.ringwald } 1041c7e0c5f6Smatthias.ringwald 104235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 104335454696SMatthias Ringwald 10440c042179S[email protected] static const uint16_t packet_type_sizes[] = { 10458f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 10468f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 10478f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 10488f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 10498f8108aaSmatthias.ringwald }; 105065389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 105165389bfcS[email protected] 0, // 3 slot packets 105265389bfcS[email protected] 1, // 5 slot packets 105365389bfcS[email protected] 25, // EDR 2 mpbs 105465389bfcS[email protected] 26, // EDR 3 mbps 105565389bfcS[email protected] 39, // 3 slot EDR packts 105665389bfcS[email protected] 40, // 5 slot EDR packet 105765389bfcS[email protected] }; 105865389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 105965389bfcS[email protected] 0x0f00, // 3 slot packets 106065389bfcS[email protected] 0xf000, // 5 slot packets 106165389bfcS[email protected] 0x1102, // EDR 2 mpbs 106265389bfcS[email protected] 0x2204, // EDR 3 mbps 106365389bfcS[email protected] 0x0300, // 3 slot EDR packts 106465389bfcS[email protected] 0x3000, // 5 slot EDR packet 106565389bfcS[email protected] }; 10668f8108aaSmatthias.ringwald 106765389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 106865389bfcS[email protected] // enable packet types based on size 10698f8108aaSmatthias.ringwald uint16_t packet_types = 0; 1070f16a69bbS[email protected] unsigned int i; 10718f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 10728f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 10738f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 10748f8108aaSmatthias.ringwald packet_types |= 1 << i; 10758f8108aaSmatthias.ringwald } 10768f8108aaSmatthias.ringwald } 107765389bfcS[email protected] // disable packet types due to missing local supported features 107865389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 1079f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 108065389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 108165389bfcS[email protected] if (feature_set) continue; 108265389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 108365389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 108465389bfcS[email protected] } 10858f8108aaSmatthias.ringwald // flip bits for "may not be used" 10868f8108aaSmatthias.ringwald packet_types ^= 0x3306; 10878f8108aaSmatthias.ringwald return packet_types; 10888f8108aaSmatthias.ringwald } 10898f8108aaSmatthias.ringwald 10908f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 10913a9fb326S[email protected] return hci_stack->packet_types; 10928f8108aaSmatthias.ringwald } 109335454696SMatthias Ringwald #endif 10948f8108aaSmatthias.ringwald 1095facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 10967dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 10973a9fb326S[email protected] return hci_stack->hci_packet_buffer; 10987dc17943Smatthias.ringwald } 10997dc17943Smatthias.ringwald 1100f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 11013a9fb326S[email protected] return hci_stack->acl_data_packet_length; 11027dc17943Smatthias.ringwald } 11037dc17943Smatthias.ringwald 110406b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 11053e68d23dSMatthias Ringwald int hci_extended_sco_link_supported(void){ 11063e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 11073e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 11083e68d23dSMatthias Ringwald } 110906b9e820SMatthias Ringwald #endif 11103e68d23dSMatthias Ringwald 11116ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 11126ac9a97eS[email protected] // No. 54, byte 6, bit 6 11134ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 6u)) != 0u; 11146ac9a97eS[email protected] } 11156ac9a97eS[email protected] 111615a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 11176ac9a97eS[email protected] // No. 51, byte 6, bit 3 11184ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 3u)) != 0u; 1119f5d8d141S[email protected] } 1120f5d8d141S[email protected] 11217f02f414SMatthias Ringwald static int hci_classic_supported(void){ 112235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 11236ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 11243a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 112535454696SMatthias Ringwald #else 112635454696SMatthias Ringwald return 0; 112735454696SMatthias Ringwald #endif 1128f5d8d141S[email protected] } 1129f5d8d141S[email protected] 11307f02f414SMatthias Ringwald static int hci_le_supported(void){ 1131a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 11326ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 11334ea43905SMatthias Ringwald return (hci_stack->local_supported_features[4u] & (1u << 6u)) != 0u; 1134f5d8d141S[email protected] #else 1135f5d8d141S[email protected] return 0; 1136f5d8d141S[email protected] #endif 1137f5d8d141S[email protected] } 1138f5d8d141S[email protected] 1139b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 1140b95a5a35SMatthias Ringwald 1141f5873674SMatthias Ringwald static void hci_get_own_address_for_addr_type(uint8_t own_addr_type, bd_addr_t own_addr){ 11426bcfa632SMatthias Ringwald if (own_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 11436bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->local_bd_addr, 6); 114469a97523S[email protected] } else { 11456bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->le_random_address, 6); 114669a97523S[email protected] } 114769a97523S[email protected] } 114869a97523S[email protected] 11496bcfa632SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 11506bcfa632SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 11516bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_own_addr_type, addr); 11526bcfa632SMatthias Ringwald } 11536bcfa632SMatthias Ringwald 11546bcfa632SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 11556bcfa632SMatthias Ringwald void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr){ 11566bcfa632SMatthias Ringwald *addr_type = hci_stack->le_advertisements_own_addr_type; 11576bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, addr); 11586bcfa632SMatthias Ringwald }; 11596bcfa632SMatthias Ringwald #endif 11606bcfa632SMatthias Ringwald 1161e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 11626bcfa632SMatthias Ringwald 11636bcfa632SMatthias Ringwald /** 11646bcfa632SMatthias Ringwald * @brief Get own addr type and address used for LE connections (Central) 11656bcfa632SMatthias Ringwald */ 11666bcfa632SMatthias Ringwald void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){ 11676bcfa632SMatthias Ringwald *addr_type = hci_stack->le_connection_own_addr_type; 11686bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, addr); 11696bcfa632SMatthias Ringwald } 11706bcfa632SMatthias Ringwald 1171384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size){ 11729ec2630cSMatthias Ringwald 1173d1dc057bS[email protected] int offset = 3; 1174d1dc057bS[email protected] int num_reports = packet[offset]; 1175d1dc057bS[email protected] offset += 1; 1176d1dc057bS[email protected] 117757c9da5bS[email protected] int i; 11784f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 117903fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1180a1df452eSMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 118133e6948bSMatthias Ringwald // sanity checks on data_length: 118233e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 118333e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 11844ea43905SMatthias Ringwald if ((offset + 9u + data_length + 1u) > size) return; 118533e6948bSMatthias Ringwald // setup event 11864ea43905SMatthias Ringwald uint8_t event_size = 10u + data_length; 1187d1dc057bS[email protected] int pos = 0; 1188045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 118957c9da5bS[email protected] event[pos++] = event_size; 11906535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address 1191d1dc057bS[email protected] offset += 8; 1192d1dc057bS[email protected] pos += 8; 1193d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 119433e6948bSMatthias Ringwald event[pos++] = data_length; 119533e6948bSMatthias Ringwald offset++; 11966535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], data_length); 119757c9da5bS[email protected] pos += data_length; 11984ea43905SMatthias Ringwald offset += data_length + 1u; // rssi 1199d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 120057c9da5bS[email protected] } 120157c9da5bS[email protected] } 1202b2f949feS[email protected] #endif 1203e8c8828eSMatthias Ringwald #endif 120457c9da5bS[email protected] 12052b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 12062b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1207bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1208bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_enabled){ 12092b6ab3e6SMatthias Ringwald // get number of active le slave connections 12102b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 12112b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 12122b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 12132b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 12142b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 12152b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 12162b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 12172b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 12182b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 12192b6ab3e6SMatthias Ringwald num_slave_connections++; 12202b6ab3e6SMatthias Ringwald } 12212b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1222bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1223bbc366e6SMatthias Ringwald } else { 1224bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 12252b6ab3e6SMatthias Ringwald } 12262b6ab3e6SMatthias Ringwald } 12272b6ab3e6SMatthias Ringwald #endif 12282b6ab3e6SMatthias Ringwald #endif 12292b6ab3e6SMatthias Ringwald 1230cd77dd38SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 123106b9e820SMatthias Ringwald 123296b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 123396b53536SMatthias Ringwald if (!hci_stack->config) return 0; 12349796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 123596b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1236a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 123796b53536SMatthias Ringwald baud_rate = 3000000; 123896b53536SMatthias Ringwald } 123996b53536SMatthias Ringwald return baud_rate; 124096b53536SMatthias Ringwald } 124196b53536SMatthias Ringwald 1242ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 12439ec2630cSMatthias Ringwald UNUSED(ds); 12449ec2630cSMatthias Ringwald 12450305bdeaSMatthias Ringwald switch (hci_stack->substate){ 12460305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 12477b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 12480305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 12497b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 12500305bdeaSMatthias Ringwald hci_run(); 12510305bdeaSMatthias Ringwald break; 12529f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 12539f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 12549f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 12559f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 12569f007422SMatthias Ringwald } 1257cf373d3aSMatthias Ringwald 1258cf373d3aSMatthias Ringwald /* fall through */ 1259cf373d3aSMatthias Ringwald 1260e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1261e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1262e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1263e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1264e47e68c7SMatthias Ringwald hci_run(); 1265688c2635SMatthias Ringwald break; 12667224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 12677224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 126896b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1269cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 12707dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 12717224be7eSMatthias Ringwald } 1272834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 127361f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1274834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1275834bce8cSMatthias Ringwald log_info("Link Reset"); 1276834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1277834bce8cSMatthias Ringwald } 1278772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1279772a36d3SMatthias Ringwald hci_run(); 1280772a36d3SMatthias Ringwald } 12814696bddbSMatthias Ringwald break; 1282559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1283559961d0SMatthias Ringwald // otherwise continue 1284559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1285559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1286559961d0SMatthias Ringwald break; 12870305bdeaSMatthias Ringwald default: 12880305bdeaSMatthias Ringwald break; 12890305bdeaSMatthias Ringwald } 12900305bdeaSMatthias Ringwald } 129106b9e820SMatthias Ringwald #endif 12920305bdeaSMatthias Ringwald 129371de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 129474b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 129574b323a9SMatthias Ringwald } 129674b323a9SMatthias Ringwald 129774b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 129871de195eSMatthias Ringwald static void hci_initializing_run(void){ 1299148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 130074b323a9SMatthias Ringwald switch (hci_stack->substate){ 130174b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 130274b323a9SMatthias Ringwald hci_state_reset(); 1303a0cf2f3fSMatthias Ringwald 130406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 13050305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1306659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1307528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1308528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1309a0cf2f3fSMatthias Ringwald #endif 13100305bdeaSMatthias Ringwald // send command 131174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 13120305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 131374b323a9SMatthias Ringwald break; 131476fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 131576fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 131676fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 131776fcb19bSMatthias Ringwald break; 1318e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1319e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1320e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1321e90bae01SMatthias Ringwald break; 132206b9e820SMatthias Ringwald 132306b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1324e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1325e47e68c7SMatthias Ringwald hci_state_reset(); 1326e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1327659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1328528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1329528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1330e47e68c7SMatthias Ringwald // send command 1331e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1332e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1333e47e68c7SMatthias Ringwald break; 13348d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 13358d29070eSMatthias Ringwald hci_state_reset(); 13368d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 13378d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 13388d29070eSMatthias Ringwald break; 1339fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: { 134096b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 13413fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1342f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 134374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 13444ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 13454696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 13464696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 134761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1348659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1349528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 13504696bddbSMatthias Ringwald } 135174b323a9SMatthias Ringwald break; 1352fab26ab3SMatthias Ringwald } 1353fab26ab3SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 135496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 13553fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1356f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1357eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 13584ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1359eb3a5314SMatthias Ringwald break; 1360fab26ab3SMatthias Ringwald } 136174b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 136274b323a9SMatthias Ringwald // Custom initialization 13633fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1364ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 13651979f09cSMatthias Ringwald bool send_cmd = false; 1366ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1367f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 13681979f09cSMatthias Ringwald send_cmd = true; 1369e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1370e47e68c7SMatthias Ringwald break; 1371f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 13721979f09cSMatthias Ringwald send_cmd = true; 1373f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1374e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1375659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1376528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1377528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1378a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1379772a36d3SMatthias Ringwald && hci_stack->config 13803fb36a29SMatthias Ringwald && hci_stack->chipset 13813fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1382772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 13832caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1384772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1385772a36d3SMatthias Ringwald } else { 13869f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1387772a36d3SMatthias Ringwald } 1388e47e68c7SMatthias Ringwald break; 1389f41911edSMatthias Ringwald default: 1390f41911edSMatthias Ringwald break; 1391e47e68c7SMatthias Ringwald } 1392ee720f3aSMatthias Ringwald 1393ee720f3aSMatthias Ringwald if (send_cmd){ 13944ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1395ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1396ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 13970305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 139874b323a9SMatthias Ringwald break; 139974b323a9SMatthias Ringwald } 1400148ca237SMatthias Ringwald log_info("Init script done"); 140192a0d36dSMatthias Ringwald 1402559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1403ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1404a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1405a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1406e021ff1eSMatthias Ringwald 1407559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 140892a0d36dSMatthias Ringwald int need_baud_change = hci_stack->config 14093fb36a29SMatthias Ringwald && hci_stack->chipset 14103fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 141192a0d36dSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 14129796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 141392a0d36dSMatthias Ringwald if (need_baud_change) { 14149796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1415cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 141692a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 141792a0d36dSMatthias Ringwald } 1418559961d0SMatthias Ringwald 1419f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1420f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1421f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1422f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1423559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1424f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1425559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1426559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1427559961d0SMatthias Ringwald break; 142892a0d36dSMatthias Ringwald } 142974b323a9SMatthias Ringwald } 143074b323a9SMatthias Ringwald // otherwise continue 1431a828a756SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1432a828a756SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1433a828a756SMatthias Ringwald break; 143453860077SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 143553860077SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 14363fb36a29SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1437f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 143853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 14394ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 144053860077SMatthias Ringwald break; 144106b9e820SMatthias Ringwald #endif 144206b9e820SMatthias Ringwald 144306b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 144406b9e820SMatthias Ringwald log_info("Resend hci_read_local_supported_commands after CSR Warm Boot double reset"); 144506b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 144606b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 144706b9e820SMatthias Ringwald break; 144853860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 144953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 145053860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 145153860077SMatthias Ringwald break; 145274b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 145374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 14540305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 145574b323a9SMatthias Ringwald break; 145653860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 145753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 14580305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 145974b323a9SMatthias Ringwald break; 14602b838201SMatthias Ringwald 14612b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 14622b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 14632b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 14642b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 14652b838201SMatthias Ringwald break; 14662b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 14672b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 14682b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 14692b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 14702b838201SMatthias Ringwald break; 14712b838201SMatthias Ringwald #endif 14722b838201SMatthias Ringwald 147374b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 14740305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 147574b323a9SMatthias Ringwald if (hci_le_supported()){ 14765ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x3FFFFFFFU); 147774b323a9SMatthias Ringwald } else { 147874b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 14795ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x1FFFFFFFU); 148074b323a9SMatthias Ringwald } 148174b323a9SMatthias Ringwald break; 14822b838201SMatthias Ringwald 148335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 148474b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 148574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 14860305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 148774b323a9SMatthias Ringwald break; 148874b323a9SMatthias Ringwald case HCI_INIT_WRITE_PAGE_TIMEOUT: 148974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_PAGE_TIMEOUT; 14900305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); // ca. 15 sec 149174b323a9SMatthias Ringwald break; 1492c33e56d3SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_LINK_POLICY_SETTING: 1493c33e56d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_LINK_POLICY_SETTING; 1494c33e56d3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1495c33e56d3SMatthias Ringwald break; 149674b323a9SMatthias Ringwald case HCI_INIT_WRITE_CLASS_OF_DEVICE: 149774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_CLASS_OF_DEVICE; 14980305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 149974b323a9SMatthias Ringwald break; 15001c2a62f5SMatthias Ringwald case HCI_INIT_WRITE_LOCAL_NAME: { 15010305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LOCAL_NAME; 15021c2a62f5SMatthias Ringwald hci_reserve_packet_buffer(); 15031c2a62f5SMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 15041c2a62f5SMatthias Ringwald // construct HCI Command and send 15051c2a62f5SMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 15061c2a62f5SMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 15071c2a62f5SMatthias Ringwald packet[0] = opcode & 0xff; 15081c2a62f5SMatthias Ringwald packet[1] = opcode >> 8; 15091c2a62f5SMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 15101c2a62f5SMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 151179186815SMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 151279186815SMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 151379186815SMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 151479186815SMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 15151c2a62f5SMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 15163c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 15171c2a62f5SMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 151874b323a9SMatthias Ringwald break; 15191c2a62f5SMatthias Ringwald } 152029af07f3SMatthias Ringwald case HCI_INIT_WRITE_EIR_DATA: { 15218a114470SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_EIR_DATA; 152229af07f3SMatthias Ringwald hci_reserve_packet_buffer(); 152329af07f3SMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 152463873210SMatthias Ringwald // construct HCI Command in-place and send 152529af07f3SMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 152629af07f3SMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 152763873210SMatthias Ringwald uint16_t offset = 0; 152863873210SMatthias Ringwald packet[offset++] = opcode & 0xff; 152963873210SMatthias Ringwald packet[offset++] = opcode >> 8; 153063873210SMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 153163873210SMatthias Ringwald packet[offset++] = 0; // FEC not required 153263873210SMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 1533ad0d1108SMatthias Ringwald if (hci_stack->eir_data){ 153463873210SMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 153563873210SMatthias Ringwald ad_context_t context; 153663873210SMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 153763873210SMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 153863873210SMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 153963873210SMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 154063873210SMatthias Ringwald // copy item 154163873210SMatthias Ringwald packet[offset++] = size + 1; 154263873210SMatthias Ringwald packet[offset++] = data_type; 154363873210SMatthias Ringwald memcpy(&packet[offset], data, size); 154463873210SMatthias Ringwald // update name item 154563873210SMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 15463c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 154763873210SMatthias Ringwald } 154863873210SMatthias Ringwald offset += size; 154963873210SMatthias Ringwald } 1550ad0d1108SMatthias Ringwald } else { 155179186815SMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 155279186815SMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 155363873210SMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 155463873210SMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 155579186815SMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 155629af07f3SMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 15573c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 155863873210SMatthias Ringwald } 1559a8c4e5adSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 1560ff00ed1cSMatthias Ringwald break; 156129af07f3SMatthias Ringwald } 1562f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1563f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 15648a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1565f6858d14SMatthias Ringwald break; 15665d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 15675d23aae8SMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 1568c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 15695d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 15705d23aae8SMatthias Ringwald break; 157174b323a9SMatthias Ringwald case HCI_INIT_WRITE_SCAN_ENABLE: 157274b323a9SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 157374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SCAN_ENABLE; 157474b323a9SMatthias Ringwald break; 1575483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1576729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1577729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1578729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1579729ed62eSMatthias Ringwald break; 1580483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1581483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1582483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1583483c5078SMatthias Ringwald break; 15848051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 1585a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1586a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 15878051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1588a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1589a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 15908051253fSMatthias Ringwald #endif 15918051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 15928051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 15931d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 15941d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 15951d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 15961d2bbd54SMatthias Ringwald #else 15971d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 15981d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 15991d2bbd54SMatthias Ringwald #endif 16008051253fSMatthias Ringwald #endif 1601a42798c3SMatthias Ringwald break; 16024e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 16034e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 16044e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 16054e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 16061d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 16071d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 16081d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 16091d2bbd54SMatthias Ringwald #else 16101d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 16111d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 16121d2bbd54SMatthias Ringwald #endif 16134e821764SMatthias Ringwald break; 161435454696SMatthias Ringwald #endif 16154e821764SMatthias Ringwald #endif 16164e821764SMatthias Ringwald 1617a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 161874b323a9SMatthias Ringwald // LE INIT 161974b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 162074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 16210305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 162274b323a9SMatthias Ringwald break; 1623daabb8b8SMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 1624daabb8b8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 1625729710c0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0x809FF, 0x0); // bits 0-8, 11, 19 1626daabb8b8SMatthias Ringwald break; 162774b323a9SMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 162874b323a9SMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 162974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 16300305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 163174b323a9SMatthias Ringwald break; 1632b435e062SMatthias Ringwald #endif 1633b435e062SMatthias Ringwald 1634b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1635dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 1636dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 1637dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 1638dcd678baSMatthias Ringwald break; 1639dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 1640dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 1641b435e062SMatthias 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); 1642dcd678baSMatthias Ringwald break; 1643b435e062SMatthias Ringwald #endif 1644b435e062SMatthias Ringwald 1645b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 16463b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 16473b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 16483b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 16493b6d4121SMatthias Ringwald break; 165074b323a9SMatthias Ringwald case HCI_INIT_LE_SET_SCAN_PARAMETERS: 165174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_SCAN_PARAMETERS; 16528b69e4c7SMatthias 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); 165374b323a9SMatthias Ringwald break; 165474b323a9SMatthias Ringwald #endif 165574b323a9SMatthias Ringwald default: 165674b323a9SMatthias Ringwald return; 165774b323a9SMatthias Ringwald } 165855975f88SMatthias Ringwald } 165955975f88SMatthias Ringwald 1660a650ba4dSMatthias Ringwald static void hci_init_done(void){ 1661a650ba4dSMatthias Ringwald // done. tell the app 1662a650ba4dSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1663a650ba4dSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1664a650ba4dSMatthias Ringwald hci_emit_state(); 1665a650ba4dSMatthias Ringwald hci_run(); 1666a650ba4dSMatthias Ringwald } 1667a650ba4dSMatthias Ringwald 166807fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 166907fd2f31SMatthias Ringwald bool command_completed = false; 16700e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1671f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 16726155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 167307fd2f31SMatthias Ringwald command_completed = true; 1674148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 16756155b3d3S[email protected] } else { 1676d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 16776155b3d3S[email protected] } 16786155b3d3S[email protected] } 16790f97eae7SMatthias Ringwald 16800e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 16816155b3d3S[email protected] uint8_t status = packet[2]; 1682f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 16836155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 16846155b3d3S[email protected] if (status){ 168507fd2f31SMatthias Ringwald command_completed = true; 1686148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 16876155b3d3S[email protected] } else { 16886155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 16896155b3d3S[email protected] } 16906155b3d3S[email protected] } else { 1691148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 16926155b3d3S[email protected] } 16936155b3d3S[email protected] } 169406b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 1695e47e68c7SMatthias Ringwald // Vendor == CSR 16960e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 1697e47e68c7SMatthias Ringwald // TODO: track actual command 169807fd2f31SMatthias Ringwald command_completed = true; 1699e47e68c7SMatthias Ringwald } 1700a2481739S[email protected] 17014e9daa6fSMatthias Ringwald // Vendor == Toshiba 17020e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 17034e9daa6fSMatthias Ringwald // TODO: track actual command 170407fd2f31SMatthias Ringwald command_completed = true; 1705004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 1706004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 17074e9daa6fSMatthias Ringwald } 170807fd2f31SMatthias Ringwald #endif 170907fd2f31SMatthias Ringwald 171007fd2f31SMatthias Ringwald return command_completed; 171107fd2f31SMatthias Ringwald } 171207fd2f31SMatthias Ringwald 171307fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 171407fd2f31SMatthias Ringwald 171507fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 171607fd2f31SMatthias Ringwald 171707fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 171807fd2f31SMatthias Ringwald 171907fd2f31SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 17204e9daa6fSMatthias Ringwald 17210f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 17220f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 17230f97eae7SMatthias Ringwald // 17240f97eae7SMatthias Ringwald // HCI Reset 17250f97eae7SMatthias Ringwald // Timeout 100 ms 17260f97eae7SMatthias Ringwald // HCI Reset 17270f97eae7SMatthias Ringwald // Command Complete Reset 17280f97eae7SMatthias Ringwald // HCI Read Local Version Information 17290f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 17300f97eae7SMatthias Ringwald // hang... 17310f97eae7SMatthias Ringwald // 17320f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 17330f97eae7SMatthias Ringwald if (!command_completed 1734a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 17350e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 17360f97eae7SMatthias Ringwald 1737f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 17380f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 17390f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 17400f97eae7SMatthias Ringwald return; 17410f97eae7SMatthias Ringwald } 17420f97eae7SMatthias Ringwald } 17430f97eae7SMatthias Ringwald 17449f007422SMatthias Ringwald // CSR & H5 17459f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 17469f007422SMatthias Ringwald if (!command_completed 1747a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 17480e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 17499f007422SMatthias Ringwald 17509f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 17519f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 17529f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 17539f007422SMatthias Ringwald return; 17549f007422SMatthias Ringwald } 17559f007422SMatthias Ringwald } 17569f007422SMatthias Ringwald 17579f007422SMatthias 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 17589f007422SMatthias Ringwald // fix: Correct substate and behave as command below 17599f007422SMatthias Ringwald if (command_completed){ 17609f007422SMatthias Ringwald switch (hci_stack->substate){ 17619f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 17629f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 17639f007422SMatthias Ringwald break; 17649f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 17659f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 17669f007422SMatthias Ringwald break; 17679f007422SMatthias Ringwald default: 17689f007422SMatthias Ringwald break; 17699f007422SMatthias Ringwald } 17709f007422SMatthias Ringwald } 17710f97eae7SMatthias Ringwald 177206b9e820SMatthias Ringwald #endif 17730f97eae7SMatthias Ringwald 1774a2481739S[email protected] if (!command_completed) return; 1775a2481739S[email protected] 177607fd2f31SMatthias Ringwald bool need_baud_change = false; 177707fd2f31SMatthias Ringwald bool need_addr_change = false; 177806b9e820SMatthias Ringwald 177906b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 178006b9e820SMatthias Ringwald need_baud_change = hci_stack->config 17813fb36a29SMatthias Ringwald && hci_stack->chipset 17823fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1783db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 17849796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1785db8bc6ffSMatthias Ringwald 178606b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 17873fb36a29SMatthias Ringwald && hci_stack->chipset 17883fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 178906b9e820SMatthias Ringwald #endif 1790a80162e9SMatthias Ringwald 17915c363727SMatthias Ringwald switch(hci_stack->substate){ 179206b9e820SMatthias Ringwald 179306b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 17949f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 1795d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 17969f007422SMatthias Ringwald // fix: just correct substate and behave as command below 17979f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 17989f007422SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 17999f007422SMatthias Ringwald break; 180074b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 1801528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 180276fcb19bSMatthias Ringwald break; 1803e90bae01SMatthias Ringwald case HCI_INIT_W4_SEND_READ_LOCAL_NAME: 180407fd2f31SMatthias Ringwald log_info("Received local name, need baud change %d", (int) need_baud_change); 18052f48d920SMatthias Ringwald if (need_baud_change){ 18062f48d920SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE; 18072f48d920SMatthias Ringwald return; 18082f48d920SMatthias Ringwald } 180953860077SMatthias Ringwald // skip baud change 181074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 181174b323a9SMatthias Ringwald return; 1812a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 18134696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 1814fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 181561f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 181696b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1817cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 1818fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 18194696bddbSMatthias Ringwald } 182074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 182174b323a9SMatthias Ringwald return; 1822a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1823528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 1824a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 1825a80162e9SMatthias Ringwald return; 182674b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 182774b323a9SMatthias Ringwald // repeat custom init 182874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 182974b323a9SMatthias Ringwald return; 183006b9e820SMatthias Ringwald #else 183106b9e820SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 183206b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 183306b9e820SMatthias Ringwald return ; 183406b9e820SMatthias Ringwald #endif 183506b9e820SMatthias Ringwald 1836a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 18370e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1838efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 1839efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 1840eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 1841eb3a5314SMatthias Ringwald return; 1842eb3a5314SMatthias Ringwald } 184353860077SMatthias Ringwald if (need_addr_change){ 184453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 184553860077SMatthias Ringwald return; 184653860077SMatthias Ringwald } 184753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 184853860077SMatthias Ringwald return; 184906b9e820SMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 18507224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 18517224be7eSMatthias Ringwald if (need_baud_change){ 185296b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1853cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 1854fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 18557224be7eSMatthias Ringwald } 1856eb3a5314SMatthias Ringwald if (need_addr_change){ 1857eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 1858eb3a5314SMatthias Ringwald return; 1859eb3a5314SMatthias Ringwald } 1860eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 1861eb3a5314SMatthias Ringwald return; 186253860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 18636ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 18646ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 18656ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 186653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 186753860077SMatthias Ringwald return; 186853860077SMatthias Ringwald } 186953860077SMatthias Ringwald // skipping st warm boot 187053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 187153860077SMatthias Ringwald return; 187253860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 187353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 187453860077SMatthias Ringwald return; 187506b9e820SMatthias Ringwald #endif 187653860077SMatthias Ringwald case HCI_INIT_W4_READ_BD_ADDR: 187753860077SMatthias Ringwald // only read buffer size if supported 18784ea43905SMatthias Ringwald if (hci_stack->local_supported_commands[0u] & 0x01u) { 187953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BUFFER_SIZE; 188053860077SMatthias Ringwald return; 188153860077SMatthias Ringwald } 188253860077SMatthias Ringwald // skipping read buffer size 188353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES; 1884a828a756SMatthias Ringwald return; 188574b323a9SMatthias Ringwald case HCI_INIT_W4_SET_EVENT_MASK: 18866155b3d3S[email protected] // skip Classic init commands for LE only chipsets 18876155b3d3S[email protected] if (!hci_classic_supported()){ 18882c68f164SMatthias Ringwald #ifdef ENABLE_BLE 18896155b3d3S[email protected] if (hci_le_supported()){ 189074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; // skip all classic command 189174b323a9SMatthias Ringwald return; 18922c68f164SMatthias Ringwald } 18932c68f164SMatthias Ringwald #endif 18946155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 1895a650ba4dSMatthias Ringwald hci_init_done(); 189674b323a9SMatthias Ringwald return; 18976155b3d3S[email protected] } 189815a95bd5SMatthias Ringwald if (!gap_ssp_supported()){ 18995c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_PAGE_TIMEOUT; 190074b323a9SMatthias Ringwald return; 19016155b3d3S[email protected] } 19026155b3d3S[email protected] break; 1903903ea03aSMatthias Ringwald #ifdef ENABLE_BLE 1904a828a756SMatthias Ringwald case HCI_INIT_W4_LE_READ_BUFFER_SIZE: 1905a828a756SMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 19064ea43905SMatthias Ringwald if (hci_stack->local_supported_commands[0u] & 0x02u) break; 1907daabb8b8SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_EVENT_MASK; 1908daabb8b8SMatthias Ringwald return; 1909daabb8b8SMatthias Ringwald 1910b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1911b435e062SMatthias Ringwald case HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED: 19126fab74dbSMatthias Ringwald log_info("Supported commands %x", hci_stack->local_supported_commands[0] & 0x30); 19134ea43905SMatthias Ringwald if ((hci_stack->local_supported_commands[0u] & 0x30u) == 0x30u){ 191480d7d618SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_SET_EVENT_MASK; 1915b435e062SMatthias Ringwald return; 1916b435e062SMatthias Ringwald } 1917b435e062SMatthias Ringwald // explicit fall through to reduce repetitions 1918b435e062SMatthias Ringwald 1919b435e062SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1920b435e062SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_WHITE_LIST_SIZE; 1921903ea03aSMatthias Ringwald #else 1922903ea03aSMatthias Ringwald hci_init_done(); 1923903ea03aSMatthias Ringwald #endif 1924a828a756SMatthias Ringwald return; 192506337f4fSMatthias Ringwald #endif /* ENABLE_LE_DATA_LENGTH_EXTENSION */ 192606337f4fSMatthias Ringwald 192706337f4fSMatthias Ringwald #endif /* ENABLE_BLE */ 1928b435e062SMatthias Ringwald 19295d23aae8SMatthias Ringwald case HCI_INIT_W4_WRITE_INQUIRY_MODE: 19305d23aae8SMatthias Ringwald // skip write secure connections host support if not supported or disabled 19314ea43905SMatthias Ringwald if (!hci_stack->secure_connections_enable || (hci_stack->local_supported_commands[1u] & 0x02u) == 0u) { 19325d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; 19335d23aae8SMatthias Ringwald return; 19345d23aae8SMatthias Ringwald } 19355d23aae8SMatthias Ringwald break; 19365d23aae8SMatthias Ringwald 193747bc3ebbSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 19383b65523cSMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 19393905afbfSMatthias Ringwald // skip write synchronous flow control if not supported 19403905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x04) break; 19413905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1942cf373d3aSMatthias Ringwald 1943cf373d3aSMatthias Ringwald /* fall through */ 19443905afbfSMatthias Ringwald 1945729ed62eSMatthias Ringwald case HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 19463905afbfSMatthias Ringwald // skip write default erroneous data reporting if not supported 19473905afbfSMatthias Ringwald if (hci_stack->local_supported_commands[0] & 0x08) break; 19483905afbfSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1949cf373d3aSMatthias Ringwald 1950cf373d3aSMatthias Ringwald /* fall through */ 19513905afbfSMatthias Ringwald 1952f064e0bbSMatthias Ringwald case HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1953a42798c3SMatthias Ringwald // skip bcm set sco pcm config on non-Broadcom chipsets 195461f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) break; 19550168d82eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 1956cf373d3aSMatthias Ringwald 1957cf373d3aSMatthias Ringwald /* fall through */ 1958a42798c3SMatthias Ringwald 1959a42798c3SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT: 1960729ed62eSMatthias Ringwald if (!hci_le_supported()){ 1961729ed62eSMatthias Ringwald // SKIP LE init for Classic only configuration 1962a650ba4dSMatthias Ringwald hci_init_done(); 1963729ed62eSMatthias Ringwald return; 1964729ed62eSMatthias Ringwald } 1965afe9bf34SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 1966729ed62eSMatthias Ringwald break; 1967f3b012f9SMatthias Ringwald 1968f3b012f9SMatthias Ringwald #else /* !ENABLE_SCO_OVER_HCI */ 1969f3b012f9SMatthias Ringwald 197074b323a9SMatthias Ringwald case HCI_INIT_W4_WRITE_SCAN_ENABLE: 19718051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 19728051253fSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) { 19738051253fSMatthias Ringwald hci_stack->substate = HCI_INIT_BCM_WRITE_SCO_PCM_INT; 19748051253fSMatthias Ringwald return; 19758051253fSMatthias Ringwald } 19768051253fSMatthias Ringwald #endif 19778051253fSMatthias Ringwald /* fall through */ 19788051253fSMatthias Ringwald 19794e821764SMatthias Ringwald case HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 1980f3b012f9SMatthias Ringwald #ifdef ENABLE_BLE 1981f3b012f9SMatthias Ringwald if (hci_le_supported()){ 1982f3b012f9SMatthias Ringwald hci_stack->substate = HCI_INIT_LE_READ_BUFFER_SIZE; 198374b323a9SMatthias Ringwald return; 19846155b3d3S[email protected] } 1985729ed62eSMatthias Ringwald #endif 1986f3b012f9SMatthias Ringwald // SKIP LE init for Classic only configuration 1987f3b012f9SMatthias Ringwald hci_init_done(); 1988f3b012f9SMatthias Ringwald return; 1989f3b012f9SMatthias Ringwald #endif /* ENABLE_SCO_OVER_HCI */ 1990f3b012f9SMatthias Ringwald 1991a4e96e78SMatthias Ringwald // avoid compile error due to duplicate cases: HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT == HCI_INIT_DONE-1 1992a4e96e78SMatthias Ringwald #if defined(ENABLE_BLE) || defined(ENABLE_LE_DATA_LENGTH_EXTENSION) || defined(ENABLE_LE_CENTRAL) 1993a650ba4dSMatthias Ringwald // Response to command before init done state -> init done 1994a650ba4dSMatthias Ringwald case (HCI_INIT_DONE-1): 1995a650ba4dSMatthias Ringwald hci_init_done(); 1996a650ba4dSMatthias Ringwald return; 1997a4e96e78SMatthias Ringwald #endif 1998a650ba4dSMatthias Ringwald 19996155b3d3S[email protected] default: 200074b323a9SMatthias Ringwald break; 20016155b3d3S[email protected] } 200255975f88SMatthias Ringwald hci_initializing_next_state(); 20036155b3d3S[email protected] } 20046155b3d3S[email protected] 20050bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 2006229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 20070bbba85bSMatthias Ringwald bd_addr_t bd_address; 20086535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 20090bbba85bSMatthias Ringwald 20106bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 20116bc9fa5eSMatthias Ringwald // cache needed data 20126bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 20136bc9fa5eSMatthias Ringwald #endif 20146bc9fa5eSMatthias Ringwald 20150bbba85bSMatthias Ringwald // connection failed, remove entry 20160bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 20170bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 20180bbba85bSMatthias Ringwald 20196bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 20200bbba85bSMatthias Ringwald // notify client if dedicated bonding 20210bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 20220bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 20230bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 20240bbba85bSMatthias Ringwald } 20250bbba85bSMatthias Ringwald 20260bbba85bSMatthias Ringwald // if authentication error, also delete link key 20270bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 20280bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 20290bbba85bSMatthias Ringwald } 20301c79b5e3SMatthias Ringwald #else 20311c79b5e3SMatthias Ringwald UNUSED(status); 20326bc9fa5eSMatthias Ringwald #endif 20330bbba85bSMatthias Ringwald } 20340bbba85bSMatthias Ringwald 2035be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 20362f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 20372f5c44baSMatthias Ringwald // SSP Controller 20382f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 20392f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 20402f5c44baSMatthias Ringwald } 20412f5c44baSMatthias Ringwald // eSCO 20422f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 20432f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 20442f5c44baSMatthias Ringwald } 20452f5c44baSMatthias Ringwald // Extended features 20462f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 20472f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 20482f5c44baSMatthias Ringwald } 20492f5c44baSMatthias Ringwald } 20502f5c44baSMatthias Ringwald 20512f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 20522f5c44baSMatthias Ringwald // SSP Host 20532f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 20542f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 20552f5c44baSMatthias Ringwald } 205650c51a77SMatthias Ringwald // SC Host 205750c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 205850c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 205950c51a77SMatthias Ringwald } 206050c51a77SMatthias Ringwald } 206150c51a77SMatthias Ringwald 206250c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 206350c51a77SMatthias Ringwald // SC Controller 206450c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 206550c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 206650c51a77SMatthias Ringwald } 20672f5c44baSMatthias Ringwald } 20682f5c44baSMatthias Ringwald 2069de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2070de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 207149bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2072de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2073de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2074de0df013SMatthias Ringwald } 2075de0df013SMatthias Ringwald } 2076be500194SMatthias Ringwald #endif 2077de0df013SMatthias Ringwald 207867c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 207967c6c9dcSMatthias Ringwald // handle BT initialization 208067c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 208167c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 208267c6c9dcSMatthias Ringwald } 208367c6c9dcSMatthias Ringwald 208467c6c9dcSMatthias Ringwald // help with BT sleep 208567c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 208667c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 208767c6c9dcSMatthias Ringwald && HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)) { 208867c6c9dcSMatthias Ringwald hci_initializing_next_state(); 208967c6c9dcSMatthias Ringwald } 209067c6c9dcSMatthias Ringwald } 209167c6c9dcSMatthias Ringwald 20929866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 20939866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 20949866fdc7SMatthias Ringwald conn->authentication_flags |= CONNECTION_ENCRYPTED; 20959866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2096abdad579SMatthias Ringwald 2097abdad579SMatthias Ringwald if ((conn->authentication_flags & CONNECTION_AUTHENTICATED) != 0) { 20981cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 20999866fdc7SMatthias Ringwald hci_emit_security_level(conn->con_handle, gap_security_level_for_connection(conn)); 2100abdad579SMatthias Ringwald return; 2101abdad579SMatthias Ringwald } 2102abdad579SMatthias Ringwald 2103abdad579SMatthias Ringwald // Request Authentication if not already done 2104abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2105abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 21069866fdc7SMatthias Ringwald } 21079866fdc7SMatthias Ringwald #endif 21089866fdc7SMatthias Ringwald 21094f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2110b08371a9SMilanka Ringwald UNUSED(size); 2111e76a89eeS[email protected] 21129cbd2215SMatthias Ringwald uint16_t manufacturer; 21139cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2114fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 21151f7b95a1Smatthias.ringwald hci_connection_t * conn; 2116645b7c25SMatthias Ringwald uint8_t status; 21179cbd2215SMatthias Ringwald #endif 21186bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 21196bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 21207ec5eeaaSmatthias.ringwald 2121645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2122645b7c25SMatthias Ringwald switch (opcode){ 2123645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2124645b7c25SMatthias Ringwald if (packet[5]) break; 21259e263bd7SMatthias Ringwald // terminate, name 248 chars 21269e263bd7SMatthias Ringwald packet[6+248] = 0; 21279e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2128645b7c25SMatthias Ringwald break; 2129645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 21301d279b20Smatthias.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" 2131c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2132429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2133429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2134429122ccSMatthias Ringwald 2135429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2136429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2137429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2138429122ccSMatthias Ringwald 2139f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 2140f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 2141a8b12447S[email protected] 2142429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2143429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 21441a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2145c3b46f5aSMatthias Ringwald } 2146645b7c25SMatthias Ringwald break; 2147645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2148645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2149891b9fc2SMatthias Ringwald uint8_t event[5]; 2150891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2151891b9fc2SMatthias Ringwald event[1] = 3; 21526535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2153891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2154891b9fc2SMatthias Ringwald } 2155645b7c25SMatthias Ringwald break; 2156a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2157645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2158f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2159ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 21606c26b087S[email protected] // determine usable ACL payload size 21616c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 21626c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 21636c26b087S[email protected] } 21649da54300S[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); 2165645b7c25SMatthias Ringwald break; 2166b435e062SMatthias Ringwald #endif 2167b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2168645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2169dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2170dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2171dcd678baSMatthias 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); 2172645b7c25SMatthias Ringwald break; 2173b435e062SMatthias Ringwald #endif 2174d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2175645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2176e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 217715d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2178645b7c25SMatthias Ringwald break; 217965a46ef3S[email protected] #endif 2180645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2181645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2182645b7c25SMatthias 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)); 218333373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 21841624665aSMatthias Ringwald if (hci_stack->link_key_db){ 21851624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 21861624665aSMatthias Ringwald } 218733373e40SMatthias Ringwald #endif 2188645b7c25SMatthias Ringwald break; 218935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2190645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 21913a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2192645b7c25SMatthias Ringwald break; 2193645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 2194f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2195f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2196f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2197f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2198f5875de5SMatthias Ringwald } 2199645b7c25SMatthias Ringwald break; 220035454696SMatthias Ringwald #endif 2201645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 22024f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 220365389bfcS[email protected] 220435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2205a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2206a5a23fc2S[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]); 22078b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 220835454696SMatthias Ringwald #endif 2209f5d8d141S[email protected] // Classic/LE 2210f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2211645b7c25SMatthias Ringwald break; 2212645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2213645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2214c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2215c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2216c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2217c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2218c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2219c21d89f3SMatthias Ringwald } 2220c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 22214696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2222645b7c25SMatthias Ringwald break; 2223645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 2224a828a756SMatthias Ringwald hci_stack->local_supported_commands[0] = 22254ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+14u] & 0x80u) >> 7u) | // bit 0 = Octet 14, bit 7 / Read Buffer Size 22264ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+24u] & 0x40u) >> 5u) | // bit 1 = Octet 24, bit 6 / Write Le Host Supported 22274ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+10u] & 0x10u) >> 2u) | // bit 2 = Octet 10, bit 4 / Write Synchronous Flow Control Enable 22284ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+18u] & 0x08u) ) | // bit 3 = Octet 18, bit 3 / Write Default Erroneous Data Reporting 22294ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+34u] & 0x01u) << 4u) | // bit 4 = Octet 34, bit 0 / LE Write Suggested Default Data Length 22304ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+35u] & 0x08u) << 2u) | // bit 5 = Octet 35, bit 3 / LE Read Maximum Data Length 22314ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+35u] & 0x20u) << 1u) | // bit 6 = Octet 35, bit 5 / LE Set Default PHY 22324ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+20u] & 0x10u) << 3u); // bit 7 = Octet 20, bit 4 / Read Encryption Key Size 22338d2d8af1SMatthias Ringwald hci_stack->local_supported_commands[1] = 22344ea43905SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+ 2u] & 0x40u) >> 6u) | // bit 8 = Octet 2, bit 6 / Read Remote Extended Features 223599b14827SMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+32u] & 0x08u) >> 2u) | // bit 9 = Octet 32, bit 3 / Write Secure Connections Host 2236f21849cbSMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+35u] & 0x02u) << 1u) | // bit 10 = Octet 35, bit 1 / LE Set Address Resolution Enable 2237f21849cbSMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+32u] & 0x02u) << 2u) | // bit 11 = Octet 32, bit 1 / Remote OOB Extended Data Request Reply 2238f21849cbSMatthias Ringwald ((packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1u+32u] & 0x40u) >> 2u); // bit 12 = Octet 32, bit 6 / Read Local OOB Extended Data command 22398d2d8af1SMatthias Ringwald log_info("Local supported commands summary %02x - %02x", hci_stack->local_supported_commands[0], hci_stack->local_supported_commands[1]); 2240645b7c25SMatthias Ringwald break; 2241e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2242645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 22434f781026SMatthias Ringwald if (packet[5]) return; 22445b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2245645b7c25SMatthias Ringwald break; 2246645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2247645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2248573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2249573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2250c3b46f5aSMatthias Ringwald if (conn != NULL) { 22519866fdc7SMatthias Ringwald uint8_t key_size = 0; 2252573897a0SMatthias Ringwald if (status == 0){ 22539866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 22544a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2255170fafaeSMatthias Ringwald } else { 2256e9f98c4aSMatthias Ringwald key_size = 1; 22579866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2258573897a0SMatthias Ringwald } 22599866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2260c3b46f5aSMatthias Ringwald } 2261645b7c25SMatthias Ringwald break; 226275a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 226375a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 226475a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 226575a8e4faSMatthias Ringwald uint8_t event[67]; 226675a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 226775a8e4faSMatthias Ringwald event[1] = 65; 226875a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 226975a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 227075a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 227175a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 227275a8e4faSMatthias Ringwald event[2] = 3; 227375a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 227475a8e4faSMatthias Ringwald } else { 227575a8e4faSMatthias Ringwald event[2] = 1; 227675a8e4faSMatthias Ringwald } 227775a8e4faSMatthias Ringwald } 227875a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 227975a8e4faSMatthias Ringwald break; 228075a8e4faSMatthias Ringwald } 2281e8c8828eSMatthias Ringwald #endif 228275a8e4faSMatthias Ringwald #endif 22836f35bb46SMatthias Ringwald default: 22846f35bb46SMatthias Ringwald break; 22854f781026SMatthias Ringwald } 2286645b7c25SMatthias Ringwald } 22874f781026SMatthias Ringwald 22880ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 22890ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 22900ce3f217SMatthias Ringwald bd_addr_t addr; 22910ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 22920ce3f217SMatthias Ringwald hci_connection_t * conn; 22930ce3f217SMatthias Ringwald 22940ce3f217SMatthias Ringwald // Connection management 22950ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 22960ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 22970ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 22980ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 22990ce3f217SMatthias Ringwald 23000ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 23010ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 23020ce3f217SMatthias Ringwald if (packet[3]){ 23030ce3f217SMatthias Ringwald 23040ce3f217SMatthias Ringwald // handle cancelled outgoing connection 23050ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 23060ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 23070ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 23080ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 23091f468175SMatthias Ringwald // reset state 23100ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 23111f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 23120ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 23130ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 23140ce3f217SMatthias Ringwald } 23150ce3f217SMatthias Ringwald 23160ce3f217SMatthias Ringwald // outgoing le connection establishment is done 23170ce3f217SMatthias Ringwald if (conn){ 23180ce3f217SMatthias Ringwald // remove entry 23190ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 23200ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 23210ce3f217SMatthias Ringwald } 23220ce3f217SMatthias Ringwald return; 23230ce3f217SMatthias Ringwald } 23240ce3f217SMatthias Ringwald #endif 23250ce3f217SMatthias Ringwald 23260ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 23270ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 23280ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 23290ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 23300ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 23310ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 23320ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 23330ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 23340ce3f217SMatthias Ringwald } 23350ce3f217SMatthias Ringwald #endif 23360ce3f217SMatthias Ringwald } else { 23370ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 23380ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 23390ce3f217SMatthias Ringwald hci_stack->le_advertisements_active = false; 23400ce3f217SMatthias Ringwald #endif 23410ce3f217SMatthias Ringwald } 23420ce3f217SMatthias Ringwald 23430ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 23440ce3f217SMatthias Ringwald if (!conn){ 23450ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 23460ce3f217SMatthias Ringwald } 23470ce3f217SMatthias Ringwald 23480ce3f217SMatthias Ringwald // no memory, sorry. 23490ce3f217SMatthias Ringwald if (!conn){ 23500ce3f217SMatthias Ringwald return; 23510ce3f217SMatthias Ringwald } 23520ce3f217SMatthias Ringwald 23530ce3f217SMatthias Ringwald conn->state = OPEN; 23540ce3f217SMatthias Ringwald conn->role = packet[6]; 23550ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 23560ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 23570ce3f217SMatthias Ringwald 23580ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 23590ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 23600ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 23610ce3f217SMatthias Ringwald } 23620ce3f217SMatthias Ringwald #endif 23630ce3f217SMatthias Ringwald 2364dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 2365dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 2366dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 2367dde9ff1eSMatthias Ringwald 23680ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 23690ce3f217SMatthias Ringwald 23700ce3f217SMatthias Ringwald // restart timer 23710ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 23720ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 23730ce3f217SMatthias Ringwald 23740ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 23750ce3f217SMatthias Ringwald 23760ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 23770ce3f217SMatthias Ringwald } 23780ce3f217SMatthias Ringwald #endif 23790ce3f217SMatthias Ringwald 238017c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 238117c6fe5cSMatthias Ringwald static bool hci_ssp_security_level_possible_for_io_cap(gap_security_level_t level, uint8_t io_cap_local, uint8_t io_cap_remote){ 238217c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 238317c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 238417c6fe5cSMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities 238517c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 238617c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 238717c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 238817c6fe5cSMatthias Ringwald if ((io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) && (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY)) return false; 238917c6fe5cSMatthias Ringwald } 239017c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 239117c6fe5cSMatthias Ringwald return true; 239217c6fe5cSMatthias Ringwald } 239317c6fe5cSMatthias Ringwald 239417c6fe5cSMatthias Ringwald static bool hci_ssp_validate_possible_security_level(bd_addr_t addr){ 239517c6fe5cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 239617c6fe5cSMatthias Ringwald if (!conn) return false; 239717c6fe5cSMatthias Ringwald // abort pairing, if requested security level cannot be met 239817c6fe5cSMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 239917c6fe5cSMatthias Ringwald // inlined hci_add_connection_flags_for_flipped_bd_addr 240017c6fe5cSMatthias Ringwald hci_connection_timestamp(conn); 240117c6fe5cSMatthias Ringwald return true; 240217c6fe5cSMatthias Ringwald } else { 240317c6fe5cSMatthias Ringwald log_info("Level %u cannot be reached", conn->requested_security_level); 240417c6fe5cSMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 240517c6fe5cSMatthias Ringwald return false; 240617c6fe5cSMatthias Ringwald }; 240717c6fe5cSMatthias Ringwald } 240817c6fe5cSMatthias Ringwald 240917c6fe5cSMatthias Ringwald #endif 241017c6fe5cSMatthias Ringwald 2411c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 24124f781026SMatthias Ringwald 24134f781026SMatthias Ringwald uint16_t event_length = packet[1]; 24144f781026SMatthias Ringwald 24154f781026SMatthias Ringwald // assert packet is complete 24164ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 24174f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 24184f781026SMatthias Ringwald return; 24194f781026SMatthias Ringwald } 24204f781026SMatthias Ringwald 24214f781026SMatthias Ringwald bd_addr_type_t addr_type; 24224f781026SMatthias Ringwald hci_con_handle_t handle; 24234f781026SMatthias Ringwald hci_connection_t * conn; 24244f781026SMatthias Ringwald int i; 24254f781026SMatthias Ringwald int create_connection_cmd; 24264f781026SMatthias Ringwald 24274f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 24285e91d96cSMatthias Ringwald hci_link_type_t link_type; 242948f33f37SMatthias Ringwald bd_addr_t addr; 24304f781026SMatthias Ringwald #endif 24314f781026SMatthias Ringwald 24324f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 24334f781026SMatthias Ringwald 24344f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 24354f781026SMatthias Ringwald 24364f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 24374f781026SMatthias Ringwald handle_command_complete_event(packet, size); 243856cf178bSmatthias.ringwald break; 243956cf178bSmatthias.ringwald 24407ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 24416bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 24426bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2443229331c6SMatthias Ringwald 2444229331c6SMatthias Ringwald // check command status to detected failed outgoing connections 2445c57fa566SMatthias Ringwald create_connection_cmd = 0; 2446c57fa566SMatthias Ringwald #ifdef ENABLE_CLASSIC 2447c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){ 2448c57fa566SMatthias Ringwald create_connection_cmd = 1; 2449c57fa566SMatthias Ringwald } 2450c57fa566SMatthias Ringwald #endif 2451c57fa566SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2452c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_le_create_connection)){ 2453c57fa566SMatthias Ringwald create_connection_cmd = 1; 2454c57fa566SMatthias Ringwald } 2455c57fa566SMatthias Ringwald #endif 2456c57fa566SMatthias Ringwald if (create_connection_cmd) { 2457229331c6SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 24588da98cbeSMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 24596ea9315cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 24606ea9315cSMatthias 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); 2461229331c6SMatthias Ringwald 2462229331c6SMatthias Ringwald // reset outgoing address info 2463229331c6SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2464229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2465229331c6SMatthias Ringwald 24666ea9315cSMatthias Ringwald // on error 24676ea9315cSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2468f75e06adSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 24696ea9315cSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 24706ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2471f7e6a692SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 24726ea9315cSMatthias Ringwald } 2473f75e06adSMatthias Ringwald #endif 2474229331c6SMatthias Ringwald // error => outgoing connection failed 24756ea9315cSMatthias Ringwald if (conn != NULL){ 2476229331c6SMatthias Ringwald hci_handle_connection_failed(conn, status); 2477229331c6SMatthias Ringwald } 2478229331c6SMatthias Ringwald } 24796ea9315cSMatthias Ringwald } 2480beb3c81dSMatthias Ringwald 2481beb3c81dSMatthias Ringwald #ifdef ENABLE_CLASSIC 2482beb3c81dSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_inquiry)) { 2483beb3c81dSMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2484beb3c81dSMatthias Ringwald log_info("command status (inquiry), status %x", status); 2485beb3c81dSMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 2486beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2487beb3c81dSMatthias Ringwald } else { 2488beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2489beb3c81dSMatthias Ringwald } 2490beb3c81dSMatthias Ringwald } 2491beb3c81dSMatthias Ringwald #endif 24927ec5eeaaSmatthias.ringwald break; 24937ec5eeaaSmatthias.ringwald 24942e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 24959784dac1SMatthias Ringwald if (size < 3) return; 24969784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 24974ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 24989784dac1SMatthias Ringwald uint16_t offset = 3; 24999784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 25004ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 25014ea43905SMatthias Ringwald offset += 2u; 2502f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 25034ea43905SMatthias Ringwald offset += 2u; 25042e440c8aS[email protected] 25055061f3afS[email protected] conn = hci_connection_for_handle(handle); 250656cf178bSmatthias.ringwald if (!conn){ 25079da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 250856cf178bSmatthias.ringwald continue; 250956cf178bSmatthias.ringwald } 251023bed257S[email protected] 2511ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets){ 2512ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 2513e35edcc1S[email protected] } else { 2514ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 2515ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 2516e35edcc1S[email protected] } 2517ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 2518760b20efSMatthias Ringwald 2519760b20efSMatthias Ringwald #ifdef ENABLE_CLASSIC 2520760b20efSMatthias Ringwald // For SCO, we do the can_send_now_check here 2521760b20efSMatthias Ringwald hci_notify_if_sco_can_send_now(); 2522760b20efSMatthias Ringwald #endif 252356cf178bSmatthias.ringwald } 25246772a24cSmatthias.ringwald break; 25252e440c8aS[email protected] } 252635454696SMatthias Ringwald 252735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2528f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 2529f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 2530f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2531f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2532f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2533f5875de5SMatthias Ringwald } 2534f5875de5SMatthias Ringwald break; 2535b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 2536b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 2537b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 2538b7f1ee76SMatthias Ringwald } 2539b7f1ee76SMatthias Ringwald break; 25401f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 2541724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 25425e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 254307e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 25445e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 25454536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 254607e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 254707e010b6SMilanka Ringwald break; 254807e010b6SMilanka Ringwald } 254907e010b6SMilanka Ringwald } 255007e010b6SMilanka Ringwald 255137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 25525e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 25535e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 25542e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 25551f7b95a1Smatthias.ringwald if (!conn) { 25565293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 25571f7b95a1Smatthias.ringwald } 2558ce4c8fabSmatthias.ringwald if (!conn) { 2559ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 25604536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 2561058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 2562ce4c8fabSmatthias.ringwald break; 2563ce4c8fabSmatthias.ringwald } 25645cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 256532ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 2566f3a16b9aSMatthias Ringwald // store info about eSCO 25675e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 256876ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 2569f3a16b9aSMatthias Ringwald } 257032ab9390Smatthias.ringwald hci_run(); 25711f7b95a1Smatthias.ringwald break; 25721f7b95a1Smatthias.ringwald 25736772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 2574fe1ed1b8Smatthias.ringwald // Connection management 2575724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 25769da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 2577f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 25782e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2579fe1ed1b8Smatthias.ringwald if (conn) { 2580b448a0e7Smatthias.ringwald if (!packet[2]){ 2581c8e4258aSmatthias.ringwald conn->state = OPEN; 2582f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 25836909f064SMatthias Ringwald 25846909f064SMatthias Ringwald // queue get remote feature 2585ba018746SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 2586ee091cf1Smatthias.ringwald 2587e6c51921SMatthias Ringwald // queue set supervision timeout if we're master 2588d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 25896909f064SMatthias Ringwald connectionSetAuthenticationFlags(conn, WRITE_SUPERVISION_TIMEOUT); 25906909f064SMatthias Ringwald } 25916909f064SMatthias Ringwald 2592c785ef68Smatthias.ringwald // restart timer 2593528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2594528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 2595c785ef68Smatthias.ringwald 25969da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 259743bfb1bdSmatthias.ringwald 259843bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 2599b448a0e7Smatthias.ringwald } else { 26000bbba85bSMatthias Ringwald // connection failed 26010bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 2602fe1ed1b8Smatthias.ringwald } 2603fe1ed1b8Smatthias.ringwald } 26046772a24cSmatthias.ringwald break; 2605fe1ed1b8Smatthias.ringwald 260644d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 2607724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 260844d0e3d5S[email protected] log_info("Synchronous Connection Complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 26091a06f663S[email protected] if (packet[2]){ 261044d0e3d5S[email protected] // connection failed 261144d0e3d5S[email protected] break; 261244d0e3d5S[email protected] } 26132e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2614e35edcc1S[email protected] if (!conn) { 2615e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2616e35edcc1S[email protected] } 2617e35edcc1S[email protected] if (!conn) { 2618e35edcc1S[email protected] break; 2619e35edcc1S[email protected] } 26201a06f663S[email protected] conn->state = OPEN; 2621f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2622ee752bb8SMatthias Ringwald 2623ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2624ee752bb8SMatthias Ringwald // update SCO 2625ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 2626ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 2627ee752bb8SMatthias Ringwald } 2628f234b250SMatthias Ringwald // trigger can send now 2629f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 2630f234b250SMatthias Ringwald hci_stack->sco_can_send_now = 1; 2631f234b250SMatthias Ringwald } 2632ee752bb8SMatthias Ringwald #endif 2633cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 2634cb70c5abSMatthias Ringwald // configure sco transport 2635cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 2636cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 2637cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 2638cb70c5abSMatthias Ringwald } 2639cb70c5abSMatthias Ringwald #endif 264044d0e3d5S[email protected] break; 264144d0e3d5S[email protected] 2642afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 2643f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2644afd4e962S[email protected] conn = hci_connection_for_handle(handle); 2645afd4e962S[email protected] if (!conn) break; 2646afd4e962S[email protected] if (!packet[2]){ 26472f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 26482f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 26492f5c44baSMatthias Ringwald 26505ccef624SMatthias Ringwald // read extended features if possible 26515ccef624SMatthias Ringwald if (((hci_stack->local_supported_commands[1] & 1) != 0) && ((conn->remote_supported_features[0] & 2) != 0)) { 26525ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 26535ccef624SMatthias Ringwald break; 26545ccef624SMatthias Ringwald } 26555ccef624SMatthias Ringwald } 26565ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 26575ccef624SMatthias Ringwald break; 26585ccef624SMatthias Ringwald 26595ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 26605ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 26615ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 26625ccef624SMatthias Ringwald if (!conn) break; 26635ccef624SMatthias Ringwald // status = ok, page = 1 266450c51a77SMatthias Ringwald if (!packet[2]) { 266550c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 266650c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 26675ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 266850c51a77SMatthias Ringwald bool done = false; 266950c51a77SMatthias Ringwald switch (page_number){ 267050c51a77SMatthias Ringwald case 1: 26712f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 267250c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 267350c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 267450c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 267550c51a77SMatthias Ringwald } else { 267650c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 267750c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 267850c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 267950c51a77SMatthias Ringwald } 268050c51a77SMatthias Ringwald done = true; 268150c51a77SMatthias Ringwald } 268250c51a77SMatthias Ringwald break; 268350c51a77SMatthias Ringwald case 2: 268450c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 268550c51a77SMatthias Ringwald done = true; 268650c51a77SMatthias Ringwald break; 268750c51a77SMatthias Ringwald default: 268850c51a77SMatthias Ringwald break; 268950c51a77SMatthias Ringwald } 269050c51a77SMatthias Ringwald if (!done) break; 2691afd4e962S[email protected] } 2692de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 2693afd4e962S[email protected] break; 2694afd4e962S[email protected] 26957fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 26967fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 2697608f51bbSMatthias Ringwald // request handled by hci_run() 269832ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 2699608f51bbSMatthias Ringwald break; 27007fde4af9Smatthias.ringwald 27019ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 27021714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 2703f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 27049ab95c90S[email protected] if (!conn) break; 27051714cbbdSMatthias Ringwald 27061714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 27071714cbbdSMatthias Ringwald 27089ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 27097bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 27109ab95c90S[email protected] // Change Connection Encryption keeps link key type 27119ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 27129ab95c90S[email protected] conn->link_key_type = link_key_type; 27139ab95c90S[email protected] } 2714e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 2715e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 2716e9f98c4aSMatthias Ringwald 2717bec5f683SMatthias Ringwald // only store link key: 2718bec5f683SMatthias Ringwald // - if bondable enabled 2719bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 27206edaed7fSMatthias Ringwald // - if security level sufficient 27216edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 2722bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 2723bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 2724532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 2725532454f9SMatthias Ringwald if (!remote_bonding){ 2726bec5f683SMatthias Ringwald break; 2727bec5f683SMatthias Ringwald } 2728bec5f683SMatthias Ringwald } 272955597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 27307fde4af9Smatthias.ringwald break; 27319ab95c90S[email protected] } 27327fde4af9Smatthias.ringwald 27337fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 27341714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 27351714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 27361714cbbdSMatthias Ringwald if (!conn) break; 27371714cbbdSMatthias Ringwald 27381714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 27391714cbbdSMatthias Ringwald // non-bondable mode: pin code negative reply will be sent (event is not forwarded to app) 27403a9fb326S[email protected] if (!hci_stack->bondable){ 27411714cbbdSMatthias Ringwald conn->authentication_flags |= DENY_PIN_CODE_REQUEST; 27421714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 2743f8fb5f6eS[email protected] hci_run(); 2744f8fb5f6eS[email protected] return; 27454c57c146S[email protected] } 27467fde4af9Smatthias.ringwald break; 27477fde4af9Smatthias.ringwald 274850d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 274950d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 275050d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 275150d7398cSMatthias Ringwald if (!conn) break; 27521714cbbdSMatthias Ringwald 27532ff4272dSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_RESPONSE); 27541714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 275550d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 2756e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 275750d7398cSMatthias Ringwald break; 275850d7398cSMatthias Ringwald 27591d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 27601714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 27611714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 27621714cbbdSMatthias Ringwald if (!conn) break; 27631714cbbdSMatthias Ringwald 27641d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 27651714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 276644565b0cSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 27678dbd6159SMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 2768dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 276911b03efaSMatthias Ringwald } else { 277011b03efaSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 277111b03efaSMatthias Ringwald } 277244565b0cSMatthias Ringwald #endif 2773dbe1a790S[email protected] break; 2774dbe1a790S[email protected] 27751849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 27761849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 27771849becdSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 27781849becdSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_REMOTE_OOB_DATA_REPLY); 27791849becdSMatthias Ringwald break; 27801849becdSMatthias Ringwald #endif 27811849becdSMatthias Ringwald 2782dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 278317c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 278417c6fe5cSMatthias Ringwald if (hci_ssp_validate_possible_security_level(addr) == false) break; 27853a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 2786dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 2787dbe1a790S[email protected] break; 2788dbe1a790S[email protected] 2789dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 279017c6fe5cSMatthias Ringwald hci_event_user_passkey_request_get_bd_addr(packet, addr); 279117c6fe5cSMatthias Ringwald if (hci_ssp_validate_possible_security_level(addr) == false) break; 27923a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 2793dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 27941d6b20aeS[email protected] break; 27951849becdSMatthias Ringwald 27963dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 27973dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 27983dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 27993dce6128SMatthias Ringwald if (!conn) break; 28003dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 28013dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 28023dce6128SMatthias Ringwald break; 280335454696SMatthias Ringwald #endif 28041d6b20aeS[email protected] 2805f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 2806254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 2807f0944df2S[email protected] conn = hci_connection_for_handle(handle); 2808f0944df2S[email protected] if (!conn) break; 28094ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 2810254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 2811254b78eeSMatthias Ringwald if (encryption_enabled){ 2812573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 2813573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 2814f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 2815573897a0SMatthias Ringwald } 2816573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 2817573897a0SMatthias Ringwald else { 2818fcaf38b9SMatthias Ringwald 2819fcaf38b9SMatthias Ringwald // dedicated bonding: send result and disconnect 2820fcaf38b9SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2821fcaf38b9SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 2822fcaf38b9SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 2823fcaf38b9SMatthias Ringwald conn->bonding_status = packet[2]; 2824fcaf38b9SMatthias Ringwald break; 2825fcaf38b9SMatthias Ringwald } 2826fcaf38b9SMatthias Ringwald 2827254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 2828254b78eeSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type) != 0; 2829254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 2830edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 2831254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 28329ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 2833254b78eeSMatthias Ringwald break; 2834254b78eeSMatthias Ringwald } 2835254b78eeSMatthias Ringwald 28366e058d3fSMatthias Ringwald if ((hci_stack->local_supported_commands[0] & 0x80) != 0){ 28376e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 2838573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 28396e058d3fSMatthias Ringwald } else { 28406e058d3fSMatthias Ringwald // if not, pretend everything is perfect 28419866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 28426e058d3fSMatthias Ringwald } 2843573897a0SMatthias Ringwald } 2844573897a0SMatthias Ringwald #endif 2845f0944df2S[email protected] } else { 2846536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 2847f0944df2S[email protected] } 2848ad83dc6aS[email protected] } 2849573897a0SMatthias Ringwald 2850f0944df2S[email protected] break; 2851f0944df2S[email protected] 285235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 28531eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 2854abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 28551eb2563eS[email protected] conn = hci_connection_for_handle(handle); 28561eb2563eS[email protected] if (!conn) break; 2857ad83dc6aS[email protected] 2858dbd5dcc3SMatthias Ringwald // clear authentication active flag 2859dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 28601714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 2861dbd5dcc3SMatthias Ringwald 2862abdad579SMatthias Ringwald // authenticated only if auth status == 0 2863abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 2864abdad579SMatthias Ringwald // authenticated 2865abdad579SMatthias Ringwald conn->authentication_flags |= CONNECTION_AUTHENTICATED; 2866abdad579SMatthias Ringwald 2867131ef17aSMatthias Ringwald // If not already encrypted, start encryption 2868131ef17aSMatthias Ringwald if ((conn->authentication_flags & CONNECTION_ENCRYPTED) == 0){ 28691eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2870ad83dc6aS[email protected] break; 2871ad83dc6aS[email protected] } 2872abdad579SMatthias Ringwald } 2873abdad579SMatthias Ringwald 2874abdad579SMatthias Ringwald // emit updated security level 28751cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 28761eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 28771eb2563eS[email protected] break; 28781714cbbdSMatthias Ringwald 28791714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 28801714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 28811714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 28821714cbbdSMatthias Ringwald if (!conn) break; 28831714cbbdSMatthias Ringwald 28841714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 28851714cbbdSMatthias Ringwald break; 288635454696SMatthias Ringwald #endif 288734d2123cS[email protected] 288886805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 2889ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 2890ccda6e14S[email protected] // see end of function, too. 2891a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 2892a4f30ec0S[email protected] if (packet[2]) break; // status != 0 2893f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 289481d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 28954ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 2896c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 28974ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 289881d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 2899c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 2900c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 290181d2bdb2SMatthias Ringwald if (release_buffer){ 290281d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 290381d2bdb2SMatthias Ringwald } 2904c6a37cfdSMatthias Ringwald } 2905c6a37cfdSMatthias Ringwald } 290635454696SMatthias Ringwald 2907c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 2908c6a37cfdSMatthias Ringwald if (!conn) break; 29091714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 29101714cbbdSMatthias Ringwald // pairing failed if it was ongoing 29111714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 29121714cbbdSMatthias Ringwald #endif 2913046ec007SMatthias Ringwald // mark connection for shutdown 2914a70e17d4SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 2915046ec007SMatthias Ringwald 2916046ec007SMatthias Ringwald // emit dedicatd bonding event 2917046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 2918046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 2919046ec007SMatthias Ringwald } 2920046ec007SMatthias Ringwald 292135454696SMatthias Ringwald #ifdef ENABLE_BLE 2922d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 2923046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 29242b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 2925bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 29269a2e4658SMatthias Ringwald } 292735454696SMatthias Ringwald #endif 2928d70217a2SMatthias Ringwald #endif 2929ccda6e14S[email protected] break; 29306772a24cSmatthias.ringwald 2931c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 2932313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 2933d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 2934c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 29357586ee35S[email protected] } else { 29367586ee35S[email protected] // if no special requests, just reboot stack 29377586ee35S[email protected] hci_power_control_off(); 29387586ee35S[email protected] hci_power_control_on(); 2939c68bdf90Smatthias.ringwald } 2940c68bdf90Smatthias.ringwald break; 2941c68bdf90Smatthias.ringwald 29420e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 29435cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 29445cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 2945c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 2946f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 2947c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2948c4c88f1bSJakob Krantz if (!conn) break; 29495cf766e8SMatthias Ringwald conn->role = packet[9]; 29505cf766e8SMatthias Ringwald break; 29510e6f3837SMatthias Ringwald #endif 29525cf766e8SMatthias Ringwald 295363fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 2954d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 29554fa24b5fS[email protected] if (hci_transport_synchronous()) { 295663fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 29574fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 29584fa24b5fS[email protected] } 295981d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 2960d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 2961d051460cS[email protected] hci_release_packet_buffer(); 2962701e3307SMatthias Ringwald 296363fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 296463fa3374SMatthias Ringwald 296535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 296663fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2967701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 296835454696SMatthias Ringwald #endif 29696b4af23dS[email protected] break; 29706b4af23dS[email protected] 297135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 297263fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 297363fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 2974f234b250SMatthias Ringwald hci_stack->sco_can_send_now = 1; 297563fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 297663fa3374SMatthias Ringwald return; 29771cfb383eSMatthias Ringwald 29781cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 29791cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 29801cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 29811cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 29829784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 29831cfb383eSMatthias Ringwald break; 298435454696SMatthias Ringwald #endif 298563fa3374SMatthias Ringwald 2986a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 29875909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 29885909f7f2Smatthias.ringwald switch (packet[2]){ 2989d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 299057c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 29914f4e0224SMatthias Ringwald // log_info("advertising report received"); 29926fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 299357c9da5bS[email protected] le_handle_advertisement_report(packet, size); 29947bdc6798S[email protected] break; 2995d70217a2SMatthias Ringwald #endif 29965909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 29970ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 29985909f7f2Smatthias.ringwald break; 29995909f7f2Smatthias.ringwald 3000f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3001c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3002c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3003c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3004c9db5c21SMilanka Ringwald if (!conn) break; 3005c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3006c9db5c21SMilanka Ringwald break; 300765a46ef3S[email protected] 300873cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 300973cd8a2aSMatthias Ringwald // connection 301073cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 301173cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 301273cd8a2aSMatthias Ringwald if (conn) { 301373cd8a2aSMatthias Ringwald // read arguments 301473cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 301573cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 301673cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 301773cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 301873cd8a2aSMatthias Ringwald 301973cd8a2aSMatthias Ringwald // validate against current connection parameter range 302073cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 302173cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 302273cd8a2aSMatthias 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); 302373cd8a2aSMatthias Ringwald if (update_parameter){ 302473cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 302573cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 302673cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 302773cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 302873cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 302973cd8a2aSMatthias Ringwald } else { 3030c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 303173cd8a2aSMatthias Ringwald } 303273cd8a2aSMatthias Ringwald } 303373cd8a2aSMatthias Ringwald break; 30340f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 30350f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 30360f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 30370f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 30380f3b27c5SMatthias Ringwald if (conn) { 30390f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 30400f3b27c5SMatthias Ringwald } 30410f3b27c5SMatthias Ringwald break; 30420f3b27c5SMatthias Ringwald #endif 30435909f7f2Smatthias.ringwald default: 30445909f7f2Smatthias.ringwald break; 30455909f7f2Smatthias.ringwald } 30465909f7f2Smatthias.ringwald break; 30475909f7f2Smatthias.ringwald #endif 30480b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 30490b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 30500b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 30510b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 30520b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 30530b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 30540b36101dSMatthias Ringwald break; 30550b36101dSMatthias Ringwald default: 30560b36101dSMatthias Ringwald break; 30570b36101dSMatthias Ringwald } 30580b36101dSMatthias Ringwald break; 30596772a24cSmatthias.ringwald default: 30606772a24cSmatthias.ringwald break; 3061fe1ed1b8Smatthias.ringwald } 3062fe1ed1b8Smatthias.ringwald 306367c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 306489db417bSmatthias.ringwald 306586805605S[email protected] // notify upper stack 3066d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 306794ab26f8Smatthias.ringwald 306886805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 3069797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 3070f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 307105ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 3072046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 3073797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 307405ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 3075b0136355SMatthias Ringwald } 307686805605S[email protected] } 307786805605S[email protected] 307894ab26f8Smatthias.ringwald // execute main loop 307994ab26f8Smatthias.ringwald hci_run(); 308016833f0aSmatthias.ringwald } 308116833f0aSmatthias.ringwald 308235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 30836f28d2eeSMatthias Ringwald 30846be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 30856f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 30866f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 30876f28d2eeSMatthias Ringwald 30886f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 308959c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 30905a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 30916f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 30926f28d2eeSMatthias Ringwald if (!conn) return; 30936f28d2eeSMatthias Ringwald 3094afaddd67SMatthias Ringwald // trigger send 3095afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 3096e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 3097a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 3098e4157653SMatthias Ringwald conn->sco_tx_ready++; 3099e4157653SMatthias Ringwald } 3100afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 31016f28d2eeSMatthias Ringwald } 31026f28d2eeSMatthias Ringwald 310359c33575SMatthias Ringwald 310459c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 310559c33575SMatthias Ringwald 31066f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 310759c33575SMatthias Ringwald 31086f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 310959c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 311059c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 311159c33575SMatthias Ringwald 311259c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 311359c33575SMatthias Ringwald 311459c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 311559c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 311659c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 311759c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 311859c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 31196f28d2eeSMatthias Ringwald } 31206be3cf7fSMatthias Ringwald #endif 31216f28d2eeSMatthias Ringwald 3122c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 31230b3f95dfSMatthias Ringwald // lookup connection struct 31242b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 31252b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 31260b3f95dfSMatthias Ringwald if (!conn) return; 31270b3f95dfSMatthias Ringwald 31286be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3129760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 3130760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 3131a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 3132760b20efSMatthias Ringwald packet[2] = 0x3c; 3133760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 3134760b20efSMatthias Ringwald size = 63; 31350b3f95dfSMatthias Ringwald } 31360b3f95dfSMatthias Ringwald } 3137760b20efSMatthias Ringwald 3138f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 3139f234b250SMatthias Ringwald // Nothing to do 3140f234b250SMatthias Ringwald } else { 314149205f5dSMatthias 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); 3142760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 31436f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 314459c33575SMatthias Ringwald 314559c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 314659c33575SMatthias Ringwald // ignore first 10 packets 31476f28d2eeSMatthias Ringwald conn->sco_rx_count++; 314859c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 314959c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 315059c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 31516f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 31526f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 315359c33575SMatthias Ringwald conn->sco_rx_count = 0; 315459c33575SMatthias Ringwald sco_schedule_tx(conn); 315559c33575SMatthias Ringwald } 315659c33575SMatthias Ringwald } else { 315759c33575SMatthias Ringwald // track expected arrival timme 315859c33575SMatthias Ringwald conn->sco_rx_count++; 315959c33575SMatthias Ringwald conn->sco_rx_ms += 7; 316059c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 316159c33575SMatthias Ringwald if (delta > 0){ 316259c33575SMatthias Ringwald conn->sco_rx_ms++; 316359c33575SMatthias Ringwald } 316459c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 31656f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 31666f28d2eeSMatthias Ringwald } 3167760b20efSMatthias Ringwald } 3168f234b250SMatthias Ringwald } 31696be3cf7fSMatthias Ringwald #endif 31706be3cf7fSMatthias Ringwald 31710b3f95dfSMatthias Ringwald // deliver to app 31720b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 31730b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 31740b3f95dfSMatthias Ringwald } 31750b3f95dfSMatthias Ringwald 3176ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3177ed325439SMatthias Ringwald // We can send one packet for each received packet 3178ed325439SMatthias Ringwald conn->sco_tx_ready++; 3179ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 3180ed325439SMatthias Ringwald #endif 3181ed325439SMatthias Ringwald 31820b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 31832b838201SMatthias Ringwald conn->num_packets_completed++; 31842b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 31852b838201SMatthias Ringwald hci_run(); 31862b838201SMatthias Ringwald #endif 3187c91d150bS[email protected] } 318835454696SMatthias Ringwald #endif 3189c91d150bS[email protected] 31900a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 31915bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 319210e830c9Smatthias.ringwald switch (packet_type) { 319310e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 319410e830c9Smatthias.ringwald event_handler(packet, size); 319510e830c9Smatthias.ringwald break; 319610e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 319710e830c9Smatthias.ringwald acl_handler(packet, size); 319810e830c9Smatthias.ringwald break; 319935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3200c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 3201c91d150bS[email protected] sco_handler(packet, size); 3202202c8a4cSMatthias Ringwald break; 320335454696SMatthias Ringwald #endif 320410e830c9Smatthias.ringwald default: 320510e830c9Smatthias.ringwald break; 320610e830c9Smatthias.ringwald } 320710e830c9Smatthias.ringwald } 320810e830c9Smatthias.ringwald 3209d6b06661SMatthias Ringwald /** 3210d6b06661SMatthias Ringwald * @brief Add event packet handler. 3211d6b06661SMatthias Ringwald */ 3212d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 3213d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 3214d6b06661SMatthias Ringwald } 3215d6b06661SMatthias Ringwald 3216d6b06661SMatthias Ringwald 3217fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 32183d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 3219fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 322016833f0aSmatthias.ringwald } 322116833f0aSmatthias.ringwald 322235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 32238abbe8b5SMatthias Ringwald /** 32248abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 32258abbe8b5SMatthias Ringwald */ 32263d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 32278abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 32288abbe8b5SMatthias Ringwald } 322935454696SMatthias Ringwald #endif 32308abbe8b5SMatthias Ringwald 323171de195eSMatthias Ringwald static void hci_state_reset(void){ 3232595bdbfbS[email protected] // no connections yet 3233595bdbfbS[email protected] hci_stack->connections = NULL; 323474308b23Smatthias.ringwald 323574308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 323674308b23Smatthias.ringwald // hci_stack->discoverable = 0; 323774308b23Smatthias.ringwald // hci_stack->connectable = 0; 323874308b23Smatthias.ringwald // hci_stack->bondable = 1; 3239b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 3240595bdbfbS[email protected] 324144935e40S[email protected] // buffer is free 324244935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 324344935e40S[email protected] 3244595bdbfbS[email protected] // no pending cmds 3245595bdbfbS[email protected] hci_stack->decline_reason = 0; 3246595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 3247595bdbfbS[email protected] 3248c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 3249c214d65bSMatthias Ringwald 3250bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 3251bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = 0xffff; 3252bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = 0xffff; 3253bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = 0xff; 3254496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 3255bea424a5SMatthias Ringwald #endif 3256bea424a5SMatthias Ringwald 3257cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3258cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 3259cf01e888SMatthias Ringwald #endif 3260cf01e888SMatthias Ringwald 3261595bdbfbS[email protected] // LE 3262b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 3263b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 3264b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 3265d70217a2SMatthias Ringwald #endif 3266d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3267a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 3268b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3269d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3270e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 3271d70217a2SMatthias Ringwald #endif 3272a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3273a61834b6SMatthias Ringwald hci_stack->le_advertisements_active = false; 3274a61834b6SMatthias Ringwald if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_PARAMS_SET) != 0){ 3275a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3276a61834b6SMatthias Ringwald } 3277a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 3278a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3279a61834b6SMatthias Ringwald } 3280a61834b6SMatthias Ringwald #endif 3281595bdbfbS[email protected] } 3282595bdbfbS[email protected] 328335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 32842d5e09d6SMatthias Ringwald /** 32852d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 32862d5e09d6SMatthias Ringwald */ 32872d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 32882d5e09d6SMatthias Ringwald // store and open remote device db 32892d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 32902d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 32912d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 32922d5e09d6SMatthias Ringwald } 32932d5e09d6SMatthias Ringwald } 329435454696SMatthias Ringwald #endif 32952d5e09d6SMatthias Ringwald 32962d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 3297475c8125Smatthias.ringwald 32983a9fb326S[email protected] #ifdef HAVE_MALLOC 32993a9fb326S[email protected] if (!hci_stack) { 33003a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 33013a9fb326S[email protected] } 33023a9fb326S[email protected] #else 33033a9fb326S[email protected] hci_stack = &hci_stack_static; 33043a9fb326S[email protected] #endif 330566fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 33063a9fb326S[email protected] 3307475c8125Smatthias.ringwald // reference to use transport layer implementation 33083a9fb326S[email protected] hci_stack->hci_transport = transport; 3309475c8125Smatthias.ringwald 331011e23e5fSmatthias.ringwald // reference to used config 33113a9fb326S[email protected] hci_stack->config = config; 331211e23e5fSmatthias.ringwald 331326a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 331426a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 331526a9b6daSMatthias Ringwald 33168fcba05dSmatthias.ringwald // max acl payload size defined in config.h 33173a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 33188fcba05dSmatthias.ringwald 331916833f0aSmatthias.ringwald // register packet handlers with transport 332010e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 3321f5454fc6Smatthias.ringwald 33223a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 3323e2386ba1S[email protected] 3324e2386ba1S[email protected] // class of device 33253a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 3326a45d6b9fS[email protected] 3327f20168b8Smatthias.ringwald // bondable by default 3328f20168b8Smatthias.ringwald hci_stack->bondable = 1; 3329f20168b8Smatthias.ringwald 3330e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 333163168530SMatthias Ringwald // classic name 333263168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 3333c4c88f1bSJakob Krantz 3334c4c88f1bSJakob Krantz // Master slave policy 3335c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 3336170fafaeSMatthias Ringwald 3337b4eb4420SMatthias Ringwald // Allow Role Switch 3338b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 3339b4eb4420SMatthias Ringwald 334078315a58SMatthias Ringwald // Default / minimum security level = 2 334178315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 334278315a58SMatthias Ringwald 33435dbec6b3SMatthias Ringwald // Default Security Mode 4 33445dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 33455dbec6b3SMatthias Ringwald 3346cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 3347cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 3348d821984bSMatthias Ringwald 3349d821984bSMatthias Ringwald // Link Supervision Timeout 3350d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 3351d821984bSMatthias Ringwald 3352e9f343c8SMatthias Ringwald #endif 335363168530SMatthias Ringwald 335463048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 33553a9fb326S[email protected] hci_stack->ssp_enable = 1; 33563a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 33573a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 33583a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 335969a97523S[email protected] 33605d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 33615d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 33625d23aae8SMatthias Ringwald 3363fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 3364fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 3365d950d659SMatthias Ringwald 3366831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3367831711daSMatthias Ringwald // connection parameter to use for outgoing connections 3368cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 3369cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 3370831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 3371831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 3372831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 3373831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 3374831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 3375831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 33767261e5d8SMatthias Ringwald 33777261e5d8SMatthias Ringwald // default LE Scanning 33788b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 33798b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 33808b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 3381831711daSMatthias Ringwald #endif 3382831711daSMatthias Ringwald 33832b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 33842b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 33852b6ab3e6SMatthias Ringwald #endif 33862b6ab3e6SMatthias Ringwald 3387831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 3388831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 3389831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 3390831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 3391831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 3392831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 3393831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 3394831711daSMatthias Ringwald 3395595bdbfbS[email protected] hci_state_reset(); 3396475c8125Smatthias.ringwald } 3397475c8125Smatthias.ringwald 33984688f216SMatthias Ringwald void hci_deinit(void){ 33994688f216SMatthias Ringwald #ifdef HAVE_MALLOC 34004688f216SMatthias Ringwald if (hci_stack) { 34014688f216SMatthias Ringwald free(hci_stack); 34024688f216SMatthias Ringwald } 34034688f216SMatthias Ringwald #endif 34044688f216SMatthias Ringwald hci_stack = NULL; 3405b5bbcbf4SMatthias Ringwald 3406b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 34074688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 3408b5bbcbf4SMatthias Ringwald #endif 34094688f216SMatthias Ringwald } 34104688f216SMatthias Ringwald 34113fb36a29SMatthias Ringwald /** 34123fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 34133fb36a29SMatthias Ringwald */ 34143fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 34153fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 34163fb36a29SMatthias Ringwald 34173fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 34183fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 34193fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 34203fb36a29SMatthias Ringwald } 34213fb36a29SMatthias Ringwald } 34223fb36a29SMatthias Ringwald 3423fb55bd0aSMatthias Ringwald /** 3424d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 3425fb55bd0aSMatthias Ringwald */ 3426fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 3427fb55bd0aSMatthias Ringwald // references to used control implementation 3428fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 3429d0b87befSMatthias Ringwald // init with transport config 3430d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 3431fb55bd0aSMatthias Ringwald } 3432fb55bd0aSMatthias Ringwald 343371de195eSMatthias Ringwald void hci_close(void){ 3434e6d6524dSMatthias Ringwald 3435e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 3436404843c1Smatthias.ringwald // close remote device db 3437a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 3438a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 3439404843c1Smatthias.ringwald } 3440e6d6524dSMatthias Ringwald #endif 34417224be7eSMatthias Ringwald 34427224be7eSMatthias Ringwald btstack_linked_list_iterator_t lit; 34437224be7eSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 34447224be7eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 34457224be7eSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 34467224be7eSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 34477224be7eSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 34487224be7eSMatthias Ringwald hci_shutdown_connection(connection); 3449f5454fc6Smatthias.ringwald } 34507224be7eSMatthias Ringwald 3451f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 34523a9fb326S[email protected] 34533a9fb326S[email protected] #ifdef HAVE_MALLOC 34543a9fb326S[email protected] free(hci_stack); 34553a9fb326S[email protected] #endif 34563a9fb326S[email protected] hci_stack = NULL; 3457404843c1Smatthias.ringwald } 3458404843c1Smatthias.ringwald 3459cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3460cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 3461cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 3462cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 3463cb70c5abSMatthias Ringwald } 3464cb70c5abSMatthias Ringwald #endif 3465cb70c5abSMatthias Ringwald 346635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3467170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 3468170fafaeSMatthias Ringwald // validate ranage and set 3469170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 3470170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 3471170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 3472170fafaeSMatthias Ringwald } 347378315a58SMatthias Ringwald 3474137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 3475137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 34765dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 3477137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 3478137715ebSMatthias Ringwald } else { 3479137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3480137715ebSMatthias Ringwald } 34815dbec6b3SMatthias Ringwald } 34825dbec6b3SMatthias Ringwald 34835dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 34845dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 34855dbec6b3SMatthias Ringwald } 34865dbec6b3SMatthias Ringwald 348778315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 348878315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 348978315a58SMatthias Ringwald } 349078315a58SMatthias Ringwald 349178315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 349278315a58SMatthias Ringwald return hci_stack->gap_security_level; 349378315a58SMatthias Ringwald } 349430cdf3c6SMatthias Ringwald 349530cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 349630cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 349730cdf3c6SMatthias Ringwald } 349830cdf3c6SMatthias Ringwald 349930cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 350030cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 350130cdf3c6SMatthias Ringwald } 3502170fafaeSMatthias Ringwald #endif 3503170fafaeSMatthias Ringwald 3504170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 350560b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 35069e61646fS[email protected] hci_stack->class_of_device = class_of_device; 35079e61646fS[email protected] } 350876f27cffSMatthias Ringwald 3509c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 3510c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 3511c33e56d3SMatthias Ringwald } 3512c33e56d3SMatthias Ringwald 3513b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 3514b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 3515b4eb4420SMatthias Ringwald } 3516b4eb4420SMatthias Ringwald 3517b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 3518b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 3519b4eb4420SMatthias Ringwald } 3520b4eb4420SMatthias Ringwald 35210c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 35220c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 35230c3eb48dSMatthias Ringwald } 35240c3eb48dSMatthias Ringwald 352576f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 352676f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 352776f27cffSMatthias Ringwald } 352835454696SMatthias Ringwald #endif 35299e61646fS[email protected] 353076f27cffSMatthias Ringwald #if !defined(HAVE_PLATFORM_IPHONE_OS) && !defined (HAVE_HOST_CONTROLLER_API) 3531f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 3532f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 35336535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 3534f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 3535f456b2d0S[email protected] } 353676f27cffSMatthias Ringwald #endif 3537f456b2d0S[email protected] 35388d213e1aSmatthias.ringwald // State-Module-Driver overview 35398d213e1aSmatthias.ringwald // state module low-level 35408d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 35418d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 35428d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 35438d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 3544d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 3545d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 3546c7e0c5f6Smatthias.ringwald 354740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 35487301ad89Smatthias.ringwald 3549038bc64cSmatthias.ringwald // power on 3550f9a30166Smatthias.ringwald int err = 0; 35513a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 3552d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 3553f9a30166Smatthias.ringwald } 3554038bc64cSmatthias.ringwald if (err){ 35559da54300S[email protected] log_error( "POWER_ON failed"); 3556038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3557038bc64cSmatthias.ringwald return err; 3558038bc64cSmatthias.ringwald } 3559038bc64cSmatthias.ringwald 356024b3c629SMatthias Ringwald // int chipset driver 35613fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 35623fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 35633fb36a29SMatthias Ringwald } 35643fb36a29SMatthias Ringwald 356524b3c629SMatthias Ringwald // init transport 356624b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 356724b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 356824b3c629SMatthias Ringwald } 356924b3c629SMatthias Ringwald 357024b3c629SMatthias Ringwald // open transport 357124b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 3572038bc64cSmatthias.ringwald if (err){ 35739da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 35743a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3575d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3576f9a30166Smatthias.ringwald } 3577038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3578038bc64cSmatthias.ringwald return err; 3579038bc64cSmatthias.ringwald } 35808d213e1aSmatthias.ringwald return 0; 35818d213e1aSmatthias.ringwald } 3582038bc64cSmatthias.ringwald 358340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 35848d213e1aSmatthias.ringwald 35859da54300S[email protected] log_info("hci_power_control_off"); 35869418f9c9Smatthias.ringwald 35878d213e1aSmatthias.ringwald // close low-level device 358824b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 35898d213e1aSmatthias.ringwald 35909da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 35919418f9c9Smatthias.ringwald 35928d213e1aSmatthias.ringwald // power off 35933a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3594d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 35958d213e1aSmatthias.ringwald } 35969418f9c9Smatthias.ringwald 35979da54300S[email protected] log_info("hci_power_control_off - control closed"); 35989418f9c9Smatthias.ringwald 35993a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 360072ea5239Smatthias.ringwald } 360172ea5239Smatthias.ringwald 360240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 360372ea5239Smatthias.ringwald 36049da54300S[email protected] log_info("hci_power_control_sleep"); 36053144bce4Smatthias.ringwald 3606b429b9b7Smatthias.ringwald #if 0 3607b429b9b7Smatthias.ringwald // don't close serial port during sleep 3608b429b9b7Smatthias.ringwald 360972ea5239Smatthias.ringwald // close low-level device 36103a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 3611b429b9b7Smatthias.ringwald #endif 361272ea5239Smatthias.ringwald 361372ea5239Smatthias.ringwald // sleep mode 36143a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 3615d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 361672ea5239Smatthias.ringwald } 3617b429b9b7Smatthias.ringwald 36183a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 36198d213e1aSmatthias.ringwald } 36208d213e1aSmatthias.ringwald 362140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 3622b429b9b7Smatthias.ringwald 36239da54300S[email protected] log_info("hci_power_control_wake"); 3624b429b9b7Smatthias.ringwald 3625b429b9b7Smatthias.ringwald // wake on 36263a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 3627d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 3628b429b9b7Smatthias.ringwald } 3629b429b9b7Smatthias.ringwald 3630b429b9b7Smatthias.ringwald #if 0 3631b429b9b7Smatthias.ringwald // open low-level device 36323a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 3633b429b9b7Smatthias.ringwald if (err){ 36349da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 36353a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3636d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3637b429b9b7Smatthias.ringwald } 3638b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 3639b429b9b7Smatthias.ringwald return err; 3640b429b9b7Smatthias.ringwald } 3641b429b9b7Smatthias.ringwald #endif 3642b429b9b7Smatthias.ringwald 3643b429b9b7Smatthias.ringwald return 0; 3644b429b9b7Smatthias.ringwald } 3645b429b9b7Smatthias.ringwald 364644935e40S[email protected] static void hci_power_transition_to_initializing(void){ 364744935e40S[email protected] // set up state machine 364844935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 364944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 365044935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 36515c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 365244935e40S[email protected] } 3653b429b9b7Smatthias.ringwald 365442bd3d77SMatthias Ringwald // returns error 365542bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 365642bd3d77SMatthias Ringwald int err; 36578d213e1aSmatthias.ringwald switch (power_mode){ 36588d213e1aSmatthias.ringwald case HCI_POWER_ON: 36598d213e1aSmatthias.ringwald err = hci_power_control_on(); 366042bd3d77SMatthias Ringwald if (err != 0) { 3661f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 366297b61c7bS[email protected] return err; 366397b61c7bS[email protected] } 366444935e40S[email protected] hci_power_transition_to_initializing(); 36658d213e1aSmatthias.ringwald break; 36668d213e1aSmatthias.ringwald case HCI_POWER_OFF: 36678d213e1aSmatthias.ringwald // do nothing 36688d213e1aSmatthias.ringwald break; 36698d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3670b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 36718d213e1aSmatthias.ringwald break; 36727bbeb3adSMilanka Ringwald default: 36737bbeb3adSMilanka Ringwald btstack_assert(false); 36747bbeb3adSMilanka Ringwald break; 36758d213e1aSmatthias.ringwald } 367642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 367742bd3d77SMatthias Ringwald } 36787301ad89Smatthias.ringwald 367942bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 36808d213e1aSmatthias.ringwald switch (power_mode){ 36818d213e1aSmatthias.ringwald case HCI_POWER_ON: 36828d213e1aSmatthias.ringwald // do nothing 36838d213e1aSmatthias.ringwald break; 36848d213e1aSmatthias.ringwald case HCI_POWER_OFF: 36858d213e1aSmatthias.ringwald // no connections yet, just turn it off 36868d213e1aSmatthias.ringwald hci_power_control_off(); 36878d213e1aSmatthias.ringwald break; 36888d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3689b546ac54Smatthias.ringwald // no connections yet, just turn it off 369072ea5239Smatthias.ringwald hci_power_control_sleep(); 36918d213e1aSmatthias.ringwald break; 36927bbeb3adSMilanka Ringwald default: 36937bbeb3adSMilanka Ringwald btstack_assert(false); 36947bbeb3adSMilanka Ringwald break; 36958d213e1aSmatthias.ringwald } 369642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 369742bd3d77SMatthias Ringwald } 36987301ad89Smatthias.ringwald 369942bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 37008d213e1aSmatthias.ringwald switch (power_mode){ 37018d213e1aSmatthias.ringwald case HCI_POWER_ON: 37028d213e1aSmatthias.ringwald // do nothing 37038d213e1aSmatthias.ringwald break; 37048d213e1aSmatthias.ringwald case HCI_POWER_OFF: 3705c7e0c5f6Smatthias.ringwald // see hci_run 37063a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3707beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 37088d213e1aSmatthias.ringwald break; 37098d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3710b546ac54Smatthias.ringwald // see hci_run 37113a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 371274b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 37138d213e1aSmatthias.ringwald break; 37147bbeb3adSMilanka Ringwald default: 37157bbeb3adSMilanka Ringwald btstack_assert(false); 37167bbeb3adSMilanka Ringwald break; 37178d213e1aSmatthias.ringwald } 371842bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 371942bd3d77SMatthias Ringwald } 37207301ad89Smatthias.ringwald 372142bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 37228d213e1aSmatthias.ringwald switch (power_mode){ 37238d213e1aSmatthias.ringwald case HCI_POWER_ON: 372444935e40S[email protected] hci_power_transition_to_initializing(); 37258d213e1aSmatthias.ringwald break; 37268d213e1aSmatthias.ringwald case HCI_POWER_OFF: 37278d213e1aSmatthias.ringwald // do nothing 37288d213e1aSmatthias.ringwald break; 37298d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3730b546ac54Smatthias.ringwald // see hci_run 37313a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 373274b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 37338d213e1aSmatthias.ringwald break; 37347bbeb3adSMilanka Ringwald default: 37357bbeb3adSMilanka Ringwald btstack_assert(false); 37367bbeb3adSMilanka Ringwald break; 37378d213e1aSmatthias.ringwald } 373800278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 373942bd3d77SMatthias Ringwald } 37408d213e1aSmatthias.ringwald 374142bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 37428d213e1aSmatthias.ringwald switch (power_mode){ 37438d213e1aSmatthias.ringwald case HCI_POWER_ON: 374428171530Smatthias.ringwald 3745423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 374628171530Smatthias.ringwald // nothing to do, if H4 supports power management 3747d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 37483a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 374974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_WRITE_SCAN_ENABLE; // init after sleep 375028171530Smatthias.ringwald break; 375128171530Smatthias.ringwald } 375228171530Smatthias.ringwald #endif 375344935e40S[email protected] hci_power_transition_to_initializing(); 37548d213e1aSmatthias.ringwald break; 37558d213e1aSmatthias.ringwald case HCI_POWER_OFF: 3756b546ac54Smatthias.ringwald // see hci_run 37573a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3758beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 37598d213e1aSmatthias.ringwald break; 37608d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3761b546ac54Smatthias.ringwald // do nothing 37628d213e1aSmatthias.ringwald break; 37637bbeb3adSMilanka Ringwald default: 37647bbeb3adSMilanka Ringwald btstack_assert(false); 37657bbeb3adSMilanka Ringwald break; 37668d213e1aSmatthias.ringwald } 376742bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 376842bd3d77SMatthias Ringwald } 37698d213e1aSmatthias.ringwald 377042bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 377142bd3d77SMatthias Ringwald int err; 37728d213e1aSmatthias.ringwald switch (power_mode){ 37738d213e1aSmatthias.ringwald case HCI_POWER_ON: 3774423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 377528171530Smatthias.ringwald // nothing to do, if H4 supports power management 3776d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 37773a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 37785c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_AFTER_SLEEP; 3779758b46ceSmatthias.ringwald hci_update_scan_enable(); 378028171530Smatthias.ringwald break; 378128171530Smatthias.ringwald } 378228171530Smatthias.ringwald #endif 37833144bce4Smatthias.ringwald err = hci_power_control_wake(); 37843144bce4Smatthias.ringwald if (err) return err; 378544935e40S[email protected] hci_power_transition_to_initializing(); 37868d213e1aSmatthias.ringwald break; 37878d213e1aSmatthias.ringwald case HCI_POWER_OFF: 37883a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 3789beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 37908d213e1aSmatthias.ringwald break; 37918d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 3792b546ac54Smatthias.ringwald // do nothing 37938d213e1aSmatthias.ringwald break; 37947bbeb3adSMilanka Ringwald default: 37957bbeb3adSMilanka Ringwald btstack_assert(false); 37967bbeb3adSMilanka Ringwald break; 37978d213e1aSmatthias.ringwald } 379842bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 379942bd3d77SMatthias Ringwald } 38007bbeb3adSMilanka Ringwald 380142bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 380242bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 380324f87726SMatthias Ringwald int err = 0; 380442bd3d77SMatthias Ringwald switch (hci_stack->state){ 380542bd3d77SMatthias Ringwald case HCI_STATE_OFF: 380642bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 380742bd3d77SMatthias Ringwald break; 380842bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 380942bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 381042bd3d77SMatthias Ringwald break; 381142bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 381242bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 381342bd3d77SMatthias Ringwald break; 381442bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 381542bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 381642bd3d77SMatthias Ringwald break; 381742bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 381842bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 381942bd3d77SMatthias Ringwald break; 382042bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 382142bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 382242bd3d77SMatthias Ringwald break; 38237bbeb3adSMilanka Ringwald default: 38247bbeb3adSMilanka Ringwald btstack_assert(false); 38257bbeb3adSMilanka Ringwald break; 382611e23e5fSmatthias.ringwald } 382724f87726SMatthias Ringwald if (err != 0){ 382842bd3d77SMatthias Ringwald return err; 382942bd3d77SMatthias Ringwald } 383068d92d03Smatthias.ringwald 3831038bc64cSmatthias.ringwald // create internal event 3832ee8bf225Smatthias.ringwald hci_emit_state(); 3833ee8bf225Smatthias.ringwald 383468d92d03Smatthias.ringwald // trigger next/first action 383568d92d03Smatthias.ringwald hci_run(); 383668d92d03Smatthias.ringwald 3837475c8125Smatthias.ringwald return 0; 3838475c8125Smatthias.ringwald } 3839475c8125Smatthias.ringwald 384035454696SMatthias Ringwald 384135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 384235454696SMatthias Ringwald 3843758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 3844758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 3845a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 3846758b46ceSmatthias.ringwald hci_run(); 3847758b46ceSmatthias.ringwald } 3848758b46ceSmatthias.ringwald 384915a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 3850381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 3851381fbed8Smatthias.ringwald 38523a9fb326S[email protected] if (hci_stack->discoverable == enable){ 38533a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 3854381fbed8Smatthias.ringwald return; 3855381fbed8Smatthias.ringwald } 3856381fbed8Smatthias.ringwald 38573a9fb326S[email protected] hci_stack->discoverable = enable; 3858758b46ceSmatthias.ringwald hci_update_scan_enable(); 3859758b46ceSmatthias.ringwald } 3860b031bebbSmatthias.ringwald 386115a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 3862758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 3863758b46ceSmatthias.ringwald 3864758b46ceSmatthias.ringwald // don't emit event 38653a9fb326S[email protected] if (hci_stack->connectable == enable) return; 3866758b46ceSmatthias.ringwald 38673a9fb326S[email protected] hci_stack->connectable = enable; 3868758b46ceSmatthias.ringwald hci_update_scan_enable(); 3869381fbed8Smatthias.ringwald } 387035454696SMatthias Ringwald #endif 3871381fbed8Smatthias.ringwald 387215a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 38736535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 38745061f3afS[email protected] } 38755061f3afS[email protected] 38762b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 38772b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 38782b838201SMatthias Ringwald 38792b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 38802b838201SMatthias Ringwald hci_reserve_packet_buffer(); 38812b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 38822b838201SMatthias Ringwald 38832b838201SMatthias Ringwald uint16_t size = 0; 38842b838201SMatthias Ringwald uint16_t num_handles = 0; 38852b838201SMatthias Ringwald packet[size++] = 0x35; 38862b838201SMatthias Ringwald packet[size++] = 0x0c; 38872b838201SMatthias Ringwald size++; // skip param len 38882b838201SMatthias Ringwald size++; // skip num handles 38892b838201SMatthias Ringwald 38902b838201SMatthias Ringwald // add { handle, packets } entries 38912b838201SMatthias Ringwald btstack_linked_item_t * it; 38922b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 38932b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 38942b838201SMatthias Ringwald if (connection->num_packets_completed){ 38952b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 38962b838201SMatthias Ringwald size += 2; 38972b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 38982b838201SMatthias Ringwald size += 2; 38992b838201SMatthias Ringwald // 39002b838201SMatthias Ringwald num_handles++; 39012b838201SMatthias Ringwald connection->num_packets_completed = 0; 39022b838201SMatthias Ringwald } 39032b838201SMatthias Ringwald } 39042b838201SMatthias Ringwald 39052b838201SMatthias Ringwald packet[2] = size - 3; 39062b838201SMatthias Ringwald packet[3] = num_handles; 39072b838201SMatthias Ringwald 39082b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 39092b838201SMatthias Ringwald 39102b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 39112b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 39122b838201SMatthias Ringwald 39132b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 39142b838201SMatthias Ringwald if (hci_transport_synchronous()){ 3915e2d22487SMatthias Ringwald hci_release_packet_buffer(); 3916068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 39172b838201SMatthias Ringwald } 39182b838201SMatthias Ringwald } 39192b838201SMatthias Ringwald #endif 39202b838201SMatthias Ringwald 392126fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 392226fe9592SMatthias Ringwald UNUSED(ds); 392326fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 3924beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 3925beceeddeSMatthias Ringwald hci_emit_state(); 392626fe9592SMatthias Ringwald hci_run(); 392726fe9592SMatthias Ringwald } 392826fe9592SMatthias Ringwald 3929f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 39304ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3931b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 3932b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 3933b5d8b22bS[email protected] if (connection) { 393428a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 3935b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 3936f30077b7SMatthias Ringwald return true; 3937b5d8b22bS[email protected] } 393828a0332dSMatthias Ringwald } else { 3939b5d8b22bS[email protected] // connection gone -> discard further fragments 394028a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 3941b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 3942b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 3943b5d8b22bS[email protected] } 3944b5d8b22bS[email protected] } 3945f30077b7SMatthias Ringwald return false; 39462b838201SMatthias Ringwald } 3947b031bebbSmatthias.ringwald 394835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3949f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 3950f30077b7SMatthias Ringwald 3951b031bebbSmatthias.ringwald // decline incoming connections 39523a9fb326S[email protected] if (hci_stack->decline_reason){ 39533a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 39543a9fb326S[email protected] hci_stack->decline_reason = 0; 39553a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 3956f30077b7SMatthias Ringwald return true; 3957ce4c8fabSmatthias.ringwald } 3958bea424a5SMatthias Ringwald // write page scan activity 3959bea424a5SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && (hci_stack->new_page_scan_interval != 0xffff) && hci_classic_supported()){ 3960bea424a5SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 3961bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = 0xffff; 3962bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = 0xffff; 3963bea424a5SMatthias Ringwald return true; 3964bea424a5SMatthias Ringwald } 3965bea424a5SMatthias Ringwald // write page scan type 3966bea424a5SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && (hci_stack->new_page_scan_type != 0xff) && hci_classic_supported()){ 3967bea424a5SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 3968bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = 0xff; 3969bea424a5SMatthias Ringwald return true; 3970bea424a5SMatthias Ringwald } 3971b031bebbSmatthias.ringwald // send scan enable 39720e588213SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && (hci_stack->new_scan_enable_value != 0xff) && hci_classic_supported()){ 39733a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 39743a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 3975f30077b7SMatthias Ringwald return true; 3976b031bebbSmatthias.ringwald } 3977f5875de5SMatthias Ringwald // start/stop inquiry 3978a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 3979f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 3980beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 3981496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 3982f30077b7SMatthias Ringwald return true; 3983f5875de5SMatthias Ringwald } 3984f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 3985f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 3986f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 3987f30077b7SMatthias Ringwald return true; 3988f5875de5SMatthias Ringwald } 3989b7f1ee76SMatthias Ringwald // remote name request 3990b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 3991b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 3992b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 3993ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 3994f30077b7SMatthias Ringwald return true; 3995b7f1ee76SMatthias Ringwald } 3996cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3997cf01e888SMatthias Ringwald // Local OOB data 3998cf01e888SMatthias Ringwald if ((hci_stack->state == HCI_STATE_WORKING) && hci_stack->classic_read_local_oob_data){ 3999cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 4000cf01e888SMatthias Ringwald if (hci_stack->local_supported_commands[1] & 0x10u){ 4001cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 4002cf01e888SMatthias Ringwald } else { 4003cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 4004cf01e888SMatthias Ringwald } 4005cf01e888SMatthias Ringwald } 4006cf01e888SMatthias Ringwald #endif 40070a51f88bSMatthias Ringwald // pairing 40080a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 40090a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 40100a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 40113f659ee4SMilanka Ringwald uint8_t pin_code[16]; 40120a51f88bSMatthias Ringwald switch (state){ 40130a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 40143f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 40153f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 40163f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 40170a51f88bSMatthias Ringwald break; 40180a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 40190a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 40200a51f88bSMatthias Ringwald break; 40210a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 4022d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 40230a51f88bSMatthias Ringwald break; 40240a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 40250a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 40260a51f88bSMatthias Ringwald break; 40270a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 40280a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 40290a51f88bSMatthias Ringwald break; 40300a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 40310a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 40320a51f88bSMatthias Ringwald break; 40330a51f88bSMatthias Ringwald default: 40340a51f88bSMatthias Ringwald break; 40350a51f88bSMatthias Ringwald } 4036f30077b7SMatthias Ringwald return true; 4037f30077b7SMatthias Ringwald } 4038f30077b7SMatthias Ringwald return false; 40390a51f88bSMatthias Ringwald } 404035454696SMatthias Ringwald #endif 4041b031bebbSmatthias.ringwald 4042a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4043f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 4044f30077b7SMatthias Ringwald 4045f30077b7SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 4046f30077b7SMatthias Ringwald 4047f30077b7SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 40484ea43905SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 4049d70217a2SMatthias Ringwald 4050a41310b7SMatthias Ringwald 4051a41310b7SMatthias Ringwald // Phase 1: collect what to stop 4052a41310b7SMatthias Ringwald 4053a41310b7SMatthias Ringwald bool scanning_stop = false; 4054a41310b7SMatthias Ringwald bool connecting_stop = false; 4055a41310b7SMatthias Ringwald bool advertising_stop = false; 4056a41310b7SMatthias Ringwald 4057a41310b7SMatthias Ringwald #ifndef ENABLE_LE_CENTRAL 4058a41310b7SMatthias Ringwald UNUSED(scanning_stop); 40590abd9f64SMatthias Ringwald UNUSED(connecting_stop); 4060a41310b7SMatthias Ringwald #endif 4061a41310b7SMatthias Ringwald #ifndef ENABLE_LE_PERIPHERAL 4062a41310b7SMatthias Ringwald UNUSED(advertising_stop); 4063a41310b7SMatthias Ringwald #endif 4064a41310b7SMatthias Ringwald 406529c24bebSMatthias Ringwald // check if whitelist needs modification 406629c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 406729c24bebSMatthias Ringwald btstack_linked_list_iterator_t lit; 406829c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 406929c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 407029c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 407129c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 407229c24bebSMatthias Ringwald whitelist_modification_pending = true; 407329c24bebSMatthias Ringwald break; 407429c24bebSMatthias Ringwald } 407529c24bebSMatthias Ringwald } 407621debf25SMatthias Ringwald // check if resolving list needs modification 407721debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 407821debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 4079ea151974SMatthias Ringwald bool resolving_list_supported = (hci_stack->local_supported_commands[1] & (1 << 2)) != 0; 4080ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 408121debf25SMatthias Ringwald resolving_list_modification_pending = true; 408221debf25SMatthias Ringwald } 408321debf25SMatthias Ringwald #endif 408429c24bebSMatthias Ringwald 4085d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4086fde725feSMatthias Ringwald // scanning control 40873251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 408829c24bebSMatthias Ringwald // stop if: 408929c24bebSMatthias Ringwald // - parameter change required 409029c24bebSMatthias Ringwald // - it's disabled 409129c24bebSMatthias Ringwald // - whitelist change required but used for scanning 409221debf25SMatthias Ringwald // - resolving list modified 409329c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 409421debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 409521debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 409621debf25SMatthias Ringwald scanning_uses_whitelist || 409721debf25SMatthias Ringwald resolving_list_modification_pending){ 409821debf25SMatthias Ringwald 40992d5c2a27SMatthias Ringwald scanning_stop = true; 4100fde725feSMatthias Ringwald } 4101fde725feSMatthias Ringwald } 410229c24bebSMatthias Ringwald #endif 4103fde725feSMatthias Ringwald 410429c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 410529c24bebSMatthias Ringwald // connecting control 4106f496d06eSMatthias Ringwald bool connecting_with_whitelist; 4107f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 4108f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 4109f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 4110af64f147SMatthias Ringwald // stop connecting if: 4111af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 4112af64f147SMatthias Ringwald // - if it got disabled 411321debf25SMatthias Ringwald // - resolving list modified 4114f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 4115f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 411621debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 411721debf25SMatthias Ringwald resolving_list_modification_pending) { 411821debf25SMatthias Ringwald 411929c24bebSMatthias Ringwald connecting_stop = true; 412029c24bebSMatthias Ringwald } 4121f496d06eSMatthias Ringwald break; 4122f496d06eSMatthias Ringwald default: 4123f496d06eSMatthias Ringwald break; 412429c24bebSMatthias Ringwald } 4125d70217a2SMatthias Ringwald #endif 412629c24bebSMatthias Ringwald 4127d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 412845c102fdSMatthias Ringwald // le advertisement control 4129bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_active){ 413029c24bebSMatthias Ringwald // stop if: 413129c24bebSMatthias Ringwald // - parameter change required 413229c24bebSMatthias Ringwald // - it's disabled 4133ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 413421debf25SMatthias Ringwald // - resolving list modified 4135a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 41364e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 4137a61834b6SMatthias Ringwald if (advertising_change || 4138a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 413921debf25SMatthias Ringwald (advertising_uses_whitelist & whitelist_modification_pending) || 414021debf25SMatthias Ringwald resolving_list_modification_pending) { 414121debf25SMatthias Ringwald 4142fde725feSMatthias Ringwald advertising_stop = true; 4143fde725feSMatthias Ringwald } 4144fde725feSMatthias Ringwald } 4145a41310b7SMatthias Ringwald #endif 4146fde725feSMatthias Ringwald 4147a41310b7SMatthias Ringwald 4148a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 4149a41310b7SMatthias Ringwald 4150a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4151a41310b7SMatthias Ringwald if (scanning_stop){ 41525226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 4153a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 4154a41310b7SMatthias Ringwald return true; 4155a41310b7SMatthias Ringwald } 4156a41310b7SMatthias Ringwald #endif 4157a41310b7SMatthias Ringwald 4158a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4159a41310b7SMatthias Ringwald if (connecting_stop){ 4160a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 4161a41310b7SMatthias Ringwald return true; 4162a41310b7SMatthias Ringwald } 4163a41310b7SMatthias Ringwald #endif 4164a41310b7SMatthias Ringwald 4165a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4166fde725feSMatthias Ringwald if (advertising_stop){ 41675226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = false; 416845c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 4169f30077b7SMatthias Ringwald return true; 417045c102fdSMatthias Ringwald } 4171a41310b7SMatthias Ringwald #endif 4172fde725feSMatthias Ringwald 4173a41310b7SMatthias Ringwald // Phase 3: modify 4174a41310b7SMatthias Ringwald 4175a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4176a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 4177a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 4178a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 4179a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 4180a41310b7SMatthias Ringwald return true; 4181a41310b7SMatthias Ringwald } 4182a41310b7SMatthias Ringwald #endif 4183a41310b7SMatthias Ringwald 4184a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 418545c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 418645c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 41876bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 418845c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 418945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 419045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 419145c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 41926bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 419345c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 419445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 419545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 419645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 4197f30077b7SMatthias Ringwald return true; 419845c102fdSMatthias Ringwald } 4199501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 4200501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 42017a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 42027a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 42036535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 42046535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 42053c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 42067a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 4207f30077b7SMatthias Ringwald return true; 420845c102fdSMatthias Ringwald } 4209501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 4210501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 4211f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 4212f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 42136535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 42146535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 42153c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 4216214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 4217f30077b7SMatthias Ringwald return true; 4218501f56b3SMatthias Ringwald } 4219d70217a2SMatthias Ringwald #endif 42209956955bSMatthias Ringwald 422113eb2a2eSMatthias Ringwald 4222057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4223a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 4224a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 4225057ab60cSMatthias Ringwald #endif 4226057ab60cSMatthias Ringwald 4227a41310b7SMatthias Ringwald // LE Whitelist Management 4228a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 42299956955bSMatthias Ringwald // add/remove entries 4230665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4231665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4232665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4233453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 4234453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4235453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 4236453459ddSMatthias Ringwald return true; 4237453459ddSMatthias Ringwald } 42389956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 4239453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 4240453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 42419956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 4242f30077b7SMatthias Ringwald return true; 42439956955bSMatthias Ringwald } 4244453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 4245665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 42469956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 42479956955bSMatthias Ringwald } 42489956955bSMatthias Ringwald } 424991915b0bSMatthias Ringwald } 42509956955bSMatthias Ringwald 425121debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 425221debf25SMatthias Ringwald // LE Resolving List Management 4253ea151974SMatthias Ringwald if (resolving_list_supported) { 425421debf25SMatthias Ringwald uint16_t i; 425521debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 425621debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 425721debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 425821debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 425921debf25SMatthias Ringwald return true; 426021debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 426121debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 426221debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 426321debf25SMatthias Ringwald return true; 426421debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 426502b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 4266ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 4267ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 4268ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 4269ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 427021debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 427121debf25SMatthias Ringwald return true; 427202b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 427302b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 427402b02cffSMatthias Ringwald uint8_t offset = i >> 3; 427502b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 427602b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 427702b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 427802b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 427902b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 428002b02cffSMatthias Ringwald sm_key_t peer_irk; 428102b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 428202b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 4283f5228c62SMatthias Ringwald 4284f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 4285f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 4286f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4287f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 4288f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 4289f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 4290f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 4291f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 4292f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 4293f5228c62SMatthias Ringwald } 4294f5228c62SMatthias Ringwald #endif 4295f5228c62SMatthias Ringwald 4296ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 4297ea151974SMatthias Ringwald peer_identity_addreses); 429802b02cffSMatthias Ringwald return true; 429902b02cffSMatthias Ringwald } 430002b02cffSMatthias Ringwald 430102b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 430202b02cffSMatthias Ringwald 430302b02cffSMatthias Ringwald /* fall through */ 430402b02cffSMatthias Ringwald 430521debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 430621debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 430721debf25SMatthias Ringwald uint8_t offset = i >> 3; 430821debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 430902b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 431002b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 431121debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 431221debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 431321debf25SMatthias Ringwald sm_key_t peer_irk; 431421debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 431521debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 431621debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 431721debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 431821debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 431921debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 432021debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 432121debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 4322ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 4323ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 432421debf25SMatthias Ringwald return true; 432521debf25SMatthias Ringwald } 432602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 432721debf25SMatthias Ringwald break; 432802b02cffSMatthias Ringwald 432921debf25SMatthias Ringwald default: 433021debf25SMatthias Ringwald break; 433121debf25SMatthias Ringwald } 4332ea151974SMatthias Ringwald } 433321debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 433421debf25SMatthias Ringwald #endif 4335a41310b7SMatthias Ringwald 4336a41310b7SMatthias Ringwald // Phase 4: restore state 433729c24bebSMatthias Ringwald 433829c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4339af64f147SMatthias Ringwald // re-start scanning 434029c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 434129c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 434229c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 434329c24bebSMatthias Ringwald return true; 434429c24bebSMatthias Ringwald } 434529c24bebSMatthias Ringwald #endif 434629c24bebSMatthias Ringwald 434713eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4348af64f147SMatthias Ringwald // re-start connecting 4349af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 43509956955bSMatthias Ringwald bd_addr_t null_addr; 43519956955bSMatthias Ringwald memset(null_addr, 0, 6); 43526bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 43536bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 43549956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 4355cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 4356cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 43579956955bSMatthias Ringwald 1, // use whitelist 43589956955bSMatthias Ringwald 0, // peer address type 43599956955bSMatthias Ringwald null_addr, // peer bd addr 43606bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 436173044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 436273044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 436373044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 436473044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 436573044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 436673044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 43679956955bSMatthias Ringwald ); 4368f30077b7SMatthias Ringwald return true; 43699956955bSMatthias Ringwald } 4370d70217a2SMatthias Ringwald #endif 4371a41310b7SMatthias Ringwald 4372a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4373a41310b7SMatthias Ringwald // re-start advertising 43748978dcf1SMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && !hci_stack->le_advertisements_active){ 4375a41310b7SMatthias Ringwald // check if advertisements should be enabled given 43765226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = true; 43776bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_advertisements_own_address); 4378a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 4379a41310b7SMatthias Ringwald return true; 4380a41310b7SMatthias Ringwald } 4381a41310b7SMatthias Ringwald #endif 4382a41310b7SMatthias Ringwald 4383f30077b7SMatthias Ringwald return false; 43847bdc6798S[email protected] } 4385b2f949feS[email protected] #endif 43867bdc6798S[email protected] 438788a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 4388f30077b7SMatthias Ringwald btstack_linked_item_t * it; 4389a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 439005ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 439132ab9390Smatthias.ringwald 43920bf6344aS[email protected] switch(connection->state){ 43930bf6344aS[email protected] case SEND_CREATE_CONNECTION: 43944f3229d8S[email protected] switch(connection->address_type){ 439535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4396f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 43979da54300S[email protected] log_info("sending hci_create_connection"); 4398b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 43994f3229d8S[email protected] break; 440035454696SMatthias Ringwald #endif 44014f3229d8S[email protected] default: 4402a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4403d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 44049da54300S[email protected] log_info("sending hci_le_create_connection"); 44056bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 44066bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 44074f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 4408cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 4409cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 44104f3229d8S[email protected] 0, // don't use whitelist 44114f3229d8S[email protected] connection->address_type, // peer address type 44124f3229d8S[email protected] connection->address, // peer bd addr 44136bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 441473044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 441573044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 441673044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 441773044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 441873044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 441973044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 44204f3229d8S[email protected] ); 44214f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 4422b2f949feS[email protected] #endif 4423d70217a2SMatthias Ringwald #endif 44244f3229d8S[email protected] break; 44254f3229d8S[email protected] } 4426f30077b7SMatthias Ringwald return true; 4427ad83dc6aS[email protected] 442835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 44290bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 44305cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 4431f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 443276ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 4433895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 4434c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 4435e35edcc1S[email protected] } 4436f30077b7SMatthias Ringwald return true; 443735454696SMatthias Ringwald #endif 44380bf6344aS[email protected] 4439a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4440d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 44410bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 44420bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 44430bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 4444f30077b7SMatthias Ringwald return true; 4445a6725849S[email protected] #endif 4446d70217a2SMatthias Ringwald #endif 44470bf6344aS[email protected] case SEND_DISCONNECT: 44480bf6344aS[email protected] connection->state = SENT_DISCONNECT; 44496ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4450f30077b7SMatthias Ringwald return true; 44510bf6344aS[email protected] 44520bf6344aS[email protected] default: 44530bf6344aS[email protected] break; 4454c7e0c5f6Smatthias.ringwald } 4455c7e0c5f6Smatthias.ringwald 4456cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 4457cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 4458cabf004eSMatthias Ringwald 4459891b9fc2SMatthias Ringwald if (connection->authentication_flags & READ_RSSI){ 4460891b9fc2SMatthias Ringwald connectionClearAuthenticationFlags(connection, READ_RSSI); 4461891b9fc2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 4462f30077b7SMatthias Ringwald return true; 4463891b9fc2SMatthias Ringwald } 4464891b9fc2SMatthias Ringwald 446594418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 446694418890SMatthias Ringwald 44676909f064SMatthias Ringwald if (connection->authentication_flags & WRITE_SUPERVISION_TIMEOUT){ 44686909f064SMatthias Ringwald connectionClearAuthenticationFlags(connection, WRITE_SUPERVISION_TIMEOUT); 44696909f064SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 4470f30077b7SMatthias Ringwald return true; 44716909f064SMatthias Ringwald } 44726909f064SMatthias Ringwald 4473ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 4474ad20f0c8SMatthias Ringwald if ( ((connection->authentication_flags & HANDLE_LINK_KEY_REQUEST) != 0) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 4475608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 447634d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 447730e72d78SMatthias Ringwald 4478e9f98c4aSMatthias Ringwald // lookup link key using cached key first 4479e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 4480e9f98c4aSMatthias Ringwald if (!have_link_key && (hci_stack->link_key_db != NULL)){ 4481e9f98c4aSMatthias Ringwald have_link_key = hci_stack->link_key_db->get_link_key(connection->address, connection->link_key, &connection->link_key_type); 4482e9f98c4aSMatthias Ringwald } 448330e72d78SMatthias Ringwald 448430e72d78SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 448530e72d78SMatthias Ringwald bool sc_enabled_remote = (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 4486e9f98c4aSMatthias Ringwald bool sc_downgrade = have_link_key && (gap_secure_connection_for_link_key_type(connection->link_key_type) == 1) && !sc_enabled_remote; 448730e72d78SMatthias Ringwald if (sc_downgrade){ 448830e72d78SMatthias Ringwald log_info("Link key based on SC, but remote does not support SC -> disconnect"); 448930e72d78SMatthias Ringwald connection->state = SENT_DISCONNECT; 449030e72d78SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 449130e72d78SMatthias Ringwald return true; 449230e72d78SMatthias Ringwald } 449330e72d78SMatthias Ringwald 4494e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 449530e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 4496e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 449732ab9390Smatthias.ringwald } else { 449832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 449932ab9390Smatthias.ringwald } 4500f30077b7SMatthias Ringwald return true; 450132ab9390Smatthias.ringwald } 45021d6b20aeS[email protected] 4503899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 45049da54300S[email protected] log_info("denying to pin request"); 4505899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 450634d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 4507f30077b7SMatthias Ringwald return true; 45084c57c146S[email protected] } 45094c57c146S[email protected] 4510dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 451134d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 4512a8d20135SMatthias Ringwald // set authentication requirements: 4513a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 4514532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 4515a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 45169faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 45179faad3abS[email protected] authreq |= 1; 4518106d6d11S[email protected] } 4519532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 4520532454f9SMatthias Ringwald if (connection->authentication_flags & RECV_IO_CAPABILITIES_RESPONSE){ 4521532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 4522532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 4523532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 4524532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 4525532454f9SMatthias Ringwald bonding = false; 4526532454f9SMatthias Ringwald } 4527532454f9SMatthias Ringwald } 4528532454f9SMatthias Ringwald if (bonding){ 4529a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 4530a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 4531532454f9SMatthias Ringwald } else { 4532a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 4533a8d20135SMatthias Ringwald } 4534532454f9SMatthias Ringwald } 45351849becdSMatthias Ringwald uint8_t have_oob_data = 0; 45361849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 45371849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 45381849becdSMatthias Ringwald have_oob_data |= 1; 45391849becdSMatthias Ringwald } 45401849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 45411849becdSMatthias Ringwald have_oob_data |= 2; 45421849becdSMatthias Ringwald } 45431849becdSMatthias Ringwald #endif 45441849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 454511b03efaSMatthias Ringwald return true; 4546f8fb5f6eS[email protected] } 454711b03efaSMatthias Ringwald 454811b03efaSMatthias Ringwald if (connection->authentication_flags & SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 454911b03efaSMatthias Ringwald connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 455011b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 4551f30077b7SMatthias Ringwald return true; 455232ab9390Smatthias.ringwald } 455332ab9390Smatthias.ringwald 45541849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 45551849becdSMatthias Ringwald if (connection->authentication_flags & SEND_REMOTE_OOB_DATA_REPLY){ 45561849becdSMatthias Ringwald connectionClearAuthenticationFlags(connection, SEND_REMOTE_OOB_DATA_REPLY); 45571849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 45581849becdSMatthias Ringwald const uint8_t * r_192 = zero; 45591849becdSMatthias Ringwald const uint8_t * c_192 = zero; 45601849becdSMatthias Ringwald const uint8_t * r_256 = zero; 45611849becdSMatthias Ringwald const uint8_t * c_256 = zero; 45621849becdSMatthias Ringwald // verify P-256 OOB 45631849becdSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && ((hci_stack->local_supported_commands[1] & 0x08u) != 0)) { 45641849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 45651849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 45661849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 45671849becdSMatthias Ringwald } 45681849becdSMatthias Ringwald } 45691849becdSMatthias Ringwald // verify P-192 OOB 45701849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 45711849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 45721849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 45731849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 45741849becdSMatthias Ringwald } 45751849becdSMatthias Ringwald } 45761849becdSMatthias Ringwald // Reply 45771849becdSMatthias Ringwald if (c_256 != zero) { 45781849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 45791849becdSMatthias Ringwald } else if (c_192 != zero){ 45801849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 45811849becdSMatthias Ringwald } else { 45821849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 45831849becdSMatthias Ringwald } 45841849becdSMatthias Ringwald return true; 45851849becdSMatthias Ringwald } 45861849becdSMatthias Ringwald #endif 45871849becdSMatthias Ringwald 4588dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 4589dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 459034d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 4591f30077b7SMatthias Ringwald return true; 4592dbe1a790S[email protected] } 4593dbe1a790S[email protected] 4594dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 4595dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 459634d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 4597f30077b7SMatthias Ringwald return true; 4598dbe1a790S[email protected] } 4599afd4e962S[email protected] 4600ba018746SMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 4601ba018746SMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 460234d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 4603f30077b7SMatthias Ringwald return true; 46042bd8b7e7S[email protected] } 46052bd8b7e7S[email protected] 46065ccef624SMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 46075ccef624SMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 46085ccef624SMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 46095ccef624SMatthias Ringwald return true; 46105ccef624SMatthias Ringwald } 46115ccef624SMatthias Ringwald 461282721f83SMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 461382721f83SMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 461482721f83SMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 461582721f83SMatthias Ringwald return true; 461682721f83SMatthias Ringwald } 461782721f83SMatthias Ringwald 4618ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 4619ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 46201bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 46216ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 46226ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4623f30077b7SMatthias Ringwald return true; 4624ad83dc6aS[email protected] } 462576f27cffSMatthias Ringwald 462634d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 462734d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 4628abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 462934d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 4630f30077b7SMatthias Ringwald return true; 4631afd4e962S[email protected] } 463276f27cffSMatthias Ringwald 4633dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 4634dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 4635dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 4636f30077b7SMatthias Ringwald return true; 4637dce78009S[email protected] } 4638573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 4639573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 4640573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 4641f30077b7SMatthias Ringwald return true; 4642573897a0SMatthias Ringwald } 464376f27cffSMatthias Ringwald #endif 464476f27cffSMatthias Ringwald 464576f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 464676f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 46471714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 46481714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 46491714cbbdSMatthias Ringwald #endif 46506ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 46516ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 46526ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 4653f30077b7SMatthias Ringwald return true; 465476f27cffSMatthias Ringwald } 46556ef3696aSMatthias Ringwald } 4656da886c03S[email protected] 46576cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 4658f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 4659f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 4660f8ee3071SMatthias Ringwald case 0: 4661f8ee3071SMatthias Ringwald break; 4662f8ee3071SMatthias Ringwald case 0xffff: 4663f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 4664f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 4665f30077b7SMatthias Ringwald return true; 4666f8ee3071SMatthias Ringwald default: 4667f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 4668f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 4669f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 4670f30077b7SMatthias Ringwald return true; 4671f8ee3071SMatthias Ringwald } 467288a03c8dSMatthias Ringwald 4673140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 4674140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 4675140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 4676140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 4677140c0557SMatthias Ringwald return true; 4678140c0557SMatthias Ringwald } 4679140c0557SMatthias Ringwald 4680965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 4681278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 4682965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 4683278ff8a9SMatthias Ringwald hci_send_cmd(&hci_qos_setup, connection->con_handle, 0, service_type, connection->qos_token_rate, connection->qos_peak_bandwidth, connection->qos_latency, connection->qos_delay_variation); 4684278ff8a9SMatthias Ringwald return true; 4685278ff8a9SMatthias Ringwald } 4686278ff8a9SMatthias Ringwald 468788a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 468888a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 468988a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 469088a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 469188a03c8dSMatthias Ringwald return true; 469288a03c8dSMatthias Ringwald } 46936cdc2862SMatthias Ringwald #endif 4694f8ee3071SMatthias Ringwald 4695a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 469673cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 469773cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 469873cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 4699da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 470073cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 4701c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 4702c37a3166S[email protected] 0x0000, 0xffff); 4703f30077b7SMatthias Ringwald return true; 470473cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 470573cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 470673cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 470773cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 470873cd8a2aSMatthias Ringwald 0x0000, 0xffff); 4709f30077b7SMatthias Ringwald return true; 471073cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 471173cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 471273cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE); 4713f30077b7SMatthias Ringwald return true; 471473cd8a2aSMatthias Ringwald default: 471573cd8a2aSMatthias Ringwald break; 4716c37a3166S[email protected] } 47174ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 4718b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 4719b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 4720b90f6e0aSMatthias 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); 4721f30077b7SMatthias Ringwald return true; 4722b90f6e0aSMatthias Ringwald } 4723c37a3166S[email protected] #endif 4724dbe1a790S[email protected] } 4725f30077b7SMatthias Ringwald return false; 4726f30077b7SMatthias Ringwald } 4727c7e0c5f6Smatthias.ringwald 4728f30077b7SMatthias Ringwald static void hci_run(void){ 4729f30077b7SMatthias Ringwald 4730f30077b7SMatthias Ringwald bool done; 4731f30077b7SMatthias Ringwald 4732f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 4733f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 4734f30077b7SMatthias Ringwald if (done) return; 4735f30077b7SMatthias Ringwald 4736f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 4737f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 4738f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 4739f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 4740f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 4741f30077b7SMatthias Ringwald return; 4742f30077b7SMatthias Ringwald } 4743f30077b7SMatthias Ringwald #endif 4744f30077b7SMatthias Ringwald 4745f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4746f30077b7SMatthias Ringwald 4747f30077b7SMatthias Ringwald // global/non-connection oriented commands 4748f30077b7SMatthias Ringwald 4749f30077b7SMatthias Ringwald 4750f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 4751f30077b7SMatthias Ringwald // general gap classic 4752f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 4753f30077b7SMatthias Ringwald if (done) return; 4754f30077b7SMatthias Ringwald #endif 4755f30077b7SMatthias Ringwald 4756f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 4757f30077b7SMatthias Ringwald // general gap le 4758f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 4759f30077b7SMatthias Ringwald if (done) return; 4760f30077b7SMatthias Ringwald #endif 4761f30077b7SMatthias Ringwald 4762f30077b7SMatthias Ringwald // send pending HCI commands 476388a03c8dSMatthias Ringwald done = hci_run_general_pending_commands(); 4764f30077b7SMatthias Ringwald if (done) return; 4765f30077b7SMatthias Ringwald 4766f30077b7SMatthias Ringwald // stack state sub statemachines 476705ae8de3SMatthias Ringwald hci_connection_t * connection; 47683a9fb326S[email protected] switch (hci_stack->state){ 47693429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 477074b323a9SMatthias Ringwald hci_initializing_run(); 47713429f56bSmatthias.ringwald break; 4772c7e0c5f6Smatthias.ringwald 4773c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 4774c7e0c5f6Smatthias.ringwald 477526fe9592SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 477626fe9592SMatthias Ringwald switch (hci_stack->substate){ 4777beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 4778beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_TIMER: 4779beceeddeSMatthias Ringwald 4780a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4781d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4782beceeddeSMatthias Ringwald // free whitelist entries 47839956955bSMatthias Ringwald { 4784665d90f2SMatthias Ringwald btstack_linked_list_iterator_t lit; 4785665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4786665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4787665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4788665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 47899956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 47909956955bSMatthias Ringwald } 47919956955bSMatthias Ringwald } 47929956955bSMatthias Ringwald #endif 4793d70217a2SMatthias Ringwald #endif 4794c7e0c5f6Smatthias.ringwald // close all open connections 47953a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 4796c7e0c5f6Smatthias.ringwald if (connection){ 4797711e6c80SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 4798d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 479932ab9390Smatthias.ringwald 48008fca890eSMatthias Ringwald // check state 48018fca890eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 48028fca890eSMatthias Ringwald connection->state = SENT_DISCONNECT; 48038fca890eSMatthias Ringwald 48048837e9efSMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 4805c7e0c5f6Smatthias.ringwald 48068837e9efSMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 48078837e9efSMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 48088837e9efSMatthias Ringwald 48098837e9efSMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 4810c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 48118837e9efSMatthias Ringwald 48128837e9efSMatthias Ringwald // finally, send the disconnect command 48136ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4814c7e0c5f6Smatthias.ringwald return; 4815c7e0c5f6Smatthias.ringwald } 481626fe9592SMatthias Ringwald 4817beceeddeSMatthias Ringwald if (hci_stack->substate == HCI_HALTING_DISCONNECT_ALL_TIMER){ 4818beceeddeSMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 4819beceeddeSMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 482026fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_TIMER; 4821beceeddeSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 482226fe9592SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 482326fe9592SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 482426fe9592SMatthias Ringwald break; 4825beceeddeSMatthias Ringwald } 482626fe9592SMatthias Ringwald 4827cf373d3aSMatthias Ringwald /* fall through */ 482826fe9592SMatthias Ringwald 482926fe9592SMatthias Ringwald case HCI_HALTING_CLOSE: 48309da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 4831c7e0c5f6Smatthias.ringwald 483272ea5239Smatthias.ringwald // switch mode 4833c7e0c5f6Smatthias.ringwald hci_power_control_off(); 48349418f9c9Smatthias.ringwald 48359da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 483672ea5239Smatthias.ringwald hci_emit_state(); 48379da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 483872ea5239Smatthias.ringwald break; 4839beceeddeSMatthias Ringwald 4840beceeddeSMatthias Ringwald case HCI_HALTING_W4_TIMER: 4841beceeddeSMatthias Ringwald // keep waiting 4842beceeddeSMatthias Ringwald 4843beceeddeSMatthias Ringwald break; 484426fe9592SMatthias Ringwald default: 484526fe9592SMatthias Ringwald break; 484626fe9592SMatthias Ringwald } 484726fe9592SMatthias Ringwald 484826fe9592SMatthias Ringwald break; 4849c7e0c5f6Smatthias.ringwald 485072ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 48513a9fb326S[email protected] switch(hci_stack->substate) { 485274b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 48539da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 485472ea5239Smatthias.ringwald // close all open connections 48553a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 485666da7044Smatthias.ringwald 4857423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 485866da7044Smatthias.ringwald // don't close connections, if H4 supports power management 4859d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 486066da7044Smatthias.ringwald connection = NULL; 486166da7044Smatthias.ringwald } 486266da7044Smatthias.ringwald #endif 486372ea5239Smatthias.ringwald if (connection){ 486432ab9390Smatthias.ringwald 486572ea5239Smatthias.ringwald // send disconnect 4866d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 486732ab9390Smatthias.ringwald 48689da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 48696ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 487072ea5239Smatthias.ringwald 487172ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 487272ea5239Smatthias.ringwald hci_shutdown_connection(connection); 487372ea5239Smatthias.ringwald return; 487472ea5239Smatthias.ringwald } 487572ea5239Smatthias.ringwald 487692368cd3S[email protected] if (hci_classic_supported()){ 487789db417bSmatthias.ringwald // disable page and inquiry scan 4878d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 487932ab9390Smatthias.ringwald 48809da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 48813a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 488289db417bSmatthias.ringwald 488389db417bSmatthias.ringwald // continue in next sub state 488474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 488589db417bSmatthias.ringwald break; 488692368cd3S[email protected] } 4887cf373d3aSMatthias Ringwald 4888cf373d3aSMatthias Ringwald /* fall through */ 488992368cd3S[email protected] 489074b323a9SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 48919da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 4892423a3e49SMatthias Ringwald #ifdef HAVE_PLATFORM_IPHONE_OS 489328171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 4894d0b87befSMatthias Ringwald if (btstack_control_iphone_power_management_enabled()){ 489528171530Smatthias.ringwald // SLEEP MODE reached 48963a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 489728171530Smatthias.ringwald hci_emit_state(); 489828171530Smatthias.ringwald break; 489928171530Smatthias.ringwald } 490028171530Smatthias.ringwald #endif 490172ea5239Smatthias.ringwald // switch mode 49023a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 4903c7e0c5f6Smatthias.ringwald hci_emit_state(); 490428171530Smatthias.ringwald break; 490528171530Smatthias.ringwald 490689db417bSmatthias.ringwald default: 490789db417bSmatthias.ringwald break; 490889db417bSmatthias.ringwald } 4909c7e0c5f6Smatthias.ringwald break; 4910c7e0c5f6Smatthias.ringwald 49113429f56bSmatthias.ringwald default: 49123429f56bSmatthias.ringwald break; 49131f504dbdSmatthias.ringwald } 49143429f56bSmatthias.ringwald } 491516833f0aSmatthias.ringwald 491631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 491735454696SMatthias Ringwald // house-keeping 491835454696SMatthias Ringwald 491935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4920c8e4258aSmatthias.ringwald bd_addr_t addr; 4921c8e4258aSmatthias.ringwald hci_connection_t * conn; 4922c123d999SMatthias Ringwald #endif 4923c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 49246f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 49259cbd2215SMatthias Ringwald #endif 4926c8e4258aSmatthias.ringwald 49276f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 49286f35bb46SMatthias Ringwald switch (opcode) { 49299cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 49309cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 49319cbd2215SMatthias Ringwald break; 49329cbd2215SMatthias Ringwald 49339cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 49346f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 4935724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 49369da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 4937c8e4258aSmatthias.ringwald 4938f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 4939ad83dc6aS[email protected] if (!conn) { 4940f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 494117f1ba2aSmatthias.ringwald if (!conn) { 494217f1ba2aSmatthias.ringwald // notify client that alloc failed 49432deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 4944f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 494517f1ba2aSmatthias.ringwald } 4946ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 4947f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 4948ad83dc6aS[email protected] } 4949ad83dc6aS[email protected] log_info("conn state %u", conn->state); 4950ad83dc6aS[email protected] switch (conn->state) { 4951ad83dc6aS[email protected] // if connection active exists 4952ad83dc6aS[email protected] case OPEN: 4953f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 4954274dad91SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, 0); 4955f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 4956672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 4957672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 4958672e36abSMatthias Ringwald break; 4959ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 4960532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 4961532f91a5SMatthias Ringwald break; 4962ad83dc6aS[email protected] default: 4963ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 4964f5e5741dSMatthias Ringwald return -1; // packet not sent to controller 4965ad83dc6aS[email protected] } 4966c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 4967229331c6SMatthias Ringwald 4968229331c6SMatthias Ringwald // track outgoing connection 4969f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 49706535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 49716f35bb46SMatthias Ringwald break; 49726f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LINK_KEY_REQUEST_REPLY: 49737fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 49746f35bb46SMatthias Ringwald break; 49756f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LINK_KEY_REQUEST_NEGATIVE_REPLY: 49767fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 49776f35bb46SMatthias Ringwald break; 49786f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_DELETE_STORED_LINK_KEY: 4979a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 4980724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 4981a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 49828ef73945Smatthias.ringwald } 49836f35bb46SMatthias Ringwald break; 4984ee752bb8SMatthias Ringwald 49855b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 49866f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 4987ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 4988ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 4989ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 49906f35bb46SMatthias Ringwald break; 49916f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 4992ee752bb8SMatthias Ringwald // accept_synchronus_connection? Voice setting at offset 18 4993ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 4994ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 49956f35bb46SMatthias Ringwald break; 4996ee752bb8SMatthias Ringwald #endif 499735454696SMatthias Ringwald #endif 49984b3e1e19SMatthias Ringwald 4999a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 50006f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_RANDOM_ADDRESS: 5001b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 1; 5002b95a5a35SMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->le_random_address); 50036f35bb46SMatthias Ringwald break; 50047e8d8b1dSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 50056f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_ADVERTISE_ENABLE: 50065226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = packet[3] != 0; 50076f35bb46SMatthias Ringwald break; 5008d70217a2SMatthias Ringwald #endif 5009d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 50106f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 5011b04dfa37SMatthias Ringwald // white list used? 50126f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 5013b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 5014b04dfa37SMatthias Ringwald case 0: 5015b04dfa37SMatthias Ringwald // whitelist not used 5016b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 5017b04dfa37SMatthias Ringwald break; 5018b04dfa37SMatthias Ringwald case 1: 5019b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 5020b04dfa37SMatthias Ringwald break; 5021b04dfa37SMatthias Ringwald default: 5022b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 5023b04dfa37SMatthias Ringwald break; 5024b04dfa37SMatthias Ringwald } 5025c163146eSMatthias Ringwald // track outgoing connection 502605002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 5027c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 50286f35bb46SMatthias Ringwald break; 50296f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 50306ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 50316f35bb46SMatthias Ringwald break; 50329cbd2215SMatthias Ringwald #endif 50339cbd2215SMatthias Ringwald #endif 50346f35bb46SMatthias Ringwald default: 50356f35bb46SMatthias Ringwald break; 5036b04dfa37SMatthias Ringwald } 503769a97523S[email protected] 50383a9fb326S[email protected] hci_stack->num_cmd_packets--; 50395bb5bc3eS[email protected] 50405bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 5041bfea0222SMatthias Ringwald return hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 504231452debSmatthias.ringwald } 50438adf0ddaSmatthias.ringwald 50442bd8b7e7S[email protected] // disconnect because of security block 50452bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 50462bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 50472bd8b7e7S[email protected] if (!connection) return; 50482bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 50492bd8b7e7S[email protected] } 50502bd8b7e7S[email protected] 50512bd8b7e7S[email protected] 5052dbe1a790S[email protected] // Configure Secure Simple Pairing 5053dbe1a790S[email protected] 505435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 505535454696SMatthias Ringwald 5056dbe1a790S[email protected] // enable will enable SSP during init 505715a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 50583a9fb326S[email protected] hci_stack->ssp_enable = enable; 5059dbe1a790S[email protected] } 5060dbe1a790S[email protected] 506195d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 506215a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 50632bd8b7e7S[email protected] } 50642bd8b7e7S[email protected] 5065dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 506615a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 50673a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 5068dbe1a790S[email protected] } 506915a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 50703a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 5071dbe1a790S[email protected] } 5072dbe1a790S[email protected] 5073dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 507415a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 50753a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 5076dbe1a790S[email protected] } 50775d23aae8SMatthias Ringwald 50785d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 50795d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 50805d23aae8SMatthias Ringwald } 50815d23aae8SMatthias Ringwald 508235454696SMatthias Ringwald #endif 5083dbe1a790S[email protected] 508494be1a66SMatthias Ringwald // va_list part of hci_send_cmd 508594be1a66SMatthias Ringwald int hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 5086d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 50879d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 50889d14b626S[email protected] return 0; 50899d14b626S[email protected] } 50909d14b626S[email protected] 50915127cc62S[email protected] // for HCI INITIALIZATION 50929da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 50935127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 50945127cc62S[email protected] 50959d14b626S[email protected] hci_reserve_packet_buffer(); 50969d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 509794be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 5098bfea0222SMatthias Ringwald int err = hci_send_cmd_packet(packet, size); 5099bfea0222SMatthias Ringwald 5100593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 5101593702caSMatthias Ringwald if ((err < 0) || hci_transport_synchronous()){ 5102e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5103068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 5104bfea0222SMatthias Ringwald } 5105bfea0222SMatthias Ringwald 5106bfea0222SMatthias Ringwald return err; 510794be1a66SMatthias Ringwald } 51089d14b626S[email protected] 510994be1a66SMatthias Ringwald /** 511094be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 511194be1a66SMatthias Ringwald */ 511294be1a66SMatthias Ringwald int hci_send_cmd(const hci_cmd_t * cmd, ...){ 51131cd208adSmatthias.ringwald va_list argptr; 51141cd208adSmatthias.ringwald va_start(argptr, cmd); 511594be1a66SMatthias Ringwald int res = hci_send_cmd_va_arg(cmd, argptr); 51161cd208adSmatthias.ringwald va_end(argptr); 511794be1a66SMatthias Ringwald return res; 511893b8dc03Smatthias.ringwald } 5119c8e4258aSmatthias.ringwald 5120ee091cf1Smatthias.ringwald // Create various non-HCI events. 5121ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 5122ee091cf1Smatthias.ringwald 5123d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 5124fb37a842SMatthias Ringwald // dump packet 5125d6b06661SMatthias Ringwald if (dump) { 5126300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 5127d6b06661SMatthias Ringwald } 51281ef6bb52SMatthias Ringwald 5129fb37a842SMatthias Ringwald // dispatch to all event handlers 51301ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 51311ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 51321ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 51331ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 5134d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 51351ef6bb52SMatthias Ringwald } 5136d6b06661SMatthias Ringwald } 5137d6b06661SMatthias Ringwald 5138d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 5139fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 51403d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 5141d6b06661SMatthias Ringwald } 5142d6b06661SMatthias Ringwald 514335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5144701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 51453bc639ceSMatthias Ringwald // notify SCO sender if waiting 5146701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 5147701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 51483bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 5149701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 5150701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 51513d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 51523bc639ceSMatthias Ringwald } 51533bc639ceSMatthias Ringwald } 51541cfb383eSMatthias Ringwald 51551cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 51569784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 5157ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 51581cfb383eSMatthias Ringwald 51591cfb383eSMatthias Ringwald uint8_t * eir_data; 51601cfb383eSMatthias Ringwald ad_context_t context; 51611cfb383eSMatthias Ringwald const uint8_t * name; 51621cfb383eSMatthias Ringwald uint8_t name_len; 51631cfb383eSMatthias Ringwald 51649784dac1SMatthias Ringwald if (size < 3) return; 51659784dac1SMatthias Ringwald 51661cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 5167a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 51681cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 51691cfb383eSMatthias Ringwald 51709784dac1SMatthias Ringwald switch (event_type){ 51719784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 51729784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 51739784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 51749784dac1SMatthias Ringwald break; 51759784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 51769784dac1SMatthias Ringwald if (size != 257) return; 51779784dac1SMatthias Ringwald if (num_responses != 1) return; 51789784dac1SMatthias Ringwald break; 51799784dac1SMatthias Ringwald default: 51809784dac1SMatthias Ringwald return; 51819784dac1SMatthias Ringwald } 51829784dac1SMatthias Ringwald 51831cfb383eSMatthias Ringwald // event[1] is set at the end 51841cfb383eSMatthias Ringwald int i; 51851cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 51861cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 51871cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 51881cfb383eSMatthias Ringwald uint8_t event_size = 18; // if name is not set by EIR 51891cfb383eSMatthias Ringwald 51906535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 51910e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 51926535961aSMatthias Ringwald (void)memcpy(&event[9], 51936535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 51946535961aSMatthias Ringwald 3); // class of device 51956535961aSMatthias Ringwald (void)memcpy(&event[12], 51966535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 51976535961aSMatthias Ringwald 2); // clock offset 51981cfb383eSMatthias Ringwald 51991cfb383eSMatthias Ringwald switch (event_type){ 52001cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 52011cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 52021cfb383eSMatthias Ringwald break; 52031cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 52041cfb383eSMatthias Ringwald event[14] = 1; 5205a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 52061cfb383eSMatthias Ringwald // 16,17 = 0, size 18 52071cfb383eSMatthias Ringwald break; 52081cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 52091cfb383eSMatthias Ringwald event[14] = 1; 5210a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 521179186815SMatthias Ringwald // EIR packets only contain a single inquiry response 52121cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 52131cfb383eSMatthias Ringwald name = NULL; 5214a8c4e5adSMatthias Ringwald // Iterate over EIR data 5215a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 52161cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 52171cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 52181cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 5219ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 52201cfb383eSMatthias Ringwald switch (data_type){ 52211cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 52221cfb383eSMatthias Ringwald if (name) continue; 5223cf373d3aSMatthias Ringwald /* fall through */ 52241cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 52251cfb383eSMatthias Ringwald name = data; 52261cfb383eSMatthias Ringwald name_len = data_size; 52271cfb383eSMatthias Ringwald break; 5228ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 5229ac9136ccSMatthias Ringwald if (data_size != 8) break; 5230ac9136ccSMatthias Ringwald event[16] = 1; 52313c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 5232ac9136ccSMatthias Ringwald break; 52331cfb383eSMatthias Ringwald default: 52341cfb383eSMatthias Ringwald break; 52351cfb383eSMatthias Ringwald } 52361cfb383eSMatthias Ringwald } 52371cfb383eSMatthias Ringwald if (name){ 5238ac9136ccSMatthias Ringwald event[25] = 1; 52391cfb383eSMatthias Ringwald // truncate name if needed 52401cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 5241ac9136ccSMatthias Ringwald event[26] = len; 5242ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 52431cfb383eSMatthias Ringwald event_size += len; 52441cfb383eSMatthias Ringwald } 52451cfb383eSMatthias Ringwald break; 52467bbeb3adSMilanka Ringwald default: 52477bbeb3adSMilanka Ringwald return; 52481cfb383eSMatthias Ringwald } 52491cfb383eSMatthias Ringwald event[1] = event_size - 2; 52501cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 52511cfb383eSMatthias Ringwald } 52521cfb383eSMatthias Ringwald } 525335454696SMatthias Ringwald #endif 52543bc639ceSMatthias Ringwald 525571de195eSMatthias Ringwald void hci_emit_state(void){ 52563a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 5257425d1371Smatthias.ringwald uint8_t event[3]; 525880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 52594ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 52603a9fb326S[email protected] event[2] = hci_stack->state; 5261d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5262c8e4258aSmatthias.ringwald } 5263c8e4258aSmatthias.ringwald 526435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 52652deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 5266425d1371Smatthias.ringwald uint8_t event[13]; 5267c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5268425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 526917f1ba2aSmatthias.ringwald event[2] = status; 52702deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 52712deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 5272c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 5273c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 5274d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5275c8e4258aSmatthias.ringwald } 527652db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 527752db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 527852db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 527952db98b2SMatthias Ringwald uint8_t event[4]; 528052db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 528152db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 528252db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 528352db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 528452db98b2SMatthias Ringwald } 528535454696SMatthias Ringwald #endif 5286c8e4258aSmatthias.ringwald 528735454696SMatthias Ringwald #ifdef ENABLE_BLE 5288d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5289667ba9d1SMatthias 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){ 52904f3229d8S[email protected] uint8_t event[21]; 52914f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 52924ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 52934f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 52944f3229d8S[email protected] event[3] = status; 5295fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 52964f3229d8S[email protected] event[6] = 0; // TODO: role 52976e2e9a6bS[email protected] event[7] = address_type; 5298724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 5299f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 5300f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 5301f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 53024f3229d8S[email protected] event[20] = 0; // master clock accuracy 5303d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 53044f3229d8S[email protected] } 530535454696SMatthias Ringwald #endif 5306d70217a2SMatthias Ringwald #endif 53074f3229d8S[email protected] 5308fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 5309fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 5310fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 5311fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 5312fd43c0e0SMatthias Ringwald } 5313fd43c0e0SMatthias Ringwald 5314fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 5315425d1371Smatthias.ringwald uint8_t event[6]; 53163c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 53174ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 53183c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 5319fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 53203c4d4b90Smatthias.ringwald event[5] = reason; 5321d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 53223c4d4b90Smatthias.ringwald } 53233c4d4b90Smatthias.ringwald 5324b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 5325e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 5326425d1371Smatthias.ringwald uint8_t event[3]; 532780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 53284ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 532943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 5330d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 533143bfb1bdSmatthias.ringwald } 5332038bc64cSmatthias.ringwald 5333b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 5334e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 5335425d1371Smatthias.ringwald uint8_t event[2]; 533680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 53374ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 5338d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5339038bc64cSmatthias.ringwald } 53401b0e3922Smatthias.ringwald 534135454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 534235454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 534335454696SMatthias Ringwald uint8_t event[9]; 534435454696SMatthias Ringwald int pos = 0; 534535454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 53464ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 534735454696SMatthias Ringwald event[pos++] = status; 534835454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 5349d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5350381fbed8Smatthias.ringwald } 5351458bf4e8S[email protected] 535235454696SMatthias Ringwald 535335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 535435454696SMatthias Ringwald 5355b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 5356df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 5357a00031e2S[email protected] uint8_t event[5]; 5358e00caf9cS[email protected] int pos = 0; 53595611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 5360e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 5361f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 5362e00caf9cS[email protected] pos += 2; 5363e00caf9cS[email protected] event[pos++] = level; 5364d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5365e00caf9cS[email protected] } 5366e00caf9cS[email protected] 536735454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 536835454696SMatthias Ringwald if (!connection) return LEVEL_0; 536935454696SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 5370fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 5371abdad579SMatthias Ringwald if ((connection->authentication_flags & CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 5372170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 53734051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 5374170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 53750e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 5376170fafaeSMatthias Ringwald security_level = LEVEL_3; 5377170fafaeSMatthias Ringwald } 5378170fafaeSMatthias Ringwald return security_level; 537935454696SMatthias Ringwald } 538035454696SMatthias Ringwald 538135454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 538235454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 538335454696SMatthias Ringwald uint8_t event[3]; 538435454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 538535454696SMatthias Ringwald event[1] = sizeof(event) - 2; 538635454696SMatthias Ringwald event[2] = enabled; 5387d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5388ad83dc6aS[email protected] } 5389ad83dc6aS[email protected] 539098a2fd1cSMatthias Ringwald // query if remote side supports eSCO 5391073bd0faSMatthias Ringwald int hci_remote_esco_supported(hci_con_handle_t con_handle){ 539298a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 539398a2fd1cSMatthias Ringwald if (!connection) return 0; 539476ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 539598a2fd1cSMatthias Ringwald } 539698a2fd1cSMatthias Ringwald 539767aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 539867aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 539967aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 540067aae551SMatthias Ringwald } 540167aae551SMatthias Ringwald 54022bd8b7e7S[email protected] // query if remote side supports SSP 54032bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 54042bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 54052bd8b7e7S[email protected] if (!connection) return 0; 540667aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 54072bd8b7e7S[email protected] } 54082bd8b7e7S[email protected] 540915a95bd5SMatthias Ringwald int gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 5410df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 5411df3354fcS[email protected] } 5412df3354fcS[email protected] 5413458bf4e8S[email protected] // GAP API 5414458bf4e8S[email protected] /** 5415458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 5416458bf4e8S[email protected] * @praram enabled 5417458bf4e8S[email protected] */ 54184c57c146S[email protected] void gap_set_bondable_mode(int enable){ 54193a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 5420458bf4e8S[email protected] } 54214ef6443cSMatthias Ringwald /** 54224ef6443cSMatthias Ringwald * @brief Get bondable mode. 54234ef6443cSMatthias Ringwald * @return 1 if bondable 54244ef6443cSMatthias Ringwald */ 54254ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 54264ef6443cSMatthias Ringwald return hci_stack->bondable; 54274ef6443cSMatthias Ringwald } 5428cb230b9dS[email protected] 5429cb230b9dS[email protected] /** 543034d2123cS[email protected] * @brief map link keys to security levels 5431cb230b9dS[email protected] */ 543234d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 543334d2123cS[email protected] switch (link_key_type){ 54343c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 54353c68dfa9S[email protected] return LEVEL_4; 54363c68dfa9S[email protected] case COMBINATION_KEY: 54373c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 54383c68dfa9S[email protected] return LEVEL_3; 54393c68dfa9S[email protected] default: 54403c68dfa9S[email protected] return LEVEL_2; 54413c68dfa9S[email protected] } 5442cb230b9dS[email protected] } 5443cb230b9dS[email protected] 54448b35e16aSMatthias Ringwald /** 54458b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 54468b35e16aSMatthias Ringwald */ 54478b35e16aSMatthias Ringwald int gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 54488b35e16aSMatthias Ringwald switch (link_key_type){ 54498b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 54508b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 54518b35e16aSMatthias Ringwald return 1; 54528b35e16aSMatthias Ringwald default: 54538b35e16aSMatthias Ringwald return 0; 54548b35e16aSMatthias Ringwald } 54558b35e16aSMatthias Ringwald } 54568b35e16aSMatthias Ringwald 54578b35e16aSMatthias Ringwald /** 54588b35e16aSMatthias Ringwald * @brief map link keys to authenticated 54598b35e16aSMatthias Ringwald */ 54608b35e16aSMatthias Ringwald int gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 54618b35e16aSMatthias Ringwald switch (link_key_type){ 54628b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 54638b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 54648b35e16aSMatthias Ringwald return 1; 54658b35e16aSMatthias Ringwald default: 54668b35e16aSMatthias Ringwald return 0; 54678b35e16aSMatthias Ringwald } 54688b35e16aSMatthias Ringwald } 54698b35e16aSMatthias Ringwald 5470106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 54715127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 5472106d6d11S[email protected] return level > LEVEL_2; 5473106d6d11S[email protected] } 5474106d6d11S[email protected] 547534d2123cS[email protected] /** 547634d2123cS[email protected] * @brief get current security level 547734d2123cS[email protected] */ 547834d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 547934d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 548034d2123cS[email protected] if (!connection) return LEVEL_0; 548134d2123cS[email protected] return gap_security_level_for_connection(connection); 548234d2123cS[email protected] } 548334d2123cS[email protected] 5484cb230b9dS[email protected] /** 5485cb230b9dS[email protected] * @brief request connection to device to 5486cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 5487cb230b9dS[email protected] */ 548834d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 548934d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 549034d2123cS[email protected] if (!connection){ 5491a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 549234d2123cS[email protected] return; 549334d2123cS[email protected] } 5494defbf200SMatthias Ringwald 5495defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 5496defbf200SMatthias Ringwald 549734d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 549883d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 549983d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 550083d08d7cSMatthias Ringwald 5501dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 5502dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 5503dbd5dcc3SMatthias Ringwald if (authentication_active){ 55041cf0a6c8SMatthias Ringwald // authentication already active 550583d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 550683d08d7cSMatthias Ringwald // increase requested level as new level is higher 550783d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 550883d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 550983d08d7cSMatthias Ringwald } 55101cf0a6c8SMatthias Ringwald } else { 551183d08d7cSMatthias Ringwald // no request active, notify if security sufficient 551283d08d7cSMatthias Ringwald if (requested_level <= current_level){ 5513a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 551434d2123cS[email protected] return; 551534d2123cS[email protected] } 5516a00031e2S[email protected] 5517e060c07dSMatthias Ringwald // store request 551834d2123cS[email protected] connection->requested_security_level = requested_level; 5519a00031e2S[email protected] 55201cf0a6c8SMatthias Ringwald // start to authenticate connection 55211eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 5522e80b2cf9S[email protected] hci_run(); 5523e00caf9cS[email protected] } 55241cf0a6c8SMatthias Ringwald } 5525ad83dc6aS[email protected] 5526ad83dc6aS[email protected] /** 5527ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 5528ad83dc6aS[email protected] * @param device 5529ad83dc6aS[email protected] * @param request MITM protection 5530ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 5531ad83dc6aS[email protected] */ 5532ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 5533ad83dc6aS[email protected] 5534ad83dc6aS[email protected] // create connection state machine 5535f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 5536ad83dc6aS[email protected] 5537ad83dc6aS[email protected] if (!connection){ 5538ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 5539ad83dc6aS[email protected] } 5540ad83dc6aS[email protected] 5541ad83dc6aS[email protected] // delete linkn key 554215a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 5543ad83dc6aS[email protected] 5544ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 5545ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 5546ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 5547f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 5548ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 5549ad83dc6aS[email protected] 5550ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 5551ad83dc6aS[email protected] 5552ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 5553ad83dc6aS[email protected] // handle: authentication failure 5554ad83dc6aS[email protected] // handle: disconnect on done 5555ad83dc6aS[email protected] 5556ad83dc6aS[email protected] hci_run(); 5557ad83dc6aS[email protected] 5558ad83dc6aS[email protected] return 0; 5559ad83dc6aS[email protected] } 556035454696SMatthias Ringwald #endif 55618e618f72S[email protected] 55628e618f72S[email protected] void gap_set_local_name(const char * local_name){ 55638e618f72S[email protected] hci_stack->local_name = local_name; 55648e618f72S[email protected] } 55658e618f72S[email protected] 556635454696SMatthias Ringwald 556735454696SMatthias Ringwald #ifdef ENABLE_BLE 556835454696SMatthias Ringwald 5569d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5570d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 5571fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 55727bdc6798S[email protected] hci_run(); 55737bdc6798S[email protected] } 55748e618f72S[email protected] 5575d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 5576fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 55777bdc6798S[email protected] hci_run(); 55787bdc6798S[email protected] } 55794f3229d8S[email protected] 5580a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 5581ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 5582a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 5583ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 5584ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 55858b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 5586ef11999fSmatthias.ringwald hci_run(); 5587ef11999fSmatthias.ringwald } 55884f3229d8S[email protected] 5589a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 5590a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 5591a7a719e9SMatthias Ringwald } 5592a7a719e9SMatthias Ringwald 5593667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 55944f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 55954f3229d8S[email protected] if (!conn){ 5596d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 5597d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 5598d32b3f05SMatthias Ringwald log_error("le connection already active"); 5599d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 5600d32b3f05SMatthias Ringwald } 5601d32b3f05SMatthias Ringwald 5602d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 56032e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 56044f3229d8S[email protected] if (!conn){ 56054f3229d8S[email protected] // notify client that alloc failed 56066e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 5607d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 5608472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 56094f3229d8S[email protected] } 5610d5b1a89eSMatthias Ringwald 5611d5b1a89eSMatthias Ringwald // set le connecting state 5612d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 5613d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 5614d5b1a89eSMatthias Ringwald } 5615d5b1a89eSMatthias Ringwald 56164f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 5617d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 5618564fca32S[email protected] hci_run(); 5619b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 56204f3229d8S[email protected] } 56210bf6344aS[email protected] 56220bf6344aS[email protected] if (!hci_is_le_connection(conn) || 5623a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 56240e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 56252e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 5626d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 5627616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 56280bf6344aS[email protected] } 56290bf6344aS[email protected] 5630b0136355SMatthias Ringwald // check if connection was just disconnected 5631b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 5632b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 5633b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 5634b0136355SMatthias Ringwald hci_run(); 5635b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 5636b0136355SMatthias Ringwald } 5637b0136355SMatthias Ringwald 5638d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 5639d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 56404f3229d8S[email protected] hci_run(); 5641b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 56424f3229d8S[email protected] } 56434f3229d8S[email protected] 56447851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 5645d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 5646665d90f2SMatthias Ringwald btstack_linked_item_t *it; 5647a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 56480bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 56490bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 56500bf6344aS[email protected] switch (conn->state){ 5651a6725849S[email protected] case SEND_CREATE_CONNECTION: 56527851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 56539c77c9dbSMatthias Ringwald case SENT_CANCEL_CONNECTION: 56547851196eSmatthias.ringwald return conn; 56557851196eSmatthias.ringwald default: 56567851196eSmatthias.ringwald break; 56577851196eSmatthias.ringwald }; 56587851196eSmatthias.ringwald } 56597851196eSmatthias.ringwald return NULL; 56607851196eSmatthias.ringwald } 56617851196eSmatthias.ringwald 5662d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 5663d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 5664616edd56SMatthias Ringwald if (!conn) return 0; 56657851196eSmatthias.ringwald switch (conn->state){ 56667851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 56677851196eSmatthias.ringwald // skip sending create connection and emit event instead 566810f8f469SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 56692e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 5670665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 56717851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 56720bf6344aS[email protected] break; 5673a6725849S[email protected] case SENT_CREATE_CONNECTION: 56747851196eSmatthias.ringwald // request to send cancel connection 56750bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 56760bf6344aS[email protected] hci_run(); 56770bf6344aS[email protected] break; 56780bf6344aS[email protected] default: 56790bf6344aS[email protected] break; 56800bf6344aS[email protected] } 5681616edd56SMatthias Ringwald return 0; 5682e31f89a7S[email protected] } 5683d70217a2SMatthias Ringwald #endif 56844f3229d8S[email protected] 568513e645d2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5686c37a3166S[email protected] /** 56876012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 5688cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 5689cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 56906012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 56916012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 56926012052bSMatthias Ringwald * @param conn_latency, default: 4 56936012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 56946012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 56956012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 56966012052bSMatthias Ringwald */ 56976012052bSMatthias Ringwald 5698cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 5699cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 5700cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 5701cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 5702cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 57036012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 57046012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 57056012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 57066012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 57076012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 57086012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 57096012052bSMatthias Ringwald } 571013e645d2SMatthias Ringwald #endif 57116012052bSMatthias Ringwald 57126012052bSMatthias Ringwald /** 5713c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 5714c37a3166S[email protected] * @param handle 5715c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 5716c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 5717c37a3166S[email protected] * @param conn_latency 5718c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 5719c37a3166S[email protected] * @returns 0 if ok 5720c37a3166S[email protected] */ 5721c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 5722c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 5723c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 5724c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5725c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 5726c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 5727c37a3166S[email protected] connection->le_conn_latency = conn_latency; 5728c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 572984cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 5730cfc59f1bSMatthias Ringwald hci_run(); 5731c37a3166S[email protected] return 0; 5732c37a3166S[email protected] } 5733c37a3166S[email protected] 573445c102fdSMatthias Ringwald /** 5735b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 5736b68d7bc3SMatthias Ringwald * @param handle 5737b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 5738b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 5739b68d7bc3SMatthias Ringwald * @param conn_latency 5740b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 5741b68d7bc3SMatthias Ringwald * @returns 0 if ok 5742b68d7bc3SMatthias Ringwald */ 5743b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 5744b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 5745b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5746b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5747b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 5748b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 5749b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 5750b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 5751b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 575209c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 575309c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 5754b68d7bc3SMatthias Ringwald return 0; 5755b68d7bc3SMatthias Ringwald } 5756b68d7bc3SMatthias Ringwald 5757d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5758d70217a2SMatthias Ringwald 5759b68d7bc3SMatthias Ringwald /** 576045c102fdSMatthias Ringwald * @brief Set Advertisement Data 576145c102fdSMatthias Ringwald * @param advertising_data_length 576245c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 576345c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 576445c102fdSMatthias Ringwald */ 576545c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 576645c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 576745c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 5768501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 5769bbc366e6SMatthias Ringwald hci_run(); 577045c102fdSMatthias Ringwald } 5771501f56b3SMatthias Ringwald 5772501f56b3SMatthias Ringwald /** 5773501f56b3SMatthias Ringwald * @brief Set Scan Response Data 5774501f56b3SMatthias Ringwald * @param advertising_data_length 5775501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 5776501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 5777501f56b3SMatthias Ringwald */ 5778501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 5779501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 5780501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 5781501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5782bbc366e6SMatthias Ringwald hci_run(); 578345c102fdSMatthias Ringwald } 578445c102fdSMatthias Ringwald 578545c102fdSMatthias Ringwald /** 578645c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 578745c102fdSMatthias Ringwald * @param adv_int_min 578845c102fdSMatthias Ringwald * @param adv_int_max 578945c102fdSMatthias Ringwald * @param adv_type 579045c102fdSMatthias Ringwald * @param direct_address_type 579145c102fdSMatthias Ringwald * @param direct_address 579245c102fdSMatthias Ringwald * @param channel_map 579345c102fdSMatthias Ringwald * @param filter_policy 579445c102fdSMatthias Ringwald * 579545c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 579645c102fdSMatthias Ringwald */ 579745c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 5798b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 579945c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 580045c102fdSMatthias Ringwald 580145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 580245c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 580345c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 580445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 580545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 580645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 58076535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 58086535961aSMatthias Ringwald 6); 580945c102fdSMatthias Ringwald 5810a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS | LE_ADVERTISEMENT_TASKS_PARAMS_SET; 5811bbc366e6SMatthias Ringwald hci_run(); 581245c102fdSMatthias Ringwald } 581345c102fdSMatthias Ringwald 581445c102fdSMatthias Ringwald /** 581545c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 581645c102fdSMatthias Ringwald * @param enabled 581745c102fdSMatthias Ringwald */ 581845c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 58195226d7f2SMatthias Ringwald hci_stack->le_advertisements_enabled = enabled != 0; 5820bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 5821cfc59f1bSMatthias Ringwald hci_run(); 582245c102fdSMatthias Ringwald } 582345c102fdSMatthias Ringwald 582435454696SMatthias Ringwald #endif 582506e5cf96SMatthias Ringwald 582606e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 582706e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 582806e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 582906e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 583006e5cf96SMatthias Ringwald 583164068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 583206e5cf96SMatthias Ringwald // update advertisement parameters, too 583306e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 5834bbc366e6SMatthias Ringwald hci_run(); 583564068776SMatthias Ringwald #endif 583664068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 583706e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 583864068776SMatthias Ringwald #endif 583906e5cf96SMatthias Ringwald } 584006e5cf96SMatthias Ringwald 5841d70217a2SMatthias Ringwald #endif 584245c102fdSMatthias Ringwald 5843616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 58445917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 58455917a5c5S[email protected] if (!conn){ 58467851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 5847616edd56SMatthias Ringwald return 0; 58485917a5c5S[email protected] } 58497fd7aa6fSMatthias Ringwald // ignore if already disconnected 58507fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 58517fd7aa6fSMatthias Ringwald return 0; 58527fd7aa6fSMatthias Ringwald } 58535917a5c5S[email protected] conn->state = SEND_DISCONNECT; 58545917a5c5S[email protected] hci_run(); 5855616edd56SMatthias Ringwald return 0; 58564f3229d8S[email protected] } 585704a6ef8cSmatthias.ringwald 5858228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 5859228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 5860228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 5861228e430cSMatthias Ringwald connectionSetAuthenticationFlags(hci_connection, READ_RSSI); 5862228e430cSMatthias Ringwald hci_run(); 5863228e430cSMatthias Ringwald return 1; 5864228e430cSMatthias Ringwald } 5865228e430cSMatthias Ringwald 5866a1bf5ae7SMatthias Ringwald /** 5867a1bf5ae7SMatthias Ringwald * @brief Get connection type 5868a1bf5ae7SMatthias Ringwald * @param con_handle 5869a1bf5ae7SMatthias Ringwald * @result connection_type 5870a1bf5ae7SMatthias Ringwald */ 5871a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 5872a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 5873a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 5874a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 5875a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 5876a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 5877a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 5878a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 5879a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 5880f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 5881a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 5882a1bf5ae7SMatthias Ringwald default: 5883a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 5884a1bf5ae7SMatthias Ringwald } 5885a1bf5ae7SMatthias Ringwald } 5886a1bf5ae7SMatthias Ringwald 58872dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 58882dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 58892dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 58902dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 58912dceb1d6SMatthias Ringwald } 58922dceb1d6SMatthias Ringwald 58932dceb1d6SMatthias Ringwald 589444f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 5895667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 589688a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 589788a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 589888a03c8dSMatthias Ringwald conn->request_role = role; 589988a03c8dSMatthias Ringwald hci_run(); 5900d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 590188a03c8dSMatthias Ringwald } 590244f858f3SMatthias Ringwald #endif 590388a03c8dSMatthias Ringwald 5904a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 59054f551432SMatthias Ringwald 5906b45b7749SMilanka Ringwald uint8_t gap_le_set_phy(hci_con_handle_t con_handle, uint8_t all_phys, uint8_t tx_phys, uint8_t rx_phys, uint8_t phy_options){ 5907b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 5908b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 5909b90f6e0aSMatthias Ringwald 5910b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 5911b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 5912b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 5913b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 5914b90f6e0aSMatthias Ringwald 5915b90f6e0aSMatthias Ringwald hci_run(); 5916b90f6e0aSMatthias Ringwald 5917b90f6e0aSMatthias Ringwald return 0; 5918b90f6e0aSMatthias Ringwald } 5919b90f6e0aSMatthias Ringwald 5920667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 59217a92a9dbSMatthias Ringwald // check if already in list 59227a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 59237a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 59247a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 59257a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 59267a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 59277a92a9dbSMatthias Ringwald continue; 59287a92a9dbSMatthias Ringwald } 59297a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 59307a92a9dbSMatthias Ringwald continue; 59317a92a9dbSMatthias Ringwald } 5932287379ccSMatthias Ringwald // disallow if already scheduled to add 5933287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 59347a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 59357a92a9dbSMatthias Ringwald } 5936287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 5937287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 5938287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 5939287379ccSMatthias Ringwald } 59407a92a9dbSMatthias Ringwald // alloc and add to list 5941e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 5942e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 5943e83201bcSMatthias Ringwald entry->address_type = address_type; 59446535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 5945e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 5946665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 5947226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 5948ac9c45e0SMatthias Ringwald } 5949ac9c45e0SMatthias Ringwald 5950667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 5951665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 5952665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 5953665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5954665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 59557a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 59567a92a9dbSMatthias Ringwald continue; 59577a92a9dbSMatthias Ringwald } 59587a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 59597a92a9dbSMatthias Ringwald continue; 59607a92a9dbSMatthias Ringwald } 5961e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 5962e83201bcSMatthias Ringwald // remove from controller if already present 5963e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 5964a3b69fdeSMatthias Ringwald } else { 5965226db5efSMatthias Ringwald // directly remove entry from whitelist 5966665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 5967e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 5968e83201bcSMatthias Ringwald } 5969a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 5970a3b69fdeSMatthias Ringwald } 5971a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 597242ff5ba1SMatthias Ringwald } 597342ff5ba1SMatthias Ringwald 5974226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 5975665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 5976665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 5977665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5978665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 5979e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 5980e83201bcSMatthias Ringwald // remove from controller if already present 5981e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 5982e83201bcSMatthias Ringwald continue; 5983e83201bcSMatthias Ringwald } 598491915b0bSMatthias Ringwald // directly remove entry from whitelist 5985665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 5986e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 5987e83201bcSMatthias Ringwald } 5988226db5efSMatthias Ringwald } 5989226db5efSMatthias Ringwald 5990a3b69fdeSMatthias Ringwald /** 5991a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 5992a3b69fdeSMatthias Ringwald * @returns 0 if ok 5993a3b69fdeSMatthias Ringwald */ 5994a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 5995a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 5996a3b69fdeSMatthias Ringwald hci_run(); 5997a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 5998a3b69fdeSMatthias Ringwald } 5999a3b69fdeSMatthias Ringwald 6000a3b69fdeSMatthias Ringwald /** 6001a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 6002a3b69fdeSMatthias Ringwald * @param address_typ 6003a3b69fdeSMatthias Ringwald * @param address 6004a3b69fdeSMatthias Ringwald * @returns 0 if ok 6005a3b69fdeSMatthias Ringwald */ 6006667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 6007a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 6008a3b69fdeSMatthias Ringwald if (status){ 6009a3b69fdeSMatthias Ringwald return status; 6010a3b69fdeSMatthias Ringwald } 6011a3b69fdeSMatthias Ringwald hci_run(); 6012a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6013a3b69fdeSMatthias Ringwald } 6014a3b69fdeSMatthias Ringwald 6015a3b69fdeSMatthias Ringwald /** 6016a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 6017a3b69fdeSMatthias Ringwald * @param address_typ 6018a3b69fdeSMatthias Ringwald * @param address 6019a3b69fdeSMatthias Ringwald * @returns 0 if ok 6020a3b69fdeSMatthias Ringwald */ 6021667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6022a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 6023a3b69fdeSMatthias Ringwald if (status){ 6024a3b69fdeSMatthias Ringwald return status; 6025a3b69fdeSMatthias Ringwald } 6026a3b69fdeSMatthias Ringwald hci_run(); 6027a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6028a3b69fdeSMatthias Ringwald } 6029a3b69fdeSMatthias Ringwald 6030226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6031226db5efSMatthias Ringwald /** 603295e257d9SMatthias Ringwald * @brief Connect with Whitelist 603395e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 603495e257d9SMatthias Ringwald * @returns - if ok 603595e257d9SMatthias Ringwald */ 603695e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 603795e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 603895e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 603995e257d9SMatthias Ringwald } 604095e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 604195e257d9SMatthias Ringwald hci_run(); 604295e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 604395e257d9SMatthias Ringwald } 604495e257d9SMatthias Ringwald 604595e257d9SMatthias Ringwald /** 6046226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 6047226db5efSMatthias Ringwald * @param address_typ 6048226db5efSMatthias Ringwald * @param address 6049226db5efSMatthias Ringwald * @returns 0 if ok 6050226db5efSMatthias Ringwald */ 6051667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 605295e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 6053226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6054226db5efSMatthias Ringwald } 6055226db5efSMatthias Ringwald 6056226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 605763f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 6058226db5efSMatthias Ringwald return status; 6059226db5efSMatthias Ringwald } 6060226db5efSMatthias Ringwald 6061226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 6062226db5efSMatthias Ringwald 6063226db5efSMatthias Ringwald hci_run(); 606495e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6065226db5efSMatthias Ringwald } 6066226db5efSMatthias Ringwald 6067226db5efSMatthias Ringwald /** 6068226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 6069226db5efSMatthias Ringwald * @param address_typ 6070226db5efSMatthias Ringwald * @param address 6071226db5efSMatthias Ringwald * @returns 0 if ok 6072226db5efSMatthias Ringwald */ 6073667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 607495e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 607595e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 607695e257d9SMatthias Ringwald } 607795e257d9SMatthias Ringwald 6078226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 607995e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 608095e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 608195e257d9SMatthias Ringwald } 6082226db5efSMatthias Ringwald hci_run(); 6083226db5efSMatthias Ringwald return 0; 6084226db5efSMatthias Ringwald } 6085226db5efSMatthias Ringwald 6086226db5efSMatthias Ringwald /** 6087226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 6088226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 6089226db5efSMatthias Ringwald */ 609095e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 609195e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 609295e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 609395e257d9SMatthias Ringwald } 6094226db5efSMatthias Ringwald hci_whitelist_clear(); 6095226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 6096e83201bcSMatthias Ringwald hci_run(); 609795e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6098ac9c45e0SMatthias Ringwald } 6099c9db5c21SMilanka Ringwald 6100b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 6101b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6102c9db5c21SMilanka Ringwald if (!conn) return 0; 6103c9db5c21SMilanka Ringwald return conn->le_connection_interval; 6104c9db5c21SMilanka Ringwald } 6105d70217a2SMatthias Ringwald #endif 61064f551432SMatthias Ringwald #endif 61074f551432SMatthias Ringwald 610835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6109ac9c45e0SMatthias Ringwald /** 6110ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 6111a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 6112ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 6113ff00ed1cSMatthias Ringwald */ 6114ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 6115ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 6116ff00ed1cSMatthias Ringwald } 6117ff00ed1cSMatthias Ringwald 6118ff00ed1cSMatthias Ringwald /** 6119f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 6120f5875de5SMatthias Ringwald * @param duration in 1.28s units 6121f5875de5SMatthias Ringwald * @return 0 if ok 6122f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 6123f5875de5SMatthias Ringwald */ 6124f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 612599449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 6126f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6127a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 6128f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 6129f5875de5SMatthias Ringwald } 6130f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 6131f5875de5SMatthias Ringwald hci_run(); 6132f5875de5SMatthias Ringwald return 0; 6133f5875de5SMatthias Ringwald } 6134f5875de5SMatthias Ringwald 6135f5875de5SMatthias Ringwald /** 6136f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 6137f5875de5SMatthias Ringwald * @returns 0 if ok 6138f5875de5SMatthias Ringwald */ 6139f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 6140a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 6141f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 6142f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 6143f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 6144f5875de5SMatthias Ringwald return 0; 6145f5875de5SMatthias Ringwald } 6146f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 6147f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 6148f5875de5SMatthias Ringwald hci_run(); 6149f5875de5SMatthias Ringwald return 0; 6150f5875de5SMatthias Ringwald } 6151f5875de5SMatthias Ringwald 6152496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 6153496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 6154496bb884SMatthias Ringwald } 6155496bb884SMatthias Ringwald 6156b7f1ee76SMatthias Ringwald 6157b7f1ee76SMatthias Ringwald /** 6158b7f1ee76SMatthias Ringwald * @brief Remote Name Request 6159b7f1ee76SMatthias Ringwald * @param addr 6160b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 6161b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 6162b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 6163b7f1ee76SMatthias Ringwald */ 6164667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 6165b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 61666535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 6167b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 6168b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 6169b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 6170b7f1ee76SMatthias Ringwald hci_run(); 6171b7f1ee76SMatthias Ringwald return 0; 6172b7f1ee76SMatthias Ringwald } 6173b7f1ee76SMatthias Ringwald 6174667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 61750a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 61766535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 61770a51f88bSMatthias Ringwald hci_run(); 61780a51f88bSMatthias Ringwald return 0; 61790a51f88bSMatthias Ringwald } 61800a51f88bSMatthias Ringwald 61810a51f88bSMatthias Ringwald /** 6182aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 6183aad97216SMatthias Ringwald * @param addr 6184aad97216SMatthias Ringwald * @param pin_data 6185aad97216SMatthias Ringwald * @param pin_len 6186aad97216SMatthias Ringwald * @return 0 if ok 6187aad97216SMatthias Ringwald */ 6188667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 6189aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6190aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 6191aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 6192aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 6193aad97216SMatthias Ringwald } 6194aad97216SMatthias Ringwald 6195aad97216SMatthias Ringwald /** 61960a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 61970a51f88bSMatthias Ringwald * @param addr 61980a51f88bSMatthias Ringwald * @param pin 61990a51f88bSMatthias Ringwald * @return 0 if ok 62000a51f88bSMatthias Ringwald */ 6201667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 6202aad97216SMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, strlen(pin)); 62030a51f88bSMatthias Ringwald } 62040a51f88bSMatthias Ringwald 62050a51f88bSMatthias Ringwald /** 62060a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 62070a51f88bSMatthias Ringwald * @param addr 62080a51f88bSMatthias Ringwald * @param pin 62090a51f88bSMatthias Ringwald * @return 0 if ok 62100a51f88bSMatthias Ringwald */ 62110a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 6212cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 62130a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 62140a51f88bSMatthias Ringwald } 62150a51f88bSMatthias Ringwald 62160a51f88bSMatthias Ringwald /** 62170a51f88bSMatthias Ringwald * @brief SSP Passkey Response 62180a51f88bSMatthias Ringwald * @param addr 62190a51f88bSMatthias Ringwald * @param passkey 62200a51f88bSMatthias Ringwald * @return 0 if ok 62210a51f88bSMatthias Ringwald */ 6222667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 6223cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6224d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 62250a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 62260a51f88bSMatthias Ringwald } 62270a51f88bSMatthias Ringwald 62280a51f88bSMatthias Ringwald /** 62290a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 62300a51f88bSMatthias Ringwald * @param addr 62310a51f88bSMatthias Ringwald * @param pin 62320a51f88bSMatthias Ringwald * @return 0 if ok 62330a51f88bSMatthias Ringwald */ 6234667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 6235cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 62360a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 62370a51f88bSMatthias Ringwald } 62380a51f88bSMatthias Ringwald 62390a51f88bSMatthias Ringwald /** 62400a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 62410a51f88bSMatthias Ringwald * @param addr 62420a51f88bSMatthias Ringwald * @param passkey 62430a51f88bSMatthias Ringwald * @return 0 if ok 62440a51f88bSMatthias Ringwald */ 6245667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 6246cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 62470a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 62480a51f88bSMatthias Ringwald } 62490a51f88bSMatthias Ringwald 62500a51f88bSMatthias Ringwald /** 62510a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 62520a51f88bSMatthias Ringwald * @param addr 62530a51f88bSMatthias Ringwald * @param pin 62540a51f88bSMatthias Ringwald * @return 0 if ok 62550a51f88bSMatthias Ringwald */ 6256667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 6257cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 62580a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 62590a51f88bSMatthias Ringwald } 62600a51f88bSMatthias Ringwald 626144565b0cSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 626244565b0cSMatthias Ringwald 626344565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 626444565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 626544565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 626644565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 626744565b0cSMatthias Ringwald hci_run(); 626844565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 626944565b0cSMatthias Ringwald } 627044565b0cSMatthias Ringwald 627144565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 627244565b0cSMatthias Ringwald return gap_set_auth_flag_and_run(addr, SEND_IO_CAPABILITIES_REPLY); 627344565b0cSMatthias Ringwald } 627444565b0cSMatthias Ringwald 627544565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 627644565b0cSMatthias Ringwald return gap_set_auth_flag_and_run(addr, SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 627744565b0cSMatthias Ringwald } 627844565b0cSMatthias Ringwald #endif 627944565b0cSMatthias Ringwald 62801849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 62811849becdSMatthias Ringwald /** 62821849becdSMatthias Ringwald * @brief Report Remote OOB Data 62831849becdSMatthias Ringwald * @param bd_addr 62841849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 62851849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 62861849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 62871849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 62881849becdSMatthias Ringwald */ 62891849becdSMatthias 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){ 62901849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 62911849becdSMatthias Ringwald if (connection == NULL) { 62921849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 62931849becdSMatthias Ringwald } 62941849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 62951849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 62961849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 62971849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 62981849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 62991849becdSMatthias Ringwald } 6300cf01e888SMatthias Ringwald /** 6301cf01e888SMatthias Ringwald * @brief Generate new OOB data 6302cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 6303cf01e888SMatthias Ringwald */ 6304cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 6305cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 6306cf01e888SMatthias Ringwald hci_run(); 6307cf01e888SMatthias Ringwald } 6308cf01e888SMatthias Ringwald 63091849becdSMatthias Ringwald #endif 63101849becdSMatthias Ringwald 6311f5875de5SMatthias Ringwald /** 6312f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 6313f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 6314f6858d14SMatthias Ringwald */ 6315b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 6316b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 6317f6858d14SMatthias Ringwald } 6318f6858d14SMatthias Ringwald 6319f6858d14SMatthias Ringwald /** 6320d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 6321d950d659SMatthias Ringwald */ 6322d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 6323d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 6324d950d659SMatthias Ringwald } 6325d950d659SMatthias Ringwald 6326d950d659SMatthias Ringwald /** 6327d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 6328d950d659SMatthias Ringwald * @return current voice setting 6329d950d659SMatthias Ringwald */ 63300cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 6331d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 6332d950d659SMatthias Ringwald } 6333d950d659SMatthias Ringwald 6334400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 6335400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 6336400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 6337400ff5abSMatthias Ringwald if (!transport_name) return 0; 6338400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 6339400ff5abSMatthias Ringwald } 6340400ff5abSMatthias Ringwald 6341b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 6342b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 6343b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 6344b3aad8daSMatthias Ringwald */ 6345b3aad8daSMatthias Ringwald int hci_get_sco_packet_length(void){ 6346a3069afeSMatthias Ringwald int sco_packet_length = 0; 6347cf119f3bSMatthias Ringwald 63481e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 63496f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 63501431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 6351cf119f3bSMatthias Ringwald 6352400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 6353400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 6354cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 635552e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 635652e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 6357400ff5abSMatthias Ringwald } else { 6358400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 6359400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 6360d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 6361d966a453SMatthias Ringwald if (sco_packet_length > hci_stack->sco_data_packet_length){ 6362d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 6363d966a453SMatthias Ringwald } 6364400ff5abSMatthias Ringwald } 6365a3069afeSMatthias Ringwald #endif 63665b7087c7SMatthias Ringwald 63675b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 63685b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 63691e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 63705b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 63715b7087c7SMatthias Ringwald #endif 6372a3069afeSMatthias Ringwald return sco_packet_length; 6373b3aad8daSMatthias Ringwald } 6374b3aad8daSMatthias Ringwald 6375c4c88f1bSJakob Krantz /** 6376c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 6377c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 6378c4c88f1bSJakob Krantz */ 6379c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 6380c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 6381c4c88f1bSJakob Krantz } 6382c4c88f1bSJakob Krantz 6383c4c88f1bSJakob Krantz #endif 6384ec111c8bSMatthias Ringwald 6385ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 6386ec111c8bSMatthias Ringwald return hci_stack->state; 6387ec111c8bSMatthias Ringwald } 6388ec111c8bSMatthias Ringwald 638907e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 63905e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 639107e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 639207e010b6SMilanka Ringwald } 639307e010b6SMilanka Ringwald #endif 6394ec111c8bSMatthias Ringwald 6395d950d659SMatthias Ringwald /** 6396d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 6397d23838ecSMatthias Ringwald */ 6398c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 6399d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 6400d23838ecSMatthias Ringwald } 6401d23838ecSMatthias Ringwald 640271de195eSMatthias Ringwald void hci_disconnect_all(void){ 6403665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6404665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 6405665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6406665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 640704a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 640804a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 640904a6ef8cSmatthias.ringwald } 6410d31fba26S[email protected] hci_run(); 641104a6ef8cSmatthias.ringwald } 641233373e40SMatthias Ringwald 641333373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 641433373e40SMatthias Ringwald return hci_stack->manufacturer; 641533373e40SMatthias Ringwald } 64169c6e867eSMatthias Ringwald 64173e329ddfSandryblack #ifdef ENABLE_BLE 64189c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 64199c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 64209c6e867eSMatthias Ringwald if (!hci_con) return NULL; 64219c6e867eSMatthias Ringwald return &hci_con->sm_connection; 64229c6e867eSMatthias Ringwald } 64239c6e867eSMatthias Ringwald 64249c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 64259c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 64266a79f6baSMatthias Ringwald #endif 64279c6e867eSMatthias Ringwald 64289c6e867eSMatthias Ringwald int gap_encryption_key_size(hci_con_handle_t con_handle){ 6429c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6430c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 6431c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 64326a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 6433c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 6434c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 64359c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 64369c6e867eSMatthias Ringwald } 64376a79f6baSMatthias Ringwald #endif 64386a79f6baSMatthias Ringwald } else { 6439c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 6440c5fb5ca4SMatthias Ringwald if ((hci_connection->authentication_flags & CONNECTION_ENCRYPTED)){ 6441c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 6442c5fb5ca4SMatthias Ringwald } 6443c5fb5ca4SMatthias Ringwald #endif 64446a79f6baSMatthias Ringwald } 6445c5fb5ca4SMatthias Ringwald return 0; 6446c5fb5ca4SMatthias Ringwald } 64479c6e867eSMatthias Ringwald 64489c6e867eSMatthias Ringwald int gap_authenticated(hci_con_handle_t con_handle){ 6449c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6450c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 64515f3981bfSMatthias Ringwald 6452c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 64536a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 64545f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 64555f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6456c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 6457c5fb5ca4SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated; 64586a79f6baSMatthias Ringwald #endif 645959a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 64605f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6461f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6462c5fb5ca4SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 646359a1a47aSMatthias Ringwald #endif 64645f3981bfSMatthias Ringwald default: 64655f3981bfSMatthias Ringwald return 0; 64665f3981bfSMatthias Ringwald } 64679c6e867eSMatthias Ringwald } 64689c6e867eSMatthias Ringwald 6469f1dfbe18SMatthias Ringwald int gap_secure_connection(hci_con_handle_t con_handle){ 6470c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6471c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 64728b35e16aSMatthias Ringwald 6473c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 64746a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 64758b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 64768b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6477c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 6478c5fb5ca4SMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc; 64796a79f6baSMatthias Ringwald #endif 648059a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 64818b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6482f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6483c5fb5ca4SMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 648459a1a47aSMatthias Ringwald #endif 64858b35e16aSMatthias Ringwald default: 64868b35e16aSMatthias Ringwald return 0; 64878b35e16aSMatthias Ringwald } 6488f1dfbe18SMatthias Ringwald } 6489f1dfbe18SMatthias Ringwald 64901e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 64911e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 64921e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 64931e122704SMatthias Ringwald 649448f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 64951e122704SMatthias Ringwald link_key_t link_key; 64961e122704SMatthias Ringwald link_key_type_t link_key_type; 649748f33f37SMatthias Ringwald #endif 64981e122704SMatthias Ringwald switch (hci_connection->address_type){ 64996a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 65001e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 65011e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 65021e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 65036a79f6baSMatthias Ringwald #endif 65041e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 65051e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 65061e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 65071e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 65081e122704SMatthias Ringwald #endif 65091e122704SMatthias Ringwald default: 65101e122704SMatthias Ringwald return false; 65111e122704SMatthias Ringwald } 65121e122704SMatthias Ringwald } 65131e122704SMatthias Ringwald 65146a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 65159c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 65169c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 65179c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 65189c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 65199c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 65209c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 65219c6e867eSMatthias Ringwald } 65229c6e867eSMatthias Ringwald #endif 6523f8ee3071SMatthias Ringwald 6524f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 6525f8ee3071SMatthias 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){ 6526f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6527f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6528f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 6529f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 6530f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 6531f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 6532f8ee3071SMatthias Ringwald hci_run(); 6533f8ee3071SMatthias Ringwald return 0; 6534f8ee3071SMatthias Ringwald } 6535f8ee3071SMatthias Ringwald 6536f8ee3071SMatthias Ringwald /** 6537f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 6538f8ee3071SMatthias Ringwald * @param con_handle 6539f8ee3071SMatthias Ringwald @ @return 0 if ok 6540f8ee3071SMatthias Ringwald */ 6541f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 6542f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6543f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6544f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 6545f8ee3071SMatthias Ringwald hci_run(); 6546f8ee3071SMatthias Ringwald return 0; 6547f8ee3071SMatthias Ringwald } 6548bea424a5SMatthias Ringwald 6549140c0557SMatthias 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){ 6550140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6551140c0557SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6552140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 6553140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 6554140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 6555140c0557SMatthias Ringwald hci_run(); 6556dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 6557140c0557SMatthias Ringwald } 6558140c0557SMatthias Ringwald 6559278ff8a9SMatthias Ringwald uint8_t gap_qos_set(hci_con_handle_t con_handle, hci_service_type_t service_type, uint32_t token_rate, uint32_t peak_bandwidth, uint32_t latency, uint32_t delay_variation){ 6560278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6561278ff8a9SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6562278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 6563278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 6564278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 6565278ff8a9SMatthias Ringwald conn->qos_latency = latency; 6566278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 6567278ff8a9SMatthias Ringwald hci_run(); 6568278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6569278ff8a9SMatthias Ringwald } 6570278ff8a9SMatthias Ringwald 6571bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 6572bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 6573bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 6574bea424a5SMatthias Ringwald hci_run(); 6575bea424a5SMatthias Ringwald } 6576bea424a5SMatthias Ringwald 6577bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 6578bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 6579bea424a5SMatthias Ringwald hci_run(); 6580bea424a5SMatthias Ringwald } 6581bea424a5SMatthias Ringwald 6582f8ee3071SMatthias Ringwald #endif 6583beceeddeSMatthias Ringwald 6584beceeddeSMatthias Ringwald void hci_halting_defer(void){ 6585beceeddeSMatthias Ringwald if (hci_stack->state != HCI_STATE_HALTING) return; 6586beceeddeSMatthias Ringwald switch (hci_stack->substate){ 6587beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 6588beceeddeSMatthias Ringwald case HCI_HALTING_CLOSE: 6589beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_TIMER; 6590beceeddeSMatthias Ringwald break; 6591beceeddeSMatthias Ringwald default: 6592beceeddeSMatthias Ringwald break; 6593beceeddeSMatthias Ringwald } 6594beceeddeSMatthias Ringwald } 6595eddac615SMatthias Ringwald 659621debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 659721debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 659821debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 659921debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 660021debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 660121debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 660202b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 660321debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 660402b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 660502b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 660602b02cffSMatthias Ringwald } 660702b02cffSMatthias Ringwald } 660802b02cffSMatthias Ringwald 660902b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 661002b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 661102b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 661202b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 661302b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 661402b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 661502b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 661602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 661721debf25SMatthias Ringwald } 661821debf25SMatthias Ringwald } 6619cf38a091SMatthias Ringwald 6620cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 6621cf38a091SMatthias Ringwald if ((hci_stack->local_supported_commands[1] & (1 << 2)) == 0) { 6622cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 6623cf38a091SMatthias Ringwald } 6624cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 6625cf38a091SMatthias Ringwald // restart le resolving list update 6626cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 6627cf38a091SMatthias Ringwald } 6628cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 6629cf38a091SMatthias Ringwald } 663021debf25SMatthias Ringwald #endif 663121debf25SMatthias Ringwald 6632eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 663318976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 663418976c74SMatthias Ringwald hci_connection_t * conn; 663518976c74SMatthias Ringwald 663618976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 663718976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 663818976c74SMatthias Ringwald 66397d33cb26SMilanka Ringwald // setup Controller info 66407d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 66417d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 66427d33cb26SMilanka Ringwald 664318976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 664418976c74SMatthias Ringwald addr[5] = 0x01; 664518976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 664618976c74SMatthias Ringwald conn->con_handle = addr[5]; 664718976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 664818976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 66497d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 665018976c74SMatthias Ringwald 665118976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 665218976c74SMatthias Ringwald addr[5] = 0x02; 665318976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 665418976c74SMatthias Ringwald conn->con_handle = addr[5]; 665518976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 665618976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 66577d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 665818976c74SMatthias Ringwald 665918976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 666018976c74SMatthias Ringwald addr[5] = 0x03; 666118976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 666218976c74SMatthias Ringwald conn->con_handle = addr[5]; 666318976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 666418976c74SMatthias Ringwald conn->state = OPEN; 66657d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 666618976c74SMatthias Ringwald 666718976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 666818976c74SMatthias Ringwald addr[5] = 0x04; 666918976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 667018976c74SMatthias Ringwald conn->con_handle = addr[5]; 667118976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 667218976c74SMatthias Ringwald conn->state = OPEN; 66737d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 667418976c74SMatthias Ringwald 667518976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 667618976c74SMatthias Ringwald addr[5] = 0x05; 667718976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 667818976c74SMatthias Ringwald conn->con_handle = addr[5]; 667918976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 668018976c74SMatthias Ringwald conn->state = OPEN; 66817d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 66828046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 668318976c74SMatthias Ringwald } 668418976c74SMatthias Ringwald 6685eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 6686eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 6687eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 6688eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6689eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 6690eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6691eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 6692eddac615SMatthias Ringwald } 6693eddac615SMatthias Ringwald } 66941470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 66951470db0cSMatthias Ringwald hci_init_done(); 66961470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 66971470db0cSMatthias Ringwald } 6698eddac615SMatthias Ringwald #endif 6699