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 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH 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 56a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 57472a5742SMatthias Ringwald #include "gap.h" 5821debf25SMatthias Ringwald #include "ble/le_device_db.h" 5945c102fdSMatthias Ringwald #endif 6045c102fdSMatthias Ringwald 6193b8dc03Smatthias.ringwald #include <stdarg.h> 6293b8dc03Smatthias.ringwald #include <string.h> 635838a2edSMatthias Ringwald #include <inttypes.h> 647f2435e6Smatthias.ringwald 6516ece135SMatthias Ringwald #include "btstack_debug.h" 660e2df43fSMatthias Ringwald #include "btstack_event.h" 674a3574a1SMatthias Ringwald #include "btstack_linked_list.h" 684a3574a1SMatthias Ringwald #include "btstack_memory.h" 6961f37892SMatthias Ringwald #include "bluetooth_company_id.h" 701cfb383eSMatthias Ringwald #include "bluetooth_data_types.h" 714a3574a1SMatthias Ringwald #include "gap.h" 724a3574a1SMatthias Ringwald #include "hci.h" 734a3574a1SMatthias Ringwald #include "hci_cmd.h" 74d8905019Smatthias.ringwald #include "hci_dump.h" 751cfb383eSMatthias Ringwald #include "ad_parser.h" 7693b8dc03Smatthias.ringwald 772b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 782b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 792b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 802b838201SMatthias Ringwald #endif 812b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 822b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 832b838201SMatthias Ringwald #endif 842b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 852b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 862b838201SMatthias Ringwald #endif 872b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 882b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 892b838201SMatthias Ringwald #endif 902b838201SMatthias Ringwald #endif 911b0e3922Smatthias.ringwald 92aa81e641SMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) && defined(ENABLE_SCO_OVER_PCM) 93aa81e641SMatthias 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." 94aa81e641SMatthias Ringwald #endif 95aa81e641SMatthias Ringwald 961e20a53eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) && defined(HAVE_SCO_TRANSPORT) 971e20a53eSMatthias 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." 981e20a53eSMatthias Ringwald #endif 991e20a53eSMatthias Ringwald 100169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 10162473419SMatthias Ringwald 10262473419SMatthias Ringwald #ifndef HCI_RESET_RESEND_TIMEOUT_MS 103659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 10462473419SMatthias Ringwald #endif 105ee091cf1Smatthias.ringwald 1061cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 1071cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 1081cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 1091cfb383eSMatthias Ringwald #endif 1101cfb383eSMatthias Ringwald 111f5875de5SMatthias Ringwald // GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested 112f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MIN 0x01 113f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MAX 0x30 114beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_IDLE 0x00 115beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W4_ACTIVE 0x80 116beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_ACTIVE 0x81 117beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W2_CANCEL 0x82 118beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W4_CANCELLED 0x83 119f5875de5SMatthias Ringwald 120b7f1ee76SMatthias Ringwald // GAP Remote Name Request 121b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_IDLE 0 122b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W2_SEND 1 123b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W4_COMPLETE 2 124b7f1ee76SMatthias Ringwald 1250a51f88bSMatthias Ringwald // GAP Pairing 1260a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_IDLE 0 1270a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN 1 1280a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE 2 1290a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY 3 1300a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE 4 1310a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION 5 1320a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE 6 133cc15bb2cSMatthias Ringwald #define GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE 7 1340a51f88bSMatthias Ringwald 13525e46151SMatthias Ringwald // 1369ce37759SMatthias Ringwald // compact storage of relevant supported HCI Commands. 1379ce37759SMatthias Ringwald // X-Macro below provides enumeration and mapping table into the supported 13825e46151SMatthias Ringwald // commands bitmap (64 bytes) from HCI Read Local Supported Commands 13925e46151SMatthias Ringwald // 14025e46151SMatthias Ringwald 14125e46151SMatthias Ringwald // format: command name, byte offset, bit nr in 64-byte supported commands 14225e46151SMatthias Ringwald #define SUPPORTED_HCI_COMMANDS \ 1439ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES , 2, 5) \ 14425e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE , 10, 4) \ 1459ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE , 14, 7) \ 14625e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, 18, 3) \ 1479ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE , 20, 4) \ 1489ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED , 24, 6) \ 1499ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY, 32, 1) \ 1509ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST , 32, 3) \ 1519ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND , 32, 6) \ 15225e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH, 34, 0) \ 15325e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH , 35, 3) \ 15425e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE , 35, 1) \ 1559ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_SET_DEFAULT_PHY , 35, 5) \ 156*63671e69SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE , 36, 6) \ 15725e46151SMatthias Ringwald 15825e46151SMatthias Ringwald // enumerate supported commands 15925e46151SMatthias Ringwald #define X(name, offset, bit) name, 16025e46151SMatthias Ringwald enum { 16125e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 16225e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS_COUNT 16325e46151SMatthias Ringwald }; 16425e46151SMatthias Ringwald #undef X 16525e46151SMatthias Ringwald 16625e46151SMatthias Ringwald // assert supported hci commands bitmap fits into provided storage 16725e46151SMatthias Ringwald #if SUPPORTED_HCI_COMMANDS_COUNT > 16 16825e46151SMatthias Ringwald #error "Storage for supported HCI commands too small" 16925e46151SMatthias Ringwald #endif 17025e46151SMatthias Ringwald 171b83d5eabSMatthias Ringwald // prototypes 17235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 173758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 17435454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled); 17535454696SMatthias Ringwald static int hci_local_ssp_activated(void); 17620dcdd22SMatthias Ringwald static bool hci_remote_ssp_supported(hci_con_handle_t con_handle); 17767aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection); 17835454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 17935454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 180a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 18135454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 182ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 18396a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 18452db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 1859784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size); 18652db98b2SMatthias Ringwald #endif 1871cfb383eSMatthias Ringwald 1887586ee35S[email protected] static int hci_power_control_on(void); 1897586ee35S[email protected] static void hci_power_control_off(void); 1906da48142SSean Wilson static void hci_state_reset(void); 191685ec254SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds); 192fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void); 193fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 194b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 195b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 196b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 197b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 198b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 19995d71764SMatthias Ringwald static void hci_run(void); 20095d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 2016a5ffed8SMatthias Ringwald 2026a5ffed8SMatthias Ringwald #ifdef ENABLE_CLASSIC 203f234b250SMatthias Ringwald static int hci_have_usb_transport(void); 204dbe0d85cSMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection); 2056a5ffed8SMatthias Ringwald #endif 2065d509858SMatthias Ringwald 207a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 208e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 20939677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 210384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size); 211667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address); 21283dc1a98SMatthias Ringwald static void hci_whitelist_free(void); 2139c77c9dbSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void); 21473799937SMatthias Ringwald static bool hci_run_general_gap_le(void); 2155d509858SMatthias Ringwald #endif 216d70217a2SMatthias Ringwald #endif 217758b46ceSmatthias.ringwald 21806b35ec0Smatthias.ringwald // the STACK is here 2193a9fb326S[email protected] #ifndef HAVE_MALLOC 2203a9fb326S[email protected] static hci_stack_t hci_stack_static; 2213a9fb326S[email protected] #endif 2223a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 22316833f0aSmatthias.ringwald 2241c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 22563168530SMatthias Ringwald // default name 22663168530SMatthias Ringwald static const char * default_classic_name = "BTstack 00:00:00:00:00:00"; 22763168530SMatthias Ringwald 22866fb9560S[email protected] // test helper 22966fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 2301c9e5e9dSMatthias Ringwald #endif 23166fb9560S[email protected] 232aee5d5c1SMatthias Ringwald // reset connection state on create and on reconnect 233aee5d5c1SMatthias Ringwald // don't overwrite addr, con handle, role 234aee5d5c1SMatthias Ringwald static void hci_connection_init(hci_connection_t * conn){ 2358daf94bcSMatthias Ringwald conn->authentication_flags = AUTH_FLAG_NONE; 23696a45072S[email protected] conn->bonding_flags = 0; 23796a45072S[email protected] conn->requested_security_level = LEVEL_0; 23852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 23988a03c8dSMatthias Ringwald conn->request_role = HCI_ROLE_INVALID; 240140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = 0xffff; 241965a4ccfSMatthias Ringwald conn->qos_service_type = HCI_SERVICE_TYPE_INVALID; 242e9f98c4aSMatthias Ringwald conn->link_key_type = INVALID_LINK_KEY; 24391a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 24491a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 24596a45072S[email protected] hci_connection_timestamp(conn); 24652db98b2SMatthias Ringwald #endif 24796a45072S[email protected] conn->acl_recombination_length = 0; 24896a45072S[email protected] conn->acl_recombination_pos = 0; 249ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 250760b20efSMatthias Ringwald 251da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 252b90f6e0aSMatthias Ringwald #ifdef ENABLE_BLE 253b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = 0xff; 254b90f6e0aSMatthias Ringwald #endif 2550f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 2560f3b27c5SMatthias Ringwald conn->le_max_tx_octets = 27; 2570f3b27c5SMatthias Ringwald #endif 258cb439cf1SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 259cb439cf1SMatthias Ringwald conn->classic_oob_c_192 = NULL; 260cb439cf1SMatthias Ringwald conn->classic_oob_r_192 = NULL; 261cb439cf1SMatthias Ringwald conn->classic_oob_c_256 = NULL; 262cb439cf1SMatthias Ringwald conn->classic_oob_r_256 = NULL; 263cb439cf1SMatthias Ringwald #endif 264aee5d5c1SMatthias Ringwald } 265aee5d5c1SMatthias Ringwald 266aee5d5c1SMatthias Ringwald /** 267aee5d5c1SMatthias Ringwald * create connection for given address 268aee5d5c1SMatthias Ringwald * 269aee5d5c1SMatthias Ringwald * @return connection OR NULL, if no memory left 270aee5d5c1SMatthias Ringwald */ 271aee5d5c1SMatthias Ringwald static hci_connection_t * create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 272aee5d5c1SMatthias Ringwald log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 273aee5d5c1SMatthias Ringwald 274aee5d5c1SMatthias Ringwald hci_connection_t * conn = btstack_memory_hci_connection_get(); 275aee5d5c1SMatthias Ringwald if (!conn) return NULL; 276aee5d5c1SMatthias Ringwald hci_connection_init(conn); 277aee5d5c1SMatthias Ringwald 278aee5d5c1SMatthias Ringwald bd_addr_copy(conn->address, addr); 279aee5d5c1SMatthias Ringwald conn->address_type = addr_type; 280aee5d5c1SMatthias Ringwald conn->con_handle = HCI_CON_HANDLE_INVALID; 281aee5d5c1SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 282aee5d5c1SMatthias Ringwald 283665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 284aee5d5c1SMatthias Ringwald 28596a45072S[email protected] return conn; 28696a45072S[email protected] } 28766fb9560S[email protected] 288da886c03S[email protected] 289da886c03S[email protected] /** 290da886c03S[email protected] * get le connection parameter range 291da886c03S[email protected] * 292da886c03S[email protected] * @return le connection parameter range struct 293da886c03S[email protected] */ 2944ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 2954ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 296da886c03S[email protected] } 297da886c03S[email protected] 298da886c03S[email protected] /** 299da886c03S[email protected] * set le connection parameter range 300da886c03S[email protected] * 301da886c03S[email protected] */ 302da886c03S[email protected] 3034ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 3044ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 305da886c03S[email protected] } 306da886c03S[email protected] 307da886c03S[email protected] /** 30873cd8a2aSMatthias Ringwald * @brief Test if connection parameters are inside in existing rage 30973cd8a2aSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 31073cd8a2aSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 31173cd8a2aSMatthias Ringwald * @param conn_latency 31273cd8a2aSMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3136b65794dSMilanka Ringwald * @return 1 if included 31473cd8a2aSMatthias Ringwald */ 31573cd8a2aSMatthias 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){ 31673cd8a2aSMatthias Ringwald if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0; 31773cd8a2aSMatthias Ringwald if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0; 31873cd8a2aSMatthias Ringwald 31973cd8a2aSMatthias Ringwald if (le_conn_latency < existing_range->le_conn_latency_min) return 0; 32073cd8a2aSMatthias Ringwald if (le_conn_latency > existing_range->le_conn_latency_max) return 0; 32173cd8a2aSMatthias Ringwald 32273cd8a2aSMatthias Ringwald if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0; 32373cd8a2aSMatthias Ringwald if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0; 32473cd8a2aSMatthias Ringwald 32573cd8a2aSMatthias Ringwald return 1; 32673cd8a2aSMatthias Ringwald } 32773cd8a2aSMatthias Ringwald 32873cd8a2aSMatthias Ringwald /** 3292b6ab3e6SMatthias Ringwald * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 3302b6ab3e6SMatthias Ringwald * @note: default: 1 3312b6ab3e6SMatthias Ringwald * @param max_peripheral_connections 3322b6ab3e6SMatthias Ringwald */ 333d4e4907bSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3342b6ab3e6SMatthias Ringwald void gap_set_max_number_peripheral_connections(int max_peripheral_connections){ 3352b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = max_peripheral_connections; 3362b6ab3e6SMatthias Ringwald } 337d4e4907bSMatthias Ringwald #endif 3382b6ab3e6SMatthias Ringwald 3392b6ab3e6SMatthias Ringwald /** 340da886c03S[email protected] * get hci connections iterator 341da886c03S[email protected] * 342da886c03S[email protected] * @return hci connections iterator 343da886c03S[email protected] */ 344da886c03S[email protected] 345665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 346665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 347da886c03S[email protected] } 348da886c03S[email protected] 34997addcc5Smatthias.ringwald /** 350ee091cf1Smatthias.ringwald * get connection for a given handle 351ee091cf1Smatthias.ringwald * 352ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 353ee091cf1Smatthias.ringwald */ 3545061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 355665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 356665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 357665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 358665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 3593ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 360da886c03S[email protected] return item; 361ee091cf1Smatthias.ringwald } 362ee091cf1Smatthias.ringwald } 363ee091cf1Smatthias.ringwald return NULL; 364ee091cf1Smatthias.ringwald } 365ee091cf1Smatthias.ringwald 36696a45072S[email protected] /** 36796a45072S[email protected] * get connection for given address 36896a45072S[email protected] * 36996a45072S[email protected] * @return connection OR NULL, if not found 37096a45072S[email protected] */ 371667ba9d1SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 372665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 373665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 374665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 375665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 37696a45072S[email protected] if (connection->address_type != addr_type) continue; 37796a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 37862bda3fbS[email protected] return connection; 37962bda3fbS[email protected] } 38062bda3fbS[email protected] return NULL; 38162bda3fbS[email protected] } 38262bda3fbS[email protected] 3833e5e0926SMatthias Ringwald inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 3843e5e0926SMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 3853e5e0926SMatthias Ringwald } 38652db98b2SMatthias Ringwald 387228e430cSMatthias Ringwald inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 388228e430cSMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 389228e430cSMatthias Ringwald } 390228e430cSMatthias Ringwald 39152db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 39252db98b2SMatthias Ringwald 393ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 394ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 395ee752bb8SMatthias Ringwald int connections = 0; 396ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 397ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 398ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 399ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 400ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 401ee752bb8SMatthias Ringwald connections++; 402ee752bb8SMatthias Ringwald } 403ee752bb8SMatthias Ringwald return connections; 404ee752bb8SMatthias Ringwald } 405ee752bb8SMatthias Ringwald #endif 406ee752bb8SMatthias Ringwald 407ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 40891a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 409aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 410528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 411c785ef68Smatthias.ringwald // connections might be timed out 412c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 413c785ef68Smatthias.ringwald } 414f316a845SMatthias Ringwald #else 415c1ab6cc1SMatthias Ringwald if (btstack_run_loop_get_time_ms() > (connection->timestamp + HCI_CONNECTION_TIMEOUT_MS)){ 4165f26aadcSMatthias Ringwald // connections might be timed out 4175f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 4185f26aadcSMatthias Ringwald } 4195f26aadcSMatthias Ringwald #endif 420c785ef68Smatthias.ringwald } 421ee091cf1Smatthias.ringwald 422ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 423aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 424528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 425f316a845SMatthias Ringwald #else 426528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 4275f26aadcSMatthias Ringwald #endif 428ee091cf1Smatthias.ringwald } 429ee091cf1Smatthias.ringwald 43043bfb1bdSmatthias.ringwald /** 43180ca58a0Smatthias.ringwald * add authentication flags and reset timer 43296a45072S[email protected] * @note: assumes classic connection 4332e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 4347fde4af9Smatthias.ringwald */ 4357fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 4367fde4af9Smatthias.ringwald bd_addr_t addr; 437724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 438f16129ceSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 4397fde4af9Smatthias.ringwald if (conn) { 44028ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 44180ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 4427fde4af9Smatthias.ringwald } 4437fde4af9Smatthias.ringwald } 4447fde4af9Smatthias.ringwald 4451714cbbdSMatthias Ringwald static bool hci_pairing_active(hci_connection_t * hci_connection){ 4468daf94bcSMatthias Ringwald return (hci_connection->authentication_flags & AUTH_FLAG_PAIRING_ACTIVE_MASK) != 0; 4471714cbbdSMatthias Ringwald } 4481714cbbdSMatthias Ringwald 4491714cbbdSMatthias Ringwald static void hci_pairing_started(hci_connection_t * hci_connection, bool ssp){ 4501714cbbdSMatthias Ringwald if (hci_pairing_active(hci_connection)) return; 4511714cbbdSMatthias Ringwald if (ssp){ 4528daf94bcSMatthias Ringwald hci_connection->authentication_flags |= AUTH_FLAG_SSP_PAIRING_ACTIVE; 4531714cbbdSMatthias Ringwald } else { 4548daf94bcSMatthias Ringwald hci_connection->authentication_flags |= AUTH_FLAG_LEGACY_PAIRING_ACTIVE; 4551714cbbdSMatthias Ringwald } 4561714cbbdSMatthias Ringwald // if we are initiator, we have sent an HCI Authenticate Request 4571714cbbdSMatthias Ringwald bool initiator = (hci_connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0; 4581714cbbdSMatthias Ringwald 4595a561920SMatthias Ringwald // if we are responder, use minimal service security level as required level 4605a561920SMatthias Ringwald if (!initiator){ 461acadfdd0SMatthias Ringwald hci_connection->requested_security_level = (gap_security_level_t) btstack_max((uint32_t) hci_connection->requested_security_level, (uint32_t) hci_stack->gap_minimal_service_security_level); 4625a561920SMatthias Ringwald } 4635a561920SMatthias Ringwald 4645a561920SMatthias Ringwald log_info("pairing started, ssp %u, initiator %u, requested level %u", (int) ssp, (int) initiator, hci_connection->requested_security_level); 46577208d90SMatthias Ringwald 46677208d90SMatthias Ringwald uint8_t event[12]; 46777208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_STARTED; 46877208d90SMatthias Ringwald event[1] = 10; 469bfaf6993SMatthias Ringwald little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle); 4704159a80bSMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[4]); 47177208d90SMatthias Ringwald event[10] = (uint8_t) ssp; 47277208d90SMatthias Ringwald event[11] = (uint8_t) initiator; 47377208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4741714cbbdSMatthias Ringwald } 4751714cbbdSMatthias Ringwald 4761714cbbdSMatthias Ringwald static void hci_pairing_complete(hci_connection_t * hci_connection, uint8_t status){ 4773c439ac4SMatthias Ringwald hci_connection->requested_security_level = LEVEL_0; 4781714cbbdSMatthias Ringwald if (!hci_pairing_active(hci_connection)) return; 4798daf94bcSMatthias Ringwald hci_connection->authentication_flags &= ~AUTH_FLAG_PAIRING_ACTIVE_MASK; 4801e7371deSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 481cb439cf1SMatthias Ringwald hci_connection->classic_oob_c_192 = NULL; 482cb439cf1SMatthias Ringwald hci_connection->classic_oob_r_192 = NULL; 483cb439cf1SMatthias Ringwald hci_connection->classic_oob_c_256 = NULL; 484cb439cf1SMatthias Ringwald hci_connection->classic_oob_r_256 = NULL; 4851e7371deSMatthias Ringwald #endif 4861714cbbdSMatthias Ringwald log_info("pairing complete, status %02x", status); 48777208d90SMatthias Ringwald 4883176c896SMatthias Ringwald uint8_t event[11]; 48977208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_COMPLETE; 49077208d90SMatthias Ringwald event[1] = 9; 491bfaf6993SMatthias Ringwald little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle); 4924159a80bSMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[4]); 49377208d90SMatthias Ringwald event[10] = status; 49477208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 4951714cbbdSMatthias Ringwald } 4961714cbbdSMatthias Ringwald 49720dcdd22SMatthias Ringwald bool hci_authentication_active_for_handle(hci_con_handle_t handle){ 4985061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 49920dcdd22SMatthias Ringwald if (!conn) return false; 50020dcdd22SMatthias Ringwald return hci_pairing_active(conn); 50180ca58a0Smatthias.ringwald } 50280ca58a0Smatthias.ringwald 50315a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 50455597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 5052bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 506a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 507c12e46e7Smatthias.ringwald } 50855597469SMatthias Ringwald 50955597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 51055597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 5112bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 51255597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 513c12e46e7Smatthias.ringwald } 5141b6fb31bSMatthias Ringwald 515e8ad470fSMatthias Ringwald bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type){ 516e8ad470fSMatthias Ringwald if (!hci_stack->link_key_db) return false; 51776b0318eSMatthias Ringwald int result = hci_stack->link_key_db->get_link_key(addr, link_key, type) != 0; 51876b0318eSMatthias Ringwald log_info("link key for %s available %u, type %u", bd_addr_to_str(addr), result, (int) *type); 51976b0318eSMatthias Ringwald return result; 520e8ad470fSMatthias Ringwald } 521e8ad470fSMatthias Ringwald 522ceecb9d9SMatthias Ringwald void gap_delete_all_link_keys(void){ 523ceecb9d9SMatthias Ringwald bd_addr_t addr; 524ceecb9d9SMatthias Ringwald link_key_t link_key; 525ceecb9d9SMatthias Ringwald link_key_type_t type; 526ceecb9d9SMatthias Ringwald btstack_link_key_iterator_t it; 527ceecb9d9SMatthias Ringwald int ok = gap_link_key_iterator_init(&it); 528ceecb9d9SMatthias Ringwald if (!ok) { 529ceecb9d9SMatthias Ringwald log_error("could not initialize iterator"); 530ceecb9d9SMatthias Ringwald return; 531ceecb9d9SMatthias Ringwald } 532ceecb9d9SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 533ceecb9d9SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 534ceecb9d9SMatthias Ringwald } 535ceecb9d9SMatthias Ringwald gap_link_key_iterator_done(&it); 536ceecb9d9SMatthias Ringwald } 537ceecb9d9SMatthias Ringwald 5381b6fb31bSMatthias Ringwald int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){ 5391b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 5401b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db->iterator_init) return 0; 5411b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_init(it); 5421b6fb31bSMatthias Ringwald } 5431b6fb31bSMatthias 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){ 5441b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 5451b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type); 5461b6fb31bSMatthias Ringwald } 5471b6fb31bSMatthias Ringwald void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){ 5481b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return; 5491b6fb31bSMatthias Ringwald hci_stack->link_key_db->iterator_done(it); 5501b6fb31bSMatthias Ringwald } 55135454696SMatthias Ringwald #endif 552c12e46e7Smatthias.ringwald 553eb8076ddSMatthias Ringwald static bool hci_is_le_connection_type(bd_addr_type_t address_type){ 554eb8076ddSMatthias Ringwald switch (address_type){ 555ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 556ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 557ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_PUBLIC: 558ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_RANDOM: 559eb8076ddSMatthias Ringwald return true; 560ce41473eSMatthias Ringwald default: 561eb8076ddSMatthias Ringwald return false; 562ce41473eSMatthias Ringwald } 5630bf6344aS[email protected] } 5640bf6344aS[email protected] 565eb8076ddSMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 566eb8076ddSMatthias Ringwald return hci_is_le_connection_type(connection->address_type); 567eb8076ddSMatthias Ringwald } 568eb8076ddSMatthias Ringwald 5697fde4af9Smatthias.ringwald /** 57043bfb1bdSmatthias.ringwald * count connections 57143bfb1bdSmatthias.ringwald */ 57240d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 57356c253c9Smatthias.ringwald int count = 0; 574665d90f2SMatthias Ringwald btstack_linked_item_t *it; 575a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL ; it = it->next){ 57615a27967SMatthias Ringwald count++; 57715a27967SMatthias Ringwald } 57843bfb1bdSmatthias.ringwald return count; 57943bfb1bdSmatthias.ringwald } 580c8e4258aSmatthias.ringwald 5818f733c6fSMatthias Ringwald uint16_t hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 582ee303eddS[email protected] 583f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 584f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 585ee303eddS[email protected] 586665d90f2SMatthias Ringwald btstack_linked_item_t *it; 587a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 588998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 589ce41473eSMatthias Ringwald if (hci_is_le_connection(connection)){ 590ce41473eSMatthias Ringwald num_packets_sent_le += connection->num_packets_sent; 591ce41473eSMatthias Ringwald } 592f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 593ce41473eSMatthias Ringwald num_packets_sent_classic += connection->num_packets_sent; 594ee303eddS[email protected] } 595ee303eddS[email protected] } 596d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 597ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 598ee303eddS[email protected] int free_slots_le = 0; 599ee303eddS[email protected] 600ee303eddS[email protected] if (free_slots_classic < 0){ 6019da54300S[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); 602998906cdSmatthias.ringwald return 0; 603998906cdSmatthias.ringwald } 604ee303eddS[email protected] 605ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 606ee303eddS[email protected] // if we have LE slots, they are used 607ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 608ee303eddS[email protected] if (free_slots_le < 0){ 6099da54300S[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); 610ee303eddS[email protected] return 0; 611998906cdSmatthias.ringwald } 612ee303eddS[email protected] } else { 613ee303eddS[email protected] // otherwise, classic slots are used for LE, too 614ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 615ee303eddS[email protected] if (free_slots_classic < 0){ 6169da54300S[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); 617ee303eddS[email protected] return 0; 618ee303eddS[email protected] } 619ee303eddS[email protected] } 620ee303eddS[email protected] 621ee303eddS[email protected] switch (address_type){ 622ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 6232125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 624ee303eddS[email protected] return 0; 625ee303eddS[email protected] 626f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6278f733c6fSMatthias Ringwald return (uint16_t) free_slots_classic; 628ee303eddS[email protected] 629ee303eddS[email protected] default: 6308f733c6fSMatthias Ringwald if (hci_stack->le_acl_packets_total_num > 0){ 6318f733c6fSMatthias Ringwald return (uint16_t) free_slots_le; 632ee303eddS[email protected] } 6338f733c6fSMatthias Ringwald return (uint16_t) free_slots_classic; 634cb00d3aaS[email protected] } 635998906cdSmatthias.ringwald } 636998906cdSmatthias.ringwald 6372125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 6382125de09SMatthias Ringwald // get connection type 6392125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 6402125de09SMatthias Ringwald if (!connection){ 6412125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 6422125de09SMatthias Ringwald return 0; 6432125de09SMatthias Ringwald } 6442125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 6452125de09SMatthias Ringwald } 6462125de09SMatthias Ringwald 64735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 648701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 649f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 650665d90f2SMatthias Ringwald btstack_linked_item_t *it; 651760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 652760b20efSMatthias Ringwald // explicit flow control 653665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 654e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 655ce41473eSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 656ce41473eSMatthias Ringwald num_sco_packets_sent += connection->num_packets_sent; 657e35edcc1S[email protected] } 658e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 659701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 66044d0e3d5S[email protected] return 0; 66144d0e3d5S[email protected] } 662e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 66349205f5dSMatthias Ringwald } else { 66449205f5dSMatthias Ringwald // implicit flow control -- TODO 6656f28d2eeSMatthias Ringwald int num_ready = 0; 6666f28d2eeSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 6676f28d2eeSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 6686f28d2eeSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 6696f28d2eeSMatthias Ringwald if (connection->sco_tx_ready == 0) continue; 6706f28d2eeSMatthias Ringwald num_ready++; 67149205f5dSMatthias Ringwald } 6726f28d2eeSMatthias Ringwald return num_ready; 6736f28d2eeSMatthias Ringwald } 674e35edcc1S[email protected] } 67535454696SMatthias Ringwald #endif 67644d0e3d5S[email protected] 6772b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 6782b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 679ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 680ac928cc2S[email protected] 681ac928cc2S[email protected] // check for async hci transport implementations 682ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 683ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 684ac928cc2S[email protected] return 0; 685ac928cc2S[email protected] } 686ac928cc2S[email protected] } 6872b838201SMatthias Ringwald return 1; 6882b838201SMatthias Ringwald } 689ac928cc2S[email protected] 6902b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 6911972f31fSMatthias Ringwald bool hci_can_send_command_packet_now(void){ 6921972f31fSMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return false; 6934ea43905SMatthias Ringwald return hci_stack->num_cmd_packets > 0u; 694ac928cc2S[email protected] } 695ac928cc2S[email protected] 6969d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 697ac928cc2S[email protected] // check for async hci transport implementations 6981972f31fSMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return true; 6999d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 700ac928cc2S[email protected] } 7019d04d3a7SMatthias Ringwald 7021972f31fSMatthias Ringwald static bool hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 7031972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false; 7049d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 705ac928cc2S[email protected] } 7069d04d3a7SMatthias Ringwald 7071972f31fSMatthias Ringwald bool hci_can_send_acl_le_packet_now(void){ 7081972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 7099d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 7109d04d3a7SMatthias Ringwald } 7119d04d3a7SMatthias Ringwald 7121972f31fSMatthias Ringwald bool hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 7131972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false; 714e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 715ac928cc2S[email protected] } 716ac928cc2S[email protected] 7171972f31fSMatthias Ringwald bool hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 7181972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 719ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 7206b4af23dS[email protected] } 7216b4af23dS[email protected] 72235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 7231972f31fSMatthias Ringwald bool hci_can_send_acl_classic_packet_now(void){ 7241972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 725f16129ceSMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_ACL); 72635454696SMatthias Ringwald } 72735454696SMatthias Ringwald 7281972f31fSMatthias Ringwald bool hci_can_send_prepared_sco_packet_now(void){ 7291972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return false; 730f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 731f234b250SMatthias Ringwald return hci_stack->sco_can_send_now; 732f234b250SMatthias Ringwald } else { 733701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 73444d0e3d5S[email protected] } 735f234b250SMatthias Ringwald } 73644d0e3d5S[email protected] 7371972f31fSMatthias Ringwald bool hci_can_send_sco_packet_now(void){ 7381972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 739701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 74044d0e3d5S[email protected] } 74144d0e3d5S[email protected] 742d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 743d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 744d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 745d057580eSMatthias Ringwald } 74635454696SMatthias Ringwald #endif 747d057580eSMatthias Ringwald 74895d71764SMatthias Ringwald // used for internal checks in l2cap.c 74902c7fc01SMatthias Ringwald bool hci_is_packet_buffer_reserved(void){ 750c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 751c8b9416aS[email protected] } 752c8b9416aS[email protected] 7536b65794dSMilanka Ringwald // reserves outgoing packet buffer. 7546b65794dSMilanka Ringwald // @return 1 if successful 755cafc12e8SMatthias Ringwald bool hci_reserve_packet_buffer(void){ 7569d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 7579d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 758cafc12e8SMatthias Ringwald return false; 7599d14b626S[email protected] } 76002c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = true; 761cafc12e8SMatthias Ringwald return true; 7626b4af23dS[email protected] } 7636b4af23dS[email protected] 76468a0fcf7S[email protected] void hci_release_packet_buffer(void){ 76502c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 76668a0fcf7S[email protected] } 76768a0fcf7S[email protected] 7686b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 7697f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 7706b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 7716b4af23dS[email protected] } 7726b4af23dS[email protected] 7733e2050f7SMatthias Ringwald static uint8_t hci_send_acl_packet_fragments(hci_connection_t *connection){ 774452cf3bbS[email protected] 775452cf3bbS[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); 776452cf3bbS[email protected] 777452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 778452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 7794ea43905SMatthias Ringwald if (hci_is_le_connection(connection) && (hci_stack->le_data_packets_length > 0u)){ 780452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 781452cf3bbS[email protected] } 782452cf3bbS[email protected] 7830f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 784fcf88f47SMatthias Ringwald if (hci_is_le_connection(connection) && (connection->le_max_tx_octets < max_acl_data_packet_length)){ 7850f3b27c5SMatthias Ringwald max_acl_data_packet_length = connection->le_max_tx_octets; 7860f3b27c5SMatthias Ringwald } 7870f3b27c5SMatthias Ringwald #endif 788452cf3bbS[email protected] 789d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 790d999b54eSMatthias Ringwald 7913e2050f7SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 792452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 793ff3cc4a5SMatthias Ringwald while (true){ 794452cf3bbS[email protected] 795d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 796d999b54eSMatthias Ringwald 797452cf3bbS[email protected] // get current data 7984ea43905SMatthias Ringwald const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u; 799452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 8001979f09cSMatthias Ringwald bool more_fragments = false; 801452cf3bbS[email protected] 802452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 803452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 8041979f09cSMatthias Ringwald more_fragments = true; 805452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 806452cf3bbS[email protected] } 807452cf3bbS[email protected] 808452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 8094ea43905SMatthias Ringwald if (acl_header_pos > 0u){ 810f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 8114ea43905SMatthias Ringwald handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u); 812f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 813452cf3bbS[email protected] } 814452cf3bbS[email protected] 815452cf3bbS[email protected] // update header len 8164ea43905SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length); 817452cf3bbS[email protected] 818452cf3bbS[email protected] // count packet 819ce41473eSMatthias Ringwald connection->num_packets_sent++; 8201979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments); 821d999b54eSMatthias Ringwald 822d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 823d999b54eSMatthias Ringwald if (more_fragments){ 824d999b54eSMatthias Ringwald // update start of next fragment to send 825d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 826d999b54eSMatthias Ringwald } else { 827d999b54eSMatthias Ringwald // done 828d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 829d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 830d999b54eSMatthias Ringwald } 831452cf3bbS[email protected] 832452cf3bbS[email protected] // send packet 833452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 834452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 8355bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 83681d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 1; 8373e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 8383e2050f7SMatthias Ringwald if (err != 0){ 8393e2050f7SMatthias Ringwald // no error from HCI Transport expected 8403e2050f7SMatthias Ringwald status = ERROR_CODE_HARDWARE_FAILURE; 8413e2050f7SMatthias Ringwald } 842452cf3bbS[email protected] 8431979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments); 844d999b54eSMatthias Ringwald 845452cf3bbS[email protected] // done yet? 846452cf3bbS[email protected] if (!more_fragments) break; 847452cf3bbS[email protected] 848452cf3bbS[email protected] // can send more? 8493e2050f7SMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return status; 850452cf3bbS[email protected] } 851452cf3bbS[email protected] 852d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 853452cf3bbS[email protected] 854d051460cS[email protected] // release buffer now for synchronous transport 855203bace6S[email protected] if (hci_transport_synchronous()){ 85681d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 857452cf3bbS[email protected] hci_release_packet_buffer(); 858fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 859452cf3bbS[email protected] } 860452cf3bbS[email protected] 8613e2050f7SMatthias Ringwald return status; 862452cf3bbS[email protected] } 863452cf3bbS[email protected] 864826f7347S[email protected] // pre: caller has reserved the packet buffer 8653e2050f7SMatthias Ringwald uint8_t hci_send_acl_packet_buffer(int size){ 8663e2050f7SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 867826f7347S[email protected] 868d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 869d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 870d713a683S[email protected] 871826f7347S[email protected] // check for free places on Bluetooth module 872d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 873826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 87497b61c7bS[email protected] hci_release_packet_buffer(); 875068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 87697b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 87797b61c7bS[email protected] } 8786218e6f1Smatthias.ringwald 8795061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 88097b61c7bS[email protected] if (!connection) { 8815fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 88297b61c7bS[email protected] hci_release_packet_buffer(); 883068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 8843e2050f7SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 88597b61c7bS[email protected] } 88652db98b2SMatthias Ringwald 88752db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 88856cf178bSmatthias.ringwald hci_connection_timestamp(connection); 88952db98b2SMatthias Ringwald #endif 89056cf178bSmatthias.ringwald 891452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 8927856c818Smatthias.ringwald 893452cf3bbS[email protected] // setup data 894452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 895452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 8966218e6f1Smatthias.ringwald 897452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 898ee091cf1Smatthias.ringwald } 899ee091cf1Smatthias.ringwald 90035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 90144d0e3d5S[email protected] // pre: caller has reserved the packet buffer 9023e2050f7SMatthias Ringwald uint8_t hci_send_sco_packet_buffer(int size){ 9033e2050f7SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 90444d0e3d5S[email protected] 90544d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 9064b3e1e19SMatthias Ringwald 9074b3e1e19SMatthias Ringwald // skip checks in loopback mode 9084b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 90944d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 91044d0e3d5S[email protected] 91144d0e3d5S[email protected] // check for free places on Bluetooth module 912701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 913cbf638a9SMatthias Ringwald log_error("hci_send_sco_packet_buffer called but no free SCO buffers on controller"); 91444d0e3d5S[email protected] hci_release_packet_buffer(); 915068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 91644d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 91744d0e3d5S[email protected] } 91844d0e3d5S[email protected] 919e35edcc1S[email protected] // track send packet in connection struct 920e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 921e35edcc1S[email protected] if (!connection) { 922e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 923e35edcc1S[email protected] hci_release_packet_buffer(); 924068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 9253e2050f7SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 926e35edcc1S[email protected] } 927f234b250SMatthias Ringwald 928f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 929f234b250SMatthias Ringwald // token used 9301972f31fSMatthias Ringwald hci_stack->sco_can_send_now = false; 931f234b250SMatthias Ringwald } else { 932760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 933ce41473eSMatthias Ringwald connection->num_packets_sent++; 9346f28d2eeSMatthias Ringwald } else { 935e4157653SMatthias Ringwald connection->sco_tx_ready--; 936760b20efSMatthias Ringwald } 9374b3e1e19SMatthias Ringwald } 938f234b250SMatthias Ringwald } 93944d0e3d5S[email protected] 94044d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 941543e835cSMatthias Ringwald 94243149fc9SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 94343149fc9SMatthias Ringwald hci_stack->sco_transport->send_packet(packet, size); 94443149fc9SMatthias Ringwald hci_release_packet_buffer(); 94543149fc9SMatthias Ringwald hci_emit_transport_packet_sent(); 94643149fc9SMatthias Ringwald 94743149fc9SMatthias Ringwald return 0; 94843149fc9SMatthias Ringwald #else 94943149fc9SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 950543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 951543e835cSMatthias Ringwald hci_release_packet_buffer(); 952fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 953543e835cSMatthias Ringwald } 954543e835cSMatthias Ringwald 9553e2050f7SMatthias Ringwald if (err != 0){ 9563e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 9573e2050f7SMatthias Ringwald } 9583e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 95943149fc9SMatthias Ringwald #endif 96044d0e3d5S[email protected] } 96135454696SMatthias Ringwald #endif 96244d0e3d5S[email protected] 963c3b46f5aSMatthias Ringwald static void acl_handler(uint8_t *packet, uint16_t size){ 964e76a89eeS[email protected] 9657856c818Smatthias.ringwald // get info 9667856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 9675061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 9687856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 9697856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 9707856c818Smatthias.ringwald 9717856c818Smatthias.ringwald // ignore non-registered handle 9727856c818Smatthias.ringwald if (!conn){ 973c3b46f5aSMatthias Ringwald log_error("acl_handler called with non-registered handle %u!" , con_handle); 9747856c818Smatthias.ringwald return; 9757856c818Smatthias.ringwald } 9767856c818Smatthias.ringwald 977e76a89eeS[email protected] // assert packet is complete 9784ea43905SMatthias Ringwald if ((acl_length + 4u) != size){ 979c3b46f5aSMatthias Ringwald log_error("acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 980e76a89eeS[email protected] return; 981e76a89eeS[email protected] } 982e76a89eeS[email protected] 98352db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 9847856c818Smatthias.ringwald // update idle timestamp 9857856c818Smatthias.ringwald hci_connection_timestamp(conn); 98652db98b2SMatthias Ringwald #endif 9877856c818Smatthias.ringwald 9882b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 9892b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 9902b838201SMatthias Ringwald conn->num_packets_completed++; 9912b838201SMatthias Ringwald #endif 9922b838201SMatthias Ringwald 9937856c818Smatthias.ringwald // handle different packet types 9944ea43905SMatthias Ringwald switch (acl_flags & 0x03u) { 9957856c818Smatthias.ringwald 9967856c818Smatthias.ringwald case 0x01: // continuation fragment 9977856c818Smatthias.ringwald 9980ca847afS[email protected] // sanity checks 9994ea43905SMatthias Ringwald if (conn->acl_recombination_pos == 0u) { 10009da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 10017856c818Smatthias.ringwald return; 10027856c818Smatthias.ringwald } 10034ea43905SMatthias Ringwald if ((conn->acl_recombination_pos + acl_length) > (4u + HCI_ACL_BUFFER_SIZE)){ 10040ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 10050ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 10060ca847afS[email protected] conn->acl_recombination_pos = 0; 10070ca847afS[email protected] return; 10080ca847afS[email protected] } 10097856c818Smatthias.ringwald 10107856c818Smatthias.ringwald // append fragment payload (header already stored) 10116535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], 10126535961aSMatthias Ringwald &packet[4], acl_length); 10137856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 10147856c818Smatthias.ringwald 10157856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 10164ea43905SMatthias Ringwald if (conn->acl_recombination_pos >= (conn->acl_recombination_length + 4u + 4u)){ // pos already incl. ACL header 1017d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 10187856c818Smatthias.ringwald // reset recombination buffer 10197856c818Smatthias.ringwald conn->acl_recombination_length = 0; 10207856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 10217856c818Smatthias.ringwald } 10227856c818Smatthias.ringwald break; 10237856c818Smatthias.ringwald 10247856c818Smatthias.ringwald case 0x02: { // first fragment 10257856c818Smatthias.ringwald 102623a77e1aS[email protected] // sanity check 102723a77e1aS[email protected] if (conn->acl_recombination_pos) { 102823a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 102923a77e1aS[email protected] conn->acl_recombination_pos = 0; 103023a77e1aS[email protected] } 103123a77e1aS[email protected] 10327856c818Smatthias.ringwald // peek into L2CAP packet! 10337856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 10347856c818Smatthias.ringwald 10357856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 10364ea43905SMatthias Ringwald if (acl_length >= (l2cap_length + 4u)){ 10377856c818Smatthias.ringwald // forward fragment as L2CAP packet 10384ea43905SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4u); 10397856c818Smatthias.ringwald } else { 10400ca847afS[email protected] 10410ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 10420ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 10430ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 10440ca847afS[email protected] return; 10450ca847afS[email protected] } 10460ca847afS[email protected] 10477856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 10486535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 10494ea43905SMatthias Ringwald packet, acl_length + 4u); 10504ea43905SMatthias Ringwald conn->acl_recombination_pos = acl_length + 4u; 10517856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 10524ea43905SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2u, l2cap_length +4u); 10537856c818Smatthias.ringwald } 10547856c818Smatthias.ringwald break; 10557856c818Smatthias.ringwald 10567856c818Smatthias.ringwald } 10577856c818Smatthias.ringwald default: 1058c3b46f5aSMatthias Ringwald log_error( "acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 10597856c818Smatthias.ringwald return; 10607856c818Smatthias.ringwald } 106194ab26f8Smatthias.ringwald 106294ab26f8Smatthias.ringwald // execute main loop 106394ab26f8Smatthias.ringwald hci_run(); 106416833f0aSmatthias.ringwald } 106522909952Smatthias.ringwald 10661ab2dc58SMatthias Ringwald static void hci_connection_stop_timer(hci_connection_t * conn){ 10671ab2dc58SMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 10681ab2dc58SMatthias Ringwald #ifdef ENABLE_CLASSIC 10691ab2dc58SMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout_sco); 10701ab2dc58SMatthias Ringwald #endif 10711ab2dc58SMatthias Ringwald } 10721ab2dc58SMatthias Ringwald 107367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 10749da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 10753c4d4b90Smatthias.ringwald 1076b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1077cb70c5abSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(HAVE_SCO_TRANSPORT) 1078cb70c5abSMatthias Ringwald bd_addr_type_t addr_type = conn->address_type; 1079cb70c5abSMatthias Ringwald #endif 1080cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1081cb70c5abSMatthias Ringwald hci_con_handle_t con_handle = conn->con_handle; 1082ee752bb8SMatthias Ringwald #endif 1083b3264428SMatthias Ringwald #endif 1084ee752bb8SMatthias Ringwald 10851ab2dc58SMatthias Ringwald hci_connection_stop_timer(conn); 1086c785ef68Smatthias.ringwald 1087665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1088a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 10893c4d4b90Smatthias.ringwald 10903c4d4b90Smatthias.ringwald // now it's gone 1091c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 1092ee752bb8SMatthias Ringwald 1093b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1094034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1095ee752bb8SMatthias Ringwald // update SCO 1096cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->hci_transport != NULL) && (hci_stack->hci_transport->set_sco_config != NULL)){ 1097ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1098ee752bb8SMatthias Ringwald } 1099034e9b53SMatthias Ringwald #endif 1100cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1101cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->sco_transport != NULL)){ 1102cb70c5abSMatthias Ringwald hci_stack->sco_transport->close(con_handle); 1103cb70c5abSMatthias Ringwald } 1104cb70c5abSMatthias Ringwald #endif 1105b3264428SMatthias Ringwald #endif 1106c7e0c5f6Smatthias.ringwald } 1107c7e0c5f6Smatthias.ringwald 110835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 110935454696SMatthias Ringwald 11100c042179S[email protected] static const uint16_t packet_type_sizes[] = { 11118f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 11128f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 11138f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 11148f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 11158f8108aaSmatthias.ringwald }; 111665389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 111765389bfcS[email protected] 0, // 3 slot packets 111865389bfcS[email protected] 1, // 5 slot packets 111965389bfcS[email protected] 25, // EDR 2 mpbs 112065389bfcS[email protected] 26, // EDR 3 mbps 112165389bfcS[email protected] 39, // 3 slot EDR packts 112265389bfcS[email protected] 40, // 5 slot EDR packet 112365389bfcS[email protected] }; 112465389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 112565389bfcS[email protected] 0x0f00, // 3 slot packets 112665389bfcS[email protected] 0xf000, // 5 slot packets 112765389bfcS[email protected] 0x1102, // EDR 2 mpbs 112865389bfcS[email protected] 0x2204, // EDR 3 mbps 112965389bfcS[email protected] 0x0300, // 3 slot EDR packts 113065389bfcS[email protected] 0x3000, // 5 slot EDR packet 113165389bfcS[email protected] }; 11328f8108aaSmatthias.ringwald 113365389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 113465389bfcS[email protected] // enable packet types based on size 11358f8108aaSmatthias.ringwald uint16_t packet_types = 0; 1136f16a69bbS[email protected] unsigned int i; 11378f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 11388f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 11398f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 11408f8108aaSmatthias.ringwald packet_types |= 1 << i; 11418f8108aaSmatthias.ringwald } 11428f8108aaSmatthias.ringwald } 114365389bfcS[email protected] // disable packet types due to missing local supported features 114465389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 1145f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 114665389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 114765389bfcS[email protected] if (feature_set) continue; 114865389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 114965389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 115065389bfcS[email protected] } 11518f8108aaSmatthias.ringwald // flip bits for "may not be used" 11528f8108aaSmatthias.ringwald packet_types ^= 0x3306; 11538f8108aaSmatthias.ringwald return packet_types; 11548f8108aaSmatthias.ringwald } 11558f8108aaSmatthias.ringwald 11568f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 11573a9fb326S[email protected] return hci_stack->packet_types; 11588f8108aaSmatthias.ringwald } 115935454696SMatthias Ringwald #endif 11608f8108aaSmatthias.ringwald 1161facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 11627dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 11633a9fb326S[email protected] return hci_stack->hci_packet_buffer; 11647dc17943Smatthias.ringwald } 11657dc17943Smatthias.ringwald 1166f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 11673a9fb326S[email protected] return hci_stack->acl_data_packet_length; 11687dc17943Smatthias.ringwald } 11697dc17943Smatthias.ringwald 117006b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 117120dcdd22SMatthias Ringwald bool hci_extended_sco_link_supported(void){ 11723e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 11733e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 11743e68d23dSMatthias Ringwald } 117506b9e820SMatthias Ringwald #endif 11763e68d23dSMatthias Ringwald 117720dcdd22SMatthias Ringwald bool hci_non_flushable_packet_boundary_flag_supported(void){ 11786ac9a97eS[email protected] // No. 54, byte 6, bit 6 11794ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 6u)) != 0u; 11806ac9a97eS[email protected] } 11816ac9a97eS[email protected] 11829885fb2dSMatthias Ringwald #ifdef ENABLE_CLASSIC 118315a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 11846ac9a97eS[email protected] // No. 51, byte 6, bit 3 11854ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 3u)) != 0u; 1186f5d8d141S[email protected] } 11879885fb2dSMatthias Ringwald #endif 1188f5d8d141S[email protected] 11897f02f414SMatthias Ringwald static int hci_classic_supported(void){ 119035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 11916ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 11923a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 119335454696SMatthias Ringwald #else 119435454696SMatthias Ringwald return 0; 119535454696SMatthias Ringwald #endif 1196f5d8d141S[email protected] } 1197f5d8d141S[email protected] 11987f02f414SMatthias Ringwald static int hci_le_supported(void){ 1199a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 12006ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 12014ea43905SMatthias Ringwald return (hci_stack->local_supported_features[4u] & (1u << 6u)) != 0u; 1202f5d8d141S[email protected] #else 1203f5d8d141S[email protected] return 0; 1204f5d8d141S[email protected] #endif 1205f5d8d141S[email protected] } 1206f5d8d141S[email protected] 12071ffa425cSMatthias Ringwald static bool hci_command_supported(uint8_t command_index){ 12081ffa425cSMatthias Ringwald return (hci_stack->local_supported_commands & (1LU << command_index)) != 0; 12091ffa425cSMatthias Ringwald } 12101ffa425cSMatthias Ringwald 1211b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 1212b95a5a35SMatthias Ringwald 1213*63671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 1214*63671e69SMatthias Ringwald static bool hci_extended_advertising_supported(void){ 1215*63671e69SMatthias Ringwald return hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE); 1216*63671e69SMatthias Ringwald } 1217*63671e69SMatthias Ringwald #endif 1218*63671e69SMatthias Ringwald 1219f5873674SMatthias Ringwald static void hci_get_own_address_for_addr_type(uint8_t own_addr_type, bd_addr_t own_addr){ 12206bcfa632SMatthias Ringwald if (own_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 12216bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->local_bd_addr, 6); 122269a97523S[email protected] } else { 12236bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->le_random_address, 6); 122469a97523S[email protected] } 122569a97523S[email protected] } 122669a97523S[email protected] 12276bcfa632SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 12286bcfa632SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 12296bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_own_addr_type, addr); 12306bcfa632SMatthias Ringwald } 12316bcfa632SMatthias Ringwald 12326bcfa632SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 12336bcfa632SMatthias Ringwald void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr){ 12346bcfa632SMatthias Ringwald *addr_type = hci_stack->le_advertisements_own_addr_type; 12356bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, addr); 12366bcfa632SMatthias Ringwald }; 12376bcfa632SMatthias Ringwald #endif 12386bcfa632SMatthias Ringwald 1239e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 12406bcfa632SMatthias Ringwald 12416bcfa632SMatthias Ringwald /** 12426bcfa632SMatthias Ringwald * @brief Get own addr type and address used for LE connections (Central) 12436bcfa632SMatthias Ringwald */ 12446bcfa632SMatthias Ringwald void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){ 12456bcfa632SMatthias Ringwald *addr_type = hci_stack->le_connection_own_addr_type; 12466bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, addr); 12476bcfa632SMatthias Ringwald } 12486bcfa632SMatthias Ringwald 1249384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size){ 12509ec2630cSMatthias Ringwald 1251d1dc057bS[email protected] int offset = 3; 1252d1dc057bS[email protected] int num_reports = packet[offset]; 1253d1dc057bS[email protected] offset += 1; 1254d1dc057bS[email protected] 125557c9da5bS[email protected] int i; 12564f4e0224SMatthias Ringwald // log_info("HCI: handle adv report with num reports: %d", num_reports); 125703fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1258a1df452eSMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 125933e6948bSMatthias Ringwald // sanity checks on data_length: 126033e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 126133e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 12624ea43905SMatthias Ringwald if ((offset + 9u + data_length + 1u) > size) return; 126333e6948bSMatthias Ringwald // setup event 12644ea43905SMatthias Ringwald uint8_t event_size = 10u + data_length; 1265d1dc057bS[email protected] int pos = 0; 1266045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 126757c9da5bS[email protected] event[pos++] = event_size; 12686535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address 1269d1dc057bS[email protected] offset += 8; 1270d1dc057bS[email protected] pos += 8; 1271d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 127233e6948bSMatthias Ringwald event[pos++] = data_length; 127333e6948bSMatthias Ringwald offset++; 12746535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], data_length); 127557c9da5bS[email protected] pos += data_length; 12764ea43905SMatthias Ringwald offset += data_length + 1u; // rssi 1277d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 127857c9da5bS[email protected] } 127957c9da5bS[email protected] } 1280b2f949feS[email protected] #endif 1281e8c8828eSMatthias Ringwald #endif 128257c9da5bS[email protected] 12832b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 12842b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1285bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1286bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_enabled){ 12872b6ab3e6SMatthias Ringwald // get number of active le slave connections 12882b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 12892b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 12902b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 12912b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 12922b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 12932b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 12942b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 12952b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 12962b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 12972b6ab3e6SMatthias Ringwald num_slave_connections++; 12982b6ab3e6SMatthias Ringwald } 12992b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1300bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1301bbc366e6SMatthias Ringwald } else { 1302bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 13032b6ab3e6SMatthias Ringwald } 13042b6ab3e6SMatthias Ringwald } 13052b6ab3e6SMatthias Ringwald #endif 13062b6ab3e6SMatthias Ringwald #endif 13072b6ab3e6SMatthias Ringwald 130859d59ecfSMatthias Ringwald #ifdef ENABLE_CLASSIC 130959d59ecfSMatthias Ringwald static void gap_run_set_local_name(void){ 131059d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 131159d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 131259d59ecfSMatthias Ringwald // construct HCI Command and send 131359d59ecfSMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 131459d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 131559d59ecfSMatthias Ringwald packet[0] = opcode & 0xff; 131659d59ecfSMatthias Ringwald packet[1] = opcode >> 8; 131759d59ecfSMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 131859d59ecfSMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 131959d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 132059d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 132159d59ecfSMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 132259d59ecfSMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 132359d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 132459d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 132559d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 132659d59ecfSMatthias Ringwald } 132759d59ecfSMatthias Ringwald 132859d59ecfSMatthias Ringwald static void gap_run_set_eir_data(void){ 132959d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 133059d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 133159d59ecfSMatthias Ringwald // construct HCI Command in-place and send 133259d59ecfSMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 133359d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 133459d59ecfSMatthias Ringwald uint16_t offset = 0; 133559d59ecfSMatthias Ringwald packet[offset++] = opcode & 0xff; 133659d59ecfSMatthias Ringwald packet[offset++] = opcode >> 8; 133759d59ecfSMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 133859d59ecfSMatthias Ringwald packet[offset++] = 0; // FEC not required 133959d59ecfSMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 134059d59ecfSMatthias Ringwald if (hci_stack->eir_data){ 134159d59ecfSMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 134259d59ecfSMatthias Ringwald ad_context_t context; 134359d59ecfSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 134459d59ecfSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 134559d59ecfSMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 134659d59ecfSMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 134759d59ecfSMatthias Ringwald // copy item 134859d59ecfSMatthias Ringwald packet[offset++] = size + 1; 134959d59ecfSMatthias Ringwald packet[offset++] = data_type; 135059d59ecfSMatthias Ringwald memcpy(&packet[offset], data, size); 135159d59ecfSMatthias Ringwald // update name item 135259d59ecfSMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 135359d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 135459d59ecfSMatthias Ringwald } 135559d59ecfSMatthias Ringwald offset += size; 135659d59ecfSMatthias Ringwald } 135759d59ecfSMatthias Ringwald } else { 135859d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 135959d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 136059d59ecfSMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 136159d59ecfSMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 136259d59ecfSMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 136359d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 136459d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 136559d59ecfSMatthias Ringwald } 136659d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 136759d59ecfSMatthias Ringwald } 1368ab4831a3SMatthias Ringwald 1369ab4831a3SMatthias Ringwald static void hci_run_gap_tasks_classic(void){ 1370baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_CLASS_OF_DEVICE) != 0) { 1371baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_CLASS_OF_DEVICE; 1372ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1373ab4831a3SMatthias Ringwald return; 1374ab4831a3SMatthias Ringwald } 1375baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_LOCAL_NAME) != 0) { 1376baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_LOCAL_NAME; 1377ab4831a3SMatthias Ringwald gap_run_set_local_name(); 1378ab4831a3SMatthias Ringwald return; 1379ab4831a3SMatthias Ringwald } 1380baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_EIR_DATA) != 0) { 1381baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_EIR_DATA; 1382ab4831a3SMatthias Ringwald gap_run_set_eir_data(); 1383ab4831a3SMatthias Ringwald return; 1384ab4831a3SMatthias Ringwald } 1385baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_DEFAULT_LINK_POLICY) != 0) { 1386baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_DEFAULT_LINK_POLICY; 1387ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1388ab4831a3SMatthias Ringwald return; 1389ab4831a3SMatthias Ringwald } 1390ab4831a3SMatthias Ringwald // write page scan activity 1391baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY) != 0) { 1392baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 1393ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 1394ab4831a3SMatthias Ringwald return; 1395ab4831a3SMatthias Ringwald } 1396ab4831a3SMatthias Ringwald // write page scan type 1397baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_TYPE) != 0) { 1398baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_TYPE; 1399ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 1400ab4831a3SMatthias Ringwald return; 1401ab4831a3SMatthias Ringwald } 140232a12730SMatthias Ringwald // write page timeout 1403baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_TIMEOUT) != 0) { 1404baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_TIMEOUT; 140532a12730SMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, hci_stack->page_timeout); 140632a12730SMatthias Ringwald return; 140732a12730SMatthias Ringwald } 1408ab4831a3SMatthias Ringwald // send scan enable 1409baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_SCAN_ENABLE) != 0) { 1410baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_SCAN_ENABLE; 1411ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 1412ab4831a3SMatthias Ringwald return; 1413ab4831a3SMatthias Ringwald } 1414c0c8a829SMatthias Ringwald // send write scan activity 1415baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY) != 0) { 1416baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 1417c0c8a829SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_scan_activity, hci_stack->inquiry_scan_interval, hci_stack->inquiry_scan_window); 1418c0c8a829SMatthias Ringwald return; 1419c0c8a829SMatthias Ringwald } 1420ab4831a3SMatthias Ringwald } 142159d59ecfSMatthias Ringwald #endif 142259d59ecfSMatthias Ringwald 14236fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 142406b9e820SMatthias Ringwald 142596b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 142696b53536SMatthias Ringwald if (!hci_stack->config) return 0; 14279796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 142896b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1429a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 143096b53536SMatthias Ringwald baud_rate = 3000000; 143196b53536SMatthias Ringwald } 143296b53536SMatthias Ringwald return baud_rate; 143396b53536SMatthias Ringwald } 143496b53536SMatthias Ringwald 1435ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 14369ec2630cSMatthias Ringwald UNUSED(ds); 14379ec2630cSMatthias Ringwald 14380305bdeaSMatthias Ringwald switch (hci_stack->substate){ 14390305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 14407b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 14410305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 14427b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 14430305bdeaSMatthias Ringwald hci_run(); 14440305bdeaSMatthias Ringwald break; 14459f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 14469f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 14479f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 14489f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 14499f007422SMatthias Ringwald } 1450cf373d3aSMatthias Ringwald 1451cf373d3aSMatthias Ringwald /* fall through */ 1452cf373d3aSMatthias Ringwald 1453e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1454e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1455e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1456e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1457e47e68c7SMatthias Ringwald hci_run(); 1458688c2635SMatthias Ringwald break; 14597224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 14607224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 146196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1462cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 14637dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 14647224be7eSMatthias Ringwald } 1465834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 146661f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1467834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1468834bce8cSMatthias Ringwald log_info("Link Reset"); 1469834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1470834bce8cSMatthias Ringwald } 1471772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1472772a36d3SMatthias Ringwald hci_run(); 1473772a36d3SMatthias Ringwald } 14744696bddbSMatthias Ringwald break; 1475559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1476559961d0SMatthias Ringwald // otherwise continue 1477559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1478559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1479559961d0SMatthias Ringwald break; 14800305bdeaSMatthias Ringwald default: 14810305bdeaSMatthias Ringwald break; 14820305bdeaSMatthias Ringwald } 14830305bdeaSMatthias Ringwald } 148406b9e820SMatthias Ringwald #endif 14850305bdeaSMatthias Ringwald 148671de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 148774b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 148874b323a9SMatthias Ringwald } 148974b323a9SMatthias Ringwald 1490f795c86eSMatthias Ringwald static void hci_init_done(void){ 1491f795c86eSMatthias Ringwald // done. tell the app 1492f795c86eSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1493f795c86eSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1494f795c86eSMatthias Ringwald hci_emit_state(); 1495f795c86eSMatthias Ringwald } 1496f795c86eSMatthias Ringwald 149774b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 149871de195eSMatthias Ringwald static void hci_initializing_run(void){ 1499148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 1500f4c579d4SMatthias Ringwald 15010d37aff3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 15020d37aff3SMatthias Ringwald 1503f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1504f9fd20c2SMatthias Ringwald bool need_baud_change = hci_stack->config 1505f4c579d4SMatthias Ringwald && hci_stack->chipset 1506f4c579d4SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1507f4c579d4SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 1508f4c579d4SMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1509f4c579d4SMatthias Ringwald #endif 1510f4c579d4SMatthias Ringwald 151174b323a9SMatthias Ringwald switch (hci_stack->substate){ 151274b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 151374b323a9SMatthias Ringwald hci_state_reset(); 1514a0cf2f3fSMatthias Ringwald 15156fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 15160305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1517659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1518528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1519528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1520a0cf2f3fSMatthias Ringwald #endif 15210305bdeaSMatthias Ringwald // send command 152274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 15230305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 152474b323a9SMatthias Ringwald break; 152576fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 152676fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 152776fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 152876fcb19bSMatthias Ringwald break; 1529e28e41c0SMatthias Ringwald 1530e28e41c0SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1531e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1532e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1533e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1534e90bae01SMatthias Ringwald break; 1535e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1536e47e68c7SMatthias Ringwald hci_state_reset(); 1537e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1538659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1539528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1540528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1541e47e68c7SMatthias Ringwald // send command 1542e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1543e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1544e47e68c7SMatthias Ringwald break; 15458d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 15468d29070eSMatthias Ringwald hci_state_reset(); 15478d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 15488d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 15498d29070eSMatthias Ringwald break; 1550f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 1551f4c579d4SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1552f4c579d4SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1553f4c579d4SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1554f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1555f4c579d4SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1556f4c579d4SMatthias Ringwald break; 1557f4c579d4SMatthias Ringwald } 1558c97af506SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 1559c97af506SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 1560c97af506SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1561c97af506SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1562c97af506SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 1563c97af506SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1564c97af506SMatthias Ringwald break; 1565f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: 1566f4c579d4SMatthias Ringwald if (need_baud_change) { 156796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 15683fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1569f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 157074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 15714ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 15724696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 15734696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 157461f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1575659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1576528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 15774696bddbSMatthias Ringwald } 157874b323a9SMatthias Ringwald break; 1579fab26ab3SMatthias Ringwald } 1580f4c579d4SMatthias Ringwald 1581f4c579d4SMatthias Ringwald /* fall through */ 1582f4c579d4SMatthias Ringwald 158374b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 158474b323a9SMatthias Ringwald // Custom initialization 15853fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1586ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 15871979f09cSMatthias Ringwald bool send_cmd = false; 1588ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1589f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 15901979f09cSMatthias Ringwald send_cmd = true; 1591e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1592e47e68c7SMatthias Ringwald break; 1593f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 15941979f09cSMatthias Ringwald send_cmd = true; 1595f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1596e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1597659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1598528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1599528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1600a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1601772a36d3SMatthias Ringwald && hci_stack->config 16023fb36a29SMatthias Ringwald && hci_stack->chipset 16033fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1604772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 16052caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1606772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1607772a36d3SMatthias Ringwald } else { 16089f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1609772a36d3SMatthias Ringwald } 1610e47e68c7SMatthias Ringwald break; 1611f41911edSMatthias Ringwald default: 1612f41911edSMatthias Ringwald break; 1613e47e68c7SMatthias Ringwald } 1614ee720f3aSMatthias Ringwald 1615ee720f3aSMatthias Ringwald if (send_cmd){ 16164ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1617ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1618ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 16190305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 162074b323a9SMatthias Ringwald break; 162174b323a9SMatthias Ringwald } 1622148ca237SMatthias Ringwald log_info("Init script done"); 162392a0d36dSMatthias Ringwald 1624559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1625ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1626a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1627a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1628e021ff1eSMatthias Ringwald 1629559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 163092a0d36dSMatthias Ringwald if (need_baud_change) { 16319796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1632cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 163392a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 163492a0d36dSMatthias Ringwald } 1635559961d0SMatthias Ringwald 1636f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1637f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1638f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1639f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1640559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1641f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1642559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1643559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1644559961d0SMatthias Ringwald break; 164592a0d36dSMatthias Ringwald } 164674b323a9SMatthias Ringwald } 164706b9e820SMatthias Ringwald #endif 1648521bd5ffSMatthias Ringwald /* fall through */ 164906b9e820SMatthias Ringwald 165006b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 165106b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 165206b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 165306b9e820SMatthias Ringwald break; 165453860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 165553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 165653860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 165753860077SMatthias Ringwald break; 165874b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 16595ffe9d0bSMatthias Ringwald // only read buffer size if supported 16601ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE)){ 166174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 16620305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 166374b323a9SMatthias Ringwald break; 16645ffe9d0bSMatthias Ringwald } 1665521bd5ffSMatthias Ringwald 16665ffe9d0bSMatthias Ringwald /* fall through */ 16675ffe9d0bSMatthias Ringwald 166853860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 166953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 16700305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 167174b323a9SMatthias Ringwald break; 16722b838201SMatthias Ringwald 16732b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 16742b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 16752b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 16762b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 16772b838201SMatthias Ringwald break; 16782b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 16792b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 16802b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 16812b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 16822b838201SMatthias Ringwald break; 16832b838201SMatthias Ringwald #endif 16842b838201SMatthias Ringwald 168574b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 16860305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 168774b323a9SMatthias Ringwald if (hci_le_supported()){ 16885ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x3FFFFFFFU); 168974b323a9SMatthias Ringwald } else { 169074b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 16915ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x1FFFFFFFU); 169274b323a9SMatthias Ringwald } 169374b323a9SMatthias Ringwald break; 16942b838201SMatthias Ringwald 169535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 169674b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 1697f795c86eSMatthias Ringwald if (hci_classic_supported() && gap_ssp_supported()){ 169874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 16990305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 170074b323a9SMatthias Ringwald break; 1701e7c662faSMatthias Ringwald } 1702521bd5ffSMatthias Ringwald 1703e7c662faSMatthias Ringwald /* fall through */ 1704e7c662faSMatthias Ringwald 1705f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1706f795c86eSMatthias Ringwald if (hci_classic_supported()){ 1707f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 17088a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1709f6858d14SMatthias Ringwald break; 1710f795c86eSMatthias Ringwald } 1711521bd5ffSMatthias Ringwald 1712f795c86eSMatthias Ringwald /* fall through */ 1713f795c86eSMatthias Ringwald 17145d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 1715f3052906SMatthias Ringwald // skip write secure connections host support if not supported or disabled 17161ffa425cSMatthias Ringwald if (hci_classic_supported() && hci_stack->secure_connections_enable 17171ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST)) { 1718c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 17195d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 17201ffa425cSMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 17215d23aae8SMatthias Ringwald break; 1722f3052906SMatthias Ringwald } 1723521bd5ffSMatthias Ringwald 1724e4c6114dSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1725521bd5ffSMatthias Ringwald /* fall through */ 1726521bd5ffSMatthias Ringwald 1727483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1728729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1729e4c6114dSMatthias Ringwald // skip write synchronous flow control if not supported 17301ffa425cSMatthias Ringwald if (hci_classic_supported() 17311ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)) { 1732729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1733729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1734729ed62eSMatthias Ringwald break; 1735f795c86eSMatthias Ringwald } 1736f795c86eSMatthias Ringwald /* fall through */ 1737f795c86eSMatthias Ringwald 1738483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1739e4c6114dSMatthias Ringwald // skip write default erroneous data reporting if not supported 17401ffa425cSMatthias Ringwald if (hci_classic_supported() 17411ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING)) { 1742483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1743483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1744483c5078SMatthias Ringwald break; 1745f795c86eSMatthias Ringwald } 1746e4c6114dSMatthias Ringwald #endif 1747f795c86eSMatthias Ringwald 1748f795c86eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(ENABLE_SCO_OVER_PCM) 1749521bd5ffSMatthias Ringwald /* fall through */ 1750521bd5ffSMatthias Ringwald 17518051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 1752a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1753e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 1754a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 17558051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1756a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1757a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 17588051253fSMatthias Ringwald #endif 17598051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 17608051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 17611d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1762f795c86eSMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 16 kHz 17631d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 17641d2bbd54SMatthias Ringwald #else 17651d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 17661d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 17671d2bbd54SMatthias Ringwald #endif 17688051253fSMatthias Ringwald #endif 1769a42798c3SMatthias Ringwald break; 1770f795c86eSMatthias Ringwald } 1771f795c86eSMatthias Ringwald #endif 1772f795c86eSMatthias Ringwald 17734e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 1774521bd5ffSMatthias Ringwald /* fall through */ 1775521bd5ffSMatthias Ringwald 17764e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 1777e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 17784e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 17794e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 17801d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 17811d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 17821d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 17831d2bbd54SMatthias Ringwald #else 17841d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 17851d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 17861d2bbd54SMatthias Ringwald #endif 17874e821764SMatthias Ringwald break; 1788f795c86eSMatthias Ringwald } 178935454696SMatthias Ringwald #endif 17904e821764SMatthias Ringwald #endif 17914e821764SMatthias Ringwald 1792a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1793521bd5ffSMatthias Ringwald /* fall through */ 1794521bd5ffSMatthias Ringwald 179574b323a9SMatthias Ringwald // LE INIT 179674b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 1797f795c86eSMatthias Ringwald if (hci_le_supported()){ 179874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 17990305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 180074b323a9SMatthias Ringwald break; 1801f795c86eSMatthias Ringwald } 1802521bd5ffSMatthias Ringwald 1803f795c86eSMatthias Ringwald /* fall through */ 1804f795c86eSMatthias Ringwald 1805699a5fcaSMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 1806699a5fcaSMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 18071ffa425cSMatthias Ringwald if (hci_le_supported() 18081ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED)) { 1809699a5fcaSMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 1810699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 1811699a5fcaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 1812daabb8b8SMatthias Ringwald break; 1813f795c86eSMatthias Ringwald } 1814521bd5ffSMatthias Ringwald 1815f795c86eSMatthias Ringwald /* fall through */ 1816f795c86eSMatthias Ringwald 1817699a5fcaSMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 1818f795c86eSMatthias Ringwald if (hci_le_supported()){ 1819699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 182091d9e5d0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0xfffffdff, 0x07); // all events from core v5.3 without LE Enhanced Connection Complete 182174b323a9SMatthias Ringwald break; 1822f795c86eSMatthias Ringwald } 1823b435e062SMatthias Ringwald #endif 1824b435e062SMatthias Ringwald 1825b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1826521bd5ffSMatthias Ringwald /* fall through */ 1827521bd5ffSMatthias Ringwald 1828dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 18291ffa425cSMatthias Ringwald if (hci_le_supported() 18301ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH)) { 1831dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 1832dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 1833dcd678baSMatthias Ringwald break; 1834f795c86eSMatthias Ringwald } 1835521bd5ffSMatthias Ringwald 1836f795c86eSMatthias Ringwald /* fall through */ 1837f795c86eSMatthias Ringwald 1838dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 18391ffa425cSMatthias Ringwald if (hci_le_supported() 18401ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)) { 1841dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 1842b435e062SMatthias 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); 1843dcd678baSMatthias Ringwald break; 1844f795c86eSMatthias Ringwald } 1845b435e062SMatthias Ringwald #endif 1846b435e062SMatthias Ringwald 1847b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1848521bd5ffSMatthias Ringwald /* fall through */ 1849521bd5ffSMatthias Ringwald 18503b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 1851f795c86eSMatthias Ringwald if (hci_le_supported()){ 18523b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 18533b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 18543b6d4121SMatthias Ringwald break; 1855f795c86eSMatthias Ringwald } 1856521bd5ffSMatthias Ringwald 185774b323a9SMatthias Ringwald #endif 18584f982f31SMatthias Ringwald 1859521bd5ffSMatthias Ringwald /* fall through */ 1860521bd5ffSMatthias Ringwald 1861f795c86eSMatthias Ringwald case HCI_INIT_DONE: 18624f982f31SMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 186373799937SMatthias Ringwald // main init sequence complete 18644f982f31SMatthias Ringwald #ifdef ENABLE_CLASSIC 186573799937SMatthias Ringwald // check if initial Classic GAP Tasks are completed 1866baa4881eSMatthias Ringwald if (hci_classic_supported() && (hci_stack->gap_tasks_classic != 0)) { 18674f982f31SMatthias Ringwald hci_run_gap_tasks_classic(); 18684f982f31SMatthias Ringwald break; 18694f982f31SMatthias Ringwald } 18704f982f31SMatthias Ringwald #endif 187173799937SMatthias Ringwald #ifdef ENABLE_BLE 187273799937SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 187373799937SMatthias Ringwald // check if initial LE GAP Tasks are completed 187473799937SMatthias Ringwald if (hci_le_supported() && hci_stack->le_scanning_param_update) { 187573799937SMatthias Ringwald hci_run_general_gap_le(); 187673799937SMatthias Ringwald break; 187773799937SMatthias Ringwald } 187873799937SMatthias Ringwald #endif 187973799937SMatthias Ringwald #endif 1880f795c86eSMatthias Ringwald hci_init_done(); 1881f795c86eSMatthias Ringwald break; 1882f795c86eSMatthias Ringwald 188374b323a9SMatthias Ringwald default: 188474b323a9SMatthias Ringwald return; 188574b323a9SMatthias Ringwald } 188655975f88SMatthias Ringwald } 188755975f88SMatthias Ringwald 188807fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 188907fd2f31SMatthias Ringwald bool command_completed = false; 18900e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1891f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 18926155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 189307fd2f31SMatthias Ringwald command_completed = true; 1894148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 18956155b3d3S[email protected] } else { 1896d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 18976155b3d3S[email protected] } 18986155b3d3S[email protected] } 18990f97eae7SMatthias Ringwald 19000e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 19016155b3d3S[email protected] uint8_t status = packet[2]; 1902f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 19036155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 19046155b3d3S[email protected] if (status){ 190507fd2f31SMatthias Ringwald command_completed = true; 1906148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 19076155b3d3S[email protected] } else { 19086155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 19096155b3d3S[email protected] } 19106155b3d3S[email protected] } else { 1911148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 19126155b3d3S[email protected] } 19136155b3d3S[email protected] } 19146fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1915e47e68c7SMatthias Ringwald // Vendor == CSR 19160e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 1917e47e68c7SMatthias Ringwald // TODO: track actual command 191807fd2f31SMatthias Ringwald command_completed = true; 1919e47e68c7SMatthias Ringwald } 1920a2481739S[email protected] 19214e9daa6fSMatthias Ringwald // Vendor == Toshiba 19220e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 19234e9daa6fSMatthias Ringwald // TODO: track actual command 192407fd2f31SMatthias Ringwald command_completed = true; 1925004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 1926004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 19274e9daa6fSMatthias Ringwald } 192807fd2f31SMatthias Ringwald #endif 192907fd2f31SMatthias Ringwald 193007fd2f31SMatthias Ringwald return command_completed; 193107fd2f31SMatthias Ringwald } 193207fd2f31SMatthias Ringwald 193307fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 193407fd2f31SMatthias Ringwald 193507fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 193607fd2f31SMatthias Ringwald 193707fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 193807fd2f31SMatthias Ringwald 19396fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 19404e9daa6fSMatthias Ringwald 19410f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 19420f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 19430f97eae7SMatthias Ringwald // 19440f97eae7SMatthias Ringwald // HCI Reset 19450f97eae7SMatthias Ringwald // Timeout 100 ms 19460f97eae7SMatthias Ringwald // HCI Reset 19470f97eae7SMatthias Ringwald // Command Complete Reset 19480f97eae7SMatthias Ringwald // HCI Read Local Version Information 19490f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 19500f97eae7SMatthias Ringwald // hang... 19510f97eae7SMatthias Ringwald // 19520f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 19530f97eae7SMatthias Ringwald if (!command_completed 1954a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 19550e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 19560f97eae7SMatthias Ringwald 1957f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 19580f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 19590f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 19600f97eae7SMatthias Ringwald return; 19610f97eae7SMatthias Ringwald } 19620f97eae7SMatthias Ringwald } 19630f97eae7SMatthias Ringwald 19649f007422SMatthias Ringwald // CSR & H5 19659f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 19669f007422SMatthias Ringwald if (!command_completed 1967a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 19680e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 19699f007422SMatthias Ringwald 19709f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 19719f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 19729f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 19739f007422SMatthias Ringwald return; 19749f007422SMatthias Ringwald } 19759f007422SMatthias Ringwald } 19769f007422SMatthias Ringwald 19779f007422SMatthias 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 19789f007422SMatthias Ringwald // fix: Correct substate and behave as command below 19799f007422SMatthias Ringwald if (command_completed){ 19809f007422SMatthias Ringwald switch (hci_stack->substate){ 19819f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 19829f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 19839f007422SMatthias Ringwald break; 19849f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 19859f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 19869f007422SMatthias Ringwald break; 19879f007422SMatthias Ringwald default: 19889f007422SMatthias Ringwald break; 19899f007422SMatthias Ringwald } 19909f007422SMatthias Ringwald } 19910f97eae7SMatthias Ringwald 199206b9e820SMatthias Ringwald #endif 19930f97eae7SMatthias Ringwald 1994a2481739S[email protected] if (!command_completed) return; 1995a2481739S[email protected] 199607fd2f31SMatthias Ringwald bool need_baud_change = false; 199707fd2f31SMatthias Ringwald bool need_addr_change = false; 199806b9e820SMatthias Ringwald 19996fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 200006b9e820SMatthias Ringwald need_baud_change = hci_stack->config 20013fb36a29SMatthias Ringwald && hci_stack->chipset 20023fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 2003db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 20049796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 2005db8bc6ffSMatthias Ringwald 200606b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 20073fb36a29SMatthias Ringwald && hci_stack->chipset 20083fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 200906b9e820SMatthias Ringwald #endif 2010a80162e9SMatthias Ringwald 20115c363727SMatthias Ringwald switch(hci_stack->substate){ 201206b9e820SMatthias Ringwald 20136fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 20149f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 2015d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 20169f007422SMatthias Ringwald // fix: just correct substate and behave as command below 2017f4c579d4SMatthias Ringwald 2018f4c579d4SMatthias Ringwald /* fall through */ 2019f4c579d4SMatthias Ringwald #endif 2020f4c579d4SMatthias Ringwald 202174b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 2022528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2023f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 20242f48d920SMatthias Ringwald return; 2025f4c579d4SMatthias Ringwald 2026f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2027a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 20284696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 2029fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 203061f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 203196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2032cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 2033fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 20344696bddbSMatthias Ringwald } 203574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 203674b323a9SMatthias Ringwald return; 2037a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 2038528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2039a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 2040a80162e9SMatthias Ringwald return; 204174b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 204274b323a9SMatthias Ringwald // repeat custom init 204374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 204474b323a9SMatthias Ringwald return; 204506b9e820SMatthias Ringwald #endif 204606b9e820SMatthias Ringwald 2047a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 20480e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 2049efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 2050efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 2051eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 2052eb3a5314SMatthias Ringwald return; 2053eb3a5314SMatthias Ringwald } 205453860077SMatthias Ringwald if (need_addr_change){ 205553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 205653860077SMatthias Ringwald return; 205753860077SMatthias Ringwald } 205853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 205953860077SMatthias Ringwald return; 20606fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 20617224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 20627224be7eSMatthias Ringwald if (need_baud_change){ 206396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2064cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 2065fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 20667224be7eSMatthias Ringwald } 2067eb3a5314SMatthias Ringwald if (need_addr_change){ 2068eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 2069eb3a5314SMatthias Ringwald return; 2070eb3a5314SMatthias Ringwald } 2071eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 2072eb3a5314SMatthias Ringwald return; 207353860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 20746ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 20756ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 20766ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 207753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 207853860077SMatthias Ringwald return; 207953860077SMatthias Ringwald } 208053860077SMatthias Ringwald // skipping st warm boot 208153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 208253860077SMatthias Ringwald return; 208353860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 208453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 208553860077SMatthias Ringwald return; 208606b9e820SMatthias Ringwald #endif 2087e7c662faSMatthias Ringwald 2088f795c86eSMatthias Ringwald case HCI_INIT_DONE: 2089eb8d95caSMatthias Ringwald // set state if we came here by fall through 2090eb8d95caSMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 2091f795c86eSMatthias Ringwald return; 2092a650ba4dSMatthias Ringwald 20936155b3d3S[email protected] default: 209474b323a9SMatthias Ringwald break; 20956155b3d3S[email protected] } 209655975f88SMatthias Ringwald hci_initializing_next_state(); 20976155b3d3S[email protected] } 20986155b3d3S[email protected] 20990bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 21003bb5ff27SMatthias Ringwald // CC2564C might emit Connection Complete for rejected incoming SCO connection 21013bb5ff27SMatthias Ringwald // To prevent accidentally free'ing the CHI connection for the ACL connection, 21023bb5ff27SMatthias Ringwald // check if the hci connection has been outgoing 21031f34df2cSMatthias Ringwald switch (conn->state){ 21041f34df2cSMatthias Ringwald case SEND_CREATE_CONNECTION: 21051f34df2cSMatthias Ringwald case RECEIVED_CONNECTION_REQUEST: 21061f34df2cSMatthias Ringwald break; 21071f34df2cSMatthias Ringwald default: 21081f34df2cSMatthias Ringwald return; 21091f34df2cSMatthias Ringwald } 21103bb5ff27SMatthias Ringwald 2111229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 21120bbba85bSMatthias Ringwald bd_addr_t bd_address; 21136535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 21140bbba85bSMatthias Ringwald 21156bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 21166bc9fa5eSMatthias Ringwald // cache needed data 21176bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 21186bc9fa5eSMatthias Ringwald #endif 21196bc9fa5eSMatthias Ringwald 21200bbba85bSMatthias Ringwald // connection failed, remove entry 21210bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 21220bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 21230bbba85bSMatthias Ringwald 21246bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 21250bbba85bSMatthias Ringwald // notify client if dedicated bonding 21260bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 21270bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 21280bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 21290bbba85bSMatthias Ringwald } 21300bbba85bSMatthias Ringwald 21310bbba85bSMatthias Ringwald // if authentication error, also delete link key 21320bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 21330bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 21340bbba85bSMatthias Ringwald } 21351c79b5e3SMatthias Ringwald #else 21361c79b5e3SMatthias Ringwald UNUSED(status); 21376bc9fa5eSMatthias Ringwald #endif 21380bbba85bSMatthias Ringwald } 21390bbba85bSMatthias Ringwald 2140be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 21412f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 21422f5c44baSMatthias Ringwald // SSP Controller 21432f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 21442f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 21452f5c44baSMatthias Ringwald } 21462f5c44baSMatthias Ringwald // eSCO 21472f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 21482f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 21492f5c44baSMatthias Ringwald } 21502f5c44baSMatthias Ringwald // Extended features 21512f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 21522f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 21532f5c44baSMatthias Ringwald } 21542f5c44baSMatthias Ringwald } 21552f5c44baSMatthias Ringwald 21562f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 21572f5c44baSMatthias Ringwald // SSP Host 21582f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 21592f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 21602f5c44baSMatthias Ringwald } 216150c51a77SMatthias Ringwald // SC Host 216250c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 216350c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 216450c51a77SMatthias Ringwald } 216550c51a77SMatthias Ringwald } 216650c51a77SMatthias Ringwald 216750c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 216850c51a77SMatthias Ringwald // SC Controller 216950c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 217050c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 217150c51a77SMatthias Ringwald } 21722f5c44baSMatthias Ringwald } 21732f5c44baSMatthias Ringwald 2174de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2175461a2bc4SMatthias Ringwald conn->bonding_flags &= ~BONDING_REMOTE_FEATURES_QUERY_ACTIVE; 2176de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 217749bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2178de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2179de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2180de0df013SMatthias Ringwald } 2181de0df013SMatthias Ringwald } 2182128825c3SMatthias Ringwald static bool hci_remote_sc_enabled(hci_connection_t * connection){ 2183128825c3SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 2184128825c3SMatthias Ringwald return (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 2185128825c3SMatthias Ringwald } 2186128825c3SMatthias Ringwald 2187be500194SMatthias Ringwald #endif 2188de0df013SMatthias Ringwald 218967c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 219067c6c9dcSMatthias Ringwald // handle BT initialization 219167c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 219267c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 219367c6c9dcSMatthias Ringwald } 219467c6c9dcSMatthias Ringwald 219567c6c9dcSMatthias Ringwald // help with BT sleep 219667c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 219767c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 2198905ee0fdSMatthias Ringwald && (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable))) { 219967c6c9dcSMatthias Ringwald hci_initializing_next_state(); 220067c6c9dcSMatthias Ringwald } 220167c6c9dcSMatthias Ringwald } 220267c6c9dcSMatthias Ringwald 22039866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 22049866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 22058daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 22069866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2207abdad579SMatthias Ringwald 22088daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) != 0) { 22091cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 22109866fdc7SMatthias Ringwald hci_emit_security_level(conn->con_handle, gap_security_level_for_connection(conn)); 2211abdad579SMatthias Ringwald return; 2212abdad579SMatthias Ringwald } 2213abdad579SMatthias Ringwald 2214d54424c3SMatthias Ringwald // Request remote features if not already done 2215dbe0d85cSMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 2216d54424c3SMatthias Ringwald 2217abdad579SMatthias Ringwald // Request Authentication if not already done 2218abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2219abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 22209866fdc7SMatthias Ringwald } 22219866fdc7SMatthias Ringwald #endif 22229866fdc7SMatthias Ringwald 22233b631737SMatthias Ringwald static void hci_store_local_supported_commands(const uint8_t * packet){ 222425e46151SMatthias Ringwald // create mapping table 222525e46151SMatthias Ringwald #define X(name, offset, bit) { offset, bit }, 222625e46151SMatthias Ringwald static struct { 222725e46151SMatthias Ringwald uint8_t byte_offset; 222825e46151SMatthias Ringwald uint8_t bit_position; 222925e46151SMatthias Ringwald } supported_hci_commands_map [] = { 223025e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 223125e46151SMatthias Ringwald }; 223225e46151SMatthias Ringwald #undef X 223325e46151SMatthias Ringwald 223425e46151SMatthias Ringwald // create names for debug purposes 223525e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 223625e46151SMatthias Ringwald #define X(name, offset, bit) #name, 223725e46151SMatthias Ringwald static const char * command_names[] = { 223825e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 223925e46151SMatthias Ringwald }; 224025e46151SMatthias Ringwald #undef X 224125e46151SMatthias Ringwald #endif 224225e46151SMatthias Ringwald 22431ffa425cSMatthias Ringwald hci_stack->local_supported_commands = 0; 224425e46151SMatthias Ringwald const uint8_t * commands_map = &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1]; 224525e46151SMatthias Ringwald uint16_t i; 224625e46151SMatthias Ringwald for (i = 0 ; i < SUPPORTED_HCI_COMMANDS_COUNT ; i++){ 224725e46151SMatthias Ringwald if ((commands_map[supported_hci_commands_map[i].byte_offset] & (1 << supported_hci_commands_map[i].bit_position)) != 0){ 224825e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 224925e46151SMatthias Ringwald log_info("Command %s (%u) supported %u/%u", command_names[i], i, supported_hci_commands_map[i].byte_offset, supported_hci_commands_map[i].bit_position); 225025e46151SMatthias Ringwald #else 22511ffa425cSMatthias Ringwald log_info("Command 0x%02x supported %u/%u", i, supported_hci_commands_map[i].byte_offset, supported_hci_commands_map[i].bit_position); 225225e46151SMatthias Ringwald #endif 22531ffa425cSMatthias Ringwald hci_stack->local_supported_commands |= (1LU << i); 225425e46151SMatthias Ringwald } 225525e46151SMatthias Ringwald } 22561ffa425cSMatthias Ringwald log_info("Local supported commands summary %04x", hci_stack->local_supported_commands); 22573b631737SMatthias Ringwald } 22583b631737SMatthias Ringwald 22594f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2260b08371a9SMilanka Ringwald UNUSED(size); 2261e76a89eeS[email protected] 22629cbd2215SMatthias Ringwald uint16_t manufacturer; 22639cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2264fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 22651f7b95a1Smatthias.ringwald hci_connection_t * conn; 2266645b7c25SMatthias Ringwald uint8_t status; 22679cbd2215SMatthias Ringwald #endif 22686bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 22696bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 22707ec5eeaaSmatthias.ringwald 2271645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2272645b7c25SMatthias Ringwald switch (opcode){ 2273645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2274645b7c25SMatthias Ringwald if (packet[5]) break; 22759e263bd7SMatthias Ringwald // terminate, name 248 chars 22769e263bd7SMatthias Ringwald packet[6+248] = 0; 22779e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2278645b7c25SMatthias Ringwald break; 2279645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 22801d279b20Smatthias.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" 2281c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2282429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2283429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2284429122ccSMatthias Ringwald 2285429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2286429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2287429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2288429122ccSMatthias Ringwald 2289f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 2290f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 2291a8b12447S[email protected] 2292429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2293429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 22941a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2295c3b46f5aSMatthias Ringwald } 2296645b7c25SMatthias Ringwald break; 2297645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2298645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2299891b9fc2SMatthias Ringwald uint8_t event[5]; 2300891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2301891b9fc2SMatthias Ringwald event[1] = 3; 23026535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2303891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2304891b9fc2SMatthias Ringwald } 2305645b7c25SMatthias Ringwald break; 2306a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2307645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2308f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2309ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 23106c26b087S[email protected] // determine usable ACL payload size 23116c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 23126c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 23136c26b087S[email protected] } 23149da54300S[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); 2315645b7c25SMatthias Ringwald break; 2316b435e062SMatthias Ringwald #endif 2317b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2318645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2319dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2320dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2321dcd678baSMatthias 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); 2322645b7c25SMatthias Ringwald break; 2323b435e062SMatthias Ringwald #endif 2324d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2325645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2326e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 232715d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2328645b7c25SMatthias Ringwald break; 232965a46ef3S[email protected] #endif 2330645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2331645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2332645b7c25SMatthias 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)); 233333373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 23341624665aSMatthias Ringwald if (hci_stack->link_key_db){ 23351624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 23361624665aSMatthias Ringwald } 233733373e40SMatthias Ringwald #endif 2338645b7c25SMatthias Ringwald break; 233935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2340645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 23413a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2342645b7c25SMatthias Ringwald break; 2343645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 2344f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2345f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2346f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2347f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2348f5875de5SMatthias Ringwald } 2349645b7c25SMatthias Ringwald break; 235035454696SMatthias Ringwald #endif 2351645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 23524f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 235365389bfcS[email protected] 235435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2355a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2356a5a23fc2S[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]); 23578b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 235835454696SMatthias Ringwald #endif 2359f5d8d141S[email protected] // Classic/LE 2360f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2361645b7c25SMatthias Ringwald break; 2362645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2363645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2364c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2365c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2366c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2367c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2368c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2369c21d89f3SMatthias Ringwald } 2370c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 23714696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2372645b7c25SMatthias Ringwald break; 2373645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 23743b631737SMatthias Ringwald hci_store_local_supported_commands(packet); 2375645b7c25SMatthias Ringwald break; 2376e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2377645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 23784f781026SMatthias Ringwald if (packet[5]) return; 23795b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2380645b7c25SMatthias Ringwald break; 2381645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2382645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2383573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2384573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2385c3b46f5aSMatthias Ringwald if (conn != NULL) { 23869866fdc7SMatthias Ringwald uint8_t key_size = 0; 2387573897a0SMatthias Ringwald if (status == 0){ 23889866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 23894a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2390170fafaeSMatthias Ringwald } else { 2391e9f98c4aSMatthias Ringwald key_size = 1; 23929866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2393573897a0SMatthias Ringwald } 23949866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2395c3b46f5aSMatthias Ringwald } 2396645b7c25SMatthias Ringwald break; 2397cc15bb2cSMatthias Ringwald // assert pairing complete event is emitted. 2398cc15bb2cSMatthias Ringwald // note: for SSP, Simple Pairing Complete Event is sufficient, but we want to be more robust 2399cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_NEGATIVE_REPLY: 2400cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_NEGATIVE_REPLY: 2401cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: 24028cc1507eSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 2403cc15bb2cSMatthias Ringwald // lookup connection by gap pairing addr 2404cc15bb2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->gap_pairing_addr, BD_ADDR_TYPE_ACL); 2405cc15bb2cSMatthias Ringwald if (conn == NULL) break; 2406cc15bb2cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 2407cc15bb2cSMatthias Ringwald break; 2408cc15bb2cSMatthias Ringwald 240975a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 241075a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 241175a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 241275a8e4faSMatthias Ringwald uint8_t event[67]; 241375a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 241475a8e4faSMatthias Ringwald event[1] = 65; 241575a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 241675a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 241775a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 241875a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 241975a8e4faSMatthias Ringwald event[2] = 3; 242075a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 242175a8e4faSMatthias Ringwald } else { 242275a8e4faSMatthias Ringwald event[2] = 1; 242375a8e4faSMatthias Ringwald } 242475a8e4faSMatthias Ringwald } 242575a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 242675a8e4faSMatthias Ringwald break; 242775a8e4faSMatthias Ringwald } 24281ae74bf3SMatthias Ringwald 24291ae74bf3SMatthias Ringwald // note: only needed if user does not provide OOB data 24301ae74bf3SMatthias Ringwald case HCI_OPCODE_HCI_REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: 24311ae74bf3SMatthias Ringwald conn = hci_connection_for_handle(hci_stack->classic_oob_con_handle); 24321ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 24331ae74bf3SMatthias Ringwald if (conn == NULL) break; 24341ae74bf3SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 24351ae74bf3SMatthias Ringwald break; 2436e8c8828eSMatthias Ringwald #endif 243775a8e4faSMatthias Ringwald #endif 24386f35bb46SMatthias Ringwald default: 24396f35bb46SMatthias Ringwald break; 24404f781026SMatthias Ringwald } 2441645b7c25SMatthias Ringwald } 24424f781026SMatthias Ringwald 24430ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 24440ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 24450ce3f217SMatthias Ringwald bd_addr_t addr; 24460ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 24470ce3f217SMatthias Ringwald hci_connection_t * conn; 24480ce3f217SMatthias Ringwald 24490ce3f217SMatthias Ringwald // Connection management 24500ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 24510ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 24520ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 24530ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 24540ce3f217SMatthias Ringwald 24550ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 24560ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 24570ce3f217SMatthias Ringwald if (packet[3]){ 24580ce3f217SMatthias Ringwald 24590ce3f217SMatthias Ringwald // handle cancelled outgoing connection 24600ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 24610ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 24620ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 24630ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 24641f468175SMatthias Ringwald // reset state 24650ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 24661f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 24670ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 24680ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 24690ce3f217SMatthias Ringwald } 24700ce3f217SMatthias Ringwald 24710ce3f217SMatthias Ringwald // outgoing le connection establishment is done 24720ce3f217SMatthias Ringwald if (conn){ 24730ce3f217SMatthias Ringwald // remove entry 24740ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 24750ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 24760ce3f217SMatthias Ringwald } 24770ce3f217SMatthias Ringwald return; 24780ce3f217SMatthias Ringwald } 24790ce3f217SMatthias Ringwald #endif 24800ce3f217SMatthias Ringwald 24810ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 24820ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 24830ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 24840ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 24850ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 24860ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 24870ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 24880ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 24890ce3f217SMatthias Ringwald } 24900ce3f217SMatthias Ringwald #endif 24910ce3f217SMatthias Ringwald } else { 24920ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 24930ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 24940ce3f217SMatthias Ringwald hci_stack->le_advertisements_active = false; 24950ce3f217SMatthias Ringwald #endif 24960ce3f217SMatthias Ringwald } 24970ce3f217SMatthias Ringwald 24980ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 24990ce3f217SMatthias Ringwald if (!conn){ 25000ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 25010ce3f217SMatthias Ringwald } 25020ce3f217SMatthias Ringwald 25030ce3f217SMatthias Ringwald // no memory, sorry. 25040ce3f217SMatthias Ringwald if (!conn){ 25050ce3f217SMatthias Ringwald return; 25060ce3f217SMatthias Ringwald } 25070ce3f217SMatthias Ringwald 25080ce3f217SMatthias Ringwald conn->state = OPEN; 25090ce3f217SMatthias Ringwald conn->role = packet[6]; 25100ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 25110ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 25120ce3f217SMatthias Ringwald 25130ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 25140ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 25150ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 25160ce3f217SMatthias Ringwald } 25170ce3f217SMatthias Ringwald #endif 25180ce3f217SMatthias Ringwald 2519dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 2520dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 2521dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 2522dde9ff1eSMatthias Ringwald 25230ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 25240ce3f217SMatthias Ringwald 25250ce3f217SMatthias Ringwald // restart timer 25260ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 25270ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 25280ce3f217SMatthias Ringwald 25290ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 25300ce3f217SMatthias Ringwald 25310ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 25320ce3f217SMatthias Ringwald } 25330ce3f217SMatthias Ringwald #endif 25340ce3f217SMatthias Ringwald 253517c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 253617c6fe5cSMatthias 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){ 253717c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 253817c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 25399a8f78c1SMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities if Authenticated is possible 25409a8f78c1SMatthias Ringwald // @see: Core Spec v5.3, Vol 3, Part C, Table 5.7 254117c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 25429a8f78c1SMatthias Ringwald // MITM not possible without keyboard or display 254317c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 254417c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 25459a8f78c1SMatthias Ringwald 25469a8f78c1SMatthias Ringwald // MITM possible if one side has keyboard and the other has keyboard or display 25479a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 25489a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 25499a8f78c1SMatthias Ringwald 25509a8f78c1SMatthias Ringwald // MITM not possible if one side has only display and other side has no keyboard 25519a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 25529a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 255317c6fe5cSMatthias Ringwald } 255417c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 255517c6fe5cSMatthias Ringwald return true; 255617c6fe5cSMatthias Ringwald } 25573817f9dfSMatthias Ringwald 25583817f9dfSMatthias Ringwald static bool btstack_is_null(uint8_t * data, uint16_t size){ 25593817f9dfSMatthias Ringwald uint16_t i; 25603817f9dfSMatthias Ringwald for (i=0; i < size ; i++){ 25613817f9dfSMatthias Ringwald if (data[i] != 0) { 25623817f9dfSMatthias Ringwald return false; 25633817f9dfSMatthias Ringwald } 25643817f9dfSMatthias Ringwald } 25653817f9dfSMatthias Ringwald return true; 25663817f9dfSMatthias Ringwald } 25673817f9dfSMatthias Ringwald 2568b3c4163bSMatthias Ringwald static void hci_ssp_assess_security_on_io_cap_request(hci_connection_t * conn){ 25692dd8985bSMatthias Ringwald // get requested security level 25702dd8985bSMatthias Ringwald gap_security_level_t requested_security_level = conn->requested_security_level; 25712dd8985bSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 25722dd8985bSMatthias Ringwald requested_security_level = LEVEL_4; 25732dd8985bSMatthias Ringwald } 25742dd8985bSMatthias Ringwald 2575b3c4163bSMatthias Ringwald // assess security: LEVEL 4 requires SC 25762dd8985bSMatthias Ringwald // skip this preliminary test if remote features are not available yet to work around potential issue in ESP32 controller 25772dd8985bSMatthias Ringwald if ((requested_security_level == LEVEL_4) && 25782dd8985bSMatthias Ringwald ((conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0) && 25792dd8985bSMatthias Ringwald !hci_remote_sc_enabled(conn)){ 2580b3c4163bSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 2581b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2582b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2583b3c4163bSMatthias Ringwald return; 2584b3c4163bSMatthias Ringwald } 2585b3c4163bSMatthias Ringwald 2586b3c4163bSMatthias Ringwald // assess security based on io capabilities 2587b3c4163bSMatthias Ringwald if (conn->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 2588b3c4163bSMatthias Ringwald // responder: fully validate io caps of both sides as well as OOB data 2589b3c4163bSMatthias Ringwald bool security_possible = false; 2590b3c4163bSMatthias Ringwald security_possible = hci_ssp_security_level_possible_for_io_cap(requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io); 2591b3c4163bSMatthias Ringwald 2592b3c4163bSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 2593b3c4163bSMatthias Ringwald // We assume that both Controller can reach LEVEL 4, if one side has received P-192 and the other has received P-256, 2594b3c4163bSMatthias Ringwald // so we merge the OOB data availability 2595b3c4163bSMatthias Ringwald uint8_t have_oob_data = conn->io_cap_response_oob_data; 2596b3c4163bSMatthias Ringwald if (conn->classic_oob_c_192 != NULL){ 2597b3c4163bSMatthias Ringwald have_oob_data |= 1; 2598b3c4163bSMatthias Ringwald } 2599b3c4163bSMatthias Ringwald if (conn->classic_oob_c_256 != NULL){ 2600b3c4163bSMatthias Ringwald have_oob_data |= 2; 2601b3c4163bSMatthias Ringwald } 2602b3c4163bSMatthias Ringwald // for up to Level 3, either P-192 as well as P-256 will do 2603b3c4163bSMatthias Ringwald // if we don't support SC, then a) conn->classic_oob_c_256 will be NULL and b) remote should not report P-256 available 2604b3c4163bSMatthias Ringwald // if remote does not SC, we should not receive P-256 data either 2605b3c4163bSMatthias Ringwald if ((requested_security_level <= LEVEL_3) && (have_oob_data != 0)){ 2606b3c4163bSMatthias Ringwald security_possible = true; 2607b3c4163bSMatthias Ringwald } 2608b3c4163bSMatthias Ringwald // for Level 4, P-256 is needed 2609b3c4163bSMatthias Ringwald if ((requested_security_level == LEVEL_4 && ((have_oob_data & 2) != 0))){ 2610b3c4163bSMatthias Ringwald security_possible = true; 2611b3c4163bSMatthias Ringwald } 2612b3c4163bSMatthias Ringwald #endif 2613b3c4163bSMatthias Ringwald 2614b3c4163bSMatthias Ringwald if (security_possible == false){ 26152dd8985bSMatthias Ringwald log_info("IOCap/OOB insufficient for level %u -> abort", requested_security_level); 2616b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2617b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2618b3c4163bSMatthias Ringwald return; 2619b3c4163bSMatthias Ringwald } 2620b3c4163bSMatthias Ringwald } else { 2621b3c4163bSMatthias Ringwald // initiator: remote io cap not yet, only check if we have ability for MITM protection if requested and OOB is not supported 26221fe968f5SMatthias Ringwald #ifndef ENABLE_CLASSIC_PAIRING_OOB 26231fe968f5SMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 2624b3c4163bSMatthias Ringwald if ((conn->requested_security_level >= LEVEL_3) && (hci_stack->ssp_io_capability >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT)){ 2625b3c4163bSMatthias Ringwald log_info("Level 3+ required, but no input/output -> abort"); 2626b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2627b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2628b3c4163bSMatthias Ringwald return; 2629b3c4163bSMatthias Ringwald } 2630b3c4163bSMatthias Ringwald #endif 26311fe968f5SMatthias Ringwald #endif 2632b3c4163bSMatthias Ringwald } 2633b3c4163bSMatthias Ringwald 2634b3c4163bSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 2635b3c4163bSMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 2636b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 2637b3c4163bSMatthias Ringwald } else { 2638b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2639b3c4163bSMatthias Ringwald } 2640b3c4163bSMatthias Ringwald #endif 2641b3c4163bSMatthias Ringwald } 2642b3c4163bSMatthias Ringwald 264317c6fe5cSMatthias Ringwald #endif 264417c6fe5cSMatthias Ringwald 2645c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 26464f781026SMatthias Ringwald 26474f781026SMatthias Ringwald uint16_t event_length = packet[1]; 26484f781026SMatthias Ringwald 26494f781026SMatthias Ringwald // assert packet is complete 26504ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 26514f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 26524f781026SMatthias Ringwald return; 26534f781026SMatthias Ringwald } 26544f781026SMatthias Ringwald 26554f781026SMatthias Ringwald bd_addr_type_t addr_type; 26564f781026SMatthias Ringwald hci_con_handle_t handle; 26574f781026SMatthias Ringwald hci_connection_t * conn; 26584f781026SMatthias Ringwald int i; 26594f781026SMatthias Ringwald int create_connection_cmd; 26604f781026SMatthias Ringwald 26614f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 26625e91d96cSMatthias Ringwald hci_link_type_t link_type; 266348f33f37SMatthias Ringwald bd_addr_t addr; 26644f781026SMatthias Ringwald #endif 26654f781026SMatthias Ringwald 26664f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 26674f781026SMatthias Ringwald 26684f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 26694f781026SMatthias Ringwald 26704f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 26714f781026SMatthias Ringwald handle_command_complete_event(packet, size); 267256cf178bSmatthias.ringwald break; 267356cf178bSmatthias.ringwald 26747ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 26756bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 26766bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2677229331c6SMatthias Ringwald 2678229331c6SMatthias Ringwald // check command status to detected failed outgoing connections 2679c57fa566SMatthias Ringwald create_connection_cmd = 0; 2680c57fa566SMatthias Ringwald #ifdef ENABLE_CLASSIC 2681c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){ 2682c57fa566SMatthias Ringwald create_connection_cmd = 1; 2683c57fa566SMatthias Ringwald } 26841f34df2cSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_accept_synchronous_connection)){ 26851f34df2cSMatthias Ringwald create_connection_cmd = 1; 26861f34df2cSMatthias Ringwald } 2687c57fa566SMatthias Ringwald #endif 2688c57fa566SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2689c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_le_create_connection)){ 2690c57fa566SMatthias Ringwald create_connection_cmd = 1; 2691c57fa566SMatthias Ringwald } 2692c57fa566SMatthias Ringwald #endif 2693c57fa566SMatthias Ringwald if (create_connection_cmd) { 2694229331c6SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 26958da98cbeSMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 26966ea9315cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 26976ea9315cSMatthias 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); 2698229331c6SMatthias Ringwald 2699229331c6SMatthias Ringwald // reset outgoing address info 2700229331c6SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2701229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2702229331c6SMatthias Ringwald 27036ea9315cSMatthias Ringwald // on error 27046ea9315cSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2705f75e06adSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 27066ea9315cSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 27076ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2708f7e6a692SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 27096ea9315cSMatthias Ringwald } 2710f75e06adSMatthias Ringwald #endif 2711229331c6SMatthias Ringwald // error => outgoing connection failed 27126ea9315cSMatthias Ringwald if (conn != NULL){ 2713229331c6SMatthias Ringwald hci_handle_connection_failed(conn, status); 2714229331c6SMatthias Ringwald } 2715229331c6SMatthias Ringwald } 27166ea9315cSMatthias Ringwald } 2717beb3c81dSMatthias Ringwald 2718beb3c81dSMatthias Ringwald #ifdef ENABLE_CLASSIC 2719beb3c81dSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_inquiry)) { 2720beb3c81dSMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2721beb3c81dSMatthias Ringwald log_info("command status (inquiry), status %x", status); 2722beb3c81dSMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 2723beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2724beb3c81dSMatthias Ringwald } else { 2725beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2726beb3c81dSMatthias Ringwald } 2727beb3c81dSMatthias Ringwald } 2728beb3c81dSMatthias Ringwald #endif 27297ec5eeaaSmatthias.ringwald break; 27307ec5eeaaSmatthias.ringwald 27312e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 27329784dac1SMatthias Ringwald if (size < 3) return; 27339784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 27344ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 27359784dac1SMatthias Ringwald uint16_t offset = 3; 27369784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 27374ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 27384ea43905SMatthias Ringwald offset += 2u; 2739f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 27404ea43905SMatthias Ringwald offset += 2u; 27412e440c8aS[email protected] 27425061f3afS[email protected] conn = hci_connection_for_handle(handle); 274356cf178bSmatthias.ringwald if (!conn){ 27449da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 274556cf178bSmatthias.ringwald continue; 274656cf178bSmatthias.ringwald } 274723bed257S[email protected] 2748ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets){ 2749ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 2750e35edcc1S[email protected] } else { 2751ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 2752ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 2753e35edcc1S[email protected] } 2754ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 2755760b20efSMatthias Ringwald 2756760b20efSMatthias Ringwald #ifdef ENABLE_CLASSIC 2757760b20efSMatthias Ringwald // For SCO, we do the can_send_now_check here 2758760b20efSMatthias Ringwald hci_notify_if_sco_can_send_now(); 2759760b20efSMatthias Ringwald #endif 276056cf178bSmatthias.ringwald } 27616772a24cSmatthias.ringwald break; 27622e440c8aS[email protected] } 276335454696SMatthias Ringwald 276435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2765f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 2766f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 2767f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2768f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2769f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2770f5875de5SMatthias Ringwald } 2771f5875de5SMatthias Ringwald break; 2772b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 2773b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 2774b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 2775b7f1ee76SMatthias Ringwald } 2776b7f1ee76SMatthias Ringwald break; 27771f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 2778724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 27795e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 278072cf8859SMatthias Ringwald 278172cf8859SMatthias Ringwald // CVE-2020-26555: reject incoming connection from device with same BD ADDR 278272cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0){ 278372cf8859SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 278472cf8859SMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 278572cf8859SMatthias Ringwald break; 278672cf8859SMatthias Ringwald } 278772cf8859SMatthias Ringwald 278807e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 27895e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 27904536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 279107e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 279207e010b6SMilanka Ringwald break; 279307e010b6SMilanka Ringwald } 279407e010b6SMilanka Ringwald } 279507e010b6SMilanka Ringwald 279637eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 27975e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 27985e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 27992e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 28001f7b95a1Smatthias.ringwald if (!conn) { 28015293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 28021f7b95a1Smatthias.ringwald } 2803ce4c8fabSmatthias.ringwald if (!conn) { 2804ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 28054536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 2806058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 2807f23819bcSMatthias Ringwald hci_run(); 2808f23819bcSMatthias Ringwald // avoid event to higher layer 2809f23819bcSMatthias Ringwald return; 2810ce4c8fabSmatthias.ringwald } 28115cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 281232ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 2813f3a16b9aSMatthias Ringwald // store info about eSCO 28145e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 281576ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 2816f3a16b9aSMatthias Ringwald } 281732ab9390Smatthias.ringwald hci_run(); 28181f7b95a1Smatthias.ringwald break; 28191f7b95a1Smatthias.ringwald 28206772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 2821fe1ed1b8Smatthias.ringwald // Connection management 2822724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 28239da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 2824f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 28252e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2826fe1ed1b8Smatthias.ringwald if (conn) { 2827b448a0e7Smatthias.ringwald if (!packet[2]){ 2828c8e4258aSmatthias.ringwald conn->state = OPEN; 2829f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 28306909f064SMatthias Ringwald 2831e6c51921SMatthias Ringwald // queue set supervision timeout if we're master 2832d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 28338daf94bcSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT); 28346909f064SMatthias Ringwald } 28356909f064SMatthias Ringwald 2836c785ef68Smatthias.ringwald // restart timer 2837528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2838528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 2839c785ef68Smatthias.ringwald 284018c4a0a3SMatthias Ringwald // trigger remote features for dedicated bonding 284118c4a0a3SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 284218c4a0a3SMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 284318c4a0a3SMatthias Ringwald } 284418c4a0a3SMatthias Ringwald 28459da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 284643bfb1bdSmatthias.ringwald 284743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 2848b448a0e7Smatthias.ringwald } else { 28490bbba85bSMatthias Ringwald // connection failed 28500bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 2851fe1ed1b8Smatthias.ringwald } 2852fe1ed1b8Smatthias.ringwald } 28536772a24cSmatthias.ringwald break; 2854fe1ed1b8Smatthias.ringwald 285544d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 2856724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 28571f34df2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 28581f34df2cSMatthias Ringwald log_info("Synchronous Connection Complete for %p (status=%u) %s", conn, packet[2], bd_addr_to_str(addr)); 28591a06f663S[email protected] if (packet[2]){ 286044d0e3d5S[email protected] // connection failed 28611f34df2cSMatthias Ringwald if (conn){ 28621f34df2cSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 28631f34df2cSMatthias Ringwald } 286444d0e3d5S[email protected] break; 286544d0e3d5S[email protected] } 2866e35edcc1S[email protected] if (!conn) { 2867e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2868e35edcc1S[email protected] } 2869e35edcc1S[email protected] if (!conn) { 2870e35edcc1S[email protected] break; 2871e35edcc1S[email protected] } 28721a06f663S[email protected] conn->state = OPEN; 2873f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2874ee752bb8SMatthias Ringwald 2875ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2876ee752bb8SMatthias Ringwald // update SCO 2877ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 2878ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 2879ee752bb8SMatthias Ringwald } 2880f234b250SMatthias Ringwald // trigger can send now 2881f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 28821972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 2883f234b250SMatthias Ringwald } 2884ee752bb8SMatthias Ringwald #endif 2885cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 2886cb70c5abSMatthias Ringwald // configure sco transport 2887cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 2888cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 2889cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 2890cb70c5abSMatthias Ringwald } 2891cb70c5abSMatthias Ringwald #endif 289244d0e3d5S[email protected] break; 289344d0e3d5S[email protected] 2894afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 2895f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2896afd4e962S[email protected] conn = hci_connection_for_handle(handle); 2897afd4e962S[email protected] if (!conn) break; 2898afd4e962S[email protected] if (!packet[2]){ 28992f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 29002f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 29012f5c44baSMatthias Ringwald 29025ccef624SMatthias Ringwald // read extended features if possible 29031ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES) 29041ffa425cSMatthias Ringwald && ((conn->remote_supported_features[0] & 2) != 0)) { 29055ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 29065ccef624SMatthias Ringwald break; 29075ccef624SMatthias Ringwald } 29085ccef624SMatthias Ringwald } 29095ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 29105ccef624SMatthias Ringwald break; 29115ccef624SMatthias Ringwald 29125ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 29135ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 29145ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 29155ccef624SMatthias Ringwald if (!conn) break; 29165ccef624SMatthias Ringwald // status = ok, page = 1 291750c51a77SMatthias Ringwald if (!packet[2]) { 291850c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 291950c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 29205ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 292150c51a77SMatthias Ringwald bool done = false; 292250c51a77SMatthias Ringwald switch (page_number){ 292350c51a77SMatthias Ringwald case 1: 29242f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 292550c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 292650c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 292750c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 292850c51a77SMatthias Ringwald } else { 292950c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 293050c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 293150c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 293250c51a77SMatthias Ringwald } 293350c51a77SMatthias Ringwald done = true; 293450c51a77SMatthias Ringwald } 293550c51a77SMatthias Ringwald break; 293650c51a77SMatthias Ringwald case 2: 293750c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 293850c51a77SMatthias Ringwald done = true; 293950c51a77SMatthias Ringwald break; 294050c51a77SMatthias Ringwald default: 294150c51a77SMatthias Ringwald break; 294250c51a77SMatthias Ringwald } 294350c51a77SMatthias Ringwald if (!done) break; 2944afd4e962S[email protected] } 2945de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 2946afd4e962S[email protected] break; 2947afd4e962S[email protected] 29487fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 2949308eeaffSMatthias Ringwald #ifndef ENABLE_EXPLICIT_LINK_KEY_REPLY 295063c0b2fdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 295163c0b2fdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 295263c0b2fdSMatthias Ringwald if (!conn) break; 295363c0b2fdSMatthias Ringwald 295463c0b2fdSMatthias Ringwald // lookup link key in db if not cached 295563c0b2fdSMatthias Ringwald if ((conn->link_key_type == INVALID_LINK_KEY) && (hci_stack->link_key_db != NULL)){ 295663c0b2fdSMatthias Ringwald hci_stack->link_key_db->get_link_key(conn->address, conn->link_key, &conn->link_key_type); 295763c0b2fdSMatthias Ringwald } 295863c0b2fdSMatthias Ringwald 295963c0b2fdSMatthias Ringwald // response sent by hci_run() 296063c0b2fdSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_HANDLE_LINK_KEY_REQUEST; 296163c0b2fdSMatthias Ringwald #endif 2962608f51bbSMatthias Ringwald break; 29637fde4af9Smatthias.ringwald 29649ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 29651714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 2966f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 29679ab95c90S[email protected] if (!conn) break; 29681714cbbdSMatthias Ringwald 29691714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 29701714cbbdSMatthias Ringwald 29713817f9dfSMatthias Ringwald // CVE-2020-26555: ignore NULL link key 29723817f9dfSMatthias Ringwald // default link_key_type = INVALID_LINK_KEY asserts that NULL key won't be used for encryption 29733817f9dfSMatthias Ringwald if (btstack_is_null(&packet[8], 16)) break; 29743817f9dfSMatthias Ringwald 29757bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 29769ab95c90S[email protected] // Change Connection Encryption keeps link key type 29779ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 29789ab95c90S[email protected] conn->link_key_type = link_key_type; 29799ab95c90S[email protected] } 29803817f9dfSMatthias Ringwald 2981e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 2982e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 2983e9f98c4aSMatthias Ringwald 2984bec5f683SMatthias Ringwald // only store link key: 2985bec5f683SMatthias Ringwald // - if bondable enabled 2986bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 29876edaed7fSMatthias Ringwald // - if security level sufficient 29886edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 2989bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 2990bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 2991532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 2992532454f9SMatthias Ringwald if (!remote_bonding){ 2993bec5f683SMatthias Ringwald break; 2994bec5f683SMatthias Ringwald } 2995bec5f683SMatthias Ringwald } 299655597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 29977fde4af9Smatthias.ringwald break; 29989ab95c90S[email protected] } 29997fde4af9Smatthias.ringwald 30007fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 30011714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 30021714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30031714cbbdSMatthias Ringwald if (!conn) break; 30041714cbbdSMatthias Ringwald 30051714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 3006a800d95eSMatthias Ringwald // abort pairing if: non-bondable mode (pin code request is not forwarded to app) 30073a9fb326S[email protected] if (!hci_stack->bondable ){ 30088daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 30091714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 3010f8fb5f6eS[email protected] hci_run(); 3011f8fb5f6eS[email protected] return; 30124c57c146S[email protected] } 3013a800d95eSMatthias Ringwald // abort pairing if: LEVEL_4 required (pin code request is not forwarded to app) 3014a800d95eSMatthias Ringwald if ((hci_stack->gap_secure_connections_only_mode) || (conn->requested_security_level == LEVEL_4)){ 3015a800d95eSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 301650dcc63cSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 301750dcc63cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 301850dcc63cSMatthias Ringwald hci_run(); 301950dcc63cSMatthias Ringwald return; 302050dcc63cSMatthias Ringwald } 30217fde4af9Smatthias.ringwald break; 30227fde4af9Smatthias.ringwald 302350d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 302450d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 302550d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 302650d7398cSMatthias Ringwald if (!conn) break; 30271714cbbdSMatthias Ringwald 30288daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE); 30291714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 303050d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 3031e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 3032d22b82d6SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3033d22b82d6SMatthias Ringwald conn->io_cap_response_oob_data = hci_event_io_capability_response_get_oob_data_present(packet); 3034d22b82d6SMatthias Ringwald #endif 303550d7398cSMatthias Ringwald break; 303650d7398cSMatthias Ringwald 30371d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 30381714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 30391714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30401714cbbdSMatthias Ringwald if (!conn) break; 30411714cbbdSMatthias Ringwald 3042c950c316SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 30439671e615SMatthias Ringwald hci_connection_timestamp(conn); 30441714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 3045dbe1a790S[email protected] break; 3046dbe1a790S[email protected] 30471849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 30481849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 30497ca4a7edSMatthias Ringwald hci_event_remote_oob_data_request_get_bd_addr(packet, addr); 30507ca4a7edSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30517ca4a7edSMatthias Ringwald if (!conn) break; 30527ca4a7edSMatthias Ringwald 30537ca4a7edSMatthias Ringwald hci_connection_timestamp(conn); 30547ca4a7edSMatthias Ringwald 30557ca4a7edSMatthias Ringwald hci_pairing_started(conn, true); 30567ca4a7edSMatthias Ringwald 30577ca4a7edSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 30581849becdSMatthias Ringwald break; 30591849becdSMatthias Ringwald #endif 30601849becdSMatthias Ringwald 3061dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 306217c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 3063367aedc0SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3064367aedc0SMatthias Ringwald if (!conn) break; 3065367aedc0SMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 3066367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 30678daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 3068367aedc0SMatthias Ringwald }; 3069367aedc0SMatthias Ringwald } else { 3070367aedc0SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3071367aedc0SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 3072367aedc0SMatthias Ringwald // don't forward event to app 3073367aedc0SMatthias Ringwald hci_run(); 3074367aedc0SMatthias Ringwald return; 3075367aedc0SMatthias Ringwald } 3076dbe1a790S[email protected] break; 3077dbe1a790S[email protected] 3078dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 3079367aedc0SMatthias Ringwald // Pairing using Passkey results in MITM protection. If Level 4 is required, support for SC is validated on IO Cap Request 3080367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 30818daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 3082367aedc0SMatthias Ringwald }; 30831d6b20aeS[email protected] break; 30841849becdSMatthias Ringwald 30853dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 30863dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 30873dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 30883dce6128SMatthias Ringwald if (!conn) break; 30893dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 30903dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 30913dce6128SMatthias Ringwald break; 309235454696SMatthias Ringwald #endif 30931d6b20aeS[email protected] 3094f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 3095254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 3096f0944df2S[email protected] conn = hci_connection_for_handle(handle); 3097f0944df2S[email protected] if (!conn) break; 30984ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 3099254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 3100254b78eeSMatthias Ringwald if (encryption_enabled){ 3101573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 3102573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 31038daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 3104573897a0SMatthias Ringwald } 3105573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 3106573897a0SMatthias Ringwald else { 3107fcaf38b9SMatthias Ringwald 3108fcaf38b9SMatthias Ringwald // dedicated bonding: send result and disconnect 3109fcaf38b9SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 3110fcaf38b9SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 3111fcaf38b9SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 3112fcaf38b9SMatthias Ringwald conn->bonding_status = packet[2]; 3113fcaf38b9SMatthias Ringwald break; 3114fcaf38b9SMatthias Ringwald } 3115fcaf38b9SMatthias Ringwald 3116254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 311736c3546bSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type); 3118254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 3119edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 3120254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 31219ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 3122254b78eeSMatthias Ringwald break; 3123254b78eeSMatthias Ringwald } 3124254b78eeSMatthias Ringwald 3125aa2fe986SMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 3126aa2fe986SMatthias Ringwald if (connected_uses_aes_ccm){ 3127aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3128aa2fe986SMatthias Ringwald } 3129aa2fe986SMatthias Ringwald 3130aa2fe986SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3131aa2fe986SMatthias Ringwald // work around for issue with PTS dongle 3132aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3133aa2fe986SMatthias Ringwald #endif 31341ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE)){ 31356e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 3136573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 31376e058d3fSMatthias Ringwald } else { 31386e058d3fSMatthias Ringwald // if not, pretend everything is perfect 31399866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 31406e058d3fSMatthias Ringwald } 3141573897a0SMatthias Ringwald } 3142573897a0SMatthias Ringwald #endif 3143f0944df2S[email protected] } else { 31448daf94bcSMatthias Ringwald conn->authentication_flags &= ~AUTH_FLAG_CONNECTION_ENCRYPTED; 3145f0944df2S[email protected] } 3146ad83dc6aS[email protected] } 3147573897a0SMatthias Ringwald 3148f0944df2S[email protected] break; 3149f0944df2S[email protected] 315035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 31511eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 3152abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 31531eb2563eS[email protected] conn = hci_connection_for_handle(handle); 31541eb2563eS[email protected] if (!conn) break; 3155ad83dc6aS[email protected] 3156dbd5dcc3SMatthias Ringwald // clear authentication active flag 3157dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 31581714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 3159dbd5dcc3SMatthias Ringwald 3160abdad579SMatthias Ringwald // authenticated only if auth status == 0 3161abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 3162abdad579SMatthias Ringwald // authenticated 31638daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3164abdad579SMatthias Ringwald 3165131ef17aSMatthias Ringwald // If not already encrypted, start encryption 31668daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0){ 31671eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3168ad83dc6aS[email protected] break; 3169ad83dc6aS[email protected] } 3170abdad579SMatthias Ringwald } 3171abdad579SMatthias Ringwald 3172abdad579SMatthias Ringwald // emit updated security level 31731eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 31741eb2563eS[email protected] break; 31751714cbbdSMatthias Ringwald 31761714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 31771714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 31781714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 31791714cbbdSMatthias Ringwald if (!conn) break; 31801714cbbdSMatthias Ringwald 3181aa2fe986SMatthias Ringwald // treat successfully paired connection as authenticated 3182aa2fe986SMatthias Ringwald if (hci_event_simple_pairing_complete_get_status(packet) == ERROR_CODE_SUCCESS){ 3183f3aafff1SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3184aa2fe986SMatthias Ringwald } 3185aa2fe986SMatthias Ringwald 31861714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 31871714cbbdSMatthias Ringwald break; 318835454696SMatthias Ringwald #endif 318934d2123cS[email protected] 319086805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 3191ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 3192ccda6e14S[email protected] // see end of function, too. 3193a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 3194a4f30ec0S[email protected] if (packet[2]) break; // status != 0 3195f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 319681d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 31974ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3198c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 31994ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 320081d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 3201c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 3202c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 320381d2bdb2SMatthias Ringwald if (release_buffer){ 320481d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 320581d2bdb2SMatthias Ringwald } 3206c6a37cfdSMatthias Ringwald } 3207c6a37cfdSMatthias Ringwald } 320835454696SMatthias Ringwald 3209c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 3210c6a37cfdSMatthias Ringwald if (!conn) break; 32111714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 32121714cbbdSMatthias Ringwald // pairing failed if it was ongoing 32131714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 32141714cbbdSMatthias Ringwald #endif 3215046ec007SMatthias Ringwald 3216046ec007SMatthias Ringwald // emit dedicatd bonding event 3217046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 3218046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 3219046ec007SMatthias Ringwald } 3220046ec007SMatthias Ringwald 3221459d27b9SMatthias Ringwald // mark connection for shutdown, stop timers, reset state 3222459d27b9SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 3223459d27b9SMatthias Ringwald hci_connection_stop_timer(conn); 3224459d27b9SMatthias Ringwald hci_connection_init(conn); 3225459d27b9SMatthias Ringwald 322635454696SMatthias Ringwald #ifdef ENABLE_BLE 3227d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3228046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 32292b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 3230bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 32319a2e4658SMatthias Ringwald } 323235454696SMatthias Ringwald #endif 3233d70217a2SMatthias Ringwald #endif 3234ccda6e14S[email protected] break; 32356772a24cSmatthias.ringwald 3236c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 3237313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 3238d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 3239c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 32407586ee35S[email protected] } else { 32417586ee35S[email protected] // if no special requests, just reboot stack 32427586ee35S[email protected] hci_power_control_off(); 32437586ee35S[email protected] hci_power_control_on(); 3244c68bdf90Smatthias.ringwald } 3245c68bdf90Smatthias.ringwald break; 3246c68bdf90Smatthias.ringwald 32470e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 32485cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 32495cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 3250c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 3251f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 3252c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3253c4c88f1bSJakob Krantz if (!conn) break; 32545cf766e8SMatthias Ringwald conn->role = packet[9]; 32555cf766e8SMatthias Ringwald break; 32560e6f3837SMatthias Ringwald #endif 32575cf766e8SMatthias Ringwald 325863fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 3259d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 32604fa24b5fS[email protected] if (hci_transport_synchronous()) { 326163fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 32624fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 32634fa24b5fS[email protected] } 326481d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 3265d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 3266d051460cS[email protected] hci_release_packet_buffer(); 3267701e3307SMatthias Ringwald 326863fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 326963fa3374SMatthias Ringwald 327035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 327163fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 3272701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 327335454696SMatthias Ringwald #endif 32746b4af23dS[email protected] break; 32756b4af23dS[email protected] 327635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 327763fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 327863fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 32791972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 328063fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 328163fa3374SMatthias Ringwald return; 32821cfb383eSMatthias Ringwald 32831cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 32841cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 32851cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 32861cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 32879784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 32881cfb383eSMatthias Ringwald break; 328935454696SMatthias Ringwald #endif 329063fa3374SMatthias Ringwald 3291a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 32925909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 32935909f7f2Smatthias.ringwald switch (packet[2]){ 3294d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 329557c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 32964f4e0224SMatthias Ringwald // log_info("advertising report received"); 32976fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 329857c9da5bS[email protected] le_handle_advertisement_report(packet, size); 32997bdc6798S[email protected] break; 3300d70217a2SMatthias Ringwald #endif 33015909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 33020ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 33035909f7f2Smatthias.ringwald break; 33045909f7f2Smatthias.ringwald 3305f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3306c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3307c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3308c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3309c9db5c21SMilanka Ringwald if (!conn) break; 3310c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3311c9db5c21SMilanka Ringwald break; 331265a46ef3S[email protected] 331373cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 331473cd8a2aSMatthias Ringwald // connection 331573cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 331673cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 331773cd8a2aSMatthias Ringwald if (conn) { 331873cd8a2aSMatthias Ringwald // read arguments 331973cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 332073cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 332173cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 332273cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 332373cd8a2aSMatthias Ringwald 332473cd8a2aSMatthias Ringwald // validate against current connection parameter range 332573cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 332673cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 332773cd8a2aSMatthias 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); 332873cd8a2aSMatthias Ringwald if (update_parameter){ 332973cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 333073cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 333173cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 333273cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 333373cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 333473cd8a2aSMatthias Ringwald } else { 3335c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 333673cd8a2aSMatthias Ringwald } 333773cd8a2aSMatthias Ringwald } 333873cd8a2aSMatthias Ringwald break; 33390f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 33400f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 33410f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 33420f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 33430f3b27c5SMatthias Ringwald if (conn) { 33440f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 33450f3b27c5SMatthias Ringwald } 33460f3b27c5SMatthias Ringwald break; 33470f3b27c5SMatthias Ringwald #endif 33485909f7f2Smatthias.ringwald default: 33495909f7f2Smatthias.ringwald break; 33505909f7f2Smatthias.ringwald } 33515909f7f2Smatthias.ringwald break; 33525909f7f2Smatthias.ringwald #endif 33530b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 33540b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 33550b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 33560b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 33570b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 33580b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 33590b36101dSMatthias Ringwald break; 33600b36101dSMatthias Ringwald default: 33610b36101dSMatthias Ringwald break; 33620b36101dSMatthias Ringwald } 33630b36101dSMatthias Ringwald break; 33646772a24cSmatthias.ringwald default: 33656772a24cSmatthias.ringwald break; 3366fe1ed1b8Smatthias.ringwald } 3367fe1ed1b8Smatthias.ringwald 336867c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 336989db417bSmatthias.ringwald 337086805605S[email protected] // notify upper stack 3371d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 337294ab26f8Smatthias.ringwald 337386805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 3374797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 3375f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 337605ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 3377046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 3378797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 337905ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 3380b0136355SMatthias Ringwald } 338186805605S[email protected] } 338286805605S[email protected] 338394ab26f8Smatthias.ringwald // execute main loop 338494ab26f8Smatthias.ringwald hci_run(); 338516833f0aSmatthias.ringwald } 338616833f0aSmatthias.ringwald 338735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 33886f28d2eeSMatthias Ringwald 33896be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 33906f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 33916f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 33926f28d2eeSMatthias Ringwald 33936f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 339459c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 33955a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 33966f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 33976f28d2eeSMatthias Ringwald if (!conn) return; 33986f28d2eeSMatthias Ringwald 3399afaddd67SMatthias Ringwald // trigger send 3400afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 3401e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 3402a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 3403e4157653SMatthias Ringwald conn->sco_tx_ready++; 3404e4157653SMatthias Ringwald } 3405afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 34066f28d2eeSMatthias Ringwald } 34076f28d2eeSMatthias Ringwald 340859c33575SMatthias Ringwald 340959c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 341059c33575SMatthias Ringwald 34116f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 341259c33575SMatthias Ringwald 34136f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 341459c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 341559c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 341659c33575SMatthias Ringwald 341759c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 341859c33575SMatthias Ringwald 341959c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 3420ddbec4ceSMatthias Ringwald btstack_run_loop_remove_timer(timer); 342159c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 342259c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 342359c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 342459c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 34256f28d2eeSMatthias Ringwald } 34266be3cf7fSMatthias Ringwald #endif 34276f28d2eeSMatthias Ringwald 3428c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 34290b3f95dfSMatthias Ringwald // lookup connection struct 34302b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 34312b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 34320b3f95dfSMatthias Ringwald if (!conn) return; 34330b3f95dfSMatthias Ringwald 34346be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3435760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 3436760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 3437a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 3438760b20efSMatthias Ringwald packet[2] = 0x3c; 3439760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 3440760b20efSMatthias Ringwald size = 63; 34410b3f95dfSMatthias Ringwald } 34420b3f95dfSMatthias Ringwald } 3443760b20efSMatthias Ringwald 3444f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 3445f234b250SMatthias Ringwald // Nothing to do 3446f234b250SMatthias Ringwald } else { 344749205f5dSMatthias 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); 3448760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 34496f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 345059c33575SMatthias Ringwald 345159c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 345259c33575SMatthias Ringwald // ignore first 10 packets 34536f28d2eeSMatthias Ringwald conn->sco_rx_count++; 345459c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 345559c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 345659c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 34576f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 34586f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 345959c33575SMatthias Ringwald conn->sco_rx_count = 0; 346059c33575SMatthias Ringwald sco_schedule_tx(conn); 346159c33575SMatthias Ringwald } 346259c33575SMatthias Ringwald } else { 346359c33575SMatthias Ringwald // track expected arrival timme 346459c33575SMatthias Ringwald conn->sco_rx_count++; 346559c33575SMatthias Ringwald conn->sco_rx_ms += 7; 346659c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 346759c33575SMatthias Ringwald if (delta > 0){ 346859c33575SMatthias Ringwald conn->sco_rx_ms++; 346959c33575SMatthias Ringwald } 347059c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 34716f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 34726f28d2eeSMatthias Ringwald } 3473760b20efSMatthias Ringwald } 3474f234b250SMatthias Ringwald } 34756be3cf7fSMatthias Ringwald #endif 34766be3cf7fSMatthias Ringwald 34770b3f95dfSMatthias Ringwald // deliver to app 34780b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 34790b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 34800b3f95dfSMatthias Ringwald } 34810b3f95dfSMatthias Ringwald 3482ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3483ed325439SMatthias Ringwald // We can send one packet for each received packet 3484ed325439SMatthias Ringwald conn->sco_tx_ready++; 3485ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 3486ed325439SMatthias Ringwald #endif 3487ed325439SMatthias Ringwald 34880b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 34892b838201SMatthias Ringwald conn->num_packets_completed++; 34902b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 34912b838201SMatthias Ringwald hci_run(); 34922b838201SMatthias Ringwald #endif 3493c91d150bS[email protected] } 349435454696SMatthias Ringwald #endif 3495c91d150bS[email protected] 34960a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 34975bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 349810e830c9Smatthias.ringwald switch (packet_type) { 349910e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 350010e830c9Smatthias.ringwald event_handler(packet, size); 350110e830c9Smatthias.ringwald break; 350210e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 350310e830c9Smatthias.ringwald acl_handler(packet, size); 350410e830c9Smatthias.ringwald break; 350535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3506c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 3507c91d150bS[email protected] sco_handler(packet, size); 3508202c8a4cSMatthias Ringwald break; 350935454696SMatthias Ringwald #endif 351010e830c9Smatthias.ringwald default: 351110e830c9Smatthias.ringwald break; 351210e830c9Smatthias.ringwald } 351310e830c9Smatthias.ringwald } 351410e830c9Smatthias.ringwald 3515d6b06661SMatthias Ringwald /** 3516d6b06661SMatthias Ringwald * @brief Add event packet handler. 3517d6b06661SMatthias Ringwald */ 3518d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 3519d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 3520d6b06661SMatthias Ringwald } 3521d6b06661SMatthias Ringwald 352267f708e0SMatthias Ringwald /** 352367f708e0SMatthias Ringwald * @brief Remove event packet handler. 352467f708e0SMatthias Ringwald */ 352567f708e0SMatthias Ringwald void hci_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){ 352667f708e0SMatthias Ringwald btstack_linked_list_remove(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 352767f708e0SMatthias Ringwald } 3528d6b06661SMatthias Ringwald 3529fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 35303d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 3531fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 353216833f0aSmatthias.ringwald } 353316833f0aSmatthias.ringwald 353435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 35358abbe8b5SMatthias Ringwald /** 35368abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 35378abbe8b5SMatthias Ringwald */ 35383d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 35398abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 35408abbe8b5SMatthias Ringwald } 354135454696SMatthias Ringwald #endif 35428abbe8b5SMatthias Ringwald 354371de195eSMatthias Ringwald static void hci_state_reset(void){ 3544595bdbfbS[email protected] // no connections yet 3545595bdbfbS[email protected] hci_stack->connections = NULL; 354674308b23Smatthias.ringwald 354774308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 354874308b23Smatthias.ringwald // hci_stack->discoverable = 0; 354974308b23Smatthias.ringwald // hci_stack->connectable = 0; 355074308b23Smatthias.ringwald // hci_stack->bondable = 1; 3551b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 3552595bdbfbS[email protected] 355344935e40S[email protected] // buffer is free 355402c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 355544935e40S[email protected] 3556595bdbfbS[email protected] // no pending cmds 3557595bdbfbS[email protected] hci_stack->decline_reason = 0; 3558595bdbfbS[email protected] 3559c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 3560c214d65bSMatthias Ringwald 3561bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 3562496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 356332a12730SMatthias Ringwald hci_stack->page_timeout = 0x6000; // ca. 15 sec 356432a12730SMatthias Ringwald 3565baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic = 356659d59ecfSMatthias Ringwald GAP_TASK_SET_DEFAULT_LINK_POLICY | 356759d59ecfSMatthias Ringwald GAP_TASK_SET_CLASS_OF_DEVICE | 356859d59ecfSMatthias Ringwald GAP_TASK_SET_LOCAL_NAME | 3569bc2dcc03SMatthias Ringwald GAP_TASK_SET_EIR_DATA | 357032a12730SMatthias Ringwald GAP_TASK_WRITE_SCAN_ENABLE | 357132a12730SMatthias Ringwald GAP_TASK_WRITE_PAGE_TIMEOUT; 3572bea424a5SMatthias Ringwald #endif 3573bea424a5SMatthias Ringwald 3574cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 35758d5f0979SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 35761ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 3577cf01e888SMatthias Ringwald #endif 3578cf01e888SMatthias Ringwald 3579595bdbfbS[email protected] // LE 3580b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 3581b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 3582b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 3583d70217a2SMatthias Ringwald #endif 3584d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3585a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 358673799937SMatthias Ringwald hci_stack->le_scanning_param_update = true; 3587b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3588d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3589e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 3590d70217a2SMatthias Ringwald #endif 3591a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3592a61834b6SMatthias Ringwald hci_stack->le_advertisements_active = false; 3593a61834b6SMatthias Ringwald if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_PARAMS_SET) != 0){ 3594a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3595a61834b6SMatthias Ringwald } 3596a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 3597a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3598a61834b6SMatthias Ringwald } 3599a61834b6SMatthias Ringwald #endif 3600595bdbfbS[email protected] } 3601595bdbfbS[email protected] 360235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 36032d5e09d6SMatthias Ringwald /** 36042d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 36052d5e09d6SMatthias Ringwald */ 36062d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 36072d5e09d6SMatthias Ringwald // store and open remote device db 36082d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 36092d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 36102d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 36112d5e09d6SMatthias Ringwald } 36122d5e09d6SMatthias Ringwald } 361335454696SMatthias Ringwald #endif 36142d5e09d6SMatthias Ringwald 36152d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 3616475c8125Smatthias.ringwald 36173a9fb326S[email protected] #ifdef HAVE_MALLOC 36183a9fb326S[email protected] if (!hci_stack) { 36193a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 36203a9fb326S[email protected] } 36213a9fb326S[email protected] #else 36223a9fb326S[email protected] hci_stack = &hci_stack_static; 36233a9fb326S[email protected] #endif 362466fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 36253a9fb326S[email protected] 3626475c8125Smatthias.ringwald // reference to use transport layer implementation 36273a9fb326S[email protected] hci_stack->hci_transport = transport; 3628475c8125Smatthias.ringwald 362911e23e5fSmatthias.ringwald // reference to used config 36303a9fb326S[email protected] hci_stack->config = config; 363111e23e5fSmatthias.ringwald 363226a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 363326a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 363426a9b6daSMatthias Ringwald 36358fcba05dSmatthias.ringwald // max acl payload size defined in config.h 36363a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 36378fcba05dSmatthias.ringwald 363816833f0aSmatthias.ringwald // register packet handlers with transport 363910e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 3640f5454fc6Smatthias.ringwald 36413a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 3642e2386ba1S[email protected] 3643e2386ba1S[email protected] // class of device 36443a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 3645a45d6b9fS[email protected] 3646f20168b8Smatthias.ringwald // bondable by default 3647f20168b8Smatthias.ringwald hci_stack->bondable = 1; 3648f20168b8Smatthias.ringwald 3649e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 365063168530SMatthias Ringwald // classic name 365163168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 3652c4c88f1bSJakob Krantz 3653c4c88f1bSJakob Krantz // Master slave policy 3654c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 3655170fafaeSMatthias Ringwald 3656b4eb4420SMatthias Ringwald // Allow Role Switch 3657b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 3658b4eb4420SMatthias Ringwald 365978315a58SMatthias Ringwald // Default / minimum security level = 2 366078315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 366178315a58SMatthias Ringwald 36625dbec6b3SMatthias Ringwald // Default Security Mode 4 36635dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 36645dbec6b3SMatthias Ringwald 3665cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 3666cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 3667d821984bSMatthias Ringwald 3668d821984bSMatthias Ringwald // Link Supervision Timeout 3669d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 3670d821984bSMatthias Ringwald 3671e9f343c8SMatthias Ringwald #endif 367263168530SMatthias Ringwald 367363048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 36743a9fb326S[email protected] hci_stack->ssp_enable = 1; 36753a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 36763a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 36773a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 367869a97523S[email protected] 36795d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 36805d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 36815d23aae8SMatthias Ringwald 3682fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 3683fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 3684d950d659SMatthias Ringwald 3685831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3686831711daSMatthias Ringwald // connection parameter to use for outgoing connections 3687cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 3688cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 3689831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 3690831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 3691831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 3692831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 3693831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 3694831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 36957261e5d8SMatthias Ringwald 36967261e5d8SMatthias Ringwald // default LE Scanning 36978b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 36988b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 36998b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 3700831711daSMatthias Ringwald #endif 3701831711daSMatthias Ringwald 37022b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 37032b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 37042b6ab3e6SMatthias Ringwald #endif 37052b6ab3e6SMatthias Ringwald 3706831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 3707831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 3708831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 3709831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 3710831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 3711831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 3712831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 3713831711daSMatthias Ringwald 3714595bdbfbS[email protected] hci_state_reset(); 3715475c8125Smatthias.ringwald } 3716475c8125Smatthias.ringwald 37174688f216SMatthias Ringwald void hci_deinit(void){ 37184688f216SMatthias Ringwald #ifdef HAVE_MALLOC 37194688f216SMatthias Ringwald if (hci_stack) { 37204688f216SMatthias Ringwald free(hci_stack); 37214688f216SMatthias Ringwald } 37224688f216SMatthias Ringwald #endif 37234688f216SMatthias Ringwald hci_stack = NULL; 3724b5bbcbf4SMatthias Ringwald 3725b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 37264688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 3727b5bbcbf4SMatthias Ringwald #endif 37284688f216SMatthias Ringwald } 37294688f216SMatthias Ringwald 37303fb36a29SMatthias Ringwald /** 37313fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 37323fb36a29SMatthias Ringwald */ 37333fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 37343fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 37353fb36a29SMatthias Ringwald 37363fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 37373fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 37383fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 37393fb36a29SMatthias Ringwald } 37403fb36a29SMatthias Ringwald } 37413fb36a29SMatthias Ringwald 3742fb55bd0aSMatthias Ringwald /** 3743d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 3744fb55bd0aSMatthias Ringwald */ 3745fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 3746fb55bd0aSMatthias Ringwald // references to used control implementation 3747fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 3748d0b87befSMatthias Ringwald // init with transport config 3749d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 3750fb55bd0aSMatthias Ringwald } 3751fb55bd0aSMatthias Ringwald 37521f9eb2ccSMatthias Ringwald static void hci_discard_connections(void){ 37531f9eb2ccSMatthias Ringwald btstack_linked_list_iterator_t lit; 37541f9eb2ccSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 37551f9eb2ccSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 37561f9eb2ccSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 37571f9eb2ccSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 37581f9eb2ccSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 37591f9eb2ccSMatthias Ringwald hci_shutdown_connection(connection); 37601f9eb2ccSMatthias Ringwald } 37611f9eb2ccSMatthias Ringwald } 37621f9eb2ccSMatthias Ringwald 376371de195eSMatthias Ringwald void hci_close(void){ 3764e6d6524dSMatthias Ringwald 3765e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 3766404843c1Smatthias.ringwald // close remote device db 3767a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 3768a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 3769404843c1Smatthias.ringwald } 3770e6d6524dSMatthias Ringwald #endif 37717224be7eSMatthias Ringwald 37721f9eb2ccSMatthias Ringwald hci_discard_connections(); 37737224be7eSMatthias Ringwald 3774f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 37753a9fb326S[email protected] 37763a9fb326S[email protected] #ifdef HAVE_MALLOC 37773a9fb326S[email protected] free(hci_stack); 37783a9fb326S[email protected] #endif 37793a9fb326S[email protected] hci_stack = NULL; 3780404843c1Smatthias.ringwald } 3781404843c1Smatthias.ringwald 3782cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3783cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 3784cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 3785cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 3786cb70c5abSMatthias Ringwald } 3787cb70c5abSMatthias Ringwald #endif 3788cb70c5abSMatthias Ringwald 378935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3790170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 3791170fafaeSMatthias Ringwald // validate ranage and set 3792170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 3793170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 3794170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 3795170fafaeSMatthias Ringwald } 379678315a58SMatthias Ringwald 3797137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 3798137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 37995dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 3800137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 3801137715ebSMatthias Ringwald } else { 3802137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3803137715ebSMatthias Ringwald } 38045dbec6b3SMatthias Ringwald } 38055dbec6b3SMatthias Ringwald 38065dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 38075dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 38085dbec6b3SMatthias Ringwald } 38095dbec6b3SMatthias Ringwald 381078315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 381178315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 381278315a58SMatthias Ringwald } 381378315a58SMatthias Ringwald 381478315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 3815d7387af3SMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 3816d7387af3SMatthias Ringwald return LEVEL_4; 3817d7387af3SMatthias Ringwald } 381878315a58SMatthias Ringwald return hci_stack->gap_security_level; 381978315a58SMatthias Ringwald } 382030cdf3c6SMatthias Ringwald 38218ad4dfffSMatthias Ringwald void gap_set_minimal_service_security_level(gap_security_level_t security_level){ 38228ad4dfffSMatthias Ringwald hci_stack->gap_minimal_service_security_level = security_level; 38238ad4dfffSMatthias Ringwald } 38248ad4dfffSMatthias Ringwald 382530cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 382630cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 382730cdf3c6SMatthias Ringwald } 382830cdf3c6SMatthias Ringwald 382930cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 383030cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 383130cdf3c6SMatthias Ringwald } 3832170fafaeSMatthias Ringwald #endif 3833170fafaeSMatthias Ringwald 3834170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 383560b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 38369e61646fS[email protected] hci_stack->class_of_device = class_of_device; 3837baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_CLASS_OF_DEVICE; 383859d59ecfSMatthias Ringwald hci_run(); 38399e61646fS[email protected] } 384076f27cffSMatthias Ringwald 3841c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 3842c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 3843baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_DEFAULT_LINK_POLICY; 384459d59ecfSMatthias Ringwald hci_run(); 3845c33e56d3SMatthias Ringwald } 3846c33e56d3SMatthias Ringwald 3847b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 3848b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 3849b4eb4420SMatthias Ringwald } 3850b4eb4420SMatthias Ringwald 3851b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 3852b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 3853b4eb4420SMatthias Ringwald } 3854b4eb4420SMatthias Ringwald 38550c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 38560c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 38570c3eb48dSMatthias Ringwald } 38580c3eb48dSMatthias Ringwald 385976f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 386076f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 386176f27cffSMatthias Ringwald } 386235454696SMatthias Ringwald #endif 38639e61646fS[email protected] 38646fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 3865f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 3866f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 38676535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 3868f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 3869f456b2d0S[email protected] } 387076f27cffSMatthias Ringwald #endif 3871f456b2d0S[email protected] 38728d213e1aSmatthias.ringwald // State-Module-Driver overview 38738d213e1aSmatthias.ringwald // state module low-level 38748d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 38758d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 38768d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 38778d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 3878d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 3879d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 3880c7e0c5f6Smatthias.ringwald 388140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 38827301ad89Smatthias.ringwald 3883038bc64cSmatthias.ringwald // power on 3884f9a30166Smatthias.ringwald int err = 0; 38853a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 3886d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 3887f9a30166Smatthias.ringwald } 3888038bc64cSmatthias.ringwald if (err){ 38899da54300S[email protected] log_error( "POWER_ON failed"); 3890038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3891038bc64cSmatthias.ringwald return err; 3892038bc64cSmatthias.ringwald } 3893038bc64cSmatthias.ringwald 389424b3c629SMatthias Ringwald // int chipset driver 38953fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 38963fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 38973fb36a29SMatthias Ringwald } 38983fb36a29SMatthias Ringwald 389924b3c629SMatthias Ringwald // init transport 390024b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 390124b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 390224b3c629SMatthias Ringwald } 390324b3c629SMatthias Ringwald 390424b3c629SMatthias Ringwald // open transport 390524b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 3906038bc64cSmatthias.ringwald if (err){ 39079da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 39083a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3909d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3910f9a30166Smatthias.ringwald } 3911038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3912038bc64cSmatthias.ringwald return err; 3913038bc64cSmatthias.ringwald } 39148d213e1aSmatthias.ringwald return 0; 39158d213e1aSmatthias.ringwald } 3916038bc64cSmatthias.ringwald 391740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 39188d213e1aSmatthias.ringwald 39199da54300S[email protected] log_info("hci_power_control_off"); 39209418f9c9Smatthias.ringwald 39218d213e1aSmatthias.ringwald // close low-level device 392224b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 39238d213e1aSmatthias.ringwald 39249da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 39259418f9c9Smatthias.ringwald 39268d213e1aSmatthias.ringwald // power off 39273a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3928d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 39298d213e1aSmatthias.ringwald } 39309418f9c9Smatthias.ringwald 39319da54300S[email protected] log_info("hci_power_control_off - control closed"); 39329418f9c9Smatthias.ringwald 39333a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 393472ea5239Smatthias.ringwald } 393572ea5239Smatthias.ringwald 393640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 393772ea5239Smatthias.ringwald 39389da54300S[email protected] log_info("hci_power_control_sleep"); 39393144bce4Smatthias.ringwald 3940b429b9b7Smatthias.ringwald #if 0 3941b429b9b7Smatthias.ringwald // don't close serial port during sleep 3942b429b9b7Smatthias.ringwald 394372ea5239Smatthias.ringwald // close low-level device 39443a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 3945b429b9b7Smatthias.ringwald #endif 394672ea5239Smatthias.ringwald 394772ea5239Smatthias.ringwald // sleep mode 39483a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 3949d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 395072ea5239Smatthias.ringwald } 3951b429b9b7Smatthias.ringwald 39523a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 39538d213e1aSmatthias.ringwald } 39548d213e1aSmatthias.ringwald 395540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 3956b429b9b7Smatthias.ringwald 39579da54300S[email protected] log_info("hci_power_control_wake"); 3958b429b9b7Smatthias.ringwald 3959b429b9b7Smatthias.ringwald // wake on 39603a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 3961d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 3962b429b9b7Smatthias.ringwald } 3963b429b9b7Smatthias.ringwald 3964b429b9b7Smatthias.ringwald #if 0 3965b429b9b7Smatthias.ringwald // open low-level device 39663a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 3967b429b9b7Smatthias.ringwald if (err){ 39689da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 39693a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3970d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3971b429b9b7Smatthias.ringwald } 3972b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 3973b429b9b7Smatthias.ringwald return err; 3974b429b9b7Smatthias.ringwald } 3975b429b9b7Smatthias.ringwald #endif 3976b429b9b7Smatthias.ringwald 3977b429b9b7Smatthias.ringwald return 0; 3978b429b9b7Smatthias.ringwald } 3979b429b9b7Smatthias.ringwald 398044935e40S[email protected] static void hci_power_transition_to_initializing(void){ 398144935e40S[email protected] // set up state machine 398244935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 398302c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 398444935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 39855c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 398644935e40S[email protected] } 3987b429b9b7Smatthias.ringwald 398842bd3d77SMatthias Ringwald // returns error 398942bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 399042bd3d77SMatthias Ringwald int err; 39918d213e1aSmatthias.ringwald switch (power_mode){ 39928d213e1aSmatthias.ringwald case HCI_POWER_ON: 39938d213e1aSmatthias.ringwald err = hci_power_control_on(); 399442bd3d77SMatthias Ringwald if (err != 0) { 3995f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 399697b61c7bS[email protected] return err; 399797b61c7bS[email protected] } 399844935e40S[email protected] hci_power_transition_to_initializing(); 39998d213e1aSmatthias.ringwald break; 40008d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40018d213e1aSmatthias.ringwald // do nothing 40028d213e1aSmatthias.ringwald break; 40038d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4004b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 40058d213e1aSmatthias.ringwald break; 40067bbeb3adSMilanka Ringwald default: 40077bbeb3adSMilanka Ringwald btstack_assert(false); 40087bbeb3adSMilanka Ringwald break; 40098d213e1aSmatthias.ringwald } 401042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 401142bd3d77SMatthias Ringwald } 40127301ad89Smatthias.ringwald 401342bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 40148d213e1aSmatthias.ringwald switch (power_mode){ 40158d213e1aSmatthias.ringwald case HCI_POWER_ON: 40168d213e1aSmatthias.ringwald // do nothing 40178d213e1aSmatthias.ringwald break; 40188d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40198d213e1aSmatthias.ringwald // no connections yet, just turn it off 40208d213e1aSmatthias.ringwald hci_power_control_off(); 40218d213e1aSmatthias.ringwald break; 40228d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4023b546ac54Smatthias.ringwald // no connections yet, just turn it off 402472ea5239Smatthias.ringwald hci_power_control_sleep(); 40258d213e1aSmatthias.ringwald break; 40267bbeb3adSMilanka Ringwald default: 40277bbeb3adSMilanka Ringwald btstack_assert(false); 40287bbeb3adSMilanka Ringwald break; 40298d213e1aSmatthias.ringwald } 403042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 403142bd3d77SMatthias Ringwald } 40327301ad89Smatthias.ringwald 403342bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 40348d213e1aSmatthias.ringwald switch (power_mode){ 40358d213e1aSmatthias.ringwald case HCI_POWER_ON: 40368d213e1aSmatthias.ringwald // do nothing 40378d213e1aSmatthias.ringwald break; 40388d213e1aSmatthias.ringwald case HCI_POWER_OFF: 4039c7e0c5f6Smatthias.ringwald // see hci_run 40403a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4041beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 4042685ec254SMatthias Ringwald // setup watchdog timer for disconnect - only triggers if Controller does not respond anymore 4043685ec254SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 1000); 4044685ec254SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4045685ec254SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 40468d213e1aSmatthias.ringwald break; 40478d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4048b546ac54Smatthias.ringwald // see hci_run 40493a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 405074b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 40518d213e1aSmatthias.ringwald break; 40527bbeb3adSMilanka Ringwald default: 40537bbeb3adSMilanka Ringwald btstack_assert(false); 40547bbeb3adSMilanka Ringwald break; 40558d213e1aSmatthias.ringwald } 405642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 405742bd3d77SMatthias Ringwald } 40587301ad89Smatthias.ringwald 405942bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 40608d213e1aSmatthias.ringwald switch (power_mode){ 40618d213e1aSmatthias.ringwald case HCI_POWER_ON: 406244935e40S[email protected] hci_power_transition_to_initializing(); 40638d213e1aSmatthias.ringwald break; 40648d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40658d213e1aSmatthias.ringwald // do nothing 40668d213e1aSmatthias.ringwald break; 40678d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4068b546ac54Smatthias.ringwald // see hci_run 40693a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 407074b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 40718d213e1aSmatthias.ringwald break; 40727bbeb3adSMilanka Ringwald default: 40737bbeb3adSMilanka Ringwald btstack_assert(false); 40747bbeb3adSMilanka Ringwald break; 40758d213e1aSmatthias.ringwald } 407600278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 407742bd3d77SMatthias Ringwald } 40788d213e1aSmatthias.ringwald 407942bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 40808d213e1aSmatthias.ringwald switch (power_mode){ 40818d213e1aSmatthias.ringwald case HCI_POWER_ON: 408244935e40S[email protected] hci_power_transition_to_initializing(); 40838d213e1aSmatthias.ringwald break; 40848d213e1aSmatthias.ringwald case HCI_POWER_OFF: 4085b546ac54Smatthias.ringwald // see hci_run 40863a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4087beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 40888d213e1aSmatthias.ringwald break; 40898d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4090b546ac54Smatthias.ringwald // do nothing 40918d213e1aSmatthias.ringwald break; 40927bbeb3adSMilanka Ringwald default: 40937bbeb3adSMilanka Ringwald btstack_assert(false); 40947bbeb3adSMilanka Ringwald break; 40958d213e1aSmatthias.ringwald } 409642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 409742bd3d77SMatthias Ringwald } 40988d213e1aSmatthias.ringwald 409942bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 410042bd3d77SMatthias Ringwald int err; 41018d213e1aSmatthias.ringwald switch (power_mode){ 41028d213e1aSmatthias.ringwald case HCI_POWER_ON: 41033144bce4Smatthias.ringwald err = hci_power_control_wake(); 41043144bce4Smatthias.ringwald if (err) return err; 410544935e40S[email protected] hci_power_transition_to_initializing(); 41068d213e1aSmatthias.ringwald break; 41078d213e1aSmatthias.ringwald case HCI_POWER_OFF: 41083a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4109beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 41108d213e1aSmatthias.ringwald break; 41118d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4112b546ac54Smatthias.ringwald // do nothing 41138d213e1aSmatthias.ringwald break; 41147bbeb3adSMilanka Ringwald default: 41157bbeb3adSMilanka Ringwald btstack_assert(false); 41167bbeb3adSMilanka Ringwald break; 41178d213e1aSmatthias.ringwald } 411842bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 411942bd3d77SMatthias Ringwald } 41207bbeb3adSMilanka Ringwald 412142bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 412242bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 412324f87726SMatthias Ringwald int err = 0; 412442bd3d77SMatthias Ringwald switch (hci_stack->state){ 412542bd3d77SMatthias Ringwald case HCI_STATE_OFF: 412642bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 412742bd3d77SMatthias Ringwald break; 412842bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 412942bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 413042bd3d77SMatthias Ringwald break; 413142bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 413242bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 413342bd3d77SMatthias Ringwald break; 413442bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 413542bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 413642bd3d77SMatthias Ringwald break; 413742bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 413842bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 413942bd3d77SMatthias Ringwald break; 414042bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 414142bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 414242bd3d77SMatthias Ringwald break; 41437bbeb3adSMilanka Ringwald default: 41447bbeb3adSMilanka Ringwald btstack_assert(false); 41457bbeb3adSMilanka Ringwald break; 414611e23e5fSmatthias.ringwald } 414724f87726SMatthias Ringwald if (err != 0){ 414842bd3d77SMatthias Ringwald return err; 414942bd3d77SMatthias Ringwald } 415068d92d03Smatthias.ringwald 4151038bc64cSmatthias.ringwald // create internal event 4152ee8bf225Smatthias.ringwald hci_emit_state(); 4153ee8bf225Smatthias.ringwald 415468d92d03Smatthias.ringwald // trigger next/first action 415568d92d03Smatthias.ringwald hci_run(); 415668d92d03Smatthias.ringwald 4157475c8125Smatthias.ringwald return 0; 4158475c8125Smatthias.ringwald } 4159475c8125Smatthias.ringwald 416035454696SMatthias Ringwald 4161091e35e3SMatthias Ringwald static void hci_halting_run(void) { 4162091e35e3SMatthias Ringwald 4163091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 4164091e35e3SMatthias Ringwald 4165091e35e3SMatthias Ringwald hci_connection_t *connection; 4166091e35e3SMatthias Ringwald 4167091e35e3SMatthias Ringwald switch (hci_stack->substate) { 4168091e35e3SMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 4169091e35e3SMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_TIMER: 4170091e35e3SMatthias Ringwald 4171091e35e3SMatthias Ringwald #ifdef ENABLE_BLE 4172091e35e3SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4173091e35e3SMatthias Ringwald hci_whitelist_free(); 4174091e35e3SMatthias Ringwald #endif 4175091e35e3SMatthias Ringwald #endif 4176091e35e3SMatthias Ringwald // close all open connections 4177091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 4178091e35e3SMatthias Ringwald if (connection) { 4179091e35e3SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 4180091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4181091e35e3SMatthias Ringwald 4182091e35e3SMatthias Ringwald // check state 4183091e35e3SMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 4184091e35e3SMatthias Ringwald connection->state = SENT_DISCONNECT; 4185091e35e3SMatthias Ringwald 4186091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 4187091e35e3SMatthias Ringwald 4188091e35e3SMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 4189091e35e3SMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 4190091e35e3SMatthias Ringwald 4191091e35e3SMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 4192091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 4193091e35e3SMatthias Ringwald 4194091e35e3SMatthias Ringwald // finally, send the disconnect command 4195091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4196091e35e3SMatthias Ringwald return; 4197091e35e3SMatthias Ringwald } 4198091e35e3SMatthias Ringwald 4199091e35e3SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 4200091e35e3SMatthias Ringwald 4201091e35e3SMatthias Ringwald if (hci_stack->substate == HCI_HALTING_DISCONNECT_ALL_TIMER) { 4202091e35e3SMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 4203091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 4204091e35e3SMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_TIMER; 4205091e35e3SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 4206091e35e3SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4207091e35e3SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 4208091e35e3SMatthias Ringwald break; 4209091e35e3SMatthias Ringwald } 4210091e35e3SMatthias Ringwald 4211091e35e3SMatthias Ringwald /* fall through */ 4212091e35e3SMatthias Ringwald 4213091e35e3SMatthias Ringwald case HCI_HALTING_CLOSE: 4214091e35e3SMatthias Ringwald // close left over connections (that had not been properly closed before) 4215091e35e3SMatthias Ringwald hci_discard_connections(); 4216091e35e3SMatthias Ringwald 4217091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling off"); 4218091e35e3SMatthias Ringwald 4219091e35e3SMatthias Ringwald // switch mode 4220091e35e3SMatthias Ringwald hci_power_control_off(); 4221091e35e3SMatthias Ringwald 4222091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, emitting state"); 4223091e35e3SMatthias Ringwald hci_emit_state(); 4224091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, done"); 4225091e35e3SMatthias Ringwald break; 4226091e35e3SMatthias Ringwald 4227091e35e3SMatthias Ringwald case HCI_HALTING_W4_TIMER: 4228091e35e3SMatthias Ringwald // keep waiting 4229091e35e3SMatthias Ringwald 4230091e35e3SMatthias Ringwald break; 4231091e35e3SMatthias Ringwald default: 4232091e35e3SMatthias Ringwald break; 4233091e35e3SMatthias Ringwald } 4234091e35e3SMatthias Ringwald }; 4235091e35e3SMatthias Ringwald 4236091e35e3SMatthias Ringwald static void hci_falling_asleep_run(void){ 4237091e35e3SMatthias Ringwald hci_connection_t * connection; 4238091e35e3SMatthias Ringwald switch(hci_stack->substate) { 4239091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 4240091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP"); 4241091e35e3SMatthias Ringwald // close all open connections 4242091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 4243091e35e3SMatthias Ringwald if (connection){ 4244091e35e3SMatthias Ringwald 4245091e35e3SMatthias Ringwald // send disconnect 4246091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4247091e35e3SMatthias Ringwald 4248091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 4249091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4250091e35e3SMatthias Ringwald 4251091e35e3SMatthias Ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 4252091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 4253091e35e3SMatthias Ringwald return; 4254091e35e3SMatthias Ringwald } 4255091e35e3SMatthias Ringwald 4256091e35e3SMatthias Ringwald if (hci_classic_supported()){ 4257091e35e3SMatthias Ringwald // disable page and inquiry scan 4258091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4259091e35e3SMatthias Ringwald 4260091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, disabling inq scans"); 4261091e35e3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 4262091e35e3SMatthias Ringwald 4263091e35e3SMatthias Ringwald // continue in next sub state 4264091e35e3SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 4265091e35e3SMatthias Ringwald break; 4266091e35e3SMatthias Ringwald } 4267091e35e3SMatthias Ringwald 4268091e35e3SMatthias Ringwald /* fall through */ 4269091e35e3SMatthias Ringwald 4270091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 4271091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling sleep"); 4272091e35e3SMatthias Ringwald // switch mode 4273091e35e3SMatthias Ringwald hci_power_control_sleep(); // changes hci_stack->state to SLEEP 4274091e35e3SMatthias Ringwald hci_emit_state(); 4275091e35e3SMatthias Ringwald break; 4276091e35e3SMatthias Ringwald 4277091e35e3SMatthias Ringwald default: 4278091e35e3SMatthias Ringwald break; 4279091e35e3SMatthias Ringwald } 4280091e35e3SMatthias Ringwald } 4281091e35e3SMatthias Ringwald 428235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 428335454696SMatthias Ringwald 4284758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 4285758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 4286a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 4287baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_SCAN_ENABLE; 4288758b46ceSmatthias.ringwald hci_run(); 4289758b46ceSmatthias.ringwald } 4290758b46ceSmatthias.ringwald 429115a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 4292381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 4293381fbed8Smatthias.ringwald 42943a9fb326S[email protected] if (hci_stack->discoverable == enable){ 42953a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 4296381fbed8Smatthias.ringwald return; 4297381fbed8Smatthias.ringwald } 4298381fbed8Smatthias.ringwald 42993a9fb326S[email protected] hci_stack->discoverable = enable; 4300758b46ceSmatthias.ringwald hci_update_scan_enable(); 4301758b46ceSmatthias.ringwald } 4302b031bebbSmatthias.ringwald 430315a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 4304758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 4305758b46ceSmatthias.ringwald 4306758b46ceSmatthias.ringwald // don't emit event 43073a9fb326S[email protected] if (hci_stack->connectable == enable) return; 4308758b46ceSmatthias.ringwald 43093a9fb326S[email protected] hci_stack->connectable = enable; 4310758b46ceSmatthias.ringwald hci_update_scan_enable(); 4311381fbed8Smatthias.ringwald } 431235454696SMatthias Ringwald #endif 4313381fbed8Smatthias.ringwald 431415a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 43156535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 43165061f3afS[email protected] } 43175061f3afS[email protected] 43182b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 43192b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 43202b838201SMatthias Ringwald 43212b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 43222b838201SMatthias Ringwald hci_reserve_packet_buffer(); 43232b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 43242b838201SMatthias Ringwald 43252b838201SMatthias Ringwald uint16_t size = 0; 43262b838201SMatthias Ringwald uint16_t num_handles = 0; 43272b838201SMatthias Ringwald packet[size++] = 0x35; 43282b838201SMatthias Ringwald packet[size++] = 0x0c; 43292b838201SMatthias Ringwald size++; // skip param len 43302b838201SMatthias Ringwald size++; // skip num handles 43312b838201SMatthias Ringwald 43322b838201SMatthias Ringwald // add { handle, packets } entries 43332b838201SMatthias Ringwald btstack_linked_item_t * it; 43342b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 43352b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 43362b838201SMatthias Ringwald if (connection->num_packets_completed){ 43372b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 43382b838201SMatthias Ringwald size += 2; 43392b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 43402b838201SMatthias Ringwald size += 2; 43412b838201SMatthias Ringwald // 43422b838201SMatthias Ringwald num_handles++; 43432b838201SMatthias Ringwald connection->num_packets_completed = 0; 43442b838201SMatthias Ringwald } 43452b838201SMatthias Ringwald } 43462b838201SMatthias Ringwald 43472b838201SMatthias Ringwald packet[2] = size - 3; 43482b838201SMatthias Ringwald packet[3] = num_handles; 43492b838201SMatthias Ringwald 43502b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 43512b838201SMatthias Ringwald 43522b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 43532b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 43542b838201SMatthias Ringwald 43552b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 43562b838201SMatthias Ringwald if (hci_transport_synchronous()){ 4357e2d22487SMatthias Ringwald hci_release_packet_buffer(); 4358068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 43592b838201SMatthias Ringwald } 43602b838201SMatthias Ringwald } 43612b838201SMatthias Ringwald #endif 43622b838201SMatthias Ringwald 436326fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 436426fe9592SMatthias Ringwald UNUSED(ds); 436526fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 4366beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 4367beceeddeSMatthias Ringwald hci_emit_state(); 436826fe9592SMatthias Ringwald hci_run(); 436926fe9592SMatthias Ringwald } 437026fe9592SMatthias Ringwald 4371f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 43724ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 4373b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 4374b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 4375b5d8b22bS[email protected] if (connection) { 437628a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 4377b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 4378f30077b7SMatthias Ringwald return true; 4379b5d8b22bS[email protected] } 438028a0332dSMatthias Ringwald } else { 4381b5d8b22bS[email protected] // connection gone -> discard further fragments 438228a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 4383b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 4384b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 4385b5d8b22bS[email protected] } 4386b5d8b22bS[email protected] } 4387f30077b7SMatthias Ringwald return false; 43882b838201SMatthias Ringwald } 4389b031bebbSmatthias.ringwald 439035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4391f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 4392f30077b7SMatthias Ringwald 439359d59ecfSMatthias Ringwald // assert stack is working and classic is active 439459d59ecfSMatthias Ringwald if (hci_classic_supported() == false) return false; 439559d59ecfSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 439659d59ecfSMatthias Ringwald 4397b031bebbSmatthias.ringwald // decline incoming connections 43983a9fb326S[email protected] if (hci_stack->decline_reason){ 43993a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 44003a9fb326S[email protected] hci_stack->decline_reason = 0; 44013a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 4402f30077b7SMatthias Ringwald return true; 4403ce4c8fabSmatthias.ringwald } 440459d59ecfSMatthias Ringwald 4405baa4881eSMatthias Ringwald if (hci_stack->gap_tasks_classic != 0){ 4406ab4831a3SMatthias Ringwald hci_run_gap_tasks_classic(); 4407f30077b7SMatthias Ringwald return true; 4408b031bebbSmatthias.ringwald } 440927741fe7SMatthias Ringwald 4410f5875de5SMatthias Ringwald // start/stop inquiry 4411a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 4412f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 4413beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 4414496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 4415f30077b7SMatthias Ringwald return true; 4416f5875de5SMatthias Ringwald } 4417f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 4418f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 4419f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 4420f30077b7SMatthias Ringwald return true; 4421f5875de5SMatthias Ringwald } 4422b7f1ee76SMatthias Ringwald // remote name request 4423b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 4424b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 4425b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 4426ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 4427f30077b7SMatthias Ringwald return true; 4428b7f1ee76SMatthias Ringwald } 4429cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 4430cf01e888SMatthias Ringwald // Local OOB data 443159d59ecfSMatthias Ringwald if (hci_stack->classic_read_local_oob_data){ 4432cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 44331e83575aSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND)){ 4434cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 4435cf01e888SMatthias Ringwald } else { 4436cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 4437cf01e888SMatthias Ringwald } 4438cf01e888SMatthias Ringwald } 4439cf01e888SMatthias Ringwald #endif 44400a51f88bSMatthias Ringwald // pairing 44410a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 44420a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 44433f659ee4SMilanka Ringwald uint8_t pin_code[16]; 44440a51f88bSMatthias Ringwald switch (state){ 44450a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 4446cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 44473f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 44483f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 44493f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 44500a51f88bSMatthias Ringwald break; 44510a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 4452cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 44530a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 44540a51f88bSMatthias Ringwald break; 44550a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 4456cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 4457d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 44580a51f88bSMatthias Ringwald break; 44590a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 4460cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 44610a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 44620a51f88bSMatthias Ringwald break; 44630a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 4464cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 44650a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 44660a51f88bSMatthias Ringwald break; 44670a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 4468cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 44690a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 44700a51f88bSMatthias Ringwald break; 44710a51f88bSMatthias Ringwald default: 44720a51f88bSMatthias Ringwald break; 44730a51f88bSMatthias Ringwald } 4474f30077b7SMatthias Ringwald return true; 4475f30077b7SMatthias Ringwald } 4476f30077b7SMatthias Ringwald return false; 44770a51f88bSMatthias Ringwald } 447835454696SMatthias Ringwald #endif 4479b031bebbSmatthias.ringwald 4480a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4481f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 4482f30077b7SMatthias Ringwald 4483a41310b7SMatthias Ringwald // Phase 1: collect what to stop 4484a41310b7SMatthias Ringwald 4485a41310b7SMatthias Ringwald bool scanning_stop = false; 4486a41310b7SMatthias Ringwald bool connecting_stop = false; 4487a41310b7SMatthias Ringwald bool advertising_stop = false; 4488a41310b7SMatthias Ringwald 4489a41310b7SMatthias Ringwald #ifndef ENABLE_LE_CENTRAL 4490a41310b7SMatthias Ringwald UNUSED(scanning_stop); 44910abd9f64SMatthias Ringwald UNUSED(connecting_stop); 4492a41310b7SMatthias Ringwald #endif 4493a41310b7SMatthias Ringwald #ifndef ENABLE_LE_PERIPHERAL 4494a41310b7SMatthias Ringwald UNUSED(advertising_stop); 4495a41310b7SMatthias Ringwald #endif 4496a41310b7SMatthias Ringwald 44979136bde8SMatthias Ringwald // check if own address changes 44989136bde8SMatthias Ringwald bool random_address_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 44999136bde8SMatthias Ringwald 450029c24bebSMatthias Ringwald // check if whitelist needs modification 450129c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 450229c24bebSMatthias Ringwald btstack_linked_list_iterator_t lit; 450329c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 450429c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 450529c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 450629c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 450729c24bebSMatthias Ringwald whitelist_modification_pending = true; 450829c24bebSMatthias Ringwald break; 450929c24bebSMatthias Ringwald } 451029c24bebSMatthias Ringwald } 451121debf25SMatthias Ringwald // check if resolving list needs modification 451221debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 451321debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 45141ffa425cSMatthias Ringwald 45151ffa425cSMatthias Ringwald bool resolving_list_supported = hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE); 4516ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 451721debf25SMatthias Ringwald resolving_list_modification_pending = true; 451821debf25SMatthias Ringwald } 451921debf25SMatthias Ringwald #endif 452029c24bebSMatthias Ringwald 4521d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4522fde725feSMatthias Ringwald // scanning control 45233251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 452429c24bebSMatthias Ringwald // stop if: 452529c24bebSMatthias Ringwald // - parameter change required 452629c24bebSMatthias Ringwald // - it's disabled 452729c24bebSMatthias Ringwald // - whitelist change required but used for scanning 452821debf25SMatthias Ringwald // - resolving list modified 452951e51a58SMatthias Ringwald // - own address changes 453029c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 453121debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 453221debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 453321debf25SMatthias Ringwald scanning_uses_whitelist || 453451e51a58SMatthias Ringwald resolving_list_modification_pending || 453551e51a58SMatthias Ringwald random_address_change){ 453621debf25SMatthias Ringwald 45372d5c2a27SMatthias Ringwald scanning_stop = true; 4538fde725feSMatthias Ringwald } 4539fde725feSMatthias Ringwald } 454029c24bebSMatthias Ringwald #endif 4541fde725feSMatthias Ringwald 454229c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 454329c24bebSMatthias Ringwald // connecting control 4544f496d06eSMatthias Ringwald bool connecting_with_whitelist; 4545f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 4546f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 4547f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 4548af64f147SMatthias Ringwald // stop connecting if: 4549af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 4550af64f147SMatthias Ringwald // - if it got disabled 455121debf25SMatthias Ringwald // - resolving list modified 455251e51a58SMatthias Ringwald // - own address changes 4553f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 4554f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 455521debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 455651e51a58SMatthias Ringwald resolving_list_modification_pending || 455751e51a58SMatthias Ringwald random_address_change) { 455821debf25SMatthias Ringwald 455929c24bebSMatthias Ringwald connecting_stop = true; 456029c24bebSMatthias Ringwald } 4561f496d06eSMatthias Ringwald break; 4562f496d06eSMatthias Ringwald default: 4563f496d06eSMatthias Ringwald break; 456429c24bebSMatthias Ringwald } 4565d70217a2SMatthias Ringwald #endif 456629c24bebSMatthias Ringwald 4567d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 456845c102fdSMatthias Ringwald // le advertisement control 4569bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_active){ 457029c24bebSMatthias Ringwald // stop if: 457129c24bebSMatthias Ringwald // - parameter change required 45729136bde8SMatthias Ringwald // - random address used in advertising and changes 457329c24bebSMatthias Ringwald // - it's disabled 4574ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 457521debf25SMatthias Ringwald // - resolving list modified 457651e51a58SMatthias Ringwald // - own address changes 4577a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 45789136bde8SMatthias Ringwald bool advertising_uses_random_address = hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC; 45794e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 4580a61834b6SMatthias Ringwald if (advertising_change || 45819136bde8SMatthias Ringwald (advertising_uses_random_address && random_address_change) || 4582a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 45839136bde8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 458451e51a58SMatthias Ringwald resolving_list_modification_pending || 458551e51a58SMatthias Ringwald random_address_change) { 458621debf25SMatthias Ringwald 4587fde725feSMatthias Ringwald advertising_stop = true; 4588fde725feSMatthias Ringwald } 4589fde725feSMatthias Ringwald } 4590a41310b7SMatthias Ringwald #endif 4591fde725feSMatthias Ringwald 4592a41310b7SMatthias Ringwald 4593a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 4594a41310b7SMatthias Ringwald 4595a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4596a41310b7SMatthias Ringwald if (scanning_stop){ 45975226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 4598*63671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4599*63671e69SMatthias Ringwald if (hci_extended_advertising_supported()) { 4600*63671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 0, 0, 0, 0); 4601*63671e69SMatthias Ringwald } else 4602*63671e69SMatthias Ringwald #endif 4603*63671e69SMatthias Ringwald { 4604a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 4605*63671e69SMatthias Ringwald } 4606a41310b7SMatthias Ringwald return true; 4607a41310b7SMatthias Ringwald } 4608a41310b7SMatthias Ringwald #endif 4609a41310b7SMatthias Ringwald 4610a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4611a41310b7SMatthias Ringwald if (connecting_stop){ 4612a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 4613a41310b7SMatthias Ringwald return true; 4614a41310b7SMatthias Ringwald } 4615a41310b7SMatthias Ringwald #endif 4616a41310b7SMatthias Ringwald 4617a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4618fde725feSMatthias Ringwald if (advertising_stop){ 46195226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = false; 462045c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 4621f30077b7SMatthias Ringwald return true; 462245c102fdSMatthias Ringwald } 4623a41310b7SMatthias Ringwald #endif 4624fde725feSMatthias Ringwald 4625a41310b7SMatthias Ringwald // Phase 3: modify 4626a41310b7SMatthias Ringwald 46279136bde8SMatthias Ringwald if (random_address_change){ 46289136bde8SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 46299136bde8SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, hci_stack->le_random_address); 46309136bde8SMatthias Ringwald return true; 46319136bde8SMatthias Ringwald } 46329136bde8SMatthias Ringwald 4633a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4634a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 4635a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 4636*63671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4637*63671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 4638*63671e69SMatthias Ringwald // prepare arrays for all PHYs 4639*63671e69SMatthias Ringwald uint8_t scan_types[1] = { hci_stack->le_scan_type }; 4640*63671e69SMatthias Ringwald uint16_t scan_intervals[1] = { hci_stack->le_scan_interval }; 4641*63671e69SMatthias Ringwald uint16_t scan_windows[1] = { hci_stack->le_scan_window }; 4642*63671e69SMatthias Ringwald uint8_t scanning_phys = 1; // LE 1M PHY 4643*63671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_parameters, hci_stack->le_own_addr_type, 4644*63671e69SMatthias Ringwald hci_stack->le_scan_filter_policy, scanning_phys, scan_types, scan_intervals, scan_windows); 4645*63671e69SMatthias Ringwald } else 4646*63671e69SMatthias Ringwald #endif 4647*63671e69SMatthias Ringwald { 4648a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 4649a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 4650*63671e69SMatthias Ringwald } 4651a41310b7SMatthias Ringwald return true; 4652a41310b7SMatthias Ringwald } 4653a41310b7SMatthias Ringwald #endif 4654a41310b7SMatthias Ringwald 4655a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 465645c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 465745c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 46586bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 465945c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 466045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 466145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 466245c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 46636bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 466445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 466545c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 466645c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 466745c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 4668f30077b7SMatthias Ringwald return true; 466945c102fdSMatthias Ringwald } 4670501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 4671501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 46727a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 46737a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 46746535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 46756535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 46763c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 46777a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 4678f30077b7SMatthias Ringwald return true; 467945c102fdSMatthias Ringwald } 4680501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 4681501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 4682f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 4683f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 46846535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 46856535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 46863c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 4687214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 4688f30077b7SMatthias Ringwald return true; 4689501f56b3SMatthias Ringwald } 4690d70217a2SMatthias Ringwald #endif 46919956955bSMatthias Ringwald 469213eb2a2eSMatthias Ringwald 4693057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4694a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 4695a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 4696057ab60cSMatthias Ringwald #endif 4697057ab60cSMatthias Ringwald 4698a41310b7SMatthias Ringwald // LE Whitelist Management 4699a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 47009956955bSMatthias Ringwald // add/remove entries 4701665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4702665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4703665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4704453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 4705453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4706453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 4707453459ddSMatthias Ringwald return true; 4708453459ddSMatthias Ringwald } 47099956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 4710453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 4711453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 47129956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 4713f30077b7SMatthias Ringwald return true; 47149956955bSMatthias Ringwald } 4715453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 4716665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 47179956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 47189956955bSMatthias Ringwald } 47199956955bSMatthias Ringwald } 472091915b0bSMatthias Ringwald } 47219956955bSMatthias Ringwald 472221debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 472321debf25SMatthias Ringwald // LE Resolving List Management 4724ea151974SMatthias Ringwald if (resolving_list_supported) { 472521debf25SMatthias Ringwald uint16_t i; 472621debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 472721debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 472821debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 472921debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 473021debf25SMatthias Ringwald return true; 473121debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 473221debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 473321debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 473421debf25SMatthias Ringwald return true; 473521debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 473602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 4737ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 4738ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 4739ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 4740ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 474121debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 474221debf25SMatthias Ringwald return true; 474302b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 474402b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 474502b02cffSMatthias Ringwald uint8_t offset = i >> 3; 474602b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 474702b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 474802b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 474902b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 475002b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 475102b02cffSMatthias Ringwald sm_key_t peer_irk; 475202b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 475302b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 4754f5228c62SMatthias Ringwald 4755f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 4756f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 4757f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4758f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 4759f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 4760f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 4761f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 4762f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 4763f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 4764f5228c62SMatthias Ringwald } 4765f5228c62SMatthias Ringwald #endif 4766f5228c62SMatthias Ringwald 4767ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 4768ea151974SMatthias Ringwald peer_identity_addreses); 476902b02cffSMatthias Ringwald return true; 477002b02cffSMatthias Ringwald } 477102b02cffSMatthias Ringwald 477202b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 477302b02cffSMatthias Ringwald 477402b02cffSMatthias Ringwald /* fall through */ 477502b02cffSMatthias Ringwald 477621debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 477721debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 477821debf25SMatthias Ringwald uint8_t offset = i >> 3; 477921debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 478002b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 478102b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 478221debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 478321debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 478421debf25SMatthias Ringwald sm_key_t peer_irk; 478521debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 478621debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 478721debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 478821debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 478921debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 479021debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 479121debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 479221debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 4793ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 4794ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 479521debf25SMatthias Ringwald return true; 479621debf25SMatthias Ringwald } 479702b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 479821debf25SMatthias Ringwald break; 479902b02cffSMatthias Ringwald 480021debf25SMatthias Ringwald default: 480121debf25SMatthias Ringwald break; 480221debf25SMatthias Ringwald } 4803ea151974SMatthias Ringwald } 480421debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 480521debf25SMatthias Ringwald #endif 4806a41310b7SMatthias Ringwald 480773799937SMatthias Ringwald // post-pone all actions until stack is fully working 480873799937SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 480973799937SMatthias Ringwald 481073799937SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 481173799937SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 481273799937SMatthias Ringwald 4813a41310b7SMatthias Ringwald // Phase 4: restore state 481429c24bebSMatthias Ringwald 481529c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4816af64f147SMatthias Ringwald // re-start scanning 481729c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 481829c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 4819*63671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4820*63671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 4821*63671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 1, 0, 0, 0); 4822*63671e69SMatthias Ringwald } else 4823*63671e69SMatthias Ringwald #endif 4824*63671e69SMatthias Ringwald { 482529c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 4826*63671e69SMatthias Ringwald } 482729c24bebSMatthias Ringwald return true; 482829c24bebSMatthias Ringwald } 482929c24bebSMatthias Ringwald #endif 483029c24bebSMatthias Ringwald 483113eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4832af64f147SMatthias Ringwald // re-start connecting 4833af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 48349956955bSMatthias Ringwald bd_addr_t null_addr; 48359956955bSMatthias Ringwald memset(null_addr, 0, 6); 48366bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 48376bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 48389956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 4839cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 4840cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 48419956955bSMatthias Ringwald 1, // use whitelist 48429956955bSMatthias Ringwald 0, // peer address type 48439956955bSMatthias Ringwald null_addr, // peer bd addr 48446bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 484573044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 484673044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 484773044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 484873044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 484973044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 485073044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 48519956955bSMatthias Ringwald ); 4852f30077b7SMatthias Ringwald return true; 48539956955bSMatthias Ringwald } 4854d70217a2SMatthias Ringwald #endif 4855a41310b7SMatthias Ringwald 4856a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4857a41310b7SMatthias Ringwald // re-start advertising 48588978dcf1SMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && !hci_stack->le_advertisements_active){ 4859a41310b7SMatthias Ringwald // check if advertisements should be enabled given 48605226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = true; 4861b892db1cSMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, hci_stack->le_advertisements_own_address); 4862a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 4863a41310b7SMatthias Ringwald return true; 4864a41310b7SMatthias Ringwald } 4865a41310b7SMatthias Ringwald #endif 4866a41310b7SMatthias Ringwald 4867f30077b7SMatthias Ringwald return false; 48687bdc6798S[email protected] } 4869b2f949feS[email protected] #endif 48707bdc6798S[email protected] 487188a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 4872f30077b7SMatthias Ringwald btstack_linked_item_t * it; 4873a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 487405ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 487532ab9390Smatthias.ringwald 48760bf6344aS[email protected] switch(connection->state){ 48770bf6344aS[email protected] case SEND_CREATE_CONNECTION: 48784f3229d8S[email protected] switch(connection->address_type){ 487935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4880f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 48819da54300S[email protected] log_info("sending hci_create_connection"); 4882b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 48834f3229d8S[email protected] break; 488435454696SMatthias Ringwald #endif 48854f3229d8S[email protected] default: 4886a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4887d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 48889da54300S[email protected] log_info("sending hci_le_create_connection"); 48896bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 48906bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 48914f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 4892cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 4893cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 48944f3229d8S[email protected] 0, // don't use whitelist 48954f3229d8S[email protected] connection->address_type, // peer address type 48964f3229d8S[email protected] connection->address, // peer bd addr 48976bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 489873044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 489973044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 490073044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 490173044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 490273044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 490373044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 49044f3229d8S[email protected] ); 49054f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 4906b2f949feS[email protected] #endif 4907d70217a2SMatthias Ringwald #endif 49084f3229d8S[email protected] break; 49094f3229d8S[email protected] } 4910f30077b7SMatthias Ringwald return true; 4911ad83dc6aS[email protected] 491235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 49130bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 49145cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 4915f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 491676ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 4917895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 4918c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 4919f30077b7SMatthias Ringwald return true; 4920ee025741SMatthias Ringwald } 4921ee025741SMatthias Ringwald break; 492235454696SMatthias Ringwald #endif 49230bf6344aS[email protected] 4924a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4925d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 49260bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 49270bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 49280bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 4929f30077b7SMatthias Ringwald return true; 4930a6725849S[email protected] #endif 4931d70217a2SMatthias Ringwald #endif 49320bf6344aS[email protected] case SEND_DISCONNECT: 49330bf6344aS[email protected] connection->state = SENT_DISCONNECT; 49346ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4935f30077b7SMatthias Ringwald return true; 49360bf6344aS[email protected] 49370bf6344aS[email protected] default: 49380bf6344aS[email protected] break; 4939c7e0c5f6Smatthias.ringwald } 4940c7e0c5f6Smatthias.ringwald 4941cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 4942cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 4943cabf004eSMatthias Ringwald 49448daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_READ_RSSI){ 49458daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_READ_RSSI); 4946891b9fc2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 4947f30077b7SMatthias Ringwald return true; 4948891b9fc2SMatthias Ringwald } 4949891b9fc2SMatthias Ringwald 495094418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 495194418890SMatthias Ringwald 49528daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT){ 49538daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT); 49546909f064SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 4955f30077b7SMatthias Ringwald return true; 49566909f064SMatthias Ringwald } 49576909f064SMatthias Ringwald 4958ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 495950f49832SMatthias Ringwald if (((connection->authentication_flags & AUTH_FLAG_HANDLE_LINK_KEY_REQUEST) != 0)){ 4960608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 49618daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 496230e72d78SMatthias Ringwald 4963e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 4964e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 496530e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 4966e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 496732ab9390Smatthias.ringwald } else { 496832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 496932ab9390Smatthias.ringwald } 4970f30077b7SMatthias Ringwald return true; 497132ab9390Smatthias.ringwald } 49721d6b20aeS[email protected] 49738daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_DENY_PIN_CODE_REQUEST){ 49749da54300S[email protected] log_info("denying to pin request"); 49758daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_DENY_PIN_CODE_REQUEST); 497634d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 4977f30077b7SMatthias Ringwald return true; 49784c57c146S[email protected] } 49794c57c146S[email protected] 4980c950c316SMatthias Ringwald // security assessment requires remote features 49812dd8985bSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST) != 0){ 4982c950c316SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 4983c950c316SMatthias Ringwald hci_ssp_assess_security_on_io_cap_request(connection); 4984c950c316SMatthias Ringwald // no return here as hci_ssp_assess_security_on_io_cap_request only sets AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY or AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY 4985c950c316SMatthias Ringwald } 4986c950c316SMatthias Ringwald 49878daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY){ 49888daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 4989a8d20135SMatthias Ringwald // set authentication requirements: 4990a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 4991532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 4992a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 49939faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 49949faad3abS[email protected] authreq |= 1; 4995106d6d11S[email protected] } 4996532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 49978daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 4998532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 4999532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 5000532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 5001532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 5002532454f9SMatthias Ringwald bonding = false; 5003532454f9SMatthias Ringwald } 5004532454f9SMatthias Ringwald } 5005532454f9SMatthias Ringwald if (bonding){ 5006a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 5007a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 5008532454f9SMatthias Ringwald } else { 5009a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 5010a8d20135SMatthias Ringwald } 5011532454f9SMatthias Ringwald } 50121849becdSMatthias Ringwald uint8_t have_oob_data = 0; 50131849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 50141849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 50151849becdSMatthias Ringwald have_oob_data |= 1; 50161849becdSMatthias Ringwald } 50171849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 50181849becdSMatthias Ringwald have_oob_data |= 2; 50191849becdSMatthias Ringwald } 50201849becdSMatthias Ringwald #endif 50211849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 502211b03efaSMatthias Ringwald return true; 5023f8fb5f6eS[email protected] } 502411b03efaSMatthias Ringwald 50258daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 50268daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 502711b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 5028f30077b7SMatthias Ringwald return true; 502932ab9390Smatthias.ringwald } 503032ab9390Smatthias.ringwald 50311849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 50327ca4a7edSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY){ 50337ca4a7edSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 50341849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 50351849becdSMatthias Ringwald const uint8_t * r_192 = zero; 50361849becdSMatthias Ringwald const uint8_t * c_192 = zero; 50371849becdSMatthias Ringwald const uint8_t * r_256 = zero; 50381849becdSMatthias Ringwald const uint8_t * c_256 = zero; 50391849becdSMatthias Ringwald // verify P-256 OOB 50401e83575aSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && hci_command_supported(SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)) { 50411849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 50421849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 50431849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 50441849becdSMatthias Ringwald } 50451849becdSMatthias Ringwald } 50461849becdSMatthias Ringwald // verify P-192 OOB 50471849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 50481849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 50491849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 50501849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 50511849becdSMatthias Ringwald } 50521849becdSMatthias Ringwald } 50537ca4a7edSMatthias Ringwald 50547ca4a7edSMatthias Ringwald // assess security 50557ca4a7edSMatthias Ringwald bool need_level_4 = hci_stack->gap_secure_connections_only_mode || (connection->requested_security_level == LEVEL_4); 50567ca4a7edSMatthias Ringwald bool can_reach_level_4 = hci_remote_sc_enabled(connection) && (c_256 != NULL); 50577ca4a7edSMatthias Ringwald if (need_level_4 && !can_reach_level_4){ 50587ca4a7edSMatthias Ringwald log_info("Level 4 required, but not possible -> abort"); 50597ca4a7edSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_INSUFFICIENT_SECURITY); 50607ca4a7edSMatthias Ringwald // send oob negative reply 50617ca4a7edSMatthias Ringwald c_256 = NULL; 50627ca4a7edSMatthias Ringwald c_192 = NULL; 50637ca4a7edSMatthias Ringwald } 50647ca4a7edSMatthias Ringwald 50651849becdSMatthias Ringwald // Reply 50661849becdSMatthias Ringwald if (c_256 != zero) { 50671849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 50681849becdSMatthias Ringwald } else if (c_192 != zero){ 50691849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 50701849becdSMatthias Ringwald } else { 50711ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = connection->con_handle; 50721849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 50731849becdSMatthias Ringwald } 50741849becdSMatthias Ringwald return true; 50751849becdSMatthias Ringwald } 50761849becdSMatthias Ringwald #endif 50771849becdSMatthias Ringwald 50788daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_REPLY){ 50798daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 508034d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 5081f30077b7SMatthias Ringwald return true; 5082dbe1a790S[email protected] } 5083dbe1a790S[email protected] 5084367aedc0SMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY){ 5085367aedc0SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 5086367aedc0SMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, &connection->address); 5087367aedc0SMatthias Ringwald return true; 5088367aedc0SMatthias Ringwald } 5089367aedc0SMatthias Ringwald 50908daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_PASSKEY_REPLY){ 50918daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 509234d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 5093f30077b7SMatthias Ringwald return true; 5094dbe1a790S[email protected] } 5095afd4e962S[email protected] 5096ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 5097ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 50981bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 50996ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 51006ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5101f30077b7SMatthias Ringwald return true; 5102ad83dc6aS[email protected] } 510376f27cffSMatthias Ringwald 51042a75353aSMatthias Ringwald if ((connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 510534d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 5106abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 510734d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 5108f30077b7SMatthias Ringwald return true; 5109afd4e962S[email protected] } 511076f27cffSMatthias Ringwald 5111dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 5112dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 5113dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 5114f30077b7SMatthias Ringwald return true; 5115dce78009S[email protected] } 5116447016ffSMatthias Ringwald 5117573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 5118573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 5119573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 5120f30077b7SMatthias Ringwald return true; 5121573897a0SMatthias Ringwald } 5122447016ffSMatthias Ringwald 5123447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 5124447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 5125447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 5126447016ffSMatthias Ringwald return true; 5127447016ffSMatthias Ringwald } 5128447016ffSMatthias Ringwald 5129447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 5130447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 5131447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 5132447016ffSMatthias Ringwald return true; 5133447016ffSMatthias Ringwald } 5134447016ffSMatthias Ringwald 5135447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 5136447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 5137447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 5138447016ffSMatthias Ringwald return true; 5139447016ffSMatthias Ringwald } 514076f27cffSMatthias Ringwald #endif 514176f27cffSMatthias Ringwald 514276f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 514376f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 51441714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 51451714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 51461714cbbdSMatthias Ringwald #endif 51476ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 51486ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 51496ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 5150f30077b7SMatthias Ringwald return true; 515176f27cffSMatthias Ringwald } 51526ef3696aSMatthias Ringwald } 5153da886c03S[email protected] 51546cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 5155f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 5156f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 5157f8ee3071SMatthias Ringwald case 0: 5158f8ee3071SMatthias Ringwald break; 5159f8ee3071SMatthias Ringwald case 0xffff: 5160f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 5161f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 5162f30077b7SMatthias Ringwald return true; 5163f8ee3071SMatthias Ringwald default: 5164f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 5165f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 5166f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 5167f30077b7SMatthias Ringwald return true; 5168f8ee3071SMatthias Ringwald } 516988a03c8dSMatthias Ringwald 5170140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 5171140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 5172140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 5173140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 5174140c0557SMatthias Ringwald return true; 5175140c0557SMatthias Ringwald } 5176140c0557SMatthias Ringwald 5177965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 5178278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 5179965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 5180278ff8a9SMatthias 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); 5181278ff8a9SMatthias Ringwald return true; 5182278ff8a9SMatthias Ringwald } 5183278ff8a9SMatthias Ringwald 518488a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 518588a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 518688a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 518788a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 518888a03c8dSMatthias Ringwald return true; 518988a03c8dSMatthias Ringwald } 51906cdc2862SMatthias Ringwald #endif 5191f8ee3071SMatthias Ringwald 5192a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 519373cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 519473cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 519573cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 5196da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 519773cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 5198c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 5199c37a3166S[email protected] 0x0000, 0xffff); 5200f30077b7SMatthias Ringwald return true; 520173cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 520273cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 520373cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 520473cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 520573cd8a2aSMatthias Ringwald 0x0000, 0xffff); 5206f30077b7SMatthias Ringwald return true; 520773cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 520873cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 520973cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE); 5210f30077b7SMatthias Ringwald return true; 521173cd8a2aSMatthias Ringwald default: 521273cd8a2aSMatthias Ringwald break; 5213c37a3166S[email protected] } 52144ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 5215b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 5216b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 5217b90f6e0aSMatthias 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); 5218f30077b7SMatthias Ringwald return true; 5219b90f6e0aSMatthias Ringwald } 5220c37a3166S[email protected] #endif 5221dbe1a790S[email protected] } 5222f30077b7SMatthias Ringwald return false; 5223f30077b7SMatthias Ringwald } 5224c7e0c5f6Smatthias.ringwald 5225f30077b7SMatthias Ringwald static void hci_run(void){ 5226f30077b7SMatthias Ringwald 5227df9e2780SMatthias Ringwald // stack state sub statemachines 52280d37aff3SMatthias Ringwald // halting needs to be called even if we cannot send command packet now 5229df9e2780SMatthias Ringwald switch (hci_stack->state) { 5230df9e2780SMatthias Ringwald case HCI_STATE_INITIALIZING: 5231df9e2780SMatthias Ringwald hci_initializing_run(); 5232df9e2780SMatthias Ringwald break; 5233df9e2780SMatthias Ringwald case HCI_STATE_HALTING: 5234df9e2780SMatthias Ringwald hci_halting_run(); 5235df9e2780SMatthias Ringwald break; 5236df9e2780SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 5237df9e2780SMatthias Ringwald hci_falling_asleep_run(); 5238df9e2780SMatthias Ringwald break; 5239df9e2780SMatthias Ringwald default: 5240df9e2780SMatthias Ringwald break; 5241df9e2780SMatthias Ringwald } 5242df9e2780SMatthias Ringwald 5243f30077b7SMatthias Ringwald bool done; 5244f30077b7SMatthias Ringwald 5245f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 5246f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 5247f30077b7SMatthias Ringwald if (done) return; 5248f30077b7SMatthias Ringwald 5249f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 5250f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 5251f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 5252f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 5253f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 5254f30077b7SMatthias Ringwald return; 5255f30077b7SMatthias Ringwald } 5256f30077b7SMatthias Ringwald #endif 5257f30077b7SMatthias Ringwald 5258f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5259f30077b7SMatthias Ringwald 5260f30077b7SMatthias Ringwald // global/non-connection oriented commands 5261f30077b7SMatthias Ringwald 5262f30077b7SMatthias Ringwald 5263f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 5264f30077b7SMatthias Ringwald // general gap classic 5265f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 5266f30077b7SMatthias Ringwald if (done) return; 5267f30077b7SMatthias Ringwald #endif 5268f30077b7SMatthias Ringwald 5269f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 5270f30077b7SMatthias Ringwald // general gap le 5271f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 5272f30077b7SMatthias Ringwald if (done) return; 5273f30077b7SMatthias Ringwald #endif 5274f30077b7SMatthias Ringwald 5275f30077b7SMatthias Ringwald // send pending HCI commands 5276df9e2780SMatthias Ringwald hci_run_general_pending_commands(); 52773429f56bSmatthias.ringwald } 527816833f0aSmatthias.ringwald 52793e2050f7SMatthias Ringwald uint8_t hci_send_cmd_packet(uint8_t *packet, int size){ 528035454696SMatthias Ringwald // house-keeping 528135454696SMatthias Ringwald 528235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5283c8e4258aSmatthias.ringwald bd_addr_t addr; 5284c8e4258aSmatthias.ringwald hci_connection_t * conn; 5285c123d999SMatthias Ringwald #endif 5286c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 52876f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 52889cbd2215SMatthias Ringwald #endif 5289c8e4258aSmatthias.ringwald 52906f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 52916f35bb46SMatthias Ringwald switch (opcode) { 52929cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 52939cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 52949cbd2215SMatthias Ringwald break; 52959cbd2215SMatthias Ringwald 52969cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 52976f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 5298724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 52999da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 5300c8e4258aSmatthias.ringwald 530172cf8859SMatthias Ringwald // CVE-2020-26555: reject outgoing connection to device with same BD ADDR 530272cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0) { 530379e0fa07SMatthias Ringwald hci_emit_connection_complete(addr, 0, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR); 53043e2050f7SMatthias Ringwald return ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 530572cf8859SMatthias Ringwald } 530672cf8859SMatthias Ringwald 5307f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 5308ad83dc6aS[email protected] if (!conn) { 5309f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 531017f1ba2aSmatthias.ringwald if (!conn) { 531117f1ba2aSmatthias.ringwald // notify client that alloc failed 53122deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 53133e2050f7SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller 531417f1ba2aSmatthias.ringwald } 5315ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 5316f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 5317ad83dc6aS[email protected] } 5318aee5d5c1SMatthias Ringwald 5319aee5d5c1SMatthias Ringwald conn->con_handle = HCI_CON_HANDLE_INVALID; 5320aee5d5c1SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 5321aee5d5c1SMatthias Ringwald 5322ad83dc6aS[email protected] log_info("conn state %u", conn->state); 53233e2050f7SMatthias Ringwald // TODO: L2CAP should not send create connection command, instead a (new) gap function should be used 5324ad83dc6aS[email protected] switch (conn->state) { 5325ad83dc6aS[email protected] // if connection active exists 5326ad83dc6aS[email protected] case OPEN: 5327f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 532872cf8859SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, ERROR_CODE_SUCCESS); 53293e2050f7SMatthias Ringwald // packet not sent to controller 53303e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 5331672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 5332672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 5333672e36abSMatthias Ringwald break; 5334ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 5335532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 5336532f91a5SMatthias Ringwald break; 5337ad83dc6aS[email protected] default: 5338ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 53393e2050f7SMatthias Ringwald // packet not sent to controller 53403e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 5341ad83dc6aS[email protected] } 5342c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 5343229331c6SMatthias Ringwald 5344229331c6SMatthias Ringwald // track outgoing connection 5345f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 53466535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 53476f35bb46SMatthias Ringwald break; 5348ee752bb8SMatthias Ringwald 53495b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 53506f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 5351ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 5352ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 5353ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 53546f35bb46SMatthias Ringwald break; 53556f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 5356c91e6e4fSMatthias Ringwald // accept_synchronous_connection? Voice setting at offset 18 5357ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 5358ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 53591f34df2cSMatthias Ringwald // track outgoing connection 53601f34df2cSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_SCO; 53611f34df2cSMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->outgoing_addr); 53626f35bb46SMatthias Ringwald break; 5363ee752bb8SMatthias Ringwald #endif 536435454696SMatthias Ringwald #endif 53654b3e1e19SMatthias Ringwald 5366a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5367d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 53686f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 5369b04dfa37SMatthias Ringwald // white list used? 53706f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 5371b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 5372b04dfa37SMatthias Ringwald case 0: 5373b04dfa37SMatthias Ringwald // whitelist not used 5374b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 5375b04dfa37SMatthias Ringwald break; 5376b04dfa37SMatthias Ringwald case 1: 5377b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 5378b04dfa37SMatthias Ringwald break; 5379b04dfa37SMatthias Ringwald default: 5380b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 5381b04dfa37SMatthias Ringwald break; 5382b04dfa37SMatthias Ringwald } 5383c163146eSMatthias Ringwald // track outgoing connection 538405002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 5385c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 53866f35bb46SMatthias Ringwald break; 53876f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 53886ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 53896f35bb46SMatthias Ringwald break; 53909cbd2215SMatthias Ringwald #endif 53919cbd2215SMatthias Ringwald #endif 53926f35bb46SMatthias Ringwald default: 53936f35bb46SMatthias Ringwald break; 5394b04dfa37SMatthias Ringwald } 539569a97523S[email protected] 53963a9fb326S[email protected] hci_stack->num_cmd_packets--; 53975bb5bc3eS[email protected] 53985bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 53993e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 54003e2050f7SMatthias Ringwald if (err != 0){ 54013e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 54023e2050f7SMatthias Ringwald } 54033e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 540431452debSmatthias.ringwald } 54058adf0ddaSmatthias.ringwald 54062bd8b7e7S[email protected] // disconnect because of security block 54072bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 54082bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 54092bd8b7e7S[email protected] if (!connection) return; 54102bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 54112bd8b7e7S[email protected] } 54122bd8b7e7S[email protected] 54132bd8b7e7S[email protected] 5414dbe1a790S[email protected] // Configure Secure Simple Pairing 5415dbe1a790S[email protected] 541635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 541735454696SMatthias Ringwald 5418dbe1a790S[email protected] // enable will enable SSP during init 541915a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 54203a9fb326S[email protected] hci_stack->ssp_enable = enable; 5421dbe1a790S[email protected] } 5422dbe1a790S[email protected] 542395d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 542415a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 54252bd8b7e7S[email protected] } 54262bd8b7e7S[email protected] 5427dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 542815a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 54293a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 5430dbe1a790S[email protected] } 543115a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 54323a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 5433dbe1a790S[email protected] } 5434dbe1a790S[email protected] 5435dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 543615a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 54373a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 5438dbe1a790S[email protected] } 54395d23aae8SMatthias Ringwald 54405d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 54415d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 54425d23aae8SMatthias Ringwald } 54435d23aae8SMatthias Ringwald 544435454696SMatthias Ringwald #endif 5445dbe1a790S[email protected] 544694be1a66SMatthias Ringwald // va_list part of hci_send_cmd 54473e2050f7SMatthias Ringwald uint8_t hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 5448d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 54499d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 54503e2050f7SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 54519d14b626S[email protected] } 54529d14b626S[email protected] 54535127cc62S[email protected] // for HCI INITIALIZATION 54549da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 54555127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 54565127cc62S[email protected] 54579d14b626S[email protected] hci_reserve_packet_buffer(); 54589d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 545994be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 54603e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_packet(packet, size); 5461bfea0222SMatthias Ringwald 5462593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 54633e2050f7SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) || hci_transport_synchronous()){ 5464e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5465068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 5466bfea0222SMatthias Ringwald } 5467bfea0222SMatthias Ringwald 54683e2050f7SMatthias Ringwald return status; 546994be1a66SMatthias Ringwald } 54709d14b626S[email protected] 547194be1a66SMatthias Ringwald /** 547294be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 547394be1a66SMatthias Ringwald */ 54743e2050f7SMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t * cmd, ...){ 54751cd208adSmatthias.ringwald va_list argptr; 54761cd208adSmatthias.ringwald va_start(argptr, cmd); 54773e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_va_arg(cmd, argptr); 54781cd208adSmatthias.ringwald va_end(argptr); 54793e2050f7SMatthias Ringwald return status; 548093b8dc03Smatthias.ringwald } 5481c8e4258aSmatthias.ringwald 5482ee091cf1Smatthias.ringwald // Create various non-HCI events. 5483ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 5484ee091cf1Smatthias.ringwald 5485d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 5486fb37a842SMatthias Ringwald // dump packet 5487d6b06661SMatthias Ringwald if (dump) { 5488300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 5489d6b06661SMatthias Ringwald } 54901ef6bb52SMatthias Ringwald 5491fb37a842SMatthias Ringwald // dispatch to all event handlers 54921ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 54931ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 54941ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 54951ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 5496d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 54971ef6bb52SMatthias Ringwald } 5498d6b06661SMatthias Ringwald } 5499d6b06661SMatthias Ringwald 5500d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 5501fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 55023d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 5503d6b06661SMatthias Ringwald } 5504d6b06661SMatthias Ringwald 550535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5506701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 55073bc639ceSMatthias Ringwald // notify SCO sender if waiting 5508701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 5509701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 55103bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 5511701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 5512701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 55133d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 55143bc639ceSMatthias Ringwald } 55153bc639ceSMatthias Ringwald } 55161cfb383eSMatthias Ringwald 55171cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 55189784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 5519ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 55201cfb383eSMatthias Ringwald 55211cfb383eSMatthias Ringwald uint8_t * eir_data; 55221cfb383eSMatthias Ringwald ad_context_t context; 55231cfb383eSMatthias Ringwald const uint8_t * name; 55241cfb383eSMatthias Ringwald uint8_t name_len; 55251cfb383eSMatthias Ringwald 55269784dac1SMatthias Ringwald if (size < 3) return; 55279784dac1SMatthias Ringwald 55281cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 5529a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 55301cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 55311cfb383eSMatthias Ringwald 55329784dac1SMatthias Ringwald switch (event_type){ 55339784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 55349784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 55359784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 55369784dac1SMatthias Ringwald break; 55379784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 55389784dac1SMatthias Ringwald if (size != 257) return; 55399784dac1SMatthias Ringwald if (num_responses != 1) return; 55409784dac1SMatthias Ringwald break; 55419784dac1SMatthias Ringwald default: 55429784dac1SMatthias Ringwald return; 55439784dac1SMatthias Ringwald } 55449784dac1SMatthias Ringwald 55451cfb383eSMatthias Ringwald // event[1] is set at the end 55461cfb383eSMatthias Ringwald int i; 55471cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 55481cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 55491cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 5550ac973fd6SMatthias Ringwald uint8_t event_size = 27; // if name is not set by EIR 55511cfb383eSMatthias Ringwald 55526535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 55530e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 55546535961aSMatthias Ringwald (void)memcpy(&event[9], 55556535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 55566535961aSMatthias Ringwald 3); // class of device 55576535961aSMatthias Ringwald (void)memcpy(&event[12], 55586535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 55596535961aSMatthias Ringwald 2); // clock offset 55601cfb383eSMatthias Ringwald 55611cfb383eSMatthias Ringwald switch (event_type){ 55621cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 55631cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 55641cfb383eSMatthias Ringwald break; 55651cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 55661cfb383eSMatthias Ringwald event[14] = 1; 5567a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 55681cfb383eSMatthias Ringwald // 16,17 = 0, size 18 55691cfb383eSMatthias Ringwald break; 55701cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 55711cfb383eSMatthias Ringwald event[14] = 1; 5572a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 557379186815SMatthias Ringwald // EIR packets only contain a single inquiry response 55741cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 55751cfb383eSMatthias Ringwald name = NULL; 5576a8c4e5adSMatthias Ringwald // Iterate over EIR data 5577a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 55781cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 55791cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 55801cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 5581ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 55821cfb383eSMatthias Ringwald switch (data_type){ 55831cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 55841cfb383eSMatthias Ringwald if (name) continue; 5585cf373d3aSMatthias Ringwald /* fall through */ 55861cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 55871cfb383eSMatthias Ringwald name = data; 55881cfb383eSMatthias Ringwald name_len = data_size; 55891cfb383eSMatthias Ringwald break; 5590ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 5591ac9136ccSMatthias Ringwald if (data_size != 8) break; 5592ac9136ccSMatthias Ringwald event[16] = 1; 55933c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 5594ac9136ccSMatthias Ringwald break; 55951cfb383eSMatthias Ringwald default: 55961cfb383eSMatthias Ringwald break; 55971cfb383eSMatthias Ringwald } 55981cfb383eSMatthias Ringwald } 55991cfb383eSMatthias Ringwald if (name){ 5600ac9136ccSMatthias Ringwald event[25] = 1; 56011cfb383eSMatthias Ringwald // truncate name if needed 56021cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 5603ac9136ccSMatthias Ringwald event[26] = len; 5604ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 56051cfb383eSMatthias Ringwald event_size += len; 56061cfb383eSMatthias Ringwald } 56071cfb383eSMatthias Ringwald break; 56087bbeb3adSMilanka Ringwald default: 56097bbeb3adSMilanka Ringwald return; 56101cfb383eSMatthias Ringwald } 56111cfb383eSMatthias Ringwald event[1] = event_size - 2; 56121cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 56131cfb383eSMatthias Ringwald } 56141cfb383eSMatthias Ringwald } 561535454696SMatthias Ringwald #endif 56163bc639ceSMatthias Ringwald 561771de195eSMatthias Ringwald void hci_emit_state(void){ 56183a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 5619425d1371Smatthias.ringwald uint8_t event[3]; 562080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 56214ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 56223a9fb326S[email protected] event[2] = hci_stack->state; 5623d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5624c8e4258aSmatthias.ringwald } 5625c8e4258aSmatthias.ringwald 562635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 56272deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 5628425d1371Smatthias.ringwald uint8_t event[13]; 5629c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5630425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 563117f1ba2aSmatthias.ringwald event[2] = status; 56322deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 56332deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 5634c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 5635c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 5636d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5637c8e4258aSmatthias.ringwald } 563852db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 563952db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 564052db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 564152db98b2SMatthias Ringwald uint8_t event[4]; 564252db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 564352db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 564452db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 564552db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 564652db98b2SMatthias Ringwald } 564735454696SMatthias Ringwald #endif 5648c8e4258aSmatthias.ringwald 564935454696SMatthias Ringwald #ifdef ENABLE_BLE 5650d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5651667ba9d1SMatthias 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){ 56524f3229d8S[email protected] uint8_t event[21]; 56534f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 56544ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 56554f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 56564f3229d8S[email protected] event[3] = status; 5657fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 56584f3229d8S[email protected] event[6] = 0; // TODO: role 56596e2e9a6bS[email protected] event[7] = address_type; 5660724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 5661f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 5662f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 5663f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 56644f3229d8S[email protected] event[20] = 0; // master clock accuracy 5665d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 56664f3229d8S[email protected] } 566735454696SMatthias Ringwald #endif 5668d70217a2SMatthias Ringwald #endif 56694f3229d8S[email protected] 5670fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 5671fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 5672fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 5673fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 5674fd43c0e0SMatthias Ringwald } 5675fd43c0e0SMatthias Ringwald 5676fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 5677425d1371Smatthias.ringwald uint8_t event[6]; 56783c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 56794ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 56803c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 5681fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 56823c4d4b90Smatthias.ringwald event[5] = reason; 5683d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 56843c4d4b90Smatthias.ringwald } 56853c4d4b90Smatthias.ringwald 5686b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 5687e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 5688425d1371Smatthias.ringwald uint8_t event[3]; 568980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 56904ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 569143bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 5692d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 569343bfb1bdSmatthias.ringwald } 5694038bc64cSmatthias.ringwald 5695b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 5696e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 5697425d1371Smatthias.ringwald uint8_t event[2]; 569880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 56994ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 5700d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5701038bc64cSmatthias.ringwald } 57021b0e3922Smatthias.ringwald 570335454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 570435454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 570535454696SMatthias Ringwald uint8_t event[9]; 570635454696SMatthias Ringwald int pos = 0; 570735454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 57084ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 570935454696SMatthias Ringwald event[pos++] = status; 571035454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 5711d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5712381fbed8Smatthias.ringwald } 5713458bf4e8S[email protected] 571435454696SMatthias Ringwald 571535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 571635454696SMatthias Ringwald 5717b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 5718df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 5719a00031e2S[email protected] uint8_t event[5]; 5720e00caf9cS[email protected] int pos = 0; 57215611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 5722e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 5723f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 5724e00caf9cS[email protected] pos += 2; 5725e00caf9cS[email protected] event[pos++] = level; 5726d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5727e00caf9cS[email protected] } 5728e00caf9cS[email protected] 572935454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 573035454696SMatthias Ringwald if (!connection) return LEVEL_0; 57318daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 5732fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 57338daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 5734170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 57354051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 5736170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 57370e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 5738170fafaeSMatthias Ringwald security_level = LEVEL_3; 5739170fafaeSMatthias Ringwald } 5740170fafaeSMatthias Ringwald return security_level; 574135454696SMatthias Ringwald } 574235454696SMatthias Ringwald 574335454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 574435454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 574535454696SMatthias Ringwald uint8_t event[3]; 574635454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 574735454696SMatthias Ringwald event[1] = sizeof(event) - 2; 574835454696SMatthias Ringwald event[2] = enabled; 5749d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5750ad83dc6aS[email protected] } 5751ad83dc6aS[email protected] 575298a2fd1cSMatthias Ringwald // query if remote side supports eSCO 575320dcdd22SMatthias Ringwald bool hci_remote_esco_supported(hci_con_handle_t con_handle){ 575498a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 575520dcdd22SMatthias Ringwald if (!connection) return false; 575676ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 575798a2fd1cSMatthias Ringwald } 575898a2fd1cSMatthias Ringwald 575967aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 576067aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 576167aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 576267aae551SMatthias Ringwald } 576367aae551SMatthias Ringwald 57642bd8b7e7S[email protected] // query if remote side supports SSP 576520dcdd22SMatthias Ringwald bool hci_remote_ssp_supported(hci_con_handle_t con_handle){ 57662bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 576720dcdd22SMatthias Ringwald if (!connection) return false; 576867aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 57692bd8b7e7S[email protected] } 57702bd8b7e7S[email protected] 577120dcdd22SMatthias Ringwald bool gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 5772df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 5773df3354fcS[email protected] } 5774df3354fcS[email protected] 57756ffc78ceSMatthias Ringwald /** 57766ffc78ceSMatthias Ringwald * Check if remote supported features query has completed 57776ffc78ceSMatthias Ringwald */ 57786ffc78ceSMatthias Ringwald bool hci_remote_features_available(hci_con_handle_t handle){ 57796ffc78ceSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 57806ffc78ceSMatthias Ringwald if (!connection) return false; 57816ffc78ceSMatthias Ringwald return (connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0; 57826ffc78ceSMatthias Ringwald } 57836ffc78ceSMatthias Ringwald 5784d6596031SMatthias Ringwald /** 5785d6596031SMatthias Ringwald * Trigger remote supported features query 5786d6596031SMatthias Ringwald */ 5787c9742cd1SMatthias Ringwald 5788c9742cd1SMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection){ 5789c9742cd1SMatthias Ringwald if ((connection->bonding_flags & (BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_RECEIVED_REMOTE_FEATURES)) == 0){ 5790c9742cd1SMatthias Ringwald connection->bonding_flags |= BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 5791c9742cd1SMatthias Ringwald } 5792c9742cd1SMatthias Ringwald } 5793c9742cd1SMatthias Ringwald 5794d6596031SMatthias Ringwald void hci_remote_features_query(hci_con_handle_t con_handle){ 5795d6596031SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5796d6596031SMatthias Ringwald if (!connection) return; 5797c9742cd1SMatthias Ringwald hci_trigger_remote_features_for_connection(connection); 5798d6596031SMatthias Ringwald hci_run(); 5799d6596031SMatthias Ringwald } 5800d6596031SMatthias Ringwald 5801458bf4e8S[email protected] // GAP API 5802458bf4e8S[email protected] /** 5803458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 5804458bf4e8S[email protected] * @praram enabled 5805458bf4e8S[email protected] */ 58064c57c146S[email protected] void gap_set_bondable_mode(int enable){ 58073a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 5808458bf4e8S[email protected] } 58094ef6443cSMatthias Ringwald /** 58104ef6443cSMatthias Ringwald * @brief Get bondable mode. 58114ef6443cSMatthias Ringwald * @return 1 if bondable 58124ef6443cSMatthias Ringwald */ 58134ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 58144ef6443cSMatthias Ringwald return hci_stack->bondable; 58154ef6443cSMatthias Ringwald } 5816cb230b9dS[email protected] 5817cb230b9dS[email protected] /** 581834d2123cS[email protected] * @brief map link keys to security levels 5819cb230b9dS[email protected] */ 582034d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 582134d2123cS[email protected] switch (link_key_type){ 58223c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 58233c68dfa9S[email protected] return LEVEL_4; 58243c68dfa9S[email protected] case COMBINATION_KEY: 58253c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 58263c68dfa9S[email protected] return LEVEL_3; 58273c68dfa9S[email protected] default: 58283c68dfa9S[email protected] return LEVEL_2; 58293c68dfa9S[email protected] } 5830cb230b9dS[email protected] } 5831cb230b9dS[email protected] 58328b35e16aSMatthias Ringwald /** 58338b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 58348b35e16aSMatthias Ringwald */ 583536c3546bSMatthias Ringwald bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 58368b35e16aSMatthias Ringwald switch (link_key_type){ 58378b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 58388b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 583936c3546bSMatthias Ringwald return true; 58408b35e16aSMatthias Ringwald default: 584136c3546bSMatthias Ringwald return false; 58428b35e16aSMatthias Ringwald } 58438b35e16aSMatthias Ringwald } 58448b35e16aSMatthias Ringwald 58458b35e16aSMatthias Ringwald /** 58468b35e16aSMatthias Ringwald * @brief map link keys to authenticated 58478b35e16aSMatthias Ringwald */ 58487040ba26SMatthias Ringwald bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 58498b35e16aSMatthias Ringwald switch (link_key_type){ 58508b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 58518b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 58527040ba26SMatthias Ringwald return true; 58538b35e16aSMatthias Ringwald default: 58547040ba26SMatthias Ringwald return false; 58558b35e16aSMatthias Ringwald } 58568b35e16aSMatthias Ringwald } 58578b35e16aSMatthias Ringwald 5858552b9260SMatthias Ringwald bool gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 58595127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 5860106d6d11S[email protected] return level > LEVEL_2; 5861106d6d11S[email protected] } 5862106d6d11S[email protected] 586334d2123cS[email protected] /** 586434d2123cS[email protected] * @brief get current security level 586534d2123cS[email protected] */ 586634d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 586734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 586834d2123cS[email protected] if (!connection) return LEVEL_0; 586934d2123cS[email protected] return gap_security_level_for_connection(connection); 587034d2123cS[email protected] } 587134d2123cS[email protected] 5872cb230b9dS[email protected] /** 5873cb230b9dS[email protected] * @brief request connection to device to 5874cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 5875cb230b9dS[email protected] */ 587634d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 587734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 587834d2123cS[email protected] if (!connection){ 5879a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 588034d2123cS[email protected] return; 588134d2123cS[email protected] } 5882defbf200SMatthias Ringwald 5883defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 5884defbf200SMatthias Ringwald 5885bc00e12cSMatthias Ringwald // Core Spec 5.2, GAP 5.2.2: "When in Secure Connections Only mode, all services (except those allowed to have Security Mode 4, Level 0) 5886bc00e12cSMatthias Ringwald // available on the BR/EDR physical transport require Security Mode 4, Level 4 " 5887bc00e12cSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode && (requested_level != LEVEL_0)){ 5888bc00e12cSMatthias Ringwald requested_level = LEVEL_4; 5889bc00e12cSMatthias Ringwald } 5890bc00e12cSMatthias Ringwald 589134d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 589283d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 589383d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 589483d08d7cSMatthias Ringwald 5895dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 5896dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 5897dbd5dcc3SMatthias Ringwald if (authentication_active){ 58981cf0a6c8SMatthias Ringwald // authentication already active 589983d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 590083d08d7cSMatthias Ringwald // increase requested level as new level is higher 590183d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 590283d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 590383d08d7cSMatthias Ringwald } 59041cf0a6c8SMatthias Ringwald } else { 590583d08d7cSMatthias Ringwald // no request active, notify if security sufficient 590683d08d7cSMatthias Ringwald if (requested_level <= current_level){ 5907a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 590834d2123cS[email protected] return; 590934d2123cS[email protected] } 5910a00031e2S[email protected] 5911e060c07dSMatthias Ringwald // store request 591234d2123cS[email protected] connection->requested_security_level = requested_level; 5913a00031e2S[email protected] 5914e5470e6dSMatthias Ringwald // request remote features if not already active 5915e5470e6dSMatthias Ringwald hci_remote_features_query(con_handle); 5916e5470e6dSMatthias Ringwald 59171cf0a6c8SMatthias Ringwald // start to authenticate connection 59181eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 5919e80b2cf9S[email protected] hci_run(); 5920e00caf9cS[email protected] } 59211cf0a6c8SMatthias Ringwald } 5922ad83dc6aS[email protected] 5923ad83dc6aS[email protected] /** 5924ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 5925ad83dc6aS[email protected] * @param device 5926ad83dc6aS[email protected] * @param request MITM protection 5927ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 5928ad83dc6aS[email protected] */ 5929ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 5930ad83dc6aS[email protected] 5931ad83dc6aS[email protected] // create connection state machine 5932f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 5933ad83dc6aS[email protected] 5934ad83dc6aS[email protected] if (!connection){ 5935ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 5936ad83dc6aS[email protected] } 5937ad83dc6aS[email protected] 5938ad83dc6aS[email protected] // delete linkn key 593915a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 5940ad83dc6aS[email protected] 5941ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 5942ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 5943ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 5944f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 5945ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 5946ad83dc6aS[email protected] 5947ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 5948ad83dc6aS[email protected] 5949ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 5950ad83dc6aS[email protected] // handle: authentication failure 5951ad83dc6aS[email protected] // handle: disconnect on done 5952ad83dc6aS[email protected] 5953ad83dc6aS[email protected] hci_run(); 5954ad83dc6aS[email protected] 5955ad83dc6aS[email protected] return 0; 5956ad83dc6aS[email protected] } 59578e618f72S[email protected] 59588e618f72S[email protected] void gap_set_local_name(const char * local_name){ 59598e618f72S[email protected] hci_stack->local_name = local_name; 5960baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_LOCAL_NAME; 596159d59ecfSMatthias Ringwald // also update EIR if not set by user 596259d59ecfSMatthias Ringwald if (hci_stack->eir_data == NULL){ 5963baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 596459d59ecfSMatthias Ringwald } 596559d59ecfSMatthias Ringwald hci_run(); 59668e618f72S[email protected] } 59671aeec2ebSMatthias Ringwald #endif 59688e618f72S[email protected] 596935454696SMatthias Ringwald 597035454696SMatthias Ringwald #ifdef ENABLE_BLE 597135454696SMatthias Ringwald 5972d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5973d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 5974fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 59757bdc6798S[email protected] hci_run(); 59767bdc6798S[email protected] } 59778e618f72S[email protected] 5978d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 5979fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 59807bdc6798S[email protected] hci_run(); 59817bdc6798S[email protected] } 59824f3229d8S[email protected] 5983a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 5984ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 5985a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 5986ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 5987ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 59888b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 5989ef11999fSmatthias.ringwald hci_run(); 5990ef11999fSmatthias.ringwald } 59914f3229d8S[email protected] 5992a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 5993a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 5994a7a719e9SMatthias Ringwald } 5995a7a719e9SMatthias Ringwald 5996667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 59974f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 59984f3229d8S[email protected] if (!conn){ 5999d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 6000d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 6001d32b3f05SMatthias Ringwald log_error("le connection already active"); 6002d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6003d32b3f05SMatthias Ringwald } 6004d32b3f05SMatthias Ringwald 6005d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 60062e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 60074f3229d8S[email protected] if (!conn){ 60084f3229d8S[email protected] // notify client that alloc failed 60096e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 6010d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 6011472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 60124f3229d8S[email protected] } 6013d5b1a89eSMatthias Ringwald 6014d5b1a89eSMatthias Ringwald // set le connecting state 6015d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 6016d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 6017d5b1a89eSMatthias Ringwald } 6018d5b1a89eSMatthias Ringwald 60194f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 6020d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 6021564fca32S[email protected] hci_run(); 6022b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 60234f3229d8S[email protected] } 60240bf6344aS[email protected] 60250bf6344aS[email protected] if (!hci_is_le_connection(conn) || 6026a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 60270e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 60282e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 6029d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 6030616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 60310bf6344aS[email protected] } 60320bf6344aS[email protected] 6033b0136355SMatthias Ringwald // check if connection was just disconnected 6034b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 6035b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 6036b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 6037b0136355SMatthias Ringwald hci_run(); 6038b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 6039b0136355SMatthias Ringwald } 6040b0136355SMatthias Ringwald 6041d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 6042d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 60434f3229d8S[email protected] hci_run(); 6044b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 60454f3229d8S[email protected] } 60464f3229d8S[email protected] 60477851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 6048d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 6049665d90f2SMatthias Ringwald btstack_linked_item_t *it; 6050a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 60510bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 60520bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 60530bf6344aS[email protected] switch (conn->state){ 6054a6725849S[email protected] case SEND_CREATE_CONNECTION: 60557851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 60569c77c9dbSMatthias Ringwald case SENT_CANCEL_CONNECTION: 60577851196eSmatthias.ringwald return conn; 60587851196eSmatthias.ringwald default: 60597851196eSmatthias.ringwald break; 60607851196eSmatthias.ringwald }; 60617851196eSmatthias.ringwald } 60627851196eSmatthias.ringwald return NULL; 60637851196eSmatthias.ringwald } 60647851196eSmatthias.ringwald 6065d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 6066d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 6067616edd56SMatthias Ringwald if (!conn) return 0; 60687851196eSmatthias.ringwald switch (conn->state){ 60697851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 60707851196eSmatthias.ringwald // skip sending create connection and emit event instead 607110f8f469SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 60722e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 6073665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 60747851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 60750bf6344aS[email protected] break; 6076a6725849S[email protected] case SENT_CREATE_CONNECTION: 60777851196eSmatthias.ringwald // request to send cancel connection 60780bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 60790bf6344aS[email protected] hci_run(); 60800bf6344aS[email protected] break; 60810bf6344aS[email protected] default: 60820bf6344aS[email protected] break; 60830bf6344aS[email protected] } 6084616edd56SMatthias Ringwald return 0; 6085e31f89a7S[email protected] } 6086d70217a2SMatthias Ringwald #endif 60874f3229d8S[email protected] 608813e645d2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6089c37a3166S[email protected] /** 60906012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 6091cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 6092cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 60936012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 60946012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 60956012052bSMatthias Ringwald * @param conn_latency, default: 4 60966012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 60976012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 60986012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 60996012052bSMatthias Ringwald */ 61006012052bSMatthias Ringwald 6101cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 6102cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 6103cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 6104cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 6105cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 61066012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 61076012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 61086012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 61096012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 61106012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 61116012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 61126012052bSMatthias Ringwald } 611313e645d2SMatthias Ringwald #endif 61146012052bSMatthias Ringwald 61156012052bSMatthias Ringwald /** 6116c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 6117c37a3166S[email protected] * @param handle 6118c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 6119c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 6120c37a3166S[email protected] * @param conn_latency 6121c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 61226b65794dSMilanka Ringwald * @return 0 if ok 6123c37a3166S[email protected] */ 6124c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 6125c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 6126c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 6127c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6128c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 6129c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 6130c37a3166S[email protected] connection->le_conn_latency = conn_latency; 6131c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 613284cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 6133cfc59f1bSMatthias Ringwald hci_run(); 6134c37a3166S[email protected] return 0; 6135c37a3166S[email protected] } 6136c37a3166S[email protected] 613745c102fdSMatthias Ringwald /** 6138b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 6139b68d7bc3SMatthias Ringwald * @param handle 6140b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 6141b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 6142b68d7bc3SMatthias Ringwald * @param conn_latency 6143b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 61446b65794dSMilanka Ringwald * @return 0 if ok 6145b68d7bc3SMatthias Ringwald */ 6146b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 6147b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 6148b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 6149b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6150b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 6151b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 6152b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 6153b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 6154b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 615509c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 615609c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 6157b68d7bc3SMatthias Ringwald return 0; 6158b68d7bc3SMatthias Ringwald } 6159b68d7bc3SMatthias Ringwald 6160d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 6161d70217a2SMatthias Ringwald 6162b68d7bc3SMatthias Ringwald /** 616345c102fdSMatthias Ringwald * @brief Set Advertisement Data 616445c102fdSMatthias Ringwald * @param advertising_data_length 616545c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 616645c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 616745c102fdSMatthias Ringwald */ 616845c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 616945c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 617045c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 6171501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 6172bbc366e6SMatthias Ringwald hci_run(); 617345c102fdSMatthias Ringwald } 6174501f56b3SMatthias Ringwald 6175501f56b3SMatthias Ringwald /** 6176501f56b3SMatthias Ringwald * @brief Set Scan Response Data 6177501f56b3SMatthias Ringwald * @param advertising_data_length 6178501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 6179501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 6180501f56b3SMatthias Ringwald */ 6181501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 6182501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 6183501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 6184501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 6185bbc366e6SMatthias Ringwald hci_run(); 618645c102fdSMatthias Ringwald } 618745c102fdSMatthias Ringwald 618845c102fdSMatthias Ringwald /** 618945c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 619045c102fdSMatthias Ringwald * @param adv_int_min 619145c102fdSMatthias Ringwald * @param adv_int_max 619245c102fdSMatthias Ringwald * @param adv_type 619345c102fdSMatthias Ringwald * @param direct_address_type 619445c102fdSMatthias Ringwald * @param direct_address 619545c102fdSMatthias Ringwald * @param channel_map 619645c102fdSMatthias Ringwald * @param filter_policy 619745c102fdSMatthias Ringwald * 619845c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 619945c102fdSMatthias Ringwald */ 620045c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 6201b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 620245c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 620345c102fdSMatthias Ringwald 620445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 620545c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 620645c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 620745c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 620845c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 620945c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 62106535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 62116535961aSMatthias Ringwald 6); 621245c102fdSMatthias Ringwald 6213a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS | LE_ADVERTISEMENT_TASKS_PARAMS_SET; 6214bbc366e6SMatthias Ringwald hci_run(); 621545c102fdSMatthias Ringwald } 621645c102fdSMatthias Ringwald 621745c102fdSMatthias Ringwald /** 621845c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 621945c102fdSMatthias Ringwald * @param enabled 622045c102fdSMatthias Ringwald */ 622145c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 62225226d7f2SMatthias Ringwald hci_stack->le_advertisements_enabled = enabled != 0; 6223bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 6224cfc59f1bSMatthias Ringwald hci_run(); 622545c102fdSMatthias Ringwald } 622645c102fdSMatthias Ringwald 622735454696SMatthias Ringwald #endif 622806e5cf96SMatthias Ringwald 622906e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 623006e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 623106e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 623206e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 623306e5cf96SMatthias Ringwald 623464068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 623506e5cf96SMatthias Ringwald // update advertisement parameters, too 623606e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 6237bbc366e6SMatthias Ringwald hci_run(); 623864068776SMatthias Ringwald #endif 623964068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 624006e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 624164068776SMatthias Ringwald #endif 624206e5cf96SMatthias Ringwald } 624306e5cf96SMatthias Ringwald 62449136bde8SMatthias Ringwald void hci_le_random_address_set(const bd_addr_t random_address){ 62459136bde8SMatthias Ringwald memcpy(hci_stack->le_random_address, random_address, 6); 62469136bde8SMatthias Ringwald hci_stack->le_random_address_set = true; 62479136bde8SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 62489136bde8SMatthias Ringwald hci_run(); 62499136bde8SMatthias Ringwald } 62509136bde8SMatthias Ringwald 6251d70217a2SMatthias Ringwald #endif 625245c102fdSMatthias Ringwald 6253616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 62545917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 62555917a5c5S[email protected] if (!conn){ 62567851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 6257616edd56SMatthias Ringwald return 0; 62585917a5c5S[email protected] } 62597fd7aa6fSMatthias Ringwald // ignore if already disconnected 62607fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 62617fd7aa6fSMatthias Ringwald return 0; 62627fd7aa6fSMatthias Ringwald } 62635917a5c5S[email protected] conn->state = SEND_DISCONNECT; 62645917a5c5S[email protected] hci_run(); 6265616edd56SMatthias Ringwald return 0; 62664f3229d8S[email protected] } 626704a6ef8cSmatthias.ringwald 6268228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 6269228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6270228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 62718daf94bcSMatthias Ringwald connectionSetAuthenticationFlags(hci_connection, AUTH_FLAG_READ_RSSI); 6272228e430cSMatthias Ringwald hci_run(); 6273228e430cSMatthias Ringwald return 1; 6274228e430cSMatthias Ringwald } 6275228e430cSMatthias Ringwald 6276a1bf5ae7SMatthias Ringwald /** 6277a1bf5ae7SMatthias Ringwald * @brief Get connection type 6278a1bf5ae7SMatthias Ringwald * @param con_handle 6279a1bf5ae7SMatthias Ringwald * @result connection_type 6280a1bf5ae7SMatthias Ringwald */ 6281a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 6282a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 6283a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6284a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 6285a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 6286a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6287a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 6288a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 6289a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 6290f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6291a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 6292a1bf5ae7SMatthias Ringwald default: 6293a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 6294a1bf5ae7SMatthias Ringwald } 6295a1bf5ae7SMatthias Ringwald } 6296a1bf5ae7SMatthias Ringwald 62972dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 62982dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 62992dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 63002dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 63012dceb1d6SMatthias Ringwald } 63022dceb1d6SMatthias Ringwald 63032dceb1d6SMatthias Ringwald 630444f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 6305667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 630688a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 630788a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 630888a03c8dSMatthias Ringwald conn->request_role = role; 630988a03c8dSMatthias Ringwald hci_run(); 6310d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 631188a03c8dSMatthias Ringwald } 631244f858f3SMatthias Ringwald #endif 631388a03c8dSMatthias Ringwald 6314a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 63154f551432SMatthias Ringwald 6316b45b7749SMilanka 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){ 6317b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6318b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6319b90f6e0aSMatthias Ringwald 6320b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 6321b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 6322b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 6323b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 6324b90f6e0aSMatthias Ringwald 6325b90f6e0aSMatthias Ringwald hci_run(); 6326b90f6e0aSMatthias Ringwald 6327b90f6e0aSMatthias Ringwald return 0; 6328b90f6e0aSMatthias Ringwald } 6329b90f6e0aSMatthias Ringwald 6330667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 63317a92a9dbSMatthias Ringwald // check if already in list 63327a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 63337a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 63347a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 63357a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 63367a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 63377a92a9dbSMatthias Ringwald continue; 63387a92a9dbSMatthias Ringwald } 63397a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 63407a92a9dbSMatthias Ringwald continue; 63417a92a9dbSMatthias Ringwald } 6342287379ccSMatthias Ringwald // disallow if already scheduled to add 6343287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 63447a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 63457a92a9dbSMatthias Ringwald } 6346287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 6347287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 6348287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 6349287379ccSMatthias Ringwald } 63507a92a9dbSMatthias Ringwald // alloc and add to list 6351e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 6352e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 6353e83201bcSMatthias Ringwald entry->address_type = address_type; 63546535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 6355e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 6356665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 6357226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 6358ac9c45e0SMatthias Ringwald } 6359ac9c45e0SMatthias Ringwald 6360667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6361665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6362665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 6363665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6364665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 63657a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 63667a92a9dbSMatthias Ringwald continue; 63677a92a9dbSMatthias Ringwald } 63687a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 63697a92a9dbSMatthias Ringwald continue; 63707a92a9dbSMatthias Ringwald } 6371e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 6372e83201bcSMatthias Ringwald // remove from controller if already present 6373e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6374a3b69fdeSMatthias Ringwald } else { 6375226db5efSMatthias Ringwald // directly remove entry from whitelist 6376665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6377e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 6378e83201bcSMatthias Ringwald } 6379a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6380a3b69fdeSMatthias Ringwald } 6381a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 638242ff5ba1SMatthias Ringwald } 638342ff5ba1SMatthias Ringwald 6384226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 6385665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6386665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 6387665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6388665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 6389e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 6390e83201bcSMatthias Ringwald // remove from controller if already present 6391e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6392e83201bcSMatthias Ringwald continue; 6393e83201bcSMatthias Ringwald } 639491915b0bSMatthias Ringwald // directly remove entry from whitelist 6395665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6396e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 6397e83201bcSMatthias Ringwald } 6398226db5efSMatthias Ringwald } 6399226db5efSMatthias Ringwald 640083dc1a98SMatthias Ringwald // free all entries unconditionally 640183dc1a98SMatthias Ringwald static void hci_whitelist_free(void){ 640283dc1a98SMatthias Ringwald btstack_linked_list_iterator_t lit; 640383dc1a98SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 640483dc1a98SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 640583dc1a98SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 640683dc1a98SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 640783dc1a98SMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 640883dc1a98SMatthias Ringwald } 640983dc1a98SMatthias Ringwald } 641083dc1a98SMatthias Ringwald 6411a3b69fdeSMatthias Ringwald /** 6412a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 64136b65794dSMilanka Ringwald * @return 0 if ok 6414a3b69fdeSMatthias Ringwald */ 6415a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 6416a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 6417a3b69fdeSMatthias Ringwald hci_run(); 6418a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6419a3b69fdeSMatthias Ringwald } 6420a3b69fdeSMatthias Ringwald 6421a3b69fdeSMatthias Ringwald /** 6422a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 6423a3b69fdeSMatthias Ringwald * @param address_typ 6424a3b69fdeSMatthias Ringwald * @param address 64256b65794dSMilanka Ringwald * @return 0 if ok 6426a3b69fdeSMatthias Ringwald */ 6427667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 6428a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 6429a3b69fdeSMatthias Ringwald if (status){ 6430a3b69fdeSMatthias Ringwald return status; 6431a3b69fdeSMatthias Ringwald } 6432a3b69fdeSMatthias Ringwald hci_run(); 6433a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6434a3b69fdeSMatthias Ringwald } 6435a3b69fdeSMatthias Ringwald 6436a3b69fdeSMatthias Ringwald /** 6437a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 6438a3b69fdeSMatthias Ringwald * @param address_typ 6439a3b69fdeSMatthias Ringwald * @param address 64406b65794dSMilanka Ringwald * @return 0 if ok 6441a3b69fdeSMatthias Ringwald */ 6442667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6443a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 6444a3b69fdeSMatthias Ringwald if (status){ 6445a3b69fdeSMatthias Ringwald return status; 6446a3b69fdeSMatthias Ringwald } 6447a3b69fdeSMatthias Ringwald hci_run(); 6448a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6449a3b69fdeSMatthias Ringwald } 6450a3b69fdeSMatthias Ringwald 6451226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6452226db5efSMatthias Ringwald /** 645395e257d9SMatthias Ringwald * @brief Connect with Whitelist 645495e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 64556b65794dSMilanka Ringwald * @return - if ok 645695e257d9SMatthias Ringwald */ 645795e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 645895e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 645995e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 646095e257d9SMatthias Ringwald } 646195e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 646295e257d9SMatthias Ringwald hci_run(); 646395e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 646495e257d9SMatthias Ringwald } 646595e257d9SMatthias Ringwald 646695e257d9SMatthias Ringwald /** 6467226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 6468226db5efSMatthias Ringwald * @param address_typ 6469226db5efSMatthias Ringwald * @param address 64706b65794dSMilanka Ringwald * @return 0 if ok 6471226db5efSMatthias Ringwald */ 6472667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 647395e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 6474226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6475226db5efSMatthias Ringwald } 6476226db5efSMatthias Ringwald 6477226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 647863f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 6479226db5efSMatthias Ringwald return status; 6480226db5efSMatthias Ringwald } 6481226db5efSMatthias Ringwald 6482226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 6483226db5efSMatthias Ringwald 6484226db5efSMatthias Ringwald hci_run(); 648595e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6486226db5efSMatthias Ringwald } 6487226db5efSMatthias Ringwald 6488226db5efSMatthias Ringwald /** 6489226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 6490226db5efSMatthias Ringwald * @param address_typ 6491226db5efSMatthias Ringwald * @param address 64926b65794dSMilanka Ringwald * @return 0 if ok 6493226db5efSMatthias Ringwald */ 6494667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 649595e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 649695e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 649795e257d9SMatthias Ringwald } 649895e257d9SMatthias Ringwald 6499226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 650095e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 650195e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 650295e257d9SMatthias Ringwald } 6503226db5efSMatthias Ringwald hci_run(); 6504226db5efSMatthias Ringwald return 0; 6505226db5efSMatthias Ringwald } 6506226db5efSMatthias Ringwald 6507226db5efSMatthias Ringwald /** 6508226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 6509226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 6510226db5efSMatthias Ringwald */ 651195e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 651295e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 651395e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 651495e257d9SMatthias Ringwald } 6515226db5efSMatthias Ringwald hci_whitelist_clear(); 6516226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 6517e83201bcSMatthias Ringwald hci_run(); 651895e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6519ac9c45e0SMatthias Ringwald } 6520c9db5c21SMilanka Ringwald 6521b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 6522b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6523c9db5c21SMilanka Ringwald if (!conn) return 0; 6524c9db5c21SMilanka Ringwald return conn->le_connection_interval; 6525c9db5c21SMilanka Ringwald } 6526d70217a2SMatthias Ringwald #endif 65274f551432SMatthias Ringwald #endif 65284f551432SMatthias Ringwald 652935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6530ac9c45e0SMatthias Ringwald /** 6531ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 6532a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 6533ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 6534ff00ed1cSMatthias Ringwald */ 6535ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 6536ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 6537baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 653859d59ecfSMatthias Ringwald hci_run(); 6539ff00ed1cSMatthias Ringwald } 6540ff00ed1cSMatthias Ringwald 6541ff00ed1cSMatthias Ringwald /** 6542f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 6543f5875de5SMatthias Ringwald * @param duration in 1.28s units 6544f5875de5SMatthias Ringwald * @return 0 if ok 6545f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 6546f5875de5SMatthias Ringwald */ 6547f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 654899449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 6549f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6550a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 6551f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 6552f5875de5SMatthias Ringwald } 6553f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 6554f5875de5SMatthias Ringwald hci_run(); 6555f5875de5SMatthias Ringwald return 0; 6556f5875de5SMatthias Ringwald } 6557f5875de5SMatthias Ringwald 6558f5875de5SMatthias Ringwald /** 6559f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 65606b65794dSMilanka Ringwald * @return 0 if ok 6561f5875de5SMatthias Ringwald */ 6562f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 6563a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 6564f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 6565f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 6566f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 6567f5875de5SMatthias Ringwald return 0; 6568f5875de5SMatthias Ringwald } 6569f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 6570f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 6571f5875de5SMatthias Ringwald hci_run(); 6572f5875de5SMatthias Ringwald return 0; 6573f5875de5SMatthias Ringwald } 6574f5875de5SMatthias Ringwald 6575496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 6576496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 6577496bb884SMatthias Ringwald } 6578496bb884SMatthias Ringwald 65791dc973e7SMatthias Ringwald void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window){ 6580c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_interval = inquiry_scan_interval; 6581c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_window = inquiry_scan_window; 6582baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 6583c0c8a829SMatthias Ringwald hci_run(); 6584c0c8a829SMatthias Ringwald } 6585c0c8a829SMatthias Ringwald 6586b7f1ee76SMatthias Ringwald 6587b7f1ee76SMatthias Ringwald /** 6588b7f1ee76SMatthias Ringwald * @brief Remote Name Request 6589b7f1ee76SMatthias Ringwald * @param addr 6590b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 6591b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 6592b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 6593b7f1ee76SMatthias Ringwald */ 6594667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 6595b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 65966535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 6597b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 6598b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 6599b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 6600b7f1ee76SMatthias Ringwald hci_run(); 6601b7f1ee76SMatthias Ringwald return 0; 6602b7f1ee76SMatthias Ringwald } 6603b7f1ee76SMatthias Ringwald 6604667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 66050a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 66066535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 66070a51f88bSMatthias Ringwald hci_run(); 66080a51f88bSMatthias Ringwald return 0; 66090a51f88bSMatthias Ringwald } 66100a51f88bSMatthias Ringwald 66110a51f88bSMatthias Ringwald /** 6612aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 6613aad97216SMatthias Ringwald * @param addr 6614aad97216SMatthias Ringwald * @param pin_data 6615aad97216SMatthias Ringwald * @param pin_len 6616aad97216SMatthias Ringwald * @return 0 if ok 6617aad97216SMatthias Ringwald */ 6618667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 6619aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6620aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 6621aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 6622aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 6623aad97216SMatthias Ringwald } 6624aad97216SMatthias Ringwald 6625aad97216SMatthias Ringwald /** 66260a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 66270a51f88bSMatthias Ringwald * @param addr 66280a51f88bSMatthias Ringwald * @param pin 66290a51f88bSMatthias Ringwald * @return 0 if ok 66300a51f88bSMatthias Ringwald */ 6631667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 6632aad97216SMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, strlen(pin)); 66330a51f88bSMatthias Ringwald } 66340a51f88bSMatthias Ringwald 66350a51f88bSMatthias Ringwald /** 66360a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 66370a51f88bSMatthias Ringwald * @param addr 66380a51f88bSMatthias Ringwald * @param pin 66390a51f88bSMatthias Ringwald * @return 0 if ok 66400a51f88bSMatthias Ringwald */ 66410a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 6642cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 66430a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 66440a51f88bSMatthias Ringwald } 66450a51f88bSMatthias Ringwald 66460a51f88bSMatthias Ringwald /** 66470a51f88bSMatthias Ringwald * @brief SSP Passkey Response 66480a51f88bSMatthias Ringwald * @param addr 66490a51f88bSMatthias Ringwald * @param passkey 66500a51f88bSMatthias Ringwald * @return 0 if ok 66510a51f88bSMatthias Ringwald */ 6652667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 6653cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6654d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 66550a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 66560a51f88bSMatthias Ringwald } 66570a51f88bSMatthias Ringwald 66580a51f88bSMatthias Ringwald /** 66590a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 66600a51f88bSMatthias Ringwald * @param addr 66610a51f88bSMatthias Ringwald * @param pin 66620a51f88bSMatthias Ringwald * @return 0 if ok 66630a51f88bSMatthias Ringwald */ 6664667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 6665cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 66660a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 66670a51f88bSMatthias Ringwald } 66680a51f88bSMatthias Ringwald 66690a51f88bSMatthias Ringwald /** 66700a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 66710a51f88bSMatthias Ringwald * @param addr 66720a51f88bSMatthias Ringwald * @param passkey 66730a51f88bSMatthias Ringwald * @return 0 if ok 66740a51f88bSMatthias Ringwald */ 6675667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 6676cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 66770a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 66780a51f88bSMatthias Ringwald } 66790a51f88bSMatthias Ringwald 66800a51f88bSMatthias Ringwald /** 66810a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 66820a51f88bSMatthias Ringwald * @param addr 66830a51f88bSMatthias Ringwald * @param pin 66840a51f88bSMatthias Ringwald * @return 0 if ok 66850a51f88bSMatthias Ringwald */ 6686667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 6687cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 66880a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 66890a51f88bSMatthias Ringwald } 66900a51f88bSMatthias Ringwald 6691308eeaffSMatthias Ringwald #if defined(ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY) || defined(ENABLE_EXPLICIT_LINK_KEY_REPLY) 669244565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 669344565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 669444565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 669544565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 669644565b0cSMatthias Ringwald hci_run(); 669744565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 669844565b0cSMatthias Ringwald } 6699308eeaffSMatthias Ringwald #endif 670044565b0cSMatthias Ringwald 6701308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 670244565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 67037ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 670444565b0cSMatthias Ringwald } 670544565b0cSMatthias Ringwald 670644565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 67077ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 670844565b0cSMatthias Ringwald } 670944565b0cSMatthias Ringwald #endif 671044565b0cSMatthias Ringwald 67111849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 67121849becdSMatthias Ringwald /** 67131849becdSMatthias Ringwald * @brief Report Remote OOB Data 67141849becdSMatthias Ringwald * @param bd_addr 67151849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 67161849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 67171849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 67181849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 67191849becdSMatthias Ringwald */ 67201849becdSMatthias 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){ 67211849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 67221849becdSMatthias Ringwald if (connection == NULL) { 67231849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 67241849becdSMatthias Ringwald } 67251849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 67261849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 6727204e8f1dSMatthias Ringwald 6728204e8f1dSMatthias Ringwald // ignore P-256 if not supported by us 6729204e8f1dSMatthias Ringwald if (hci_stack->secure_connections_active){ 67301849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 67311849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 6732204e8f1dSMatthias Ringwald } 6733204e8f1dSMatthias Ringwald 67341849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 67351849becdSMatthias Ringwald } 6736cf01e888SMatthias Ringwald /** 6737cf01e888SMatthias Ringwald * @brief Generate new OOB data 6738cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 6739cf01e888SMatthias Ringwald */ 6740cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 6741cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 6742cf01e888SMatthias Ringwald hci_run(); 6743cf01e888SMatthias Ringwald } 6744cf01e888SMatthias Ringwald 67451849becdSMatthias Ringwald #endif 67461849becdSMatthias Ringwald 6747308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_LINK_KEY_REPLY 6748308eeaffSMatthias Ringwald uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 6749308eeaffSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 6750308eeaffSMatthias Ringwald if (connection == NULL) { 6751308eeaffSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6752308eeaffSMatthias Ringwald } 6753308eeaffSMatthias Ringwald 6754308eeaffSMatthias Ringwald memcpy(connection->link_key, link_key, sizeof(link_key_t)); 6755308eeaffSMatthias Ringwald connection->link_key_type = type; 6756308eeaffSMatthias Ringwald 6757308eeaffSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 6758308eeaffSMatthias Ringwald } 6759308eeaffSMatthias Ringwald 6760308eeaffSMatthias Ringwald #endif // ENABLE_EXPLICIT_LINK_KEY_REPLY 6761f5875de5SMatthias Ringwald /** 6762f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 6763f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 6764f6858d14SMatthias Ringwald */ 6765b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 6766b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 6767f6858d14SMatthias Ringwald } 6768f6858d14SMatthias Ringwald 6769f6858d14SMatthias Ringwald /** 6770d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 6771d950d659SMatthias Ringwald */ 6772d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 6773d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 6774d950d659SMatthias Ringwald } 6775d950d659SMatthias Ringwald 6776d950d659SMatthias Ringwald /** 6777d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 6778d950d659SMatthias Ringwald * @return current voice setting 6779d950d659SMatthias Ringwald */ 67800cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 6781d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 6782d950d659SMatthias Ringwald } 6783d950d659SMatthias Ringwald 6784400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 6785400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 6786400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 6787400ff5abSMatthias Ringwald if (!transport_name) return 0; 6788400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 6789400ff5abSMatthias Ringwald } 6790400ff5abSMatthias Ringwald 6791b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 6792b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 6793b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 6794b3aad8daSMatthias Ringwald */ 679520dcdd22SMatthias Ringwald uint16_t hci_get_sco_packet_length(void){ 679620dcdd22SMatthias Ringwald uint16_t sco_packet_length = 0; 6797cf119f3bSMatthias Ringwald 67981e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 67996f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 68001431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 6801cf119f3bSMatthias Ringwald 6802400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 6803400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 6804cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 680552e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 680652e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 6807400ff5abSMatthias Ringwald } else { 6808400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 6809400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 6810d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 6811d966a453SMatthias Ringwald if (sco_packet_length > hci_stack->sco_data_packet_length){ 6812d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 6813d966a453SMatthias Ringwald } 6814400ff5abSMatthias Ringwald } 6815a3069afeSMatthias Ringwald #endif 68165b7087c7SMatthias Ringwald 68175b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 68185b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 68191e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 68205b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 68215b7087c7SMatthias Ringwald #endif 6822a3069afeSMatthias Ringwald return sco_packet_length; 6823b3aad8daSMatthias Ringwald } 6824b3aad8daSMatthias Ringwald 6825c4c88f1bSJakob Krantz /** 6826c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 6827c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 6828c4c88f1bSJakob Krantz */ 6829c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 6830c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 6831c4c88f1bSJakob Krantz } 6832c4c88f1bSJakob Krantz 6833c4c88f1bSJakob Krantz #endif 6834ec111c8bSMatthias Ringwald 6835ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 6836ec111c8bSMatthias Ringwald return hci_stack->state; 6837ec111c8bSMatthias Ringwald } 6838ec111c8bSMatthias Ringwald 683907e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 68405e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 684107e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 684207e010b6SMilanka Ringwald } 684307e010b6SMilanka Ringwald #endif 6844ec111c8bSMatthias Ringwald 6845d950d659SMatthias Ringwald /** 6846d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 6847d23838ecSMatthias Ringwald */ 6848c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 6849d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 6850d23838ecSMatthias Ringwald } 6851d23838ecSMatthias Ringwald 685271de195eSMatthias Ringwald void hci_disconnect_all(void){ 6853665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6854665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 6855665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6856665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 685704a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 685804a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 685904a6ef8cSmatthias.ringwald } 6860d31fba26S[email protected] hci_run(); 686104a6ef8cSmatthias.ringwald } 686233373e40SMatthias Ringwald 686333373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 686433373e40SMatthias Ringwald return hci_stack->manufacturer; 686533373e40SMatthias Ringwald } 68669c6e867eSMatthias Ringwald 68673e329ddfSandryblack #ifdef ENABLE_BLE 68689c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 68699c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 68709c6e867eSMatthias Ringwald if (!hci_con) return NULL; 68719c6e867eSMatthias Ringwald return &hci_con->sm_connection; 68729c6e867eSMatthias Ringwald } 68739c6e867eSMatthias Ringwald 68749c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 68759c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 68766a79f6baSMatthias Ringwald #endif 68779c6e867eSMatthias Ringwald 68786d105c71SMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 6879c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6880c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 6881c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 68826a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 6883c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 6884c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 68859c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 68869c6e867eSMatthias Ringwald } 68876a79f6baSMatthias Ringwald #endif 68886a79f6baSMatthias Ringwald } else { 6889c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 68908daf94bcSMatthias Ringwald if ((hci_connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED)){ 6891c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 6892c5fb5ca4SMatthias Ringwald } 6893c5fb5ca4SMatthias Ringwald #endif 68946a79f6baSMatthias Ringwald } 6895c5fb5ca4SMatthias Ringwald return 0; 6896c5fb5ca4SMatthias Ringwald } 68979c6e867eSMatthias Ringwald 6898ff3f1026SMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 6899c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6900ff3f1026SMatthias Ringwald if (hci_connection == NULL) return false; 69015f3981bfSMatthias Ringwald 6902c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 69036a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 69045f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 69055f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6906c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 6907ff3f1026SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated != 0; 69086a79f6baSMatthias Ringwald #endif 690959a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 69105f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6911f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6912ff3f1026SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 691359a1a47aSMatthias Ringwald #endif 69145f3981bfSMatthias Ringwald default: 6915ff3f1026SMatthias Ringwald return false; 69165f3981bfSMatthias Ringwald } 69179c6e867eSMatthias Ringwald } 69189c6e867eSMatthias Ringwald 6919f461b3dfSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 6920c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6921c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 69228b35e16aSMatthias Ringwald 6923c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 69246a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 69258b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 69268b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6927f461b3dfSMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return false; // unencrypted connection cannot be authenticated 6928f461b3dfSMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc != 0; 69296a79f6baSMatthias Ringwald #endif 693059a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 69318b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6932f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6933f461b3dfSMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 693459a1a47aSMatthias Ringwald #endif 69358b35e16aSMatthias Ringwald default: 6936f461b3dfSMatthias Ringwald return false; 69378b35e16aSMatthias Ringwald } 6938f1dfbe18SMatthias Ringwald } 6939f1dfbe18SMatthias Ringwald 69401e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 69411e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 69421e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 69431e122704SMatthias Ringwald 694448f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 69451e122704SMatthias Ringwald link_key_t link_key; 69461e122704SMatthias Ringwald link_key_type_t link_key_type; 694748f33f37SMatthias Ringwald #endif 69481e122704SMatthias Ringwald switch (hci_connection->address_type){ 69496a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 69501e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 69511e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 69521e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 69536a79f6baSMatthias Ringwald #endif 69541e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 69551e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 69561e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 69571e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 69581e122704SMatthias Ringwald #endif 69591e122704SMatthias Ringwald default: 69601e122704SMatthias Ringwald return false; 69611e122704SMatthias Ringwald } 69621e122704SMatthias Ringwald } 69631e122704SMatthias Ringwald 69646a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 69659c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 69669c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 69679c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 69689c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 69699c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 69709c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 69719c6e867eSMatthias Ringwald } 69729c6e867eSMatthias Ringwald #endif 6973f8ee3071SMatthias Ringwald 6974f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 6975f8ee3071SMatthias 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){ 6976f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6977f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6978f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 6979f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 6980f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 6981f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 6982f8ee3071SMatthias Ringwald hci_run(); 6983f8ee3071SMatthias Ringwald return 0; 6984f8ee3071SMatthias Ringwald } 6985f8ee3071SMatthias Ringwald 6986f8ee3071SMatthias Ringwald /** 6987f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 6988f8ee3071SMatthias Ringwald * @param con_handle 6989f8ee3071SMatthias Ringwald @ @return 0 if ok 6990f8ee3071SMatthias Ringwald */ 6991f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 6992f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6993f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6994f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 6995f8ee3071SMatthias Ringwald hci_run(); 6996f8ee3071SMatthias Ringwald return 0; 6997f8ee3071SMatthias Ringwald } 6998bea424a5SMatthias Ringwald 6999140c0557SMatthias 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){ 7000140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7001140c0557SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7002140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 7003140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 7004140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 7005140c0557SMatthias Ringwald hci_run(); 7006dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 7007140c0557SMatthias Ringwald } 7008140c0557SMatthias Ringwald 7009278ff8a9SMatthias 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){ 7010278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7011278ff8a9SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7012278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 7013278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 7014278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 7015278ff8a9SMatthias Ringwald conn->qos_latency = latency; 7016278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 7017278ff8a9SMatthias Ringwald hci_run(); 7018278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 7019278ff8a9SMatthias Ringwald } 7020278ff8a9SMatthias Ringwald 7021bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 7022bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 7023bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 7024baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 7025bea424a5SMatthias Ringwald hci_run(); 7026bea424a5SMatthias Ringwald } 7027bea424a5SMatthias Ringwald 7028bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 7029bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 7030baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_TYPE; 7031bea424a5SMatthias Ringwald hci_run(); 7032bea424a5SMatthias Ringwald } 7033bea424a5SMatthias Ringwald 703432a12730SMatthias Ringwald void gap_set_page_timeout(uint16_t page_timeout){ 703532a12730SMatthias Ringwald hci_stack->page_timeout = page_timeout; 7036baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_TIMEOUT; 703732a12730SMatthias Ringwald hci_run(); 703832a12730SMatthias Ringwald } 703932a12730SMatthias Ringwald 7040f8ee3071SMatthias Ringwald #endif 7041beceeddeSMatthias Ringwald 7042beceeddeSMatthias Ringwald void hci_halting_defer(void){ 7043beceeddeSMatthias Ringwald if (hci_stack->state != HCI_STATE_HALTING) return; 7044beceeddeSMatthias Ringwald switch (hci_stack->substate){ 7045beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 7046beceeddeSMatthias Ringwald case HCI_HALTING_CLOSE: 7047beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_TIMER; 7048beceeddeSMatthias Ringwald break; 7049beceeddeSMatthias Ringwald default: 7050beceeddeSMatthias Ringwald break; 7051beceeddeSMatthias Ringwald } 7052beceeddeSMatthias Ringwald } 7053eddac615SMatthias Ringwald 705421debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 705521debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 705621debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 705721debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 705821debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 705921debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 706002b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 706121debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 706202b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 706302b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 706402b02cffSMatthias Ringwald } 706502b02cffSMatthias Ringwald } 706602b02cffSMatthias Ringwald 706702b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 706802b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 706902b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 707002b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 707102b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 707202b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 707302b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 707402b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 707521debf25SMatthias Ringwald } 707621debf25SMatthias Ringwald } 7077cf38a091SMatthias Ringwald 7078cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 70791ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE) == false){ 7080cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 7081cf38a091SMatthias Ringwald } 7082cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 7083cf38a091SMatthias Ringwald // restart le resolving list update 7084cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 7085cf38a091SMatthias Ringwald } 7086cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 7087cf38a091SMatthias Ringwald } 708821debf25SMatthias Ringwald #endif 708921debf25SMatthias Ringwald 7090eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 709118976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 709218976c74SMatthias Ringwald hci_connection_t * conn; 709318976c74SMatthias Ringwald 709418976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 709518976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 709618976c74SMatthias Ringwald 70977d33cb26SMilanka Ringwald // setup Controller info 70987d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 70997d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 71007d33cb26SMilanka Ringwald 710118976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 710218976c74SMatthias Ringwald addr[5] = 0x01; 710318976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 710418976c74SMatthias Ringwald conn->con_handle = addr[5]; 710518976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 710618976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 71077d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 710818976c74SMatthias Ringwald 710918976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 711018976c74SMatthias Ringwald addr[5] = 0x02; 711118976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 711218976c74SMatthias Ringwald conn->con_handle = addr[5]; 711318976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 711418976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 71157d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 711618976c74SMatthias Ringwald 711718976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 711818976c74SMatthias Ringwald addr[5] = 0x03; 711918976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 712018976c74SMatthias Ringwald conn->con_handle = addr[5]; 712118976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 712218976c74SMatthias Ringwald conn->state = OPEN; 71237d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 712418976c74SMatthias Ringwald 712518976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 712618976c74SMatthias Ringwald addr[5] = 0x04; 712718976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 712818976c74SMatthias Ringwald conn->con_handle = addr[5]; 712918976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 713018976c74SMatthias Ringwald conn->state = OPEN; 71317d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 713218976c74SMatthias Ringwald 713318976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 713418976c74SMatthias Ringwald addr[5] = 0x05; 713518976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 713618976c74SMatthias Ringwald conn->con_handle = addr[5]; 713718976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 713818976c74SMatthias Ringwald conn->state = OPEN; 71397d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 71408046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 714118976c74SMatthias Ringwald } 714218976c74SMatthias Ringwald 7143eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 7144eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 7145eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 7146eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7147eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 7148eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 7149eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 7150eddac615SMatthias Ringwald } 7151eddac615SMatthias Ringwald } 71521470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 7153b6ffb67fSMatthias Ringwald hci_stack->le_scanning_param_update = false; 71541470db0cSMatthias Ringwald hci_init_done(); 71551470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 71561470db0cSMatthias Ringwald } 7157eddac615SMatthias Ringwald #endif 7158