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 1251*a0698cb4SMatthias Ringwald uint16_t offset = 3; 1252*a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset]; 1253d1dc057bS[email protected] offset += 1; 1254d1dc057bS[email protected] 1255*a0698cb4SMatthias 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; 1264*a0698cb4SMatthias 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] } 1279*a0698cb4SMatthias Ringwald 1280*a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 1281*a0698cb4SMatthias Ringwald void le_handle_extended_advertisement_report(uint8_t *packet, uint16_t size) { 1282*a0698cb4SMatthias Ringwald uint16_t offset = 3; 1283*a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset++]; 1284*a0698cb4SMatthias Ringwald 1285*a0698cb4SMatthias Ringwald uint8_t i; 1286*a0698cb4SMatthias Ringwald uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1287*a0698cb4SMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 1288*a0698cb4SMatthias Ringwald // sanity checks on data_length: 1289*a0698cb4SMatthias Ringwald uint16_t data_length = packet[offset + 23]; 1290*a0698cb4SMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 1291*a0698cb4SMatthias Ringwald if ((offset + 24u + data_length) > size) return; 1292*a0698cb4SMatthias Ringwald uint16_t event_type = little_endian_read_16(packet, offset); 1293*a0698cb4SMatthias Ringwald offset += 2; 1294*a0698cb4SMatthias Ringwald if ((event_type & 0x10) != 0) { 1295*a0698cb4SMatthias Ringwald // setup legacy event 1296*a0698cb4SMatthias Ringwald uint8_t legacy_event_type; 1297*a0698cb4SMatthias Ringwald switch (event_type){ 1298*a0698cb4SMatthias Ringwald case 0b0010011: 1299*a0698cb4SMatthias Ringwald // ADV_IND 1300*a0698cb4SMatthias Ringwald legacy_event_type = 0; 1301*a0698cb4SMatthias Ringwald break; 1302*a0698cb4SMatthias Ringwald case 0b0010101: 1303*a0698cb4SMatthias Ringwald // ADV_DIRECT_IND 1304*a0698cb4SMatthias Ringwald legacy_event_type = 1; 1305*a0698cb4SMatthias Ringwald break; 1306*a0698cb4SMatthias Ringwald case 0b0010010: 1307*a0698cb4SMatthias Ringwald // ADV_SCAN_IND 1308*a0698cb4SMatthias Ringwald legacy_event_type = 2; 1309*a0698cb4SMatthias Ringwald break; 1310*a0698cb4SMatthias Ringwald case 0b0010000: 1311*a0698cb4SMatthias Ringwald // ADV_NONCONN_IND 1312*a0698cb4SMatthias Ringwald legacy_event_type = 3; 1313*a0698cb4SMatthias Ringwald break; 1314*a0698cb4SMatthias Ringwald case 0b0011011: 1315*a0698cb4SMatthias Ringwald case 0b0011010: 1316*a0698cb4SMatthias Ringwald // SCAN_RSP 1317*a0698cb4SMatthias Ringwald legacy_event_type = 4; 1318*a0698cb4SMatthias Ringwald break; 1319*a0698cb4SMatthias Ringwald default: 1320*a0698cb4SMatthias Ringwald legacy_event_type = 0; 1321*a0698cb4SMatthias Ringwald break; 1322*a0698cb4SMatthias Ringwald } 1323*a0698cb4SMatthias Ringwald uint16_t pos = 0; 1324*a0698cb4SMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 1325*a0698cb4SMatthias Ringwald event[pos++] = 10u + data_length; 1326*a0698cb4SMatthias Ringwald event[pos++] = legacy_event_type; 1327*a0698cb4SMatthias Ringwald // copy address type + address 1328*a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + 6); 1329*a0698cb4SMatthias Ringwald offset += 7; 1330*a0698cb4SMatthias Ringwald pos += 7; 1331*a0698cb4SMatthias Ringwald // skip primary_phy, secondary_phy, advertising_sid, tx_power 1332*a0698cb4SMatthias Ringwald offset += 4; 1333*a0698cb4SMatthias Ringwald // copy rssi 1334*a0698cb4SMatthias Ringwald event[pos++] = packet[offset++]; 1335*a0698cb4SMatthias Ringwald // skip periodic advertising interval and direct address 1336*a0698cb4SMatthias Ringwald offset += 9; 1337*a0698cb4SMatthias Ringwald // copy data len + data; 1338*a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + data_length); 1339*a0698cb4SMatthias Ringwald pos += 1 +data_length; 1340*a0698cb4SMatthias Ringwald offset += 1+ data_length; 1341*a0698cb4SMatthias Ringwald hci_emit_event(event, pos, 1); 1342*a0698cb4SMatthias Ringwald } else { 1343*a0698cb4SMatthias Ringwald // TODO: process new events 1344*a0698cb4SMatthias Ringwald } 1345*a0698cb4SMatthias Ringwald } 1346*a0698cb4SMatthias Ringwald } 1347*a0698cb4SMatthias Ringwald #endif 1348*a0698cb4SMatthias Ringwald 1349b2f949feS[email protected] #endif 1350e8c8828eSMatthias Ringwald #endif 135157c9da5bS[email protected] 13522b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 13532b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1354bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1355bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_enabled){ 13562b6ab3e6SMatthias Ringwald // get number of active le slave connections 13572b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 13582b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 13592b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 13602b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 13612b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 13622b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 13632b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 13642b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 13652b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 13662b6ab3e6SMatthias Ringwald num_slave_connections++; 13672b6ab3e6SMatthias Ringwald } 13682b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1369bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1370bbc366e6SMatthias Ringwald } else { 1371bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 13722b6ab3e6SMatthias Ringwald } 13732b6ab3e6SMatthias Ringwald } 13742b6ab3e6SMatthias Ringwald #endif 13752b6ab3e6SMatthias Ringwald #endif 13762b6ab3e6SMatthias Ringwald 137759d59ecfSMatthias Ringwald #ifdef ENABLE_CLASSIC 137859d59ecfSMatthias Ringwald static void gap_run_set_local_name(void){ 137959d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 138059d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 138159d59ecfSMatthias Ringwald // construct HCI Command and send 138259d59ecfSMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 138359d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 138459d59ecfSMatthias Ringwald packet[0] = opcode & 0xff; 138559d59ecfSMatthias Ringwald packet[1] = opcode >> 8; 138659d59ecfSMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 138759d59ecfSMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 138859d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 138959d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 139059d59ecfSMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 139159d59ecfSMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 139259d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 139359d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 139459d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 139559d59ecfSMatthias Ringwald } 139659d59ecfSMatthias Ringwald 139759d59ecfSMatthias Ringwald static void gap_run_set_eir_data(void){ 139859d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 139959d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 140059d59ecfSMatthias Ringwald // construct HCI Command in-place and send 140159d59ecfSMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 140259d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 140359d59ecfSMatthias Ringwald uint16_t offset = 0; 140459d59ecfSMatthias Ringwald packet[offset++] = opcode & 0xff; 140559d59ecfSMatthias Ringwald packet[offset++] = opcode >> 8; 140659d59ecfSMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 140759d59ecfSMatthias Ringwald packet[offset++] = 0; // FEC not required 140859d59ecfSMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 140959d59ecfSMatthias Ringwald if (hci_stack->eir_data){ 141059d59ecfSMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 141159d59ecfSMatthias Ringwald ad_context_t context; 141259d59ecfSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 141359d59ecfSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 141459d59ecfSMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 141559d59ecfSMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 141659d59ecfSMatthias Ringwald // copy item 141759d59ecfSMatthias Ringwald packet[offset++] = size + 1; 141859d59ecfSMatthias Ringwald packet[offset++] = data_type; 141959d59ecfSMatthias Ringwald memcpy(&packet[offset], data, size); 142059d59ecfSMatthias Ringwald // update name item 142159d59ecfSMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 142259d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 142359d59ecfSMatthias Ringwald } 142459d59ecfSMatthias Ringwald offset += size; 142559d59ecfSMatthias Ringwald } 142659d59ecfSMatthias Ringwald } else { 142759d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 142859d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 142959d59ecfSMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 143059d59ecfSMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 143159d59ecfSMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 143259d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 143359d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 143459d59ecfSMatthias Ringwald } 143559d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 143659d59ecfSMatthias Ringwald } 1437ab4831a3SMatthias Ringwald 1438ab4831a3SMatthias Ringwald static void hci_run_gap_tasks_classic(void){ 1439baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_CLASS_OF_DEVICE) != 0) { 1440baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_CLASS_OF_DEVICE; 1441ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1442ab4831a3SMatthias Ringwald return; 1443ab4831a3SMatthias Ringwald } 1444baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_LOCAL_NAME) != 0) { 1445baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_LOCAL_NAME; 1446ab4831a3SMatthias Ringwald gap_run_set_local_name(); 1447ab4831a3SMatthias Ringwald return; 1448ab4831a3SMatthias Ringwald } 1449baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_EIR_DATA) != 0) { 1450baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_EIR_DATA; 1451ab4831a3SMatthias Ringwald gap_run_set_eir_data(); 1452ab4831a3SMatthias Ringwald return; 1453ab4831a3SMatthias Ringwald } 1454baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_DEFAULT_LINK_POLICY) != 0) { 1455baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_DEFAULT_LINK_POLICY; 1456ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1457ab4831a3SMatthias Ringwald return; 1458ab4831a3SMatthias Ringwald } 1459ab4831a3SMatthias Ringwald // write page scan activity 1460baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY) != 0) { 1461baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 1462ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 1463ab4831a3SMatthias Ringwald return; 1464ab4831a3SMatthias Ringwald } 1465ab4831a3SMatthias Ringwald // write page scan type 1466baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_TYPE) != 0) { 1467baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_TYPE; 1468ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 1469ab4831a3SMatthias Ringwald return; 1470ab4831a3SMatthias Ringwald } 147132a12730SMatthias Ringwald // write page timeout 1472baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_TIMEOUT) != 0) { 1473baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_TIMEOUT; 147432a12730SMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, hci_stack->page_timeout); 147532a12730SMatthias Ringwald return; 147632a12730SMatthias Ringwald } 1477ab4831a3SMatthias Ringwald // send scan enable 1478baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_SCAN_ENABLE) != 0) { 1479baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_SCAN_ENABLE; 1480ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 1481ab4831a3SMatthias Ringwald return; 1482ab4831a3SMatthias Ringwald } 1483c0c8a829SMatthias Ringwald // send write scan activity 1484baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY) != 0) { 1485baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 1486c0c8a829SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_scan_activity, hci_stack->inquiry_scan_interval, hci_stack->inquiry_scan_window); 1487c0c8a829SMatthias Ringwald return; 1488c0c8a829SMatthias Ringwald } 1489ab4831a3SMatthias Ringwald } 149059d59ecfSMatthias Ringwald #endif 149159d59ecfSMatthias Ringwald 14926fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 149306b9e820SMatthias Ringwald 149496b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 149596b53536SMatthias Ringwald if (!hci_stack->config) return 0; 14969796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 149796b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1498a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 149996b53536SMatthias Ringwald baud_rate = 3000000; 150096b53536SMatthias Ringwald } 150196b53536SMatthias Ringwald return baud_rate; 150296b53536SMatthias Ringwald } 150396b53536SMatthias Ringwald 1504ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 15059ec2630cSMatthias Ringwald UNUSED(ds); 15069ec2630cSMatthias Ringwald 15070305bdeaSMatthias Ringwald switch (hci_stack->substate){ 15080305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 15097b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 15100305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 15117b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 15120305bdeaSMatthias Ringwald hci_run(); 15130305bdeaSMatthias Ringwald break; 15149f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 15159f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 15169f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 15179f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 15189f007422SMatthias Ringwald } 1519cf373d3aSMatthias Ringwald 1520cf373d3aSMatthias Ringwald /* fall through */ 1521cf373d3aSMatthias Ringwald 1522e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1523e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1524e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1525e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1526e47e68c7SMatthias Ringwald hci_run(); 1527688c2635SMatthias Ringwald break; 15287224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 15297224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 153096b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1531cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 15327dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 15337224be7eSMatthias Ringwald } 1534834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 153561f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1536834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1537834bce8cSMatthias Ringwald log_info("Link Reset"); 1538834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1539834bce8cSMatthias Ringwald } 1540772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1541772a36d3SMatthias Ringwald hci_run(); 1542772a36d3SMatthias Ringwald } 15434696bddbSMatthias Ringwald break; 1544559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1545559961d0SMatthias Ringwald // otherwise continue 1546559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1547559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1548559961d0SMatthias Ringwald break; 15490305bdeaSMatthias Ringwald default: 15500305bdeaSMatthias Ringwald break; 15510305bdeaSMatthias Ringwald } 15520305bdeaSMatthias Ringwald } 155306b9e820SMatthias Ringwald #endif 15540305bdeaSMatthias Ringwald 155571de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 155674b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 155774b323a9SMatthias Ringwald } 155874b323a9SMatthias Ringwald 1559f795c86eSMatthias Ringwald static void hci_init_done(void){ 1560f795c86eSMatthias Ringwald // done. tell the app 1561f795c86eSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1562f795c86eSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1563f795c86eSMatthias Ringwald hci_emit_state(); 1564f795c86eSMatthias Ringwald } 1565f795c86eSMatthias Ringwald 156674b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 156771de195eSMatthias Ringwald static void hci_initializing_run(void){ 1568148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 1569f4c579d4SMatthias Ringwald 15700d37aff3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 15710d37aff3SMatthias Ringwald 1572f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1573f9fd20c2SMatthias Ringwald bool need_baud_change = hci_stack->config 1574f4c579d4SMatthias Ringwald && hci_stack->chipset 1575f4c579d4SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1576f4c579d4SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 1577f4c579d4SMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1578f4c579d4SMatthias Ringwald #endif 1579f4c579d4SMatthias Ringwald 158074b323a9SMatthias Ringwald switch (hci_stack->substate){ 158174b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 158274b323a9SMatthias Ringwald hci_state_reset(); 1583a0cf2f3fSMatthias Ringwald 15846fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 15850305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1586659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1587528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1588528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1589a0cf2f3fSMatthias Ringwald #endif 15900305bdeaSMatthias Ringwald // send command 159174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 15920305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 159374b323a9SMatthias Ringwald break; 159476fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 159576fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 159676fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 159776fcb19bSMatthias Ringwald break; 1598e28e41c0SMatthias Ringwald 1599e28e41c0SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1600e90bae01SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 1601e90bae01SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 1602e90bae01SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 1603e90bae01SMatthias Ringwald break; 1604e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1605e47e68c7SMatthias Ringwald hci_state_reset(); 1606e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1607659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1608528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1609528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1610e47e68c7SMatthias Ringwald // send command 1611e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1612e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1613e47e68c7SMatthias Ringwald break; 16148d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 16158d29070eSMatthias Ringwald hci_state_reset(); 16168d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 16178d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 16188d29070eSMatthias Ringwald break; 1619f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 1620f4c579d4SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1621f4c579d4SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1622f4c579d4SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1623f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1624f4c579d4SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1625f4c579d4SMatthias Ringwald break; 1626f4c579d4SMatthias Ringwald } 1627c97af506SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 1628c97af506SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 1629c97af506SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1630c97af506SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1631c97af506SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 1632c97af506SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1633c97af506SMatthias Ringwald break; 1634f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: 1635f4c579d4SMatthias Ringwald if (need_baud_change) { 163696b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 16373fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1638f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 163974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 16404ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 16414696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 16424696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 164361f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1644659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1645528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 16464696bddbSMatthias Ringwald } 164774b323a9SMatthias Ringwald break; 1648fab26ab3SMatthias Ringwald } 1649f4c579d4SMatthias Ringwald 1650f4c579d4SMatthias Ringwald /* fall through */ 1651f4c579d4SMatthias Ringwald 165274b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 165374b323a9SMatthias Ringwald // Custom initialization 16543fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1655ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 16561979f09cSMatthias Ringwald bool send_cmd = false; 1657ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1658f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 16591979f09cSMatthias Ringwald send_cmd = true; 1660e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1661e47e68c7SMatthias Ringwald break; 1662f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 16631979f09cSMatthias Ringwald send_cmd = true; 1664f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1665e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1666659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1667528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1668528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1669a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1670772a36d3SMatthias Ringwald && hci_stack->config 16713fb36a29SMatthias Ringwald && hci_stack->chipset 16723fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1673772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 16742caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1675772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1676772a36d3SMatthias Ringwald } else { 16779f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1678772a36d3SMatthias Ringwald } 1679e47e68c7SMatthias Ringwald break; 1680f41911edSMatthias Ringwald default: 1681f41911edSMatthias Ringwald break; 1682e47e68c7SMatthias Ringwald } 1683ee720f3aSMatthias Ringwald 1684ee720f3aSMatthias Ringwald if (send_cmd){ 16854ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1686ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1687ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 16880305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 168974b323a9SMatthias Ringwald break; 169074b323a9SMatthias Ringwald } 1691148ca237SMatthias Ringwald log_info("Init script done"); 169292a0d36dSMatthias Ringwald 1693559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1694ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1695a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1696a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1697e021ff1eSMatthias Ringwald 1698559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 169992a0d36dSMatthias Ringwald if (need_baud_change) { 17009796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1701cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 170292a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 170392a0d36dSMatthias Ringwald } 1704559961d0SMatthias Ringwald 1705f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1706f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1707f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1708f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1709559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1710f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1711559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1712559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1713559961d0SMatthias Ringwald break; 171492a0d36dSMatthias Ringwald } 171574b323a9SMatthias Ringwald } 171606b9e820SMatthias Ringwald #endif 1717521bd5ffSMatthias Ringwald /* fall through */ 171806b9e820SMatthias Ringwald 171906b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 172006b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 172106b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 172206b9e820SMatthias Ringwald break; 172353860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 172453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 172553860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 172653860077SMatthias Ringwald break; 172774b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 17285ffe9d0bSMatthias Ringwald // only read buffer size if supported 17291ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE)){ 173074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 17310305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 173274b323a9SMatthias Ringwald break; 17335ffe9d0bSMatthias Ringwald } 1734521bd5ffSMatthias Ringwald 17355ffe9d0bSMatthias Ringwald /* fall through */ 17365ffe9d0bSMatthias Ringwald 173753860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 173853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 17390305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 174074b323a9SMatthias Ringwald break; 17412b838201SMatthias Ringwald 17422b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 17432b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 17442b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 17452b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 17462b838201SMatthias Ringwald break; 17472b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 17482b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 17492b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 17502b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 17512b838201SMatthias Ringwald break; 17522b838201SMatthias Ringwald #endif 17532b838201SMatthias Ringwald 175474b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 17550305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 175674b323a9SMatthias Ringwald if (hci_le_supported()){ 17575ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x3FFFFFFFU); 175874b323a9SMatthias Ringwald } else { 175974b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 17605ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x1FFFFFFFU); 176174b323a9SMatthias Ringwald } 176274b323a9SMatthias Ringwald break; 17632b838201SMatthias Ringwald 176435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 176574b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 1766f795c86eSMatthias Ringwald if (hci_classic_supported() && gap_ssp_supported()){ 176774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 17680305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 176974b323a9SMatthias Ringwald break; 1770e7c662faSMatthias Ringwald } 1771521bd5ffSMatthias Ringwald 1772e7c662faSMatthias Ringwald /* fall through */ 1773e7c662faSMatthias Ringwald 1774f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1775f795c86eSMatthias Ringwald if (hci_classic_supported()){ 1776f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 17778a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1778f6858d14SMatthias Ringwald break; 1779f795c86eSMatthias Ringwald } 1780521bd5ffSMatthias Ringwald 1781f795c86eSMatthias Ringwald /* fall through */ 1782f795c86eSMatthias Ringwald 17835d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 1784f3052906SMatthias Ringwald // skip write secure connections host support if not supported or disabled 17851ffa425cSMatthias Ringwald if (hci_classic_supported() && hci_stack->secure_connections_enable 17861ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST)) { 1787c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 17885d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 17891ffa425cSMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 17905d23aae8SMatthias Ringwald break; 1791f3052906SMatthias Ringwald } 1792521bd5ffSMatthias Ringwald 1793e4c6114dSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1794521bd5ffSMatthias Ringwald /* fall through */ 1795521bd5ffSMatthias Ringwald 1796483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 1797729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 1798e4c6114dSMatthias Ringwald // skip write synchronous flow control if not supported 17991ffa425cSMatthias Ringwald if (hci_classic_supported() 18001ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)) { 1801729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 1802729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 1803729ed62eSMatthias Ringwald break; 1804f795c86eSMatthias Ringwald } 1805f795c86eSMatthias Ringwald /* fall through */ 1806f795c86eSMatthias Ringwald 1807483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 1808e4c6114dSMatthias Ringwald // skip write default erroneous data reporting if not supported 18091ffa425cSMatthias Ringwald if (hci_classic_supported() 18101ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING)) { 1811483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 1812483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 1813483c5078SMatthias Ringwald break; 1814f795c86eSMatthias Ringwald } 1815e4c6114dSMatthias Ringwald #endif 1816f795c86eSMatthias Ringwald 1817f795c86eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(ENABLE_SCO_OVER_PCM) 1818521bd5ffSMatthias Ringwald /* fall through */ 1819521bd5ffSMatthias Ringwald 18208051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 1821a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 1822e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 1823a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 18248051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1825a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 1826a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 18278051253fSMatthias Ringwald #endif 18288051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 18298051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 18301d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 1831f795c86eSMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 16 kHz 18321d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 18331d2bbd54SMatthias Ringwald #else 18341d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 18351d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 18361d2bbd54SMatthias Ringwald #endif 18378051253fSMatthias Ringwald #endif 1838a42798c3SMatthias Ringwald break; 1839f795c86eSMatthias Ringwald } 1840f795c86eSMatthias Ringwald #endif 1841f795c86eSMatthias Ringwald 18424e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 1843521bd5ffSMatthias Ringwald /* fall through */ 1844521bd5ffSMatthias Ringwald 18454e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 1846e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 18474e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 18484e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 18491d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 18501d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 18511d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 18521d2bbd54SMatthias Ringwald #else 18531d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 18541d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 18551d2bbd54SMatthias Ringwald #endif 18564e821764SMatthias Ringwald break; 1857f795c86eSMatthias Ringwald } 185835454696SMatthias Ringwald #endif 18594e821764SMatthias Ringwald #endif 18604e821764SMatthias Ringwald 1861a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 1862521bd5ffSMatthias Ringwald /* fall through */ 1863521bd5ffSMatthias Ringwald 186474b323a9SMatthias Ringwald // LE INIT 186574b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 1866f795c86eSMatthias Ringwald if (hci_le_supported()){ 186774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 18680305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 186974b323a9SMatthias Ringwald break; 1870f795c86eSMatthias Ringwald } 1871521bd5ffSMatthias Ringwald 1872f795c86eSMatthias Ringwald /* fall through */ 1873f795c86eSMatthias Ringwald 1874699a5fcaSMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 1875699a5fcaSMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 18761ffa425cSMatthias Ringwald if (hci_le_supported() 18771ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED)) { 1878699a5fcaSMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 1879699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 1880699a5fcaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 1881daabb8b8SMatthias Ringwald break; 1882f795c86eSMatthias Ringwald } 1883521bd5ffSMatthias Ringwald 1884f795c86eSMatthias Ringwald /* fall through */ 1885f795c86eSMatthias Ringwald 1886699a5fcaSMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 1887f795c86eSMatthias Ringwald if (hci_le_supported()){ 1888699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 188991d9e5d0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0xfffffdff, 0x07); // all events from core v5.3 without LE Enhanced Connection Complete 189074b323a9SMatthias Ringwald break; 1891f795c86eSMatthias Ringwald } 1892b435e062SMatthias Ringwald #endif 1893b435e062SMatthias Ringwald 1894b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 1895521bd5ffSMatthias Ringwald /* fall through */ 1896521bd5ffSMatthias Ringwald 1897dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 18981ffa425cSMatthias Ringwald if (hci_le_supported() 18991ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH)) { 1900dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 1901dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 1902dcd678baSMatthias Ringwald break; 1903f795c86eSMatthias Ringwald } 1904521bd5ffSMatthias Ringwald 1905f795c86eSMatthias Ringwald /* fall through */ 1906f795c86eSMatthias Ringwald 1907dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 19081ffa425cSMatthias Ringwald if (hci_le_supported() 19091ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)) { 1910dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 1911b435e062SMatthias 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); 1912dcd678baSMatthias Ringwald break; 1913f795c86eSMatthias Ringwald } 1914b435e062SMatthias Ringwald #endif 1915b435e062SMatthias Ringwald 1916b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 1917521bd5ffSMatthias Ringwald /* fall through */ 1918521bd5ffSMatthias Ringwald 19193b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 1920f795c86eSMatthias Ringwald if (hci_le_supported()){ 19213b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 19223b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 19233b6d4121SMatthias Ringwald break; 1924f795c86eSMatthias Ringwald } 1925521bd5ffSMatthias Ringwald 192674b323a9SMatthias Ringwald #endif 19274f982f31SMatthias Ringwald 1928521bd5ffSMatthias Ringwald /* fall through */ 1929521bd5ffSMatthias Ringwald 1930f795c86eSMatthias Ringwald case HCI_INIT_DONE: 19314f982f31SMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 193273799937SMatthias Ringwald // main init sequence complete 19334f982f31SMatthias Ringwald #ifdef ENABLE_CLASSIC 193473799937SMatthias Ringwald // check if initial Classic GAP Tasks are completed 1935baa4881eSMatthias Ringwald if (hci_classic_supported() && (hci_stack->gap_tasks_classic != 0)) { 19364f982f31SMatthias Ringwald hci_run_gap_tasks_classic(); 19374f982f31SMatthias Ringwald break; 19384f982f31SMatthias Ringwald } 19394f982f31SMatthias Ringwald #endif 194073799937SMatthias Ringwald #ifdef ENABLE_BLE 194173799937SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 194273799937SMatthias Ringwald // check if initial LE GAP Tasks are completed 194373799937SMatthias Ringwald if (hci_le_supported() && hci_stack->le_scanning_param_update) { 194473799937SMatthias Ringwald hci_run_general_gap_le(); 194573799937SMatthias Ringwald break; 194673799937SMatthias Ringwald } 194773799937SMatthias Ringwald #endif 194873799937SMatthias Ringwald #endif 1949f795c86eSMatthias Ringwald hci_init_done(); 1950f795c86eSMatthias Ringwald break; 1951f795c86eSMatthias Ringwald 195274b323a9SMatthias Ringwald default: 195374b323a9SMatthias Ringwald return; 195474b323a9SMatthias Ringwald } 195555975f88SMatthias Ringwald } 195655975f88SMatthias Ringwald 195707fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 195807fd2f31SMatthias Ringwald bool command_completed = false; 19590e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 1960f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 19616155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 196207fd2f31SMatthias Ringwald command_completed = true; 1963148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 19646155b3d3S[email protected] } else { 1965d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 19666155b3d3S[email protected] } 19676155b3d3S[email protected] } 19680f97eae7SMatthias Ringwald 19690e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 19706155b3d3S[email protected] uint8_t status = packet[2]; 1971f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 19726155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 19736155b3d3S[email protected] if (status){ 197407fd2f31SMatthias Ringwald command_completed = true; 1975148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 19766155b3d3S[email protected] } else { 19776155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 19786155b3d3S[email protected] } 19796155b3d3S[email protected] } else { 1980148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 19816155b3d3S[email protected] } 19826155b3d3S[email protected] } 19836fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1984e47e68c7SMatthias Ringwald // Vendor == CSR 19850e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 1986e47e68c7SMatthias Ringwald // TODO: track actual command 198707fd2f31SMatthias Ringwald command_completed = true; 1988e47e68c7SMatthias Ringwald } 1989a2481739S[email protected] 19904e9daa6fSMatthias Ringwald // Vendor == Toshiba 19910e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 19924e9daa6fSMatthias Ringwald // TODO: track actual command 199307fd2f31SMatthias Ringwald command_completed = true; 1994004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 1995004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 19964e9daa6fSMatthias Ringwald } 199707fd2f31SMatthias Ringwald #endif 199807fd2f31SMatthias Ringwald 199907fd2f31SMatthias Ringwald return command_completed; 200007fd2f31SMatthias Ringwald } 200107fd2f31SMatthias Ringwald 200207fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 200307fd2f31SMatthias Ringwald 200407fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 200507fd2f31SMatthias Ringwald 200607fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 200707fd2f31SMatthias Ringwald 20086fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 20094e9daa6fSMatthias Ringwald 20100f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 20110f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 20120f97eae7SMatthias Ringwald // 20130f97eae7SMatthias Ringwald // HCI Reset 20140f97eae7SMatthias Ringwald // Timeout 100 ms 20150f97eae7SMatthias Ringwald // HCI Reset 20160f97eae7SMatthias Ringwald // Command Complete Reset 20170f97eae7SMatthias Ringwald // HCI Read Local Version Information 20180f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 20190f97eae7SMatthias Ringwald // hang... 20200f97eae7SMatthias Ringwald // 20210f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 20220f97eae7SMatthias Ringwald if (!command_completed 2023a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 20240e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 20250f97eae7SMatthias Ringwald 2026f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 20270f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 20280f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 20290f97eae7SMatthias Ringwald return; 20300f97eae7SMatthias Ringwald } 20310f97eae7SMatthias Ringwald } 20320f97eae7SMatthias Ringwald 20339f007422SMatthias Ringwald // CSR & H5 20349f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 20359f007422SMatthias Ringwald if (!command_completed 2036a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 20370e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 20389f007422SMatthias Ringwald 20399f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 20409f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 20419f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 20429f007422SMatthias Ringwald return; 20439f007422SMatthias Ringwald } 20449f007422SMatthias Ringwald } 20459f007422SMatthias Ringwald 20469f007422SMatthias 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 20479f007422SMatthias Ringwald // fix: Correct substate and behave as command below 20489f007422SMatthias Ringwald if (command_completed){ 20499f007422SMatthias Ringwald switch (hci_stack->substate){ 20509f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 20519f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 20529f007422SMatthias Ringwald break; 20539f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 20549f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 20559f007422SMatthias Ringwald break; 20569f007422SMatthias Ringwald default: 20579f007422SMatthias Ringwald break; 20589f007422SMatthias Ringwald } 20599f007422SMatthias Ringwald } 20600f97eae7SMatthias Ringwald 206106b9e820SMatthias Ringwald #endif 20620f97eae7SMatthias Ringwald 2063a2481739S[email protected] if (!command_completed) return; 2064a2481739S[email protected] 206507fd2f31SMatthias Ringwald bool need_baud_change = false; 206607fd2f31SMatthias Ringwald bool need_addr_change = false; 206706b9e820SMatthias Ringwald 20686fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 206906b9e820SMatthias Ringwald need_baud_change = hci_stack->config 20703fb36a29SMatthias Ringwald && hci_stack->chipset 20713fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 2072db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 20739796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 2074db8bc6ffSMatthias Ringwald 207506b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 20763fb36a29SMatthias Ringwald && hci_stack->chipset 20773fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 207806b9e820SMatthias Ringwald #endif 2079a80162e9SMatthias Ringwald 20805c363727SMatthias Ringwald switch(hci_stack->substate){ 208106b9e820SMatthias Ringwald 20826fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 20839f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 2084d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 20859f007422SMatthias Ringwald // fix: just correct substate and behave as command below 2086f4c579d4SMatthias Ringwald 2087f4c579d4SMatthias Ringwald /* fall through */ 2088f4c579d4SMatthias Ringwald #endif 2089f4c579d4SMatthias Ringwald 209074b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 2091528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2092f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 20932f48d920SMatthias Ringwald return; 2094f4c579d4SMatthias Ringwald 2095f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2096a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 20974696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 2098fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 209961f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 210096b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2101cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 2102fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 21034696bddbSMatthias Ringwald } 210474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 210574b323a9SMatthias Ringwald return; 2106a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 2107528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2108a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 2109a80162e9SMatthias Ringwald return; 211074b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 211174b323a9SMatthias Ringwald // repeat custom init 211274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 211374b323a9SMatthias Ringwald return; 211406b9e820SMatthias Ringwald #endif 211506b9e820SMatthias Ringwald 2116a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 21170e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 2118efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 2119efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 2120eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 2121eb3a5314SMatthias Ringwald return; 2122eb3a5314SMatthias Ringwald } 212353860077SMatthias Ringwald if (need_addr_change){ 212453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 212553860077SMatthias Ringwald return; 212653860077SMatthias Ringwald } 212753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 212853860077SMatthias Ringwald return; 21296fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 21307224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 21317224be7eSMatthias Ringwald if (need_baud_change){ 213296b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2133cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 2134fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 21357224be7eSMatthias Ringwald } 2136eb3a5314SMatthias Ringwald if (need_addr_change){ 2137eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 2138eb3a5314SMatthias Ringwald return; 2139eb3a5314SMatthias Ringwald } 2140eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 2141eb3a5314SMatthias Ringwald return; 214253860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 21436ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 21446ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 21456ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 214653860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 214753860077SMatthias Ringwald return; 214853860077SMatthias Ringwald } 214953860077SMatthias Ringwald // skipping st warm boot 215053860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 215153860077SMatthias Ringwald return; 215253860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 215353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 215453860077SMatthias Ringwald return; 215506b9e820SMatthias Ringwald #endif 2156e7c662faSMatthias Ringwald 2157f795c86eSMatthias Ringwald case HCI_INIT_DONE: 2158eb8d95caSMatthias Ringwald // set state if we came here by fall through 2159eb8d95caSMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 2160f795c86eSMatthias Ringwald return; 2161a650ba4dSMatthias Ringwald 21626155b3d3S[email protected] default: 216374b323a9SMatthias Ringwald break; 21646155b3d3S[email protected] } 216555975f88SMatthias Ringwald hci_initializing_next_state(); 21666155b3d3S[email protected] } 21676155b3d3S[email protected] 21680bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 21693bb5ff27SMatthias Ringwald // CC2564C might emit Connection Complete for rejected incoming SCO connection 21703bb5ff27SMatthias Ringwald // To prevent accidentally free'ing the CHI connection for the ACL connection, 21713bb5ff27SMatthias Ringwald // check if the hci connection has been outgoing 21721f34df2cSMatthias Ringwald switch (conn->state){ 21731f34df2cSMatthias Ringwald case SEND_CREATE_CONNECTION: 21741f34df2cSMatthias Ringwald case RECEIVED_CONNECTION_REQUEST: 21751f34df2cSMatthias Ringwald break; 21761f34df2cSMatthias Ringwald default: 21771f34df2cSMatthias Ringwald return; 21781f34df2cSMatthias Ringwald } 21793bb5ff27SMatthias Ringwald 2180229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 21810bbba85bSMatthias Ringwald bd_addr_t bd_address; 21826535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 21830bbba85bSMatthias Ringwald 21846bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 21856bc9fa5eSMatthias Ringwald // cache needed data 21866bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 21876bc9fa5eSMatthias Ringwald #endif 21886bc9fa5eSMatthias Ringwald 21890bbba85bSMatthias Ringwald // connection failed, remove entry 21900bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 21910bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 21920bbba85bSMatthias Ringwald 21936bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 21940bbba85bSMatthias Ringwald // notify client if dedicated bonding 21950bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 21960bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 21970bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 21980bbba85bSMatthias Ringwald } 21990bbba85bSMatthias Ringwald 22000bbba85bSMatthias Ringwald // if authentication error, also delete link key 22010bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 22020bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 22030bbba85bSMatthias Ringwald } 22041c79b5e3SMatthias Ringwald #else 22051c79b5e3SMatthias Ringwald UNUSED(status); 22066bc9fa5eSMatthias Ringwald #endif 22070bbba85bSMatthias Ringwald } 22080bbba85bSMatthias Ringwald 2209be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 22102f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 22112f5c44baSMatthias Ringwald // SSP Controller 22122f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 22132f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 22142f5c44baSMatthias Ringwald } 22152f5c44baSMatthias Ringwald // eSCO 22162f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 22172f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 22182f5c44baSMatthias Ringwald } 22192f5c44baSMatthias Ringwald // Extended features 22202f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 22212f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 22222f5c44baSMatthias Ringwald } 22232f5c44baSMatthias Ringwald } 22242f5c44baSMatthias Ringwald 22252f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 22262f5c44baSMatthias Ringwald // SSP Host 22272f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 22282f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 22292f5c44baSMatthias Ringwald } 223050c51a77SMatthias Ringwald // SC Host 223150c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 223250c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 223350c51a77SMatthias Ringwald } 223450c51a77SMatthias Ringwald } 223550c51a77SMatthias Ringwald 223650c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 223750c51a77SMatthias Ringwald // SC Controller 223850c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 223950c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 224050c51a77SMatthias Ringwald } 22412f5c44baSMatthias Ringwald } 22422f5c44baSMatthias Ringwald 2243de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2244461a2bc4SMatthias Ringwald conn->bonding_flags &= ~BONDING_REMOTE_FEATURES_QUERY_ACTIVE; 2245de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 224649bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2247de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2248de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2249de0df013SMatthias Ringwald } 2250de0df013SMatthias Ringwald } 2251128825c3SMatthias Ringwald static bool hci_remote_sc_enabled(hci_connection_t * connection){ 2252128825c3SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 2253128825c3SMatthias Ringwald return (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 2254128825c3SMatthias Ringwald } 2255128825c3SMatthias Ringwald 2256be500194SMatthias Ringwald #endif 2257de0df013SMatthias Ringwald 225867c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 225967c6c9dcSMatthias Ringwald // handle BT initialization 226067c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 226167c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 226267c6c9dcSMatthias Ringwald } 226367c6c9dcSMatthias Ringwald 226467c6c9dcSMatthias Ringwald // help with BT sleep 226567c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 226667c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 2267905ee0fdSMatthias Ringwald && (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable))) { 226867c6c9dcSMatthias Ringwald hci_initializing_next_state(); 226967c6c9dcSMatthias Ringwald } 227067c6c9dcSMatthias Ringwald } 227167c6c9dcSMatthias Ringwald 22729866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 22739866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 22748daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 22759866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2276abdad579SMatthias Ringwald 22778daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) != 0) { 22781cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 22799866fdc7SMatthias Ringwald hci_emit_security_level(conn->con_handle, gap_security_level_for_connection(conn)); 2280abdad579SMatthias Ringwald return; 2281abdad579SMatthias Ringwald } 2282abdad579SMatthias Ringwald 2283d54424c3SMatthias Ringwald // Request remote features if not already done 2284dbe0d85cSMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 2285d54424c3SMatthias Ringwald 2286abdad579SMatthias Ringwald // Request Authentication if not already done 2287abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2288abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 22899866fdc7SMatthias Ringwald } 22909866fdc7SMatthias Ringwald #endif 22919866fdc7SMatthias Ringwald 22923b631737SMatthias Ringwald static void hci_store_local_supported_commands(const uint8_t * packet){ 229325e46151SMatthias Ringwald // create mapping table 229425e46151SMatthias Ringwald #define X(name, offset, bit) { offset, bit }, 229525e46151SMatthias Ringwald static struct { 229625e46151SMatthias Ringwald uint8_t byte_offset; 229725e46151SMatthias Ringwald uint8_t bit_position; 229825e46151SMatthias Ringwald } supported_hci_commands_map [] = { 229925e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 230025e46151SMatthias Ringwald }; 230125e46151SMatthias Ringwald #undef X 230225e46151SMatthias Ringwald 230325e46151SMatthias Ringwald // create names for debug purposes 230425e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 230525e46151SMatthias Ringwald #define X(name, offset, bit) #name, 230625e46151SMatthias Ringwald static const char * command_names[] = { 230725e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 230825e46151SMatthias Ringwald }; 230925e46151SMatthias Ringwald #undef X 231025e46151SMatthias Ringwald #endif 231125e46151SMatthias Ringwald 23121ffa425cSMatthias Ringwald hci_stack->local_supported_commands = 0; 231325e46151SMatthias Ringwald const uint8_t * commands_map = &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1]; 231425e46151SMatthias Ringwald uint16_t i; 231525e46151SMatthias Ringwald for (i = 0 ; i < SUPPORTED_HCI_COMMANDS_COUNT ; i++){ 231625e46151SMatthias Ringwald if ((commands_map[supported_hci_commands_map[i].byte_offset] & (1 << supported_hci_commands_map[i].bit_position)) != 0){ 231725e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 231825e46151SMatthias 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); 231925e46151SMatthias Ringwald #else 23201ffa425cSMatthias Ringwald log_info("Command 0x%02x supported %u/%u", i, supported_hci_commands_map[i].byte_offset, supported_hci_commands_map[i].bit_position); 232125e46151SMatthias Ringwald #endif 23221ffa425cSMatthias Ringwald hci_stack->local_supported_commands |= (1LU << i); 232325e46151SMatthias Ringwald } 232425e46151SMatthias Ringwald } 23251ffa425cSMatthias Ringwald log_info("Local supported commands summary %04x", hci_stack->local_supported_commands); 23263b631737SMatthias Ringwald } 23273b631737SMatthias Ringwald 23284f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2329b08371a9SMilanka Ringwald UNUSED(size); 2330e76a89eeS[email protected] 23319cbd2215SMatthias Ringwald uint16_t manufacturer; 23329cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2333fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 23341f7b95a1Smatthias.ringwald hci_connection_t * conn; 2335645b7c25SMatthias Ringwald uint8_t status; 23369cbd2215SMatthias Ringwald #endif 23376bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 23386bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 23397ec5eeaaSmatthias.ringwald 2340645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2341645b7c25SMatthias Ringwald switch (opcode){ 2342645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2343645b7c25SMatthias Ringwald if (packet[5]) break; 23449e263bd7SMatthias Ringwald // terminate, name 248 chars 23459e263bd7SMatthias Ringwald packet[6+248] = 0; 23469e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2347645b7c25SMatthias Ringwald break; 2348645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 23491d279b20Smatthias.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" 2350c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2351429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2352429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2353429122ccSMatthias Ringwald 2354429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2355429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2356429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2357429122ccSMatthias Ringwald 2358f8fbdce0SMatthias Ringwald hci_stack->acl_packets_total_num = little_endian_read_16(packet, 9); 2359f8fbdce0SMatthias Ringwald hci_stack->sco_packets_total_num = little_endian_read_16(packet, 11); 2360a8b12447S[email protected] 2361429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2362429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 23631a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2364c3b46f5aSMatthias Ringwald } 2365645b7c25SMatthias Ringwald break; 2366645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2367645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2368891b9fc2SMatthias Ringwald uint8_t event[5]; 2369891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2370891b9fc2SMatthias Ringwald event[1] = 3; 23716535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2372891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2373891b9fc2SMatthias Ringwald } 2374645b7c25SMatthias Ringwald break; 2375a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2376645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2377f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2378ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 23796c26b087S[email protected] // determine usable ACL payload size 23806c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 23816c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 23826c26b087S[email protected] } 23839da54300S[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); 2384645b7c25SMatthias Ringwald break; 2385b435e062SMatthias Ringwald #endif 2386b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2387645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2388dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2389dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2390dcd678baSMatthias 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); 2391645b7c25SMatthias Ringwald break; 2392b435e062SMatthias Ringwald #endif 2393d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2394645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2395e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 239615d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2397645b7c25SMatthias Ringwald break; 239865a46ef3S[email protected] #endif 2399645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2400645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2401645b7c25SMatthias 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)); 240233373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 24031624665aSMatthias Ringwald if (hci_stack->link_key_db){ 24041624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 24051624665aSMatthias Ringwald } 240633373e40SMatthias Ringwald #endif 2407645b7c25SMatthias Ringwald break; 240835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2409645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 24103a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 2411645b7c25SMatthias Ringwald break; 2412645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 2413f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2414f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2415f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2416f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2417f5875de5SMatthias Ringwald } 2418645b7c25SMatthias Ringwald break; 241935454696SMatthias Ringwald #endif 2420645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 24214f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 242265389bfcS[email protected] 242335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2424a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2425a5a23fc2S[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]); 24268b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 242735454696SMatthias Ringwald #endif 2428f5d8d141S[email protected] // Classic/LE 2429f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2430645b7c25SMatthias Ringwald break; 2431645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2432645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2433c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2434c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2435c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2436c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2437c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2438c21d89f3SMatthias Ringwald } 2439c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 24404696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2441645b7c25SMatthias Ringwald break; 2442645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 24433b631737SMatthias Ringwald hci_store_local_supported_commands(packet); 2444645b7c25SMatthias Ringwald break; 2445e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2446645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 24474f781026SMatthias Ringwald if (packet[5]) return; 24485b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2449645b7c25SMatthias Ringwald break; 2450645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2451645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2452573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2453573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2454c3b46f5aSMatthias Ringwald if (conn != NULL) { 24559866fdc7SMatthias Ringwald uint8_t key_size = 0; 2456573897a0SMatthias Ringwald if (status == 0){ 24579866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 24584a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2459170fafaeSMatthias Ringwald } else { 2460e9f98c4aSMatthias Ringwald key_size = 1; 24619866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2462573897a0SMatthias Ringwald } 24639866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2464c3b46f5aSMatthias Ringwald } 2465645b7c25SMatthias Ringwald break; 2466cc15bb2cSMatthias Ringwald // assert pairing complete event is emitted. 2467cc15bb2cSMatthias Ringwald // note: for SSP, Simple Pairing Complete Event is sufficient, but we want to be more robust 2468cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_NEGATIVE_REPLY: 2469cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_NEGATIVE_REPLY: 2470cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: 24718cc1507eSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 2472cc15bb2cSMatthias Ringwald // lookup connection by gap pairing addr 2473cc15bb2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->gap_pairing_addr, BD_ADDR_TYPE_ACL); 2474cc15bb2cSMatthias Ringwald if (conn == NULL) break; 2475cc15bb2cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 2476cc15bb2cSMatthias Ringwald break; 2477cc15bb2cSMatthias Ringwald 247875a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 247975a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 248075a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 248175a8e4faSMatthias Ringwald uint8_t event[67]; 248275a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 248375a8e4faSMatthias Ringwald event[1] = 65; 248475a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 248575a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 248675a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 248775a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 248875a8e4faSMatthias Ringwald event[2] = 3; 248975a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 249075a8e4faSMatthias Ringwald } else { 249175a8e4faSMatthias Ringwald event[2] = 1; 249275a8e4faSMatthias Ringwald } 249375a8e4faSMatthias Ringwald } 249475a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 249575a8e4faSMatthias Ringwald break; 249675a8e4faSMatthias Ringwald } 24971ae74bf3SMatthias Ringwald 24981ae74bf3SMatthias Ringwald // note: only needed if user does not provide OOB data 24991ae74bf3SMatthias Ringwald case HCI_OPCODE_HCI_REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: 25001ae74bf3SMatthias Ringwald conn = hci_connection_for_handle(hci_stack->classic_oob_con_handle); 25011ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 25021ae74bf3SMatthias Ringwald if (conn == NULL) break; 25031ae74bf3SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 25041ae74bf3SMatthias Ringwald break; 2505e8c8828eSMatthias Ringwald #endif 250675a8e4faSMatthias Ringwald #endif 25076f35bb46SMatthias Ringwald default: 25086f35bb46SMatthias Ringwald break; 25094f781026SMatthias Ringwald } 2510645b7c25SMatthias Ringwald } 25114f781026SMatthias Ringwald 25120ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 25130ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 25140ce3f217SMatthias Ringwald bd_addr_t addr; 25150ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 25160ce3f217SMatthias Ringwald hci_connection_t * conn; 25170ce3f217SMatthias Ringwald 25180ce3f217SMatthias Ringwald // Connection management 25190ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 25200ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 25210ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 25220ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 25230ce3f217SMatthias Ringwald 25240ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 25250ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 25260ce3f217SMatthias Ringwald if (packet[3]){ 25270ce3f217SMatthias Ringwald 25280ce3f217SMatthias Ringwald // handle cancelled outgoing connection 25290ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 25300ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 25310ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 25320ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 25331f468175SMatthias Ringwald // reset state 25340ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 25351f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 25360ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 25370ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 25380ce3f217SMatthias Ringwald } 25390ce3f217SMatthias Ringwald 25400ce3f217SMatthias Ringwald // outgoing le connection establishment is done 25410ce3f217SMatthias Ringwald if (conn){ 25420ce3f217SMatthias Ringwald // remove entry 25430ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 25440ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 25450ce3f217SMatthias Ringwald } 25460ce3f217SMatthias Ringwald return; 25470ce3f217SMatthias Ringwald } 25480ce3f217SMatthias Ringwald #endif 25490ce3f217SMatthias Ringwald 25500ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 25510ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 25520ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 25530ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 25540ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 25550ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 25560ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 25570ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 25580ce3f217SMatthias Ringwald } 25590ce3f217SMatthias Ringwald #endif 25600ce3f217SMatthias Ringwald } else { 25610ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 25620ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 25630ce3f217SMatthias Ringwald hci_stack->le_advertisements_active = false; 25640ce3f217SMatthias Ringwald #endif 25650ce3f217SMatthias Ringwald } 25660ce3f217SMatthias Ringwald 25670ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 25680ce3f217SMatthias Ringwald if (!conn){ 25690ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 25700ce3f217SMatthias Ringwald } 25710ce3f217SMatthias Ringwald 25720ce3f217SMatthias Ringwald // no memory, sorry. 25730ce3f217SMatthias Ringwald if (!conn){ 25740ce3f217SMatthias Ringwald return; 25750ce3f217SMatthias Ringwald } 25760ce3f217SMatthias Ringwald 25770ce3f217SMatthias Ringwald conn->state = OPEN; 25780ce3f217SMatthias Ringwald conn->role = packet[6]; 25790ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 25800ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 25810ce3f217SMatthias Ringwald 25820ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 25830ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 25840ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 25850ce3f217SMatthias Ringwald } 25860ce3f217SMatthias Ringwald #endif 25870ce3f217SMatthias Ringwald 2588dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 2589dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 2590dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 2591dde9ff1eSMatthias Ringwald 25920ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 25930ce3f217SMatthias Ringwald 25940ce3f217SMatthias Ringwald // restart timer 25950ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 25960ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 25970ce3f217SMatthias Ringwald 25980ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 25990ce3f217SMatthias Ringwald 26000ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 26010ce3f217SMatthias Ringwald } 26020ce3f217SMatthias Ringwald #endif 26030ce3f217SMatthias Ringwald 260417c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 260517c6fe5cSMatthias 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){ 260617c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 260717c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 26089a8f78c1SMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities if Authenticated is possible 26099a8f78c1SMatthias Ringwald // @see: Core Spec v5.3, Vol 3, Part C, Table 5.7 261017c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 26119a8f78c1SMatthias Ringwald // MITM not possible without keyboard or display 261217c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 261317c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 26149a8f78c1SMatthias Ringwald 26159a8f78c1SMatthias Ringwald // MITM possible if one side has keyboard and the other has keyboard or display 26169a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 26179a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 26189a8f78c1SMatthias Ringwald 26199a8f78c1SMatthias Ringwald // MITM not possible if one side has only display and other side has no keyboard 26209a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 26219a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 262217c6fe5cSMatthias Ringwald } 262317c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 262417c6fe5cSMatthias Ringwald return true; 262517c6fe5cSMatthias Ringwald } 26263817f9dfSMatthias Ringwald 26273817f9dfSMatthias Ringwald static bool btstack_is_null(uint8_t * data, uint16_t size){ 26283817f9dfSMatthias Ringwald uint16_t i; 26293817f9dfSMatthias Ringwald for (i=0; i < size ; i++){ 26303817f9dfSMatthias Ringwald if (data[i] != 0) { 26313817f9dfSMatthias Ringwald return false; 26323817f9dfSMatthias Ringwald } 26333817f9dfSMatthias Ringwald } 26343817f9dfSMatthias Ringwald return true; 26353817f9dfSMatthias Ringwald } 26363817f9dfSMatthias Ringwald 2637b3c4163bSMatthias Ringwald static void hci_ssp_assess_security_on_io_cap_request(hci_connection_t * conn){ 26382dd8985bSMatthias Ringwald // get requested security level 26392dd8985bSMatthias Ringwald gap_security_level_t requested_security_level = conn->requested_security_level; 26402dd8985bSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 26412dd8985bSMatthias Ringwald requested_security_level = LEVEL_4; 26422dd8985bSMatthias Ringwald } 26432dd8985bSMatthias Ringwald 2644b3c4163bSMatthias Ringwald // assess security: LEVEL 4 requires SC 26452dd8985bSMatthias Ringwald // skip this preliminary test if remote features are not available yet to work around potential issue in ESP32 controller 26462dd8985bSMatthias Ringwald if ((requested_security_level == LEVEL_4) && 26472dd8985bSMatthias Ringwald ((conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0) && 26482dd8985bSMatthias Ringwald !hci_remote_sc_enabled(conn)){ 2649b3c4163bSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 2650b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2651b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2652b3c4163bSMatthias Ringwald return; 2653b3c4163bSMatthias Ringwald } 2654b3c4163bSMatthias Ringwald 2655b3c4163bSMatthias Ringwald // assess security based on io capabilities 2656b3c4163bSMatthias Ringwald if (conn->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 2657b3c4163bSMatthias Ringwald // responder: fully validate io caps of both sides as well as OOB data 2658b3c4163bSMatthias Ringwald bool security_possible = false; 2659b3c4163bSMatthias Ringwald security_possible = hci_ssp_security_level_possible_for_io_cap(requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io); 2660b3c4163bSMatthias Ringwald 2661b3c4163bSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 2662b3c4163bSMatthias Ringwald // We assume that both Controller can reach LEVEL 4, if one side has received P-192 and the other has received P-256, 2663b3c4163bSMatthias Ringwald // so we merge the OOB data availability 2664b3c4163bSMatthias Ringwald uint8_t have_oob_data = conn->io_cap_response_oob_data; 2665b3c4163bSMatthias Ringwald if (conn->classic_oob_c_192 != NULL){ 2666b3c4163bSMatthias Ringwald have_oob_data |= 1; 2667b3c4163bSMatthias Ringwald } 2668b3c4163bSMatthias Ringwald if (conn->classic_oob_c_256 != NULL){ 2669b3c4163bSMatthias Ringwald have_oob_data |= 2; 2670b3c4163bSMatthias Ringwald } 2671b3c4163bSMatthias Ringwald // for up to Level 3, either P-192 as well as P-256 will do 2672b3c4163bSMatthias 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 2673b3c4163bSMatthias Ringwald // if remote does not SC, we should not receive P-256 data either 2674b3c4163bSMatthias Ringwald if ((requested_security_level <= LEVEL_3) && (have_oob_data != 0)){ 2675b3c4163bSMatthias Ringwald security_possible = true; 2676b3c4163bSMatthias Ringwald } 2677b3c4163bSMatthias Ringwald // for Level 4, P-256 is needed 2678b3c4163bSMatthias Ringwald if ((requested_security_level == LEVEL_4 && ((have_oob_data & 2) != 0))){ 2679b3c4163bSMatthias Ringwald security_possible = true; 2680b3c4163bSMatthias Ringwald } 2681b3c4163bSMatthias Ringwald #endif 2682b3c4163bSMatthias Ringwald 2683b3c4163bSMatthias Ringwald if (security_possible == false){ 26842dd8985bSMatthias Ringwald log_info("IOCap/OOB insufficient for level %u -> abort", requested_security_level); 2685b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2686b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2687b3c4163bSMatthias Ringwald return; 2688b3c4163bSMatthias Ringwald } 2689b3c4163bSMatthias Ringwald } else { 2690b3c4163bSMatthias Ringwald // initiator: remote io cap not yet, only check if we have ability for MITM protection if requested and OOB is not supported 26911fe968f5SMatthias Ringwald #ifndef ENABLE_CLASSIC_PAIRING_OOB 26921fe968f5SMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 2693b3c4163bSMatthias Ringwald if ((conn->requested_security_level >= LEVEL_3) && (hci_stack->ssp_io_capability >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT)){ 2694b3c4163bSMatthias Ringwald log_info("Level 3+ required, but no input/output -> abort"); 2695b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 2696b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2697b3c4163bSMatthias Ringwald return; 2698b3c4163bSMatthias Ringwald } 2699b3c4163bSMatthias Ringwald #endif 27001fe968f5SMatthias Ringwald #endif 2701b3c4163bSMatthias Ringwald } 2702b3c4163bSMatthias Ringwald 2703b3c4163bSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 2704b3c4163bSMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 2705b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 2706b3c4163bSMatthias Ringwald } else { 2707b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 2708b3c4163bSMatthias Ringwald } 2709b3c4163bSMatthias Ringwald #endif 2710b3c4163bSMatthias Ringwald } 2711b3c4163bSMatthias Ringwald 271217c6fe5cSMatthias Ringwald #endif 271317c6fe5cSMatthias Ringwald 2714c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 27154f781026SMatthias Ringwald 27164f781026SMatthias Ringwald uint16_t event_length = packet[1]; 27174f781026SMatthias Ringwald 27184f781026SMatthias Ringwald // assert packet is complete 27194ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 27204f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 27214f781026SMatthias Ringwald return; 27224f781026SMatthias Ringwald } 27234f781026SMatthias Ringwald 27244f781026SMatthias Ringwald bd_addr_type_t addr_type; 27254f781026SMatthias Ringwald hci_con_handle_t handle; 27264f781026SMatthias Ringwald hci_connection_t * conn; 27274f781026SMatthias Ringwald int i; 27284f781026SMatthias Ringwald int create_connection_cmd; 27294f781026SMatthias Ringwald 27304f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 27315e91d96cSMatthias Ringwald hci_link_type_t link_type; 273248f33f37SMatthias Ringwald bd_addr_t addr; 27334f781026SMatthias Ringwald #endif 27344f781026SMatthias Ringwald 27354f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 27364f781026SMatthias Ringwald 27374f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 27384f781026SMatthias Ringwald 27394f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 27404f781026SMatthias Ringwald handle_command_complete_event(packet, size); 274156cf178bSmatthias.ringwald break; 274256cf178bSmatthias.ringwald 27437ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 27446bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 27456bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2746229331c6SMatthias Ringwald 2747229331c6SMatthias Ringwald // check command status to detected failed outgoing connections 2748c57fa566SMatthias Ringwald create_connection_cmd = 0; 2749c57fa566SMatthias Ringwald #ifdef ENABLE_CLASSIC 2750c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_create_connection)){ 2751c57fa566SMatthias Ringwald create_connection_cmd = 1; 2752c57fa566SMatthias Ringwald } 27531f34df2cSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_accept_synchronous_connection)){ 27541f34df2cSMatthias Ringwald create_connection_cmd = 1; 27551f34df2cSMatthias Ringwald } 2756c57fa566SMatthias Ringwald #endif 2757c57fa566SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2758c57fa566SMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_le_create_connection)){ 2759c57fa566SMatthias Ringwald create_connection_cmd = 1; 2760c57fa566SMatthias Ringwald } 2761c57fa566SMatthias Ringwald #endif 2762c57fa566SMatthias Ringwald if (create_connection_cmd) { 2763229331c6SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 27648da98cbeSMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 27656ea9315cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 27666ea9315cSMatthias 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); 2767229331c6SMatthias Ringwald 2768229331c6SMatthias Ringwald // reset outgoing address info 2769229331c6SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2770229331c6SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2771229331c6SMatthias Ringwald 27726ea9315cSMatthias Ringwald // on error 27736ea9315cSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2774f75e06adSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 27756ea9315cSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 27766ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2777f7e6a692SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 27786ea9315cSMatthias Ringwald } 2779f75e06adSMatthias Ringwald #endif 2780229331c6SMatthias Ringwald // error => outgoing connection failed 27816ea9315cSMatthias Ringwald if (conn != NULL){ 2782229331c6SMatthias Ringwald hci_handle_connection_failed(conn, status); 2783229331c6SMatthias Ringwald } 2784229331c6SMatthias Ringwald } 27856ea9315cSMatthias Ringwald } 2786beb3c81dSMatthias Ringwald 2787beb3c81dSMatthias Ringwald #ifdef ENABLE_CLASSIC 2788beb3c81dSMatthias Ringwald if (HCI_EVENT_IS_COMMAND_STATUS(packet, hci_inquiry)) { 2789beb3c81dSMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2790beb3c81dSMatthias Ringwald log_info("command status (inquiry), status %x", status); 2791beb3c81dSMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 2792beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2793beb3c81dSMatthias Ringwald } else { 2794beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2795beb3c81dSMatthias Ringwald } 2796beb3c81dSMatthias Ringwald } 2797beb3c81dSMatthias Ringwald #endif 27987ec5eeaaSmatthias.ringwald break; 27997ec5eeaaSmatthias.ringwald 28002e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 28019784dac1SMatthias Ringwald if (size < 3) return; 28029784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 28034ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 28049784dac1SMatthias Ringwald uint16_t offset = 3; 28059784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 28064ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 28074ea43905SMatthias Ringwald offset += 2u; 2808f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 28094ea43905SMatthias Ringwald offset += 2u; 28102e440c8aS[email protected] 28115061f3afS[email protected] conn = hci_connection_for_handle(handle); 281256cf178bSmatthias.ringwald if (!conn){ 28139da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 281456cf178bSmatthias.ringwald continue; 281556cf178bSmatthias.ringwald } 281623bed257S[email protected] 2817ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets){ 2818ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 2819e35edcc1S[email protected] } else { 2820ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 2821ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 2822e35edcc1S[email protected] } 2823ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 2824760b20efSMatthias Ringwald 2825760b20efSMatthias Ringwald #ifdef ENABLE_CLASSIC 2826760b20efSMatthias Ringwald // For SCO, we do the can_send_now_check here 2827760b20efSMatthias Ringwald hci_notify_if_sco_can_send_now(); 2828760b20efSMatthias Ringwald #endif 282956cf178bSmatthias.ringwald } 28306772a24cSmatthias.ringwald break; 28312e440c8aS[email protected] } 283235454696SMatthias Ringwald 283335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2834f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 2835f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 2836f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2837f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2838f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2839f5875de5SMatthias Ringwald } 2840f5875de5SMatthias Ringwald break; 2841b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 2842b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 2843b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 2844b7f1ee76SMatthias Ringwald } 2845b7f1ee76SMatthias Ringwald break; 28461f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 2847724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 28485e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 284972cf8859SMatthias Ringwald 285072cf8859SMatthias Ringwald // CVE-2020-26555: reject incoming connection from device with same BD ADDR 285172cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0){ 285272cf8859SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 285372cf8859SMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 285472cf8859SMatthias Ringwald break; 285572cf8859SMatthias Ringwald } 285672cf8859SMatthias Ringwald 285707e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 28585e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 28594536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 286007e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 286107e010b6SMilanka Ringwald break; 286207e010b6SMilanka Ringwald } 286307e010b6SMilanka Ringwald } 286407e010b6SMilanka Ringwald 286537eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 28665e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 28675e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 28682e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 28691f7b95a1Smatthias.ringwald if (!conn) { 28705293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 28711f7b95a1Smatthias.ringwald } 2872ce4c8fabSmatthias.ringwald if (!conn) { 2873ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 28744536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 2875058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 2876f23819bcSMatthias Ringwald hci_run(); 2877f23819bcSMatthias Ringwald // avoid event to higher layer 2878f23819bcSMatthias Ringwald return; 2879ce4c8fabSmatthias.ringwald } 28805cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 288132ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 2882f3a16b9aSMatthias Ringwald // store info about eSCO 28835e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 288476ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 2885f3a16b9aSMatthias Ringwald } 288632ab9390Smatthias.ringwald hci_run(); 28871f7b95a1Smatthias.ringwald break; 28881f7b95a1Smatthias.ringwald 28896772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 2890fe1ed1b8Smatthias.ringwald // Connection management 2891724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 28929da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 2893f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 28942e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 2895fe1ed1b8Smatthias.ringwald if (conn) { 2896b448a0e7Smatthias.ringwald if (!packet[2]){ 2897c8e4258aSmatthias.ringwald conn->state = OPEN; 2898f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 28996909f064SMatthias Ringwald 2900e6c51921SMatthias Ringwald // queue set supervision timeout if we're master 2901d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 29028daf94bcSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT); 29036909f064SMatthias Ringwald } 29046909f064SMatthias Ringwald 2905c785ef68Smatthias.ringwald // restart timer 2906528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 2907528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 2908c785ef68Smatthias.ringwald 290918c4a0a3SMatthias Ringwald // trigger remote features for dedicated bonding 291018c4a0a3SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 291118c4a0a3SMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 291218c4a0a3SMatthias Ringwald } 291318c4a0a3SMatthias Ringwald 29149da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 291543bfb1bdSmatthias.ringwald 291643bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 2917b448a0e7Smatthias.ringwald } else { 29180bbba85bSMatthias Ringwald // connection failed 29190bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 2920fe1ed1b8Smatthias.ringwald } 2921fe1ed1b8Smatthias.ringwald } 29226772a24cSmatthias.ringwald break; 2923fe1ed1b8Smatthias.ringwald 292444d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 2925724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 29261f34df2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 29271f34df2cSMatthias Ringwald log_info("Synchronous Connection Complete for %p (status=%u) %s", conn, packet[2], bd_addr_to_str(addr)); 29281a06f663S[email protected] if (packet[2]){ 292944d0e3d5S[email protected] // connection failed 29301f34df2cSMatthias Ringwald if (conn){ 29311f34df2cSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 29321f34df2cSMatthias Ringwald } 293344d0e3d5S[email protected] break; 293444d0e3d5S[email protected] } 2935e35edcc1S[email protected] if (!conn) { 2936e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 2937e35edcc1S[email protected] } 2938e35edcc1S[email protected] if (!conn) { 2939e35edcc1S[email protected] break; 2940e35edcc1S[email protected] } 29411a06f663S[email protected] conn->state = OPEN; 2942f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 2943ee752bb8SMatthias Ringwald 2944ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2945ee752bb8SMatthias Ringwald // update SCO 2946ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 2947ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 2948ee752bb8SMatthias Ringwald } 2949f234b250SMatthias Ringwald // trigger can send now 2950f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 29511972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 2952f234b250SMatthias Ringwald } 2953ee752bb8SMatthias Ringwald #endif 2954cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 2955cb70c5abSMatthias Ringwald // configure sco transport 2956cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 2957cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 2958cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 2959cb70c5abSMatthias Ringwald } 2960cb70c5abSMatthias Ringwald #endif 296144d0e3d5S[email protected] break; 296244d0e3d5S[email protected] 2963afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 2964f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 2965afd4e962S[email protected] conn = hci_connection_for_handle(handle); 2966afd4e962S[email protected] if (!conn) break; 2967afd4e962S[email protected] if (!packet[2]){ 29682f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 29692f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 29702f5c44baSMatthias Ringwald 29715ccef624SMatthias Ringwald // read extended features if possible 29721ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES) 29731ffa425cSMatthias Ringwald && ((conn->remote_supported_features[0] & 2) != 0)) { 29745ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 29755ccef624SMatthias Ringwald break; 29765ccef624SMatthias Ringwald } 29775ccef624SMatthias Ringwald } 29785ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 29795ccef624SMatthias Ringwald break; 29805ccef624SMatthias Ringwald 29815ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 29825ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 29835ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 29845ccef624SMatthias Ringwald if (!conn) break; 29855ccef624SMatthias Ringwald // status = ok, page = 1 298650c51a77SMatthias Ringwald if (!packet[2]) { 298750c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 298850c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 29895ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 299050c51a77SMatthias Ringwald bool done = false; 299150c51a77SMatthias Ringwald switch (page_number){ 299250c51a77SMatthias Ringwald case 1: 29932f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 299450c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 299550c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 299650c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 299750c51a77SMatthias Ringwald } else { 299850c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 299950c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 300050c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 300150c51a77SMatthias Ringwald } 300250c51a77SMatthias Ringwald done = true; 300350c51a77SMatthias Ringwald } 300450c51a77SMatthias Ringwald break; 300550c51a77SMatthias Ringwald case 2: 300650c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 300750c51a77SMatthias Ringwald done = true; 300850c51a77SMatthias Ringwald break; 300950c51a77SMatthias Ringwald default: 301050c51a77SMatthias Ringwald break; 301150c51a77SMatthias Ringwald } 301250c51a77SMatthias Ringwald if (!done) break; 3013afd4e962S[email protected] } 3014de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 3015afd4e962S[email protected] break; 3016afd4e962S[email protected] 30177fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 3018308eeaffSMatthias Ringwald #ifndef ENABLE_EXPLICIT_LINK_KEY_REPLY 301963c0b2fdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 302063c0b2fdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 302163c0b2fdSMatthias Ringwald if (!conn) break; 302263c0b2fdSMatthias Ringwald 302363c0b2fdSMatthias Ringwald // lookup link key in db if not cached 302463c0b2fdSMatthias Ringwald if ((conn->link_key_type == INVALID_LINK_KEY) && (hci_stack->link_key_db != NULL)){ 302563c0b2fdSMatthias Ringwald hci_stack->link_key_db->get_link_key(conn->address, conn->link_key, &conn->link_key_type); 302663c0b2fdSMatthias Ringwald } 302763c0b2fdSMatthias Ringwald 302863c0b2fdSMatthias Ringwald // response sent by hci_run() 302963c0b2fdSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_HANDLE_LINK_KEY_REQUEST; 303063c0b2fdSMatthias Ringwald #endif 3031608f51bbSMatthias Ringwald break; 30327fde4af9Smatthias.ringwald 30339ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 30341714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 3035f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30369ab95c90S[email protected] if (!conn) break; 30371714cbbdSMatthias Ringwald 30381714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 30391714cbbdSMatthias Ringwald 30403817f9dfSMatthias Ringwald // CVE-2020-26555: ignore NULL link key 30413817f9dfSMatthias Ringwald // default link_key_type = INVALID_LINK_KEY asserts that NULL key won't be used for encryption 30423817f9dfSMatthias Ringwald if (btstack_is_null(&packet[8], 16)) break; 30433817f9dfSMatthias Ringwald 30447bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 30459ab95c90S[email protected] // Change Connection Encryption keeps link key type 30469ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 30479ab95c90S[email protected] conn->link_key_type = link_key_type; 30489ab95c90S[email protected] } 30493817f9dfSMatthias Ringwald 3050e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 3051e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 3052e9f98c4aSMatthias Ringwald 3053bec5f683SMatthias Ringwald // only store link key: 3054bec5f683SMatthias Ringwald // - if bondable enabled 3055bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 30566edaed7fSMatthias Ringwald // - if security level sufficient 30576edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 3058bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 3059bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 3060532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 3061532454f9SMatthias Ringwald if (!remote_bonding){ 3062bec5f683SMatthias Ringwald break; 3063bec5f683SMatthias Ringwald } 3064bec5f683SMatthias Ringwald } 306555597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 30667fde4af9Smatthias.ringwald break; 30679ab95c90S[email protected] } 30687fde4af9Smatthias.ringwald 30697fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 30701714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 30711714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 30721714cbbdSMatthias Ringwald if (!conn) break; 30731714cbbdSMatthias Ringwald 30741714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 3075a800d95eSMatthias Ringwald // abort pairing if: non-bondable mode (pin code request is not forwarded to app) 30763a9fb326S[email protected] if (!hci_stack->bondable ){ 30778daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 30781714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 3079f8fb5f6eS[email protected] hci_run(); 3080f8fb5f6eS[email protected] return; 30814c57c146S[email protected] } 3082a800d95eSMatthias Ringwald // abort pairing if: LEVEL_4 required (pin code request is not forwarded to app) 3083a800d95eSMatthias Ringwald if ((hci_stack->gap_secure_connections_only_mode) || (conn->requested_security_level == LEVEL_4)){ 3084a800d95eSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 308550dcc63cSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 308650dcc63cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 308750dcc63cSMatthias Ringwald hci_run(); 308850dcc63cSMatthias Ringwald return; 308950dcc63cSMatthias Ringwald } 30907fde4af9Smatthias.ringwald break; 30917fde4af9Smatthias.ringwald 309250d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 309350d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 309450d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 309550d7398cSMatthias Ringwald if (!conn) break; 30961714cbbdSMatthias Ringwald 30978daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE); 30981714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 309950d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 3100e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 3101d22b82d6SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3102d22b82d6SMatthias Ringwald conn->io_cap_response_oob_data = hci_event_io_capability_response_get_oob_data_present(packet); 3103d22b82d6SMatthias Ringwald #endif 310450d7398cSMatthias Ringwald break; 310550d7398cSMatthias Ringwald 31061d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 31071714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 31081714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 31091714cbbdSMatthias Ringwald if (!conn) break; 31101714cbbdSMatthias Ringwald 3111c950c316SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 31129671e615SMatthias Ringwald hci_connection_timestamp(conn); 31131714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 3114dbe1a790S[email protected] break; 3115dbe1a790S[email protected] 31161849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 31171849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 31187ca4a7edSMatthias Ringwald hci_event_remote_oob_data_request_get_bd_addr(packet, addr); 31197ca4a7edSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 31207ca4a7edSMatthias Ringwald if (!conn) break; 31217ca4a7edSMatthias Ringwald 31227ca4a7edSMatthias Ringwald hci_connection_timestamp(conn); 31237ca4a7edSMatthias Ringwald 31247ca4a7edSMatthias Ringwald hci_pairing_started(conn, true); 31257ca4a7edSMatthias Ringwald 31267ca4a7edSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 31271849becdSMatthias Ringwald break; 31281849becdSMatthias Ringwald #endif 31291849becdSMatthias Ringwald 3130dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 313117c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 3132367aedc0SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3133367aedc0SMatthias Ringwald if (!conn) break; 3134367aedc0SMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 3135367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 31368daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 3137367aedc0SMatthias Ringwald }; 3138367aedc0SMatthias Ringwald } else { 3139367aedc0SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3140367aedc0SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 3141367aedc0SMatthias Ringwald // don't forward event to app 3142367aedc0SMatthias Ringwald hci_run(); 3143367aedc0SMatthias Ringwald return; 3144367aedc0SMatthias Ringwald } 3145dbe1a790S[email protected] break; 3146dbe1a790S[email protected] 3147dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 3148367aedc0SMatthias Ringwald // Pairing using Passkey results in MITM protection. If Level 4 is required, support for SC is validated on IO Cap Request 3149367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 31508daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 3151367aedc0SMatthias Ringwald }; 31521d6b20aeS[email protected] break; 31531849becdSMatthias Ringwald 31543dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 31553dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 31563dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 31573dce6128SMatthias Ringwald if (!conn) break; 31583dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 31593dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 31603dce6128SMatthias Ringwald break; 316135454696SMatthias Ringwald #endif 31621d6b20aeS[email protected] 3163f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 3164254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 3165f0944df2S[email protected] conn = hci_connection_for_handle(handle); 3166f0944df2S[email protected] if (!conn) break; 31674ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 3168254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 3169254b78eeSMatthias Ringwald if (encryption_enabled){ 3170573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 3171573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 31728daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 3173573897a0SMatthias Ringwald } 3174573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 3175573897a0SMatthias Ringwald else { 3176fcaf38b9SMatthias Ringwald 3177fcaf38b9SMatthias Ringwald // dedicated bonding: send result and disconnect 3178fcaf38b9SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 3179fcaf38b9SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 3180fcaf38b9SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 3181fcaf38b9SMatthias Ringwald conn->bonding_status = packet[2]; 3182fcaf38b9SMatthias Ringwald break; 3183fcaf38b9SMatthias Ringwald } 3184fcaf38b9SMatthias Ringwald 3185254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 318636c3546bSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type); 3187254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 3188edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 3189254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 31909ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 3191254b78eeSMatthias Ringwald break; 3192254b78eeSMatthias Ringwald } 3193254b78eeSMatthias Ringwald 3194aa2fe986SMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 3195aa2fe986SMatthias Ringwald if (connected_uses_aes_ccm){ 3196aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3197aa2fe986SMatthias Ringwald } 3198aa2fe986SMatthias Ringwald 3199aa2fe986SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3200aa2fe986SMatthias Ringwald // work around for issue with PTS dongle 3201aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3202aa2fe986SMatthias Ringwald #endif 32031ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE)){ 32046e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 3205573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 32066e058d3fSMatthias Ringwald } else { 32076e058d3fSMatthias Ringwald // if not, pretend everything is perfect 32089866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 32096e058d3fSMatthias Ringwald } 3210573897a0SMatthias Ringwald } 3211573897a0SMatthias Ringwald #endif 3212f0944df2S[email protected] } else { 32138daf94bcSMatthias Ringwald conn->authentication_flags &= ~AUTH_FLAG_CONNECTION_ENCRYPTED; 3214f0944df2S[email protected] } 3215ad83dc6aS[email protected] } 3216573897a0SMatthias Ringwald 3217f0944df2S[email protected] break; 3218f0944df2S[email protected] 321935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 32201eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 3221abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 32221eb2563eS[email protected] conn = hci_connection_for_handle(handle); 32231eb2563eS[email protected] if (!conn) break; 3224ad83dc6aS[email protected] 3225dbd5dcc3SMatthias Ringwald // clear authentication active flag 3226dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 32271714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 3228dbd5dcc3SMatthias Ringwald 3229abdad579SMatthias Ringwald // authenticated only if auth status == 0 3230abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 3231abdad579SMatthias Ringwald // authenticated 32328daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3233abdad579SMatthias Ringwald 3234131ef17aSMatthias Ringwald // If not already encrypted, start encryption 32358daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0){ 32361eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3237ad83dc6aS[email protected] break; 3238ad83dc6aS[email protected] } 3239abdad579SMatthias Ringwald } 3240abdad579SMatthias Ringwald 3241abdad579SMatthias Ringwald // emit updated security level 32421eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 32431eb2563eS[email protected] break; 32441714cbbdSMatthias Ringwald 32451714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 32461714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 32471714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 32481714cbbdSMatthias Ringwald if (!conn) break; 32491714cbbdSMatthias Ringwald 3250aa2fe986SMatthias Ringwald // treat successfully paired connection as authenticated 3251aa2fe986SMatthias Ringwald if (hci_event_simple_pairing_complete_get_status(packet) == ERROR_CODE_SUCCESS){ 3252f3aafff1SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3253aa2fe986SMatthias Ringwald } 3254aa2fe986SMatthias Ringwald 32551714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 32561714cbbdSMatthias Ringwald break; 325735454696SMatthias Ringwald #endif 325834d2123cS[email protected] 325986805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 3260ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 3261ccda6e14S[email protected] // see end of function, too. 3262a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 3263a4f30ec0S[email protected] if (packet[2]) break; // status != 0 3264f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 326581d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 32664ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3267c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 32684ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 326981d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 3270c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 3271c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 327281d2bdb2SMatthias Ringwald if (release_buffer){ 327381d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 327481d2bdb2SMatthias Ringwald } 3275c6a37cfdSMatthias Ringwald } 3276c6a37cfdSMatthias Ringwald } 327735454696SMatthias Ringwald 3278c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 3279c6a37cfdSMatthias Ringwald if (!conn) break; 32801714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 32811714cbbdSMatthias Ringwald // pairing failed if it was ongoing 32821714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 32831714cbbdSMatthias Ringwald #endif 3284046ec007SMatthias Ringwald 3285046ec007SMatthias Ringwald // emit dedicatd bonding event 3286046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 3287046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 3288046ec007SMatthias Ringwald } 3289046ec007SMatthias Ringwald 3290459d27b9SMatthias Ringwald // mark connection for shutdown, stop timers, reset state 3291459d27b9SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 3292459d27b9SMatthias Ringwald hci_connection_stop_timer(conn); 3293459d27b9SMatthias Ringwald hci_connection_init(conn); 3294459d27b9SMatthias Ringwald 329535454696SMatthias Ringwald #ifdef ENABLE_BLE 3296d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3297046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 32982b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 3299bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 33009a2e4658SMatthias Ringwald } 330135454696SMatthias Ringwald #endif 3302d70217a2SMatthias Ringwald #endif 3303ccda6e14S[email protected] break; 33046772a24cSmatthias.ringwald 3305c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 3306313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 3307d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 3308c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 33097586ee35S[email protected] } else { 33107586ee35S[email protected] // if no special requests, just reboot stack 33117586ee35S[email protected] hci_power_control_off(); 33127586ee35S[email protected] hci_power_control_on(); 3313c68bdf90Smatthias.ringwald } 3314c68bdf90Smatthias.ringwald break; 3315c68bdf90Smatthias.ringwald 33160e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 33175cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 33185cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 3319c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 3320f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 3321c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3322c4c88f1bSJakob Krantz if (!conn) break; 33235cf766e8SMatthias Ringwald conn->role = packet[9]; 33245cf766e8SMatthias Ringwald break; 33250e6f3837SMatthias Ringwald #endif 33265cf766e8SMatthias Ringwald 332763fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 3328d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 33294fa24b5fS[email protected] if (hci_transport_synchronous()) { 333063fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 33314fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 33324fa24b5fS[email protected] } 333381d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 3334d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 3335d051460cS[email protected] hci_release_packet_buffer(); 3336701e3307SMatthias Ringwald 333763fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 333863fa3374SMatthias Ringwald 333935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 334063fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 3341701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 334235454696SMatthias Ringwald #endif 33436b4af23dS[email protected] break; 33446b4af23dS[email protected] 334535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 334663fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 334763fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 33481972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 334963fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 335063fa3374SMatthias Ringwald return; 33511cfb383eSMatthias Ringwald 33521cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 33531cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 33541cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 33551cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 33569784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 33571cfb383eSMatthias Ringwald break; 335835454696SMatthias Ringwald #endif 335963fa3374SMatthias Ringwald 3360a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 33615909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 33625909f7f2Smatthias.ringwald switch (packet[2]){ 3363d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 336457c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 33656fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 336657c9da5bS[email protected] le_handle_advertisement_report(packet, size); 33677bdc6798S[email protected] break; 3368*a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 3369*a0698cb4SMatthias Ringwald case HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT: 3370*a0698cb4SMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 3371*a0698cb4SMatthias Ringwald le_handle_extended_advertisement_report(packet, size); 3372*a0698cb4SMatthias Ringwald break; 3373*a0698cb4SMatthias Ringwald #endif 3374d70217a2SMatthias Ringwald #endif 33755909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 33760ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 33775909f7f2Smatthias.ringwald break; 33785909f7f2Smatthias.ringwald 3379f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3380c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3381c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3382c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3383c9db5c21SMilanka Ringwald if (!conn) break; 3384c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3385c9db5c21SMilanka Ringwald break; 338665a46ef3S[email protected] 338773cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 338873cd8a2aSMatthias Ringwald // connection 338973cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 339073cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 339173cd8a2aSMatthias Ringwald if (conn) { 339273cd8a2aSMatthias Ringwald // read arguments 339373cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 339473cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 339573cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 339673cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 339773cd8a2aSMatthias Ringwald 339873cd8a2aSMatthias Ringwald // validate against current connection parameter range 339973cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 340073cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 340173cd8a2aSMatthias 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); 340273cd8a2aSMatthias Ringwald if (update_parameter){ 340373cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 340473cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 340573cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 340673cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 340773cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 340873cd8a2aSMatthias Ringwald } else { 3409c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 341073cd8a2aSMatthias Ringwald } 341173cd8a2aSMatthias Ringwald } 341273cd8a2aSMatthias Ringwald break; 34130f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 34140f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 34150f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 34160f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 34170f3b27c5SMatthias Ringwald if (conn) { 34180f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 34190f3b27c5SMatthias Ringwald } 34200f3b27c5SMatthias Ringwald break; 34210f3b27c5SMatthias Ringwald #endif 34225909f7f2Smatthias.ringwald default: 34235909f7f2Smatthias.ringwald break; 34245909f7f2Smatthias.ringwald } 34255909f7f2Smatthias.ringwald break; 34265909f7f2Smatthias.ringwald #endif 34270b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 34280b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 34290b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 34300b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 34310b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 34320b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 34330b36101dSMatthias Ringwald break; 34340b36101dSMatthias Ringwald default: 34350b36101dSMatthias Ringwald break; 34360b36101dSMatthias Ringwald } 34370b36101dSMatthias Ringwald break; 34386772a24cSmatthias.ringwald default: 34396772a24cSmatthias.ringwald break; 3440fe1ed1b8Smatthias.ringwald } 3441fe1ed1b8Smatthias.ringwald 344267c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 344389db417bSmatthias.ringwald 344486805605S[email protected] // notify upper stack 3445d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 344694ab26f8Smatthias.ringwald 344786805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 3448797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 3449f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 345005ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 3451046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 3452797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 345305ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 3454b0136355SMatthias Ringwald } 345586805605S[email protected] } 345686805605S[email protected] 345794ab26f8Smatthias.ringwald // execute main loop 345894ab26f8Smatthias.ringwald hci_run(); 345916833f0aSmatthias.ringwald } 346016833f0aSmatthias.ringwald 346135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 34626f28d2eeSMatthias Ringwald 34636be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 34646f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 34656f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 34666f28d2eeSMatthias Ringwald 34676f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 346859c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 34695a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 34706f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 34716f28d2eeSMatthias Ringwald if (!conn) return; 34726f28d2eeSMatthias Ringwald 3473afaddd67SMatthias Ringwald // trigger send 3474afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 3475e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 3476a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 3477e4157653SMatthias Ringwald conn->sco_tx_ready++; 3478e4157653SMatthias Ringwald } 3479afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 34806f28d2eeSMatthias Ringwald } 34816f28d2eeSMatthias Ringwald 348259c33575SMatthias Ringwald 348359c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 348459c33575SMatthias Ringwald 34856f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 348659c33575SMatthias Ringwald 34876f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 348859c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 348959c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 349059c33575SMatthias Ringwald 349159c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 349259c33575SMatthias Ringwald 349359c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 3494ddbec4ceSMatthias Ringwald btstack_run_loop_remove_timer(timer); 349559c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 349659c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 349759c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 349859c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 34996f28d2eeSMatthias Ringwald } 35006be3cf7fSMatthias Ringwald #endif 35016f28d2eeSMatthias Ringwald 3502c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 35030b3f95dfSMatthias Ringwald // lookup connection struct 35042b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 35052b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 35060b3f95dfSMatthias Ringwald if (!conn) return; 35070b3f95dfSMatthias Ringwald 35086be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3509760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 3510760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 3511a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 3512760b20efSMatthias Ringwald packet[2] = 0x3c; 3513760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 3514760b20efSMatthias Ringwald size = 63; 35150b3f95dfSMatthias Ringwald } 35160b3f95dfSMatthias Ringwald } 3517760b20efSMatthias Ringwald 3518f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 3519f234b250SMatthias Ringwald // Nothing to do 3520f234b250SMatthias Ringwald } else { 352149205f5dSMatthias 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); 3522760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 35236f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 352459c33575SMatthias Ringwald 352559c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 352659c33575SMatthias Ringwald // ignore first 10 packets 35276f28d2eeSMatthias Ringwald conn->sco_rx_count++; 352859c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 352959c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 353059c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 35316f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 35326f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 353359c33575SMatthias Ringwald conn->sco_rx_count = 0; 353459c33575SMatthias Ringwald sco_schedule_tx(conn); 353559c33575SMatthias Ringwald } 353659c33575SMatthias Ringwald } else { 353759c33575SMatthias Ringwald // track expected arrival timme 353859c33575SMatthias Ringwald conn->sco_rx_count++; 353959c33575SMatthias Ringwald conn->sco_rx_ms += 7; 354059c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 354159c33575SMatthias Ringwald if (delta > 0){ 354259c33575SMatthias Ringwald conn->sco_rx_ms++; 354359c33575SMatthias Ringwald } 354459c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 35456f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 35466f28d2eeSMatthias Ringwald } 3547760b20efSMatthias Ringwald } 3548f234b250SMatthias Ringwald } 35496be3cf7fSMatthias Ringwald #endif 35506be3cf7fSMatthias Ringwald 35510b3f95dfSMatthias Ringwald // deliver to app 35520b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 35530b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 35540b3f95dfSMatthias Ringwald } 35550b3f95dfSMatthias Ringwald 3556ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3557ed325439SMatthias Ringwald // We can send one packet for each received packet 3558ed325439SMatthias Ringwald conn->sco_tx_ready++; 3559ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 3560ed325439SMatthias Ringwald #endif 3561ed325439SMatthias Ringwald 35620b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 35632b838201SMatthias Ringwald conn->num_packets_completed++; 35642b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 35652b838201SMatthias Ringwald hci_run(); 35662b838201SMatthias Ringwald #endif 3567c91d150bS[email protected] } 356835454696SMatthias Ringwald #endif 3569c91d150bS[email protected] 35700a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 35715bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 357210e830c9Smatthias.ringwald switch (packet_type) { 357310e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 357410e830c9Smatthias.ringwald event_handler(packet, size); 357510e830c9Smatthias.ringwald break; 357610e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 357710e830c9Smatthias.ringwald acl_handler(packet, size); 357810e830c9Smatthias.ringwald break; 357935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3580c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 3581c91d150bS[email protected] sco_handler(packet, size); 3582202c8a4cSMatthias Ringwald break; 358335454696SMatthias Ringwald #endif 358410e830c9Smatthias.ringwald default: 358510e830c9Smatthias.ringwald break; 358610e830c9Smatthias.ringwald } 358710e830c9Smatthias.ringwald } 358810e830c9Smatthias.ringwald 3589d6b06661SMatthias Ringwald /** 3590d6b06661SMatthias Ringwald * @brief Add event packet handler. 3591d6b06661SMatthias Ringwald */ 3592d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 3593d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 3594d6b06661SMatthias Ringwald } 3595d6b06661SMatthias Ringwald 359667f708e0SMatthias Ringwald /** 359767f708e0SMatthias Ringwald * @brief Remove event packet handler. 359867f708e0SMatthias Ringwald */ 359967f708e0SMatthias Ringwald void hci_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){ 360067f708e0SMatthias Ringwald btstack_linked_list_remove(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 360167f708e0SMatthias Ringwald } 3602d6b06661SMatthias Ringwald 3603fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 36043d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 3605fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 360616833f0aSmatthias.ringwald } 360716833f0aSmatthias.ringwald 360835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 36098abbe8b5SMatthias Ringwald /** 36108abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 36118abbe8b5SMatthias Ringwald */ 36123d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 36138abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 36148abbe8b5SMatthias Ringwald } 361535454696SMatthias Ringwald #endif 36168abbe8b5SMatthias Ringwald 361771de195eSMatthias Ringwald static void hci_state_reset(void){ 3618595bdbfbS[email protected] // no connections yet 3619595bdbfbS[email protected] hci_stack->connections = NULL; 362074308b23Smatthias.ringwald 362174308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 362274308b23Smatthias.ringwald // hci_stack->discoverable = 0; 362374308b23Smatthias.ringwald // hci_stack->connectable = 0; 362474308b23Smatthias.ringwald // hci_stack->bondable = 1; 3625b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 3626595bdbfbS[email protected] 362744935e40S[email protected] // buffer is free 362802c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 362944935e40S[email protected] 3630595bdbfbS[email protected] // no pending cmds 3631595bdbfbS[email protected] hci_stack->decline_reason = 0; 3632595bdbfbS[email protected] 3633c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 3634c214d65bSMatthias Ringwald 3635bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 3636496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 363732a12730SMatthias Ringwald hci_stack->page_timeout = 0x6000; // ca. 15 sec 363832a12730SMatthias Ringwald 3639baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic = 364059d59ecfSMatthias Ringwald GAP_TASK_SET_DEFAULT_LINK_POLICY | 364159d59ecfSMatthias Ringwald GAP_TASK_SET_CLASS_OF_DEVICE | 364259d59ecfSMatthias Ringwald GAP_TASK_SET_LOCAL_NAME | 3643bc2dcc03SMatthias Ringwald GAP_TASK_SET_EIR_DATA | 364432a12730SMatthias Ringwald GAP_TASK_WRITE_SCAN_ENABLE | 364532a12730SMatthias Ringwald GAP_TASK_WRITE_PAGE_TIMEOUT; 3646bea424a5SMatthias Ringwald #endif 3647bea424a5SMatthias Ringwald 3648cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 36498d5f0979SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 36501ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 3651cf01e888SMatthias Ringwald #endif 3652cf01e888SMatthias Ringwald 3653595bdbfbS[email protected] // LE 3654b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 3655b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 3656b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 3657d70217a2SMatthias Ringwald #endif 3658d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3659a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 366073799937SMatthias Ringwald hci_stack->le_scanning_param_update = true; 3661b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3662d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3663e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 3664d70217a2SMatthias Ringwald #endif 3665a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3666a61834b6SMatthias Ringwald hci_stack->le_advertisements_active = false; 3667a61834b6SMatthias Ringwald if ((hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_PARAMS_SET) != 0){ 3668a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 3669a61834b6SMatthias Ringwald } 3670a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 3671a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 3672a61834b6SMatthias Ringwald } 3673a61834b6SMatthias Ringwald #endif 3674595bdbfbS[email protected] } 3675595bdbfbS[email protected] 367635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 36772d5e09d6SMatthias Ringwald /** 36782d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 36792d5e09d6SMatthias Ringwald */ 36802d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 36812d5e09d6SMatthias Ringwald // store and open remote device db 36822d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 36832d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 36842d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 36852d5e09d6SMatthias Ringwald } 36862d5e09d6SMatthias Ringwald } 368735454696SMatthias Ringwald #endif 36882d5e09d6SMatthias Ringwald 36892d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 3690475c8125Smatthias.ringwald 36913a9fb326S[email protected] #ifdef HAVE_MALLOC 36923a9fb326S[email protected] if (!hci_stack) { 36933a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 36943a9fb326S[email protected] } 36953a9fb326S[email protected] #else 36963a9fb326S[email protected] hci_stack = &hci_stack_static; 36973a9fb326S[email protected] #endif 369866fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 36993a9fb326S[email protected] 3700475c8125Smatthias.ringwald // reference to use transport layer implementation 37013a9fb326S[email protected] hci_stack->hci_transport = transport; 3702475c8125Smatthias.ringwald 370311e23e5fSmatthias.ringwald // reference to used config 37043a9fb326S[email protected] hci_stack->config = config; 370511e23e5fSmatthias.ringwald 370626a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 370726a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 370826a9b6daSMatthias Ringwald 37098fcba05dSmatthias.ringwald // max acl payload size defined in config.h 37103a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 37118fcba05dSmatthias.ringwald 371216833f0aSmatthias.ringwald // register packet handlers with transport 371310e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 3714f5454fc6Smatthias.ringwald 37153a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 3716e2386ba1S[email protected] 3717e2386ba1S[email protected] // class of device 37183a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 3719a45d6b9fS[email protected] 3720f20168b8Smatthias.ringwald // bondable by default 3721f20168b8Smatthias.ringwald hci_stack->bondable = 1; 3722f20168b8Smatthias.ringwald 3723e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 372463168530SMatthias Ringwald // classic name 372563168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 3726c4c88f1bSJakob Krantz 3727c4c88f1bSJakob Krantz // Master slave policy 3728c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 3729170fafaeSMatthias Ringwald 3730b4eb4420SMatthias Ringwald // Allow Role Switch 3731b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 3732b4eb4420SMatthias Ringwald 373378315a58SMatthias Ringwald // Default / minimum security level = 2 373478315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 373578315a58SMatthias Ringwald 37365dbec6b3SMatthias Ringwald // Default Security Mode 4 37375dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 37385dbec6b3SMatthias Ringwald 3739cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 3740cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 3741d821984bSMatthias Ringwald 3742d821984bSMatthias Ringwald // Link Supervision Timeout 3743d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 3744d821984bSMatthias Ringwald 3745e9f343c8SMatthias Ringwald #endif 374663168530SMatthias Ringwald 374763048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 37483a9fb326S[email protected] hci_stack->ssp_enable = 1; 37493a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 37503a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 37513a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 375269a97523S[email protected] 37535d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 37545d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 37555d23aae8SMatthias Ringwald 3756fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 3757fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 3758d950d659SMatthias Ringwald 3759831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 3760831711daSMatthias Ringwald // connection parameter to use for outgoing connections 3761cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 3762cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 3763831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 3764831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 3765831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 3766831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 3767831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 3768831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 37697261e5d8SMatthias Ringwald 37707261e5d8SMatthias Ringwald // default LE Scanning 37718b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 37728b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 37738b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 3774831711daSMatthias Ringwald #endif 3775831711daSMatthias Ringwald 37762b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 37772b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 37782b6ab3e6SMatthias Ringwald #endif 37792b6ab3e6SMatthias Ringwald 3780831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 3781831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 3782831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 3783831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 3784831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 3785831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 3786831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 3787831711daSMatthias Ringwald 3788595bdbfbS[email protected] hci_state_reset(); 3789475c8125Smatthias.ringwald } 3790475c8125Smatthias.ringwald 37914688f216SMatthias Ringwald void hci_deinit(void){ 37924688f216SMatthias Ringwald #ifdef HAVE_MALLOC 37934688f216SMatthias Ringwald if (hci_stack) { 37944688f216SMatthias Ringwald free(hci_stack); 37954688f216SMatthias Ringwald } 37964688f216SMatthias Ringwald #endif 37974688f216SMatthias Ringwald hci_stack = NULL; 3798b5bbcbf4SMatthias Ringwald 3799b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 38004688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 3801b5bbcbf4SMatthias Ringwald #endif 38024688f216SMatthias Ringwald } 38034688f216SMatthias Ringwald 38043fb36a29SMatthias Ringwald /** 38053fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 38063fb36a29SMatthias Ringwald */ 38073fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 38083fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 38093fb36a29SMatthias Ringwald 38103fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 38113fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 38123fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 38133fb36a29SMatthias Ringwald } 38143fb36a29SMatthias Ringwald } 38153fb36a29SMatthias Ringwald 3816fb55bd0aSMatthias Ringwald /** 3817d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 3818fb55bd0aSMatthias Ringwald */ 3819fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 3820fb55bd0aSMatthias Ringwald // references to used control implementation 3821fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 3822d0b87befSMatthias Ringwald // init with transport config 3823d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 3824fb55bd0aSMatthias Ringwald } 3825fb55bd0aSMatthias Ringwald 38261f9eb2ccSMatthias Ringwald static void hci_discard_connections(void){ 38271f9eb2ccSMatthias Ringwald btstack_linked_list_iterator_t lit; 38281f9eb2ccSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->connections); 38291f9eb2ccSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 38301f9eb2ccSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 38311f9eb2ccSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&lit); 38321f9eb2ccSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 38331f9eb2ccSMatthias Ringwald hci_shutdown_connection(connection); 38341f9eb2ccSMatthias Ringwald } 38351f9eb2ccSMatthias Ringwald } 38361f9eb2ccSMatthias Ringwald 383771de195eSMatthias Ringwald void hci_close(void){ 3838e6d6524dSMatthias Ringwald 3839e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 3840404843c1Smatthias.ringwald // close remote device db 3841a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 3842a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 3843404843c1Smatthias.ringwald } 3844e6d6524dSMatthias Ringwald #endif 38457224be7eSMatthias Ringwald 38461f9eb2ccSMatthias Ringwald hci_discard_connections(); 38477224be7eSMatthias Ringwald 3848f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 38493a9fb326S[email protected] 38503a9fb326S[email protected] #ifdef HAVE_MALLOC 38513a9fb326S[email protected] free(hci_stack); 38523a9fb326S[email protected] #endif 38533a9fb326S[email protected] hci_stack = NULL; 3854404843c1Smatthias.ringwald } 3855404843c1Smatthias.ringwald 3856cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3857cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 3858cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 3859cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 3860cb70c5abSMatthias Ringwald } 3861cb70c5abSMatthias Ringwald #endif 3862cb70c5abSMatthias Ringwald 386335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 3864170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 3865170fafaeSMatthias Ringwald // validate ranage and set 3866170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 3867170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 3868170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 3869170fafaeSMatthias Ringwald } 387078315a58SMatthias Ringwald 3871137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 3872137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 38735dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 3874137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 3875137715ebSMatthias Ringwald } else { 3876137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 3877137715ebSMatthias Ringwald } 38785dbec6b3SMatthias Ringwald } 38795dbec6b3SMatthias Ringwald 38805dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 38815dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 38825dbec6b3SMatthias Ringwald } 38835dbec6b3SMatthias Ringwald 388478315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 388578315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 388678315a58SMatthias Ringwald } 388778315a58SMatthias Ringwald 388878315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 3889d7387af3SMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 3890d7387af3SMatthias Ringwald return LEVEL_4; 3891d7387af3SMatthias Ringwald } 389278315a58SMatthias Ringwald return hci_stack->gap_security_level; 389378315a58SMatthias Ringwald } 389430cdf3c6SMatthias Ringwald 38958ad4dfffSMatthias Ringwald void gap_set_minimal_service_security_level(gap_security_level_t security_level){ 38968ad4dfffSMatthias Ringwald hci_stack->gap_minimal_service_security_level = security_level; 38978ad4dfffSMatthias Ringwald } 38988ad4dfffSMatthias Ringwald 389930cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 390030cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 390130cdf3c6SMatthias Ringwald } 390230cdf3c6SMatthias Ringwald 390330cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 390430cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 390530cdf3c6SMatthias Ringwald } 3906170fafaeSMatthias Ringwald #endif 3907170fafaeSMatthias Ringwald 3908170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 390960b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 39109e61646fS[email protected] hci_stack->class_of_device = class_of_device; 3911baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_CLASS_OF_DEVICE; 391259d59ecfSMatthias Ringwald hci_run(); 39139e61646fS[email protected] } 391476f27cffSMatthias Ringwald 3915c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 3916c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 3917baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_DEFAULT_LINK_POLICY; 391859d59ecfSMatthias Ringwald hci_run(); 3919c33e56d3SMatthias Ringwald } 3920c33e56d3SMatthias Ringwald 3921b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 3922b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 3923b4eb4420SMatthias Ringwald } 3924b4eb4420SMatthias Ringwald 3925b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 3926b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 3927b4eb4420SMatthias Ringwald } 3928b4eb4420SMatthias Ringwald 39290c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 39300c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 39310c3eb48dSMatthias Ringwald } 39320c3eb48dSMatthias Ringwald 393376f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 393476f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 393576f27cffSMatthias Ringwald } 393635454696SMatthias Ringwald #endif 39379e61646fS[email protected] 39386fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 3939f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 3940f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 39416535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 3942f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 3943f456b2d0S[email protected] } 394476f27cffSMatthias Ringwald #endif 3945f456b2d0S[email protected] 39468d213e1aSmatthias.ringwald // State-Module-Driver overview 39478d213e1aSmatthias.ringwald // state module low-level 39488d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 39498d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 39508d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 39518d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 3952d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 3953d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 3954c7e0c5f6Smatthias.ringwald 395540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 39567301ad89Smatthias.ringwald 3957038bc64cSmatthias.ringwald // power on 3958f9a30166Smatthias.ringwald int err = 0; 39593a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 3960d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 3961f9a30166Smatthias.ringwald } 3962038bc64cSmatthias.ringwald if (err){ 39639da54300S[email protected] log_error( "POWER_ON failed"); 3964038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3965038bc64cSmatthias.ringwald return err; 3966038bc64cSmatthias.ringwald } 3967038bc64cSmatthias.ringwald 396824b3c629SMatthias Ringwald // int chipset driver 39693fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 39703fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 39713fb36a29SMatthias Ringwald } 39723fb36a29SMatthias Ringwald 397324b3c629SMatthias Ringwald // init transport 397424b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 397524b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 397624b3c629SMatthias Ringwald } 397724b3c629SMatthias Ringwald 397824b3c629SMatthias Ringwald // open transport 397924b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 3980038bc64cSmatthias.ringwald if (err){ 39819da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 39823a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 3983d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 3984f9a30166Smatthias.ringwald } 3985038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 3986038bc64cSmatthias.ringwald return err; 3987038bc64cSmatthias.ringwald } 39888d213e1aSmatthias.ringwald return 0; 39898d213e1aSmatthias.ringwald } 3990038bc64cSmatthias.ringwald 399140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 39928d213e1aSmatthias.ringwald 39939da54300S[email protected] log_info("hci_power_control_off"); 39949418f9c9Smatthias.ringwald 39958d213e1aSmatthias.ringwald // close low-level device 399624b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 39978d213e1aSmatthias.ringwald 39989da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 39999418f9c9Smatthias.ringwald 40008d213e1aSmatthias.ringwald // power off 40013a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4002d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 40038d213e1aSmatthias.ringwald } 40049418f9c9Smatthias.ringwald 40059da54300S[email protected] log_info("hci_power_control_off - control closed"); 40069418f9c9Smatthias.ringwald 40073a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 400872ea5239Smatthias.ringwald } 400972ea5239Smatthias.ringwald 401040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 401172ea5239Smatthias.ringwald 40129da54300S[email protected] log_info("hci_power_control_sleep"); 40133144bce4Smatthias.ringwald 4014b429b9b7Smatthias.ringwald #if 0 4015b429b9b7Smatthias.ringwald // don't close serial port during sleep 4016b429b9b7Smatthias.ringwald 401772ea5239Smatthias.ringwald // close low-level device 40183a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 4019b429b9b7Smatthias.ringwald #endif 402072ea5239Smatthias.ringwald 402172ea5239Smatthias.ringwald // sleep mode 40223a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 4023d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 402472ea5239Smatthias.ringwald } 4025b429b9b7Smatthias.ringwald 40263a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 40278d213e1aSmatthias.ringwald } 40288d213e1aSmatthias.ringwald 402940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 4030b429b9b7Smatthias.ringwald 40319da54300S[email protected] log_info("hci_power_control_wake"); 4032b429b9b7Smatthias.ringwald 4033b429b9b7Smatthias.ringwald // wake on 40343a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 4035d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 4036b429b9b7Smatthias.ringwald } 4037b429b9b7Smatthias.ringwald 4038b429b9b7Smatthias.ringwald #if 0 4039b429b9b7Smatthias.ringwald // open low-level device 40403a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 4041b429b9b7Smatthias.ringwald if (err){ 40429da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 40433a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4044d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4045b429b9b7Smatthias.ringwald } 4046b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 4047b429b9b7Smatthias.ringwald return err; 4048b429b9b7Smatthias.ringwald } 4049b429b9b7Smatthias.ringwald #endif 4050b429b9b7Smatthias.ringwald 4051b429b9b7Smatthias.ringwald return 0; 4052b429b9b7Smatthias.ringwald } 4053b429b9b7Smatthias.ringwald 405444935e40S[email protected] static void hci_power_transition_to_initializing(void){ 405544935e40S[email protected] // set up state machine 405644935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 405702c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 405844935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 40595c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 406044935e40S[email protected] } 4061b429b9b7Smatthias.ringwald 406242bd3d77SMatthias Ringwald // returns error 406342bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 406442bd3d77SMatthias Ringwald int err; 40658d213e1aSmatthias.ringwald switch (power_mode){ 40668d213e1aSmatthias.ringwald case HCI_POWER_ON: 40678d213e1aSmatthias.ringwald err = hci_power_control_on(); 406842bd3d77SMatthias Ringwald if (err != 0) { 4069f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 407097b61c7bS[email protected] return err; 407197b61c7bS[email protected] } 407244935e40S[email protected] hci_power_transition_to_initializing(); 40738d213e1aSmatthias.ringwald break; 40748d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40758d213e1aSmatthias.ringwald // do nothing 40768d213e1aSmatthias.ringwald break; 40778d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4078b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 40798d213e1aSmatthias.ringwald break; 40807bbeb3adSMilanka Ringwald default: 40817bbeb3adSMilanka Ringwald btstack_assert(false); 40827bbeb3adSMilanka Ringwald break; 40838d213e1aSmatthias.ringwald } 408442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 408542bd3d77SMatthias Ringwald } 40867301ad89Smatthias.ringwald 408742bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 40888d213e1aSmatthias.ringwald switch (power_mode){ 40898d213e1aSmatthias.ringwald case HCI_POWER_ON: 40908d213e1aSmatthias.ringwald // do nothing 40918d213e1aSmatthias.ringwald break; 40928d213e1aSmatthias.ringwald case HCI_POWER_OFF: 40938d213e1aSmatthias.ringwald // no connections yet, just turn it off 40948d213e1aSmatthias.ringwald hci_power_control_off(); 40958d213e1aSmatthias.ringwald break; 40968d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4097b546ac54Smatthias.ringwald // no connections yet, just turn it off 409872ea5239Smatthias.ringwald hci_power_control_sleep(); 40998d213e1aSmatthias.ringwald break; 41007bbeb3adSMilanka Ringwald default: 41017bbeb3adSMilanka Ringwald btstack_assert(false); 41027bbeb3adSMilanka Ringwald break; 41038d213e1aSmatthias.ringwald } 410442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 410542bd3d77SMatthias Ringwald } 41067301ad89Smatthias.ringwald 410742bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 41088d213e1aSmatthias.ringwald switch (power_mode){ 41098d213e1aSmatthias.ringwald case HCI_POWER_ON: 41108d213e1aSmatthias.ringwald // do nothing 41118d213e1aSmatthias.ringwald break; 41128d213e1aSmatthias.ringwald case HCI_POWER_OFF: 4113c7e0c5f6Smatthias.ringwald // see hci_run 41143a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4115beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 4116685ec254SMatthias Ringwald // setup watchdog timer for disconnect - only triggers if Controller does not respond anymore 4117685ec254SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 1000); 4118685ec254SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4119685ec254SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 41208d213e1aSmatthias.ringwald break; 41218d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4122b546ac54Smatthias.ringwald // see hci_run 41233a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 412474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 41258d213e1aSmatthias.ringwald break; 41267bbeb3adSMilanka Ringwald default: 41277bbeb3adSMilanka Ringwald btstack_assert(false); 41287bbeb3adSMilanka Ringwald break; 41298d213e1aSmatthias.ringwald } 413042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 413142bd3d77SMatthias Ringwald } 41327301ad89Smatthias.ringwald 413342bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 41348d213e1aSmatthias.ringwald switch (power_mode){ 41358d213e1aSmatthias.ringwald case HCI_POWER_ON: 413644935e40S[email protected] hci_power_transition_to_initializing(); 41378d213e1aSmatthias.ringwald break; 41388d213e1aSmatthias.ringwald case HCI_POWER_OFF: 41398d213e1aSmatthias.ringwald // do nothing 41408d213e1aSmatthias.ringwald break; 41418d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4142b546ac54Smatthias.ringwald // see hci_run 41433a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 414474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 41458d213e1aSmatthias.ringwald break; 41467bbeb3adSMilanka Ringwald default: 41477bbeb3adSMilanka Ringwald btstack_assert(false); 41487bbeb3adSMilanka Ringwald break; 41498d213e1aSmatthias.ringwald } 415000278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 415142bd3d77SMatthias Ringwald } 41528d213e1aSmatthias.ringwald 415342bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 41548d213e1aSmatthias.ringwald switch (power_mode){ 41558d213e1aSmatthias.ringwald case HCI_POWER_ON: 415644935e40S[email protected] hci_power_transition_to_initializing(); 41578d213e1aSmatthias.ringwald break; 41588d213e1aSmatthias.ringwald case HCI_POWER_OFF: 4159b546ac54Smatthias.ringwald // see hci_run 41603a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4161beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 41628d213e1aSmatthias.ringwald break; 41638d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4164b546ac54Smatthias.ringwald // do nothing 41658d213e1aSmatthias.ringwald break; 41667bbeb3adSMilanka Ringwald default: 41677bbeb3adSMilanka Ringwald btstack_assert(false); 41687bbeb3adSMilanka Ringwald break; 41698d213e1aSmatthias.ringwald } 417042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 417142bd3d77SMatthias Ringwald } 41728d213e1aSmatthias.ringwald 417342bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 417442bd3d77SMatthias Ringwald int err; 41758d213e1aSmatthias.ringwald switch (power_mode){ 41768d213e1aSmatthias.ringwald case HCI_POWER_ON: 41773144bce4Smatthias.ringwald err = hci_power_control_wake(); 41783144bce4Smatthias.ringwald if (err) return err; 417944935e40S[email protected] hci_power_transition_to_initializing(); 41808d213e1aSmatthias.ringwald break; 41818d213e1aSmatthias.ringwald case HCI_POWER_OFF: 41823a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 4183beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_NO_TIMER; 41848d213e1aSmatthias.ringwald break; 41858d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4186b546ac54Smatthias.ringwald // do nothing 41878d213e1aSmatthias.ringwald break; 41887bbeb3adSMilanka Ringwald default: 41897bbeb3adSMilanka Ringwald btstack_assert(false); 41907bbeb3adSMilanka Ringwald break; 41918d213e1aSmatthias.ringwald } 419242bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 419342bd3d77SMatthias Ringwald } 41947bbeb3adSMilanka Ringwald 419542bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 419642bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 419724f87726SMatthias Ringwald int err = 0; 419842bd3d77SMatthias Ringwald switch (hci_stack->state){ 419942bd3d77SMatthias Ringwald case HCI_STATE_OFF: 420042bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 420142bd3d77SMatthias Ringwald break; 420242bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 420342bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 420442bd3d77SMatthias Ringwald break; 420542bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 420642bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 420742bd3d77SMatthias Ringwald break; 420842bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 420942bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 421042bd3d77SMatthias Ringwald break; 421142bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 421242bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 421342bd3d77SMatthias Ringwald break; 421442bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 421542bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 421642bd3d77SMatthias Ringwald break; 42177bbeb3adSMilanka Ringwald default: 42187bbeb3adSMilanka Ringwald btstack_assert(false); 42197bbeb3adSMilanka Ringwald break; 422011e23e5fSmatthias.ringwald } 422124f87726SMatthias Ringwald if (err != 0){ 422242bd3d77SMatthias Ringwald return err; 422342bd3d77SMatthias Ringwald } 422468d92d03Smatthias.ringwald 4225038bc64cSmatthias.ringwald // create internal event 4226ee8bf225Smatthias.ringwald hci_emit_state(); 4227ee8bf225Smatthias.ringwald 422868d92d03Smatthias.ringwald // trigger next/first action 422968d92d03Smatthias.ringwald hci_run(); 423068d92d03Smatthias.ringwald 4231475c8125Smatthias.ringwald return 0; 4232475c8125Smatthias.ringwald } 4233475c8125Smatthias.ringwald 423435454696SMatthias Ringwald 4235091e35e3SMatthias Ringwald static void hci_halting_run(void) { 4236091e35e3SMatthias Ringwald 4237091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 4238091e35e3SMatthias Ringwald 4239091e35e3SMatthias Ringwald hci_connection_t *connection; 4240091e35e3SMatthias Ringwald 4241091e35e3SMatthias Ringwald switch (hci_stack->substate) { 4242091e35e3SMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 4243091e35e3SMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_TIMER: 4244091e35e3SMatthias Ringwald 4245091e35e3SMatthias Ringwald #ifdef ENABLE_BLE 4246091e35e3SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4247091e35e3SMatthias Ringwald hci_whitelist_free(); 4248091e35e3SMatthias Ringwald #endif 4249091e35e3SMatthias Ringwald #endif 4250091e35e3SMatthias Ringwald // close all open connections 4251091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 4252091e35e3SMatthias Ringwald if (connection) { 4253091e35e3SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 4254091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4255091e35e3SMatthias Ringwald 4256091e35e3SMatthias Ringwald // check state 4257091e35e3SMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 4258091e35e3SMatthias Ringwald connection->state = SENT_DISCONNECT; 4259091e35e3SMatthias Ringwald 4260091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 4261091e35e3SMatthias Ringwald 4262091e35e3SMatthias Ringwald // cancel all l2cap connections right away instead of waiting for disconnection complete event ... 4263091e35e3SMatthias Ringwald hci_emit_disconnection_complete(con_handle, 0x16); // terminated by local host 4264091e35e3SMatthias Ringwald 4265091e35e3SMatthias Ringwald // ... which would be ignored anyway as we shutdown (free) the connection now 4266091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 4267091e35e3SMatthias Ringwald 4268091e35e3SMatthias Ringwald // finally, send the disconnect command 4269091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4270091e35e3SMatthias Ringwald return; 4271091e35e3SMatthias Ringwald } 4272091e35e3SMatthias Ringwald 4273091e35e3SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 4274091e35e3SMatthias Ringwald 4275091e35e3SMatthias Ringwald if (hci_stack->substate == HCI_HALTING_DISCONNECT_ALL_TIMER) { 4276091e35e3SMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 4277091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 4278091e35e3SMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_TIMER; 4279091e35e3SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 4280091e35e3SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4281091e35e3SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 4282091e35e3SMatthias Ringwald break; 4283091e35e3SMatthias Ringwald } 4284091e35e3SMatthias Ringwald 4285091e35e3SMatthias Ringwald /* fall through */ 4286091e35e3SMatthias Ringwald 4287091e35e3SMatthias Ringwald case HCI_HALTING_CLOSE: 4288091e35e3SMatthias Ringwald // close left over connections (that had not been properly closed before) 4289091e35e3SMatthias Ringwald hci_discard_connections(); 4290091e35e3SMatthias Ringwald 4291091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling off"); 4292091e35e3SMatthias Ringwald 4293091e35e3SMatthias Ringwald // switch mode 4294091e35e3SMatthias Ringwald hci_power_control_off(); 4295091e35e3SMatthias Ringwald 4296091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, emitting state"); 4297091e35e3SMatthias Ringwald hci_emit_state(); 4298091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, done"); 4299091e35e3SMatthias Ringwald break; 4300091e35e3SMatthias Ringwald 4301091e35e3SMatthias Ringwald case HCI_HALTING_W4_TIMER: 4302091e35e3SMatthias Ringwald // keep waiting 4303091e35e3SMatthias Ringwald 4304091e35e3SMatthias Ringwald break; 4305091e35e3SMatthias Ringwald default: 4306091e35e3SMatthias Ringwald break; 4307091e35e3SMatthias Ringwald } 4308091e35e3SMatthias Ringwald }; 4309091e35e3SMatthias Ringwald 4310091e35e3SMatthias Ringwald static void hci_falling_asleep_run(void){ 4311091e35e3SMatthias Ringwald hci_connection_t * connection; 4312091e35e3SMatthias Ringwald switch(hci_stack->substate) { 4313091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 4314091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP"); 4315091e35e3SMatthias Ringwald // close all open connections 4316091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 4317091e35e3SMatthias Ringwald if (connection){ 4318091e35e3SMatthias Ringwald 4319091e35e3SMatthias Ringwald // send disconnect 4320091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4321091e35e3SMatthias Ringwald 4322091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 4323091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 4324091e35e3SMatthias Ringwald 4325091e35e3SMatthias Ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 4326091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 4327091e35e3SMatthias Ringwald return; 4328091e35e3SMatthias Ringwald } 4329091e35e3SMatthias Ringwald 4330091e35e3SMatthias Ringwald if (hci_classic_supported()){ 4331091e35e3SMatthias Ringwald // disable page and inquiry scan 4332091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4333091e35e3SMatthias Ringwald 4334091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, disabling inq scans"); 4335091e35e3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 4336091e35e3SMatthias Ringwald 4337091e35e3SMatthias Ringwald // continue in next sub state 4338091e35e3SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 4339091e35e3SMatthias Ringwald break; 4340091e35e3SMatthias Ringwald } 4341091e35e3SMatthias Ringwald 4342091e35e3SMatthias Ringwald /* fall through */ 4343091e35e3SMatthias Ringwald 4344091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 4345091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling sleep"); 4346091e35e3SMatthias Ringwald // switch mode 4347091e35e3SMatthias Ringwald hci_power_control_sleep(); // changes hci_stack->state to SLEEP 4348091e35e3SMatthias Ringwald hci_emit_state(); 4349091e35e3SMatthias Ringwald break; 4350091e35e3SMatthias Ringwald 4351091e35e3SMatthias Ringwald default: 4352091e35e3SMatthias Ringwald break; 4353091e35e3SMatthias Ringwald } 4354091e35e3SMatthias Ringwald } 4355091e35e3SMatthias Ringwald 435635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 435735454696SMatthias Ringwald 4358758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 4359758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 4360a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 4361baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_SCAN_ENABLE; 4362758b46ceSmatthias.ringwald hci_run(); 4363758b46ceSmatthias.ringwald } 4364758b46ceSmatthias.ringwald 436515a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 4366381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 4367381fbed8Smatthias.ringwald 43683a9fb326S[email protected] if (hci_stack->discoverable == enable){ 43693a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 4370381fbed8Smatthias.ringwald return; 4371381fbed8Smatthias.ringwald } 4372381fbed8Smatthias.ringwald 43733a9fb326S[email protected] hci_stack->discoverable = enable; 4374758b46ceSmatthias.ringwald hci_update_scan_enable(); 4375758b46ceSmatthias.ringwald } 4376b031bebbSmatthias.ringwald 437715a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 4378758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 4379758b46ceSmatthias.ringwald 4380758b46ceSmatthias.ringwald // don't emit event 43813a9fb326S[email protected] if (hci_stack->connectable == enable) return; 4382758b46ceSmatthias.ringwald 43833a9fb326S[email protected] hci_stack->connectable = enable; 4384758b46ceSmatthias.ringwald hci_update_scan_enable(); 4385381fbed8Smatthias.ringwald } 438635454696SMatthias Ringwald #endif 4387381fbed8Smatthias.ringwald 438815a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 43896535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 43905061f3afS[email protected] } 43915061f3afS[email protected] 43922b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 43932b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 43942b838201SMatthias Ringwald 43952b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 43962b838201SMatthias Ringwald hci_reserve_packet_buffer(); 43972b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 43982b838201SMatthias Ringwald 43992b838201SMatthias Ringwald uint16_t size = 0; 44002b838201SMatthias Ringwald uint16_t num_handles = 0; 44012b838201SMatthias Ringwald packet[size++] = 0x35; 44022b838201SMatthias Ringwald packet[size++] = 0x0c; 44032b838201SMatthias Ringwald size++; // skip param len 44042b838201SMatthias Ringwald size++; // skip num handles 44052b838201SMatthias Ringwald 44062b838201SMatthias Ringwald // add { handle, packets } entries 44072b838201SMatthias Ringwald btstack_linked_item_t * it; 44082b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 44092b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 44102b838201SMatthias Ringwald if (connection->num_packets_completed){ 44112b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 44122b838201SMatthias Ringwald size += 2; 44132b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 44142b838201SMatthias Ringwald size += 2; 44152b838201SMatthias Ringwald // 44162b838201SMatthias Ringwald num_handles++; 44172b838201SMatthias Ringwald connection->num_packets_completed = 0; 44182b838201SMatthias Ringwald } 44192b838201SMatthias Ringwald } 44202b838201SMatthias Ringwald 44212b838201SMatthias Ringwald packet[2] = size - 3; 44222b838201SMatthias Ringwald packet[3] = num_handles; 44232b838201SMatthias Ringwald 44242b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 44252b838201SMatthias Ringwald 44262b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 44272b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 44282b838201SMatthias Ringwald 44292b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 44302b838201SMatthias Ringwald if (hci_transport_synchronous()){ 4431e2d22487SMatthias Ringwald hci_release_packet_buffer(); 4432068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 44332b838201SMatthias Ringwald } 44342b838201SMatthias Ringwald } 44352b838201SMatthias Ringwald #endif 44362b838201SMatthias Ringwald 443726fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 443826fe9592SMatthias Ringwald UNUSED(ds); 443926fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 4440beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 4441beceeddeSMatthias Ringwald hci_emit_state(); 444226fe9592SMatthias Ringwald hci_run(); 444326fe9592SMatthias Ringwald } 444426fe9592SMatthias Ringwald 4445f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 44464ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 4447b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 4448b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 4449b5d8b22bS[email protected] if (connection) { 445028a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 4451b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 4452f30077b7SMatthias Ringwald return true; 4453b5d8b22bS[email protected] } 445428a0332dSMatthias Ringwald } else { 4455b5d8b22bS[email protected] // connection gone -> discard further fragments 445628a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 4457b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 4458b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 4459b5d8b22bS[email protected] } 4460b5d8b22bS[email protected] } 4461f30077b7SMatthias Ringwald return false; 44622b838201SMatthias Ringwald } 4463b031bebbSmatthias.ringwald 446435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4465f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 4466f30077b7SMatthias Ringwald 446759d59ecfSMatthias Ringwald // assert stack is working and classic is active 446859d59ecfSMatthias Ringwald if (hci_classic_supported() == false) return false; 446959d59ecfSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 447059d59ecfSMatthias Ringwald 4471b031bebbSmatthias.ringwald // decline incoming connections 44723a9fb326S[email protected] if (hci_stack->decline_reason){ 44733a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 44743a9fb326S[email protected] hci_stack->decline_reason = 0; 44753a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 4476f30077b7SMatthias Ringwald return true; 4477ce4c8fabSmatthias.ringwald } 447859d59ecfSMatthias Ringwald 4479baa4881eSMatthias Ringwald if (hci_stack->gap_tasks_classic != 0){ 4480ab4831a3SMatthias Ringwald hci_run_gap_tasks_classic(); 4481f30077b7SMatthias Ringwald return true; 4482b031bebbSmatthias.ringwald } 448327741fe7SMatthias Ringwald 4484f5875de5SMatthias Ringwald // start/stop inquiry 4485a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 4486f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 4487beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 4488496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 4489f30077b7SMatthias Ringwald return true; 4490f5875de5SMatthias Ringwald } 4491f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 4492f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 4493f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 4494f30077b7SMatthias Ringwald return true; 4495f5875de5SMatthias Ringwald } 4496b7f1ee76SMatthias Ringwald // remote name request 4497b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 4498b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 4499b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 4500ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 4501f30077b7SMatthias Ringwald return true; 4502b7f1ee76SMatthias Ringwald } 4503cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 4504cf01e888SMatthias Ringwald // Local OOB data 450559d59ecfSMatthias Ringwald if (hci_stack->classic_read_local_oob_data){ 4506cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 45071e83575aSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND)){ 4508cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 4509cf01e888SMatthias Ringwald } else { 4510cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 4511cf01e888SMatthias Ringwald } 4512cf01e888SMatthias Ringwald } 4513cf01e888SMatthias Ringwald #endif 45140a51f88bSMatthias Ringwald // pairing 45150a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 45160a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 45173f659ee4SMilanka Ringwald uint8_t pin_code[16]; 45180a51f88bSMatthias Ringwald switch (state){ 45190a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 4520cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 45213f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 45223f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 45233f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 45240a51f88bSMatthias Ringwald break; 45250a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 4526cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 45270a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 45280a51f88bSMatthias Ringwald break; 45290a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 4530cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 4531d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 45320a51f88bSMatthias Ringwald break; 45330a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 4534cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 45350a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 45360a51f88bSMatthias Ringwald break; 45370a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 4538cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 45390a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 45400a51f88bSMatthias Ringwald break; 45410a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 4542cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 45430a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 45440a51f88bSMatthias Ringwald break; 45450a51f88bSMatthias Ringwald default: 45460a51f88bSMatthias Ringwald break; 45470a51f88bSMatthias Ringwald } 4548f30077b7SMatthias Ringwald return true; 4549f30077b7SMatthias Ringwald } 4550f30077b7SMatthias Ringwald return false; 45510a51f88bSMatthias Ringwald } 455235454696SMatthias Ringwald #endif 4553b031bebbSmatthias.ringwald 4554a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4555f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 4556f30077b7SMatthias Ringwald 4557a41310b7SMatthias Ringwald // Phase 1: collect what to stop 4558a41310b7SMatthias Ringwald 4559a41310b7SMatthias Ringwald bool scanning_stop = false; 4560a41310b7SMatthias Ringwald bool connecting_stop = false; 4561a41310b7SMatthias Ringwald bool advertising_stop = false; 4562a41310b7SMatthias Ringwald 4563a41310b7SMatthias Ringwald #ifndef ENABLE_LE_CENTRAL 4564a41310b7SMatthias Ringwald UNUSED(scanning_stop); 45650abd9f64SMatthias Ringwald UNUSED(connecting_stop); 4566a41310b7SMatthias Ringwald #endif 4567a41310b7SMatthias Ringwald #ifndef ENABLE_LE_PERIPHERAL 4568a41310b7SMatthias Ringwald UNUSED(advertising_stop); 4569a41310b7SMatthias Ringwald #endif 4570a41310b7SMatthias Ringwald 45719136bde8SMatthias Ringwald // check if own address changes 45729136bde8SMatthias Ringwald bool random_address_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 45739136bde8SMatthias Ringwald 457429c24bebSMatthias Ringwald // check if whitelist needs modification 457529c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 457629c24bebSMatthias Ringwald btstack_linked_list_iterator_t lit; 457729c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 457829c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 457929c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 458029c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 458129c24bebSMatthias Ringwald whitelist_modification_pending = true; 458229c24bebSMatthias Ringwald break; 458329c24bebSMatthias Ringwald } 458429c24bebSMatthias Ringwald } 458521debf25SMatthias Ringwald // check if resolving list needs modification 458621debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 458721debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 45881ffa425cSMatthias Ringwald 45891ffa425cSMatthias Ringwald bool resolving_list_supported = hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE); 4590ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 459121debf25SMatthias Ringwald resolving_list_modification_pending = true; 459221debf25SMatthias Ringwald } 459321debf25SMatthias Ringwald #endif 459429c24bebSMatthias Ringwald 4595d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4596fde725feSMatthias Ringwald // scanning control 45973251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 459829c24bebSMatthias Ringwald // stop if: 459929c24bebSMatthias Ringwald // - parameter change required 460029c24bebSMatthias Ringwald // - it's disabled 460129c24bebSMatthias Ringwald // - whitelist change required but used for scanning 460221debf25SMatthias Ringwald // - resolving list modified 460351e51a58SMatthias Ringwald // - own address changes 460429c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 460521debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 460621debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 460721debf25SMatthias Ringwald scanning_uses_whitelist || 460851e51a58SMatthias Ringwald resolving_list_modification_pending || 460951e51a58SMatthias Ringwald random_address_change){ 461021debf25SMatthias Ringwald 46112d5c2a27SMatthias Ringwald scanning_stop = true; 4612fde725feSMatthias Ringwald } 4613fde725feSMatthias Ringwald } 461429c24bebSMatthias Ringwald #endif 4615fde725feSMatthias Ringwald 461629c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 461729c24bebSMatthias Ringwald // connecting control 4618f496d06eSMatthias Ringwald bool connecting_with_whitelist; 4619f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 4620f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 4621f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 4622af64f147SMatthias Ringwald // stop connecting if: 4623af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 4624af64f147SMatthias Ringwald // - if it got disabled 462521debf25SMatthias Ringwald // - resolving list modified 462651e51a58SMatthias Ringwald // - own address changes 4627f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 4628f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 462921debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 463051e51a58SMatthias Ringwald resolving_list_modification_pending || 463151e51a58SMatthias Ringwald random_address_change) { 463221debf25SMatthias Ringwald 463329c24bebSMatthias Ringwald connecting_stop = true; 463429c24bebSMatthias Ringwald } 4635f496d06eSMatthias Ringwald break; 4636f496d06eSMatthias Ringwald default: 4637f496d06eSMatthias Ringwald break; 463829c24bebSMatthias Ringwald } 4639d70217a2SMatthias Ringwald #endif 464029c24bebSMatthias Ringwald 4641d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 464245c102fdSMatthias Ringwald // le advertisement control 4643bbc366e6SMatthias Ringwald if (hci_stack->le_advertisements_active){ 464429c24bebSMatthias Ringwald // stop if: 464529c24bebSMatthias Ringwald // - parameter change required 46469136bde8SMatthias Ringwald // - random address used in advertising and changes 464729c24bebSMatthias Ringwald // - it's disabled 4648ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 464921debf25SMatthias Ringwald // - resolving list modified 465051e51a58SMatthias Ringwald // - own address changes 4651a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 46529136bde8SMatthias Ringwald bool advertising_uses_random_address = hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC; 46534e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 4654a61834b6SMatthias Ringwald if (advertising_change || 46559136bde8SMatthias Ringwald (advertising_uses_random_address && random_address_change) || 4656a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 46579136bde8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 465851e51a58SMatthias Ringwald resolving_list_modification_pending || 465951e51a58SMatthias Ringwald random_address_change) { 466021debf25SMatthias Ringwald 4661fde725feSMatthias Ringwald advertising_stop = true; 4662fde725feSMatthias Ringwald } 4663fde725feSMatthias Ringwald } 4664a41310b7SMatthias Ringwald #endif 4665fde725feSMatthias Ringwald 4666a41310b7SMatthias Ringwald 4667a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 4668a41310b7SMatthias Ringwald 4669a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4670a41310b7SMatthias Ringwald if (scanning_stop){ 46715226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 467263671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 467363671e69SMatthias Ringwald if (hci_extended_advertising_supported()) { 467463671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 0, 0, 0, 0); 467563671e69SMatthias Ringwald } else 467663671e69SMatthias Ringwald #endif 467763671e69SMatthias Ringwald { 4678a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 467963671e69SMatthias Ringwald } 4680a41310b7SMatthias Ringwald return true; 4681a41310b7SMatthias Ringwald } 4682a41310b7SMatthias Ringwald #endif 4683a41310b7SMatthias Ringwald 4684a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4685a41310b7SMatthias Ringwald if (connecting_stop){ 4686a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 4687a41310b7SMatthias Ringwald return true; 4688a41310b7SMatthias Ringwald } 4689a41310b7SMatthias Ringwald #endif 4690a41310b7SMatthias Ringwald 4691a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4692fde725feSMatthias Ringwald if (advertising_stop){ 46935226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = false; 469445c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 4695f30077b7SMatthias Ringwald return true; 469645c102fdSMatthias Ringwald } 4697a41310b7SMatthias Ringwald #endif 4698fde725feSMatthias Ringwald 4699a41310b7SMatthias Ringwald // Phase 3: modify 4700a41310b7SMatthias Ringwald 47019136bde8SMatthias Ringwald if (random_address_change){ 47029136bde8SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 47039136bde8SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, hci_stack->le_random_address); 47049136bde8SMatthias Ringwald return true; 47059136bde8SMatthias Ringwald } 47069136bde8SMatthias Ringwald 4707a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4708a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 4709a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 471063671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 471163671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 471263671e69SMatthias Ringwald // prepare arrays for all PHYs 471363671e69SMatthias Ringwald uint8_t scan_types[1] = { hci_stack->le_scan_type }; 471463671e69SMatthias Ringwald uint16_t scan_intervals[1] = { hci_stack->le_scan_interval }; 471563671e69SMatthias Ringwald uint16_t scan_windows[1] = { hci_stack->le_scan_window }; 471663671e69SMatthias Ringwald uint8_t scanning_phys = 1; // LE 1M PHY 471763671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_parameters, hci_stack->le_own_addr_type, 471863671e69SMatthias Ringwald hci_stack->le_scan_filter_policy, scanning_phys, scan_types, scan_intervals, scan_windows); 471963671e69SMatthias Ringwald } else 472063671e69SMatthias Ringwald #endif 472163671e69SMatthias Ringwald { 4722a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 4723a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 472463671e69SMatthias Ringwald } 4725a41310b7SMatthias Ringwald return true; 4726a41310b7SMatthias Ringwald } 4727a41310b7SMatthias Ringwald #endif 4728a41310b7SMatthias Ringwald 4729a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 473045c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 473145c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 47326bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 473345c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 473445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 473545c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 473645c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 47376bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 473845c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 473945c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 474045c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 474145c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 4742f30077b7SMatthias Ringwald return true; 474345c102fdSMatthias Ringwald } 4744501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 4745501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 47467a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 47477a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 47486535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 47496535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 47503c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 47517a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 4752f30077b7SMatthias Ringwald return true; 475345c102fdSMatthias Ringwald } 4754501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 4755501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 4756f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 4757f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 47586535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 47596535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 47603c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 4761214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 4762f30077b7SMatthias Ringwald return true; 4763501f56b3SMatthias Ringwald } 4764d70217a2SMatthias Ringwald #endif 47659956955bSMatthias Ringwald 476613eb2a2eSMatthias Ringwald 4767057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4768a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 4769a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 4770057ab60cSMatthias Ringwald #endif 4771057ab60cSMatthias Ringwald 4772a41310b7SMatthias Ringwald // LE Whitelist Management 4773a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 47749956955bSMatthias Ringwald // add/remove entries 4775665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4776665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 4777665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 4778453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 4779453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 4780453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 4781453459ddSMatthias Ringwald return true; 4782453459ddSMatthias Ringwald } 47839956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 4784453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 4785453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 47869956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 4787f30077b7SMatthias Ringwald return true; 47889956955bSMatthias Ringwald } 4789453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 4790665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 47919956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 47929956955bSMatthias Ringwald } 47939956955bSMatthias Ringwald } 479491915b0bSMatthias Ringwald } 47959956955bSMatthias Ringwald 479621debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 479721debf25SMatthias Ringwald // LE Resolving List Management 4798ea151974SMatthias Ringwald if (resolving_list_supported) { 479921debf25SMatthias Ringwald uint16_t i; 480021debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 480121debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 480221debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 480321debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 480421debf25SMatthias Ringwald return true; 480521debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 480621debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 480721debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 480821debf25SMatthias Ringwald return true; 480921debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 481002b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 4811ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 4812ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 4813ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 4814ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 481521debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 481621debf25SMatthias Ringwald return true; 481702b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 481802b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 481902b02cffSMatthias Ringwald uint8_t offset = i >> 3; 482002b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 482102b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 482202b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 482302b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 482402b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 482502b02cffSMatthias Ringwald sm_key_t peer_irk; 482602b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 482702b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 4828f5228c62SMatthias Ringwald 4829f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 4830f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 4831f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 4832f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 4833f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 4834f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 4835f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 4836f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 4837f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 4838f5228c62SMatthias Ringwald } 4839f5228c62SMatthias Ringwald #endif 4840f5228c62SMatthias Ringwald 4841ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 4842ea151974SMatthias Ringwald peer_identity_addreses); 484302b02cffSMatthias Ringwald return true; 484402b02cffSMatthias Ringwald } 484502b02cffSMatthias Ringwald 484602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 484702b02cffSMatthias Ringwald 484802b02cffSMatthias Ringwald /* fall through */ 484902b02cffSMatthias Ringwald 485021debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 485121debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 485221debf25SMatthias Ringwald uint8_t offset = i >> 3; 485321debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 485402b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 485502b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 485621debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 485721debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 485821debf25SMatthias Ringwald sm_key_t peer_irk; 485921debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 486021debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 486121debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 486221debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 486321debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 486421debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 486521debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 486621debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 4867ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 4868ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 486921debf25SMatthias Ringwald return true; 487021debf25SMatthias Ringwald } 487102b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 487221debf25SMatthias Ringwald break; 487302b02cffSMatthias Ringwald 487421debf25SMatthias Ringwald default: 487521debf25SMatthias Ringwald break; 487621debf25SMatthias Ringwald } 4877ea151974SMatthias Ringwald } 487821debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 487921debf25SMatthias Ringwald #endif 4880a41310b7SMatthias Ringwald 488173799937SMatthias Ringwald // post-pone all actions until stack is fully working 488273799937SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 488373799937SMatthias Ringwald 488473799937SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 488573799937SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 488673799937SMatthias Ringwald 4887a41310b7SMatthias Ringwald // Phase 4: restore state 488829c24bebSMatthias Ringwald 488929c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4890af64f147SMatthias Ringwald // re-start scanning 489129c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 489229c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 489363671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 489463671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 489563671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 1, 0, 0, 0); 489663671e69SMatthias Ringwald } else 489763671e69SMatthias Ringwald #endif 489863671e69SMatthias Ringwald { 489929c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 490063671e69SMatthias Ringwald } 490129c24bebSMatthias Ringwald return true; 490229c24bebSMatthias Ringwald } 490329c24bebSMatthias Ringwald #endif 490429c24bebSMatthias Ringwald 490513eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4906af64f147SMatthias Ringwald // re-start connecting 4907af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 49089956955bSMatthias Ringwald bd_addr_t null_addr; 49099956955bSMatthias Ringwald memset(null_addr, 0, 6); 49106bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 49116bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 49129956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 4913cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 4914cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 49159956955bSMatthias Ringwald 1, // use whitelist 49169956955bSMatthias Ringwald 0, // peer address type 49179956955bSMatthias Ringwald null_addr, // peer bd addr 49186bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 491973044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 492073044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 492173044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 492273044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 492373044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 492473044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 49259956955bSMatthias Ringwald ); 4926f30077b7SMatthias Ringwald return true; 49279956955bSMatthias Ringwald } 4928d70217a2SMatthias Ringwald #endif 4929a41310b7SMatthias Ringwald 4930a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4931a41310b7SMatthias Ringwald // re-start advertising 49328978dcf1SMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && !hci_stack->le_advertisements_active){ 4933a41310b7SMatthias Ringwald // check if advertisements should be enabled given 49345226d7f2SMatthias Ringwald hci_stack->le_advertisements_active = true; 4935b892db1cSMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, hci_stack->le_advertisements_own_address); 4936a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 4937a41310b7SMatthias Ringwald return true; 4938a41310b7SMatthias Ringwald } 4939a41310b7SMatthias Ringwald #endif 4940a41310b7SMatthias Ringwald 4941f30077b7SMatthias Ringwald return false; 49427bdc6798S[email protected] } 4943b2f949feS[email protected] #endif 49447bdc6798S[email protected] 494588a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 4946f30077b7SMatthias Ringwald btstack_linked_item_t * it; 4947a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 494805ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 494932ab9390Smatthias.ringwald 49500bf6344aS[email protected] switch(connection->state){ 49510bf6344aS[email protected] case SEND_CREATE_CONNECTION: 49524f3229d8S[email protected] switch(connection->address_type){ 495335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4954f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 49559da54300S[email protected] log_info("sending hci_create_connection"); 4956b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 49574f3229d8S[email protected] break; 495835454696SMatthias Ringwald #endif 49594f3229d8S[email protected] default: 4960a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4961d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 49629da54300S[email protected] log_info("sending hci_le_create_connection"); 49636bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 49646bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 49654f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 4966cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 4967cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 49684f3229d8S[email protected] 0, // don't use whitelist 49694f3229d8S[email protected] connection->address_type, // peer address type 49704f3229d8S[email protected] connection->address, // peer bd addr 49716bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 497273044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 497373044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 497473044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 497573044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 497673044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 497773044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 49784f3229d8S[email protected] ); 49794f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 4980b2f949feS[email protected] #endif 4981d70217a2SMatthias Ringwald #endif 49824f3229d8S[email protected] break; 49834f3229d8S[email protected] } 4984f30077b7SMatthias Ringwald return true; 4985ad83dc6aS[email protected] 498635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 49870bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 49885cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 4989f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 499076ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 4991895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 4992c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 4993f30077b7SMatthias Ringwald return true; 4994ee025741SMatthias Ringwald } 4995ee025741SMatthias Ringwald break; 499635454696SMatthias Ringwald #endif 49970bf6344aS[email protected] 4998a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 4999d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 50000bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 50010bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 50020bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 5003f30077b7SMatthias Ringwald return true; 5004a6725849S[email protected] #endif 5005d70217a2SMatthias Ringwald #endif 50060bf6344aS[email protected] case SEND_DISCONNECT: 50070bf6344aS[email protected] connection->state = SENT_DISCONNECT; 50086ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5009f30077b7SMatthias Ringwald return true; 50100bf6344aS[email protected] 50110bf6344aS[email protected] default: 50120bf6344aS[email protected] break; 5013c7e0c5f6Smatthias.ringwald } 5014c7e0c5f6Smatthias.ringwald 5015cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 5016cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 5017cabf004eSMatthias Ringwald 50188daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_READ_RSSI){ 50198daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_READ_RSSI); 5020891b9fc2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 5021f30077b7SMatthias Ringwald return true; 5022891b9fc2SMatthias Ringwald } 5023891b9fc2SMatthias Ringwald 502494418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 502594418890SMatthias Ringwald 50268daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT){ 50278daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_WRITE_SUPERVISION_TIMEOUT); 50286909f064SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 5029f30077b7SMatthias Ringwald return true; 50306909f064SMatthias Ringwald } 50316909f064SMatthias Ringwald 5032ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 503350f49832SMatthias Ringwald if (((connection->authentication_flags & AUTH_FLAG_HANDLE_LINK_KEY_REQUEST) != 0)){ 5034608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 50358daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 503630e72d78SMatthias Ringwald 5037e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 5038e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 503930e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 5040e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 504132ab9390Smatthias.ringwald } else { 504232ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 504332ab9390Smatthias.ringwald } 5044f30077b7SMatthias Ringwald return true; 504532ab9390Smatthias.ringwald } 50461d6b20aeS[email protected] 50478daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_DENY_PIN_CODE_REQUEST){ 50489da54300S[email protected] log_info("denying to pin request"); 50498daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_DENY_PIN_CODE_REQUEST); 505034d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 5051f30077b7SMatthias Ringwald return true; 50524c57c146S[email protected] } 50534c57c146S[email protected] 5054c950c316SMatthias Ringwald // security assessment requires remote features 50552dd8985bSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST) != 0){ 5056c950c316SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 5057c950c316SMatthias Ringwald hci_ssp_assess_security_on_io_cap_request(connection); 5058c950c316SMatthias 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 5059c950c316SMatthias Ringwald } 5060c950c316SMatthias Ringwald 50618daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY){ 50628daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 5063a8d20135SMatthias Ringwald // set authentication requirements: 5064a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 5065532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 5066a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 50679faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 50689faad3abS[email protected] authreq |= 1; 5069106d6d11S[email protected] } 5070532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 50718daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 5072532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 5073532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 5074532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 5075532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 5076532454f9SMatthias Ringwald bonding = false; 5077532454f9SMatthias Ringwald } 5078532454f9SMatthias Ringwald } 5079532454f9SMatthias Ringwald if (bonding){ 5080a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 5081a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 5082532454f9SMatthias Ringwald } else { 5083a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 5084a8d20135SMatthias Ringwald } 5085532454f9SMatthias Ringwald } 50861849becdSMatthias Ringwald uint8_t have_oob_data = 0; 50871849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 50881849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 50891849becdSMatthias Ringwald have_oob_data |= 1; 50901849becdSMatthias Ringwald } 50911849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 50921849becdSMatthias Ringwald have_oob_data |= 2; 50931849becdSMatthias Ringwald } 50941849becdSMatthias Ringwald #endif 50951849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 509611b03efaSMatthias Ringwald return true; 5097f8fb5f6eS[email protected] } 509811b03efaSMatthias Ringwald 50998daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 51008daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 510111b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 5102f30077b7SMatthias Ringwald return true; 510332ab9390Smatthias.ringwald } 510432ab9390Smatthias.ringwald 51051849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 51067ca4a7edSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY){ 51077ca4a7edSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 51081849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 51091849becdSMatthias Ringwald const uint8_t * r_192 = zero; 51101849becdSMatthias Ringwald const uint8_t * c_192 = zero; 51111849becdSMatthias Ringwald const uint8_t * r_256 = zero; 51121849becdSMatthias Ringwald const uint8_t * c_256 = zero; 51131849becdSMatthias Ringwald // verify P-256 OOB 51141e83575aSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && hci_command_supported(SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)) { 51151849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 51161849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 51171849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 51181849becdSMatthias Ringwald } 51191849becdSMatthias Ringwald } 51201849becdSMatthias Ringwald // verify P-192 OOB 51211849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 51221849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 51231849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 51241849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 51251849becdSMatthias Ringwald } 51261849becdSMatthias Ringwald } 51277ca4a7edSMatthias Ringwald 51287ca4a7edSMatthias Ringwald // assess security 51297ca4a7edSMatthias Ringwald bool need_level_4 = hci_stack->gap_secure_connections_only_mode || (connection->requested_security_level == LEVEL_4); 51307ca4a7edSMatthias Ringwald bool can_reach_level_4 = hci_remote_sc_enabled(connection) && (c_256 != NULL); 51317ca4a7edSMatthias Ringwald if (need_level_4 && !can_reach_level_4){ 51327ca4a7edSMatthias Ringwald log_info("Level 4 required, but not possible -> abort"); 51337ca4a7edSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_INSUFFICIENT_SECURITY); 51347ca4a7edSMatthias Ringwald // send oob negative reply 51357ca4a7edSMatthias Ringwald c_256 = NULL; 51367ca4a7edSMatthias Ringwald c_192 = NULL; 51377ca4a7edSMatthias Ringwald } 51387ca4a7edSMatthias Ringwald 51391849becdSMatthias Ringwald // Reply 51401849becdSMatthias Ringwald if (c_256 != zero) { 51411849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 51421849becdSMatthias Ringwald } else if (c_192 != zero){ 51431849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 51441849becdSMatthias Ringwald } else { 51451ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = connection->con_handle; 51461849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 51471849becdSMatthias Ringwald } 51481849becdSMatthias Ringwald return true; 51491849becdSMatthias Ringwald } 51501849becdSMatthias Ringwald #endif 51511849becdSMatthias Ringwald 51528daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_REPLY){ 51538daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 515434d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 5155f30077b7SMatthias Ringwald return true; 5156dbe1a790S[email protected] } 5157dbe1a790S[email protected] 5158367aedc0SMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY){ 5159367aedc0SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 5160367aedc0SMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, &connection->address); 5161367aedc0SMatthias Ringwald return true; 5162367aedc0SMatthias Ringwald } 5163367aedc0SMatthias Ringwald 51648daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_PASSKEY_REPLY){ 51658daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 516634d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 5167f30077b7SMatthias Ringwald return true; 5168dbe1a790S[email protected] } 5169afd4e962S[email protected] 5170ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 5171ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 51721bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 51736ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 51746ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5175f30077b7SMatthias Ringwald return true; 5176ad83dc6aS[email protected] } 517776f27cffSMatthias Ringwald 51782a75353aSMatthias Ringwald if ((connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 517934d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 5180abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 518134d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 5182f30077b7SMatthias Ringwald return true; 5183afd4e962S[email protected] } 518476f27cffSMatthias Ringwald 5185dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 5186dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 5187dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 5188f30077b7SMatthias Ringwald return true; 5189dce78009S[email protected] } 5190447016ffSMatthias Ringwald 5191573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 5192573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 5193573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 5194f30077b7SMatthias Ringwald return true; 5195573897a0SMatthias Ringwald } 5196447016ffSMatthias Ringwald 5197447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 5198447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 5199447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 5200447016ffSMatthias Ringwald return true; 5201447016ffSMatthias Ringwald } 5202447016ffSMatthias Ringwald 5203447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 5204447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 5205447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 5206447016ffSMatthias Ringwald return true; 5207447016ffSMatthias Ringwald } 5208447016ffSMatthias Ringwald 5209447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 5210447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 5211447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 5212447016ffSMatthias Ringwald return true; 5213447016ffSMatthias Ringwald } 521476f27cffSMatthias Ringwald #endif 521576f27cffSMatthias Ringwald 521676f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 521776f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 52181714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 52191714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 52201714cbbdSMatthias Ringwald #endif 52216ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 52226ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 52236ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 5224f30077b7SMatthias Ringwald return true; 522576f27cffSMatthias Ringwald } 52266ef3696aSMatthias Ringwald } 5227da886c03S[email protected] 52286cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 5229f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 5230f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 5231f8ee3071SMatthias Ringwald case 0: 5232f8ee3071SMatthias Ringwald break; 5233f8ee3071SMatthias Ringwald case 0xffff: 5234f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 5235f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 5236f30077b7SMatthias Ringwald return true; 5237f8ee3071SMatthias Ringwald default: 5238f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 5239f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 5240f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 5241f30077b7SMatthias Ringwald return true; 5242f8ee3071SMatthias Ringwald } 524388a03c8dSMatthias Ringwald 5244140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 5245140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 5246140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 5247140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 5248140c0557SMatthias Ringwald return true; 5249140c0557SMatthias Ringwald } 5250140c0557SMatthias Ringwald 5251965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 5252278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 5253965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 5254278ff8a9SMatthias 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); 5255278ff8a9SMatthias Ringwald return true; 5256278ff8a9SMatthias Ringwald } 5257278ff8a9SMatthias Ringwald 525888a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 525988a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 526088a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 526188a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 526288a03c8dSMatthias Ringwald return true; 526388a03c8dSMatthias Ringwald } 52646cdc2862SMatthias Ringwald #endif 5265f8ee3071SMatthias Ringwald 5266a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 526773cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 526873cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 526973cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 5270da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 527173cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 5272c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 5273c37a3166S[email protected] 0x0000, 0xffff); 5274f30077b7SMatthias Ringwald return true; 527573cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 527673cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 527773cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 527873cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 527973cd8a2aSMatthias Ringwald 0x0000, 0xffff); 5280f30077b7SMatthias Ringwald return true; 528173cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 528273cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 528373cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, ERROR_CODE_UNSUPPORTED_LMP_PARAMETER_VALUE_UNSUPPORTED_LL_PARAMETER_VALUE); 5284f30077b7SMatthias Ringwald return true; 528573cd8a2aSMatthias Ringwald default: 528673cd8a2aSMatthias Ringwald break; 5287c37a3166S[email protected] } 52884ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 5289b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 5290b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 5291b90f6e0aSMatthias 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); 5292f30077b7SMatthias Ringwald return true; 5293b90f6e0aSMatthias Ringwald } 5294c37a3166S[email protected] #endif 5295dbe1a790S[email protected] } 5296f30077b7SMatthias Ringwald return false; 5297f30077b7SMatthias Ringwald } 5298c7e0c5f6Smatthias.ringwald 5299f30077b7SMatthias Ringwald static void hci_run(void){ 5300f30077b7SMatthias Ringwald 5301df9e2780SMatthias Ringwald // stack state sub statemachines 53020d37aff3SMatthias Ringwald // halting needs to be called even if we cannot send command packet now 5303df9e2780SMatthias Ringwald switch (hci_stack->state) { 5304df9e2780SMatthias Ringwald case HCI_STATE_INITIALIZING: 5305df9e2780SMatthias Ringwald hci_initializing_run(); 5306df9e2780SMatthias Ringwald break; 5307df9e2780SMatthias Ringwald case HCI_STATE_HALTING: 5308df9e2780SMatthias Ringwald hci_halting_run(); 5309df9e2780SMatthias Ringwald break; 5310df9e2780SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 5311df9e2780SMatthias Ringwald hci_falling_asleep_run(); 5312df9e2780SMatthias Ringwald break; 5313df9e2780SMatthias Ringwald default: 5314df9e2780SMatthias Ringwald break; 5315df9e2780SMatthias Ringwald } 5316df9e2780SMatthias Ringwald 5317f30077b7SMatthias Ringwald bool done; 5318f30077b7SMatthias Ringwald 5319f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 5320f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 5321f30077b7SMatthias Ringwald if (done) return; 5322f30077b7SMatthias Ringwald 5323f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 5324f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 5325f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 5326f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 5327f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 5328f30077b7SMatthias Ringwald return; 5329f30077b7SMatthias Ringwald } 5330f30077b7SMatthias Ringwald #endif 5331f30077b7SMatthias Ringwald 5332f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5333f30077b7SMatthias Ringwald 5334f30077b7SMatthias Ringwald // global/non-connection oriented commands 5335f30077b7SMatthias Ringwald 5336f30077b7SMatthias Ringwald 5337f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 5338f30077b7SMatthias Ringwald // general gap classic 5339f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 5340f30077b7SMatthias Ringwald if (done) return; 5341f30077b7SMatthias Ringwald #endif 5342f30077b7SMatthias Ringwald 5343f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 5344f30077b7SMatthias Ringwald // general gap le 5345f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 5346f30077b7SMatthias Ringwald if (done) return; 5347f30077b7SMatthias Ringwald #endif 5348f30077b7SMatthias Ringwald 5349f30077b7SMatthias Ringwald // send pending HCI commands 5350df9e2780SMatthias Ringwald hci_run_general_pending_commands(); 53513429f56bSmatthias.ringwald } 535216833f0aSmatthias.ringwald 53533e2050f7SMatthias Ringwald uint8_t hci_send_cmd_packet(uint8_t *packet, int size){ 535435454696SMatthias Ringwald // house-keeping 535535454696SMatthias Ringwald 535635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5357c8e4258aSmatthias.ringwald bd_addr_t addr; 5358c8e4258aSmatthias.ringwald hci_connection_t * conn; 5359c123d999SMatthias Ringwald #endif 5360c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 53616f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 53629cbd2215SMatthias Ringwald #endif 5363c8e4258aSmatthias.ringwald 53646f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 53656f35bb46SMatthias Ringwald switch (opcode) { 53669cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 53679cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 53689cbd2215SMatthias Ringwald break; 53699cbd2215SMatthias Ringwald 53709cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 53716f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 5372724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 53739da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 5374c8e4258aSmatthias.ringwald 537572cf8859SMatthias Ringwald // CVE-2020-26555: reject outgoing connection to device with same BD ADDR 537672cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0) { 537779e0fa07SMatthias Ringwald hci_emit_connection_complete(addr, 0, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR); 53783e2050f7SMatthias Ringwald return ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 537972cf8859SMatthias Ringwald } 538072cf8859SMatthias Ringwald 5381f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 5382ad83dc6aS[email protected] if (!conn) { 5383f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 538417f1ba2aSmatthias.ringwald if (!conn) { 538517f1ba2aSmatthias.ringwald // notify client that alloc failed 53862deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 53873e2050f7SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller 538817f1ba2aSmatthias.ringwald } 5389ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 5390f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 5391ad83dc6aS[email protected] } 5392aee5d5c1SMatthias Ringwald 5393aee5d5c1SMatthias Ringwald conn->con_handle = HCI_CON_HANDLE_INVALID; 5394aee5d5c1SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 5395aee5d5c1SMatthias Ringwald 5396ad83dc6aS[email protected] log_info("conn state %u", conn->state); 53973e2050f7SMatthias Ringwald // TODO: L2CAP should not send create connection command, instead a (new) gap function should be used 5398ad83dc6aS[email protected] switch (conn->state) { 5399ad83dc6aS[email protected] // if connection active exists 5400ad83dc6aS[email protected] case OPEN: 5401f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 540272cf8859SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, ERROR_CODE_SUCCESS); 54033e2050f7SMatthias Ringwald // packet not sent to controller 54043e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 5405672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 5406672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 5407672e36abSMatthias Ringwald break; 5408ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 5409532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 5410532f91a5SMatthias Ringwald break; 5411ad83dc6aS[email protected] default: 5412ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 54133e2050f7SMatthias Ringwald // packet not sent to controller 54143e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 5415ad83dc6aS[email protected] } 5416c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 5417229331c6SMatthias Ringwald 5418229331c6SMatthias Ringwald // track outgoing connection 5419f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 54206535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 54216f35bb46SMatthias Ringwald break; 5422ee752bb8SMatthias Ringwald 54235b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 54246f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 5425ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 5426ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 5427ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 54286f35bb46SMatthias Ringwald break; 54296f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 5430c91e6e4fSMatthias Ringwald // accept_synchronous_connection? Voice setting at offset 18 5431ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 5432ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 54331f34df2cSMatthias Ringwald // track outgoing connection 54341f34df2cSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_SCO; 54351f34df2cSMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->outgoing_addr); 54366f35bb46SMatthias Ringwald break; 5437ee752bb8SMatthias Ringwald #endif 543835454696SMatthias Ringwald #endif 54394b3e1e19SMatthias Ringwald 5440a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5441d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 54426f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 5443b04dfa37SMatthias Ringwald // white list used? 54446f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 5445b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 5446b04dfa37SMatthias Ringwald case 0: 5447b04dfa37SMatthias Ringwald // whitelist not used 5448b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 5449b04dfa37SMatthias Ringwald break; 5450b04dfa37SMatthias Ringwald case 1: 5451b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 5452b04dfa37SMatthias Ringwald break; 5453b04dfa37SMatthias Ringwald default: 5454b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 5455b04dfa37SMatthias Ringwald break; 5456b04dfa37SMatthias Ringwald } 5457c163146eSMatthias Ringwald // track outgoing connection 545805002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 5459c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 54606f35bb46SMatthias Ringwald break; 54616f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 54626ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 54636f35bb46SMatthias Ringwald break; 54649cbd2215SMatthias Ringwald #endif 54659cbd2215SMatthias Ringwald #endif 54666f35bb46SMatthias Ringwald default: 54676f35bb46SMatthias Ringwald break; 5468b04dfa37SMatthias Ringwald } 546969a97523S[email protected] 54703a9fb326S[email protected] hci_stack->num_cmd_packets--; 54715bb5bc3eS[email protected] 54725bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 54733e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 54743e2050f7SMatthias Ringwald if (err != 0){ 54753e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 54763e2050f7SMatthias Ringwald } 54773e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 547831452debSmatthias.ringwald } 54798adf0ddaSmatthias.ringwald 54802bd8b7e7S[email protected] // disconnect because of security block 54812bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 54822bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 54832bd8b7e7S[email protected] if (!connection) return; 54842bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 54852bd8b7e7S[email protected] } 54862bd8b7e7S[email protected] 54872bd8b7e7S[email protected] 5488dbe1a790S[email protected] // Configure Secure Simple Pairing 5489dbe1a790S[email protected] 549035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 549135454696SMatthias Ringwald 5492dbe1a790S[email protected] // enable will enable SSP during init 549315a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 54943a9fb326S[email protected] hci_stack->ssp_enable = enable; 5495dbe1a790S[email protected] } 5496dbe1a790S[email protected] 549795d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 549815a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 54992bd8b7e7S[email protected] } 55002bd8b7e7S[email protected] 5501dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 550215a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 55033a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 5504dbe1a790S[email protected] } 550515a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 55063a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 5507dbe1a790S[email protected] } 5508dbe1a790S[email protected] 5509dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 551015a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 55113a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 5512dbe1a790S[email protected] } 55135d23aae8SMatthias Ringwald 55145d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 55155d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 55165d23aae8SMatthias Ringwald } 55175d23aae8SMatthias Ringwald 551835454696SMatthias Ringwald #endif 5519dbe1a790S[email protected] 552094be1a66SMatthias Ringwald // va_list part of hci_send_cmd 55213e2050f7SMatthias Ringwald uint8_t hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 5522d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 55239d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 55243e2050f7SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 55259d14b626S[email protected] } 55269d14b626S[email protected] 55275127cc62S[email protected] // for HCI INITIALIZATION 55289da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 55295127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 55305127cc62S[email protected] 55319d14b626S[email protected] hci_reserve_packet_buffer(); 55329d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 553394be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 55343e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_packet(packet, size); 5535bfea0222SMatthias Ringwald 5536593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 55373e2050f7SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) || hci_transport_synchronous()){ 5538e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5539068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 5540bfea0222SMatthias Ringwald } 5541bfea0222SMatthias Ringwald 55423e2050f7SMatthias Ringwald return status; 554394be1a66SMatthias Ringwald } 55449d14b626S[email protected] 554594be1a66SMatthias Ringwald /** 554694be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 554794be1a66SMatthias Ringwald */ 55483e2050f7SMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t * cmd, ...){ 55491cd208adSmatthias.ringwald va_list argptr; 55501cd208adSmatthias.ringwald va_start(argptr, cmd); 55513e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_va_arg(cmd, argptr); 55521cd208adSmatthias.ringwald va_end(argptr); 55533e2050f7SMatthias Ringwald return status; 555493b8dc03Smatthias.ringwald } 5555c8e4258aSmatthias.ringwald 5556ee091cf1Smatthias.ringwald // Create various non-HCI events. 5557ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 5558ee091cf1Smatthias.ringwald 5559d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 5560fb37a842SMatthias Ringwald // dump packet 5561d6b06661SMatthias Ringwald if (dump) { 5562300c1ba4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, size); 5563d6b06661SMatthias Ringwald } 55641ef6bb52SMatthias Ringwald 5565fb37a842SMatthias Ringwald // dispatch to all event handlers 55661ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 55671ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 55681ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 55691ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 5570d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 55711ef6bb52SMatthias Ringwald } 5572d6b06661SMatthias Ringwald } 5573d6b06661SMatthias Ringwald 5574d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 5575fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 55763d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 5577d6b06661SMatthias Ringwald } 5578d6b06661SMatthias Ringwald 557935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 5580701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 55813bc639ceSMatthias Ringwald // notify SCO sender if waiting 5582701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 5583701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 55843bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 5585701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 5586701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 55873d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 55883bc639ceSMatthias Ringwald } 55893bc639ceSMatthias Ringwald } 55901cfb383eSMatthias Ringwald 55911cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 55929784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 5593ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 55941cfb383eSMatthias Ringwald 55951cfb383eSMatthias Ringwald uint8_t * eir_data; 55961cfb383eSMatthias Ringwald ad_context_t context; 55971cfb383eSMatthias Ringwald const uint8_t * name; 55981cfb383eSMatthias Ringwald uint8_t name_len; 55991cfb383eSMatthias Ringwald 56009784dac1SMatthias Ringwald if (size < 3) return; 56019784dac1SMatthias Ringwald 56021cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 5603a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 56041cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 56051cfb383eSMatthias Ringwald 56069784dac1SMatthias Ringwald switch (event_type){ 56079784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 56089784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 56099784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 56109784dac1SMatthias Ringwald break; 56119784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 56129784dac1SMatthias Ringwald if (size != 257) return; 56139784dac1SMatthias Ringwald if (num_responses != 1) return; 56149784dac1SMatthias Ringwald break; 56159784dac1SMatthias Ringwald default: 56169784dac1SMatthias Ringwald return; 56179784dac1SMatthias Ringwald } 56189784dac1SMatthias Ringwald 56191cfb383eSMatthias Ringwald // event[1] is set at the end 56201cfb383eSMatthias Ringwald int i; 56211cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 56221cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 56231cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 5624ac973fd6SMatthias Ringwald uint8_t event_size = 27; // if name is not set by EIR 56251cfb383eSMatthias Ringwald 56266535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 56270e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 56286535961aSMatthias Ringwald (void)memcpy(&event[9], 56296535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 56306535961aSMatthias Ringwald 3); // class of device 56316535961aSMatthias Ringwald (void)memcpy(&event[12], 56326535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 56336535961aSMatthias Ringwald 2); // clock offset 56341cfb383eSMatthias Ringwald 56351cfb383eSMatthias Ringwald switch (event_type){ 56361cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 56371cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 56381cfb383eSMatthias Ringwald break; 56391cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 56401cfb383eSMatthias Ringwald event[14] = 1; 5641a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 56421cfb383eSMatthias Ringwald // 16,17 = 0, size 18 56431cfb383eSMatthias Ringwald break; 56441cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 56451cfb383eSMatthias Ringwald event[14] = 1; 5646a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 564779186815SMatthias Ringwald // EIR packets only contain a single inquiry response 56481cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 56491cfb383eSMatthias Ringwald name = NULL; 5650a8c4e5adSMatthias Ringwald // Iterate over EIR data 5651a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 56521cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 56531cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 56541cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 5655ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 56561cfb383eSMatthias Ringwald switch (data_type){ 56571cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 56581cfb383eSMatthias Ringwald if (name) continue; 5659cf373d3aSMatthias Ringwald /* fall through */ 56601cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 56611cfb383eSMatthias Ringwald name = data; 56621cfb383eSMatthias Ringwald name_len = data_size; 56631cfb383eSMatthias Ringwald break; 5664ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 5665ac9136ccSMatthias Ringwald if (data_size != 8) break; 5666ac9136ccSMatthias Ringwald event[16] = 1; 56673c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 5668ac9136ccSMatthias Ringwald break; 56691cfb383eSMatthias Ringwald default: 56701cfb383eSMatthias Ringwald break; 56711cfb383eSMatthias Ringwald } 56721cfb383eSMatthias Ringwald } 56731cfb383eSMatthias Ringwald if (name){ 5674ac9136ccSMatthias Ringwald event[25] = 1; 56751cfb383eSMatthias Ringwald // truncate name if needed 56761cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 5677ac9136ccSMatthias Ringwald event[26] = len; 5678ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 56791cfb383eSMatthias Ringwald event_size += len; 56801cfb383eSMatthias Ringwald } 56811cfb383eSMatthias Ringwald break; 56827bbeb3adSMilanka Ringwald default: 56837bbeb3adSMilanka Ringwald return; 56841cfb383eSMatthias Ringwald } 56851cfb383eSMatthias Ringwald event[1] = event_size - 2; 56861cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 56871cfb383eSMatthias Ringwald } 56881cfb383eSMatthias Ringwald } 568935454696SMatthias Ringwald #endif 56903bc639ceSMatthias Ringwald 569171de195eSMatthias Ringwald void hci_emit_state(void){ 56923a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 5693425d1371Smatthias.ringwald uint8_t event[3]; 569480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 56954ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 56963a9fb326S[email protected] event[2] = hci_stack->state; 5697d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5698c8e4258aSmatthias.ringwald } 5699c8e4258aSmatthias.ringwald 570035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 57012deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 5702425d1371Smatthias.ringwald uint8_t event[13]; 5703c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5704425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 570517f1ba2aSmatthias.ringwald event[2] = status; 57062deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 57072deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 5708c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 5709c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 5710d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5711c8e4258aSmatthias.ringwald } 571252db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 571352db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 571452db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 571552db98b2SMatthias Ringwald uint8_t event[4]; 571652db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 571752db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 571852db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 571952db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 572052db98b2SMatthias Ringwald } 572135454696SMatthias Ringwald #endif 5722c8e4258aSmatthias.ringwald 572335454696SMatthias Ringwald #ifdef ENABLE_BLE 5724d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5725667ba9d1SMatthias 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){ 57264f3229d8S[email protected] uint8_t event[21]; 57274f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 57284ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 57294f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 57304f3229d8S[email protected] event[3] = status; 5731fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 57324f3229d8S[email protected] event[6] = 0; // TODO: role 57336e2e9a6bS[email protected] event[7] = address_type; 5734724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 5735f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 5736f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 5737f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 57384f3229d8S[email protected] event[20] = 0; // master clock accuracy 5739d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 57404f3229d8S[email protected] } 574135454696SMatthias Ringwald #endif 5742d70217a2SMatthias Ringwald #endif 57434f3229d8S[email protected] 5744fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 5745fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 5746fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 5747fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 5748fd43c0e0SMatthias Ringwald } 5749fd43c0e0SMatthias Ringwald 5750fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 5751425d1371Smatthias.ringwald uint8_t event[6]; 57523c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 57534ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 57543c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 5755fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 57563c4d4b90Smatthias.ringwald event[5] = reason; 5757d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 57583c4d4b90Smatthias.ringwald } 57593c4d4b90Smatthias.ringwald 5760b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 5761e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 5762425d1371Smatthias.ringwald uint8_t event[3]; 576380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 57644ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 576543bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 5766d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 576743bfb1bdSmatthias.ringwald } 5768038bc64cSmatthias.ringwald 5769b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 5770e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 5771425d1371Smatthias.ringwald uint8_t event[2]; 577280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 57734ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 5774d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5775038bc64cSmatthias.ringwald } 57761b0e3922Smatthias.ringwald 577735454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 577835454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 577935454696SMatthias Ringwald uint8_t event[9]; 578035454696SMatthias Ringwald int pos = 0; 578135454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 57824ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 578335454696SMatthias Ringwald event[pos++] = status; 578435454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 5785d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5786381fbed8Smatthias.ringwald } 5787458bf4e8S[email protected] 578835454696SMatthias Ringwald 578935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 579035454696SMatthias Ringwald 5791b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 5792df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 5793a00031e2S[email protected] uint8_t event[5]; 5794e00caf9cS[email protected] int pos = 0; 57955611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 5796e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 5797f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 5798e00caf9cS[email protected] pos += 2; 5799e00caf9cS[email protected] event[pos++] = level; 5800d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5801e00caf9cS[email protected] } 5802e00caf9cS[email protected] 580335454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 580435454696SMatthias Ringwald if (!connection) return LEVEL_0; 58058daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 5806fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 58078daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 5808170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 58094051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 5810170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 58110e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 5812170fafaeSMatthias Ringwald security_level = LEVEL_3; 5813170fafaeSMatthias Ringwald } 5814170fafaeSMatthias Ringwald return security_level; 581535454696SMatthias Ringwald } 581635454696SMatthias Ringwald 581735454696SMatthias Ringwald static void hci_emit_discoverable_enabled(uint8_t enabled){ 581835454696SMatthias Ringwald log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 581935454696SMatthias Ringwald uint8_t event[3]; 582035454696SMatthias Ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 582135454696SMatthias Ringwald event[1] = sizeof(event) - 2; 582235454696SMatthias Ringwald event[2] = enabled; 5823d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5824ad83dc6aS[email protected] } 5825ad83dc6aS[email protected] 582698a2fd1cSMatthias Ringwald // query if remote side supports eSCO 582720dcdd22SMatthias Ringwald bool hci_remote_esco_supported(hci_con_handle_t con_handle){ 582898a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 582920dcdd22SMatthias Ringwald if (!connection) return false; 583076ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 583198a2fd1cSMatthias Ringwald } 583298a2fd1cSMatthias Ringwald 583367aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 583467aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 583567aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 583667aae551SMatthias Ringwald } 583767aae551SMatthias Ringwald 58382bd8b7e7S[email protected] // query if remote side supports SSP 583920dcdd22SMatthias Ringwald bool hci_remote_ssp_supported(hci_con_handle_t con_handle){ 58402bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 584120dcdd22SMatthias Ringwald if (!connection) return false; 584267aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 58432bd8b7e7S[email protected] } 58442bd8b7e7S[email protected] 584520dcdd22SMatthias Ringwald bool gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 5846df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 5847df3354fcS[email protected] } 5848df3354fcS[email protected] 58496ffc78ceSMatthias Ringwald /** 58506ffc78ceSMatthias Ringwald * Check if remote supported features query has completed 58516ffc78ceSMatthias Ringwald */ 58526ffc78ceSMatthias Ringwald bool hci_remote_features_available(hci_con_handle_t handle){ 58536ffc78ceSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 58546ffc78ceSMatthias Ringwald if (!connection) return false; 58556ffc78ceSMatthias Ringwald return (connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0; 58566ffc78ceSMatthias Ringwald } 58576ffc78ceSMatthias Ringwald 5858d6596031SMatthias Ringwald /** 5859d6596031SMatthias Ringwald * Trigger remote supported features query 5860d6596031SMatthias Ringwald */ 5861c9742cd1SMatthias Ringwald 5862c9742cd1SMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection){ 5863c9742cd1SMatthias Ringwald if ((connection->bonding_flags & (BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_RECEIVED_REMOTE_FEATURES)) == 0){ 5864c9742cd1SMatthias Ringwald connection->bonding_flags |= BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 5865c9742cd1SMatthias Ringwald } 5866c9742cd1SMatthias Ringwald } 5867c9742cd1SMatthias Ringwald 5868d6596031SMatthias Ringwald void hci_remote_features_query(hci_con_handle_t con_handle){ 5869d6596031SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 5870d6596031SMatthias Ringwald if (!connection) return; 5871c9742cd1SMatthias Ringwald hci_trigger_remote_features_for_connection(connection); 5872d6596031SMatthias Ringwald hci_run(); 5873d6596031SMatthias Ringwald } 5874d6596031SMatthias Ringwald 5875458bf4e8S[email protected] // GAP API 5876458bf4e8S[email protected] /** 5877458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 5878458bf4e8S[email protected] * @praram enabled 5879458bf4e8S[email protected] */ 58804c57c146S[email protected] void gap_set_bondable_mode(int enable){ 58813a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 5882458bf4e8S[email protected] } 58834ef6443cSMatthias Ringwald /** 58844ef6443cSMatthias Ringwald * @brief Get bondable mode. 58854ef6443cSMatthias Ringwald * @return 1 if bondable 58864ef6443cSMatthias Ringwald */ 58874ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 58884ef6443cSMatthias Ringwald return hci_stack->bondable; 58894ef6443cSMatthias Ringwald } 5890cb230b9dS[email protected] 5891cb230b9dS[email protected] /** 589234d2123cS[email protected] * @brief map link keys to security levels 5893cb230b9dS[email protected] */ 589434d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 589534d2123cS[email protected] switch (link_key_type){ 58963c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 58973c68dfa9S[email protected] return LEVEL_4; 58983c68dfa9S[email protected] case COMBINATION_KEY: 58993c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 59003c68dfa9S[email protected] return LEVEL_3; 59013c68dfa9S[email protected] default: 59023c68dfa9S[email protected] return LEVEL_2; 59033c68dfa9S[email protected] } 5904cb230b9dS[email protected] } 5905cb230b9dS[email protected] 59068b35e16aSMatthias Ringwald /** 59078b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 59088b35e16aSMatthias Ringwald */ 590936c3546bSMatthias Ringwald bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 59108b35e16aSMatthias Ringwald switch (link_key_type){ 59118b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 59128b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 591336c3546bSMatthias Ringwald return true; 59148b35e16aSMatthias Ringwald default: 591536c3546bSMatthias Ringwald return false; 59168b35e16aSMatthias Ringwald } 59178b35e16aSMatthias Ringwald } 59188b35e16aSMatthias Ringwald 59198b35e16aSMatthias Ringwald /** 59208b35e16aSMatthias Ringwald * @brief map link keys to authenticated 59218b35e16aSMatthias Ringwald */ 59227040ba26SMatthias Ringwald bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 59238b35e16aSMatthias Ringwald switch (link_key_type){ 59248b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 59258b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 59267040ba26SMatthias Ringwald return true; 59278b35e16aSMatthias Ringwald default: 59287040ba26SMatthias Ringwald return false; 59298b35e16aSMatthias Ringwald } 59308b35e16aSMatthias Ringwald } 59318b35e16aSMatthias Ringwald 5932552b9260SMatthias Ringwald bool gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 59335127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 5934106d6d11S[email protected] return level > LEVEL_2; 5935106d6d11S[email protected] } 5936106d6d11S[email protected] 593734d2123cS[email protected] /** 593834d2123cS[email protected] * @brief get current security level 593934d2123cS[email protected] */ 594034d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 594134d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 594234d2123cS[email protected] if (!connection) return LEVEL_0; 594334d2123cS[email protected] return gap_security_level_for_connection(connection); 594434d2123cS[email protected] } 594534d2123cS[email protected] 5946cb230b9dS[email protected] /** 5947cb230b9dS[email protected] * @brief request connection to device to 5948cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 5949cb230b9dS[email protected] */ 595034d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 595134d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 595234d2123cS[email protected] if (!connection){ 5953a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 595434d2123cS[email protected] return; 595534d2123cS[email protected] } 5956defbf200SMatthias Ringwald 5957defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 5958defbf200SMatthias Ringwald 5959bc00e12cSMatthias 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) 5960bc00e12cSMatthias Ringwald // available on the BR/EDR physical transport require Security Mode 4, Level 4 " 5961bc00e12cSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode && (requested_level != LEVEL_0)){ 5962bc00e12cSMatthias Ringwald requested_level = LEVEL_4; 5963bc00e12cSMatthias Ringwald } 5964bc00e12cSMatthias Ringwald 596534d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 596683d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 596783d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 596883d08d7cSMatthias Ringwald 5969dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 5970dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 5971dbd5dcc3SMatthias Ringwald if (authentication_active){ 59721cf0a6c8SMatthias Ringwald // authentication already active 597383d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 597483d08d7cSMatthias Ringwald // increase requested level as new level is higher 597583d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 597683d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 597783d08d7cSMatthias Ringwald } 59781cf0a6c8SMatthias Ringwald } else { 597983d08d7cSMatthias Ringwald // no request active, notify if security sufficient 598083d08d7cSMatthias Ringwald if (requested_level <= current_level){ 5981a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 598234d2123cS[email protected] return; 598334d2123cS[email protected] } 5984a00031e2S[email protected] 5985e060c07dSMatthias Ringwald // store request 598634d2123cS[email protected] connection->requested_security_level = requested_level; 5987a00031e2S[email protected] 5988e5470e6dSMatthias Ringwald // request remote features if not already active 5989e5470e6dSMatthias Ringwald hci_remote_features_query(con_handle); 5990e5470e6dSMatthias Ringwald 59911cf0a6c8SMatthias Ringwald // start to authenticate connection 59921eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 5993e80b2cf9S[email protected] hci_run(); 5994e00caf9cS[email protected] } 59951cf0a6c8SMatthias Ringwald } 5996ad83dc6aS[email protected] 5997ad83dc6aS[email protected] /** 5998ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 5999ad83dc6aS[email protected] * @param device 6000ad83dc6aS[email protected] * @param request MITM protection 6001ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 6002ad83dc6aS[email protected] */ 6003ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 6004ad83dc6aS[email protected] 6005ad83dc6aS[email protected] // create connection state machine 6006f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 6007ad83dc6aS[email protected] 6008ad83dc6aS[email protected] if (!connection){ 6009ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 6010ad83dc6aS[email protected] } 6011ad83dc6aS[email protected] 6012ad83dc6aS[email protected] // delete linkn key 601315a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 6014ad83dc6aS[email protected] 6015ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 6016ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 6017ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 6018f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 6019ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 6020ad83dc6aS[email protected] 6021ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 6022ad83dc6aS[email protected] 6023ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 6024ad83dc6aS[email protected] // handle: authentication failure 6025ad83dc6aS[email protected] // handle: disconnect on done 6026ad83dc6aS[email protected] 6027ad83dc6aS[email protected] hci_run(); 6028ad83dc6aS[email protected] 6029ad83dc6aS[email protected] return 0; 6030ad83dc6aS[email protected] } 60318e618f72S[email protected] 60328e618f72S[email protected] void gap_set_local_name(const char * local_name){ 60338e618f72S[email protected] hci_stack->local_name = local_name; 6034baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_LOCAL_NAME; 603559d59ecfSMatthias Ringwald // also update EIR if not set by user 603659d59ecfSMatthias Ringwald if (hci_stack->eir_data == NULL){ 6037baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 603859d59ecfSMatthias Ringwald } 603959d59ecfSMatthias Ringwald hci_run(); 60408e618f72S[email protected] } 60411aeec2ebSMatthias Ringwald #endif 60428e618f72S[email protected] 604335454696SMatthias Ringwald 604435454696SMatthias Ringwald #ifdef ENABLE_BLE 604535454696SMatthias Ringwald 6046d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6047d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 6048fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 60497bdc6798S[email protected] hci_run(); 60507bdc6798S[email protected] } 60518e618f72S[email protected] 6052d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 6053fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 60547bdc6798S[email protected] hci_run(); 60557bdc6798S[email protected] } 60564f3229d8S[email protected] 6057a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 6058ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 6059a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 6060ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 6061ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 60628b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 6063ef11999fSmatthias.ringwald hci_run(); 6064ef11999fSmatthias.ringwald } 60654f3229d8S[email protected] 6066a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 6067a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 6068a7a719e9SMatthias Ringwald } 6069a7a719e9SMatthias Ringwald 6070667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 60714f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 60724f3229d8S[email protected] if (!conn){ 6073d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 6074d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 6075d32b3f05SMatthias Ringwald log_error("le connection already active"); 6076d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6077d32b3f05SMatthias Ringwald } 6078d32b3f05SMatthias Ringwald 6079d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 60802e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 60814f3229d8S[email protected] if (!conn){ 60824f3229d8S[email protected] // notify client that alloc failed 60836e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 6084d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 6085472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 60864f3229d8S[email protected] } 6087d5b1a89eSMatthias Ringwald 6088d5b1a89eSMatthias Ringwald // set le connecting state 6089d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 6090d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 6091d5b1a89eSMatthias Ringwald } 6092d5b1a89eSMatthias Ringwald 60934f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 6094d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 6095564fca32S[email protected] hci_run(); 6096b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 60974f3229d8S[email protected] } 60980bf6344aS[email protected] 60990bf6344aS[email protected] if (!hci_is_le_connection(conn) || 6100a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 61010e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 61022e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 6103d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 6104616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 61050bf6344aS[email protected] } 61060bf6344aS[email protected] 6107b0136355SMatthias Ringwald // check if connection was just disconnected 6108b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 6109b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 6110b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 6111b0136355SMatthias Ringwald hci_run(); 6112b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 6113b0136355SMatthias Ringwald } 6114b0136355SMatthias Ringwald 6115d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 6116d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 61174f3229d8S[email protected] hci_run(); 6118b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 61194f3229d8S[email protected] } 61204f3229d8S[email protected] 61217851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 6122d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 6123665d90f2SMatthias Ringwald btstack_linked_item_t *it; 6124a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 61250bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 61260bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 61270bf6344aS[email protected] switch (conn->state){ 6128a6725849S[email protected] case SEND_CREATE_CONNECTION: 61297851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 61309c77c9dbSMatthias Ringwald case SENT_CANCEL_CONNECTION: 61317851196eSmatthias.ringwald return conn; 61327851196eSmatthias.ringwald default: 61337851196eSmatthias.ringwald break; 61347851196eSmatthias.ringwald }; 61357851196eSmatthias.ringwald } 61367851196eSmatthias.ringwald return NULL; 61377851196eSmatthias.ringwald } 61387851196eSmatthias.ringwald 6139d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 6140d8e8f12aSMatthias Ringwald hci_connection_t * conn = gap_get_outgoing_connection(); 6141616edd56SMatthias Ringwald if (!conn) return 0; 61427851196eSmatthias.ringwald switch (conn->state){ 61437851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 61447851196eSmatthias.ringwald // skip sending create connection and emit event instead 614510f8f469SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 61462e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 6147665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 61487851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 61490bf6344aS[email protected] break; 6150a6725849S[email protected] case SENT_CREATE_CONNECTION: 61517851196eSmatthias.ringwald // request to send cancel connection 61520bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 61530bf6344aS[email protected] hci_run(); 61540bf6344aS[email protected] break; 61550bf6344aS[email protected] default: 61560bf6344aS[email protected] break; 61570bf6344aS[email protected] } 6158616edd56SMatthias Ringwald return 0; 6159e31f89a7S[email protected] } 6160d70217a2SMatthias Ringwald #endif 61614f3229d8S[email protected] 616213e645d2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6163c37a3166S[email protected] /** 61646012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 6165cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 6166cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 61676012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 61686012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 61696012052bSMatthias Ringwald * @param conn_latency, default: 4 61706012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 61716012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 61726012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 61736012052bSMatthias Ringwald */ 61746012052bSMatthias Ringwald 6175cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 6176cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 6177cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 6178cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 6179cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 61806012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 61816012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 61826012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 61836012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 61846012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 61856012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 61866012052bSMatthias Ringwald } 618713e645d2SMatthias Ringwald #endif 61886012052bSMatthias Ringwald 61896012052bSMatthias Ringwald /** 6190c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 6191c37a3166S[email protected] * @param handle 6192c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 6193c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 6194c37a3166S[email protected] * @param conn_latency 6195c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 61966b65794dSMilanka Ringwald * @return 0 if ok 6197c37a3166S[email protected] */ 6198c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 6199c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 6200c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 6201c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6202c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 6203c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 6204c37a3166S[email protected] connection->le_conn_latency = conn_latency; 6205c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 620684cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 6207cfc59f1bSMatthias Ringwald hci_run(); 6208c37a3166S[email protected] return 0; 6209c37a3166S[email protected] } 6210c37a3166S[email protected] 621145c102fdSMatthias Ringwald /** 6212b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 6213b68d7bc3SMatthias Ringwald * @param handle 6214b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 6215b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 6216b68d7bc3SMatthias Ringwald * @param conn_latency 6217b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 62186b65794dSMilanka Ringwald * @return 0 if ok 6219b68d7bc3SMatthias Ringwald */ 6220b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 6221b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 6222b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 6223b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6224b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 6225b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 6226b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 6227b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 6228b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 622909c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 623009c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 6231b68d7bc3SMatthias Ringwald return 0; 6232b68d7bc3SMatthias Ringwald } 6233b68d7bc3SMatthias Ringwald 6234d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 6235d70217a2SMatthias Ringwald 6236b68d7bc3SMatthias Ringwald /** 623745c102fdSMatthias Ringwald * @brief Set Advertisement Data 623845c102fdSMatthias Ringwald * @param advertising_data_length 623945c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 624045c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 624145c102fdSMatthias Ringwald */ 624245c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 624345c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 624445c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 6245501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 6246bbc366e6SMatthias Ringwald hci_run(); 624745c102fdSMatthias Ringwald } 6248501f56b3SMatthias Ringwald 6249501f56b3SMatthias Ringwald /** 6250501f56b3SMatthias Ringwald * @brief Set Scan Response Data 6251501f56b3SMatthias Ringwald * @param advertising_data_length 6252501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 6253501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 6254501f56b3SMatthias Ringwald */ 6255501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 6256501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 6257501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 6258501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 6259bbc366e6SMatthias Ringwald hci_run(); 626045c102fdSMatthias Ringwald } 626145c102fdSMatthias Ringwald 626245c102fdSMatthias Ringwald /** 626345c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 626445c102fdSMatthias Ringwald * @param adv_int_min 626545c102fdSMatthias Ringwald * @param adv_int_max 626645c102fdSMatthias Ringwald * @param adv_type 626745c102fdSMatthias Ringwald * @param direct_address_type 626845c102fdSMatthias Ringwald * @param direct_address 626945c102fdSMatthias Ringwald * @param channel_map 627045c102fdSMatthias Ringwald * @param filter_policy 627145c102fdSMatthias Ringwald * 627245c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 627345c102fdSMatthias Ringwald */ 627445c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 6275b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 627645c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 627745c102fdSMatthias Ringwald 627845c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 627945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 628045c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 628145c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 628245c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 628345c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 62846535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 62856535961aSMatthias Ringwald 6); 628645c102fdSMatthias Ringwald 6287a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS | LE_ADVERTISEMENT_TASKS_PARAMS_SET; 6288bbc366e6SMatthias Ringwald hci_run(); 628945c102fdSMatthias Ringwald } 629045c102fdSMatthias Ringwald 629145c102fdSMatthias Ringwald /** 629245c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 629345c102fdSMatthias Ringwald * @param enabled 629445c102fdSMatthias Ringwald */ 629545c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 62965226d7f2SMatthias Ringwald hci_stack->le_advertisements_enabled = enabled != 0; 6297bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 6298cfc59f1bSMatthias Ringwald hci_run(); 629945c102fdSMatthias Ringwald } 630045c102fdSMatthias Ringwald 630135454696SMatthias Ringwald #endif 630206e5cf96SMatthias Ringwald 630306e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 630406e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 630506e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 630606e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 630706e5cf96SMatthias Ringwald 630864068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 630906e5cf96SMatthias Ringwald // update advertisement parameters, too 631006e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 6311bbc366e6SMatthias Ringwald hci_run(); 631264068776SMatthias Ringwald #endif 631364068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 631406e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 631564068776SMatthias Ringwald #endif 631606e5cf96SMatthias Ringwald } 631706e5cf96SMatthias Ringwald 63189136bde8SMatthias Ringwald void hci_le_random_address_set(const bd_addr_t random_address){ 63199136bde8SMatthias Ringwald memcpy(hci_stack->le_random_address, random_address, 6); 63209136bde8SMatthias Ringwald hci_stack->le_random_address_set = true; 63219136bde8SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 63229136bde8SMatthias Ringwald hci_run(); 63239136bde8SMatthias Ringwald } 63249136bde8SMatthias Ringwald 6325d70217a2SMatthias Ringwald #endif 632645c102fdSMatthias Ringwald 6327616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 63285917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 63295917a5c5S[email protected] if (!conn){ 63307851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 6331616edd56SMatthias Ringwald return 0; 63325917a5c5S[email protected] } 63337fd7aa6fSMatthias Ringwald // ignore if already disconnected 63347fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 63357fd7aa6fSMatthias Ringwald return 0; 63367fd7aa6fSMatthias Ringwald } 63375917a5c5S[email protected] conn->state = SEND_DISCONNECT; 63385917a5c5S[email protected] hci_run(); 6339616edd56SMatthias Ringwald return 0; 63404f3229d8S[email protected] } 634104a6ef8cSmatthias.ringwald 6342228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 6343228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6344228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 63458daf94bcSMatthias Ringwald connectionSetAuthenticationFlags(hci_connection, AUTH_FLAG_READ_RSSI); 6346228e430cSMatthias Ringwald hci_run(); 6347228e430cSMatthias Ringwald return 1; 6348228e430cSMatthias Ringwald } 6349228e430cSMatthias Ringwald 6350a1bf5ae7SMatthias Ringwald /** 6351a1bf5ae7SMatthias Ringwald * @brief Get connection type 6352a1bf5ae7SMatthias Ringwald * @param con_handle 6353a1bf5ae7SMatthias Ringwald * @result connection_type 6354a1bf5ae7SMatthias Ringwald */ 6355a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 6356a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 6357a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 6358a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 6359a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 6360a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6361a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 6362a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 6363a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 6364f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6365a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 6366a1bf5ae7SMatthias Ringwald default: 6367a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 6368a1bf5ae7SMatthias Ringwald } 6369a1bf5ae7SMatthias Ringwald } 6370a1bf5ae7SMatthias Ringwald 63712dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 63722dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 63732dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 63742dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 63752dceb1d6SMatthias Ringwald } 63762dceb1d6SMatthias Ringwald 63772dceb1d6SMatthias Ringwald 637844f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 6379667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 638088a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 638188a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 638288a03c8dSMatthias Ringwald conn->request_role = role; 638388a03c8dSMatthias Ringwald hci_run(); 6384d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 638588a03c8dSMatthias Ringwald } 638644f858f3SMatthias Ringwald #endif 638788a03c8dSMatthias Ringwald 6388a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 63894f551432SMatthias Ringwald 6390b45b7749SMilanka 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){ 6391b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6392b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6393b90f6e0aSMatthias Ringwald 6394b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 6395b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 6396b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 6397b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 6398b90f6e0aSMatthias Ringwald 6399b90f6e0aSMatthias Ringwald hci_run(); 6400b90f6e0aSMatthias Ringwald 6401b90f6e0aSMatthias Ringwald return 0; 6402b90f6e0aSMatthias Ringwald } 6403b90f6e0aSMatthias Ringwald 6404667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 64057a92a9dbSMatthias Ringwald // check if already in list 64067a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 64077a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 64087a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 64097a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 64107a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 64117a92a9dbSMatthias Ringwald continue; 64127a92a9dbSMatthias Ringwald } 64137a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 64147a92a9dbSMatthias Ringwald continue; 64157a92a9dbSMatthias Ringwald } 6416287379ccSMatthias Ringwald // disallow if already scheduled to add 6417287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 64187a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 64197a92a9dbSMatthias Ringwald } 6420287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 6421287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 6422287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 6423287379ccSMatthias Ringwald } 64247a92a9dbSMatthias Ringwald // alloc and add to list 6425e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 6426e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 6427e83201bcSMatthias Ringwald entry->address_type = address_type; 64286535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 6429e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 6430665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 6431226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 6432ac9c45e0SMatthias Ringwald } 6433ac9c45e0SMatthias Ringwald 6434667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6435665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6436665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 6437665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6438665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 64397a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 64407a92a9dbSMatthias Ringwald continue; 64417a92a9dbSMatthias Ringwald } 64427a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 64437a92a9dbSMatthias Ringwald continue; 64447a92a9dbSMatthias Ringwald } 6445e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 6446e83201bcSMatthias Ringwald // remove from controller if already present 6447e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6448a3b69fdeSMatthias Ringwald } else { 6449226db5efSMatthias Ringwald // directly remove entry from whitelist 6450665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6451e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 6452e83201bcSMatthias Ringwald } 6453a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6454a3b69fdeSMatthias Ringwald } 6455a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 645642ff5ba1SMatthias Ringwald } 645742ff5ba1SMatthias Ringwald 6458226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 6459665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6460665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 6461665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6462665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 6463e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 6464e83201bcSMatthias Ringwald // remove from controller if already present 6465e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6466e83201bcSMatthias Ringwald continue; 6467e83201bcSMatthias Ringwald } 646891915b0bSMatthias Ringwald // directly remove entry from whitelist 6469665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 6470e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 6471e83201bcSMatthias Ringwald } 6472226db5efSMatthias Ringwald } 6473226db5efSMatthias Ringwald 647483dc1a98SMatthias Ringwald // free all entries unconditionally 647583dc1a98SMatthias Ringwald static void hci_whitelist_free(void){ 647683dc1a98SMatthias Ringwald btstack_linked_list_iterator_t lit; 647783dc1a98SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 647883dc1a98SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 647983dc1a98SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 648083dc1a98SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 648183dc1a98SMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 648283dc1a98SMatthias Ringwald } 648383dc1a98SMatthias Ringwald } 648483dc1a98SMatthias Ringwald 6485a3b69fdeSMatthias Ringwald /** 6486a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 64876b65794dSMilanka Ringwald * @return 0 if ok 6488a3b69fdeSMatthias Ringwald */ 6489a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 6490a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 6491a3b69fdeSMatthias Ringwald hci_run(); 6492a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6493a3b69fdeSMatthias Ringwald } 6494a3b69fdeSMatthias Ringwald 6495a3b69fdeSMatthias Ringwald /** 6496a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 6497a3b69fdeSMatthias Ringwald * @param address_typ 6498a3b69fdeSMatthias Ringwald * @param address 64996b65794dSMilanka Ringwald * @return 0 if ok 6500a3b69fdeSMatthias Ringwald */ 6501667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 6502a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 6503a3b69fdeSMatthias Ringwald if (status){ 6504a3b69fdeSMatthias Ringwald return status; 6505a3b69fdeSMatthias Ringwald } 6506a3b69fdeSMatthias Ringwald hci_run(); 6507a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6508a3b69fdeSMatthias Ringwald } 6509a3b69fdeSMatthias Ringwald 6510a3b69fdeSMatthias Ringwald /** 6511a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 6512a3b69fdeSMatthias Ringwald * @param address_typ 6513a3b69fdeSMatthias Ringwald * @param address 65146b65794dSMilanka Ringwald * @return 0 if ok 6515a3b69fdeSMatthias Ringwald */ 6516667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 6517a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 6518a3b69fdeSMatthias Ringwald if (status){ 6519a3b69fdeSMatthias Ringwald return status; 6520a3b69fdeSMatthias Ringwald } 6521a3b69fdeSMatthias Ringwald hci_run(); 6522a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 6523a3b69fdeSMatthias Ringwald } 6524a3b69fdeSMatthias Ringwald 6525226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6526226db5efSMatthias Ringwald /** 652795e257d9SMatthias Ringwald * @brief Connect with Whitelist 652895e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 65296b65794dSMilanka Ringwald * @return - if ok 653095e257d9SMatthias Ringwald */ 653195e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 653295e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 653395e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 653495e257d9SMatthias Ringwald } 653595e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 653695e257d9SMatthias Ringwald hci_run(); 653795e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 653895e257d9SMatthias Ringwald } 653995e257d9SMatthias Ringwald 654095e257d9SMatthias Ringwald /** 6541226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 6542226db5efSMatthias Ringwald * @param address_typ 6543226db5efSMatthias Ringwald * @param address 65446b65794dSMilanka Ringwald * @return 0 if ok 6545226db5efSMatthias Ringwald */ 6546667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 654795e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 6548226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 6549226db5efSMatthias Ringwald } 6550226db5efSMatthias Ringwald 6551226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 655263f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 6553226db5efSMatthias Ringwald return status; 6554226db5efSMatthias Ringwald } 6555226db5efSMatthias Ringwald 6556226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 6557226db5efSMatthias Ringwald 6558226db5efSMatthias Ringwald hci_run(); 655995e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6560226db5efSMatthias Ringwald } 6561226db5efSMatthias Ringwald 6562226db5efSMatthias Ringwald /** 6563226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 6564226db5efSMatthias Ringwald * @param address_typ 6565226db5efSMatthias Ringwald * @param address 65666b65794dSMilanka Ringwald * @return 0 if ok 6567226db5efSMatthias Ringwald */ 6568667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 656995e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 657095e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 657195e257d9SMatthias Ringwald } 657295e257d9SMatthias Ringwald 6573226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 657495e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 657595e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 657695e257d9SMatthias Ringwald } 6577226db5efSMatthias Ringwald hci_run(); 6578226db5efSMatthias Ringwald return 0; 6579226db5efSMatthias Ringwald } 6580226db5efSMatthias Ringwald 6581226db5efSMatthias Ringwald /** 6582226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 6583226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 6584226db5efSMatthias Ringwald */ 658595e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 658695e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 658795e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 658895e257d9SMatthias Ringwald } 6589226db5efSMatthias Ringwald hci_whitelist_clear(); 6590226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 6591e83201bcSMatthias Ringwald hci_run(); 659295e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 6593ac9c45e0SMatthias Ringwald } 6594c9db5c21SMilanka Ringwald 6595b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 6596b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 6597c9db5c21SMilanka Ringwald if (!conn) return 0; 6598c9db5c21SMilanka Ringwald return conn->le_connection_interval; 6599c9db5c21SMilanka Ringwald } 6600d70217a2SMatthias Ringwald #endif 66014f551432SMatthias Ringwald #endif 66024f551432SMatthias Ringwald 660335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6604ac9c45e0SMatthias Ringwald /** 6605ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 6606a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 6607ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 6608ff00ed1cSMatthias Ringwald */ 6609ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 6610ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 6611baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 661259d59ecfSMatthias Ringwald hci_run(); 6613ff00ed1cSMatthias Ringwald } 6614ff00ed1cSMatthias Ringwald 6615ff00ed1cSMatthias Ringwald /** 6616f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 6617f5875de5SMatthias Ringwald * @param duration in 1.28s units 6618f5875de5SMatthias Ringwald * @return 0 if ok 6619f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 6620f5875de5SMatthias Ringwald */ 6621f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 662299449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 6623f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6624a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 6625f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 6626f5875de5SMatthias Ringwald } 6627f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 6628f5875de5SMatthias Ringwald hci_run(); 6629f5875de5SMatthias Ringwald return 0; 6630f5875de5SMatthias Ringwald } 6631f5875de5SMatthias Ringwald 6632f5875de5SMatthias Ringwald /** 6633f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 66346b65794dSMilanka Ringwald * @return 0 if ok 6635f5875de5SMatthias Ringwald */ 6636f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 6637a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 6638f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 6639f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 6640f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 6641f5875de5SMatthias Ringwald return 0; 6642f5875de5SMatthias Ringwald } 6643f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_ACTIVE) return ERROR_CODE_COMMAND_DISALLOWED; 6644f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 6645f5875de5SMatthias Ringwald hci_run(); 6646f5875de5SMatthias Ringwald return 0; 6647f5875de5SMatthias Ringwald } 6648f5875de5SMatthias Ringwald 6649496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 6650496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 6651496bb884SMatthias Ringwald } 6652496bb884SMatthias Ringwald 66531dc973e7SMatthias Ringwald void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window){ 6654c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_interval = inquiry_scan_interval; 6655c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_window = inquiry_scan_window; 6656baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 6657c0c8a829SMatthias Ringwald hci_run(); 6658c0c8a829SMatthias Ringwald } 6659c0c8a829SMatthias Ringwald 6660b7f1ee76SMatthias Ringwald 6661b7f1ee76SMatthias Ringwald /** 6662b7f1ee76SMatthias Ringwald * @brief Remote Name Request 6663b7f1ee76SMatthias Ringwald * @param addr 6664b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 6665b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 6666b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 6667b7f1ee76SMatthias Ringwald */ 6668667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 6669b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 66706535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 6671b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 6672b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 6673b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 6674b7f1ee76SMatthias Ringwald hci_run(); 6675b7f1ee76SMatthias Ringwald return 0; 6676b7f1ee76SMatthias Ringwald } 6677b7f1ee76SMatthias Ringwald 6678667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 66790a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 66806535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 66810a51f88bSMatthias Ringwald hci_run(); 66820a51f88bSMatthias Ringwald return 0; 66830a51f88bSMatthias Ringwald } 66840a51f88bSMatthias Ringwald 66850a51f88bSMatthias Ringwald /** 6686aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 6687aad97216SMatthias Ringwald * @param addr 6688aad97216SMatthias Ringwald * @param pin_data 6689aad97216SMatthias Ringwald * @param pin_len 6690aad97216SMatthias Ringwald * @return 0 if ok 6691aad97216SMatthias Ringwald */ 6692667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 6693aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6694aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 6695aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 6696aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 6697aad97216SMatthias Ringwald } 6698aad97216SMatthias Ringwald 6699aad97216SMatthias Ringwald /** 67000a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 67010a51f88bSMatthias Ringwald * @param addr 67020a51f88bSMatthias Ringwald * @param pin 67030a51f88bSMatthias Ringwald * @return 0 if ok 67040a51f88bSMatthias Ringwald */ 6705667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 6706aad97216SMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, strlen(pin)); 67070a51f88bSMatthias Ringwald } 67080a51f88bSMatthias Ringwald 67090a51f88bSMatthias Ringwald /** 67100a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 67110a51f88bSMatthias Ringwald * @param addr 67120a51f88bSMatthias Ringwald * @param pin 67130a51f88bSMatthias Ringwald * @return 0 if ok 67140a51f88bSMatthias Ringwald */ 67150a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 6716cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 67170a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 67180a51f88bSMatthias Ringwald } 67190a51f88bSMatthias Ringwald 67200a51f88bSMatthias Ringwald /** 67210a51f88bSMatthias Ringwald * @brief SSP Passkey Response 67220a51f88bSMatthias Ringwald * @param addr 67230a51f88bSMatthias Ringwald * @param passkey 67240a51f88bSMatthias Ringwald * @return 0 if ok 67250a51f88bSMatthias Ringwald */ 6726667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 6727cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 6728d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 67290a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 67300a51f88bSMatthias Ringwald } 67310a51f88bSMatthias Ringwald 67320a51f88bSMatthias Ringwald /** 67330a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 67340a51f88bSMatthias Ringwald * @param addr 67350a51f88bSMatthias Ringwald * @param pin 67360a51f88bSMatthias Ringwald * @return 0 if ok 67370a51f88bSMatthias Ringwald */ 6738667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 6739cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 67400a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 67410a51f88bSMatthias Ringwald } 67420a51f88bSMatthias Ringwald 67430a51f88bSMatthias Ringwald /** 67440a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 67450a51f88bSMatthias Ringwald * @param addr 67460a51f88bSMatthias Ringwald * @param passkey 67470a51f88bSMatthias Ringwald * @return 0 if ok 67480a51f88bSMatthias Ringwald */ 6749667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 6750cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 67510a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 67520a51f88bSMatthias Ringwald } 67530a51f88bSMatthias Ringwald 67540a51f88bSMatthias Ringwald /** 67550a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 67560a51f88bSMatthias Ringwald * @param addr 67570a51f88bSMatthias Ringwald * @param pin 67580a51f88bSMatthias Ringwald * @return 0 if ok 67590a51f88bSMatthias Ringwald */ 6760667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 6761cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 67620a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 67630a51f88bSMatthias Ringwald } 67640a51f88bSMatthias Ringwald 6765308eeaffSMatthias Ringwald #if defined(ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY) || defined(ENABLE_EXPLICIT_LINK_KEY_REPLY) 676644565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 676744565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 676844565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 676944565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 677044565b0cSMatthias Ringwald hci_run(); 677144565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 677244565b0cSMatthias Ringwald } 6773308eeaffSMatthias Ringwald #endif 677444565b0cSMatthias Ringwald 6775308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 677644565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 67777ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 677844565b0cSMatthias Ringwald } 677944565b0cSMatthias Ringwald 678044565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 67817ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 678244565b0cSMatthias Ringwald } 678344565b0cSMatthias Ringwald #endif 678444565b0cSMatthias Ringwald 67851849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 67861849becdSMatthias Ringwald /** 67871849becdSMatthias Ringwald * @brief Report Remote OOB Data 67881849becdSMatthias Ringwald * @param bd_addr 67891849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 67901849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 67911849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 67921849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 67931849becdSMatthias Ringwald */ 67941849becdSMatthias 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){ 67951849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 67961849becdSMatthias Ringwald if (connection == NULL) { 67971849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 67981849becdSMatthias Ringwald } 67991849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 68001849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 6801204e8f1dSMatthias Ringwald 6802204e8f1dSMatthias Ringwald // ignore P-256 if not supported by us 6803204e8f1dSMatthias Ringwald if (hci_stack->secure_connections_active){ 68041849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 68051849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 6806204e8f1dSMatthias Ringwald } 6807204e8f1dSMatthias Ringwald 68081849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 68091849becdSMatthias Ringwald } 6810cf01e888SMatthias Ringwald /** 6811cf01e888SMatthias Ringwald * @brief Generate new OOB data 6812cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 6813cf01e888SMatthias Ringwald */ 6814cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 6815cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 6816cf01e888SMatthias Ringwald hci_run(); 6817cf01e888SMatthias Ringwald } 6818cf01e888SMatthias Ringwald 68191849becdSMatthias Ringwald #endif 68201849becdSMatthias Ringwald 6821308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_LINK_KEY_REPLY 6822308eeaffSMatthias Ringwald uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 6823308eeaffSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 6824308eeaffSMatthias Ringwald if (connection == NULL) { 6825308eeaffSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 6826308eeaffSMatthias Ringwald } 6827308eeaffSMatthias Ringwald 6828308eeaffSMatthias Ringwald memcpy(connection->link_key, link_key, sizeof(link_key_t)); 6829308eeaffSMatthias Ringwald connection->link_key_type = type; 6830308eeaffSMatthias Ringwald 6831308eeaffSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 6832308eeaffSMatthias Ringwald } 6833308eeaffSMatthias Ringwald 6834308eeaffSMatthias Ringwald #endif // ENABLE_EXPLICIT_LINK_KEY_REPLY 6835f5875de5SMatthias Ringwald /** 6836f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 6837f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 6838f6858d14SMatthias Ringwald */ 6839b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 6840b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 6841f6858d14SMatthias Ringwald } 6842f6858d14SMatthias Ringwald 6843f6858d14SMatthias Ringwald /** 6844d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 6845d950d659SMatthias Ringwald */ 6846d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 6847d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 6848d950d659SMatthias Ringwald } 6849d950d659SMatthias Ringwald 6850d950d659SMatthias Ringwald /** 6851d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 6852d950d659SMatthias Ringwald * @return current voice setting 6853d950d659SMatthias Ringwald */ 68540cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 6855d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 6856d950d659SMatthias Ringwald } 6857d950d659SMatthias Ringwald 6858400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 6859400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 6860400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 6861400ff5abSMatthias Ringwald if (!transport_name) return 0; 6862400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 6863400ff5abSMatthias Ringwald } 6864400ff5abSMatthias Ringwald 6865b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 6866b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 6867b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 6868b3aad8daSMatthias Ringwald */ 686920dcdd22SMatthias Ringwald uint16_t hci_get_sco_packet_length(void){ 687020dcdd22SMatthias Ringwald uint16_t sco_packet_length = 0; 6871cf119f3bSMatthias Ringwald 68721e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 68736f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 68741431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 6875cf119f3bSMatthias Ringwald 6876400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 6877400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 6878cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 687952e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 688052e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 6881400ff5abSMatthias Ringwald } else { 6882400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 6883400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 6884d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 6885d966a453SMatthias Ringwald if (sco_packet_length > hci_stack->sco_data_packet_length){ 6886d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 6887d966a453SMatthias Ringwald } 6888400ff5abSMatthias Ringwald } 6889a3069afeSMatthias Ringwald #endif 68905b7087c7SMatthias Ringwald 68915b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 68925b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 68931e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 68945b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 68955b7087c7SMatthias Ringwald #endif 6896a3069afeSMatthias Ringwald return sco_packet_length; 6897b3aad8daSMatthias Ringwald } 6898b3aad8daSMatthias Ringwald 6899c4c88f1bSJakob Krantz /** 6900c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 6901c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 6902c4c88f1bSJakob Krantz */ 6903c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 6904c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 6905c4c88f1bSJakob Krantz } 6906c4c88f1bSJakob Krantz 6907c4c88f1bSJakob Krantz #endif 6908ec111c8bSMatthias Ringwald 6909ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 6910ec111c8bSMatthias Ringwald return hci_stack->state; 6911ec111c8bSMatthias Ringwald } 6912ec111c8bSMatthias Ringwald 691307e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 69145e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 691507e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 691607e010b6SMilanka Ringwald } 691707e010b6SMilanka Ringwald #endif 6918ec111c8bSMatthias Ringwald 6919d950d659SMatthias Ringwald /** 6920d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 6921d23838ecSMatthias Ringwald */ 6922c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 6923d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 6924d23838ecSMatthias Ringwald } 6925d23838ecSMatthias Ringwald 692671de195eSMatthias Ringwald void hci_disconnect_all(void){ 6927665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 6928665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 6929665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 6930665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 693104a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 693204a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 693304a6ef8cSmatthias.ringwald } 6934d31fba26S[email protected] hci_run(); 693504a6ef8cSmatthias.ringwald } 693633373e40SMatthias Ringwald 693733373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 693833373e40SMatthias Ringwald return hci_stack->manufacturer; 693933373e40SMatthias Ringwald } 69409c6e867eSMatthias Ringwald 69413e329ddfSandryblack #ifdef ENABLE_BLE 69429c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 69439c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 69449c6e867eSMatthias Ringwald if (!hci_con) return NULL; 69459c6e867eSMatthias Ringwald return &hci_con->sm_connection; 69469c6e867eSMatthias Ringwald } 69479c6e867eSMatthias Ringwald 69489c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 69499c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 69506a79f6baSMatthias Ringwald #endif 69519c6e867eSMatthias Ringwald 69526d105c71SMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 6953c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6954c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 6955c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 69566a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 6957c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 6958c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 69599c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 69609c6e867eSMatthias Ringwald } 69616a79f6baSMatthias Ringwald #endif 69626a79f6baSMatthias Ringwald } else { 6963c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 69648daf94bcSMatthias Ringwald if ((hci_connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED)){ 6965c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 6966c5fb5ca4SMatthias Ringwald } 6967c5fb5ca4SMatthias Ringwald #endif 69686a79f6baSMatthias Ringwald } 6969c5fb5ca4SMatthias Ringwald return 0; 6970c5fb5ca4SMatthias Ringwald } 69719c6e867eSMatthias Ringwald 6972ff3f1026SMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 6973c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6974ff3f1026SMatthias Ringwald if (hci_connection == NULL) return false; 69755f3981bfSMatthias Ringwald 6976c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 69776a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 69785f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 69795f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 6980c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 6981ff3f1026SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated != 0; 69826a79f6baSMatthias Ringwald #endif 698359a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 69845f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 6985f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6986ff3f1026SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 698759a1a47aSMatthias Ringwald #endif 69885f3981bfSMatthias Ringwald default: 6989ff3f1026SMatthias Ringwald return false; 69905f3981bfSMatthias Ringwald } 69919c6e867eSMatthias Ringwald } 69929c6e867eSMatthias Ringwald 6993f461b3dfSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 6994c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 6995c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 69968b35e16aSMatthias Ringwald 6997c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 69986a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 69998b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 70008b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 7001f461b3dfSMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return false; // unencrypted connection cannot be authenticated 7002f461b3dfSMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc != 0; 70036a79f6baSMatthias Ringwald #endif 700459a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 70058b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 7006f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 7007f461b3dfSMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 700859a1a47aSMatthias Ringwald #endif 70098b35e16aSMatthias Ringwald default: 7010f461b3dfSMatthias Ringwald return false; 70118b35e16aSMatthias Ringwald } 7012f1dfbe18SMatthias Ringwald } 7013f1dfbe18SMatthias Ringwald 70141e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 70151e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 70161e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 70171e122704SMatthias Ringwald 701848f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 70191e122704SMatthias Ringwald link_key_t link_key; 70201e122704SMatthias Ringwald link_key_type_t link_key_type; 702148f33f37SMatthias Ringwald #endif 70221e122704SMatthias Ringwald switch (hci_connection->address_type){ 70236a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 70241e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 70251e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 70261e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 70276a79f6baSMatthias Ringwald #endif 70281e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 70291e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 70301e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 70311e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 70321e122704SMatthias Ringwald #endif 70331e122704SMatthias Ringwald default: 70341e122704SMatthias Ringwald return false; 70351e122704SMatthias Ringwald } 70361e122704SMatthias Ringwald } 70371e122704SMatthias Ringwald 70386a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 70399c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 70409c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 70419c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 70429c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 70439c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 70449c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 70459c6e867eSMatthias Ringwald } 70469c6e867eSMatthias Ringwald #endif 7047f8ee3071SMatthias Ringwald 7048f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 7049f8ee3071SMatthias 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){ 7050f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7051f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7052f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 7053f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 7054f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 7055f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 7056f8ee3071SMatthias Ringwald hci_run(); 7057f8ee3071SMatthias Ringwald return 0; 7058f8ee3071SMatthias Ringwald } 7059f8ee3071SMatthias Ringwald 7060f8ee3071SMatthias Ringwald /** 7061f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 7062f8ee3071SMatthias Ringwald * @param con_handle 7063f8ee3071SMatthias Ringwald @ @return 0 if ok 7064f8ee3071SMatthias Ringwald */ 7065f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 7066f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7067f8ee3071SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7068f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 7069f8ee3071SMatthias Ringwald hci_run(); 7070f8ee3071SMatthias Ringwald return 0; 7071f8ee3071SMatthias Ringwald } 7072bea424a5SMatthias Ringwald 7073140c0557SMatthias 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){ 7074140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7075140c0557SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7076140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 7077140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 7078140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 7079140c0557SMatthias Ringwald hci_run(); 7080dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 7081140c0557SMatthias Ringwald } 7082140c0557SMatthias Ringwald 7083278ff8a9SMatthias 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){ 7084278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7085278ff8a9SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7086278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 7087278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 7088278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 7089278ff8a9SMatthias Ringwald conn->qos_latency = latency; 7090278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 7091278ff8a9SMatthias Ringwald hci_run(); 7092278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 7093278ff8a9SMatthias Ringwald } 7094278ff8a9SMatthias Ringwald 7095bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 7096bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 7097bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 7098baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 7099bea424a5SMatthias Ringwald hci_run(); 7100bea424a5SMatthias Ringwald } 7101bea424a5SMatthias Ringwald 7102bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 7103bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 7104baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_TYPE; 7105bea424a5SMatthias Ringwald hci_run(); 7106bea424a5SMatthias Ringwald } 7107bea424a5SMatthias Ringwald 710832a12730SMatthias Ringwald void gap_set_page_timeout(uint16_t page_timeout){ 710932a12730SMatthias Ringwald hci_stack->page_timeout = page_timeout; 7110baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_TIMEOUT; 711132a12730SMatthias Ringwald hci_run(); 711232a12730SMatthias Ringwald } 711332a12730SMatthias Ringwald 7114f8ee3071SMatthias Ringwald #endif 7115beceeddeSMatthias Ringwald 7116beceeddeSMatthias Ringwald void hci_halting_defer(void){ 7117beceeddeSMatthias Ringwald if (hci_stack->state != HCI_STATE_HALTING) return; 7118beceeddeSMatthias Ringwald switch (hci_stack->substate){ 7119beceeddeSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL_NO_TIMER: 7120beceeddeSMatthias Ringwald case HCI_HALTING_CLOSE: 7121beceeddeSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL_TIMER; 7122beceeddeSMatthias Ringwald break; 7123beceeddeSMatthias Ringwald default: 7124beceeddeSMatthias Ringwald break; 7125beceeddeSMatthias Ringwald } 7126beceeddeSMatthias Ringwald } 7127eddac615SMatthias Ringwald 712821debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 712921debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 713021debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 713121debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 713221debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 713321debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 713402b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 713521debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 713602b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 713702b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 713802b02cffSMatthias Ringwald } 713902b02cffSMatthias Ringwald } 714002b02cffSMatthias Ringwald 714102b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 714202b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 714302b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 714402b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 714502b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 714602b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 714702b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 714802b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 714921debf25SMatthias Ringwald } 715021debf25SMatthias Ringwald } 7151cf38a091SMatthias Ringwald 7152cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 71531ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE) == false){ 7154cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 7155cf38a091SMatthias Ringwald } 7156cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 7157cf38a091SMatthias Ringwald // restart le resolving list update 7158cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 7159cf38a091SMatthias Ringwald } 7160cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 7161cf38a091SMatthias Ringwald } 716221debf25SMatthias Ringwald #endif 716321debf25SMatthias Ringwald 7164eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 716518976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 716618976c74SMatthias Ringwald hci_connection_t * conn; 716718976c74SMatthias Ringwald 716818976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 716918976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 717018976c74SMatthias Ringwald 71717d33cb26SMilanka Ringwald // setup Controller info 71727d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 71737d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 71747d33cb26SMilanka Ringwald 717518976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 717618976c74SMatthias Ringwald addr[5] = 0x01; 717718976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 717818976c74SMatthias Ringwald conn->con_handle = addr[5]; 717918976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 718018976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 71817d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 718218976c74SMatthias Ringwald 718318976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 718418976c74SMatthias Ringwald addr[5] = 0x02; 718518976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 718618976c74SMatthias Ringwald conn->con_handle = addr[5]; 718718976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 718818976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 71897d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 719018976c74SMatthias Ringwald 719118976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 719218976c74SMatthias Ringwald addr[5] = 0x03; 719318976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 719418976c74SMatthias Ringwald conn->con_handle = addr[5]; 719518976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 719618976c74SMatthias Ringwald conn->state = OPEN; 71977d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 719818976c74SMatthias Ringwald 719918976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 720018976c74SMatthias Ringwald addr[5] = 0x04; 720118976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 720218976c74SMatthias Ringwald conn->con_handle = addr[5]; 720318976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 720418976c74SMatthias Ringwald conn->state = OPEN; 72057d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 720618976c74SMatthias Ringwald 720718976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 720818976c74SMatthias Ringwald addr[5] = 0x05; 720918976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 721018976c74SMatthias Ringwald conn->con_handle = addr[5]; 721118976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 721218976c74SMatthias Ringwald conn->state = OPEN; 72137d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 72148046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 721518976c74SMatthias Ringwald } 721618976c74SMatthias Ringwald 7217eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 7218eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 7219eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 7220eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7221eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 7222eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 7223eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 7224eddac615SMatthias Ringwald } 7225eddac615SMatthias Ringwald } 72261470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 7227b6ffb67fSMatthias Ringwald hci_stack->le_scanning_param_update = false; 72281470db0cSMatthias Ringwald hci_init_done(); 72291470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 72301470db0cSMatthias Ringwald } 7231eddac615SMatthias Ringwald #endif 7232