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) \ 15663671e69SMatthias 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 121363671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 121463671e69SMatthias Ringwald static bool hci_extended_advertising_supported(void){ 121563671e69SMatthias Ringwald return hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE); 121663671e69SMatthias Ringwald } 121763671e69SMatthias Ringwald #endif 121863671e69SMatthias 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 1251a0698cb4SMatthias Ringwald uint16_t offset = 3; 1252a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset]; 1253d1dc057bS[email protected] offset += 1; 1254d1dc057bS[email protected] 1255a0698cb4SMatthias Ringwald uint16_t i; 125603fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1257a1df452eSMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 125833e6948bSMatthias Ringwald // sanity checks on data_length: 125933e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 126033e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 12614ea43905SMatthias Ringwald if ((offset + 9u + data_length + 1u) > size) return; 126233e6948bSMatthias Ringwald // setup event 12634ea43905SMatthias Ringwald uint8_t event_size = 10u + data_length; 1264a0698cb4SMatthias Ringwald uint16_t pos = 0; 1265045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 126657c9da5bS[email protected] event[pos++] = event_size; 12676535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address 1268d1dc057bS[email protected] offset += 8; 1269d1dc057bS[email protected] pos += 8; 1270d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 127133e6948bSMatthias Ringwald event[pos++] = data_length; 127233e6948bSMatthias Ringwald offset++; 12736535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], data_length); 127457c9da5bS[email protected] pos += data_length; 12754ea43905SMatthias Ringwald offset += data_length + 1u; // rssi 1276d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 127757c9da5bS[email protected] } 127857c9da5bS[email protected] } 1279a0698cb4SMatthias Ringwald 1280a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 1281a0698cb4SMatthias Ringwald void le_handle_extended_advertisement_report(uint8_t *packet, uint16_t size) { 1282a0698cb4SMatthias Ringwald uint16_t offset = 3; 1283a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset++]; 1284c95a0fc7SMatthias Ringwald uint8_t event[2 + 255]; // use upper bound to avoid var size automatic var 1285a0698cb4SMatthias Ringwald uint8_t i; 1286a0698cb4SMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 1287a0698cb4SMatthias Ringwald // sanity checks on data_length: 1288a0698cb4SMatthias Ringwald uint16_t data_length = packet[offset + 23]; 1289a0698cb4SMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 1290a0698cb4SMatthias Ringwald if ((offset + 24u + data_length) > size) return; 1291a0698cb4SMatthias Ringwald uint16_t event_type = little_endian_read_16(packet, offset); 1292a0698cb4SMatthias Ringwald offset += 2; 1293a0698cb4SMatthias Ringwald if ((event_type & 0x10) != 0) { 1294a0698cb4SMatthias Ringwald // setup legacy event 1295a0698cb4SMatthias Ringwald uint8_t legacy_event_type; 1296a0698cb4SMatthias Ringwald switch (event_type){ 1297a0698cb4SMatthias Ringwald case 0b0010011: 1298a0698cb4SMatthias Ringwald // ADV_IND 1299a0698cb4SMatthias Ringwald legacy_event_type = 0; 1300a0698cb4SMatthias Ringwald break; 1301a0698cb4SMatthias Ringwald case 0b0010101: 1302a0698cb4SMatthias Ringwald // ADV_DIRECT_IND 1303a0698cb4SMatthias Ringwald legacy_event_type = 1; 1304a0698cb4SMatthias Ringwald break; 1305a0698cb4SMatthias Ringwald case 0b0010010: 1306a0698cb4SMatthias Ringwald // ADV_SCAN_IND 1307a0698cb4SMatthias Ringwald legacy_event_type = 2; 1308a0698cb4SMatthias Ringwald break; 1309a0698cb4SMatthias Ringwald case 0b0010000: 1310a0698cb4SMatthias Ringwald // ADV_NONCONN_IND 1311a0698cb4SMatthias Ringwald legacy_event_type = 3; 1312a0698cb4SMatthias Ringwald break; 1313a0698cb4SMatthias Ringwald case 0b0011011: 1314a0698cb4SMatthias Ringwald case 0b0011010: 1315a0698cb4SMatthias Ringwald // SCAN_RSP 1316a0698cb4SMatthias Ringwald legacy_event_type = 4; 1317a0698cb4SMatthias Ringwald break; 1318a0698cb4SMatthias Ringwald default: 1319a0698cb4SMatthias Ringwald legacy_event_type = 0; 1320a0698cb4SMatthias Ringwald break; 1321a0698cb4SMatthias Ringwald } 1322a0698cb4SMatthias Ringwald uint16_t pos = 0; 1323a0698cb4SMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 1324a0698cb4SMatthias Ringwald event[pos++] = 10u + data_length; 1325a0698cb4SMatthias Ringwald event[pos++] = legacy_event_type; 1326a0698cb4SMatthias Ringwald // copy address type + address 1327a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + 6); 1328a0698cb4SMatthias Ringwald offset += 7; 1329a0698cb4SMatthias Ringwald pos += 7; 1330a0698cb4SMatthias Ringwald // skip primary_phy, secondary_phy, advertising_sid, tx_power 1331a0698cb4SMatthias Ringwald offset += 4; 1332a0698cb4SMatthias Ringwald // copy rssi 1333a0698cb4SMatthias Ringwald event[pos++] = packet[offset++]; 1334a0698cb4SMatthias Ringwald // skip periodic advertising interval and direct address 1335a0698cb4SMatthias Ringwald offset += 9; 1336a0698cb4SMatthias Ringwald // copy data len + data; 1337a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + data_length); 1338a0698cb4SMatthias Ringwald pos += 1 +data_length; 1339a0698cb4SMatthias Ringwald offset += 1+ data_length; 1340a0698cb4SMatthias Ringwald hci_emit_event(event, pos, 1); 1341a0698cb4SMatthias Ringwald } else { 1342c95a0fc7SMatthias Ringwald event[0] = GAP_EVENT_EXTENDED_ADVERTISING_REPORT; 1343c95a0fc7SMatthias Ringwald uint8_t report_len = 24 + data_length; 1344c95a0fc7SMatthias Ringwald event[1] = report_len; 1345c95a0fc7SMatthias Ringwald memcpy(&event[2], &packet[offset], report_len); 1346c95a0fc7SMatthias Ringwald offset += report_len; 1347c95a0fc7SMatthias Ringwald hci_emit_event(event, 2 + report_len, 1); 1348a0698cb4SMatthias Ringwald } 1349a0698cb4SMatthias Ringwald } 1350a0698cb4SMatthias Ringwald } 1351a0698cb4SMatthias Ringwald #endif 1352a0698cb4SMatthias Ringwald 1353b2f949feS[email protected] #endif 1354e8c8828eSMatthias Ringwald #endif 135557c9da5bS[email protected] 13562b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 13572b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1358bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1359bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_enabled){ 13602b6ab3e6SMatthias Ringwald // get number of active le slave connections 13612b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 13622b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 13632b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 13642b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 13652b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 13662b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 13672b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 13682b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 13692b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 13702b6ab3e6SMatthias Ringwald num_slave_connections++; 13712b6ab3e6SMatthias Ringwald } 13722b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1373bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1374bbc366e6SMatthias Ringwald } else { 1375bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 13762b6ab3e6SMatthias Ringwald } 13772b6ab3e6SMatthias Ringwald } 13782b6ab3e6SMatthias Ringwald #endif 13792b6ab3e6SMatthias Ringwald #endif 13802b6ab3e6SMatthias Ringwald 138159d59ecfSMatthias Ringwald #ifdef ENABLE_CLASSIC 138259d59ecfSMatthias Ringwald static void gap_run_set_local_name(void){ 138359d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 138459d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 138559d59ecfSMatthias Ringwald // construct HCI Command and send 138659d59ecfSMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 138759d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 138859d59ecfSMatthias Ringwald packet[0] = opcode & 0xff; 138959d59ecfSMatthias Ringwald packet[1] = opcode >> 8; 139059d59ecfSMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 139159d59ecfSMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 139259d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 139359d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 139459d59ecfSMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 139559d59ecfSMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 139659d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 139759d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 139859d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 139959d59ecfSMatthias Ringwald } 140059d59ecfSMatthias Ringwald 140159d59ecfSMatthias Ringwald static void gap_run_set_eir_data(void){ 140259d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 140359d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 140459d59ecfSMatthias Ringwald // construct HCI Command in-place and send 140559d59ecfSMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 140659d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 140759d59ecfSMatthias Ringwald uint16_t offset = 0; 140859d59ecfSMatthias Ringwald packet[offset++] = opcode & 0xff; 140959d59ecfSMatthias Ringwald packet[offset++] = opcode >> 8; 141059d59ecfSMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 141159d59ecfSMatthias Ringwald packet[offset++] = 0; // FEC not required 141259d59ecfSMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 141359d59ecfSMatthias Ringwald if (hci_stack->eir_data){ 141459d59ecfSMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 141559d59ecfSMatthias Ringwald ad_context_t context; 141659d59ecfSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 141759d59ecfSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 141859d59ecfSMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 141959d59ecfSMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 142059d59ecfSMatthias Ringwald // copy item 142159d59ecfSMatthias Ringwald packet[offset++] = size + 1; 142259d59ecfSMatthias Ringwald packet[offset++] = data_type; 142359d59ecfSMatthias Ringwald memcpy(&packet[offset], data, size); 142459d59ecfSMatthias Ringwald // update name item 142559d59ecfSMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 142659d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 142759d59ecfSMatthias Ringwald } 142859d59ecfSMatthias Ringwald offset += size; 142959d59ecfSMatthias Ringwald } 143059d59ecfSMatthias Ringwald } else { 143159d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 143259d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 143359d59ecfSMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 143459d59ecfSMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 143559d59ecfSMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 143659d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 143759d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 143859d59ecfSMatthias Ringwald } 143959d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 144059d59ecfSMatthias Ringwald } 1441ab4831a3SMatthias Ringwald 1442ab4831a3SMatthias Ringwald static void hci_run_gap_tasks_classic(void){ 1443baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_CLASS_OF_DEVICE) != 0) { 1444baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_CLASS_OF_DEVICE; 1445ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1446ab4831a3SMatthias Ringwald return; 1447ab4831a3SMatthias Ringwald } 1448baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_LOCAL_NAME) != 0) { 1449baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_LOCAL_NAME; 1450ab4831a3SMatthias Ringwald gap_run_set_local_name(); 1451ab4831a3SMatthias Ringwald return; 1452ab4831a3SMatthias Ringwald } 1453baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_EIR_DATA) != 0) { 1454baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_EIR_DATA; 1455ab4831a3SMatthias Ringwald gap_run_set_eir_data(); 1456ab4831a3SMatthias Ringwald return; 1457ab4831a3SMatthias Ringwald } 1458baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_DEFAULT_LINK_POLICY) != 0) { 1459baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_DEFAULT_LINK_POLICY; 1460ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1461ab4831a3SMatthias Ringwald return; 1462ab4831a3SMatthias Ringwald } 1463ab4831a3SMatthias Ringwald // write page scan activity 1464baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY) != 0) { 1465baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 1466ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 1467ab4831a3SMatthias Ringwald return; 1468ab4831a3SMatthias Ringwald } 1469ab4831a3SMatthias Ringwald // write page scan type 1470baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_TYPE) != 0) { 1471baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_TYPE; 1472ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 1473ab4831a3SMatthias Ringwald return; 1474ab4831a3SMatthias Ringwald } 147532a12730SMatthias Ringwald // write page timeout 1476baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_TIMEOUT) != 0) { 1477baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_TIMEOUT; 147832a12730SMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, hci_stack->page_timeout); 147932a12730SMatthias Ringwald return; 148032a12730SMatthias Ringwald } 1481ab4831a3SMatthias Ringwald // send scan enable 1482baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_SCAN_ENABLE) != 0) { 1483baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_SCAN_ENABLE; 1484ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 1485ab4831a3SMatthias Ringwald return; 1486ab4831a3SMatthias Ringwald } 1487c0c8a829SMatthias Ringwald // send write scan activity 1488baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY) != 0) { 1489baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 1490c0c8a829SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_scan_activity, hci_stack->inquiry_scan_interval, hci_stack->inquiry_scan_window); 1491c0c8a829SMatthias Ringwald return; 1492c0c8a829SMatthias Ringwald } 1493ab4831a3SMatthias Ringwald } 149459d59ecfSMatthias Ringwald #endif 149559d59ecfSMatthias Ringwald 14966fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 149706b9e820SMatthias Ringwald 149896b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 149996b53536SMatthias Ringwald if (!hci_stack->config) return 0; 15009796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 150196b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1502a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 150396b53536SMatthias Ringwald baud_rate = 3000000; 150496b53536SMatthias Ringwald } 150596b53536SMatthias Ringwald return baud_rate; 150696b53536SMatthias Ringwald } 150796b53536SMatthias Ringwald 1508ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 15099ec2630cSMatthias Ringwald UNUSED(ds); 15109ec2630cSMatthias Ringwald 15110305bdeaSMatthias Ringwald switch (hci_stack->substate){ 15120305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 15137b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 15140305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 15157b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 15160305bdeaSMatthias Ringwald hci_run(); 15170305bdeaSMatthias Ringwald break; 15189f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 15199f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 15209f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 15219f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 15229f007422SMatthias Ringwald } 1523cf373d3aSMatthias Ringwald 1524cf373d3aSMatthias Ringwald /* fall through */ 1525cf373d3aSMatthias Ringwald 1526e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1527e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1528e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1529e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1530e47e68c7SMatthias Ringwald hci_run(); 1531688c2635SMatthias Ringwald break; 15327224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 15337224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 153496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1535cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 15367dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 15377224be7eSMatthias Ringwald } 1538834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 153961f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1540834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1541834bce8cSMatthias Ringwald log_info("Link Reset"); 1542834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1543834bce8cSMatthias Ringwald } 1544772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1545772a36d3SMatthias Ringwald hci_run(); 1546772a36d3SMatthias Ringwald } 15474696bddbSMatthias Ringwald break; 1548559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1549559961d0SMatthias Ringwald // otherwise continue 1550559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1551559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1552559961d0SMatthias Ringwald break; 15530305bdeaSMatthias Ringwald default: 15540305bdeaSMatthias Ringwald break; 15550305bdeaSMatthias Ringwald } 15560305bdeaSMatthias Ringwald } 155706b9e820SMatthias Ringwald #endif 15580305bdeaSMatthias Ringwald 155971de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 156074b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 156174b323a9SMatthias Ringwald } 156274b323a9SMatthias Ringwald 1563f795c86eSMatthias Ringwald static void hci_init_done(void){ 1564f795c86eSMatthias Ringwald // done. tell the app 1565f795c86eSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1566f795c86eSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1567f795c86eSMatthias Ringwald hci_emit_state(); 1568f795c86eSMatthias Ringwald } 1569f795c86eSMatthias Ringwald 157074b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 157171de195eSMatthias Ringwald static void hci_initializing_run(void){ 1572148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 1573f4c579d4SMatthias Ringwald 15740d37aff3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 15750d37aff3SMatthias Ringwald 1576f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1577f9fd20c2SMatthias Ringwald bool need_baud_change = hci_stack->config 1578f4c579d4SMatthias Ringwald && hci_stack->chipset 1579f4c579d4SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1580f4c579d4SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 1581f4c579d4SMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1582f4c579d4SMatthias Ringwald #endif 1583f4c579d4SMatthias Ringwald 158474b323a9SMatthias Ringwald switch (hci_stack->substate){ 158574b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 158674b323a9SMatthias Ringwald hci_state_reset(); 1587a0cf2f3fSMatthias Ringwald 15886fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 15890305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1590659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1591528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1592528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1593a0cf2f3fSMatthias Ringwald #endif 15940305bdeaSMatthias Ringwald // send command 159574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 15960305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 159774b323a9SMatthias Ringwald break; 159876fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 159976fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 160076fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 160176fcb19bSMatthias Ringwald break; 1602e28e41c0SMatthias Ringwald 1603e28e41c0SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1604e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1605e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1606e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1607e90bae01SMatthias Ringwald break; 1608e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1609e47e68c7SMatthias Ringwald hci_state_reset(); 1610e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1611659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1612528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1613528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1614e47e68c7SMatthias Ringwald // send command 1615e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1616e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1617e47e68c7SMatthias Ringwald break; 16188d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 16198d29070eSMatthias Ringwald hci_state_reset(); 16208d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 16218d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 16228d29070eSMatthias Ringwald break; 1623f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 1624f4c579d4SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1625f4c579d4SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1626f4c579d4SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1627f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1628f4c579d4SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1629f4c579d4SMatthias Ringwald break; 1630f4c579d4SMatthias Ringwald } 1631c97af506SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 1632c97af506SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 1633c97af506SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1634c97af506SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1635c97af506SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 1636c97af506SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1637c97af506SMatthias Ringwald break; 1638f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: 1639f4c579d4SMatthias Ringwald if (need_baud_change) { 164096b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 16413fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1642f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 164374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 16444ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 16454696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 16464696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 164761f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1648659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1649528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 16504696bddbSMatthias Ringwald } 165174b323a9SMatthias Ringwald break; 1652fab26ab3SMatthias Ringwald } 1653f4c579d4SMatthias Ringwald 1654f4c579d4SMatthias Ringwald /* fall through */ 1655f4c579d4SMatthias Ringwald 165674b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 165774b323a9SMatthias Ringwald // Custom initialization 16583fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1659ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 16601979f09cSMatthias Ringwald bool send_cmd = false; 1661ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1662f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 16631979f09cSMatthias Ringwald send_cmd = true; 1664e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1665e47e68c7SMatthias Ringwald break; 1666f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 16671979f09cSMatthias Ringwald send_cmd = true; 1668f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1669e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1670659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1671528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1672528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1673a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1674772a36d3SMatthias Ringwald && hci_stack->config 16753fb36a29SMatthias Ringwald && hci_stack->chipset 16763fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1677772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 16782caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1679772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1680772a36d3SMatthias Ringwald } else { 16819f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1682772a36d3SMatthias Ringwald } 1683e47e68c7SMatthias Ringwald break; 1684f41911edSMatthias Ringwald default: 1685f41911edSMatthias Ringwald break; 1686e47e68c7SMatthias Ringwald } 1687ee720f3aSMatthias Ringwald 1688ee720f3aSMatthias Ringwald if (send_cmd){ 16894ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1690ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1691ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 16920305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 169374b323a9SMatthias Ringwald break; 169474b323a9SMatthias Ringwald } 1695148ca237SMatthias Ringwald log_info("Init script done"); 169692a0d36dSMatthias Ringwald 1697559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1698ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1699a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1700a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1701e021ff1eSMatthias Ringwald 1702559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 170392a0d36dSMatthias Ringwald if (need_baud_change) { 17049796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1705cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 170692a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 170792a0d36dSMatthias Ringwald } 1708559961d0SMatthias Ringwald 1709f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1710f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1711f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1712f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1713559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1714f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1715559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1716559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1717559961d0SMatthias Ringwald break; 171892a0d36dSMatthias Ringwald } 171974b323a9SMatthias Ringwald } 172006b9e820SMatthias Ringwald #endif 1721521bd5ffSMatthias Ringwald /* fall through */ 172206b9e820SMatthias Ringwald 172306b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 172406b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 172506b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 172606b9e820SMatthias Ringwald break; 172753860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 172853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 172953860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 173053860077SMatthias Ringwald break; 173174b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 17325ffe9d0bSMatthias Ringwald // only read buffer size if supported 17331ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE)){ 173474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 17350305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 173674b323a9SMatthias Ringwald break; 17375ffe9d0bSMatthias Ringwald } 1738521bd5ffSMatthias Ringwald 17395ffe9d0bSMatthias Ringwald /* fall through */ 17405ffe9d0bSMatthias Ringwald 174153860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 174253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 17430305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 174474b323a9SMatthias Ringwald break; 17452b838201SMatthias Ringwald 17462b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 17472b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 17482b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 17492b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 17502b838201SMatthias Ringwald break; 17512b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 17522b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 17532b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 17542b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 17552b838201SMatthias Ringwald break; 17562b838201SMatthias Ringwald #endif 17572b838201SMatthias Ringwald 175874b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 17590305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 176074b323a9SMatthias Ringwald if (hci_le_supported()){ 17615ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x3FFFFFFFU); 176274b323a9SMatthias Ringwald } else { 176374b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 17645ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x1FFFFFFFU); 176574b323a9SMatthias Ringwald } 176674b323a9SMatthias Ringwald break; 17672b838201SMatthias Ringwald 176835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 176974b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 1770f795c86eSMatthias Ringwald if (hci_classic_supported() && gap_ssp_supported()){ 177174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 17720305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 177374b323a9SMatthias Ringwald break; 1774e7c662faSMatthias Ringwald } 1775521bd5ffSMatthias Ringwald 1776e7c662faSMatthias Ringwald /* fall through */ 1777e7c662faSMatthias Ringwald 1778f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1779f795c86eSMatthias Ringwald if (hci_classic_supported()){ 1780f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 17818a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1782f6858d14SMatthias Ringwald break; 1783f795c86eSMatthias Ringwald } 1784521bd5ffSMatthias Ringwald 1785f795c86eSMatthias Ringwald /* fall through */ 1786f795c86eSMatthias Ringwald 17875d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 1788f3052906SMatthias Ringwald // skip write secure connections host support if not supported or disabled 17891ffa425cSMatthias Ringwald if (hci_classic_supported() && hci_stack->secure_connections_enable 17901ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST)) { 1791c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 17925d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 17931ffa425cSMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 17945d23aae8SMatthias Ringwald break; 1795f3052906SMatthias Ringwald } 1796521bd5ffSMatthias Ringwald 1797e4c6114dSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1798521bd5ffSMatthias Ringwald /* fall through */ 1799521bd5ffSMatthias Ringwald 1800483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1801729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1802e4c6114dSMatthias Ringwald // skip write synchronous flow control if not supported 18031ffa425cSMatthias Ringwald if (hci_classic_supported() 18041ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)) { 1805729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1806729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1807729ed62eSMatthias Ringwald break; 1808f795c86eSMatthias Ringwald } 1809f795c86eSMatthias Ringwald /* fall through */ 1810f795c86eSMatthias Ringwald 1811483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1812e4c6114dSMatthias Ringwald // skip write default erroneous data reporting if not supported 18131ffa425cSMatthias Ringwald if (hci_classic_supported() 18141ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING)) { 1815483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1816483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1817483c5078SMatthias Ringwald break; 1818f795c86eSMatthias Ringwald } 1819e4c6114dSMatthias Ringwald #endif 1820f795c86eSMatthias Ringwald 1821f795c86eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(ENABLE_SCO_OVER_PCM) 1822521bd5ffSMatthias Ringwald /* fall through */ 1823521bd5ffSMatthias Ringwald 18248051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 1825a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1826e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 1827a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 18288051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1829a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1830a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 18318051253fSMatthias Ringwald #endif 18328051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 18338051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 18341d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1835f795c86eSMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 16 kHz 18361d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 18371d2bbd54SMatthias Ringwald #else 18381d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 18391d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 18401d2bbd54SMatthias Ringwald #endif 18418051253fSMatthias Ringwald #endif 1842a42798c3SMatthias Ringwald break; 1843f795c86eSMatthias Ringwald } 1844f795c86eSMatthias Ringwald #endif 1845f795c86eSMatthias Ringwald 18464e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 1847521bd5ffSMatthias Ringwald /* fall through */ 1848521bd5ffSMatthias Ringwald 18494e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 1850e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 18514e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 18524e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 18531d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 18541d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 18551d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 18561d2bbd54SMatthias Ringwald #else 18571d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 18581d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 18591d2bbd54SMatthias Ringwald #endif 18604e821764SMatthias Ringwald break; 1861f795c86eSMatthias Ringwald } 186235454696SMatthias Ringwald #endif 18634e821764SMatthias Ringwald #endif 18644e821764SMatthias Ringwald 1865a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1866521bd5ffSMatthias Ringwald /* fall through */ 1867521bd5ffSMatthias Ringwald 186874b323a9SMatthias Ringwald // LE INIT 186974b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 1870f795c86eSMatthias Ringwald if (hci_le_supported()){ 187174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 18720305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 187374b323a9SMatthias Ringwald break; 1874f795c86eSMatthias Ringwald } 1875521bd5ffSMatthias Ringwald 1876f795c86eSMatthias Ringwald /* fall through */ 1877f795c86eSMatthias Ringwald 1878699a5fcaSMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 1879699a5fcaSMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 18801ffa425cSMatthias Ringwald if (hci_le_supported() 18811ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED)) { 1882699a5fcaSMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 1883699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 1884699a5fcaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 1885daabb8b8SMatthias Ringwald break; 1886f795c86eSMatthias Ringwald } 1887521bd5ffSMatthias Ringwald 1888f795c86eSMatthias Ringwald /* fall through */ 1889f795c86eSMatthias Ringwald 1890699a5fcaSMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 1891f795c86eSMatthias Ringwald if (hci_le_supported()){ 1892699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 189391d9e5d0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0xfffffdff, 0x07); // all events from core v5.3 without LE Enhanced Connection Complete 189474b323a9SMatthias Ringwald break; 1895f795c86eSMatthias Ringwald } 1896b435e062SMatthias Ringwald #endif 1897b435e062SMatthias Ringwald 1898b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1899521bd5ffSMatthias Ringwald /* fall through */ 1900521bd5ffSMatthias Ringwald 1901dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 19021ffa425cSMatthias Ringwald if (hci_le_supported() 19031ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH)) { 1904dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 1905dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 1906dcd678baSMatthias Ringwald break; 1907f795c86eSMatthias Ringwald } 1908521bd5ffSMatthias Ringwald 1909f795c86eSMatthias Ringwald /* fall through */ 1910f795c86eSMatthias Ringwald 1911dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 19121ffa425cSMatthias Ringwald if (hci_le_supported() 19131ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)) { 1914dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 1915b435e062SMatthias 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); 1916dcd678baSMatthias Ringwald break; 1917f795c86eSMatthias Ringwald } 1918b435e062SMatthias Ringwald #endif 1919b435e062SMatthias Ringwald 1920b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1921521bd5ffSMatthias Ringwald /* fall through */ 1922521bd5ffSMatthias Ringwald 19233b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 1924f795c86eSMatthias Ringwald if (hci_le_supported()){ 19253b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 19263b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 19273b6d4121SMatthias Ringwald break; 1928f795c86eSMatthias Ringwald } 1929521bd5ffSMatthias Ringwald 193074b323a9SMatthias Ringwald #endif 19314f982f31SMatthias Ringwald 1932521bd5ffSMatthias Ringwald /* fall through */ 1933521bd5ffSMatthias Ringwald 1934f795c86eSMatthias Ringwald case HCI_INIT_DONE: 19354f982f31SMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 193673799937SMatthias Ringwald // main init sequence complete 19374f982f31SMatthias Ringwald #ifdef ENABLE_CLASSIC 193873799937SMatthias Ringwald // check if initial Classic GAP Tasks are completed 1939baa4881eSMatthias Ringwald if (hci_classic_supported() && (hci_stack->gap_tasks_classic != 0)) { 19404f982f31SMatthias Ringwald hci_run_gap_tasks_classic(); 19414f982f31SMatthias Ringwald break; 19424f982f31SMatthias Ringwald } 19434f982f31SMatthias Ringwald #endif 194473799937SMatthias Ringwald #ifdef ENABLE_BLE 194573799937SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 194673799937SMatthias Ringwald // check if initial LE GAP Tasks are completed 194773799937SMatthias Ringwald if (hci_le_supported() && hci_stack->le_scanning_param_update) { 194873799937SMatthias Ringwald hci_run_general_gap_le(); 194973799937SMatthias Ringwald break; 195073799937SMatthias Ringwald } 195173799937SMatthias Ringwald #endif 195273799937SMatthias Ringwald #endif 1953f795c86eSMatthias Ringwald hci_init_done(); 1954f795c86eSMatthias Ringwald break; 1955f795c86eSMatthias Ringwald 195674b323a9SMatthias Ringwald default: 195774b323a9SMatthias Ringwald return; 195874b323a9SMatthias Ringwald } 195955975f88SMatthias Ringwald } 196055975f88SMatthias Ringwald 196107fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 196207fd2f31SMatthias Ringwald bool command_completed = false; 19630e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1964f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 19656155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 196607fd2f31SMatthias Ringwald command_completed = true; 1967148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 19686155b3d3S[email protected] } else { 1969d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 19706155b3d3S[email protected] } 19716155b3d3S[email protected] } 19720f97eae7SMatthias Ringwald 19730e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 19746155b3d3S[email protected] uint8_t status = packet[2]; 1975f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 19766155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 19776155b3d3S[email protected] if (status){ 197807fd2f31SMatthias Ringwald command_completed = true; 1979148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 19806155b3d3S[email protected] } else { 19816155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 19826155b3d3S[email protected] } 19836155b3d3S[email protected] } else { 1984148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 19856155b3d3S[email protected] } 19866155b3d3S[email protected] } 19876fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1988e47e68c7SMatthias Ringwald // Vendor == CSR 19890e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 1990e47e68c7SMatthias Ringwald // TODO: track actual command 199107fd2f31SMatthias Ringwald command_completed = true; 1992e47e68c7SMatthias Ringwald } 1993a2481739S[email protected] 19944e9daa6fSMatthias Ringwald // Vendor == Toshiba 19950e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 19964e9daa6fSMatthias Ringwald // TODO: track actual command 199707fd2f31SMatthias Ringwald command_completed = true; 1998004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 1999004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 20004e9daa6fSMatthias Ringwald } 200107fd2f31SMatthias Ringwald #endif 200207fd2f31SMatthias Ringwald 200307fd2f31SMatthias Ringwald return command_completed; 200407fd2f31SMatthias Ringwald } 200507fd2f31SMatthias Ringwald 200607fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 200707fd2f31SMatthias Ringwald 200807fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 200907fd2f31SMatthias Ringwald 201007fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 201107fd2f31SMatthias Ringwald 20126fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 20134e9daa6fSMatthias Ringwald 20140f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 20150f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 20160f97eae7SMatthias Ringwald // 20170f97eae7SMatthias Ringwald // HCI Reset 20180f97eae7SMatthias Ringwald // Timeout 100 ms 20190f97eae7SMatthias Ringwald // HCI Reset 20200f97eae7SMatthias Ringwald // Command Complete Reset 20210f97eae7SMatthias Ringwald // HCI Read Local Version Information 20220f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 20230f97eae7SMatthias Ringwald // hang... 20240f97eae7SMatthias Ringwald // 20250f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 20260f97eae7SMatthias Ringwald if (!command_completed 2027a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 20280e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 20290f97eae7SMatthias Ringwald 2030f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 20310f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 20320f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 20330f97eae7SMatthias Ringwald return; 20340f97eae7SMatthias Ringwald } 20350f97eae7SMatthias Ringwald } 20360f97eae7SMatthias Ringwald 20379f007422SMatthias Ringwald // CSR & H5 20389f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 20399f007422SMatthias Ringwald if (!command_completed 2040a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 20410e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 20429f007422SMatthias Ringwald 20439f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 20449f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 20459f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 20469f007422SMatthias Ringwald return; 20479f007422SMatthias Ringwald } 20489f007422SMatthias Ringwald } 20499f007422SMatthias Ringwald 20509f007422SMatthias 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 20519f007422SMatthias Ringwald // fix: Correct substate and behave as command below 20529f007422SMatthias Ringwald if (command_completed){ 20539f007422SMatthias Ringwald switch (hci_stack->substate){ 20549f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 20559f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 20569f007422SMatthias Ringwald break; 20579f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 20589f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 20599f007422SMatthias Ringwald break; 20609f007422SMatthias Ringwald default: 20619f007422SMatthias Ringwald break; 20629f007422SMatthias Ringwald } 20639f007422SMatthias Ringwald } 20640f97eae7SMatthias Ringwald 206506b9e820SMatthias Ringwald #endif 20660f97eae7SMatthias Ringwald 2067a2481739S[email protected] if (!command_completed) return; 2068a2481739S[email protected] 206907fd2f31SMatthias Ringwald bool need_baud_change = false; 207007fd2f31SMatthias Ringwald bool need_addr_change = false; 207106b9e820SMatthias Ringwald 20726fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 207306b9e820SMatthias Ringwald need_baud_change = hci_stack->config 20743fb36a29SMatthias Ringwald && hci_stack->chipset 20753fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 2076db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 20779796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 2078db8bc6ffSMatthias Ringwald 207906b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 20803fb36a29SMatthias Ringwald && hci_stack->chipset 20813fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 208206b9e820SMatthias Ringwald #endif 2083a80162e9SMatthias Ringwald 20845c363727SMatthias Ringwald switch(hci_stack->substate){ 208506b9e820SMatthias Ringwald 20866fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 20879f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 2088d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 20899f007422SMatthias Ringwald // fix: just correct substate and behave as command below 2090f4c579d4SMatthias Ringwald 2091f4c579d4SMatthias Ringwald /* fall through */ 2092f4c579d4SMatthias Ringwald #endif 2093f4c579d4SMatthias Ringwald 209474b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 2095528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2096f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 20972f48d920SMatthias Ringwald return; 2098f4c579d4SMatthias Ringwald 2099f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2100a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 21014696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 2102fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 210361f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 210496b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2105cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 2106fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 21074696bddbSMatthias Ringwald } 210874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 210974b323a9SMatthias Ringwald return; 2110a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 2111528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2112a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 2113a80162e9SMatthias Ringwald return; 211474b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 211574b323a9SMatthias Ringwald // repeat custom init 211674b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 211774b323a9SMatthias Ringwald return; 211806b9e820SMatthias Ringwald #endif 211906b9e820SMatthias Ringwald 2120a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 21210e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 2122efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 2123efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 2124eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 2125eb3a5314SMatthias Ringwald return; 2126eb3a5314SMatthias Ringwald } 212753860077SMatthias Ringwald if (need_addr_change){ 212853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 212953860077SMatthias Ringwald return; 213053860077SMatthias Ringwald } 213153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 213253860077SMatthias Ringwald return; 21336fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 21347224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 21357224be7eSMatthias Ringwald if (need_baud_change){ 213696b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2137cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 2138fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 21397224be7eSMatthias Ringwald } 2140eb3a5314SMatthias Ringwald if (need_addr_change){ 2141eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 2142eb3a5314SMatthias Ringwald return; 2143eb3a5314SMatthias Ringwald } 2144eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 2145eb3a5314SMatthias Ringwald return; 214653860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 21476ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 21486ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 21496ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 215053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 215153860077SMatthias Ringwald return; 215253860077SMatthias Ringwald } 215353860077SMatthias Ringwald // skipping st warm boot 215453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 215553860077SMatthias Ringwald return; 215653860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 215753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 215853860077SMatthias Ringwald return; 215906b9e820SMatthias Ringwald #endif 2160e7c662faSMatthias Ringwald 2161f795c86eSMatthias Ringwald case HCI_INIT_DONE: 2162eb8d95caSMatthias Ringwald // set state if we came here by fall through 2163eb8d95caSMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 2164f795c86eSMatthias Ringwald return; 2165a650ba4dSMatthias Ringwald 21666155b3d3S[email protected] default: 216774b323a9SMatthias Ringwald break; 21686155b3d3S[email protected] } 216955975f88SMatthias Ringwald hci_initializing_next_state(); 21706155b3d3S[email protected] } 21716155b3d3S[email protected] 21720bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 21733bb5ff27SMatthias Ringwald // CC2564C might emit Connection Complete for rejected incoming SCO connection 21743bb5ff27SMatthias Ringwald // To prevent accidentally free'ing the CHI connection for the ACL connection, 21753bb5ff27SMatthias Ringwald // check if the hci connection has been outgoing 21761f34df2cSMatthias Ringwald switch (conn->state){ 21771f34df2cSMatthias Ringwald case SEND_CREATE_CONNECTION: 21781f34df2cSMatthias Ringwald case RECEIVED_CONNECTION_REQUEST: 21791f34df2cSMatthias Ringwald break; 21801f34df2cSMatthias Ringwald default: 21811f34df2cSMatthias Ringwald return; 21821f34df2cSMatthias Ringwald } 21833bb5ff27SMatthias Ringwald 2184229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 21850bbba85bSMatthias Ringwald bd_addr_t bd_address; 21866535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 21870bbba85bSMatthias Ringwald 21886bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 21896bc9fa5eSMatthias Ringwald // cache needed data 21906bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 21916bc9fa5eSMatthias Ringwald #endif 21926bc9fa5eSMatthias Ringwald 21930bbba85bSMatthias Ringwald // connection failed, remove entry 21940bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 21950bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 21960bbba85bSMatthias Ringwald 21976bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 21980bbba85bSMatthias Ringwald // notify client if dedicated bonding 21990bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 22000bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 22010bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 22020bbba85bSMatthias Ringwald } 22030bbba85bSMatthias Ringwald 22040bbba85bSMatthias Ringwald // if authentication error, also delete link key 22050bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 22060bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 22070bbba85bSMatthias Ringwald } 22081c79b5e3SMatthias Ringwald #else 22091c79b5e3SMatthias Ringwald UNUSED(status); 22106bc9fa5eSMatthias Ringwald #endif 22110bbba85bSMatthias Ringwald } 22120bbba85bSMatthias Ringwald 2213be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 22142f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 22152f5c44baSMatthias Ringwald // SSP Controller 22162f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 22172f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 22182f5c44baSMatthias Ringwald } 22192f5c44baSMatthias Ringwald // eSCO 22202f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 22212f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 22222f5c44baSMatthias Ringwald } 22232f5c44baSMatthias Ringwald // Extended features 22242f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 22252f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 22262f5c44baSMatthias Ringwald } 22272f5c44baSMatthias Ringwald } 22282f5c44baSMatthias Ringwald 22292f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 22302f5c44baSMatthias Ringwald // SSP Host 22312f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 22322f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 22332f5c44baSMatthias Ringwald } 223450c51a77SMatthias Ringwald // SC Host 223550c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 223650c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 223750c51a77SMatthias Ringwald } 223850c51a77SMatthias Ringwald } 223950c51a77SMatthias Ringwald 224050c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 224150c51a77SMatthias Ringwald // SC Controller 224250c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 224350c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 224450c51a77SMatthias Ringwald } 22452f5c44baSMatthias Ringwald } 22462f5c44baSMatthias Ringwald 2247de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2248461a2bc4SMatthias Ringwald conn->bonding_flags &= ~BONDING_REMOTE_FEATURES_QUERY_ACTIVE; 2249de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 225049bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2251de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2252de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2253de0df013SMatthias Ringwald } 2254de0df013SMatthias Ringwald } 2255128825c3SMatthias Ringwald static bool hci_remote_sc_enabled(hci_connection_t * connection){ 2256128825c3SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 2257128825c3SMatthias Ringwald return (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 2258128825c3SMatthias Ringwald } 2259128825c3SMatthias Ringwald 2260be500194SMatthias Ringwald #endif 2261de0df013SMatthias Ringwald 226267c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 226367c6c9dcSMatthias Ringwald // handle BT initialization 226467c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 226567c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 226667c6c9dcSMatthias Ringwald } 226767c6c9dcSMatthias Ringwald 226867c6c9dcSMatthias Ringwald // help with BT sleep 226967c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 227067c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 2271905ee0fdSMatthias Ringwald && (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable))) { 227267c6c9dcSMatthias Ringwald hci_initializing_next_state(); 227367c6c9dcSMatthias Ringwald } 227467c6c9dcSMatthias Ringwald } 227567c6c9dcSMatthias Ringwald 22769866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 22779866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 22788daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 22799866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2280abdad579SMatthias Ringwald 22818daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) != 0) { 22821cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 22839866fdc7SMatthias Ringwald hci_emit_security_level(conn->con_handle, gap_security_level_for_connection(conn)); 2284abdad579SMatthias Ringwald return; 2285abdad579SMatthias Ringwald } 2286abdad579SMatthias Ringwald 2287d54424c3SMatthias Ringwald // Request remote features if not already done 2288dbe0d85cSMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 2289d54424c3SMatthias Ringwald 2290abdad579SMatthias Ringwald // Request Authentication if not already done 2291abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2292abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 22939866fdc7SMatthias Ringwald } 22949866fdc7SMatthias Ringwald #endif 22959866fdc7SMatthias Ringwald 22963b631737SMatthias Ringwald static void hci_store_local_supported_commands(const uint8_t * packet){ 229725e46151SMatthias Ringwald // create mapping table 229825e46151SMatthias Ringwald #define X(name, offset, bit) { offset, bit }, 229925e46151SMatthias Ringwald static struct { 230025e46151SMatthias Ringwald uint8_t byte_offset; 230125e46151SMatthias Ringwald uint8_t bit_position; 230225e46151SMatthias Ringwald } supported_hci_commands_map [] = { 230325e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 230425e46151SMatthias Ringwald }; 230525e46151SMatthias Ringwald #undef X 230625e46151SMatthias Ringwald 230725e46151SMatthias Ringwald // create names for debug purposes 230825e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 230925e46151SMatthias Ringwald #define X(name, offset, bit) #name, 231025e46151SMatthias Ringwald static const char * command_names[] = { 231125e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 231225e46151SMatthias Ringwald }; 231325e46151SMatthias Ringwald #undef X 231425e46151SMatthias Ringwald #endif 231525e46151SMatthias Ringwald 23161ffa425cSMatthias Ringwald hci_stack->local_supported_commands = 0; 231725e46151SMatthias Ringwald const uint8_t * commands_map = &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1]; 231825e46151SMatthias Ringwald uint16_t i; 231925e46151SMatthias Ringwald for (i = 0 ; i < SUPPORTED_HCI_COMMANDS_COUNT ; i++){ 232025e46151SMatthias Ringwald if ((commands_map[supported_hci_commands_map[i].byte_offset] & (1 << supported_hci_commands_map[i].bit_position)) != 0){ 232125e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 232225e46151SMatthias 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); 232325e46151SMatthias Ringwald #else 23241ffa425cSMatthias Ringwald log_info("Command 0x%02x supported %u/%u", i, supported_hci_commands_map[i].byte_offset, supported_hci_commands_map[i].bit_position); 232525e46151SMatthias Ringwald #endif 23261ffa425cSMatthias Ringwald hci_stack->local_supported_commands |= (1LU << i); 232725e46151SMatthias Ringwald } 232825e46151SMatthias Ringwald } 23291ffa425cSMatthias Ringwald log_info("Local supported commands summary %04x", hci_stack->local_supported_commands); 23303b631737SMatthias Ringwald } 23313b631737SMatthias Ringwald 23324f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2333b08371a9SMilanka Ringwald UNUSED(size); 2334e76a89eeS[email protected] 23359cbd2215SMatthias Ringwald uint16_t manufacturer; 23369cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2337fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 23381f7b95a1Smatthias.ringwald hci_connection_t * conn; 2339645b7c25SMatthias Ringwald uint8_t status; 23409cbd2215SMatthias Ringwald #endif 23416bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 23426bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 23437ec5eeaaSmatthias.ringwald 2344645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2345645b7c25SMatthias Ringwald switch (opcode){ 2346645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2347645b7c25SMatthias Ringwald if (packet[5]) break; 23489e263bd7SMatthias Ringwald // terminate, name 248 chars 23499e263bd7SMatthias Ringwald packet[6+248] = 0; 23509e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2351645b7c25SMatthias Ringwald break; 2352645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 23531d279b20Smatthias.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" 2354c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2355429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2356429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2357429122ccSMatthias Ringwald 2358429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2359429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2360429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2361429122ccSMatthias Ringwald 2362f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 2363f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 2364a8b12447S[email protected] 2365429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2366429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 23671a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2368c3b46f5aSMatthias Ringwald } 2369645b7c25SMatthias Ringwald break; 2370645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2371645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2372891b9fc2SMatthias Ringwald uint8_t event[5]; 2373891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2374891b9fc2SMatthias Ringwald event[1] = 3; 23756535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2376891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2377891b9fc2SMatthias Ringwald } 2378645b7c25SMatthias Ringwald break; 2379a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2380645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2381f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2382ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 23836c26b087S[email protected] // determine usable ACL payload size 23846c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 23856c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 23866c26b087S[email protected] } 23879da54300S[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); 2388645b7c25SMatthias Ringwald break; 2389b435e062SMatthias Ringwald #endif 2390b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2391645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2392dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2393dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2394dcd678baSMatthias 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); 2395645b7c25SMatthias Ringwald break; 2396b435e062SMatthias Ringwald #endif 2397d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2398645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2399e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 240015d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2401645b7c25SMatthias Ringwald break; 240265a46ef3S[email protected] #endif 2403645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2404645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2405645b7c25SMatthias 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)); 240633373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 24071624665aSMatthias Ringwald if (hci_stack->link_key_db){ 24081624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 24091624665aSMatthias Ringwald } 241033373e40SMatthias Ringwald #endif 2411645b7c25SMatthias Ringwald break; 241235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2413645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 24143a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2415645b7c25SMatthias Ringwald break; 2416645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 2417f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2418f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2419f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2420f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2421f5875de5SMatthias Ringwald } 2422645b7c25SMatthias Ringwald break; 242335454696SMatthias Ringwald #endif 2424645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 24254f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 242665389bfcS[email protected] 242735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2428a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2429a5a23fc2S[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]); 24308b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 243135454696SMatthias Ringwald #endif 2432f5d8d141S[email protected] // Classic/LE 2433f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2434645b7c25SMatthias Ringwald break; 2435645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2436645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2437c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2438c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2439c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2440c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2441c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2442c21d89f3SMatthias Ringwald } 2443c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 24444696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2445645b7c25SMatthias Ringwald break; 2446645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 24473b631737SMatthias Ringwald hci_store_local_supported_commands(packet); 2448645b7c25SMatthias Ringwald break; 2449e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2450645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 24514f781026SMatthias Ringwald if (packet[5]) return; 24525b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2453645b7c25SMatthias Ringwald break; 2454645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2455645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2456573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2457573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2458c3b46f5aSMatthias Ringwald if (conn != NULL) { 24599866fdc7SMatthias Ringwald uint8_t key_size = 0; 2460573897a0SMatthias Ringwald if (status == 0){ 24619866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 24624a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2463170fafaeSMatthias Ringwald } else { 2464e9f98c4aSMatthias Ringwald key_size = 1; 24659866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2466573897a0SMatthias Ringwald } 24679866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2468c3b46f5aSMatthias Ringwald } 2469645b7c25SMatthias Ringwald break; 2470cc15bb2cSMatthias Ringwald // assert pairing complete event is emitted. 2471cc15bb2cSMatthias Ringwald // note: for SSP, Simple Pairing Complete Event is sufficient, but we want to be more robust 2472cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_NEGATIVE_REPLY: 2473cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_NEGATIVE_REPLY: 2474cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: 24758cc1507eSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 2476cc15bb2cSMatthias Ringwald // lookup connection by gap pairing addr 2477cc15bb2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->gap_pairing_addr, BD_ADDR_TYPE_ACL); 2478cc15bb2cSMatthias Ringwald if (conn == NULL) break; 2479cc15bb2cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 2480cc15bb2cSMatthias Ringwald break; 2481cc15bb2cSMatthias Ringwald 248275a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 248375a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 248475a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 248575a8e4faSMatthias Ringwald uint8_t event[67]; 248675a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 248775a8e4faSMatthias Ringwald event[1] = 65; 248875a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 248975a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 249075a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 249175a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 249275a8e4faSMatthias Ringwald event[2] = 3; 249375a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 249475a8e4faSMatthias Ringwald } else { 249575a8e4faSMatthias Ringwald event[2] = 1; 249675a8e4faSMatthias Ringwald } 249775a8e4faSMatthias Ringwald } 249875a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 249975a8e4faSMatthias Ringwald break; 250075a8e4faSMatthias Ringwald } 25011ae74bf3SMatthias Ringwald 25021ae74bf3SMatthias Ringwald // note: only needed if user does not provide OOB data 25031ae74bf3SMatthias Ringwald case HCI_OPCODE_HCI_REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: 25041ae74bf3SMatthias Ringwald conn = hci_connection_for_handle(hci_stack->classic_oob_con_handle); 25051ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 25061ae74bf3SMatthias Ringwald if (conn == NULL) break; 25071ae74bf3SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 25081ae74bf3SMatthias Ringwald break; 2509e8c8828eSMatthias Ringwald #endif 251075a8e4faSMatthias Ringwald #endif 25116f35bb46SMatthias Ringwald default: 25126f35bb46SMatthias Ringwald break; 25134f781026SMatthias Ringwald } 2514645b7c25SMatthias Ringwald } 25154f781026SMatthias Ringwald 25160ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 25170ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 25180ce3f217SMatthias Ringwald bd_addr_t addr; 25190ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 25200ce3f217SMatthias Ringwald hci_connection_t * conn; 25210ce3f217SMatthias Ringwald 25220ce3f217SMatthias Ringwald // Connection management 25230ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 25240ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 25250ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 25260ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 25270ce3f217SMatthias Ringwald 25280ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 25290ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 25300ce3f217SMatthias Ringwald if (packet[3]){ 25310ce3f217SMatthias Ringwald 25320ce3f217SMatthias Ringwald // handle cancelled outgoing connection 25330ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 25340ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 25350ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 25360ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 25371f468175SMatthias Ringwald // reset state 25380ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 25391f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 25400ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 25410ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 25420ce3f217SMatthias Ringwald } 25430ce3f217SMatthias Ringwald 25440ce3f217SMatthias Ringwald // outgoing le connection establishment is done 25450ce3f217SMatthias Ringwald if (conn){ 25460ce3f217SMatthias Ringwald // remove entry 25470ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 25480ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 25490ce3f217SMatthias Ringwald } 25500ce3f217SMatthias Ringwald return; 25510ce3f217SMatthias Ringwald } 25520ce3f217SMatthias Ringwald #endif 25530ce3f217SMatthias Ringwald 25540ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 25550ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 25560ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 25570ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 25580ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 25590ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 25600ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 25610ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 25620ce3f217SMatthias Ringwald } 25630ce3f217SMatthias Ringwald #endif 25640ce3f217SMatthias Ringwald } else { 25650ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 25660ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 25670ce3f217SMatthias Ringwald hci_stack->le_advertisements_active = false; 25680ce3f217SMatthias Ringwald #endif 25690ce3f217SMatthias Ringwald } 25700ce3f217SMatthias Ringwald 25710ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 25720ce3f217SMatthias Ringwald if (!conn){ 25730ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 25740ce3f217SMatthias Ringwald } 25750ce3f217SMatthias Ringwald 25760ce3f217SMatthias Ringwald // no memory, sorry. 25770ce3f217SMatthias Ringwald if (!conn){ 25780ce3f217SMatthias Ringwald return; 25790ce3f217SMatthias Ringwald } 25800ce3f217SMatthias Ringwald 25810ce3f217SMatthias Ringwald conn->state = OPEN; 25820ce3f217SMatthias Ringwald conn->role = packet[6]; 25830ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 25840ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 25850ce3f217SMatthias Ringwald 25860ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 25870ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 25880ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 25890ce3f217SMatthias Ringwald } 25900ce3f217SMatthias Ringwald #endif 25910ce3f217SMatthias Ringwald 2592dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 2593dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 2594dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 2595dde9ff1eSMatthias Ringwald 25960ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 25970ce3f217SMatthias Ringwald 25980ce3f217SMatthias Ringwald // restart timer 25990ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 26000ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 26010ce3f217SMatthias Ringwald 26020ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 26030ce3f217SMatthias Ringwald 26040ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 26050ce3f217SMatthias Ringwald } 26060ce3f217SMatthias Ringwald #endif 26070ce3f217SMatthias Ringwald 260817c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 260917c6fe5cSMatthias 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){ 261017c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 261117c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 26129a8f78c1SMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities if Authenticated is possible 26139a8f78c1SMatthias Ringwald // @see: Core Spec v5.3, Vol 3, Part C, Table 5.7 261417c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 26159a8f78c1SMatthias Ringwald // MITM not possible without keyboard or display 261617c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 261717c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 26189a8f78c1SMatthias Ringwald 26199a8f78c1SMatthias Ringwald // MITM possible if one side has keyboard and the other has keyboard or display 26209a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 26219a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 26229a8f78c1SMatthias Ringwald 26239a8f78c1SMatthias Ringwald // MITM not possible if one side has only display and other side has no keyboard 26249a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 26259a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 262617c6fe5cSMatthias Ringwald } 262717c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 262817c6fe5cSMatthias Ringwald return true; 262917c6fe5cSMatthias Ringwald } 26303817f9dfSMatthias Ringwald 26313817f9dfSMatthias Ringwald static bool btstack_is_null(uint8_t * data, uint16_t size){ 26323817f9dfSMatthias Ringwald uint16_t i; 26333817f9dfSMatthias Ringwald for (i=0; i < size ; i++){ 26343817f9dfSMatthias Ringwald if (data[i] != 0) { 26353817f9dfSMatthias Ringwald return false; 26363817f9dfSMatthias Ringwald } 26373817f9dfSMatthias Ringwald } 26383817f9dfSMatthias Ringwald return true; 26393817f9dfSMatthias Ringwald } 26403817f9dfSMatthias Ringwald 2641b3c4163bSMatthias Ringwald static void hci_ssp_assess_security_on_io_cap_request(hci_connection_t * conn){ 26422dd8985bSMatthias Ringwald // get requested security level 26432dd8985bSMatthias Ringwald gap_security_level_t requested_security_level = conn->requested_security_level; 26442dd8985bSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 26452dd8985bSMatthias Ringwald requested_security_level = LEVEL_4; 26462dd8985bSMatthias Ringwald } 26472dd8985bSMatthias Ringwald 2648b3c4163bSMatthias Ringwald // assess security: LEVEL 4 requires SC 26492dd8985bSMatthias Ringwald // skip this preliminary test if remote features are not available yet to work around potential issue in ESP32 controller 26502dd8985bSMatthias Ringwald if ((requested_security_level == LEVEL_4) && 26512dd8985bSMatthias Ringwald ((conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0) && 26522dd8985bSMatthias Ringwald !hci_remote_sc_enabled(conn)){ 2653b3c4163bSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 2654b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2655b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2656b3c4163bSMatthias Ringwald return; 2657b3c4163bSMatthias Ringwald } 2658b3c4163bSMatthias Ringwald 2659b3c4163bSMatthias Ringwald // assess security based on io capabilities 2660b3c4163bSMatthias Ringwald if (conn->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 2661b3c4163bSMatthias Ringwald // responder: fully validate io caps of both sides as well as OOB data 2662b3c4163bSMatthias Ringwald bool security_possible = false; 2663b3c4163bSMatthias Ringwald security_possible = hci_ssp_security_level_possible_for_io_cap(requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io); 2664b3c4163bSMatthias Ringwald 2665b3c4163bSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 2666b3c4163bSMatthias Ringwald // We assume that both Controller can reach LEVEL 4, if one side has received P-192 and the other has received P-256, 2667b3c4163bSMatthias Ringwald // so we merge the OOB data availability 2668b3c4163bSMatthias Ringwald uint8_t have_oob_data = conn->io_cap_response_oob_data; 2669b3c4163bSMatthias Ringwald if (conn->classic_oob_c_192 != NULL){ 2670b3c4163bSMatthias Ringwald have_oob_data |= 1; 2671b3c4163bSMatthias Ringwald } 2672b3c4163bSMatthias Ringwald if (conn->classic_oob_c_256 != NULL){ 2673b3c4163bSMatthias Ringwald have_oob_data |= 2; 2674b3c4163bSMatthias Ringwald } 2675b3c4163bSMatthias Ringwald // for up to Level 3, either P-192 as well as P-256 will do 2676b3c4163bSMatthias 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 2677b3c4163bSMatthias Ringwald // if remote does not SC, we should not receive P-256 data either 2678b3c4163bSMatthias Ringwald if ((requested_security_level <= LEVEL_3) && (have_oob_data != 0)){ 2679b3c4163bSMatthias Ringwald security_possible = true; 2680b3c4163bSMatthias Ringwald } 2681b3c4163bSMatthias Ringwald // for Level 4, P-256 is needed 2682b3c4163bSMatthias Ringwald if ((requested_security_level == LEVEL_4 && ((have_oob_data & 2) != 0))){ 2683b3c4163bSMatthias Ringwald security_possible = true; 2684b3c4163bSMatthias Ringwald } 2685b3c4163bSMatthias Ringwald #endif 2686b3c4163bSMatthias Ringwald 2687b3c4163bSMatthias Ringwald if (security_possible == false){ 26882dd8985bSMatthias Ringwald log_info("IOCap/OOB insufficient for level %u -> abort", requested_security_level); 2689b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2690b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2691b3c4163bSMatthias Ringwald return; 2692b3c4163bSMatthias Ringwald } 2693b3c4163bSMatthias Ringwald } else { 2694b3c4163bSMatthias Ringwald // initiator: remote io cap not yet, only check if we have ability for MITM protection if requested and OOB is not supported 26951fe968f5SMatthias Ringwald #ifndef ENABLE_CLASSIC_PAIRING_OOB 26961fe968f5SMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 2697b3c4163bSMatthias Ringwald if ((conn->requested_security_level >= LEVEL_3) && (hci_stack->ssp_io_capability >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT)){ 2698b3c4163bSMatthias Ringwald log_info("Level 3+ required, but no input/output -> abort"); 2699b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2700b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2701b3c4163bSMatthias Ringwald return; 2702b3c4163bSMatthias Ringwald } 2703b3c4163bSMatthias Ringwald #endif 27041fe968f5SMatthias Ringwald #endif 2705b3c4163bSMatthias Ringwald } 2706b3c4163bSMatthias Ringwald 2707b3c4163bSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 2708b3c4163bSMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 2709b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 2710b3c4163bSMatthias Ringwald } else { 2711b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2712b3c4163bSMatthias Ringwald } 2713b3c4163bSMatthias Ringwald #endif 2714b3c4163bSMatthias Ringwald } 2715b3c4163bSMatthias Ringwald 271617c6fe5cSMatthias Ringwald #endif 271717c6fe5cSMatthias Ringwald 2718c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 27194f781026SMatthias Ringwald 27204f781026SMatthias Ringwald uint16_t event_length = packet[1]; 27214f781026SMatthias Ringwald 27224f781026SMatthias Ringwald // assert packet is complete 27234ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 27244f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 27254f781026SMatthias Ringwald return; 27264f781026SMatthias Ringwald } 27274f781026SMatthias Ringwald 27284f781026SMatthias Ringwald bd_addr_type_t addr_type; 27294f781026SMatthias Ringwald hci_con_handle_t handle; 27304f781026SMatthias Ringwald hci_connection_t * conn; 27314f781026SMatthias Ringwald int i; 27324f781026SMatthias Ringwald int create_connection_cmd; 27334f781026SMatthias Ringwald 27344f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 27355e91d96cSMatthias Ringwald hci_link_type_t link_type; 273648f33f37SMatthias Ringwald bd_addr_t addr; 27374f781026SMatthias Ringwald #endif 27384f781026SMatthias Ringwald 27394f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 27404f781026SMatthias Ringwald 27414f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 27424f781026SMatthias Ringwald 27434f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 27444f781026SMatthias Ringwald handle_command_complete_event(packet, size); 274556cf178bSmatthias.ringwald break; 274656cf178bSmatthias.ringwald 27477ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 27486bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 27496bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2750229331c6SMatthias Ringwald 2751229331c6SMatthias Ringwald // check command status to detected failed outgoing connections 2752c57fa566SMatthias Ringwald create_connection_cmd = 0; 2753c57fa566SMatthias Ringwald #ifdef ENABLE_CLASSIC 2754c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){ 2755c57fa566SMatthias Ringwald create_connection_cmd = 1; 2756c57fa566SMatthias Ringwald } 27571f34df2cSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_accept_synchronous_connection)){ 27581f34df2cSMatthias Ringwald create_connection_cmd = 1; 27591f34df2cSMatthias Ringwald } 2760c57fa566SMatthias Ringwald #endif 2761c57fa566SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2762c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_le_create_connection)){ 2763c57fa566SMatthias Ringwald create_connection_cmd = 1; 2764c57fa566SMatthias Ringwald } 2765c57fa566SMatthias Ringwald #endif 2766c57fa566SMatthias Ringwald if (create_connection_cmd) { 2767229331c6SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 27688da98cbeSMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 27696ea9315cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 27706ea9315cSMatthias 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); 2771229331c6SMatthias Ringwald 2772229331c6SMatthias Ringwald // reset outgoing address info 2773229331c6SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2774229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2775229331c6SMatthias Ringwald 27766ea9315cSMatthias Ringwald // on error 27776ea9315cSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2778f75e06adSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 27796ea9315cSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 27806ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2781f7e6a692SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 27826ea9315cSMatthias Ringwald } 2783f75e06adSMatthias Ringwald #endif 2784229331c6SMatthias Ringwald // error => outgoing connection failed 27856ea9315cSMatthias Ringwald if (conn != NULL){ 2786229331c6SMatthias Ringwald hci_handle_connection_failed(conn, status); 2787229331c6SMatthias Ringwald } 2788229331c6SMatthias Ringwald } 27896ea9315cSMatthias Ringwald } 2790beb3c81dSMatthias Ringwald 2791beb3c81dSMatthias Ringwald #ifdef ENABLE_CLASSIC 2792beb3c81dSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_inquiry)) { 2793beb3c81dSMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2794beb3c81dSMatthias Ringwald log_info("command status (inquiry), status %x", status); 2795beb3c81dSMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 2796beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2797beb3c81dSMatthias Ringwald } else { 2798beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2799beb3c81dSMatthias Ringwald } 2800beb3c81dSMatthias Ringwald } 2801beb3c81dSMatthias Ringwald #endif 28027ec5eeaaSmatthias.ringwald break; 28037ec5eeaaSmatthias.ringwald 28042e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 28059784dac1SMatthias Ringwald if (size < 3) return; 28069784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 28074ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 28089784dac1SMatthias Ringwald uint16_t offset = 3; 28099784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 28104ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 28114ea43905SMatthias Ringwald offset += 2u; 2812f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 28134ea43905SMatthias Ringwald offset += 2u; 28142e440c8aS[email protected] 28155061f3afS[email protected] conn = hci_connection_for_handle(handle); 281656cf178bSmatthias.ringwald if (!conn){ 28179da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 281856cf178bSmatthias.ringwald continue; 281956cf178bSmatthias.ringwald } 282023bed257S[email protected] 2821ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets){ 2822ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 2823e35edcc1S[email protected] } else { 2824ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 2825ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 2826e35edcc1S[email protected] } 2827ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 2828760b20efSMatthias Ringwald 2829760b20efSMatthias Ringwald #ifdef ENABLE_CLASSIC 2830760b20efSMatthias Ringwald // For SCO, we do the can_send_now_check here 2831760b20efSMatthias Ringwald hci_notify_if_sco_can_send_now(); 2832760b20efSMatthias Ringwald #endif 283356cf178bSmatthias.ringwald } 28346772a24cSmatthias.ringwald break; 28352e440c8aS[email protected] } 283635454696SMatthias Ringwald 283735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2838f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 2839f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 2840f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2841f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2842f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2843f5875de5SMatthias Ringwald } 2844f5875de5SMatthias Ringwald break; 2845b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 2846b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 2847b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 2848b7f1ee76SMatthias Ringwald } 2849b7f1ee76SMatthias Ringwald break; 28501f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 2851724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 28525e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 285372cf8859SMatthias Ringwald 285472cf8859SMatthias Ringwald // CVE-2020-26555: reject incoming connection from device with same BD ADDR 285572cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0){ 285672cf8859SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 285772cf8859SMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 285872cf8859SMatthias Ringwald break; 285972cf8859SMatthias Ringwald } 286072cf8859SMatthias Ringwald 286107e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 28625e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 28634536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 286407e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 286507e010b6SMilanka Ringwald break; 286607e010b6SMilanka Ringwald } 286707e010b6SMilanka Ringwald } 286807e010b6SMilanka Ringwald 286937eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 28705e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 28715e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 28722e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 28731f7b95a1Smatthias.ringwald if (!conn) { 28745293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 28751f7b95a1Smatthias.ringwald } 2876ce4c8fabSmatthias.ringwald if (!conn) { 2877ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 28784536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 2879058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 2880f23819bcSMatthias Ringwald hci_run(); 2881f23819bcSMatthias Ringwald // avoid event to higher layer 2882f23819bcSMatthias Ringwald return; 2883ce4c8fabSmatthias.ringwald } 28845cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 288532ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 2886f3a16b9aSMatthias Ringwald // store info about eSCO 28875e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 288876ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 2889f3a16b9aSMatthias Ringwald } 289032ab9390Smatthias.ringwald hci_run(); 28911f7b95a1Smatthias.ringwald break; 28921f7b95a1Smatthias.ringwald 28936772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 2894fe1ed1b8Smatthias.ringwald // Connection management 2895724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 28969da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 2897f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 28982e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2899fe1ed1b8Smatthias.ringwald if (conn) { 2900b448a0e7Smatthias.ringwald if (!packet[2]){ 2901c8e4258aSmatthias.ringwald conn->state = OPEN; 2902f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 29036909f064SMatthias Ringwald 2904e6c51921SMatthias Ringwald // queue set supervision timeout if we're master 2905d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 29068daf94bcSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT); 29076909f064SMatthias Ringwald } 29086909f064SMatthias Ringwald 2909c785ef68Smatthias.ringwald // restart timer 2910528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2911528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 2912c785ef68Smatthias.ringwald 291318c4a0a3SMatthias Ringwald // trigger remote features for dedicated bonding 291418c4a0a3SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 291518c4a0a3SMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 291618c4a0a3SMatthias Ringwald } 291718c4a0a3SMatthias Ringwald 29189da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 291943bfb1bdSmatthias.ringwald 292043bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 2921b448a0e7Smatthias.ringwald } else { 29220bbba85bSMatthias Ringwald // connection failed 29230bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 2924fe1ed1b8Smatthias.ringwald } 2925fe1ed1b8Smatthias.ringwald } 29266772a24cSmatthias.ringwald break; 2927fe1ed1b8Smatthias.ringwald 292844d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 2929724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 29301f34df2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 29311f34df2cSMatthias Ringwald log_info("Synchronous Connection Complete for %p (status=%u) %s", conn, packet[2], bd_addr_to_str(addr)); 29321a06f663S[email protected] if (packet[2]){ 293344d0e3d5S[email protected] // connection failed 29341f34df2cSMatthias Ringwald if (conn){ 29351f34df2cSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 29361f34df2cSMatthias Ringwald } 293744d0e3d5S[email protected] break; 293844d0e3d5S[email protected] } 2939e35edcc1S[email protected] if (!conn) { 2940e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2941e35edcc1S[email protected] } 2942e35edcc1S[email protected] if (!conn) { 2943e35edcc1S[email protected] break; 2944e35edcc1S[email protected] } 29451a06f663S[email protected] conn->state = OPEN; 2946f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2947ee752bb8SMatthias Ringwald 2948ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2949ee752bb8SMatthias Ringwald // update SCO 2950ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 2951ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 2952ee752bb8SMatthias Ringwald } 2953f234b250SMatthias Ringwald // trigger can send now 2954f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 29551972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 2956f234b250SMatthias Ringwald } 2957ee752bb8SMatthias Ringwald #endif 2958cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 2959cb70c5abSMatthias Ringwald // configure sco transport 2960cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 2961cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 2962cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 2963cb70c5abSMatthias Ringwald } 2964cb70c5abSMatthias Ringwald #endif 296544d0e3d5S[email protected] break; 296644d0e3d5S[email protected] 2967afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 2968f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2969afd4e962S[email protected] conn = hci_connection_for_handle(handle); 2970afd4e962S[email protected] if (!conn) break; 2971afd4e962S[email protected] if (!packet[2]){ 29722f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 29732f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 29742f5c44baSMatthias Ringwald 29755ccef624SMatthias Ringwald // read extended features if possible 29761ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES) 29771ffa425cSMatthias Ringwald && ((conn->remote_supported_features[0] & 2) != 0)) { 29785ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 29795ccef624SMatthias Ringwald break; 29805ccef624SMatthias Ringwald } 29815ccef624SMatthias Ringwald } 29825ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 29835ccef624SMatthias Ringwald break; 29845ccef624SMatthias Ringwald 29855ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 29865ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 29875ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 29885ccef624SMatthias Ringwald if (!conn) break; 29895ccef624SMatthias Ringwald // status = ok, page = 1 299050c51a77SMatthias Ringwald if (!packet[2]) { 299150c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 299250c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 29935ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 299450c51a77SMatthias Ringwald bool done = false; 299550c51a77SMatthias Ringwald switch (page_number){ 299650c51a77SMatthias Ringwald case 1: 29972f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 299850c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 299950c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 300050c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 300150c51a77SMatthias Ringwald } else { 300250c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 300350c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 300450c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 300550c51a77SMatthias Ringwald } 300650c51a77SMatthias Ringwald done = true; 300750c51a77SMatthias Ringwald } 300850c51a77SMatthias Ringwald break; 300950c51a77SMatthias Ringwald case 2: 301050c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 301150c51a77SMatthias Ringwald done = true; 301250c51a77SMatthias Ringwald break; 301350c51a77SMatthias Ringwald default: 301450c51a77SMatthias Ringwald break; 301550c51a77SMatthias Ringwald } 301650c51a77SMatthias Ringwald if (!done) break; 3017afd4e962S[email protected] } 3018de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 3019afd4e962S[email protected] break; 3020afd4e962S[email protected] 30217fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 3022308eeaffSMatthias Ringwald #ifndef ENABLE_EXPLICIT_LINK_KEY_REPLY 302363c0b2fdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 302463c0b2fdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 302563c0b2fdSMatthias Ringwald if (!conn) break; 302663c0b2fdSMatthias Ringwald 302763c0b2fdSMatthias Ringwald // lookup link key in db if not cached 302863c0b2fdSMatthias Ringwald if ((conn->link_key_type == INVALID_LINK_KEY) && (hci_stack->link_key_db != NULL)){ 302963c0b2fdSMatthias Ringwald hci_stack->link_key_db->get_link_key(conn->address, conn->link_key, &conn->link_key_type); 303063c0b2fdSMatthias Ringwald } 303163c0b2fdSMatthias Ringwald 303263c0b2fdSMatthias Ringwald // response sent by hci_run() 303363c0b2fdSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_HANDLE_LINK_KEY_REQUEST; 303463c0b2fdSMatthias Ringwald #endif 3035608f51bbSMatthias Ringwald break; 30367fde4af9Smatthias.ringwald 30379ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 30381714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 3039f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30409ab95c90S[email protected] if (!conn) break; 30411714cbbdSMatthias Ringwald 30421714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 30431714cbbdSMatthias Ringwald 30443817f9dfSMatthias Ringwald // CVE-2020-26555: ignore NULL link key 30453817f9dfSMatthias Ringwald // default link_key_type = INVALID_LINK_KEY asserts that NULL key won't be used for encryption 30463817f9dfSMatthias Ringwald if (btstack_is_null(&packet[8], 16)) break; 30473817f9dfSMatthias Ringwald 30487bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 30499ab95c90S[email protected] // Change Connection Encryption keeps link key type 30509ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 30519ab95c90S[email protected] conn->link_key_type = link_key_type; 30529ab95c90S[email protected] } 30533817f9dfSMatthias Ringwald 3054e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 3055e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 3056e9f98c4aSMatthias Ringwald 3057bec5f683SMatthias Ringwald // only store link key: 3058bec5f683SMatthias Ringwald // - if bondable enabled 3059bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 30606edaed7fSMatthias Ringwald // - if security level sufficient 30616edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 3062bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 3063bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 3064532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 3065532454f9SMatthias Ringwald if (!remote_bonding){ 3066bec5f683SMatthias Ringwald break; 3067bec5f683SMatthias Ringwald } 3068bec5f683SMatthias Ringwald } 306955597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 30707fde4af9Smatthias.ringwald break; 30719ab95c90S[email protected] } 30727fde4af9Smatthias.ringwald 30737fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 30741714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 30751714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30761714cbbdSMatthias Ringwald if (!conn) break; 30771714cbbdSMatthias Ringwald 30781714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 3079a800d95eSMatthias Ringwald // abort pairing if: non-bondable mode (pin code request is not forwarded to app) 30803a9fb326S[email protected] if (!hci_stack->bondable ){ 30818daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 30821714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 3083f8fb5f6eS[email protected] hci_run(); 3084f8fb5f6eS[email protected] return; 30854c57c146S[email protected] } 3086a800d95eSMatthias Ringwald // abort pairing if: LEVEL_4 required (pin code request is not forwarded to app) 3087a800d95eSMatthias Ringwald if ((hci_stack->gap_secure_connections_only_mode) || (conn->requested_security_level == LEVEL_4)){ 3088a800d95eSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 308950dcc63cSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 309050dcc63cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 309150dcc63cSMatthias Ringwald hci_run(); 309250dcc63cSMatthias Ringwald return; 309350dcc63cSMatthias Ringwald } 30947fde4af9Smatthias.ringwald break; 30957fde4af9Smatthias.ringwald 309650d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 309750d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 309850d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 309950d7398cSMatthias Ringwald if (!conn) break; 31001714cbbdSMatthias Ringwald 31018daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE); 31021714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 310350d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 3104e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 3105d22b82d6SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3106d22b82d6SMatthias Ringwald conn->io_cap_response_oob_data = hci_event_io_capability_response_get_oob_data_present(packet); 3107d22b82d6SMatthias Ringwald #endif 310850d7398cSMatthias Ringwald break; 310950d7398cSMatthias Ringwald 31101d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 31111714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 31121714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 31131714cbbdSMatthias Ringwald if (!conn) break; 31141714cbbdSMatthias Ringwald 3115c950c316SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 31169671e615SMatthias Ringwald hci_connection_timestamp(conn); 31171714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 3118dbe1a790S[email protected] break; 3119dbe1a790S[email protected] 31201849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 31211849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 31227ca4a7edSMatthias Ringwald hci_event_remote_oob_data_request_get_bd_addr(packet, addr); 31237ca4a7edSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 31247ca4a7edSMatthias Ringwald if (!conn) break; 31257ca4a7edSMatthias Ringwald 31267ca4a7edSMatthias Ringwald hci_connection_timestamp(conn); 31277ca4a7edSMatthias Ringwald 31287ca4a7edSMatthias Ringwald hci_pairing_started(conn, true); 31297ca4a7edSMatthias Ringwald 31307ca4a7edSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 31311849becdSMatthias Ringwald break; 31321849becdSMatthias Ringwald #endif 31331849becdSMatthias Ringwald 3134dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 313517c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 3136367aedc0SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3137367aedc0SMatthias Ringwald if (!conn) break; 3138367aedc0SMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 3139367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 31408daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 3141367aedc0SMatthias Ringwald }; 3142367aedc0SMatthias Ringwald } else { 3143367aedc0SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3144367aedc0SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 3145367aedc0SMatthias Ringwald // don't forward event to app 3146367aedc0SMatthias Ringwald hci_run(); 3147367aedc0SMatthias Ringwald return; 3148367aedc0SMatthias Ringwald } 3149dbe1a790S[email protected] break; 3150dbe1a790S[email protected] 3151dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 3152367aedc0SMatthias Ringwald // Pairing using Passkey results in MITM protection. If Level 4 is required, support for SC is validated on IO Cap Request 3153367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 31548daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 3155367aedc0SMatthias Ringwald }; 31561d6b20aeS[email protected] break; 31571849becdSMatthias Ringwald 31583dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 31593dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 31603dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 31613dce6128SMatthias Ringwald if (!conn) break; 31623dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 31633dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 31643dce6128SMatthias Ringwald break; 316535454696SMatthias Ringwald #endif 31661d6b20aeS[email protected] 3167f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 3168254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 3169f0944df2S[email protected] conn = hci_connection_for_handle(handle); 3170f0944df2S[email protected] if (!conn) break; 31714ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 3172254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 3173254b78eeSMatthias Ringwald if (encryption_enabled){ 3174573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 3175573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 31768daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 3177573897a0SMatthias Ringwald } 3178573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 3179573897a0SMatthias Ringwald else { 3180fcaf38b9SMatthias Ringwald 3181fcaf38b9SMatthias Ringwald // dedicated bonding: send result and disconnect 3182fcaf38b9SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 3183fcaf38b9SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 3184fcaf38b9SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 3185fcaf38b9SMatthias Ringwald conn->bonding_status = packet[2]; 3186fcaf38b9SMatthias Ringwald break; 3187fcaf38b9SMatthias Ringwald } 3188fcaf38b9SMatthias Ringwald 3189254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 319036c3546bSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type); 3191254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 3192edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 3193254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 31949ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 3195254b78eeSMatthias Ringwald break; 3196254b78eeSMatthias Ringwald } 3197254b78eeSMatthias Ringwald 3198aa2fe986SMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 3199aa2fe986SMatthias Ringwald if (connected_uses_aes_ccm){ 3200aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3201aa2fe986SMatthias Ringwald } 3202aa2fe986SMatthias Ringwald 3203aa2fe986SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3204aa2fe986SMatthias Ringwald // work around for issue with PTS dongle 3205aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3206aa2fe986SMatthias Ringwald #endif 32071ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE)){ 32086e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 3209573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 32106e058d3fSMatthias Ringwald } else { 32116e058d3fSMatthias Ringwald // if not, pretend everything is perfect 32129866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 32136e058d3fSMatthias Ringwald } 3214573897a0SMatthias Ringwald } 3215573897a0SMatthias Ringwald #endif 3216f0944df2S[email protected] } else { 32178daf94bcSMatthias Ringwald conn->authentication_flags &= ~AUTH_FLAG_CONNECTION_ENCRYPTED; 3218f0944df2S[email protected] } 3219ad83dc6aS[email protected] } 3220573897a0SMatthias Ringwald 3221f0944df2S[email protected] break; 3222f0944df2S[email protected] 322335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 32241eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 3225abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 32261eb2563eS[email protected] conn = hci_connection_for_handle(handle); 32271eb2563eS[email protected] if (!conn) break; 3228ad83dc6aS[email protected] 3229dbd5dcc3SMatthias Ringwald // clear authentication active flag 3230dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 32311714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 3232dbd5dcc3SMatthias Ringwald 3233abdad579SMatthias Ringwald // authenticated only if auth status == 0 3234abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 3235abdad579SMatthias Ringwald // authenticated 32368daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3237abdad579SMatthias Ringwald 3238131ef17aSMatthias Ringwald // If not already encrypted, start encryption 32398daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0){ 32401eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3241ad83dc6aS[email protected] break; 3242ad83dc6aS[email protected] } 3243abdad579SMatthias Ringwald } 3244abdad579SMatthias Ringwald 3245abdad579SMatthias Ringwald // emit updated security level 32461eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 32471eb2563eS[email protected] break; 32481714cbbdSMatthias Ringwald 32491714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 32501714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 32511714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 32521714cbbdSMatthias Ringwald if (!conn) break; 32531714cbbdSMatthias Ringwald 3254aa2fe986SMatthias Ringwald // treat successfully paired connection as authenticated 3255aa2fe986SMatthias Ringwald if (hci_event_simple_pairing_complete_get_status(packet) == ERROR_CODE_SUCCESS){ 3256f3aafff1SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3257aa2fe986SMatthias Ringwald } 3258aa2fe986SMatthias Ringwald 32591714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 32601714cbbdSMatthias Ringwald break; 326135454696SMatthias Ringwald #endif 326234d2123cS[email protected] 326386805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 3264ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 3265ccda6e14S[email protected] // see end of function, too. 3266a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 3267a4f30ec0S[email protected] if (packet[2]) break; // status != 0 3268f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 326981d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 32704ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3271c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 32724ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 327381d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 3274c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 3275c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 327681d2bdb2SMatthias Ringwald if (release_buffer){ 327781d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 327881d2bdb2SMatthias Ringwald } 3279c6a37cfdSMatthias Ringwald } 3280c6a37cfdSMatthias Ringwald } 328135454696SMatthias Ringwald 3282c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 3283c6a37cfdSMatthias Ringwald if (!conn) break; 32841714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 32851714cbbdSMatthias Ringwald // pairing failed if it was ongoing 32861714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 32871714cbbdSMatthias Ringwald #endif 3288046ec007SMatthias Ringwald 3289046ec007SMatthias Ringwald // emit dedicatd bonding event 3290046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 3291046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 3292046ec007SMatthias Ringwald } 3293046ec007SMatthias Ringwald 3294459d27b9SMatthias Ringwald // mark connection for shutdown, stop timers, reset state 3295459d27b9SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 3296459d27b9SMatthias Ringwald hci_connection_stop_timer(conn); 3297459d27b9SMatthias Ringwald hci_connection_init(conn); 3298459d27b9SMatthias Ringwald 329935454696SMatthias Ringwald #ifdef ENABLE_BLE 3300d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3301046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 33022b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 3303bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 33049a2e4658SMatthias Ringwald } 330535454696SMatthias Ringwald #endif 3306d70217a2SMatthias Ringwald #endif 3307ccda6e14S[email protected] break; 33086772a24cSmatthias.ringwald 3309c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 3310313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 3311d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 3312c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 33137586ee35S[email protected] } else { 33147586ee35S[email protected] // if no special requests, just reboot stack 33157586ee35S[email protected] hci_power_control_off(); 33167586ee35S[email protected] hci_power_control_on(); 3317c68bdf90Smatthias.ringwald } 3318c68bdf90Smatthias.ringwald break; 3319c68bdf90Smatthias.ringwald 33200e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 33215cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 33225cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 3323c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 3324f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 3325c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3326c4c88f1bSJakob Krantz if (!conn) break; 33275cf766e8SMatthias Ringwald conn->role = packet[9]; 33285cf766e8SMatthias Ringwald break; 33290e6f3837SMatthias Ringwald #endif 33305cf766e8SMatthias Ringwald 333163fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 3332d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 33334fa24b5fS[email protected] if (hci_transport_synchronous()) { 333463fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 33354fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 33364fa24b5fS[email protected] } 333781d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 3338d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 3339d051460cS[email protected] hci_release_packet_buffer(); 3340701e3307SMatthias Ringwald 334163fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 334263fa3374SMatthias Ringwald 334335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 334463fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 3345701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 334635454696SMatthias Ringwald #endif 33476b4af23dS[email protected] break; 33486b4af23dS[email protected] 334935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 335063fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 335163fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 33521972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 335363fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 335463fa3374SMatthias Ringwald return; 33551cfb383eSMatthias Ringwald 33561cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 33571cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 33581cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 33591cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 33609784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 33611cfb383eSMatthias Ringwald break; 336235454696SMatthias Ringwald #endif 336363fa3374SMatthias Ringwald 3364a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 33655909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 33665909f7f2Smatthias.ringwald switch (packet[2]){ 3367d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 336857c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 33696fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 337057c9da5bS[email protected] le_handle_advertisement_report(packet, size); 33717bdc6798S[email protected] break; 3372a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 3373a0698cb4SMatthias Ringwald case HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT: 3374a0698cb4SMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 3375a0698cb4SMatthias Ringwald le_handle_extended_advertisement_report(packet, size); 3376a0698cb4SMatthias Ringwald break; 3377a0698cb4SMatthias Ringwald #endif 3378d70217a2SMatthias Ringwald #endif 33795909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 33800ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 33815909f7f2Smatthias.ringwald break; 33825909f7f2Smatthias.ringwald 3383f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3384c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3385c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3386c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3387c9db5c21SMilanka Ringwald if (!conn) break; 3388c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3389c9db5c21SMilanka Ringwald break; 339065a46ef3S[email protected] 339173cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 339273cd8a2aSMatthias Ringwald // connection 339373cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 339473cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 339573cd8a2aSMatthias Ringwald if (conn) { 339673cd8a2aSMatthias Ringwald // read arguments 339773cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 339873cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 339973cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 340073cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 340173cd8a2aSMatthias Ringwald 340273cd8a2aSMatthias Ringwald // validate against current connection parameter range 340373cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 340473cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 340573cd8a2aSMatthias 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); 340673cd8a2aSMatthias Ringwald if (update_parameter){ 340773cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 340873cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 340973cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 341073cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 341173cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 341273cd8a2aSMatthias Ringwald } else { 3413c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 341473cd8a2aSMatthias Ringwald } 341573cd8a2aSMatthias Ringwald } 341673cd8a2aSMatthias Ringwald break; 34170f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 34180f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 34190f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 34200f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 34210f3b27c5SMatthias Ringwald if (conn) { 34220f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 34230f3b27c5SMatthias Ringwald } 34240f3b27c5SMatthias Ringwald break; 34250f3b27c5SMatthias Ringwald #endif 34265909f7f2Smatthias.ringwald default: 34275909f7f2Smatthias.ringwald break; 34285909f7f2Smatthias.ringwald } 34295909f7f2Smatthias.ringwald break; 34305909f7f2Smatthias.ringwald #endif 34310b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 34320b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 34330b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 34340b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 34350b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 34360b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 34370b36101dSMatthias Ringwald break; 34380b36101dSMatthias Ringwald default: 34390b36101dSMatthias Ringwald break; 34400b36101dSMatthias Ringwald } 34410b36101dSMatthias Ringwald break; 34426772a24cSmatthias.ringwald default: 34436772a24cSmatthias.ringwald break; 3444fe1ed1b8Smatthias.ringwald } 3445fe1ed1b8Smatthias.ringwald 344667c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 344789db417bSmatthias.ringwald 344886805605S[email protected] // notify upper stack 3449d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 345094ab26f8Smatthias.ringwald 345186805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 3452797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 3453f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 345405ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 3455046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 3456797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 345705ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 3458b0136355SMatthias Ringwald } 345986805605S[email protected] } 346086805605S[email protected] 346194ab26f8Smatthias.ringwald // execute main loop 346294ab26f8Smatthias.ringwald hci_run(); 346316833f0aSmatthias.ringwald } 346416833f0aSmatthias.ringwald 346535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 34666f28d2eeSMatthias Ringwald 34676be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 34686f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 34696f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 34706f28d2eeSMatthias Ringwald 34716f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 347259c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 34735a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 34746f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 34756f28d2eeSMatthias Ringwald if (!conn) return; 34766f28d2eeSMatthias Ringwald 3477afaddd67SMatthias Ringwald // trigger send 3478afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 3479e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 3480a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 3481e4157653SMatthias Ringwald conn->sco_tx_ready++; 3482e4157653SMatthias Ringwald } 3483afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 34846f28d2eeSMatthias Ringwald } 34856f28d2eeSMatthias Ringwald 348659c33575SMatthias Ringwald 348759c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 348859c33575SMatthias Ringwald 34896f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 349059c33575SMatthias Ringwald 34916f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 349259c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 349359c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 349459c33575SMatthias Ringwald 349559c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 349659c33575SMatthias Ringwald 349759c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 3498ddbec4ceSMatthias Ringwald btstack_run_loop_remove_timer(timer); 349959c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 350059c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 350159c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 350259c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 35036f28d2eeSMatthias Ringwald } 35046be3cf7fSMatthias Ringwald #endif 35056f28d2eeSMatthias Ringwald 3506c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 35070b3f95dfSMatthias Ringwald // lookup connection struct 35082b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 35092b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 35100b3f95dfSMatthias Ringwald if (!conn) return; 35110b3f95dfSMatthias Ringwald 35126be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3513760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 3514760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 3515a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 3516760b20efSMatthias Ringwald packet[2] = 0x3c; 3517760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 3518760b20efSMatthias Ringwald size = 63; 35190b3f95dfSMatthias Ringwald } 35200b3f95dfSMatthias Ringwald } 3521760b20efSMatthias Ringwald 3522f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 3523f234b250SMatthias Ringwald // Nothing to do 3524f234b250SMatthias Ringwald } else { 352549205f5dSMatthias 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); 3526760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 35276f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 352859c33575SMatthias Ringwald 352959c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 353059c33575SMatthias Ringwald // ignore first 10 packets 35316f28d2eeSMatthias Ringwald conn->sco_rx_count++; 353259c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 353359c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 353459c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 35356f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 35366f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 353759c33575SMatthias Ringwald conn->sco_rx_count = 0; 353859c33575SMatthias Ringwald sco_schedule_tx(conn); 353959c33575SMatthias Ringwald } 354059c33575SMatthias Ringwald } else { 354159c33575SMatthias Ringwald // track expected arrival timme 354259c33575SMatthias Ringwald conn->sco_rx_count++; 354359c33575SMatthias Ringwald conn->sco_rx_ms += 7; 354459c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 354559c33575SMatthias Ringwald if (delta > 0){ 354659c33575SMatthias Ringwald conn->sco_rx_ms++; 354759c33575SMatthias Ringwald } 354859c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 35496f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 35506f28d2eeSMatthias Ringwald } 3551760b20efSMatthias Ringwald } 3552f234b250SMatthias Ringwald } 35536be3cf7fSMatthias Ringwald #endif 35546be3cf7fSMatthias Ringwald 35550b3f95dfSMatthias Ringwald // deliver to app 35560b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 35570b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 35580b3f95dfSMatthias Ringwald } 35590b3f95dfSMatthias Ringwald 3560ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3561ed325439SMatthias Ringwald // We can send one packet for each received packet 3562ed325439SMatthias Ringwald conn->sco_tx_ready++; 3563ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 3564ed325439SMatthias Ringwald #endif 3565ed325439SMatthias Ringwald 35660b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 35672b838201SMatthias Ringwald conn->num_packets_completed++; 35682b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 35692b838201SMatthias Ringwald hci_run(); 35702b838201SMatthias Ringwald #endif 3571c91d150bS[email protected] } 357235454696SMatthias Ringwald #endif 3573c91d150bS[email protected] 35740a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 35755bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 357610e830c9Smatthias.ringwald switch (packet_type) { 357710e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 357810e830c9Smatthias.ringwald event_handler(packet, size); 357910e830c9Smatthias.ringwald break; 358010e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 358110e830c9Smatthias.ringwald acl_handler(packet, size); 358210e830c9Smatthias.ringwald break; 358335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3584c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 3585c91d150bS[email protected] sco_handler(packet, size); 3586202c8a4cSMatthias Ringwald break; 358735454696SMatthias Ringwald #endif 358810e830c9Smatthias.ringwald default: 358910e830c9Smatthias.ringwald break; 359010e830c9Smatthias.ringwald } 359110e830c9Smatthias.ringwald } 359210e830c9Smatthias.ringwald 3593d6b06661SMatthias Ringwald /** 3594d6b06661SMatthias Ringwald * @brief Add event packet handler. 3595d6b06661SMatthias Ringwald */ 3596d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 3597d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 3598d6b06661SMatthias Ringwald } 3599d6b06661SMatthias Ringwald 360067f708e0SMatthias Ringwald /** 360167f708e0SMatthias Ringwald * @brief Remove event packet handler. 360267f708e0SMatthias Ringwald */ 360367f708e0SMatthias Ringwald void hci_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){ 360467f708e0SMatthias Ringwald btstack_linked_list_remove(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 360567f708e0SMatthias Ringwald } 3606d6b06661SMatthias Ringwald 3607fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 36083d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 3609fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 361016833f0aSmatthias.ringwald } 361116833f0aSmatthias.ringwald 361235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 36138abbe8b5SMatthias Ringwald /** 36148abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 36158abbe8b5SMatthias Ringwald */ 36163d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 36178abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 36188abbe8b5SMatthias Ringwald } 361935454696SMatthias Ringwald #endif 36208abbe8b5SMatthias Ringwald 362171de195eSMatthias Ringwald static void hci_state_reset(void){ 3622595bdbfbS[email protected] // no connections yet 3623595bdbfbS[email protected] hci_stack->connections = NULL; 362474308b23Smatthias.ringwald 362574308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 362674308b23Smatthias.ringwald // hci_stack->discoverable = 0; 362774308b23Smatthias.ringwald // hci_stack->connectable = 0; 362874308b23Smatthias.ringwald // hci_stack->bondable = 1; 3629b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 3630595bdbfbS[email protected] 363144935e40S[email protected] // buffer is free 363202c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 363344935e40S[email protected] 3634595bdbfbS[email protected] // no pending cmds 3635595bdbfbS[email protected] hci_stack->decline_reason = 0; 3636595bdbfbS[email protected] 3637c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 3638c214d65bSMatthias Ringwald 3639bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 3640496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 364132a12730SMatthias Ringwald hci_stack->page_timeout = 0x6000; // ca. 15 sec 364232a12730SMatthias Ringwald 3643baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic = 364459d59ecfSMatthias Ringwald GAP_TASK_SET_DEFAULT_LINK_POLICY | 364559d59ecfSMatthias Ringwald GAP_TASK_SET_CLASS_OF_DEVICE | 364659d59ecfSMatthias Ringwald GAP_TASK_SET_LOCAL_NAME | 3647bc2dcc03SMatthias Ringwald GAP_TASK_SET_EIR_DATA | 364832a12730SMatthias Ringwald GAP_TASK_WRITE_SCAN_ENABLE | 364932a12730SMatthias Ringwald GAP_TASK_WRITE_PAGE_TIMEOUT; 3650bea424a5SMatthias Ringwald #endif 3651bea424a5SMatthias Ringwald 3652cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 36538d5f0979SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 36541ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 3655cf01e888SMatthias Ringwald #endif 3656cf01e888SMatthias Ringwald 3657595bdbfbS[email protected] // LE 3658b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 3659b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 3660b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 3661d70217a2SMatthias Ringwald #endif 3662d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3663a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 366473799937SMatthias Ringwald hci_stack->le_scanning_param_update = true; 3665b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3666d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3667e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 3668d70217a2SMatthias Ringwald #endif 3669a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3670a61834b6SMatthias Ringwald hci_stack->le_advertisements_active = false; 3671a61834b6SMatthias Ringwald if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_PARAMS_SET) != 0){ 3672a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3673a61834b6SMatthias Ringwald } 3674a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 3675a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3676a61834b6SMatthias Ringwald } 3677a61834b6SMatthias Ringwald #endif 3678595bdbfbS[email protected] } 3679595bdbfbS[email protected] 368035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 36812d5e09d6SMatthias Ringwald /** 36822d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 36832d5e09d6SMatthias Ringwald */ 36842d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 36852d5e09d6SMatthias Ringwald // store and open remote device db 36862d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 36872d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 36882d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 36892d5e09d6SMatthias Ringwald } 36902d5e09d6SMatthias Ringwald } 369135454696SMatthias Ringwald #endif 36922d5e09d6SMatthias Ringwald 36932d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 3694475c8125Smatthias.ringwald 36953a9fb326S[email protected] #ifdef HAVE_MALLOC 36963a9fb326S[email protected] if (!hci_stack) { 36973a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 36983a9fb326S[email protected] } 36993a9fb326S[email protected] #else 37003a9fb326S[email protected] hci_stack = &hci_stack_static; 37013a9fb326S[email protected] #endif 370266fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 37033a9fb326S[email protected] 3704475c8125Smatthias.ringwald // reference to use transport layer implementation 37053a9fb326S[email protected] hci_stack->hci_transport = transport; 3706475c8125Smatthias.ringwald 370711e23e5fSmatthias.ringwald // reference to used config 37083a9fb326S[email protected] hci_stack->config = config; 370911e23e5fSmatthias.ringwald 371026a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 371126a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 371226a9b6daSMatthias Ringwald 37138fcba05dSmatthias.ringwald // max acl payload size defined in config.h 37143a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 37158fcba05dSmatthias.ringwald 371616833f0aSmatthias.ringwald // register packet handlers with transport 371710e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 3718f5454fc6Smatthias.ringwald 37193a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 3720e2386ba1S[email protected] 3721e2386ba1S[email protected] // class of device 37223a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 3723a45d6b9fS[email protected] 3724f20168b8Smatthias.ringwald // bondable by default 3725f20168b8Smatthias.ringwald hci_stack->bondable = 1; 3726f20168b8Smatthias.ringwald 3727e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 372863168530SMatthias Ringwald // classic name 372963168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 3730c4c88f1bSJakob Krantz 3731c4c88f1bSJakob Krantz // Master slave policy 3732c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 3733170fafaeSMatthias Ringwald 3734b4eb4420SMatthias Ringwald // Allow Role Switch 3735b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 3736b4eb4420SMatthias Ringwald 373778315a58SMatthias Ringwald // Default / minimum security level = 2 373878315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 373978315a58SMatthias Ringwald 37405dbec6b3SMatthias Ringwald // Default Security Mode 4 37415dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 37425dbec6b3SMatthias Ringwald 3743cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 3744cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 3745d821984bSMatthias Ringwald 3746d821984bSMatthias Ringwald // Link Supervision Timeout 3747d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 3748d821984bSMatthias Ringwald 3749e9f343c8SMatthias Ringwald #endif 375063168530SMatthias Ringwald 375163048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 37523a9fb326S[email protected] hci_stack->ssp_enable = 1; 37533a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 37543a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 37553a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 375669a97523S[email protected] 37575d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 37585d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 37595d23aae8SMatthias Ringwald 3760fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 3761fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 3762d950d659SMatthias Ringwald 3763831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3764831711daSMatthias Ringwald // connection parameter to use for outgoing connections 3765cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 3766cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 3767831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 3768831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 3769831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 3770831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 3771831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 3772831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 37737261e5d8SMatthias Ringwald 37747261e5d8SMatthias Ringwald // default LE Scanning 37758b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 37768b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 37778b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 3778831711daSMatthias Ringwald #endif 3779831711daSMatthias Ringwald 37802b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 37812b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 37822b6ab3e6SMatthias Ringwald #endif 37832b6ab3e6SMatthias Ringwald 3784831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 3785831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 3786831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 3787831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 3788831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 3789831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 3790831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 3791831711daSMatthias Ringwald 3792595bdbfbS[email protected] hci_state_reset(); 3793475c8125Smatthias.ringwald } 3794475c8125Smatthias.ringwald 37954688f216SMatthias Ringwald void hci_deinit(void){ 37964688f216SMatthias Ringwald #ifdef HAVE_MALLOC 37974688f216SMatthias Ringwald if (hci_stack) { 37984688f216SMatthias Ringwald free(hci_stack); 37994688f216SMatthias Ringwald } 38004688f216SMatthias Ringwald #endif 38014688f216SMatthias Ringwald hci_stack = NULL; 3802b5bbcbf4SMatthias Ringwald 3803b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 38044688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 3805b5bbcbf4SMatthias Ringwald #endif 38064688f216SMatthias Ringwald } 38074688f216SMatthias Ringwald 38083fb36a29SMatthias Ringwald /** 38093fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 38103fb36a29SMatthias Ringwald */ 38113fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 38123fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 38133fb36a29SMatthias Ringwald 38143fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 38153fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 38163fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 38173fb36a29SMatthias Ringwald } 38183fb36a29SMatthias Ringwald } 38193fb36a29SMatthias Ringwald 3820fb55bd0aSMatthias Ringwald /** 3821d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 3822fb55bd0aSMatthias Ringwald */ 3823fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 3824fb55bd0aSMatthias Ringwald // references to used control implementation 3825fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 3826d0b87befSMatthias Ringwald // init with transport config 3827d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 3828fb55bd0aSMatthias Ringwald } 3829fb55bd0aSMatthias Ringwald 38301f9eb2ccSMatthias Ringwald static void hci_discard_connections(void){ 38311f9eb2ccSMatthias Ringwald btstack_linked_list_iterator_t lit; 38321f9eb2ccSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 38331f9eb2ccSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 38341f9eb2ccSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 38351f9eb2ccSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 38361f9eb2ccSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 38371f9eb2ccSMatthias Ringwald hci_shutdown_connection(connection); 38381f9eb2ccSMatthias Ringwald } 38391f9eb2ccSMatthias Ringwald } 38401f9eb2ccSMatthias Ringwald 384171de195eSMatthias Ringwald void hci_close(void){ 3842e6d6524dSMatthias Ringwald 3843e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 3844404843c1Smatthias.ringwald // close remote device db 3845a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 3846a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 3847404843c1Smatthias.ringwald } 3848e6d6524dSMatthias Ringwald #endif 38497224be7eSMatthias Ringwald 38501f9eb2ccSMatthias Ringwald hci_discard_connections(); 38517224be7eSMatthias Ringwald 3852f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 38533a9fb326S[email protected] 38543a9fb326S[email protected] #ifdef HAVE_MALLOC 38553a9fb326S[email protected] free(hci_stack); 38563a9fb326S[email protected] #endif 38573a9fb326S[email protected] hci_stack = NULL; 3858404843c1Smatthias.ringwald } 3859404843c1Smatthias.ringwald 3860cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3861cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 3862cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 3863cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 3864cb70c5abSMatthias Ringwald } 3865cb70c5abSMatthias Ringwald #endif 3866cb70c5abSMatthias Ringwald 386735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3868170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 3869170fafaeSMatthias Ringwald // validate ranage and set 3870170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 3871170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 3872170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 3873170fafaeSMatthias Ringwald } 387478315a58SMatthias Ringwald 3875137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 3876137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 38775dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 3878137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 3879137715ebSMatthias Ringwald } else { 3880137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3881137715ebSMatthias Ringwald } 38825dbec6b3SMatthias Ringwald } 38835dbec6b3SMatthias Ringwald 38845dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 38855dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 38865dbec6b3SMatthias Ringwald } 38875dbec6b3SMatthias Ringwald 388878315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 388978315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 389078315a58SMatthias Ringwald } 389178315a58SMatthias Ringwald 389278315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 3893d7387af3SMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 3894d7387af3SMatthias Ringwald return LEVEL_4; 3895d7387af3SMatthias Ringwald } 389678315a58SMatthias Ringwald return hci_stack->gap_security_level; 389778315a58SMatthias Ringwald } 389830cdf3c6SMatthias Ringwald 38998ad4dfffSMatthias Ringwald void gap_set_minimal_service_security_level(gap_security_level_t security_level){ 39008ad4dfffSMatthias Ringwald hci_stack->gap_minimal_service_security_level = security_level; 39018ad4dfffSMatthias Ringwald } 39028ad4dfffSMatthias Ringwald 390330cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 390430cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 390530cdf3c6SMatthias Ringwald } 390630cdf3c6SMatthias Ringwald 390730cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 390830cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 390930cdf3c6SMatthias Ringwald } 3910170fafaeSMatthias Ringwald #endif 3911170fafaeSMatthias Ringwald 3912170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 391360b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 39149e61646fS[email protected] hci_stack->class_of_device = class_of_device; 3915baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_CLASS_OF_DEVICE; 391659d59ecfSMatthias Ringwald hci_run(); 39179e61646fS[email protected] } 391876f27cffSMatthias Ringwald 3919c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 3920c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 3921baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_DEFAULT_LINK_POLICY; 392259d59ecfSMatthias Ringwald hci_run(); 3923c33e56d3SMatthias Ringwald } 3924c33e56d3SMatthias Ringwald 3925b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 3926b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 3927b4eb4420SMatthias Ringwald } 3928b4eb4420SMatthias Ringwald 3929b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 3930b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 3931b4eb4420SMatthias Ringwald } 3932b4eb4420SMatthias Ringwald 39330c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 39340c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 39350c3eb48dSMatthias Ringwald } 39360c3eb48dSMatthias Ringwald 393776f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 393876f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 393976f27cffSMatthias Ringwald } 394035454696SMatthias Ringwald #endif 39419e61646fS[email protected] 39426fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 3943f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 3944f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 39456535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 3946f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 3947f456b2d0S[email protected] } 394876f27cffSMatthias Ringwald #endif 3949f456b2d0S[email protected] 39508d213e1aSmatthias.ringwald // State-Module-Driver overview 39518d213e1aSmatthias.ringwald // state module low-level 39528d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 39538d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 39548d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 39558d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 3956d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 3957d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 3958c7e0c5f6Smatthias.ringwald 395940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 39607301ad89Smatthias.ringwald 3961038bc64cSmatthias.ringwald // power on 3962f9a30166Smatthias.ringwald int err = 0; 39633a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 3964d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 3965f9a30166Smatthias.ringwald } 3966038bc64cSmatthias.ringwald if (err){ 39679da54300S[email protected] log_error( "POWER_ON failed"); 3968038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3969038bc64cSmatthias.ringwald return err; 3970038bc64cSmatthias.ringwald } 3971038bc64cSmatthias.ringwald 397224b3c629SMatthias Ringwald // int chipset driver 39733fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 39743fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 39753fb36a29SMatthias Ringwald } 39763fb36a29SMatthias Ringwald 397724b3c629SMatthias Ringwald // init transport 397824b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 397924b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 398024b3c629SMatthias Ringwald } 398124b3c629SMatthias Ringwald 398224b3c629SMatthias Ringwald // open transport 398324b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 3984038bc64cSmatthias.ringwald if (err){ 39859da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 39863a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3987d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3988f9a30166Smatthias.ringwald } 3989038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3990038bc64cSmatthias.ringwald return err; 3991038bc64cSmatthias.ringwald } 39928d213e1aSmatthias.ringwald return 0; 39938d213e1aSmatthias.ringwald } 3994038bc64cSmatthias.ringwald 399540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 39968d213e1aSmatthias.ringwald 39979da54300S[email protected] log_info("hci_power_control_off"); 39989418f9c9Smatthias.ringwald 39998d213e1aSmatthias.ringwald // close low-level device 400024b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 40018d213e1aSmatthias.ringwald 40029da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 40039418f9c9Smatthias.ringwald 40048d213e1aSmatthias.ringwald // power off 40053a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4006d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 40078d213e1aSmatthias.ringwald } 40089418f9c9Smatthias.ringwald 40099da54300S[email protected] log_info("hci_power_control_off - control closed"); 40109418f9c9Smatthias.ringwald 40113a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 401272ea5239Smatthias.ringwald } 401372ea5239Smatthias.ringwald 401440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 401572ea5239Smatthias.ringwald 40169da54300S[email protected] log_info("hci_power_control_sleep"); 40173144bce4Smatthias.ringwald 4018b429b9b7Smatthias.ringwald #if 0 4019b429b9b7Smatthias.ringwald // don't close serial port during sleep 4020b429b9b7Smatthias.ringwald 402172ea5239Smatthias.ringwald // close low-level device 40223a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 4023b429b9b7Smatthias.ringwald #endif 402472ea5239Smatthias.ringwald 402572ea5239Smatthias.ringwald // sleep mode 40263a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 4027d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 402872ea5239Smatthias.ringwald } 4029b429b9b7Smatthias.ringwald 40303a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 40318d213e1aSmatthias.ringwald } 40328d213e1aSmatthias.ringwald 403340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 4034b429b9b7Smatthias.ringwald 40359da54300S[email protected] log_info("hci_power_control_wake"); 4036b429b9b7Smatthias.ringwald 4037b429b9b7Smatthias.ringwald // wake on 40383a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 4039d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 4040b429b9b7Smatthias.ringwald } 4041b429b9b7Smatthias.ringwald 4042b429b9b7Smatthias.ringwald #if 0 4043b429b9b7Smatthias.ringwald // open low-level device 40443a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 4045b429b9b7Smatthias.ringwald if (err){ 40469da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 40473a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4048d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4049b429b9b7Smatthias.ringwald } 4050b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 4051b429b9b7Smatthias.ringwald return err; 4052b429b9b7Smatthias.ringwald } 4053b429b9b7Smatthias.ringwald #endif 4054b429b9b7Smatthias.ringwald 4055b429b9b7Smatthias.ringwald return 0; 4056b429b9b7Smatthias.ringwald } 4057b429b9b7Smatthias.ringwald 405844935e40S[email protected] static void hci_power_transition_to_initializing(void){ 405944935e40S[email protected] // set up state machine 406044935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 406102c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 406244935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 40635c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 406444935e40S[email protected] } 4065b429b9b7Smatthias.ringwald 406642bd3d77SMatthias Ringwald // returns error 406742bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 406842bd3d77SMatthias Ringwald int err; 40698d213e1aSmatthias.ringwald switch (power_mode){ 40708d213e1aSmatthias.ringwald case HCI_POWER_ON: 40718d213e1aSmatthias.ringwald err = hci_power_control_on(); 407242bd3d77SMatthias Ringwald if (err != 0) { 4073f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 407497b61c7bS[email protected] return err; 407597b61c7bS[email protected] } 407644935e40S[email protected] hci_power_transition_to_initializing(); 40778d213e1aSmatthias.ringwald break; 40788d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40798d213e1aSmatthias.ringwald // do nothing 40808d213e1aSmatthias.ringwald break; 40818d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4082b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 40838d213e1aSmatthias.ringwald break; 40847bbeb3adSMilanka Ringwald default: 40857bbeb3adSMilanka Ringwald btstack_assert(false); 40867bbeb3adSMilanka Ringwald break; 40878d213e1aSmatthias.ringwald } 408842bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 408942bd3d77SMatthias Ringwald } 40907301ad89Smatthias.ringwald 409142bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 40928d213e1aSmatthias.ringwald switch (power_mode){ 40938d213e1aSmatthias.ringwald case HCI_POWER_ON: 40948d213e1aSmatthias.ringwald // do nothing 40958d213e1aSmatthias.ringwald break; 40968d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40978d213e1aSmatthias.ringwald // no connections yet, just turn it off 40988d213e1aSmatthias.ringwald hci_power_control_off(); 40998d213e1aSmatthias.ringwald break; 41008d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4101b546ac54Smatthias.ringwald // no connections yet, just turn it off 410272ea5239Smatthias.ringwald hci_power_control_sleep(); 41038d213e1aSmatthias.ringwald break; 41047bbeb3adSMilanka Ringwald default: 41057bbeb3adSMilanka Ringwald btstack_assert(false); 41067bbeb3adSMilanka Ringwald break; 41078d213e1aSmatthias.ringwald } 410842bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 410942bd3d77SMatthias Ringwald } 41107301ad89Smatthias.ringwald 411142bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 41128d213e1aSmatthias.ringwald switch (power_mode){ 41138d213e1aSmatthias.ringwald case HCI_POWER_ON: 41148d213e1aSmatthias.ringwald // do nothing 41158d213e1aSmatthias.ringwald break; 41168d213e1aSmatthias.ringwald case HCI_POWER_OFF: 4117c7e0c5f6Smatthias.ringwald // see hci_run 41183a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4119beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 4120685ec254SMatthias Ringwald // setup watchdog timer for disconnect - only triggers if Controller does not respond anymore 4121685ec254SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 1000); 4122685ec254SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4123685ec254SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 41248d213e1aSmatthias.ringwald break; 41258d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4126b546ac54Smatthias.ringwald // see hci_run 41273a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 412874b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 41298d213e1aSmatthias.ringwald break; 41307bbeb3adSMilanka Ringwald default: 41317bbeb3adSMilanka Ringwald btstack_assert(false); 41327bbeb3adSMilanka Ringwald break; 41338d213e1aSmatthias.ringwald } 413442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 413542bd3d77SMatthias Ringwald } 41367301ad89Smatthias.ringwald 413742bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 41388d213e1aSmatthias.ringwald switch (power_mode){ 41398d213e1aSmatthias.ringwald case HCI_POWER_ON: 414044935e40S[email protected] hci_power_transition_to_initializing(); 41418d213e1aSmatthias.ringwald break; 41428d213e1aSmatthias.ringwald case HCI_POWER_OFF: 41438d213e1aSmatthias.ringwald // do nothing 41448d213e1aSmatthias.ringwald break; 41458d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4146b546ac54Smatthias.ringwald // see hci_run 41473a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 414874b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 41498d213e1aSmatthias.ringwald break; 41507bbeb3adSMilanka Ringwald default: 41517bbeb3adSMilanka Ringwald btstack_assert(false); 41527bbeb3adSMilanka Ringwald break; 41538d213e1aSmatthias.ringwald } 415400278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 415542bd3d77SMatthias Ringwald } 41568d213e1aSmatthias.ringwald 415742bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 41588d213e1aSmatthias.ringwald switch (power_mode){ 41598d213e1aSmatthias.ringwald case HCI_POWER_ON: 416044935e40S[email protected] hci_power_transition_to_initializing(); 41618d213e1aSmatthias.ringwald break; 41628d213e1aSmatthias.ringwald case HCI_POWER_OFF: 4163b546ac54Smatthias.ringwald // see hci_run 41643a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4165beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 41668d213e1aSmatthias.ringwald break; 41678d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4168b546ac54Smatthias.ringwald // do nothing 41698d213e1aSmatthias.ringwald break; 41707bbeb3adSMilanka Ringwald default: 41717bbeb3adSMilanka Ringwald btstack_assert(false); 41727bbeb3adSMilanka Ringwald break; 41738d213e1aSmatthias.ringwald } 417442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 417542bd3d77SMatthias Ringwald } 41768d213e1aSmatthias.ringwald 417742bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 417842bd3d77SMatthias Ringwald int err; 41798d213e1aSmatthias.ringwald switch (power_mode){ 41808d213e1aSmatthias.ringwald case HCI_POWER_ON: 41813144bce4Smatthias.ringwald err = hci_power_control_wake(); 41823144bce4Smatthias.ringwald if (err) return err; 418344935e40S[email protected] hci_power_transition_to_initializing(); 41848d213e1aSmatthias.ringwald break; 41858d213e1aSmatthias.ringwald case HCI_POWER_OFF: 41863a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4187beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 41888d213e1aSmatthias.ringwald break; 41898d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4190b546ac54Smatthias.ringwald // do nothing 41918d213e1aSmatthias.ringwald break; 41927bbeb3adSMilanka Ringwald default: 41937bbeb3adSMilanka Ringwald btstack_assert(false); 41947bbeb3adSMilanka Ringwald break; 41958d213e1aSmatthias.ringwald } 419642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 419742bd3d77SMatthias Ringwald } 41987bbeb3adSMilanka Ringwald 419942bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 420042bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 420124f87726SMatthias Ringwald int err = 0; 420242bd3d77SMatthias Ringwald switch (hci_stack->state){ 420342bd3d77SMatthias Ringwald case HCI_STATE_OFF: 420442bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 420542bd3d77SMatthias Ringwald break; 420642bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 420742bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 420842bd3d77SMatthias Ringwald break; 420942bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 421042bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 421142bd3d77SMatthias Ringwald break; 421242bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 421342bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 421442bd3d77SMatthias Ringwald break; 421542bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 421642bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 421742bd3d77SMatthias Ringwald break; 421842bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 421942bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 422042bd3d77SMatthias Ringwald break; 42217bbeb3adSMilanka Ringwald default: 42227bbeb3adSMilanka Ringwald btstack_assert(false); 42237bbeb3adSMilanka Ringwald break; 422411e23e5fSmatthias.ringwald } 422524f87726SMatthias Ringwald if (err != 0){ 422642bd3d77SMatthias Ringwald return err; 422742bd3d77SMatthias Ringwald } 422868d92d03Smatthias.ringwald 4229038bc64cSmatthias.ringwald // create internal event 4230ee8bf225Smatthias.ringwald hci_emit_state(); 4231ee8bf225Smatthias.ringwald 423268d92d03Smatthias.ringwald // trigger next/first action 423368d92d03Smatthias.ringwald hci_run(); 423468d92d03Smatthias.ringwald 4235475c8125Smatthias.ringwald return 0; 4236475c8125Smatthias.ringwald } 4237475c8125Smatthias.ringwald 423835454696SMatthias Ringwald 4239091e35e3SMatthias Ringwald static void hci_halting_run(void) { 4240091e35e3SMatthias Ringwald 4241091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 4242091e35e3SMatthias Ringwald 4243091e35e3SMatthias Ringwald hci_connection_t *connection; 4244091e35e3SMatthias Ringwald 4245091e35e3SMatthias Ringwald switch (hci_stack->substate) { 4246091e35e3SMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 4247091e35e3SMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_TIMER: 4248091e35e3SMatthias Ringwald 4249091e35e3SMatthias Ringwald #ifdef ENABLE_BLE 4250091e35e3SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4251091e35e3SMatthias Ringwald hci_whitelist_free(); 4252091e35e3SMatthias Ringwald #endif 4253091e35e3SMatthias Ringwald #endif 4254091e35e3SMatthias Ringwald // close all open connections 4255091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 4256091e35e3SMatthias Ringwald if (connection) { 4257091e35e3SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 4258091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4259091e35e3SMatthias Ringwald 4260091e35e3SMatthias Ringwald // check state 4261091e35e3SMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 4262091e35e3SMatthias Ringwald connection->state = SENT_DISCONNECT; 4263091e35e3SMatthias Ringwald 4264091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 4265091e35e3SMatthias Ringwald 4266091e35e3SMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 4267091e35e3SMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 4268091e35e3SMatthias Ringwald 4269091e35e3SMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 4270091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 4271091e35e3SMatthias Ringwald 4272091e35e3SMatthias Ringwald // finally, send the disconnect command 4273091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4274091e35e3SMatthias Ringwald return; 4275091e35e3SMatthias Ringwald } 4276091e35e3SMatthias Ringwald 4277091e35e3SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 4278091e35e3SMatthias Ringwald 4279091e35e3SMatthias Ringwald if (hci_stack->substate == HCI_HALTING_DISCONNECT_ALL_TIMER) { 4280091e35e3SMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 4281091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 4282091e35e3SMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_TIMER; 4283091e35e3SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 4284091e35e3SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4285091e35e3SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 4286091e35e3SMatthias Ringwald break; 4287091e35e3SMatthias Ringwald } 4288091e35e3SMatthias Ringwald 4289091e35e3SMatthias Ringwald /* fall through */ 4290091e35e3SMatthias Ringwald 4291091e35e3SMatthias Ringwald case HCI_HALTING_CLOSE: 4292091e35e3SMatthias Ringwald // close left over connections (that had not been properly closed before) 4293091e35e3SMatthias Ringwald hci_discard_connections(); 4294091e35e3SMatthias Ringwald 4295091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling off"); 4296091e35e3SMatthias Ringwald 4297091e35e3SMatthias Ringwald // switch mode 4298091e35e3SMatthias Ringwald hci_power_control_off(); 4299091e35e3SMatthias Ringwald 4300091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, emitting state"); 4301091e35e3SMatthias Ringwald hci_emit_state(); 4302091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, done"); 4303091e35e3SMatthias Ringwald break; 4304091e35e3SMatthias Ringwald 4305091e35e3SMatthias Ringwald case HCI_HALTING_W4_TIMER: 4306091e35e3SMatthias Ringwald // keep waiting 4307091e35e3SMatthias Ringwald 4308091e35e3SMatthias Ringwald break; 4309091e35e3SMatthias Ringwald default: 4310091e35e3SMatthias Ringwald break; 4311091e35e3SMatthias Ringwald } 4312091e35e3SMatthias Ringwald }; 4313091e35e3SMatthias Ringwald 4314091e35e3SMatthias Ringwald static void hci_falling_asleep_run(void){ 4315091e35e3SMatthias Ringwald hci_connection_t * connection; 4316091e35e3SMatthias Ringwald switch(hci_stack->substate) { 4317091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 4318091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP"); 4319091e35e3SMatthias Ringwald // close all open connections 4320091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 4321091e35e3SMatthias Ringwald if (connection){ 4322091e35e3SMatthias Ringwald 4323091e35e3SMatthias Ringwald // send disconnect 4324091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4325091e35e3SMatthias Ringwald 4326091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 4327091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4328091e35e3SMatthias Ringwald 4329091e35e3SMatthias Ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 4330091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 4331091e35e3SMatthias Ringwald return; 4332091e35e3SMatthias Ringwald } 4333091e35e3SMatthias Ringwald 4334091e35e3SMatthias Ringwald if (hci_classic_supported()){ 4335091e35e3SMatthias Ringwald // disable page and inquiry scan 4336091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4337091e35e3SMatthias Ringwald 4338091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, disabling inq scans"); 4339091e35e3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 4340091e35e3SMatthias Ringwald 4341091e35e3SMatthias Ringwald // continue in next sub state 4342091e35e3SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 4343091e35e3SMatthias Ringwald break; 4344091e35e3SMatthias Ringwald } 4345091e35e3SMatthias Ringwald 4346091e35e3SMatthias Ringwald /* fall through */ 4347091e35e3SMatthias Ringwald 4348091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 4349091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling sleep"); 4350091e35e3SMatthias Ringwald // switch mode 4351091e35e3SMatthias Ringwald hci_power_control_sleep(); // changes hci_stack->state to SLEEP 4352091e35e3SMatthias Ringwald hci_emit_state(); 4353091e35e3SMatthias Ringwald break; 4354091e35e3SMatthias Ringwald 4355091e35e3SMatthias Ringwald default: 4356091e35e3SMatthias Ringwald break; 4357091e35e3SMatthias Ringwald } 4358091e35e3SMatthias Ringwald } 4359091e35e3SMatthias Ringwald 436035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 436135454696SMatthias Ringwald 4362758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 4363758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 4364a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 4365baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_SCAN_ENABLE; 4366758b46ceSmatthias.ringwald hci_run(); 4367758b46ceSmatthias.ringwald } 4368758b46ceSmatthias.ringwald 436915a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 4370381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 4371381fbed8Smatthias.ringwald 43723a9fb326S[email protected] if (hci_stack->discoverable == enable){ 43733a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 4374381fbed8Smatthias.ringwald return; 4375381fbed8Smatthias.ringwald } 4376381fbed8Smatthias.ringwald 43773a9fb326S[email protected] hci_stack->discoverable = enable; 4378758b46ceSmatthias.ringwald hci_update_scan_enable(); 4379758b46ceSmatthias.ringwald } 4380b031bebbSmatthias.ringwald 438115a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 4382758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 4383758b46ceSmatthias.ringwald 4384758b46ceSmatthias.ringwald // don't emit event 43853a9fb326S[email protected] if (hci_stack->connectable == enable) return; 4386758b46ceSmatthias.ringwald 43873a9fb326S[email protected] hci_stack->connectable = enable; 4388758b46ceSmatthias.ringwald hci_update_scan_enable(); 4389381fbed8Smatthias.ringwald } 439035454696SMatthias Ringwald #endif 4391381fbed8Smatthias.ringwald 439215a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 43936535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 43945061f3afS[email protected] } 43955061f3afS[email protected] 43962b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 43972b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 43982b838201SMatthias Ringwald 43992b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 44002b838201SMatthias Ringwald hci_reserve_packet_buffer(); 44012b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 44022b838201SMatthias Ringwald 44032b838201SMatthias Ringwald uint16_t size = 0; 44042b838201SMatthias Ringwald uint16_t num_handles = 0; 44052b838201SMatthias Ringwald packet[size++] = 0x35; 44062b838201SMatthias Ringwald packet[size++] = 0x0c; 44072b838201SMatthias Ringwald size++; // skip param len 44082b838201SMatthias Ringwald size++; // skip num handles 44092b838201SMatthias Ringwald 44102b838201SMatthias Ringwald // add { handle, packets } entries 44112b838201SMatthias Ringwald btstack_linked_item_t * it; 44122b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 44132b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 44142b838201SMatthias Ringwald if (connection->num_packets_completed){ 44152b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 44162b838201SMatthias Ringwald size += 2; 44172b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 44182b838201SMatthias Ringwald size += 2; 44192b838201SMatthias Ringwald // 44202b838201SMatthias Ringwald num_handles++; 44212b838201SMatthias Ringwald connection->num_packets_completed = 0; 44222b838201SMatthias Ringwald } 44232b838201SMatthias Ringwald } 44242b838201SMatthias Ringwald 44252b838201SMatthias Ringwald packet[2] = size - 3; 44262b838201SMatthias Ringwald packet[3] = num_handles; 44272b838201SMatthias Ringwald 44282b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 44292b838201SMatthias Ringwald 44302b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 44312b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 44322b838201SMatthias Ringwald 44332b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 44342b838201SMatthias Ringwald if (hci_transport_synchronous()){ 4435e2d22487SMatthias Ringwald hci_release_packet_buffer(); 4436068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 44372b838201SMatthias Ringwald } 44382b838201SMatthias Ringwald } 44392b838201SMatthias Ringwald #endif 44402b838201SMatthias Ringwald 444126fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 444226fe9592SMatthias Ringwald UNUSED(ds); 444326fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 4444beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 4445beceeddeSMatthias Ringwald hci_emit_state(); 444626fe9592SMatthias Ringwald hci_run(); 444726fe9592SMatthias Ringwald } 444826fe9592SMatthias Ringwald 4449f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 44504ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 4451b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 4452b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 4453b5d8b22bS[email protected] if (connection) { 445428a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 4455b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 4456f30077b7SMatthias Ringwald return true; 4457b5d8b22bS[email protected] } 445828a0332dSMatthias Ringwald } else { 4459b5d8b22bS[email protected] // connection gone -> discard further fragments 446028a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 4461b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 4462b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 4463b5d8b22bS[email protected] } 4464b5d8b22bS[email protected] } 4465f30077b7SMatthias Ringwald return false; 44662b838201SMatthias Ringwald } 4467b031bebbSmatthias.ringwald 446835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4469f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 4470f30077b7SMatthias Ringwald 447159d59ecfSMatthias Ringwald // assert stack is working and classic is active 447259d59ecfSMatthias Ringwald if (hci_classic_supported() == false) return false; 447359d59ecfSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 447459d59ecfSMatthias Ringwald 4475b031bebbSmatthias.ringwald // decline incoming connections 44763a9fb326S[email protected] if (hci_stack->decline_reason){ 44773a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 44783a9fb326S[email protected] hci_stack->decline_reason = 0; 44793a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 4480f30077b7SMatthias Ringwald return true; 4481ce4c8fabSmatthias.ringwald } 448259d59ecfSMatthias Ringwald 4483baa4881eSMatthias Ringwald if (hci_stack->gap_tasks_classic != 0){ 4484ab4831a3SMatthias Ringwald hci_run_gap_tasks_classic(); 4485f30077b7SMatthias Ringwald return true; 4486b031bebbSmatthias.ringwald } 448727741fe7SMatthias Ringwald 4488f5875de5SMatthias Ringwald // start/stop inquiry 4489a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 4490f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 4491beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 4492496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 4493f30077b7SMatthias Ringwald return true; 4494f5875de5SMatthias Ringwald } 4495f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 4496f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 4497f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 4498f30077b7SMatthias Ringwald return true; 4499f5875de5SMatthias Ringwald } 4500b7f1ee76SMatthias Ringwald // remote name request 4501b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 4502b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 4503b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 4504ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 4505f30077b7SMatthias Ringwald return true; 4506b7f1ee76SMatthias Ringwald } 4507cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 4508cf01e888SMatthias Ringwald // Local OOB data 450959d59ecfSMatthias Ringwald if (hci_stack->classic_read_local_oob_data){ 4510cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 45111e83575aSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND)){ 4512cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 4513cf01e888SMatthias Ringwald } else { 4514cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 4515cf01e888SMatthias Ringwald } 4516cf01e888SMatthias Ringwald } 4517cf01e888SMatthias Ringwald #endif 45180a51f88bSMatthias Ringwald // pairing 45190a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 45200a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 45213f659ee4SMilanka Ringwald uint8_t pin_code[16]; 45220a51f88bSMatthias Ringwald switch (state){ 45230a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 4524cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 45253f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 45263f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 45273f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 45280a51f88bSMatthias Ringwald break; 45290a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 4530cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 45310a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 45320a51f88bSMatthias Ringwald break; 45330a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 4534cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 4535d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 45360a51f88bSMatthias Ringwald break; 45370a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 4538cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 45390a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 45400a51f88bSMatthias Ringwald break; 45410a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 4542cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 45430a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 45440a51f88bSMatthias Ringwald break; 45450a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 4546cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 45470a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 45480a51f88bSMatthias Ringwald break; 45490a51f88bSMatthias Ringwald default: 45500a51f88bSMatthias Ringwald break; 45510a51f88bSMatthias Ringwald } 4552f30077b7SMatthias Ringwald return true; 4553f30077b7SMatthias Ringwald } 4554f30077b7SMatthias Ringwald return false; 45550a51f88bSMatthias Ringwald } 455635454696SMatthias Ringwald #endif 4557b031bebbSmatthias.ringwald 4558a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4559f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 4560f30077b7SMatthias Ringwald 4561a41310b7SMatthias Ringwald // Phase 1: collect what to stop 4562a41310b7SMatthias Ringwald 4563a41310b7SMatthias Ringwald bool scanning_stop = false; 4564a41310b7SMatthias Ringwald bool connecting_stop = false; 4565a41310b7SMatthias Ringwald bool advertising_stop = false; 4566a41310b7SMatthias Ringwald 4567a41310b7SMatthias Ringwald #ifndef ENABLE_LE_CENTRAL 4568a41310b7SMatthias Ringwald UNUSED(scanning_stop); 45690abd9f64SMatthias Ringwald UNUSED(connecting_stop); 4570a41310b7SMatthias Ringwald #endif 4571a41310b7SMatthias Ringwald #ifndef ENABLE_LE_PERIPHERAL 4572a41310b7SMatthias Ringwald UNUSED(advertising_stop); 4573a41310b7SMatthias Ringwald #endif 4574a41310b7SMatthias Ringwald 45759136bde8SMatthias Ringwald // check if own address changes 45769136bde8SMatthias Ringwald bool random_address_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 45779136bde8SMatthias Ringwald 457829c24bebSMatthias Ringwald // check if whitelist needs modification 457929c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 458029c24bebSMatthias Ringwald btstack_linked_list_iterator_t lit; 458129c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 458229c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 458329c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 458429c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 458529c24bebSMatthias Ringwald whitelist_modification_pending = true; 458629c24bebSMatthias Ringwald break; 458729c24bebSMatthias Ringwald } 458829c24bebSMatthias Ringwald } 458921debf25SMatthias Ringwald // check if resolving list needs modification 459021debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 459121debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 45921ffa425cSMatthias Ringwald 45931ffa425cSMatthias Ringwald bool resolving_list_supported = hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE); 4594ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 459521debf25SMatthias Ringwald resolving_list_modification_pending = true; 459621debf25SMatthias Ringwald } 459721debf25SMatthias Ringwald #endif 459829c24bebSMatthias Ringwald 4599d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4600fde725feSMatthias Ringwald // scanning control 46013251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 460229c24bebSMatthias Ringwald // stop if: 460329c24bebSMatthias Ringwald // - parameter change required 460429c24bebSMatthias Ringwald // - it's disabled 460529c24bebSMatthias Ringwald // - whitelist change required but used for scanning 460621debf25SMatthias Ringwald // - resolving list modified 460751e51a58SMatthias Ringwald // - own address changes 460829c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 460921debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 461021debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 461121debf25SMatthias Ringwald scanning_uses_whitelist || 461251e51a58SMatthias Ringwald resolving_list_modification_pending || 461351e51a58SMatthias Ringwald random_address_change){ 461421debf25SMatthias Ringwald 46152d5c2a27SMatthias Ringwald scanning_stop = true; 4616fde725feSMatthias Ringwald } 4617fde725feSMatthias Ringwald } 461829c24bebSMatthias Ringwald #endif 4619fde725feSMatthias Ringwald 462029c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 462129c24bebSMatthias Ringwald // connecting control 4622f496d06eSMatthias Ringwald bool connecting_with_whitelist; 4623f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 4624f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 4625f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 4626af64f147SMatthias Ringwald // stop connecting if: 4627af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 4628af64f147SMatthias Ringwald // - if it got disabled 462921debf25SMatthias Ringwald // - resolving list modified 463051e51a58SMatthias Ringwald // - own address changes 4631f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 4632f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 463321debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 463451e51a58SMatthias Ringwald resolving_list_modification_pending || 463551e51a58SMatthias Ringwald random_address_change) { 463621debf25SMatthias Ringwald 463729c24bebSMatthias Ringwald connecting_stop = true; 463829c24bebSMatthias Ringwald } 4639f496d06eSMatthias Ringwald break; 4640f496d06eSMatthias Ringwald default: 4641f496d06eSMatthias Ringwald break; 464229c24bebSMatthias Ringwald } 4643d70217a2SMatthias Ringwald #endif 464429c24bebSMatthias Ringwald 4645d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 464645c102fdSMatthias Ringwald // le advertisement control 4647bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_active){ 464829c24bebSMatthias Ringwald // stop if: 464929c24bebSMatthias Ringwald // - parameter change required 46509136bde8SMatthias Ringwald // - random address used in advertising and changes 465129c24bebSMatthias Ringwald // - it's disabled 4652ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 465321debf25SMatthias Ringwald // - resolving list modified 465451e51a58SMatthias Ringwald // - own address changes 4655a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 46569136bde8SMatthias Ringwald bool advertising_uses_random_address = hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC; 46574e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 4658a61834b6SMatthias Ringwald if (advertising_change || 46599136bde8SMatthias Ringwald (advertising_uses_random_address && random_address_change) || 4660a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 46619136bde8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 466251e51a58SMatthias Ringwald resolving_list_modification_pending || 466351e51a58SMatthias Ringwald random_address_change) { 466421debf25SMatthias Ringwald 4665fde725feSMatthias Ringwald advertising_stop = true; 4666fde725feSMatthias Ringwald } 4667fde725feSMatthias Ringwald } 4668a41310b7SMatthias Ringwald #endif 4669fde725feSMatthias Ringwald 4670a41310b7SMatthias Ringwald 4671a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 4672a41310b7SMatthias Ringwald 4673a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4674a41310b7SMatthias Ringwald if (scanning_stop){ 46755226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 467663671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 467763671e69SMatthias Ringwald if (hci_extended_advertising_supported()) { 467863671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 0, 0, 0, 0); 467963671e69SMatthias Ringwald } else 468063671e69SMatthias Ringwald #endif 468163671e69SMatthias Ringwald { 4682a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 468363671e69SMatthias Ringwald } 4684a41310b7SMatthias Ringwald return true; 4685a41310b7SMatthias Ringwald } 4686a41310b7SMatthias Ringwald #endif 4687a41310b7SMatthias Ringwald 4688a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4689a41310b7SMatthias Ringwald if (connecting_stop){ 4690a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 4691a41310b7SMatthias Ringwald return true; 4692a41310b7SMatthias Ringwald } 4693a41310b7SMatthias Ringwald #endif 4694a41310b7SMatthias Ringwald 4695a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4696fde725feSMatthias Ringwald if (advertising_stop){ 46975226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = false; 4698*e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4699*e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 4700*e464cd48SMatthias Ringwald const uint8_t advertising_handles[] = { 0 }; 4701*e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 4702*e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 4703*e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 1, advertising_handles, durations, max_events); 4704*e464cd48SMatthias Ringwald } else 4705*e464cd48SMatthias Ringwald #endif 4706*e464cd48SMatthias Ringwald { 470745c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 4708*e464cd48SMatthias Ringwald } 4709f30077b7SMatthias Ringwald return true; 471045c102fdSMatthias Ringwald } 4711a41310b7SMatthias Ringwald #endif 4712fde725feSMatthias Ringwald 4713a41310b7SMatthias Ringwald // Phase 3: modify 4714a41310b7SMatthias Ringwald 47159136bde8SMatthias Ringwald if (random_address_change){ 47169136bde8SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 4717*e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4718*e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 4719*e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, 0, hci_stack->le_random_address); 4720*e464cd48SMatthias Ringwald } 4721*e464cd48SMatthias Ringwald #endif 4722*e464cd48SMatthias Ringwald { 47239136bde8SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, hci_stack->le_random_address); 4724*e464cd48SMatthias Ringwald } 47259136bde8SMatthias Ringwald return true; 47269136bde8SMatthias Ringwald } 47279136bde8SMatthias Ringwald 4728a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4729a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 4730a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 473163671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 473263671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 473363671e69SMatthias Ringwald // prepare arrays for all PHYs 473463671e69SMatthias Ringwald uint8_t scan_types[1] = { hci_stack->le_scan_type }; 473563671e69SMatthias Ringwald uint16_t scan_intervals[1] = { hci_stack->le_scan_interval }; 473663671e69SMatthias Ringwald uint16_t scan_windows[1] = { hci_stack->le_scan_window }; 473763671e69SMatthias Ringwald uint8_t scanning_phys = 1; // LE 1M PHY 473863671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_parameters, hci_stack->le_own_addr_type, 473963671e69SMatthias Ringwald hci_stack->le_scan_filter_policy, scanning_phys, scan_types, scan_intervals, scan_windows); 474063671e69SMatthias Ringwald } else 474163671e69SMatthias Ringwald #endif 474263671e69SMatthias Ringwald { 4743a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 4744a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 474563671e69SMatthias Ringwald } 4746a41310b7SMatthias Ringwald return true; 4747a41310b7SMatthias Ringwald } 4748a41310b7SMatthias Ringwald #endif 4749a41310b7SMatthias Ringwald 4750a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 475145c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 475245c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 47536bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 4754*e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4755*e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 4756*e464cd48SMatthias Ringwald // map advertisment type to advertising event properties 4757*e464cd48SMatthias Ringwald uint16_t adv_event_properties = 0; 4758*e464cd48SMatthias Ringwald const uint16_t mapping[] = { 0b00010011, 0b00010101, 0b00011101, 0b00010010, 0b00010000}; 4759*e464cd48SMatthias Ringwald if (hci_stack->le_advertisements_type < (sizeof(mapping)/sizeof(uint16_t))){ 4760*e464cd48SMatthias Ringwald adv_event_properties = mapping[hci_stack->le_advertisements_type]; 4761*e464cd48SMatthias Ringwald } 4762*e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 4763*e464cd48SMatthias Ringwald 0, 4764*e464cd48SMatthias Ringwald adv_event_properties, 4765*e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_min, 4766*e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_max, 4767*e464cd48SMatthias Ringwald hci_stack->le_advertisements_channel_map, 4768*e464cd48SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 4769*e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 4770*e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address, 4771*e464cd48SMatthias Ringwald hci_stack->le_advertisements_filter_policy, 4772*e464cd48SMatthias Ringwald 0x7f, // tx power: no preference 4773*e464cd48SMatthias Ringwald 0x01, // primary adv phy: LE 1M 4774*e464cd48SMatthias Ringwald 0, // secondary adv max skip 4775*e464cd48SMatthias Ringwald 0, // secondary adv phy 4776*e464cd48SMatthias Ringwald 0, // adv sid 4777*e464cd48SMatthias Ringwald 0 // scan request notification 4778*e464cd48SMatthias Ringwald ); 4779*e464cd48SMatthias Ringwald } 4780*e464cd48SMatthias Ringwald #endif 4781*e464cd48SMatthias Ringwald { 478245c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 478345c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 478445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 478545c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 47866bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 478745c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 478845c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 478945c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 479045c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 4791*e464cd48SMatthias Ringwald } 4792f30077b7SMatthias Ringwald return true; 479345c102fdSMatthias Ringwald } 4794501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 4795501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 47967a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 47977a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 47986535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 47996535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 48003c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 4801*e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4802*e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 4803*e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, 0, 0x03, 0x01, hci_stack->le_advertisements_data_len, adv_data_clean); 4804*e464cd48SMatthias Ringwald } else 4805*e464cd48SMatthias Ringwald #endif 4806*e464cd48SMatthias Ringwald { 48077a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 4808*e464cd48SMatthias Ringwald } 4809f30077b7SMatthias Ringwald return true; 481045c102fdSMatthias Ringwald } 4811501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 4812501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 4813f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 4814f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 48156535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 48166535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 48173c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 4818*e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4819*e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 4820*e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, 0, 0x03, 0x01, hci_stack->le_scan_response_data_len, scan_data_clean); 4821*e464cd48SMatthias Ringwald } else 4822*e464cd48SMatthias Ringwald #endif 4823*e464cd48SMatthias Ringwald { 4824214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 4825*e464cd48SMatthias Ringwald } 4826f30077b7SMatthias Ringwald return true; 4827501f56b3SMatthias Ringwald } 4828d70217a2SMatthias Ringwald #endif 48299956955bSMatthias Ringwald 483013eb2a2eSMatthias Ringwald 4831057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4832a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 4833a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 4834057ab60cSMatthias Ringwald #endif 4835057ab60cSMatthias Ringwald 4836a41310b7SMatthias Ringwald // LE Whitelist Management 4837a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 48389956955bSMatthias Ringwald // add/remove entries 4839665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4840665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4841665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4842453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 4843453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4844453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 4845453459ddSMatthias Ringwald return true; 4846453459ddSMatthias Ringwald } 48479956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 4848453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 4849453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 48509956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 4851f30077b7SMatthias Ringwald return true; 48529956955bSMatthias Ringwald } 4853453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 4854665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 48559956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 48569956955bSMatthias Ringwald } 48579956955bSMatthias Ringwald } 485891915b0bSMatthias Ringwald } 48599956955bSMatthias Ringwald 486021debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 486121debf25SMatthias Ringwald // LE Resolving List Management 4862ea151974SMatthias Ringwald if (resolving_list_supported) { 486321debf25SMatthias Ringwald uint16_t i; 486421debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 486521debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 486621debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 486721debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 486821debf25SMatthias Ringwald return true; 486921debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 487021debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 487121debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 487221debf25SMatthias Ringwald return true; 487321debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 487402b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 4875ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 4876ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 4877ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 4878ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 487921debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 488021debf25SMatthias Ringwald return true; 488102b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 488202b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 488302b02cffSMatthias Ringwald uint8_t offset = i >> 3; 488402b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 488502b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 488602b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 488702b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 488802b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 488902b02cffSMatthias Ringwald sm_key_t peer_irk; 489002b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 489102b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 4892f5228c62SMatthias Ringwald 4893f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 4894f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 4895f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4896f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 4897f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 4898f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 4899f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 4900f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 4901f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 4902f5228c62SMatthias Ringwald } 4903f5228c62SMatthias Ringwald #endif 4904f5228c62SMatthias Ringwald 4905ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 4906ea151974SMatthias Ringwald peer_identity_addreses); 490702b02cffSMatthias Ringwald return true; 490802b02cffSMatthias Ringwald } 490902b02cffSMatthias Ringwald 491002b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 491102b02cffSMatthias Ringwald 491202b02cffSMatthias Ringwald /* fall through */ 491302b02cffSMatthias Ringwald 491421debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 491521debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 491621debf25SMatthias Ringwald uint8_t offset = i >> 3; 491721debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 491802b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 491902b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 492021debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 492121debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 492221debf25SMatthias Ringwald sm_key_t peer_irk; 492321debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 492421debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 492521debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 492621debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 492721debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 492821debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 492921debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 493021debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 4931ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 4932ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 493321debf25SMatthias Ringwald return true; 493421debf25SMatthias Ringwald } 493502b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 493621debf25SMatthias Ringwald break; 493702b02cffSMatthias Ringwald 493821debf25SMatthias Ringwald default: 493921debf25SMatthias Ringwald break; 494021debf25SMatthias Ringwald } 4941ea151974SMatthias Ringwald } 494221debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 494321debf25SMatthias Ringwald #endif 4944a41310b7SMatthias Ringwald 494573799937SMatthias Ringwald // post-pone all actions until stack is fully working 494673799937SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 494773799937SMatthias Ringwald 494873799937SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 494973799937SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 495073799937SMatthias Ringwald 4951a41310b7SMatthias Ringwald // Phase 4: restore state 495229c24bebSMatthias Ringwald 495329c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4954af64f147SMatthias Ringwald // re-start scanning 495529c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 495629c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 495763671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 495863671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 495963671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 1, 0, 0, 0); 496063671e69SMatthias Ringwald } else 496163671e69SMatthias Ringwald #endif 496263671e69SMatthias Ringwald { 496329c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 496463671e69SMatthias Ringwald } 496529c24bebSMatthias Ringwald return true; 496629c24bebSMatthias Ringwald } 496729c24bebSMatthias Ringwald #endif 496829c24bebSMatthias Ringwald 496913eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4970af64f147SMatthias Ringwald // re-start connecting 4971af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 49729956955bSMatthias Ringwald bd_addr_t null_addr; 49739956955bSMatthias Ringwald memset(null_addr, 0, 6); 49746bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 49756bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 49769956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 4977cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 4978cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 49799956955bSMatthias Ringwald 1, // use whitelist 49809956955bSMatthias Ringwald 0, // peer address type 49819956955bSMatthias Ringwald null_addr, // peer bd addr 49826bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 498373044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 498473044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 498573044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 498673044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 498773044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 498873044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 49899956955bSMatthias Ringwald ); 4990f30077b7SMatthias Ringwald return true; 49919956955bSMatthias Ringwald } 4992d70217a2SMatthias Ringwald #endif 4993a41310b7SMatthias Ringwald 4994a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4995a41310b7SMatthias Ringwald // re-start advertising 49968978dcf1SMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && !hci_stack->le_advertisements_active){ 4997a41310b7SMatthias Ringwald // check if advertisements should be enabled given 49985226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = true; 4999b892db1cSMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, hci_stack->le_advertisements_own_address); 5000*e464cd48SMatthias Ringwald 5001*e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5002*e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5003*e464cd48SMatthias Ringwald const uint8_t advertising_handles[] = { 0 }; 5004*e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 5005*e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 5006*e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 5007*e464cd48SMatthias Ringwald } else 5008*e464cd48SMatthias Ringwald #endif 5009*e464cd48SMatthias Ringwald { 5010a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 5011*e464cd48SMatthias Ringwald } 5012a41310b7SMatthias Ringwald return true; 5013a41310b7SMatthias Ringwald } 5014a41310b7SMatthias Ringwald #endif 5015a41310b7SMatthias Ringwald 5016f30077b7SMatthias Ringwald return false; 50177bdc6798S[email protected] } 5018b2f949feS[email protected] #endif 50197bdc6798S[email protected] 502088a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 5021f30077b7SMatthias Ringwald btstack_linked_item_t * it; 5022a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 502305ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 502432ab9390Smatthias.ringwald 50250bf6344aS[email protected] switch(connection->state){ 50260bf6344aS[email protected] case SEND_CREATE_CONNECTION: 50274f3229d8S[email protected] switch(connection->address_type){ 502835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5029f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 50309da54300S[email protected] log_info("sending hci_create_connection"); 5031b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 50324f3229d8S[email protected] break; 503335454696SMatthias Ringwald #endif 50344f3229d8S[email protected] default: 5035a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5036d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 50379da54300S[email protected] log_info("sending hci_le_create_connection"); 50386bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 50396bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 50404f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 5041cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 5042cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 50434f3229d8S[email protected] 0, // don't use whitelist 50444f3229d8S[email protected] connection->address_type, // peer address type 50454f3229d8S[email protected] connection->address, // peer bd addr 50466bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 504773044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 504873044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 504973044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 505073044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 505173044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 505273044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 50534f3229d8S[email protected] ); 50544f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 5055b2f949feS[email protected] #endif 5056d70217a2SMatthias Ringwald #endif 50574f3229d8S[email protected] break; 50584f3229d8S[email protected] } 5059f30077b7SMatthias Ringwald return true; 5060ad83dc6aS[email protected] 506135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 50620bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 50635cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 5064f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 506576ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 5066895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 5067c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 5068f30077b7SMatthias Ringwald return true; 5069ee025741SMatthias Ringwald } 5070ee025741SMatthias Ringwald break; 507135454696SMatthias Ringwald #endif 50720bf6344aS[email protected] 5073a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5074d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 50750bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 50760bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 50770bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 5078f30077b7SMatthias Ringwald return true; 5079a6725849S[email protected] #endif 5080d70217a2SMatthias Ringwald #endif 50810bf6344aS[email protected] case SEND_DISCONNECT: 50820bf6344aS[email protected] connection->state = SENT_DISCONNECT; 50836ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5084f30077b7SMatthias Ringwald return true; 50850bf6344aS[email protected] 50860bf6344aS[email protected] default: 50870bf6344aS[email protected] break; 5088c7e0c5f6Smatthias.ringwald } 5089c7e0c5f6Smatthias.ringwald 5090cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 5091cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 5092cabf004eSMatthias Ringwald 50938daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_READ_RSSI){ 50948daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_READ_RSSI); 5095891b9fc2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 5096f30077b7SMatthias Ringwald return true; 5097891b9fc2SMatthias Ringwald } 5098891b9fc2SMatthias Ringwald 509994418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 510094418890SMatthias Ringwald 51018daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT){ 51028daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT); 51036909f064SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 5104f30077b7SMatthias Ringwald return true; 51056909f064SMatthias Ringwald } 51066909f064SMatthias Ringwald 5107ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 510850f49832SMatthias Ringwald if (((connection->authentication_flags & AUTH_FLAG_HANDLE_LINK_KEY_REQUEST) != 0)){ 5109608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 51108daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 511130e72d78SMatthias Ringwald 5112e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 5113e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 511430e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 5115e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 511632ab9390Smatthias.ringwald } else { 511732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 511832ab9390Smatthias.ringwald } 5119f30077b7SMatthias Ringwald return true; 512032ab9390Smatthias.ringwald } 51211d6b20aeS[email protected] 51228daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_DENY_PIN_CODE_REQUEST){ 51239da54300S[email protected] log_info("denying to pin request"); 51248daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_DENY_PIN_CODE_REQUEST); 512534d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 5126f30077b7SMatthias Ringwald return true; 51274c57c146S[email protected] } 51284c57c146S[email protected] 5129c950c316SMatthias Ringwald // security assessment requires remote features 51302dd8985bSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST) != 0){ 5131c950c316SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 5132c950c316SMatthias Ringwald hci_ssp_assess_security_on_io_cap_request(connection); 5133c950c316SMatthias 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 5134c950c316SMatthias Ringwald } 5135c950c316SMatthias Ringwald 51368daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY){ 51378daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 5138a8d20135SMatthias Ringwald // set authentication requirements: 5139a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 5140532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 5141a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 51429faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 51439faad3abS[email protected] authreq |= 1; 5144106d6d11S[email protected] } 5145532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 51468daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 5147532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 5148532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 5149532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 5150532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 5151532454f9SMatthias Ringwald bonding = false; 5152532454f9SMatthias Ringwald } 5153532454f9SMatthias Ringwald } 5154532454f9SMatthias Ringwald if (bonding){ 5155a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 5156a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 5157532454f9SMatthias Ringwald } else { 5158a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 5159a8d20135SMatthias Ringwald } 5160532454f9SMatthias Ringwald } 51611849becdSMatthias Ringwald uint8_t have_oob_data = 0; 51621849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 51631849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 51641849becdSMatthias Ringwald have_oob_data |= 1; 51651849becdSMatthias Ringwald } 51661849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 51671849becdSMatthias Ringwald have_oob_data |= 2; 51681849becdSMatthias Ringwald } 51691849becdSMatthias Ringwald #endif 51701849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 517111b03efaSMatthias Ringwald return true; 5172f8fb5f6eS[email protected] } 517311b03efaSMatthias Ringwald 51748daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 51758daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 517611b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 5177f30077b7SMatthias Ringwald return true; 517832ab9390Smatthias.ringwald } 517932ab9390Smatthias.ringwald 51801849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 51817ca4a7edSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY){ 51827ca4a7edSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 51831849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 51841849becdSMatthias Ringwald const uint8_t * r_192 = zero; 51851849becdSMatthias Ringwald const uint8_t * c_192 = zero; 51861849becdSMatthias Ringwald const uint8_t * r_256 = zero; 51871849becdSMatthias Ringwald const uint8_t * c_256 = zero; 51881849becdSMatthias Ringwald // verify P-256 OOB 51891e83575aSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && hci_command_supported(SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)) { 51901849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 51911849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 51921849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 51931849becdSMatthias Ringwald } 51941849becdSMatthias Ringwald } 51951849becdSMatthias Ringwald // verify P-192 OOB 51961849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 51971849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 51981849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 51991849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 52001849becdSMatthias Ringwald } 52011849becdSMatthias Ringwald } 52027ca4a7edSMatthias Ringwald 52037ca4a7edSMatthias Ringwald // assess security 52047ca4a7edSMatthias Ringwald bool need_level_4 = hci_stack->gap_secure_connections_only_mode || (connection->requested_security_level == LEVEL_4); 52057ca4a7edSMatthias Ringwald bool can_reach_level_4 = hci_remote_sc_enabled(connection) && (c_256 != NULL); 52067ca4a7edSMatthias Ringwald if (need_level_4 && !can_reach_level_4){ 52077ca4a7edSMatthias Ringwald log_info("Level 4 required, but not possible -> abort"); 52087ca4a7edSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_INSUFFICIENT_SECURITY); 52097ca4a7edSMatthias Ringwald // send oob negative reply 52107ca4a7edSMatthias Ringwald c_256 = NULL; 52117ca4a7edSMatthias Ringwald c_192 = NULL; 52127ca4a7edSMatthias Ringwald } 52137ca4a7edSMatthias Ringwald 52141849becdSMatthias Ringwald // Reply 52151849becdSMatthias Ringwald if (c_256 != zero) { 52161849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 52171849becdSMatthias Ringwald } else if (c_192 != zero){ 52181849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 52191849becdSMatthias Ringwald } else { 52201ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = connection->con_handle; 52211849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 52221849becdSMatthias Ringwald } 52231849becdSMatthias Ringwald return true; 52241849becdSMatthias Ringwald } 52251849becdSMatthias Ringwald #endif 52261849becdSMatthias Ringwald 52278daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_REPLY){ 52288daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 522934d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 5230f30077b7SMatthias Ringwald return true; 5231dbe1a790S[email protected] } 5232dbe1a790S[email protected] 5233367aedc0SMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY){ 5234367aedc0SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 5235367aedc0SMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, &connection->address); 5236367aedc0SMatthias Ringwald return true; 5237367aedc0SMatthias Ringwald } 5238367aedc0SMatthias Ringwald 52398daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_PASSKEY_REPLY){ 52408daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 524134d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 5242f30077b7SMatthias Ringwald return true; 5243dbe1a790S[email protected] } 5244afd4e962S[email protected] 5245ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 5246ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 52471bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 52486ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 52496ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5250f30077b7SMatthias Ringwald return true; 5251ad83dc6aS[email protected] } 525276f27cffSMatthias Ringwald 52532a75353aSMatthias Ringwald if ((connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 525434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 5255abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 525634d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 5257f30077b7SMatthias Ringwald return true; 5258afd4e962S[email protected] } 525976f27cffSMatthias Ringwald 5260dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 5261dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 5262dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 5263f30077b7SMatthias Ringwald return true; 5264dce78009S[email protected] } 5265447016ffSMatthias Ringwald 5266573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 5267573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 5268573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 5269f30077b7SMatthias Ringwald return true; 5270573897a0SMatthias Ringwald } 5271447016ffSMatthias Ringwald 5272447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 5273447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 5274447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 5275447016ffSMatthias Ringwald return true; 5276447016ffSMatthias Ringwald } 5277447016ffSMatthias Ringwald 5278447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 5279447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 5280447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 5281447016ffSMatthias Ringwald return true; 5282447016ffSMatthias Ringwald } 5283447016ffSMatthias Ringwald 5284447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 5285447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 5286447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 5287447016ffSMatthias Ringwald return true; 5288447016ffSMatthias Ringwald } 528976f27cffSMatthias Ringwald #endif 529076f27cffSMatthias Ringwald 529176f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 529276f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 52931714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 52941714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 52951714cbbdSMatthias Ringwald #endif 52966ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 52976ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 52986ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 5299f30077b7SMatthias Ringwald return true; 530076f27cffSMatthias Ringwald } 53016ef3696aSMatthias Ringwald } 5302da886c03S[email protected] 53036cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 5304f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 5305f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 5306f8ee3071SMatthias Ringwald case 0: 5307f8ee3071SMatthias Ringwald break; 5308f8ee3071SMatthias Ringwald case 0xffff: 5309f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 5310f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 5311f30077b7SMatthias Ringwald return true; 5312f8ee3071SMatthias Ringwald default: 5313f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 5314f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 5315f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 5316f30077b7SMatthias Ringwald return true; 5317f8ee3071SMatthias Ringwald } 531888a03c8dSMatthias Ringwald 5319140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 5320140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 5321140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 5322140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 5323140c0557SMatthias Ringwald return true; 5324140c0557SMatthias Ringwald } 5325140c0557SMatthias Ringwald 5326965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 5327278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 5328965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 5329278ff8a9SMatthias 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); 5330278ff8a9SMatthias Ringwald return true; 5331278ff8a9SMatthias Ringwald } 5332278ff8a9SMatthias Ringwald 533388a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 533488a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 533588a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 533688a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 533788a03c8dSMatthias Ringwald return true; 533888a03c8dSMatthias Ringwald } 53396cdc2862SMatthias Ringwald #endif 5340f8ee3071SMatthias Ringwald 5341a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 534273cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 534373cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 534473cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 5345da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 534673cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 5347c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 5348c37a3166S[email protected] 0x0000, 0xffff); 5349f30077b7SMatthias Ringwald return true; 535073cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 535173cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 535273cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 535373cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 535473cd8a2aSMatthias Ringwald 0x0000, 0xffff); 5355f30077b7SMatthias Ringwald return true; 535673cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 535773cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 535873cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE); 5359f30077b7SMatthias Ringwald return true; 536073cd8a2aSMatthias Ringwald default: 536173cd8a2aSMatthias Ringwald break; 5362c37a3166S[email protected] } 53634ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 5364b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 5365b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 5366b90f6e0aSMatthias 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); 5367f30077b7SMatthias Ringwald return true; 5368b90f6e0aSMatthias Ringwald } 5369c37a3166S[email protected] #endif 5370dbe1a790S[email protected] } 5371f30077b7SMatthias Ringwald return false; 5372f30077b7SMatthias Ringwald } 5373c7e0c5f6Smatthias.ringwald 5374f30077b7SMatthias Ringwald static void hci_run(void){ 5375f30077b7SMatthias Ringwald 5376df9e2780SMatthias Ringwald // stack state sub statemachines 53770d37aff3SMatthias Ringwald // halting needs to be called even if we cannot send command packet now 5378df9e2780SMatthias Ringwald switch (hci_stack->state) { 5379df9e2780SMatthias Ringwald case HCI_STATE_INITIALIZING: 5380df9e2780SMatthias Ringwald hci_initializing_run(); 5381df9e2780SMatthias Ringwald break; 5382df9e2780SMatthias Ringwald case HCI_STATE_HALTING: 5383df9e2780SMatthias Ringwald hci_halting_run(); 5384df9e2780SMatthias Ringwald break; 5385df9e2780SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 5386df9e2780SMatthias Ringwald hci_falling_asleep_run(); 5387df9e2780SMatthias Ringwald break; 5388df9e2780SMatthias Ringwald default: 5389df9e2780SMatthias Ringwald break; 5390df9e2780SMatthias Ringwald } 5391df9e2780SMatthias Ringwald 5392f30077b7SMatthias Ringwald bool done; 5393f30077b7SMatthias Ringwald 5394f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 5395f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 5396f30077b7SMatthias Ringwald if (done) return; 5397f30077b7SMatthias Ringwald 5398f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 5399f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 5400f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 5401f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 5402f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 5403f30077b7SMatthias Ringwald return; 5404f30077b7SMatthias Ringwald } 5405f30077b7SMatthias Ringwald #endif 5406f30077b7SMatthias Ringwald 5407f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5408f30077b7SMatthias Ringwald 5409f30077b7SMatthias Ringwald // global/non-connection oriented commands 5410f30077b7SMatthias Ringwald 5411f30077b7SMatthias Ringwald 5412f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 5413f30077b7SMatthias Ringwald // general gap classic 5414f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 5415f30077b7SMatthias Ringwald if (done) return; 5416f30077b7SMatthias Ringwald #endif 5417f30077b7SMatthias Ringwald 5418f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 5419f30077b7SMatthias Ringwald // general gap le 5420f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 5421f30077b7SMatthias Ringwald if (done) return; 5422f30077b7SMatthias Ringwald #endif 5423f30077b7SMatthias Ringwald 5424f30077b7SMatthias Ringwald // send pending HCI commands 5425df9e2780SMatthias Ringwald hci_run_general_pending_commands(); 54263429f56bSmatthias.ringwald } 542716833f0aSmatthias.ringwald 54283e2050f7SMatthias Ringwald uint8_t hci_send_cmd_packet(uint8_t *packet, int size){ 542935454696SMatthias Ringwald // house-keeping 543035454696SMatthias Ringwald 543135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5432c8e4258aSmatthias.ringwald bd_addr_t addr; 5433c8e4258aSmatthias.ringwald hci_connection_t * conn; 5434c123d999SMatthias Ringwald #endif 5435c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 54366f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 54379cbd2215SMatthias Ringwald #endif 5438c8e4258aSmatthias.ringwald 54396f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 54406f35bb46SMatthias Ringwald switch (opcode) { 54419cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 54429cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 54439cbd2215SMatthias Ringwald break; 54449cbd2215SMatthias Ringwald 54459cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 54466f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 5447724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 54489da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 5449c8e4258aSmatthias.ringwald 545072cf8859SMatthias Ringwald // CVE-2020-26555: reject outgoing connection to device with same BD ADDR 545172cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0) { 545279e0fa07SMatthias Ringwald hci_emit_connection_complete(addr, 0, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR); 54533e2050f7SMatthias Ringwald return ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 545472cf8859SMatthias Ringwald } 545572cf8859SMatthias Ringwald 5456f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 5457ad83dc6aS[email protected] if (!conn) { 5458f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 545917f1ba2aSmatthias.ringwald if (!conn) { 546017f1ba2aSmatthias.ringwald // notify client that alloc failed 54612deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 54623e2050f7SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller 546317f1ba2aSmatthias.ringwald } 5464ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 5465f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 5466ad83dc6aS[email protected] } 5467aee5d5c1SMatthias Ringwald 5468aee5d5c1SMatthias Ringwald conn->con_handle = HCI_CON_HANDLE_INVALID; 5469aee5d5c1SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 5470aee5d5c1SMatthias Ringwald 5471ad83dc6aS[email protected] log_info("conn state %u", conn->state); 54723e2050f7SMatthias Ringwald // TODO: L2CAP should not send create connection command, instead a (new) gap function should be used 5473ad83dc6aS[email protected] switch (conn->state) { 5474ad83dc6aS[email protected] // if connection active exists 5475ad83dc6aS[email protected] case OPEN: 5476f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 547772cf8859SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, ERROR_CODE_SUCCESS); 54783e2050f7SMatthias Ringwald // packet not sent to controller 54793e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 5480672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 5481672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 5482672e36abSMatthias Ringwald break; 5483ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 5484532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 5485532f91a5SMatthias Ringwald break; 5486ad83dc6aS[email protected] default: 5487ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 54883e2050f7SMatthias Ringwald // packet not sent to controller 54893e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 5490ad83dc6aS[email protected] } 5491c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 5492229331c6SMatthias Ringwald 5493229331c6SMatthias Ringwald // track outgoing connection 5494f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 54956535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 54966f35bb46SMatthias Ringwald break; 5497ee752bb8SMatthias Ringwald 54985b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 54996f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 5500ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 5501ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 5502ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 55036f35bb46SMatthias Ringwald break; 55046f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 5505c91e6e4fSMatthias Ringwald // accept_synchronous_connection? Voice setting at offset 18 5506ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 5507ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 55081f34df2cSMatthias Ringwald // track outgoing connection 55091f34df2cSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_SCO; 55101f34df2cSMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->outgoing_addr); 55116f35bb46SMatthias Ringwald break; 5512ee752bb8SMatthias Ringwald #endif 551335454696SMatthias Ringwald #endif 55144b3e1e19SMatthias Ringwald 5515a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5516d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 55176f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 5518b04dfa37SMatthias Ringwald // white list used? 55196f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 5520b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 5521b04dfa37SMatthias Ringwald case 0: 5522b04dfa37SMatthias Ringwald // whitelist not used 5523b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 5524b04dfa37SMatthias Ringwald break; 5525b04dfa37SMatthias Ringwald case 1: 5526b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 5527b04dfa37SMatthias Ringwald break; 5528b04dfa37SMatthias Ringwald default: 5529b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 5530b04dfa37SMatthias Ringwald break; 5531b04dfa37SMatthias Ringwald } 5532c163146eSMatthias Ringwald // track outgoing connection 553305002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 5534c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 55356f35bb46SMatthias Ringwald break; 55366f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 55376ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 55386f35bb46SMatthias Ringwald break; 55399cbd2215SMatthias Ringwald #endif 55409cbd2215SMatthias Ringwald #endif 55416f35bb46SMatthias Ringwald default: 55426f35bb46SMatthias Ringwald break; 5543b04dfa37SMatthias Ringwald } 554469a97523S[email protected] 55453a9fb326S[email protected] hci_stack->num_cmd_packets--; 55465bb5bc3eS[email protected] 55475bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 55483e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 55493e2050f7SMatthias Ringwald if (err != 0){ 55503e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 55513e2050f7SMatthias Ringwald } 55523e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 555331452debSmatthias.ringwald } 55548adf0ddaSmatthias.ringwald 55552bd8b7e7S[email protected] // disconnect because of security block 55562bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 55572bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 55582bd8b7e7S[email protected] if (!connection) return; 55592bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 55602bd8b7e7S[email protected] } 55612bd8b7e7S[email protected] 55622bd8b7e7S[email protected] 5563dbe1a790S[email protected] // Configure Secure Simple Pairing 5564dbe1a790S[email protected] 556535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 556635454696SMatthias Ringwald 5567dbe1a790S[email protected] // enable will enable SSP during init 556815a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 55693a9fb326S[email protected] hci_stack->ssp_enable = enable; 5570dbe1a790S[email protected] } 5571dbe1a790S[email protected] 557295d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 557315a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 55742bd8b7e7S[email protected] } 55752bd8b7e7S[email protected] 5576dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 557715a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 55783a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 5579dbe1a790S[email protected] } 558015a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 55813a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 5582dbe1a790S[email protected] } 5583dbe1a790S[email protected] 5584dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 558515a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 55863a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 5587dbe1a790S[email protected] } 55885d23aae8SMatthias Ringwald 55895d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 55905d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 55915d23aae8SMatthias Ringwald } 55925d23aae8SMatthias Ringwald 559335454696SMatthias Ringwald #endif 5594dbe1a790S[email protected] 559594be1a66SMatthias Ringwald // va_list part of hci_send_cmd 55963e2050f7SMatthias Ringwald uint8_t hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 5597d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 55989d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 55993e2050f7SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 56009d14b626S[email protected] } 56019d14b626S[email protected] 56025127cc62S[email protected] // for HCI INITIALIZATION 56039da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 56045127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 56055127cc62S[email protected] 56069d14b626S[email protected] hci_reserve_packet_buffer(); 56079d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 560894be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 56093e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_packet(packet, size); 5610bfea0222SMatthias Ringwald 5611593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 56123e2050f7SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) || hci_transport_synchronous()){ 5613e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5614068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 5615bfea0222SMatthias Ringwald } 5616bfea0222SMatthias Ringwald 56173e2050f7SMatthias Ringwald return status; 561894be1a66SMatthias Ringwald } 56199d14b626S[email protected] 562094be1a66SMatthias Ringwald /** 562194be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 562294be1a66SMatthias Ringwald */ 56233e2050f7SMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t * cmd, ...){ 56241cd208adSmatthias.ringwald va_list argptr; 56251cd208adSmatthias.ringwald va_start(argptr, cmd); 56263e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_va_arg(cmd, argptr); 56271cd208adSmatthias.ringwald va_end(argptr); 56283e2050f7SMatthias Ringwald return status; 562993b8dc03Smatthias.ringwald } 5630c8e4258aSmatthias.ringwald 5631ee091cf1Smatthias.ringwald // Create various non-HCI events. 5632ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 5633ee091cf1Smatthias.ringwald 5634d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 5635fb37a842SMatthias Ringwald // dump packet 5636d6b06661SMatthias Ringwald if (dump) { 5637300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 5638d6b06661SMatthias Ringwald } 56391ef6bb52SMatthias Ringwald 5640fb37a842SMatthias Ringwald // dispatch to all event handlers 56411ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 56421ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 56431ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 56441ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 5645d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 56461ef6bb52SMatthias Ringwald } 5647d6b06661SMatthias Ringwald } 5648d6b06661SMatthias Ringwald 5649d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 5650fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 56513d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 5652d6b06661SMatthias Ringwald } 5653d6b06661SMatthias Ringwald 565435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5655701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 56563bc639ceSMatthias Ringwald // notify SCO sender if waiting 5657701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 5658701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 56593bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 5660701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 5661701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 56623d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 56633bc639ceSMatthias Ringwald } 56643bc639ceSMatthias Ringwald } 56651cfb383eSMatthias Ringwald 56661cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 56679784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 5668ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 56691cfb383eSMatthias Ringwald 56701cfb383eSMatthias Ringwald uint8_t * eir_data; 56711cfb383eSMatthias Ringwald ad_context_t context; 56721cfb383eSMatthias Ringwald const uint8_t * name; 56731cfb383eSMatthias Ringwald uint8_t name_len; 56741cfb383eSMatthias Ringwald 56759784dac1SMatthias Ringwald if (size < 3) return; 56769784dac1SMatthias Ringwald 56771cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 5678a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 56791cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 56801cfb383eSMatthias Ringwald 56819784dac1SMatthias Ringwald switch (event_type){ 56829784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 56839784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 56849784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 56859784dac1SMatthias Ringwald break; 56869784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 56879784dac1SMatthias Ringwald if (size != 257) return; 56889784dac1SMatthias Ringwald if (num_responses != 1) return; 56899784dac1SMatthias Ringwald break; 56909784dac1SMatthias Ringwald default: 56919784dac1SMatthias Ringwald return; 56929784dac1SMatthias Ringwald } 56939784dac1SMatthias Ringwald 56941cfb383eSMatthias Ringwald // event[1] is set at the end 56951cfb383eSMatthias Ringwald int i; 56961cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 56971cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 56981cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 5699ac973fd6SMatthias Ringwald uint8_t event_size = 27; // if name is not set by EIR 57001cfb383eSMatthias Ringwald 57016535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 57020e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 57036535961aSMatthias Ringwald (void)memcpy(&event[9], 57046535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 57056535961aSMatthias Ringwald 3); // class of device 57066535961aSMatthias Ringwald (void)memcpy(&event[12], 57076535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 57086535961aSMatthias Ringwald 2); // clock offset 57091cfb383eSMatthias Ringwald 57101cfb383eSMatthias Ringwald switch (event_type){ 57111cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 57121cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 57131cfb383eSMatthias Ringwald break; 57141cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 57151cfb383eSMatthias Ringwald event[14] = 1; 5716a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 57171cfb383eSMatthias Ringwald // 16,17 = 0, size 18 57181cfb383eSMatthias Ringwald break; 57191cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 57201cfb383eSMatthias Ringwald event[14] = 1; 5721a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 572279186815SMatthias Ringwald // EIR packets only contain a single inquiry response 57231cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 57241cfb383eSMatthias Ringwald name = NULL; 5725a8c4e5adSMatthias Ringwald // Iterate over EIR data 5726a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 57271cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 57281cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 57291cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 5730ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 57311cfb383eSMatthias Ringwald switch (data_type){ 57321cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 57331cfb383eSMatthias Ringwald if (name) continue; 5734cf373d3aSMatthias Ringwald /* fall through */ 57351cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 57361cfb383eSMatthias Ringwald name = data; 57371cfb383eSMatthias Ringwald name_len = data_size; 57381cfb383eSMatthias Ringwald break; 5739ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 5740ac9136ccSMatthias Ringwald if (data_size != 8) break; 5741ac9136ccSMatthias Ringwald event[16] = 1; 57423c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 5743ac9136ccSMatthias Ringwald break; 57441cfb383eSMatthias Ringwald default: 57451cfb383eSMatthias Ringwald break; 57461cfb383eSMatthias Ringwald } 57471cfb383eSMatthias Ringwald } 57481cfb383eSMatthias Ringwald if (name){ 5749ac9136ccSMatthias Ringwald event[25] = 1; 57501cfb383eSMatthias Ringwald // truncate name if needed 57511cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 5752ac9136ccSMatthias Ringwald event[26] = len; 5753ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 57541cfb383eSMatthias Ringwald event_size += len; 57551cfb383eSMatthias Ringwald } 57561cfb383eSMatthias Ringwald break; 57577bbeb3adSMilanka Ringwald default: 57587bbeb3adSMilanka Ringwald return; 57591cfb383eSMatthias Ringwald } 57601cfb383eSMatthias Ringwald event[1] = event_size - 2; 57611cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 57621cfb383eSMatthias Ringwald } 57631cfb383eSMatthias Ringwald } 576435454696SMatthias Ringwald #endif 57653bc639ceSMatthias Ringwald 576671de195eSMatthias Ringwald void hci_emit_state(void){ 57673a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 5768425d1371Smatthias.ringwald uint8_t event[3]; 576980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 57704ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 57713a9fb326S[email protected] event[2] = hci_stack->state; 5772d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5773c8e4258aSmatthias.ringwald } 5774c8e4258aSmatthias.ringwald 577535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 57762deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 5777425d1371Smatthias.ringwald uint8_t event[13]; 5778c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5779425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 578017f1ba2aSmatthias.ringwald event[2] = status; 57812deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 57822deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 5783c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 5784c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 5785d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5786c8e4258aSmatthias.ringwald } 578752db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 578852db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 578952db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 579052db98b2SMatthias Ringwald uint8_t event[4]; 579152db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 579252db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 579352db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 579452db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 579552db98b2SMatthias Ringwald } 579635454696SMatthias Ringwald #endif 5797c8e4258aSmatthias.ringwald 579835454696SMatthias Ringwald #ifdef ENABLE_BLE 5799d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5800667ba9d1SMatthias 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){ 58014f3229d8S[email protected] uint8_t event[21]; 58024f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 58034ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 58044f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 58054f3229d8S[email protected] event[3] = status; 5806fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 58074f3229d8S[email protected] event[6] = 0; // TODO: role 58086e2e9a6bS[email protected] event[7] = address_type; 5809724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 5810f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 5811f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 5812f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 58134f3229d8S[email protected] event[20] = 0; // master clock accuracy 5814d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 58154f3229d8S[email protected] } 581635454696SMatthias Ringwald #endif 5817d70217a2SMatthias Ringwald #endif 58184f3229d8S[email protected] 5819fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 5820fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 5821fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 5822fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 5823fd43c0e0SMatthias Ringwald } 5824fd43c0e0SMatthias Ringwald 5825fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 5826425d1371Smatthias.ringwald uint8_t event[6]; 58273c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 58284ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 58293c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 5830fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 58313c4d4b90Smatthias.ringwald event[5] = reason; 5832d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 58333c4d4b90Smatthias.ringwald } 58343c4d4b90Smatthias.ringwald 5835b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 5836e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 5837425d1371Smatthias.ringwald uint8_t event[3]; 583880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 58394ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 584043bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 5841d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 584243bfb1bdSmatthias.ringwald } 5843038bc64cSmatthias.ringwald 5844b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 5845e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 5846425d1371Smatthias.ringwald uint8_t event[2]; 584780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 58484ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 5849d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5850038bc64cSmatthias.ringwald } 58511b0e3922Smatthias.ringwald 585235454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 585335454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 585435454696SMatthias Ringwald uint8_t event[9]; 585535454696SMatthias Ringwald int pos = 0; 585635454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 58574ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 585835454696SMatthias Ringwald event[pos++] = status; 585935454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 5860d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5861381fbed8Smatthias.ringwald } 5862458bf4e8S[email protected] 586335454696SMatthias Ringwald 586435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 586535454696SMatthias Ringwald 5866b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 5867df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 5868a00031e2S[email protected] uint8_t event[5]; 5869e00caf9cS[email protected] int pos = 0; 58705611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 5871e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 5872f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 5873e00caf9cS[email protected] pos += 2; 5874e00caf9cS[email protected] event[pos++] = level; 5875d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5876e00caf9cS[email protected] } 5877e00caf9cS[email protected] 587835454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 587935454696SMatthias Ringwald if (!connection) return LEVEL_0; 58808daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 5881fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 58828daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 5883170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 58844051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 5885170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 58860e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 5887170fafaeSMatthias Ringwald security_level = LEVEL_3; 5888170fafaeSMatthias Ringwald } 5889170fafaeSMatthias Ringwald return security_level; 589035454696SMatthias Ringwald } 589135454696SMatthias Ringwald 589235454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 589335454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 589435454696SMatthias Ringwald uint8_t event[3]; 589535454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 589635454696SMatthias Ringwald event[1] = sizeof(event) - 2; 589735454696SMatthias Ringwald event[2] = enabled; 5898d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5899ad83dc6aS[email protected] } 5900ad83dc6aS[email protected] 590198a2fd1cSMatthias Ringwald // query if remote side supports eSCO 590220dcdd22SMatthias Ringwald bool hci_remote_esco_supported(hci_con_handle_t con_handle){ 590398a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 590420dcdd22SMatthias Ringwald if (!connection) return false; 590576ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 590698a2fd1cSMatthias Ringwald } 590798a2fd1cSMatthias Ringwald 590867aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 590967aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 591067aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 591167aae551SMatthias Ringwald } 591267aae551SMatthias Ringwald 59132bd8b7e7S[email protected] // query if remote side supports SSP 591420dcdd22SMatthias Ringwald bool hci_remote_ssp_supported(hci_con_handle_t con_handle){ 59152bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 591620dcdd22SMatthias Ringwald if (!connection) return false; 591767aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 59182bd8b7e7S[email protected] } 59192bd8b7e7S[email protected] 592020dcdd22SMatthias Ringwald bool gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 5921df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 5922df3354fcS[email protected] } 5923df3354fcS[email protected] 59246ffc78ceSMatthias Ringwald /** 59256ffc78ceSMatthias Ringwald * Check if remote supported features query has completed 59266ffc78ceSMatthias Ringwald */ 59276ffc78ceSMatthias Ringwald bool hci_remote_features_available(hci_con_handle_t handle){ 59286ffc78ceSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 59296ffc78ceSMatthias Ringwald if (!connection) return false; 59306ffc78ceSMatthias Ringwald return (connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0; 59316ffc78ceSMatthias Ringwald } 59326ffc78ceSMatthias Ringwald 5933d6596031SMatthias Ringwald /** 5934d6596031SMatthias Ringwald * Trigger remote supported features query 5935d6596031SMatthias Ringwald */ 5936c9742cd1SMatthias Ringwald 5937c9742cd1SMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection){ 5938c9742cd1SMatthias Ringwald if ((connection->bonding_flags & (BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_RECEIVED_REMOTE_FEATURES)) == 0){ 5939c9742cd1SMatthias Ringwald connection->bonding_flags |= BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 5940c9742cd1SMatthias Ringwald } 5941c9742cd1SMatthias Ringwald } 5942c9742cd1SMatthias Ringwald 5943d6596031SMatthias Ringwald void hci_remote_features_query(hci_con_handle_t con_handle){ 5944d6596031SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5945d6596031SMatthias Ringwald if (!connection) return; 5946c9742cd1SMatthias Ringwald hci_trigger_remote_features_for_connection(connection); 5947d6596031SMatthias Ringwald hci_run(); 5948d6596031SMatthias Ringwald } 5949d6596031SMatthias Ringwald 5950458bf4e8S[email protected] // GAP API 5951458bf4e8S[email protected] /** 5952458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 5953458bf4e8S[email protected] * @praram enabled 5954458bf4e8S[email protected] */ 59554c57c146S[email protected] void gap_set_bondable_mode(int enable){ 59563a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 5957458bf4e8S[email protected] } 59584ef6443cSMatthias Ringwald /** 59594ef6443cSMatthias Ringwald * @brief Get bondable mode. 59604ef6443cSMatthias Ringwald * @return 1 if bondable 59614ef6443cSMatthias Ringwald */ 59624ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 59634ef6443cSMatthias Ringwald return hci_stack->bondable; 59644ef6443cSMatthias Ringwald } 5965cb230b9dS[email protected] 5966cb230b9dS[email protected] /** 596734d2123cS[email protected] * @brief map link keys to security levels 5968cb230b9dS[email protected] */ 596934d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 597034d2123cS[email protected] switch (link_key_type){ 59713c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 59723c68dfa9S[email protected] return LEVEL_4; 59733c68dfa9S[email protected] case COMBINATION_KEY: 59743c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 59753c68dfa9S[email protected] return LEVEL_3; 59763c68dfa9S[email protected] default: 59773c68dfa9S[email protected] return LEVEL_2; 59783c68dfa9S[email protected] } 5979cb230b9dS[email protected] } 5980cb230b9dS[email protected] 59818b35e16aSMatthias Ringwald /** 59828b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 59838b35e16aSMatthias Ringwald */ 598436c3546bSMatthias Ringwald bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 59858b35e16aSMatthias Ringwald switch (link_key_type){ 59868b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 59878b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 598836c3546bSMatthias Ringwald return true; 59898b35e16aSMatthias Ringwald default: 599036c3546bSMatthias Ringwald return false; 59918b35e16aSMatthias Ringwald } 59928b35e16aSMatthias Ringwald } 59938b35e16aSMatthias Ringwald 59948b35e16aSMatthias Ringwald /** 59958b35e16aSMatthias Ringwald * @brief map link keys to authenticated 59968b35e16aSMatthias Ringwald */ 59977040ba26SMatthias Ringwald bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 59988b35e16aSMatthias Ringwald switch (link_key_type){ 59998b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 60008b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 60017040ba26SMatthias Ringwald return true; 60028b35e16aSMatthias Ringwald default: 60037040ba26SMatthias Ringwald return false; 60048b35e16aSMatthias Ringwald } 60058b35e16aSMatthias Ringwald } 60068b35e16aSMatthias Ringwald 6007552b9260SMatthias Ringwald bool gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 60085127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 6009106d6d11S[email protected] return level > LEVEL_2; 6010106d6d11S[email protected] } 6011106d6d11S[email protected] 601234d2123cS[email protected] /** 601334d2123cS[email protected] * @brief get current security level 601434d2123cS[email protected] */ 601534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 601634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 601734d2123cS[email protected] if (!connection) return LEVEL_0; 601834d2123cS[email protected] return gap_security_level_for_connection(connection); 601934d2123cS[email protected] } 602034d2123cS[email protected] 6021cb230b9dS[email protected] /** 6022cb230b9dS[email protected] * @brief request connection to device to 6023cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 6024cb230b9dS[email protected] */ 602534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 602634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 602734d2123cS[email protected] if (!connection){ 6028a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 602934d2123cS[email protected] return; 603034d2123cS[email protected] } 6031defbf200SMatthias Ringwald 6032defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 6033defbf200SMatthias Ringwald 6034bc00e12cSMatthias 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) 6035bc00e12cSMatthias Ringwald // available on the BR/EDR physical transport require Security Mode 4, Level 4 " 6036bc00e12cSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode && (requested_level != LEVEL_0)){ 6037bc00e12cSMatthias Ringwald requested_level = LEVEL_4; 6038bc00e12cSMatthias Ringwald } 6039bc00e12cSMatthias Ringwald 604034d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 604183d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 604283d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 604383d08d7cSMatthias Ringwald 6044dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 6045dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 6046dbd5dcc3SMatthias Ringwald if (authentication_active){ 60471cf0a6c8SMatthias Ringwald // authentication already active 604883d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 604983d08d7cSMatthias Ringwald // increase requested level as new level is higher 605083d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 605183d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 605283d08d7cSMatthias Ringwald } 60531cf0a6c8SMatthias Ringwald } else { 605483d08d7cSMatthias Ringwald // no request active, notify if security sufficient 605583d08d7cSMatthias Ringwald if (requested_level <= current_level){ 6056a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 605734d2123cS[email protected] return; 605834d2123cS[email protected] } 6059a00031e2S[email protected] 6060e060c07dSMatthias Ringwald // store request 606134d2123cS[email protected] connection->requested_security_level = requested_level; 6062a00031e2S[email protected] 6063e5470e6dSMatthias Ringwald // request remote features if not already active 6064e5470e6dSMatthias Ringwald hci_remote_features_query(con_handle); 6065e5470e6dSMatthias Ringwald 60661cf0a6c8SMatthias Ringwald // start to authenticate connection 60671eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 6068e80b2cf9S[email protected] hci_run(); 6069e00caf9cS[email protected] } 60701cf0a6c8SMatthias Ringwald } 6071ad83dc6aS[email protected] 6072ad83dc6aS[email protected] /** 6073ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 6074ad83dc6aS[email protected] * @param device 6075ad83dc6aS[email protected] * @param request MITM protection 6076ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 6077ad83dc6aS[email protected] */ 6078ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 6079ad83dc6aS[email protected] 6080ad83dc6aS[email protected] // create connection state machine 6081f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 6082ad83dc6aS[email protected] 6083ad83dc6aS[email protected] if (!connection){ 6084ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 6085ad83dc6aS[email protected] } 6086ad83dc6aS[email protected] 6087ad83dc6aS[email protected] // delete linkn key 608815a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 6089ad83dc6aS[email protected] 6090ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 6091ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 6092ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 6093f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 6094ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 6095ad83dc6aS[email protected] 6096ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 6097ad83dc6aS[email protected] 6098ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 6099ad83dc6aS[email protected] // handle: authentication failure 6100ad83dc6aS[email protected] // handle: disconnect on done 6101ad83dc6aS[email protected] 6102ad83dc6aS[email protected] hci_run(); 6103ad83dc6aS[email protected] 6104ad83dc6aS[email protected] return 0; 6105ad83dc6aS[email protected] } 61068e618f72S[email protected] 61078e618f72S[email protected] void gap_set_local_name(const char * local_name){ 61088e618f72S[email protected] hci_stack->local_name = local_name; 6109baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_LOCAL_NAME; 611059d59ecfSMatthias Ringwald // also update EIR if not set by user 611159d59ecfSMatthias Ringwald if (hci_stack->eir_data == NULL){ 6112baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 611359d59ecfSMatthias Ringwald } 611459d59ecfSMatthias Ringwald hci_run(); 61158e618f72S[email protected] } 61161aeec2ebSMatthias Ringwald #endif 61178e618f72S[email protected] 611835454696SMatthias Ringwald 611935454696SMatthias Ringwald #ifdef ENABLE_BLE 612035454696SMatthias Ringwald 6121d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6122d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 6123fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 61247bdc6798S[email protected] hci_run(); 61257bdc6798S[email protected] } 61268e618f72S[email protected] 6127d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 6128fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 61297bdc6798S[email protected] hci_run(); 61307bdc6798S[email protected] } 61314f3229d8S[email protected] 6132a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 6133ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 6134a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 6135ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 6136ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 61378b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 6138ef11999fSmatthias.ringwald hci_run(); 6139ef11999fSmatthias.ringwald } 61404f3229d8S[email protected] 6141a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 6142a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 6143a7a719e9SMatthias Ringwald } 6144a7a719e9SMatthias Ringwald 6145667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 61464f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 61474f3229d8S[email protected] if (!conn){ 6148d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 6149d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 6150d32b3f05SMatthias Ringwald log_error("le connection already active"); 6151d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6152d32b3f05SMatthias Ringwald } 6153d32b3f05SMatthias Ringwald 6154d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 61552e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 61564f3229d8S[email protected] if (!conn){ 61574f3229d8S[email protected] // notify client that alloc failed 61586e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 6159d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 6160472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 61614f3229d8S[email protected] } 6162d5b1a89eSMatthias Ringwald 6163d5b1a89eSMatthias Ringwald // set le connecting state 6164d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 6165d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 6166d5b1a89eSMatthias Ringwald } 6167d5b1a89eSMatthias Ringwald 61684f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 6169d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 6170564fca32S[email protected] hci_run(); 6171b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 61724f3229d8S[email protected] } 61730bf6344aS[email protected] 61740bf6344aS[email protected] if (!hci_is_le_connection(conn) || 6175a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 61760e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 61772e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 6178d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 6179616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 61800bf6344aS[email protected] } 61810bf6344aS[email protected] 6182b0136355SMatthias Ringwald // check if connection was just disconnected 6183b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 6184b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 6185b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 6186b0136355SMatthias Ringwald hci_run(); 6187b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 6188b0136355SMatthias Ringwald } 6189b0136355SMatthias Ringwald 6190d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 6191d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 61924f3229d8S[email protected] hci_run(); 6193b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 61944f3229d8S[email protected] } 61954f3229d8S[email protected] 61967851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 6197d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 6198665d90f2SMatthias Ringwald btstack_linked_item_t *it; 6199a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 62000bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 62010bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 62020bf6344aS[email protected] switch (conn->state){ 6203a6725849S[email protected] case SEND_CREATE_CONNECTION: 62047851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 62059c77c9dbSMatthias Ringwald case SENT_CANCEL_CONNECTION: 62067851196eSmatthias.ringwald return conn; 62077851196eSmatthias.ringwald default: 62087851196eSmatthias.ringwald break; 62097851196eSmatthias.ringwald }; 62107851196eSmatthias.ringwald } 62117851196eSmatthias.ringwald return NULL; 62127851196eSmatthias.ringwald } 62137851196eSmatthias.ringwald 6214d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 6215d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 6216616edd56SMatthias Ringwald if (!conn) return 0; 62177851196eSmatthias.ringwald switch (conn->state){ 62187851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 62197851196eSmatthias.ringwald // skip sending create connection and emit event instead 622010f8f469SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 62212e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 6222665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 62237851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 62240bf6344aS[email protected] break; 6225a6725849S[email protected] case SENT_CREATE_CONNECTION: 62267851196eSmatthias.ringwald // request to send cancel connection 62270bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 62280bf6344aS[email protected] hci_run(); 62290bf6344aS[email protected] break; 62300bf6344aS[email protected] default: 62310bf6344aS[email protected] break; 62320bf6344aS[email protected] } 6233616edd56SMatthias Ringwald return 0; 6234e31f89a7S[email protected] } 6235d70217a2SMatthias Ringwald #endif 62364f3229d8S[email protected] 623713e645d2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6238c37a3166S[email protected] /** 62396012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 6240cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 6241cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 62426012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 62436012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 62446012052bSMatthias Ringwald * @param conn_latency, default: 4 62456012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 62466012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 62476012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 62486012052bSMatthias Ringwald */ 62496012052bSMatthias Ringwald 6250cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 6251cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 6252cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 6253cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 6254cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 62556012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 62566012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 62576012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 62586012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 62596012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 62606012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 62616012052bSMatthias Ringwald } 626213e645d2SMatthias Ringwald #endif 62636012052bSMatthias Ringwald 62646012052bSMatthias Ringwald /** 6265c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 6266c37a3166S[email protected] * @param handle 6267c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 6268c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 6269c37a3166S[email protected] * @param conn_latency 6270c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 62716b65794dSMilanka Ringwald * @return 0 if ok 6272c37a3166S[email protected] */ 6273c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 6274c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 6275c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 6276c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6277c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 6278c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 6279c37a3166S[email protected] connection->le_conn_latency = conn_latency; 6280c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 628184cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 6282cfc59f1bSMatthias Ringwald hci_run(); 6283c37a3166S[email protected] return 0; 6284c37a3166S[email protected] } 6285c37a3166S[email protected] 628645c102fdSMatthias Ringwald /** 6287b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 6288b68d7bc3SMatthias Ringwald * @param handle 6289b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 6290b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 6291b68d7bc3SMatthias Ringwald * @param conn_latency 6292b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 62936b65794dSMilanka Ringwald * @return 0 if ok 6294b68d7bc3SMatthias Ringwald */ 6295b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 6296b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 6297b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 6298b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6299b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 6300b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 6301b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 6302b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 6303b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 630409c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 630509c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 6306b68d7bc3SMatthias Ringwald return 0; 6307b68d7bc3SMatthias Ringwald } 6308b68d7bc3SMatthias Ringwald 6309d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 6310d70217a2SMatthias Ringwald 6311b68d7bc3SMatthias Ringwald /** 631245c102fdSMatthias Ringwald * @brief Set Advertisement Data 631345c102fdSMatthias Ringwald * @param advertising_data_length 631445c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 631545c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 631645c102fdSMatthias Ringwald */ 631745c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 631845c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 631945c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 6320501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 6321bbc366e6SMatthias Ringwald hci_run(); 632245c102fdSMatthias Ringwald } 6323501f56b3SMatthias Ringwald 6324501f56b3SMatthias Ringwald /** 6325501f56b3SMatthias Ringwald * @brief Set Scan Response Data 6326501f56b3SMatthias Ringwald * @param advertising_data_length 6327501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 6328501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 6329501f56b3SMatthias Ringwald */ 6330501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 6331501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 6332501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 6333501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 6334bbc366e6SMatthias Ringwald hci_run(); 633545c102fdSMatthias Ringwald } 633645c102fdSMatthias Ringwald 633745c102fdSMatthias Ringwald /** 633845c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 633945c102fdSMatthias Ringwald * @param adv_int_min 634045c102fdSMatthias Ringwald * @param adv_int_max 634145c102fdSMatthias Ringwald * @param adv_type 634245c102fdSMatthias Ringwald * @param direct_address_type 634345c102fdSMatthias Ringwald * @param direct_address 634445c102fdSMatthias Ringwald * @param channel_map 634545c102fdSMatthias Ringwald * @param filter_policy 634645c102fdSMatthias Ringwald * 634745c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 634845c102fdSMatthias Ringwald */ 634945c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 6350b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 635145c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 635245c102fdSMatthias Ringwald 635345c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 635445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 635545c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 635645c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 635745c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 635845c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 63596535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 63606535961aSMatthias Ringwald 6); 636145c102fdSMatthias Ringwald 6362a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS | LE_ADVERTISEMENT_TASKS_PARAMS_SET; 6363bbc366e6SMatthias Ringwald hci_run(); 636445c102fdSMatthias Ringwald } 636545c102fdSMatthias Ringwald 636645c102fdSMatthias Ringwald /** 636745c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 636845c102fdSMatthias Ringwald * @param enabled 636945c102fdSMatthias Ringwald */ 637045c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 63715226d7f2SMatthias Ringwald hci_stack->le_advertisements_enabled = enabled != 0; 6372bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 6373cfc59f1bSMatthias Ringwald hci_run(); 637445c102fdSMatthias Ringwald } 637545c102fdSMatthias Ringwald 637635454696SMatthias Ringwald #endif 637706e5cf96SMatthias Ringwald 637806e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 637906e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 638006e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 638106e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 638206e5cf96SMatthias Ringwald 638364068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 638406e5cf96SMatthias Ringwald // update advertisement parameters, too 638506e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 6386bbc366e6SMatthias Ringwald hci_run(); 638764068776SMatthias Ringwald #endif 638864068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 638906e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 639064068776SMatthias Ringwald #endif 639106e5cf96SMatthias Ringwald } 639206e5cf96SMatthias Ringwald 63939136bde8SMatthias Ringwald void hci_le_random_address_set(const bd_addr_t random_address){ 63949136bde8SMatthias Ringwald memcpy(hci_stack->le_random_address, random_address, 6); 63959136bde8SMatthias Ringwald hci_stack->le_random_address_set = true; 63969136bde8SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 63979136bde8SMatthias Ringwald hci_run(); 63989136bde8SMatthias Ringwald } 63999136bde8SMatthias Ringwald 6400d70217a2SMatthias Ringwald #endif 640145c102fdSMatthias Ringwald 6402616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 64035917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 64045917a5c5S[email protected] if (!conn){ 64057851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 6406616edd56SMatthias Ringwald return 0; 64075917a5c5S[email protected] } 64087fd7aa6fSMatthias Ringwald // ignore if already disconnected 64097fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 64107fd7aa6fSMatthias Ringwald return 0; 64117fd7aa6fSMatthias Ringwald } 64125917a5c5S[email protected] conn->state = SEND_DISCONNECT; 64135917a5c5S[email protected] hci_run(); 6414616edd56SMatthias Ringwald return 0; 64154f3229d8S[email protected] } 641604a6ef8cSmatthias.ringwald 6417228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 6418228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6419228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 64208daf94bcSMatthias Ringwald connectionSetAuthenticationFlags(hci_connection, AUTH_FLAG_READ_RSSI); 6421228e430cSMatthias Ringwald hci_run(); 6422228e430cSMatthias Ringwald return 1; 6423228e430cSMatthias Ringwald } 6424228e430cSMatthias Ringwald 6425a1bf5ae7SMatthias Ringwald /** 6426a1bf5ae7SMatthias Ringwald * @brief Get connection type 6427a1bf5ae7SMatthias Ringwald * @param con_handle 6428a1bf5ae7SMatthias Ringwald * @result connection_type 6429a1bf5ae7SMatthias Ringwald */ 6430a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 6431a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 6432a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6433a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 6434a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 6435a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6436a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 6437a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 6438a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 6439f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6440a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 6441a1bf5ae7SMatthias Ringwald default: 6442a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 6443a1bf5ae7SMatthias Ringwald } 6444a1bf5ae7SMatthias Ringwald } 6445a1bf5ae7SMatthias Ringwald 64462dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 64472dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 64482dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 64492dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 64502dceb1d6SMatthias Ringwald } 64512dceb1d6SMatthias Ringwald 64522dceb1d6SMatthias Ringwald 645344f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 6454667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 645588a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 645688a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 645788a03c8dSMatthias Ringwald conn->request_role = role; 645888a03c8dSMatthias Ringwald hci_run(); 6459d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 646088a03c8dSMatthias Ringwald } 646144f858f3SMatthias Ringwald #endif 646288a03c8dSMatthias Ringwald 6463a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 64644f551432SMatthias Ringwald 6465b45b7749SMilanka 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){ 6466b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6467b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6468b90f6e0aSMatthias Ringwald 6469b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 6470b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 6471b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 6472b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 6473b90f6e0aSMatthias Ringwald 6474b90f6e0aSMatthias Ringwald hci_run(); 6475b90f6e0aSMatthias Ringwald 6476b90f6e0aSMatthias Ringwald return 0; 6477b90f6e0aSMatthias Ringwald } 6478b90f6e0aSMatthias Ringwald 6479667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 64807a92a9dbSMatthias Ringwald // check if already in list 64817a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 64827a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 64837a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 64847a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 64857a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 64867a92a9dbSMatthias Ringwald continue; 64877a92a9dbSMatthias Ringwald } 64887a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 64897a92a9dbSMatthias Ringwald continue; 64907a92a9dbSMatthias Ringwald } 6491287379ccSMatthias Ringwald // disallow if already scheduled to add 6492287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 64937a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 64947a92a9dbSMatthias Ringwald } 6495287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 6496287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 6497287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 6498287379ccSMatthias Ringwald } 64997a92a9dbSMatthias Ringwald // alloc and add to list 6500e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 6501e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 6502e83201bcSMatthias Ringwald entry->address_type = address_type; 65036535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 6504e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 6505665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 6506226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 6507ac9c45e0SMatthias Ringwald } 6508ac9c45e0SMatthias Ringwald 6509667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6510665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6511665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 6512665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6513665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 65147a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 65157a92a9dbSMatthias Ringwald continue; 65167a92a9dbSMatthias Ringwald } 65177a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 65187a92a9dbSMatthias Ringwald continue; 65197a92a9dbSMatthias Ringwald } 6520e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 6521e83201bcSMatthias Ringwald // remove from controller if already present 6522e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6523a3b69fdeSMatthias Ringwald } else { 6524226db5efSMatthias Ringwald // directly remove entry from whitelist 6525665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6526e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 6527e83201bcSMatthias Ringwald } 6528a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6529a3b69fdeSMatthias Ringwald } 6530a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 653142ff5ba1SMatthias Ringwald } 653242ff5ba1SMatthias Ringwald 6533226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 6534665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6535665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 6536665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6537665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 6538e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 6539e83201bcSMatthias Ringwald // remove from controller if already present 6540e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6541e83201bcSMatthias Ringwald continue; 6542e83201bcSMatthias Ringwald } 654391915b0bSMatthias Ringwald // directly remove entry from whitelist 6544665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6545e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 6546e83201bcSMatthias Ringwald } 6547226db5efSMatthias Ringwald } 6548226db5efSMatthias Ringwald 654983dc1a98SMatthias Ringwald // free all entries unconditionally 655083dc1a98SMatthias Ringwald static void hci_whitelist_free(void){ 655183dc1a98SMatthias Ringwald btstack_linked_list_iterator_t lit; 655283dc1a98SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 655383dc1a98SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 655483dc1a98SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 655583dc1a98SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 655683dc1a98SMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 655783dc1a98SMatthias Ringwald } 655883dc1a98SMatthias Ringwald } 655983dc1a98SMatthias Ringwald 6560a3b69fdeSMatthias Ringwald /** 6561a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 65626b65794dSMilanka Ringwald * @return 0 if ok 6563a3b69fdeSMatthias Ringwald */ 6564a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 6565a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 6566a3b69fdeSMatthias Ringwald hci_run(); 6567a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6568a3b69fdeSMatthias Ringwald } 6569a3b69fdeSMatthias Ringwald 6570a3b69fdeSMatthias Ringwald /** 6571a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 6572a3b69fdeSMatthias Ringwald * @param address_typ 6573a3b69fdeSMatthias Ringwald * @param address 65746b65794dSMilanka Ringwald * @return 0 if ok 6575a3b69fdeSMatthias Ringwald */ 6576667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 6577a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 6578a3b69fdeSMatthias Ringwald if (status){ 6579a3b69fdeSMatthias Ringwald return status; 6580a3b69fdeSMatthias Ringwald } 6581a3b69fdeSMatthias Ringwald hci_run(); 6582a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6583a3b69fdeSMatthias Ringwald } 6584a3b69fdeSMatthias Ringwald 6585a3b69fdeSMatthias Ringwald /** 6586a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 6587a3b69fdeSMatthias Ringwald * @param address_typ 6588a3b69fdeSMatthias Ringwald * @param address 65896b65794dSMilanka Ringwald * @return 0 if ok 6590a3b69fdeSMatthias Ringwald */ 6591667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6592a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 6593a3b69fdeSMatthias Ringwald if (status){ 6594a3b69fdeSMatthias Ringwald return status; 6595a3b69fdeSMatthias Ringwald } 6596a3b69fdeSMatthias Ringwald hci_run(); 6597a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6598a3b69fdeSMatthias Ringwald } 6599a3b69fdeSMatthias Ringwald 6600226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6601226db5efSMatthias Ringwald /** 660295e257d9SMatthias Ringwald * @brief Connect with Whitelist 660395e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 66046b65794dSMilanka Ringwald * @return - if ok 660595e257d9SMatthias Ringwald */ 660695e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 660795e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 660895e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 660995e257d9SMatthias Ringwald } 661095e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 661195e257d9SMatthias Ringwald hci_run(); 661295e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 661395e257d9SMatthias Ringwald } 661495e257d9SMatthias Ringwald 661595e257d9SMatthias Ringwald /** 6616226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 6617226db5efSMatthias Ringwald * @param address_typ 6618226db5efSMatthias Ringwald * @param address 66196b65794dSMilanka Ringwald * @return 0 if ok 6620226db5efSMatthias Ringwald */ 6621667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 662295e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 6623226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6624226db5efSMatthias Ringwald } 6625226db5efSMatthias Ringwald 6626226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 662763f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 6628226db5efSMatthias Ringwald return status; 6629226db5efSMatthias Ringwald } 6630226db5efSMatthias Ringwald 6631226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 6632226db5efSMatthias Ringwald 6633226db5efSMatthias Ringwald hci_run(); 663495e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6635226db5efSMatthias Ringwald } 6636226db5efSMatthias Ringwald 6637226db5efSMatthias Ringwald /** 6638226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 6639226db5efSMatthias Ringwald * @param address_typ 6640226db5efSMatthias Ringwald * @param address 66416b65794dSMilanka Ringwald * @return 0 if ok 6642226db5efSMatthias Ringwald */ 6643667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 664495e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 664595e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 664695e257d9SMatthias Ringwald } 664795e257d9SMatthias Ringwald 6648226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 664995e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 665095e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 665195e257d9SMatthias Ringwald } 6652226db5efSMatthias Ringwald hci_run(); 6653226db5efSMatthias Ringwald return 0; 6654226db5efSMatthias Ringwald } 6655226db5efSMatthias Ringwald 6656226db5efSMatthias Ringwald /** 6657226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 6658226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 6659226db5efSMatthias Ringwald */ 666095e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 666195e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 666295e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 666395e257d9SMatthias Ringwald } 6664226db5efSMatthias Ringwald hci_whitelist_clear(); 6665226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 6666e83201bcSMatthias Ringwald hci_run(); 666795e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6668ac9c45e0SMatthias Ringwald } 6669c9db5c21SMilanka Ringwald 6670b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 6671b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6672c9db5c21SMilanka Ringwald if (!conn) return 0; 6673c9db5c21SMilanka Ringwald return conn->le_connection_interval; 6674c9db5c21SMilanka Ringwald } 6675d70217a2SMatthias Ringwald #endif 66764f551432SMatthias Ringwald #endif 66774f551432SMatthias Ringwald 667835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6679ac9c45e0SMatthias Ringwald /** 6680ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 6681a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 6682ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 6683ff00ed1cSMatthias Ringwald */ 6684ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 6685ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 6686baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 668759d59ecfSMatthias Ringwald hci_run(); 6688ff00ed1cSMatthias Ringwald } 6689ff00ed1cSMatthias Ringwald 6690ff00ed1cSMatthias Ringwald /** 6691f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 6692f5875de5SMatthias Ringwald * @param duration in 1.28s units 6693f5875de5SMatthias Ringwald * @return 0 if ok 6694f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 6695f5875de5SMatthias Ringwald */ 6696f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 669799449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 6698f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6699a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 6700f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 6701f5875de5SMatthias Ringwald } 6702f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 6703f5875de5SMatthias Ringwald hci_run(); 6704f5875de5SMatthias Ringwald return 0; 6705f5875de5SMatthias Ringwald } 6706f5875de5SMatthias Ringwald 6707f5875de5SMatthias Ringwald /** 6708f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 67096b65794dSMilanka Ringwald * @return 0 if ok 6710f5875de5SMatthias Ringwald */ 6711f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 6712a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 6713f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 6714f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 6715f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 6716f5875de5SMatthias Ringwald return 0; 6717f5875de5SMatthias Ringwald } 6718f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 6719f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 6720f5875de5SMatthias Ringwald hci_run(); 6721f5875de5SMatthias Ringwald return 0; 6722f5875de5SMatthias Ringwald } 6723f5875de5SMatthias Ringwald 6724496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 6725496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 6726496bb884SMatthias Ringwald } 6727496bb884SMatthias Ringwald 67281dc973e7SMatthias Ringwald void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window){ 6729c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_interval = inquiry_scan_interval; 6730c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_window = inquiry_scan_window; 6731baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 6732c0c8a829SMatthias Ringwald hci_run(); 6733c0c8a829SMatthias Ringwald } 6734c0c8a829SMatthias Ringwald 6735b7f1ee76SMatthias Ringwald 6736b7f1ee76SMatthias Ringwald /** 6737b7f1ee76SMatthias Ringwald * @brief Remote Name Request 6738b7f1ee76SMatthias Ringwald * @param addr 6739b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 6740b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 6741b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 6742b7f1ee76SMatthias Ringwald */ 6743667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 6744b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 67456535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 6746b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 6747b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 6748b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 6749b7f1ee76SMatthias Ringwald hci_run(); 6750b7f1ee76SMatthias Ringwald return 0; 6751b7f1ee76SMatthias Ringwald } 6752b7f1ee76SMatthias Ringwald 6753667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 67540a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 67556535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 67560a51f88bSMatthias Ringwald hci_run(); 67570a51f88bSMatthias Ringwald return 0; 67580a51f88bSMatthias Ringwald } 67590a51f88bSMatthias Ringwald 67600a51f88bSMatthias Ringwald /** 6761aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 6762aad97216SMatthias Ringwald * @param addr 6763aad97216SMatthias Ringwald * @param pin_data 6764aad97216SMatthias Ringwald * @param pin_len 6765aad97216SMatthias Ringwald * @return 0 if ok 6766aad97216SMatthias Ringwald */ 6767667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 6768aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6769aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 6770aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 6771aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 6772aad97216SMatthias Ringwald } 6773aad97216SMatthias Ringwald 6774aad97216SMatthias Ringwald /** 67750a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 67760a51f88bSMatthias Ringwald * @param addr 67770a51f88bSMatthias Ringwald * @param pin 67780a51f88bSMatthias Ringwald * @return 0 if ok 67790a51f88bSMatthias Ringwald */ 6780667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 6781aad97216SMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, strlen(pin)); 67820a51f88bSMatthias Ringwald } 67830a51f88bSMatthias Ringwald 67840a51f88bSMatthias Ringwald /** 67850a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 67860a51f88bSMatthias Ringwald * @param addr 67870a51f88bSMatthias Ringwald * @param pin 67880a51f88bSMatthias Ringwald * @return 0 if ok 67890a51f88bSMatthias Ringwald */ 67900a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 6791cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 67920a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 67930a51f88bSMatthias Ringwald } 67940a51f88bSMatthias Ringwald 67950a51f88bSMatthias Ringwald /** 67960a51f88bSMatthias Ringwald * @brief SSP Passkey Response 67970a51f88bSMatthias Ringwald * @param addr 67980a51f88bSMatthias Ringwald * @param passkey 67990a51f88bSMatthias Ringwald * @return 0 if ok 68000a51f88bSMatthias Ringwald */ 6801667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 6802cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6803d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 68040a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 68050a51f88bSMatthias Ringwald } 68060a51f88bSMatthias Ringwald 68070a51f88bSMatthias Ringwald /** 68080a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 68090a51f88bSMatthias Ringwald * @param addr 68100a51f88bSMatthias Ringwald * @param pin 68110a51f88bSMatthias Ringwald * @return 0 if ok 68120a51f88bSMatthias Ringwald */ 6813667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 6814cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 68150a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 68160a51f88bSMatthias Ringwald } 68170a51f88bSMatthias Ringwald 68180a51f88bSMatthias Ringwald /** 68190a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 68200a51f88bSMatthias Ringwald * @param addr 68210a51f88bSMatthias Ringwald * @param passkey 68220a51f88bSMatthias Ringwald * @return 0 if ok 68230a51f88bSMatthias Ringwald */ 6824667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 6825cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 68260a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 68270a51f88bSMatthias Ringwald } 68280a51f88bSMatthias Ringwald 68290a51f88bSMatthias Ringwald /** 68300a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 68310a51f88bSMatthias Ringwald * @param addr 68320a51f88bSMatthias Ringwald * @param pin 68330a51f88bSMatthias Ringwald * @return 0 if ok 68340a51f88bSMatthias Ringwald */ 6835667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 6836cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 68370a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 68380a51f88bSMatthias Ringwald } 68390a51f88bSMatthias Ringwald 6840308eeaffSMatthias Ringwald #if defined(ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY) || defined(ENABLE_EXPLICIT_LINK_KEY_REPLY) 684144565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 684244565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 684344565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 684444565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 684544565b0cSMatthias Ringwald hci_run(); 684644565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 684744565b0cSMatthias Ringwald } 6848308eeaffSMatthias Ringwald #endif 684944565b0cSMatthias Ringwald 6850308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 685144565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 68527ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 685344565b0cSMatthias Ringwald } 685444565b0cSMatthias Ringwald 685544565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 68567ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 685744565b0cSMatthias Ringwald } 685844565b0cSMatthias Ringwald #endif 685944565b0cSMatthias Ringwald 68601849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 68611849becdSMatthias Ringwald /** 68621849becdSMatthias Ringwald * @brief Report Remote OOB Data 68631849becdSMatthias Ringwald * @param bd_addr 68641849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 68651849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 68661849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 68671849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 68681849becdSMatthias Ringwald */ 68691849becdSMatthias 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){ 68701849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 68711849becdSMatthias Ringwald if (connection == NULL) { 68721849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 68731849becdSMatthias Ringwald } 68741849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 68751849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 6876204e8f1dSMatthias Ringwald 6877204e8f1dSMatthias Ringwald // ignore P-256 if not supported by us 6878204e8f1dSMatthias Ringwald if (hci_stack->secure_connections_active){ 68791849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 68801849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 6881204e8f1dSMatthias Ringwald } 6882204e8f1dSMatthias Ringwald 68831849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 68841849becdSMatthias Ringwald } 6885cf01e888SMatthias Ringwald /** 6886cf01e888SMatthias Ringwald * @brief Generate new OOB data 6887cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 6888cf01e888SMatthias Ringwald */ 6889cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 6890cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 6891cf01e888SMatthias Ringwald hci_run(); 6892cf01e888SMatthias Ringwald } 6893cf01e888SMatthias Ringwald 68941849becdSMatthias Ringwald #endif 68951849becdSMatthias Ringwald 6896308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_LINK_KEY_REPLY 6897308eeaffSMatthias Ringwald uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 6898308eeaffSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 6899308eeaffSMatthias Ringwald if (connection == NULL) { 6900308eeaffSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6901308eeaffSMatthias Ringwald } 6902308eeaffSMatthias Ringwald 6903308eeaffSMatthias Ringwald memcpy(connection->link_key, link_key, sizeof(link_key_t)); 6904308eeaffSMatthias Ringwald connection->link_key_type = type; 6905308eeaffSMatthias Ringwald 6906308eeaffSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 6907308eeaffSMatthias Ringwald } 6908308eeaffSMatthias Ringwald 6909308eeaffSMatthias Ringwald #endif // ENABLE_EXPLICIT_LINK_KEY_REPLY 6910f5875de5SMatthias Ringwald /** 6911f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 6912f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 6913f6858d14SMatthias Ringwald */ 6914b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 6915b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 6916f6858d14SMatthias Ringwald } 6917f6858d14SMatthias Ringwald 6918f6858d14SMatthias Ringwald /** 6919d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 6920d950d659SMatthias Ringwald */ 6921d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 6922d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 6923d950d659SMatthias Ringwald } 6924d950d659SMatthias Ringwald 6925d950d659SMatthias Ringwald /** 6926d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 6927d950d659SMatthias Ringwald * @return current voice setting 6928d950d659SMatthias Ringwald */ 69290cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 6930d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 6931d950d659SMatthias Ringwald } 6932d950d659SMatthias Ringwald 6933400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 6934400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 6935400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 6936400ff5abSMatthias Ringwald if (!transport_name) return 0; 6937400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 6938400ff5abSMatthias Ringwald } 6939400ff5abSMatthias Ringwald 6940b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 6941b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 6942b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 6943b3aad8daSMatthias Ringwald */ 694420dcdd22SMatthias Ringwald uint16_t hci_get_sco_packet_length(void){ 694520dcdd22SMatthias Ringwald uint16_t sco_packet_length = 0; 6946cf119f3bSMatthias Ringwald 69471e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 69486f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 69491431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 6950cf119f3bSMatthias Ringwald 6951400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 6952400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 6953cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 695452e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 695552e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 6956400ff5abSMatthias Ringwald } else { 6957400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 6958400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 6959d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 6960d966a453SMatthias Ringwald if (sco_packet_length > hci_stack->sco_data_packet_length){ 6961d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 6962d966a453SMatthias Ringwald } 6963400ff5abSMatthias Ringwald } 6964a3069afeSMatthias Ringwald #endif 69655b7087c7SMatthias Ringwald 69665b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 69675b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 69681e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 69695b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 69705b7087c7SMatthias Ringwald #endif 6971a3069afeSMatthias Ringwald return sco_packet_length; 6972b3aad8daSMatthias Ringwald } 6973b3aad8daSMatthias Ringwald 6974c4c88f1bSJakob Krantz /** 6975c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 6976c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 6977c4c88f1bSJakob Krantz */ 6978c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 6979c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 6980c4c88f1bSJakob Krantz } 6981c4c88f1bSJakob Krantz 6982c4c88f1bSJakob Krantz #endif 6983ec111c8bSMatthias Ringwald 6984ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 6985ec111c8bSMatthias Ringwald return hci_stack->state; 6986ec111c8bSMatthias Ringwald } 6987ec111c8bSMatthias Ringwald 698807e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 69895e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 699007e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 699107e010b6SMilanka Ringwald } 699207e010b6SMilanka Ringwald #endif 6993ec111c8bSMatthias Ringwald 6994d950d659SMatthias Ringwald /** 6995d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 6996d23838ecSMatthias Ringwald */ 6997c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 6998d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 6999d23838ecSMatthias Ringwald } 7000d23838ecSMatthias Ringwald 700171de195eSMatthias Ringwald void hci_disconnect_all(void){ 7002665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 7003665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 7004665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7005665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 700604a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 700704a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 700804a6ef8cSmatthias.ringwald } 7009d31fba26S[email protected] hci_run(); 701004a6ef8cSmatthias.ringwald } 701133373e40SMatthias Ringwald 701233373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 701333373e40SMatthias Ringwald return hci_stack->manufacturer; 701433373e40SMatthias Ringwald } 70159c6e867eSMatthias Ringwald 70163e329ddfSandryblack #ifdef ENABLE_BLE 70179c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 70189c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 70199c6e867eSMatthias Ringwald if (!hci_con) return NULL; 70209c6e867eSMatthias Ringwald return &hci_con->sm_connection; 70219c6e867eSMatthias Ringwald } 70229c6e867eSMatthias Ringwald 70239c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 70249c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 70256a79f6baSMatthias Ringwald #endif 70269c6e867eSMatthias Ringwald 70276d105c71SMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 7028c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 7029c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 7030c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 70316a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 7032c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 7033c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 70349c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 70359c6e867eSMatthias Ringwald } 70366a79f6baSMatthias Ringwald #endif 70376a79f6baSMatthias Ringwald } else { 7038c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 70398daf94bcSMatthias Ringwald if ((hci_connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED)){ 7040c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 7041c5fb5ca4SMatthias Ringwald } 7042c5fb5ca4SMatthias Ringwald #endif 70436a79f6baSMatthias Ringwald } 7044c5fb5ca4SMatthias Ringwald return 0; 7045c5fb5ca4SMatthias Ringwald } 70469c6e867eSMatthias Ringwald 7047ff3f1026SMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 7048c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 7049ff3f1026SMatthias Ringwald if (hci_connection == NULL) return false; 70505f3981bfSMatthias Ringwald 7051c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 70526a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 70535f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 70545f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 7055c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 7056ff3f1026SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated != 0; 70576a79f6baSMatthias Ringwald #endif 705859a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 70595f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 7060f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 7061ff3f1026SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 706259a1a47aSMatthias Ringwald #endif 70635f3981bfSMatthias Ringwald default: 7064ff3f1026SMatthias Ringwald return false; 70655f3981bfSMatthias Ringwald } 70669c6e867eSMatthias Ringwald } 70679c6e867eSMatthias Ringwald 7068f461b3dfSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 7069c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 7070c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 70718b35e16aSMatthias Ringwald 7072c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 70736a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 70748b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 70758b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 7076f461b3dfSMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return false; // unencrypted connection cannot be authenticated 7077f461b3dfSMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc != 0; 70786a79f6baSMatthias Ringwald #endif 707959a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 70808b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 7081f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 7082f461b3dfSMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 708359a1a47aSMatthias Ringwald #endif 70848b35e16aSMatthias Ringwald default: 7085f461b3dfSMatthias Ringwald return false; 70868b35e16aSMatthias Ringwald } 7087f1dfbe18SMatthias Ringwald } 7088f1dfbe18SMatthias Ringwald 70891e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 70901e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 70911e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 70921e122704SMatthias Ringwald 709348f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 70941e122704SMatthias Ringwald link_key_t link_key; 70951e122704SMatthias Ringwald link_key_type_t link_key_type; 709648f33f37SMatthias Ringwald #endif 70971e122704SMatthias Ringwald switch (hci_connection->address_type){ 70986a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 70991e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 71001e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 71011e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 71026a79f6baSMatthias Ringwald #endif 71031e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 71041e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 71051e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 71061e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 71071e122704SMatthias Ringwald #endif 71081e122704SMatthias Ringwald default: 71091e122704SMatthias Ringwald return false; 71101e122704SMatthias Ringwald } 71111e122704SMatthias Ringwald } 71121e122704SMatthias Ringwald 71136a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 71149c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 71159c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 71169c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 71179c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 71189c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 71199c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 71209c6e867eSMatthias Ringwald } 71219c6e867eSMatthias Ringwald #endif 7122f8ee3071SMatthias Ringwald 7123f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 7124f8ee3071SMatthias 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){ 7125f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7126f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7127f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 7128f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 7129f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 7130f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 7131f8ee3071SMatthias Ringwald hci_run(); 7132f8ee3071SMatthias Ringwald return 0; 7133f8ee3071SMatthias Ringwald } 7134f8ee3071SMatthias Ringwald 7135f8ee3071SMatthias Ringwald /** 7136f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 7137f8ee3071SMatthias Ringwald * @param con_handle 7138f8ee3071SMatthias Ringwald @ @return 0 if ok 7139f8ee3071SMatthias Ringwald */ 7140f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 7141f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7142f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7143f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 7144f8ee3071SMatthias Ringwald hci_run(); 7145f8ee3071SMatthias Ringwald return 0; 7146f8ee3071SMatthias Ringwald } 7147bea424a5SMatthias Ringwald 7148140c0557SMatthias 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){ 7149140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7150140c0557SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7151140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 7152140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 7153140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 7154140c0557SMatthias Ringwald hci_run(); 7155dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 7156140c0557SMatthias Ringwald } 7157140c0557SMatthias Ringwald 7158278ff8a9SMatthias 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){ 7159278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7160278ff8a9SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7161278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 7162278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 7163278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 7164278ff8a9SMatthias Ringwald conn->qos_latency = latency; 7165278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 7166278ff8a9SMatthias Ringwald hci_run(); 7167278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 7168278ff8a9SMatthias Ringwald } 7169278ff8a9SMatthias Ringwald 7170bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 7171bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 7172bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 7173baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 7174bea424a5SMatthias Ringwald hci_run(); 7175bea424a5SMatthias Ringwald } 7176bea424a5SMatthias Ringwald 7177bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 7178bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 7179baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_TYPE; 7180bea424a5SMatthias Ringwald hci_run(); 7181bea424a5SMatthias Ringwald } 7182bea424a5SMatthias Ringwald 718332a12730SMatthias Ringwald void gap_set_page_timeout(uint16_t page_timeout){ 718432a12730SMatthias Ringwald hci_stack->page_timeout = page_timeout; 7185baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_TIMEOUT; 718632a12730SMatthias Ringwald hci_run(); 718732a12730SMatthias Ringwald } 718832a12730SMatthias Ringwald 7189f8ee3071SMatthias Ringwald #endif 7190beceeddeSMatthias Ringwald 7191beceeddeSMatthias Ringwald void hci_halting_defer(void){ 7192beceeddeSMatthias Ringwald if (hci_stack->state != HCI_STATE_HALTING) return; 7193beceeddeSMatthias Ringwald switch (hci_stack->substate){ 7194beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 7195beceeddeSMatthias Ringwald case HCI_HALTING_CLOSE: 7196beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_TIMER; 7197beceeddeSMatthias Ringwald break; 7198beceeddeSMatthias Ringwald default: 7199beceeddeSMatthias Ringwald break; 7200beceeddeSMatthias Ringwald } 7201beceeddeSMatthias Ringwald } 7202eddac615SMatthias Ringwald 720321debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 720421debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 720521debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 720621debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 720721debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 720821debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 720902b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 721021debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 721102b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 721202b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 721302b02cffSMatthias Ringwald } 721402b02cffSMatthias Ringwald } 721502b02cffSMatthias Ringwald 721602b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 721702b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 721802b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 721902b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 722002b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 722102b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 722202b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 722302b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 722421debf25SMatthias Ringwald } 722521debf25SMatthias Ringwald } 7226cf38a091SMatthias Ringwald 7227cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 72281ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE) == false){ 7229cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 7230cf38a091SMatthias Ringwald } 7231cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 7232cf38a091SMatthias Ringwald // restart le resolving list update 7233cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 7234cf38a091SMatthias Ringwald } 7235cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 7236cf38a091SMatthias Ringwald } 723721debf25SMatthias Ringwald #endif 723821debf25SMatthias Ringwald 7239eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 724018976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 724118976c74SMatthias Ringwald hci_connection_t * conn; 724218976c74SMatthias Ringwald 724318976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 724418976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 724518976c74SMatthias Ringwald 72467d33cb26SMilanka Ringwald // setup Controller info 72477d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 72487d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 72497d33cb26SMilanka Ringwald 725018976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 725118976c74SMatthias Ringwald addr[5] = 0x01; 725218976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 725318976c74SMatthias Ringwald conn->con_handle = addr[5]; 725418976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 725518976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 72567d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 725718976c74SMatthias Ringwald 725818976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 725918976c74SMatthias Ringwald addr[5] = 0x02; 726018976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 726118976c74SMatthias Ringwald conn->con_handle = addr[5]; 726218976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 726318976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 72647d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 726518976c74SMatthias Ringwald 726618976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 726718976c74SMatthias Ringwald addr[5] = 0x03; 726818976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 726918976c74SMatthias Ringwald conn->con_handle = addr[5]; 727018976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 727118976c74SMatthias Ringwald conn->state = OPEN; 72727d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 727318976c74SMatthias Ringwald 727418976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 727518976c74SMatthias Ringwald addr[5] = 0x04; 727618976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 727718976c74SMatthias Ringwald conn->con_handle = addr[5]; 727818976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 727918976c74SMatthias Ringwald conn->state = OPEN; 72807d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 728118976c74SMatthias Ringwald 728218976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 728318976c74SMatthias Ringwald addr[5] = 0x05; 728418976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 728518976c74SMatthias Ringwald conn->con_handle = addr[5]; 728618976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 728718976c74SMatthias Ringwald conn->state = OPEN; 72887d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 72898046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 729018976c74SMatthias Ringwald } 729118976c74SMatthias Ringwald 7292eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 7293eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 7294eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 7295eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7296eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 7297eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 7298eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 7299eddac615SMatthias Ringwald } 7300eddac615SMatthias Ringwald } 73011470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 7302b6ffb67fSMatthias Ringwald hci_stack->le_scanning_param_update = false; 73031470db0cSMatthias Ringwald hci_init_done(); 73041470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 73051470db0cSMatthias Ringwald } 7306eddac615SMatthias Ringwald #endif 7307