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 778ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 788ad9cf99SMatthias Ringwald #include <stdio.h> // sprintf 798ad9cf99SMatthias Ringwald #endif 808ad9cf99SMatthias Ringwald 812b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 822b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_NUM 832b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_NUM" 842b838201SMatthias Ringwald #endif 852b838201SMatthias Ringwald #ifndef HCI_HOST_ACL_PACKET_LEN 862b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_ACL_PACKET_LEN" 872b838201SMatthias Ringwald #endif 882b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_NUM 892b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_NUM" 902b838201SMatthias Ringwald #endif 912b838201SMatthias Ringwald #ifndef HCI_HOST_SCO_PACKET_LEN 922b838201SMatthias Ringwald #error "ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL requires to define HCI_HOST_SCO_PACKET_LEN" 932b838201SMatthias Ringwald #endif 942b838201SMatthias Ringwald #endif 951b0e3922Smatthias.ringwald 96aa81e641SMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) && defined(ENABLE_SCO_OVER_PCM) 97aa81e641SMatthias Ringwald #error "SCO data can either be routed over HCI or over PCM, but not over both. Please only enable ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM." 98aa81e641SMatthias Ringwald #endif 99aa81e641SMatthias Ringwald 1001e20a53eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) && defined(HAVE_SCO_TRANSPORT) 1011e20a53eSMatthias Ringwald #error "SCO data can either be routed over HCI or over PCM, but not over both. Please only enable ENABLE_SCO_OVER_HCI or HAVE_SCO_TRANSPORT." 1021e20a53eSMatthias Ringwald #endif 1031e20a53eSMatthias Ringwald 104169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 10562473419SMatthias Ringwald 10662473419SMatthias Ringwald #ifndef HCI_RESET_RESEND_TIMEOUT_MS 107659d758cSMatthias Ringwald #define HCI_RESET_RESEND_TIMEOUT_MS 200 10862473419SMatthias Ringwald #endif 109ee091cf1Smatthias.ringwald 1101cfb383eSMatthias Ringwald // Names are arbitrarily shortened to 32 bytes if not requested otherwise 1111cfb383eSMatthias Ringwald #ifndef GAP_INQUIRY_MAX_NAME_LEN 1121cfb383eSMatthias Ringwald #define GAP_INQUIRY_MAX_NAME_LEN 32 1131cfb383eSMatthias Ringwald #endif 1141cfb383eSMatthias Ringwald 115f5875de5SMatthias Ringwald // GAP inquiry state: 0 = off, 0x01 - 0x30 = requested duration, 0xfe = active, 0xff = stop requested 116f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MIN 0x01 117f5875de5SMatthias Ringwald #define GAP_INQUIRY_DURATION_MAX 0x30 11830199e24SMatthias Ringwald #define GAP_INQUIRY_MIN_PERIODIC_LEN_MIN 0x02 11930199e24SMatthias Ringwald #define GAP_INQUIRY_MAX_PERIODIC_LEN_MIN 0x03 120beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_IDLE 0x00 121beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W4_ACTIVE 0x80 122beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_ACTIVE 0x81 123beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W2_CANCEL 0x82 124beb3c81dSMatthias Ringwald #define GAP_INQUIRY_STATE_W4_CANCELLED 0x83 12530199e24SMatthias Ringwald #define GAP_INQUIRY_STATE_PERIODIC 0x84 12630199e24SMatthias Ringwald #define GAP_INQUIRY_STATE_W2_EXIT_PERIODIC 0x85 127f5875de5SMatthias Ringwald 128b7f1ee76SMatthias Ringwald // GAP Remote Name Request 129b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_IDLE 0 130b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W2_SEND 1 131b7f1ee76SMatthias Ringwald #define GAP_REMOTE_NAME_STATE_W4_COMPLETE 2 132b7f1ee76SMatthias Ringwald 1330a51f88bSMatthias Ringwald // GAP Pairing 1340a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_IDLE 0 1350a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN 1 1360a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PIN_NEGATIVE 2 1370a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY 3 1380a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE 4 1390a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION 5 1400a51f88bSMatthias Ringwald #define GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE 6 141cc15bb2cSMatthias Ringwald #define GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE 7 1420a51f88bSMatthias Ringwald 14325e46151SMatthias Ringwald // 1449ce37759SMatthias Ringwald // compact storage of relevant supported HCI Commands. 1459ce37759SMatthias Ringwald // X-Macro below provides enumeration and mapping table into the supported 14625e46151SMatthias Ringwald // commands bitmap (64 bytes) from HCI Read Local Supported Commands 14725e46151SMatthias Ringwald // 14825e46151SMatthias Ringwald 14925e46151SMatthias Ringwald // format: command name, byte offset, bit nr in 64-byte supported commands 1508c021356SMatthias Ringwald // currently stored in 32-bit variable 15125e46151SMatthias Ringwald #define SUPPORTED_HCI_COMMANDS \ 1529ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES , 2, 5) \ 15325e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE , 10, 4) \ 1549ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE , 14, 7) \ 15525e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING, 18, 3) \ 1569ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE , 20, 4) \ 157ff7d1758SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_SET_EVENT_MASK_PAGE_2 , 22, 2) \ 1589ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED , 24, 6) \ 1599ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY, 32, 1) \ 1609ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST , 32, 3) \ 1619ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND , 32, 6) \ 16225e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH, 34, 0) \ 16325e46151SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE , 35, 1) \ 164a391128dSMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH , 35, 3) \ 1659ce37759SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_SET_DEFAULT_PHY , 35, 5) \ 16663671e69SMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE , 36, 6) \ 16779b7ea2dSMatthias Ringwald X( SUPPORTED_HCI_COMMAND_LE_READ_BUFFER_SIZE_V2 , 41, 5) \ 168a391128dSMatthias Ringwald X( SUPPORTED_HCI_COMMAND_SET_MIN_ENCRYPTION_KEY_SIZE , 45, 7) \ 16925e46151SMatthias Ringwald 17025e46151SMatthias Ringwald // enumerate supported commands 17125e46151SMatthias Ringwald #define X(name, offset, bit) name, 17225e46151SMatthias Ringwald enum { 17325e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 17425e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS_COUNT 17525e46151SMatthias Ringwald }; 17625e46151SMatthias Ringwald #undef X 17725e46151SMatthias Ringwald 178b83d5eabSMatthias Ringwald // prototypes 17935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 180758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 18171fd255dSMatthias Ringwald static void hci_emit_scan_mode_changed(uint8_t discoverable, uint8_t connectable); 18235454696SMatthias Ringwald static int hci_local_ssp_activated(void); 18320dcdd22SMatthias Ringwald static bool hci_remote_ssp_supported(hci_con_handle_t con_handle); 18467aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection); 18535454696SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void); 18635454696SMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status); 187a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 18835454696SMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level); 189ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer); 19096a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 19152db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn); 1929784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size); 19352db98b2SMatthias Ringwald #endif 1941cfb383eSMatthias Ringwald 1957586ee35S[email protected] static int hci_power_control_on(void); 1967586ee35S[email protected] static void hci_power_control_off(void); 1976da48142SSean Wilson static void hci_state_reset(void); 198685ec254SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds); 199fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void); 200fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason); 201b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void); 202b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void); 203b83d5eabSMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status); 204b83d5eabSMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump); 205b83d5eabSMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size); 20695d71764SMatthias Ringwald static void hci_run(void); 20795d71764SMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection); 2086a5ffed8SMatthias Ringwald 2096a5ffed8SMatthias Ringwald #ifdef ENABLE_CLASSIC 210f234b250SMatthias Ringwald static int hci_have_usb_transport(void); 211dbe0d85cSMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection); 2126a5ffed8SMatthias Ringwald #endif 2135d509858SMatthias Ringwald 214a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 215314aa2ccSMatthias Ringwald static void hci_whitelist_free(void); 216e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 21739677e66SMatthias Ringwald // called from test/ble_client/advertising_data_parser.c 218384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size); 219667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address); 2209c77c9dbSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void); 221c42da3bcSMatthias Ringwald static void hci_le_scan_stop(void); 22273799937SMatthias Ringwald static bool hci_run_general_gap_le(void); 2235d509858SMatthias Ringwald #endif 22425df6c61SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 225ee54aff5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 226f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_free(void); 22725df6c61SMatthias Ringwald static le_advertising_set_t * hci_advertising_set_for_handle(uint8_t advertising_handle); 228ee54aff5SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 229ee54aff5SMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 2304ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 2318d72db10SMatthias Ringwald static uint8_t hci_iso_stream_create(hci_iso_type_t iso_type, hci_con_handle_t con_handle, uint8_t group_id); 2324ae8623eSMatthias Ringwald static void hci_iso_stream_finalize(hci_iso_stream_t * iso_stream); 233*778463d0SMatthias Ringwald static void hci_iso_stream_finalize_by_type_and_group_id(hci_iso_type_t iso_type, uint8_t group_id); 23415a15be3SMatthias Ringwald static hci_iso_stream_t * hci_iso_stream_for_con_handle(hci_con_handle_t con_handle); 23515a15be3SMatthias Ringwald static void hci_iso_stream_requested_finalize(uint8_t big_handle); 23615a15be3SMatthias Ringwald static void hci_iso_stream_requested_confirm(uint8_t big_handle); 237d39e4f95SMatthias Ringwald static void hci_iso_packet_handler(uint8_t * packet, uint16_t size); 2387aa35f6aSMatthias Ringwald static le_audio_big_t * hci_big_for_handle(uint8_t big_handle); 2395ade6657SMatthias Ringwald static void hci_iso_notify_can_send_now(void); 2407aa35f6aSMatthias Ringwald static void hci_emit_big_created(const le_audio_big_t * big, uint8_t status); 2417aa35f6aSMatthias Ringwald static void hci_emit_big_terminated(const le_audio_big_t * big); 2422a6c0f82SMatthias Ringwald static void hci_emit_big_sync_created(const le_audio_big_sync_t * big_sync, uint8_t status); 243d04a9afcSMatthias Ringwald static void hci_emit_big_sync_stopped(uint8_t big_handle); 2442a6c0f82SMatthias Ringwald static le_audio_big_sync_t * hci_big_sync_for_handle(uint8_t big_handle); 2454ae8623eSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 246ee54aff5SMatthias Ringwald #endif /* ENABLE_BLE */ 247758b46ceSmatthias.ringwald 24806b35ec0Smatthias.ringwald // the STACK is here 2493a9fb326S[email protected] #ifndef HAVE_MALLOC 2503a9fb326S[email protected] static hci_stack_t hci_stack_static; 2513a9fb326S[email protected] #endif 2523a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 25316833f0aSmatthias.ringwald 2541c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 25563168530SMatthias Ringwald // default name 25663168530SMatthias Ringwald static const char * default_classic_name = "BTstack 00:00:00:00:00:00"; 25763168530SMatthias Ringwald 25866fb9560S[email protected] // test helper 25966fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 2601c9e5e9dSMatthias Ringwald #endif 26166fb9560S[email protected] 262aee5d5c1SMatthias Ringwald // reset connection state on create and on reconnect 263aee5d5c1SMatthias Ringwald // don't overwrite addr, con handle, role 264aee5d5c1SMatthias Ringwald static void hci_connection_init(hci_connection_t * conn){ 2658daf94bcSMatthias Ringwald conn->authentication_flags = AUTH_FLAG_NONE; 26696a45072S[email protected] conn->bonding_flags = 0; 26796a45072S[email protected] conn->requested_security_level = LEVEL_0; 26852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 26988a03c8dSMatthias Ringwald conn->request_role = HCI_ROLE_INVALID; 270140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = 0xffff; 271965a4ccfSMatthias Ringwald conn->qos_service_type = HCI_SERVICE_TYPE_INVALID; 272e9f98c4aSMatthias Ringwald conn->link_key_type = INVALID_LINK_KEY; 27391a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 27491a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 27596a45072S[email protected] hci_connection_timestamp(conn); 27652db98b2SMatthias Ringwald #endif 27796a45072S[email protected] conn->acl_recombination_length = 0; 27896a45072S[email protected] conn->acl_recombination_pos = 0; 279ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 280760b20efSMatthias Ringwald 281da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 282b90f6e0aSMatthias Ringwald #ifdef ENABLE_BLE 283b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = 0xff; 284b90f6e0aSMatthias Ringwald #endif 2850f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 2860f3b27c5SMatthias Ringwald conn->le_max_tx_octets = 27; 2870f3b27c5SMatthias Ringwald #endif 288cb439cf1SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 289cb439cf1SMatthias Ringwald conn->classic_oob_c_192 = NULL; 290cb439cf1SMatthias Ringwald conn->classic_oob_r_192 = NULL; 291cb439cf1SMatthias Ringwald conn->classic_oob_c_256 = NULL; 292cb439cf1SMatthias Ringwald conn->classic_oob_r_256 = NULL; 293cb439cf1SMatthias Ringwald #endif 294aee5d5c1SMatthias Ringwald } 295aee5d5c1SMatthias Ringwald 296aee5d5c1SMatthias Ringwald /** 297aee5d5c1SMatthias Ringwald * create connection for given address 298aee5d5c1SMatthias Ringwald * 299aee5d5c1SMatthias Ringwald * @return connection OR NULL, if no memory left 300aee5d5c1SMatthias Ringwald */ 301aee5d5c1SMatthias Ringwald static hci_connection_t * create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 302aee5d5c1SMatthias Ringwald log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 303aee5d5c1SMatthias Ringwald 304aee5d5c1SMatthias Ringwald hci_connection_t * conn = btstack_memory_hci_connection_get(); 305aee5d5c1SMatthias Ringwald if (!conn) return NULL; 306aee5d5c1SMatthias Ringwald hci_connection_init(conn); 307aee5d5c1SMatthias Ringwald 308aee5d5c1SMatthias Ringwald bd_addr_copy(conn->address, addr); 309aee5d5c1SMatthias Ringwald conn->address_type = addr_type; 310aee5d5c1SMatthias Ringwald conn->con_handle = HCI_CON_HANDLE_INVALID; 311aee5d5c1SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 312aee5d5c1SMatthias Ringwald 313665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 314aee5d5c1SMatthias Ringwald 31596a45072S[email protected] return conn; 31696a45072S[email protected] } 31766fb9560S[email protected] 318da886c03S[email protected] 319da886c03S[email protected] /** 320da886c03S[email protected] * get le connection parameter range 321da886c03S[email protected] * 322da886c03S[email protected] * @return le connection parameter range struct 323da886c03S[email protected] */ 3244ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 3254ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 326da886c03S[email protected] } 327da886c03S[email protected] 328da886c03S[email protected] /** 329da886c03S[email protected] * set le connection parameter range 330da886c03S[email protected] * 331da886c03S[email protected] */ 332da886c03S[email protected] 3334ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 3344ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 335da886c03S[email protected] } 336da886c03S[email protected] 337da886c03S[email protected] /** 33873cd8a2aSMatthias Ringwald * @brief Test if connection parameters are inside in existing rage 33973cd8a2aSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 34073cd8a2aSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 34173cd8a2aSMatthias Ringwald * @param conn_latency 34273cd8a2aSMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3436b65794dSMilanka Ringwald * @return 1 if included 34473cd8a2aSMatthias Ringwald */ 34573cd8a2aSMatthias 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){ 34673cd8a2aSMatthias Ringwald if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0; 34773cd8a2aSMatthias Ringwald if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0; 34873cd8a2aSMatthias Ringwald 34973cd8a2aSMatthias Ringwald if (le_conn_latency < existing_range->le_conn_latency_min) return 0; 35073cd8a2aSMatthias Ringwald if (le_conn_latency > existing_range->le_conn_latency_max) return 0; 35173cd8a2aSMatthias Ringwald 35273cd8a2aSMatthias Ringwald if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0; 35373cd8a2aSMatthias Ringwald if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0; 35473cd8a2aSMatthias Ringwald 35573cd8a2aSMatthias Ringwald return 1; 35673cd8a2aSMatthias Ringwald } 35773cd8a2aSMatthias Ringwald 35873cd8a2aSMatthias Ringwald /** 3592b6ab3e6SMatthias Ringwald * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 3602b6ab3e6SMatthias Ringwald * @note: default: 1 3612b6ab3e6SMatthias Ringwald * @param max_peripheral_connections 3622b6ab3e6SMatthias Ringwald */ 363d4e4907bSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3642b6ab3e6SMatthias Ringwald void gap_set_max_number_peripheral_connections(int max_peripheral_connections){ 3652b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = max_peripheral_connections; 3662b6ab3e6SMatthias Ringwald } 367d4e4907bSMatthias Ringwald #endif 3682b6ab3e6SMatthias Ringwald 3692b6ab3e6SMatthias Ringwald /** 370da886c03S[email protected] * get hci connections iterator 371da886c03S[email protected] * 372da886c03S[email protected] * @return hci connections iterator 373da886c03S[email protected] */ 374da886c03S[email protected] 375665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 376665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 377da886c03S[email protected] } 378da886c03S[email protected] 37997addcc5Smatthias.ringwald /** 380ee091cf1Smatthias.ringwald * get connection for a given handle 381ee091cf1Smatthias.ringwald * 382ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 383ee091cf1Smatthias.ringwald */ 3845061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 385665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 386665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 387665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 388665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 3893ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 390da886c03S[email protected] return item; 391ee091cf1Smatthias.ringwald } 392ee091cf1Smatthias.ringwald } 393ee091cf1Smatthias.ringwald return NULL; 394ee091cf1Smatthias.ringwald } 395ee091cf1Smatthias.ringwald 39696a45072S[email protected] /** 39796a45072S[email protected] * get connection for given address 39896a45072S[email protected] * 39996a45072S[email protected] * @return connection OR NULL, if not found 40096a45072S[email protected] */ 401667ba9d1SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 402665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 403665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 404665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 405665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 40696a45072S[email protected] if (connection->address_type != addr_type) continue; 40796a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 40862bda3fbS[email protected] return connection; 40962bda3fbS[email protected] } 41062bda3fbS[email protected] return NULL; 41162bda3fbS[email protected] } 41262bda3fbS[email protected] 413d5f71a7eSMatthias Ringwald #ifdef ENABLE_CLASSIC 414d5f71a7eSMatthias Ringwald 4153e5e0926SMatthias Ringwald inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 4163e5e0926SMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 4173e5e0926SMatthias Ringwald } 41852db98b2SMatthias Ringwald 419228e430cSMatthias Ringwald inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 420228e430cSMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 421228e430cSMatthias Ringwald } 422228e430cSMatthias Ringwald 423ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 424ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 425ee752bb8SMatthias Ringwald int connections = 0; 426ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 427ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 428ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 429ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 430ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 431ee752bb8SMatthias Ringwald connections++; 432ee752bb8SMatthias Ringwald } 433ee752bb8SMatthias Ringwald return connections; 434ee752bb8SMatthias Ringwald } 435ee752bb8SMatthias Ringwald #endif 436ee752bb8SMatthias Ringwald 437ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 43891a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 439aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 440528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 441c785ef68Smatthias.ringwald // connections might be timed out 442c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 443c785ef68Smatthias.ringwald } 444f316a845SMatthias Ringwald #else 445c1ab6cc1SMatthias Ringwald if (btstack_run_loop_get_time_ms() > (connection->timestamp + HCI_CONNECTION_TIMEOUT_MS)){ 4465f26aadcSMatthias Ringwald // connections might be timed out 4475f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 4485f26aadcSMatthias Ringwald } 4495f26aadcSMatthias Ringwald #endif 450c785ef68Smatthias.ringwald } 451ee091cf1Smatthias.ringwald 452ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 453aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 454528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 455f316a845SMatthias Ringwald #else 456528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 4575f26aadcSMatthias Ringwald #endif 458ee091cf1Smatthias.ringwald } 459ee091cf1Smatthias.ringwald 46043bfb1bdSmatthias.ringwald /** 46180ca58a0Smatthias.ringwald * add authentication flags and reset timer 46296a45072S[email protected] * @note: assumes classic connection 4632e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 4647fde4af9Smatthias.ringwald */ 4657fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 4667fde4af9Smatthias.ringwald bd_addr_t addr; 467724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 468f16129ceSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 4697fde4af9Smatthias.ringwald if (conn) { 47028ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 47180ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 4727fde4af9Smatthias.ringwald } 4737fde4af9Smatthias.ringwald } 4747fde4af9Smatthias.ringwald 4751714cbbdSMatthias Ringwald static bool hci_pairing_active(hci_connection_t * hci_connection){ 4768daf94bcSMatthias Ringwald return (hci_connection->authentication_flags & AUTH_FLAG_PAIRING_ACTIVE_MASK) != 0; 4771714cbbdSMatthias Ringwald } 4781714cbbdSMatthias Ringwald 4791714cbbdSMatthias Ringwald static void hci_pairing_started(hci_connection_t * hci_connection, bool ssp){ 4801714cbbdSMatthias Ringwald if (hci_pairing_active(hci_connection)) return; 4811714cbbdSMatthias Ringwald if (ssp){ 4828daf94bcSMatthias Ringwald hci_connection->authentication_flags |= AUTH_FLAG_SSP_PAIRING_ACTIVE; 4831714cbbdSMatthias Ringwald } else { 4848daf94bcSMatthias Ringwald hci_connection->authentication_flags |= AUTH_FLAG_LEGACY_PAIRING_ACTIVE; 4851714cbbdSMatthias Ringwald } 4861714cbbdSMatthias Ringwald // if we are initiator, we have sent an HCI Authenticate Request 4871714cbbdSMatthias Ringwald bool initiator = (hci_connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0; 4881714cbbdSMatthias Ringwald 4895a561920SMatthias Ringwald // if we are responder, use minimal service security level as required level 4905a561920SMatthias Ringwald if (!initiator){ 491acadfdd0SMatthias 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); 4925a561920SMatthias Ringwald } 4935a561920SMatthias Ringwald 4945a561920SMatthias Ringwald log_info("pairing started, ssp %u, initiator %u, requested level %u", (int) ssp, (int) initiator, hci_connection->requested_security_level); 49577208d90SMatthias Ringwald 49677208d90SMatthias Ringwald uint8_t event[12]; 49777208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_STARTED; 49877208d90SMatthias Ringwald event[1] = 10; 499bfaf6993SMatthias Ringwald little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle); 5004159a80bSMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[4]); 50177208d90SMatthias Ringwald event[10] = (uint8_t) ssp; 50277208d90SMatthias Ringwald event[11] = (uint8_t) initiator; 50377208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5041714cbbdSMatthias Ringwald } 5051714cbbdSMatthias Ringwald 5061714cbbdSMatthias Ringwald static void hci_pairing_complete(hci_connection_t * hci_connection, uint8_t status){ 5073c439ac4SMatthias Ringwald hci_connection->requested_security_level = LEVEL_0; 5081714cbbdSMatthias Ringwald if (!hci_pairing_active(hci_connection)) return; 5098daf94bcSMatthias Ringwald hci_connection->authentication_flags &= ~AUTH_FLAG_PAIRING_ACTIVE_MASK; 5101e7371deSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 511cb439cf1SMatthias Ringwald hci_connection->classic_oob_c_192 = NULL; 512cb439cf1SMatthias Ringwald hci_connection->classic_oob_r_192 = NULL; 513cb439cf1SMatthias Ringwald hci_connection->classic_oob_c_256 = NULL; 514cb439cf1SMatthias Ringwald hci_connection->classic_oob_r_256 = NULL; 5151e7371deSMatthias Ringwald #endif 5161714cbbdSMatthias Ringwald log_info("pairing complete, status %02x", status); 51777208d90SMatthias Ringwald 5183176c896SMatthias Ringwald uint8_t event[11]; 51977208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_COMPLETE; 52077208d90SMatthias Ringwald event[1] = 9; 521bfaf6993SMatthias Ringwald little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle); 5224159a80bSMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[4]); 52377208d90SMatthias Ringwald event[10] = status; 52477208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5255999ee11SMatthias Ringwald 5265999ee11SMatthias Ringwald // emit dedicated bonding done on failure, otherwise verify that connection can be encrypted 5275999ee11SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) && ((hci_connection->bonding_flags & BONDING_DEDICATED) != 0)){ 5285999ee11SMatthias Ringwald hci_connection->bonding_flags &= ~BONDING_DEDICATED; 5295999ee11SMatthias Ringwald hci_connection->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 5305999ee11SMatthias Ringwald hci_connection->bonding_status = status; 5315999ee11SMatthias Ringwald } 5321714cbbdSMatthias Ringwald } 5331714cbbdSMatthias Ringwald 53420dcdd22SMatthias Ringwald bool hci_authentication_active_for_handle(hci_con_handle_t handle){ 5355061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 53620dcdd22SMatthias Ringwald if (!conn) return false; 53720dcdd22SMatthias Ringwald return hci_pairing_active(conn); 53880ca58a0Smatthias.ringwald } 53980ca58a0Smatthias.ringwald 54015a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 54155597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 5422bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 543a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 544c12e46e7Smatthias.ringwald } 54555597469SMatthias Ringwald 54655597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 54755597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 5482bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 54955597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 550c12e46e7Smatthias.ringwald } 5511b6fb31bSMatthias Ringwald 552e8ad470fSMatthias Ringwald bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type){ 553e8ad470fSMatthias Ringwald if (!hci_stack->link_key_db) return false; 55476b0318eSMatthias Ringwald int result = hci_stack->link_key_db->get_link_key(addr, link_key, type) != 0; 55576b0318eSMatthias Ringwald log_info("link key for %s available %u, type %u", bd_addr_to_str(addr), result, (int) *type); 55676b0318eSMatthias Ringwald return result; 557e8ad470fSMatthias Ringwald } 558e8ad470fSMatthias Ringwald 559ceecb9d9SMatthias Ringwald void gap_delete_all_link_keys(void){ 560ceecb9d9SMatthias Ringwald bd_addr_t addr; 561ceecb9d9SMatthias Ringwald link_key_t link_key; 562ceecb9d9SMatthias Ringwald link_key_type_t type; 563ceecb9d9SMatthias Ringwald btstack_link_key_iterator_t it; 564ceecb9d9SMatthias Ringwald int ok = gap_link_key_iterator_init(&it); 565ceecb9d9SMatthias Ringwald if (!ok) { 566ceecb9d9SMatthias Ringwald log_error("could not initialize iterator"); 567ceecb9d9SMatthias Ringwald return; 568ceecb9d9SMatthias Ringwald } 569ceecb9d9SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 570ceecb9d9SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 571ceecb9d9SMatthias Ringwald } 572ceecb9d9SMatthias Ringwald gap_link_key_iterator_done(&it); 573ceecb9d9SMatthias Ringwald } 574ceecb9d9SMatthias Ringwald 5751b6fb31bSMatthias Ringwald int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){ 5761b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 5771b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db->iterator_init) return 0; 5781b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_init(it); 5791b6fb31bSMatthias Ringwald } 5801b6fb31bSMatthias 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){ 5811b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 5821b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type); 5831b6fb31bSMatthias Ringwald } 5841b6fb31bSMatthias Ringwald void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){ 5851b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return; 5861b6fb31bSMatthias Ringwald hci_stack->link_key_db->iterator_done(it); 5871b6fb31bSMatthias Ringwald } 58835454696SMatthias Ringwald #endif 589c12e46e7Smatthias.ringwald 590eb8076ddSMatthias Ringwald static bool hci_is_le_connection_type(bd_addr_type_t address_type){ 591eb8076ddSMatthias Ringwald switch (address_type){ 592ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 593ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 594ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_PUBLIC: 595ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_RANDOM: 596eb8076ddSMatthias Ringwald return true; 597ce41473eSMatthias Ringwald default: 598eb8076ddSMatthias Ringwald return false; 599ce41473eSMatthias Ringwald } 6000bf6344aS[email protected] } 6010bf6344aS[email protected] 602eb8076ddSMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 603eb8076ddSMatthias Ringwald return hci_is_le_connection_type(connection->address_type); 604eb8076ddSMatthias Ringwald } 605eb8076ddSMatthias Ringwald 6067fde4af9Smatthias.ringwald /** 60743bfb1bdSmatthias.ringwald * count connections 60843bfb1bdSmatthias.ringwald */ 60940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 61056c253c9Smatthias.ringwald int count = 0; 611665d90f2SMatthias Ringwald btstack_linked_item_t *it; 612a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL ; it = it->next){ 61315a27967SMatthias Ringwald count++; 61415a27967SMatthias Ringwald } 61543bfb1bdSmatthias.ringwald return count; 61643bfb1bdSmatthias.ringwald } 617c8e4258aSmatthias.ringwald 6188f733c6fSMatthias Ringwald uint16_t hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 619ee303eddS[email protected] 620f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 621f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 622ee303eddS[email protected] 623665d90f2SMatthias Ringwald btstack_linked_item_t *it; 624a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 625998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 626ce41473eSMatthias Ringwald if (hci_is_le_connection(connection)){ 627ce41473eSMatthias Ringwald num_packets_sent_le += connection->num_packets_sent; 628ce41473eSMatthias Ringwald } 629f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 630ce41473eSMatthias Ringwald num_packets_sent_classic += connection->num_packets_sent; 631ee303eddS[email protected] } 632ee303eddS[email protected] } 633d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 634ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 635ee303eddS[email protected] int free_slots_le = 0; 636ee303eddS[email protected] 637ee303eddS[email protected] if (free_slots_classic < 0){ 6389da54300S[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); 639998906cdSmatthias.ringwald return 0; 640998906cdSmatthias.ringwald } 641ee303eddS[email protected] 642ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 643ee303eddS[email protected] // if we have LE slots, they are used 644ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 645ee303eddS[email protected] if (free_slots_le < 0){ 6469da54300S[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); 647ee303eddS[email protected] return 0; 648998906cdSmatthias.ringwald } 649ee303eddS[email protected] } else { 650ee303eddS[email protected] // otherwise, classic slots are used for LE, too 651ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 652ee303eddS[email protected] if (free_slots_classic < 0){ 6539da54300S[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); 654ee303eddS[email protected] return 0; 655ee303eddS[email protected] } 656ee303eddS[email protected] } 657ee303eddS[email protected] 658ee303eddS[email protected] switch (address_type){ 659ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 6602125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 661ee303eddS[email protected] return 0; 662ee303eddS[email protected] 663f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6648f733c6fSMatthias Ringwald return (uint16_t) free_slots_classic; 665ee303eddS[email protected] 666ee303eddS[email protected] default: 6678f733c6fSMatthias Ringwald if (hci_stack->le_acl_packets_total_num > 0){ 6688f733c6fSMatthias Ringwald return (uint16_t) free_slots_le; 669ee303eddS[email protected] } 6708f733c6fSMatthias Ringwald return (uint16_t) free_slots_classic; 671cb00d3aaS[email protected] } 672998906cdSmatthias.ringwald } 673998906cdSmatthias.ringwald 6742125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 6752125de09SMatthias Ringwald // get connection type 6762125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 6772125de09SMatthias Ringwald if (!connection){ 6782125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 6792125de09SMatthias Ringwald return 0; 6802125de09SMatthias Ringwald } 6812125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 6822125de09SMatthias Ringwald } 6832125de09SMatthias Ringwald 68435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 685701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 686f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 687665d90f2SMatthias Ringwald btstack_linked_item_t *it; 688760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 689760b20efSMatthias Ringwald // explicit flow control 690665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 691e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 692ce41473eSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 693ce41473eSMatthias Ringwald num_sco_packets_sent += connection->num_packets_sent; 694e35edcc1S[email protected] } 695e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 696701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 69744d0e3d5S[email protected] return 0; 69844d0e3d5S[email protected] } 699e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 70049205f5dSMatthias Ringwald } else { 70149205f5dSMatthias Ringwald // implicit flow control -- TODO 7026f28d2eeSMatthias Ringwald int num_ready = 0; 7036f28d2eeSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 7046f28d2eeSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 7056f28d2eeSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 7066f28d2eeSMatthias Ringwald if (connection->sco_tx_ready == 0) continue; 7076f28d2eeSMatthias Ringwald num_ready++; 70849205f5dSMatthias Ringwald } 7096f28d2eeSMatthias Ringwald return num_ready; 7106f28d2eeSMatthias Ringwald } 711e35edcc1S[email protected] } 71235454696SMatthias Ringwald #endif 71344d0e3d5S[email protected] 7142b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 7152b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 716ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 717ac928cc2S[email protected] 718ac928cc2S[email protected] // check for async hci transport implementations 719ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 720ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 721ac928cc2S[email protected] return 0; 722ac928cc2S[email protected] } 723ac928cc2S[email protected] } 7242b838201SMatthias Ringwald return 1; 7252b838201SMatthias Ringwald } 726ac928cc2S[email protected] 7272b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 7281972f31fSMatthias Ringwald bool hci_can_send_command_packet_now(void){ 7291972f31fSMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return false; 7304ea43905SMatthias Ringwald return hci_stack->num_cmd_packets > 0u; 731ac928cc2S[email protected] } 732ac928cc2S[email protected] 7339d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 734ac928cc2S[email protected] // check for async hci transport implementations 7351972f31fSMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return true; 7369d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 737ac928cc2S[email protected] } 7389d04d3a7SMatthias Ringwald 7391972f31fSMatthias Ringwald static bool hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 7401972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false; 7419d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 742ac928cc2S[email protected] } 7439d04d3a7SMatthias Ringwald 7441972f31fSMatthias Ringwald bool hci_can_send_acl_le_packet_now(void){ 7451972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 7469d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 7479d04d3a7SMatthias Ringwald } 7489d04d3a7SMatthias Ringwald 7491972f31fSMatthias Ringwald bool hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 7501972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false; 751e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 752ac928cc2S[email protected] } 753ac928cc2S[email protected] 7541972f31fSMatthias Ringwald bool hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 7551972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 756ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 7576b4af23dS[email protected] } 7586b4af23dS[email protected] 75935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 7601972f31fSMatthias Ringwald bool hci_can_send_acl_classic_packet_now(void){ 7611972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 762f16129ceSMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_ACL); 76335454696SMatthias Ringwald } 76435454696SMatthias Ringwald 7651972f31fSMatthias Ringwald bool hci_can_send_prepared_sco_packet_now(void){ 7661972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return false; 767f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 768f234b250SMatthias Ringwald return hci_stack->sco_can_send_now; 769f234b250SMatthias Ringwald } else { 770701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 77144d0e3d5S[email protected] } 772f234b250SMatthias Ringwald } 77344d0e3d5S[email protected] 7741972f31fSMatthias Ringwald bool hci_can_send_sco_packet_now(void){ 7751972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 776701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 77744d0e3d5S[email protected] } 77844d0e3d5S[email protected] 779d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 780d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 781d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 782d057580eSMatthias Ringwald } 78335454696SMatthias Ringwald #endif 784d057580eSMatthias Ringwald 78595d71764SMatthias Ringwald // used for internal checks in l2cap.c 78602c7fc01SMatthias Ringwald bool hci_is_packet_buffer_reserved(void){ 787c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 788c8b9416aS[email protected] } 789c8b9416aS[email protected] 7906b65794dSMilanka Ringwald // reserves outgoing packet buffer. 7916b65794dSMilanka Ringwald // @return 1 if successful 792cafc12e8SMatthias Ringwald bool hci_reserve_packet_buffer(void){ 7939d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 7949d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 795cafc12e8SMatthias Ringwald return false; 7969d14b626S[email protected] } 79702c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = true; 798cafc12e8SMatthias Ringwald return true; 7996b4af23dS[email protected] } 8006b4af23dS[email protected] 80168a0fcf7S[email protected] void hci_release_packet_buffer(void){ 80202c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 80368a0fcf7S[email protected] } 80468a0fcf7S[email protected] 8056b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 8067f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 8076b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 8086b4af23dS[email protected] } 8096b4af23dS[email protected] 8108ad9cf99SMatthias Ringwald // used for debugging 8118ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 8128ad9cf99SMatthias Ringwald static void hci_controller_dump_packets(void){ 8138ad9cf99SMatthias Ringwald // format: "{handle:04x}:{count:02d} " 8148ad9cf99SMatthias Ringwald char summaries[3][7 * 8 + 1]; 8158ad9cf99SMatthias Ringwald uint16_t totals[3]; 8168ad9cf99SMatthias Ringwald uint8_t index; 8178ad9cf99SMatthias Ringwald for (index = 0 ; index < 3 ; index++){ 8188ad9cf99SMatthias Ringwald summaries[index][0] = 0; 8198ad9cf99SMatthias Ringwald totals[index] = 0; 8208ad9cf99SMatthias Ringwald } 8218ad9cf99SMatthias Ringwald btstack_linked_item_t *it; 8228ad9cf99SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 8238ad9cf99SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 8248ad9cf99SMatthias Ringwald switch (connection->address_type){ 8258ad9cf99SMatthias Ringwald case BD_ADDR_TYPE_ACL: 8268ad9cf99SMatthias Ringwald index = 0; 8278ad9cf99SMatthias Ringwald break; 8288ad9cf99SMatthias Ringwald case BD_ADDR_TYPE_SCO: 8298ad9cf99SMatthias Ringwald index = 2; 8308ad9cf99SMatthias Ringwald break; 8318ad9cf99SMatthias Ringwald default: 8328ad9cf99SMatthias Ringwald index = 1; 833ce7ca7c8SMatthias Ringwald break; 8348ad9cf99SMatthias Ringwald } 8358ad9cf99SMatthias Ringwald totals[index] += connection->num_packets_sent; 8368ad9cf99SMatthias Ringwald char item_text[10]; 8378ad9cf99SMatthias Ringwald sprintf(item_text, "%04x:%02d ", connection->con_handle,connection->num_packets_sent); 8388ad9cf99SMatthias Ringwald btstack_strcat(summaries[index], sizeof(summaries[0]), item_text); 8398ad9cf99SMatthias Ringwald } 8408ad9cf99SMatthias Ringwald for (index = 0 ; index < 3 ; index++){ 8418ad9cf99SMatthias Ringwald if (summaries[index][0] == 0){ 8428ad9cf99SMatthias Ringwald summaries[index][0] = '-'; 8438ad9cf99SMatthias Ringwald summaries[index][1] = 0; 8448ad9cf99SMatthias Ringwald } 8458ad9cf99SMatthias Ringwald } 8468ad9cf99SMatthias Ringwald log_info("Controller ACL BR/EDR: %s total %u / LE: %s total %u / SCO: %s total %u", summaries[0], totals[0], summaries[1], totals[1], summaries[2], totals[2]); 8478ad9cf99SMatthias Ringwald } 8488ad9cf99SMatthias Ringwald #endif 8498ad9cf99SMatthias Ringwald 8503e2050f7SMatthias Ringwald static uint8_t hci_send_acl_packet_fragments(hci_connection_t *connection){ 851452cf3bbS[email protected] 852452cf3bbS[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); 853452cf3bbS[email protected] 854452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 855452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 8564ea43905SMatthias Ringwald if (hci_is_le_connection(connection) && (hci_stack->le_data_packets_length > 0u)){ 857452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 858452cf3bbS[email protected] } 859452cf3bbS[email protected] 8600f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 861fcf88f47SMatthias Ringwald if (hci_is_le_connection(connection) && (connection->le_max_tx_octets < max_acl_data_packet_length)){ 8620f3b27c5SMatthias Ringwald max_acl_data_packet_length = connection->le_max_tx_octets; 8630f3b27c5SMatthias Ringwald } 8640f3b27c5SMatthias Ringwald #endif 865452cf3bbS[email protected] 866d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 867d999b54eSMatthias Ringwald 8683e2050f7SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 869452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 870ff3cc4a5SMatthias Ringwald while (true){ 871452cf3bbS[email protected] 872d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 873d999b54eSMatthias Ringwald 874452cf3bbS[email protected] // get current data 8754ea43905SMatthias Ringwald const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u; 876452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 8771979f09cSMatthias Ringwald bool more_fragments = false; 878452cf3bbS[email protected] 879452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 880452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 8811979f09cSMatthias Ringwald more_fragments = true; 882452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 883452cf3bbS[email protected] } 884452cf3bbS[email protected] 885452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 8864ea43905SMatthias Ringwald if (acl_header_pos > 0u){ 887f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 8884ea43905SMatthias Ringwald handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u); 889f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 890452cf3bbS[email protected] } 891452cf3bbS[email protected] 892452cf3bbS[email protected] // update header len 8934ea43905SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length); 894452cf3bbS[email protected] 895452cf3bbS[email protected] // count packet 896ce41473eSMatthias Ringwald connection->num_packets_sent++; 8971979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments); 898d999b54eSMatthias Ringwald 899d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 900d999b54eSMatthias Ringwald if (more_fragments){ 901d999b54eSMatthias Ringwald // update start of next fragment to send 902d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 903d999b54eSMatthias Ringwald } else { 904d999b54eSMatthias Ringwald // done 905d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 906d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 907d999b54eSMatthias Ringwald } 908452cf3bbS[email protected] 909452cf3bbS[email protected] // send packet 910452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 911452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 9125bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 91381d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 1; 9143e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 9153e2050f7SMatthias Ringwald if (err != 0){ 9163e2050f7SMatthias Ringwald // no error from HCI Transport expected 9173e2050f7SMatthias Ringwald status = ERROR_CODE_HARDWARE_FAILURE; 9183e2050f7SMatthias Ringwald } 919452cf3bbS[email protected] 9208ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 9218ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 9228ad9cf99SMatthias Ringwald #endif 9238ad9cf99SMatthias Ringwald 9241979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments); 925d999b54eSMatthias Ringwald 926452cf3bbS[email protected] // done yet? 927452cf3bbS[email protected] if (!more_fragments) break; 928452cf3bbS[email protected] 929452cf3bbS[email protected] // can send more? 9303e2050f7SMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return status; 931452cf3bbS[email protected] } 932452cf3bbS[email protected] 933d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 934452cf3bbS[email protected] 935d051460cS[email protected] // release buffer now for synchronous transport 936203bace6S[email protected] if (hci_transport_synchronous()){ 93781d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 938452cf3bbS[email protected] hci_release_packet_buffer(); 939fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 940452cf3bbS[email protected] } 941452cf3bbS[email protected] 9423e2050f7SMatthias Ringwald return status; 943452cf3bbS[email protected] } 944452cf3bbS[email protected] 945826f7347S[email protected] // pre: caller has reserved the packet buffer 9463e2050f7SMatthias Ringwald uint8_t hci_send_acl_packet_buffer(int size){ 9473e2050f7SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 948826f7347S[email protected] 949d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 950d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 951d713a683S[email protected] 952826f7347S[email protected] // check for free places on Bluetooth module 953d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 954826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 95597b61c7bS[email protected] hci_release_packet_buffer(); 956068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 95797b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 95897b61c7bS[email protected] } 9596218e6f1Smatthias.ringwald 9605061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 96197b61c7bS[email protected] if (!connection) { 9625fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 96397b61c7bS[email protected] hci_release_packet_buffer(); 964068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 9653e2050f7SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 96697b61c7bS[email protected] } 96752db98b2SMatthias Ringwald 96852db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 96956cf178bSmatthias.ringwald hci_connection_timestamp(connection); 97052db98b2SMatthias Ringwald #endif 97156cf178bSmatthias.ringwald 972452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 9737856c818Smatthias.ringwald 974452cf3bbS[email protected] // setup data 975452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 976452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 9776218e6f1Smatthias.ringwald 978452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 979ee091cf1Smatthias.ringwald } 980ee091cf1Smatthias.ringwald 98135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 98244d0e3d5S[email protected] // pre: caller has reserved the packet buffer 9833e2050f7SMatthias Ringwald uint8_t hci_send_sco_packet_buffer(int size){ 9843e2050f7SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 98544d0e3d5S[email protected] 98644d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 9874b3e1e19SMatthias Ringwald 9884b3e1e19SMatthias Ringwald // skip checks in loopback mode 9894b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 99044d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 99144d0e3d5S[email protected] 99244d0e3d5S[email protected] // check for free places on Bluetooth module 993701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 994cbf638a9SMatthias Ringwald log_error("hci_send_sco_packet_buffer called but no free SCO buffers on controller"); 99544d0e3d5S[email protected] hci_release_packet_buffer(); 996068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 99744d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 99844d0e3d5S[email protected] } 99944d0e3d5S[email protected] 1000e35edcc1S[email protected] // track send packet in connection struct 1001e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 1002e35edcc1S[email protected] if (!connection) { 1003e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 1004e35edcc1S[email protected] hci_release_packet_buffer(); 1005068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 10063e2050f7SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1007e35edcc1S[email protected] } 1008f234b250SMatthias Ringwald 1009f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 1010f234b250SMatthias Ringwald // token used 10111972f31fSMatthias Ringwald hci_stack->sco_can_send_now = false; 1012f234b250SMatthias Ringwald } else { 1013760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 1014ce41473eSMatthias Ringwald connection->num_packets_sent++; 10156f28d2eeSMatthias Ringwald } else { 1016e4157653SMatthias Ringwald connection->sco_tx_ready--; 1017760b20efSMatthias Ringwald } 10184b3e1e19SMatthias Ringwald } 1019f234b250SMatthias Ringwald } 102044d0e3d5S[email protected] 102144d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 1022543e835cSMatthias Ringwald 102343149fc9SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 102443149fc9SMatthias Ringwald hci_stack->sco_transport->send_packet(packet, size); 102543149fc9SMatthias Ringwald hci_release_packet_buffer(); 102643149fc9SMatthias Ringwald hci_emit_transport_packet_sent(); 102743149fc9SMatthias Ringwald 102843149fc9SMatthias Ringwald return 0; 102943149fc9SMatthias Ringwald #else 103043149fc9SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 1031543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 1032543e835cSMatthias Ringwald hci_release_packet_buffer(); 1033fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 1034543e835cSMatthias Ringwald } 1035543e835cSMatthias Ringwald 10363e2050f7SMatthias Ringwald if (err != 0){ 10373e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 10383e2050f7SMatthias Ringwald } 10393e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 104043149fc9SMatthias Ringwald #endif 104144d0e3d5S[email protected] } 104235454696SMatthias Ringwald #endif 104344d0e3d5S[email protected] 10440a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 104568ab6207SMatthias Ringwald static uint8_t hci_send_iso_packet_fragments(void){ 104668ab6207SMatthias Ringwald 104768ab6207SMatthias Ringwald uint16_t max_iso_data_packet_length = hci_stack->le_iso_packets_length; 104868ab6207SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 104968ab6207SMatthias Ringwald // multiple packets could be send on a synchronous HCI transport 105068ab6207SMatthias Ringwald while (true){ 105168ab6207SMatthias Ringwald 105268ab6207SMatthias Ringwald // get current data 105368ab6207SMatthias Ringwald const uint16_t iso_header_pos = hci_stack->iso_fragmentation_pos - 4u; 105468ab6207SMatthias Ringwald int current_iso_data_packet_length = hci_stack->iso_fragmentation_total_size - hci_stack->iso_fragmentation_pos; 105568ab6207SMatthias Ringwald bool more_fragments = false; 105668ab6207SMatthias Ringwald 105768ab6207SMatthias Ringwald // if ISO packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 105868ab6207SMatthias Ringwald if (current_iso_data_packet_length > max_iso_data_packet_length){ 105968ab6207SMatthias Ringwald more_fragments = true; 106068ab6207SMatthias Ringwald current_iso_data_packet_length = max_iso_data_packet_length; 106168ab6207SMatthias Ringwald } 106268ab6207SMatthias Ringwald 106368ab6207SMatthias Ringwald // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 106468ab6207SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 106568ab6207SMatthias Ringwald uint8_t pb_flags; 106668ab6207SMatthias Ringwald if (iso_header_pos == 0u){ 106768ab6207SMatthias Ringwald // first fragment, keep TS field 106868ab6207SMatthias Ringwald pb_flags = more_fragments ? 0x00 : 0x02; 106968ab6207SMatthias Ringwald handle_and_flags = (handle_and_flags & 0x4fffu) | (pb_flags << 12u); 107068ab6207SMatthias Ringwald } else { 107168ab6207SMatthias Ringwald // later fragment, drop TS field 107268ab6207SMatthias Ringwald pb_flags = more_fragments ? 0x01 : 0x03; 107368ab6207SMatthias Ringwald handle_and_flags = (handle_and_flags & 0x0fffu) | (pb_flags << 12u); 107468ab6207SMatthias Ringwald } 107568ab6207SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, iso_header_pos, handle_and_flags); 107668ab6207SMatthias Ringwald 107768ab6207SMatthias Ringwald // update header len 107868ab6207SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, iso_header_pos + 2u, current_iso_data_packet_length); 107968ab6207SMatthias Ringwald 108068ab6207SMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 108168ab6207SMatthias Ringwald if (more_fragments){ 108268ab6207SMatthias Ringwald // update start of next fragment to send 108368ab6207SMatthias Ringwald hci_stack->iso_fragmentation_pos += current_iso_data_packet_length; 108468ab6207SMatthias Ringwald } else { 108568ab6207SMatthias Ringwald // done 108668ab6207SMatthias Ringwald hci_stack->iso_fragmentation_pos = 0; 108768ab6207SMatthias Ringwald hci_stack->iso_fragmentation_total_size = 0; 108868ab6207SMatthias Ringwald } 108968ab6207SMatthias Ringwald 109068ab6207SMatthias Ringwald // send packet 109168ab6207SMatthias Ringwald uint8_t * packet = &hci_stack->hci_packet_buffer[iso_header_pos]; 109268ab6207SMatthias Ringwald const int size = current_iso_data_packet_length + 4; 109368ab6207SMatthias Ringwald hci_dump_packet(HCI_ISO_DATA_PACKET, 0, packet, size); 109468ab6207SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = true; 109568ab6207SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_ISO_DATA_PACKET, packet, size); 109668ab6207SMatthias Ringwald if (err != 0){ 109768ab6207SMatthias Ringwald // no error from HCI Transport expected 109868ab6207SMatthias Ringwald status = ERROR_CODE_HARDWARE_FAILURE; 109968ab6207SMatthias Ringwald } 110068ab6207SMatthias Ringwald 110168ab6207SMatthias Ringwald // done yet? 110268ab6207SMatthias Ringwald if (!more_fragments) break; 110368ab6207SMatthias Ringwald 110468ab6207SMatthias Ringwald // can send more? 110568ab6207SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ISO_DATA_PACKET)) return false; 110668ab6207SMatthias Ringwald } 110768ab6207SMatthias Ringwald 110868ab6207SMatthias Ringwald // release buffer now for synchronous transport 110968ab6207SMatthias Ringwald if (hci_transport_synchronous()){ 111068ab6207SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = false; 111168ab6207SMatthias Ringwald hci_release_packet_buffer(); 111268ab6207SMatthias Ringwald hci_emit_transport_packet_sent(); 111368ab6207SMatthias Ringwald } 111468ab6207SMatthias Ringwald 111568ab6207SMatthias Ringwald return status; 111668ab6207SMatthias Ringwald } 111768ab6207SMatthias Ringwald 1118ace08712SMatthias Ringwald uint8_t hci_send_iso_packet_buffer(uint16_t size){ 1119ace08712SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 1120ace08712SMatthias Ringwald 1121e5413aa0SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) little_endian_read_16(hci_stack->hci_packet_buffer, 0) & 0xfff; 1122e5413aa0SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(con_handle); 1123e5413aa0SMatthias Ringwald if (iso_stream == NULL){ 1124977f918dSMatthias Ringwald hci_release_packet_buffer(); 1125977f918dSMatthias Ringwald hci_iso_notify_can_send_now(); 1126e5413aa0SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1127e5413aa0SMatthias Ringwald } 1128e5413aa0SMatthias Ringwald 1129e5413aa0SMatthias Ringwald // TODO: check for space on controller 1130e5413aa0SMatthias Ringwald 1131977f918dSMatthias Ringwald // skip iso packets if needed 1132977f918dSMatthias Ringwald if (iso_stream->num_packets_to_skip > 0){ 1133977f918dSMatthias Ringwald iso_stream->num_packets_to_skip--; 1134977f918dSMatthias Ringwald // pretend it was processed and trigger next one 1135977f918dSMatthias Ringwald hci_release_packet_buffer(); 1136977f918dSMatthias Ringwald hci_iso_notify_can_send_now(); 1137977f918dSMatthias Ringwald return ERROR_CODE_SUCCESS; 1138977f918dSMatthias Ringwald } 1139977f918dSMatthias Ringwald 1140e5413aa0SMatthias Ringwald // track outgoing packet sent 1141e5413aa0SMatthias Ringwald log_info("Outgoing ISO packet for con handle 0x%04x", con_handle); 1142e5413aa0SMatthias Ringwald iso_stream->num_packets_sent++; 1143e5413aa0SMatthias Ringwald 114468ab6207SMatthias Ringwald // setup data 114568ab6207SMatthias Ringwald hci_stack->iso_fragmentation_total_size = size; 114668ab6207SMatthias Ringwald hci_stack->iso_fragmentation_pos = 4; // start of L2CAP packet 114768ab6207SMatthias Ringwald 114868ab6207SMatthias Ringwald return hci_send_iso_packet_fragments(); 1149ace08712SMatthias Ringwald } 1150ace08712SMatthias Ringwald #endif 1151ace08712SMatthias Ringwald 1152c3b46f5aSMatthias Ringwald static void acl_handler(uint8_t *packet, uint16_t size){ 1153e76a89eeS[email protected] 11547856c818Smatthias.ringwald // get info 11557856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 11565061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 11577856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 11587856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 11597856c818Smatthias.ringwald 11607856c818Smatthias.ringwald // ignore non-registered handle 11617856c818Smatthias.ringwald if (!conn){ 1162c3b46f5aSMatthias Ringwald log_error("acl_handler called with non-registered handle %u!" , con_handle); 11637856c818Smatthias.ringwald return; 11647856c818Smatthias.ringwald } 11657856c818Smatthias.ringwald 1166e76a89eeS[email protected] // assert packet is complete 11674ea43905SMatthias Ringwald if ((acl_length + 4u) != size){ 1168c3b46f5aSMatthias Ringwald log_error("acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 1169e76a89eeS[email protected] return; 1170e76a89eeS[email protected] } 1171e76a89eeS[email protected] 117252db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 11737856c818Smatthias.ringwald // update idle timestamp 11747856c818Smatthias.ringwald hci_connection_timestamp(conn); 117552db98b2SMatthias Ringwald #endif 11767856c818Smatthias.ringwald 11772b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 11782b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 11792b838201SMatthias Ringwald conn->num_packets_completed++; 11802b838201SMatthias Ringwald #endif 11812b838201SMatthias Ringwald 11827856c818Smatthias.ringwald // handle different packet types 11834ea43905SMatthias Ringwald switch (acl_flags & 0x03u) { 11847856c818Smatthias.ringwald 11857856c818Smatthias.ringwald case 0x01: // continuation fragment 11867856c818Smatthias.ringwald 11870ca847afS[email protected] // sanity checks 11884ea43905SMatthias Ringwald if (conn->acl_recombination_pos == 0u) { 11899da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 11907856c818Smatthias.ringwald return; 11917856c818Smatthias.ringwald } 11924ea43905SMatthias Ringwald if ((conn->acl_recombination_pos + acl_length) > (4u + HCI_ACL_BUFFER_SIZE)){ 11930ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 11940ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 11950ca847afS[email protected] conn->acl_recombination_pos = 0; 11960ca847afS[email protected] return; 11970ca847afS[email protected] } 11987856c818Smatthias.ringwald 11997856c818Smatthias.ringwald // append fragment payload (header already stored) 12006535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], 12016535961aSMatthias Ringwald &packet[4], acl_length); 12027856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 12037856c818Smatthias.ringwald 12047856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 12054ea43905SMatthias Ringwald if (conn->acl_recombination_pos >= (conn->acl_recombination_length + 4u + 4u)){ // pos already incl. ACL header 1206d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 12077856c818Smatthias.ringwald // reset recombination buffer 12087856c818Smatthias.ringwald conn->acl_recombination_length = 0; 12097856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 12107856c818Smatthias.ringwald } 12117856c818Smatthias.ringwald break; 12127856c818Smatthias.ringwald 12137856c818Smatthias.ringwald case 0x02: { // first fragment 12147856c818Smatthias.ringwald 121523a77e1aS[email protected] // sanity check 121623a77e1aS[email protected] if (conn->acl_recombination_pos) { 121760737d2bSMatthias Ringwald // we just received the first fragment, but still have data. Only warn if the packet wasn't a flushable packet 121860737d2bSMatthias Ringwald if ((conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE+1] >> 4) != 0x02){ 121960737d2bSMatthias Ringwald log_error( "ACL First Fragment but %u bytes in buffer for handle 0x%02x, dropping stale fragments", conn->acl_recombination_pos, con_handle); 122060737d2bSMatthias Ringwald } 122123a77e1aS[email protected] conn->acl_recombination_pos = 0; 122223a77e1aS[email protected] } 122323a77e1aS[email protected] 12247856c818Smatthias.ringwald // peek into L2CAP packet! 12257856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 12267856c818Smatthias.ringwald 12277856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 12284ea43905SMatthias Ringwald if (acl_length >= (l2cap_length + 4u)){ 12297856c818Smatthias.ringwald // forward fragment as L2CAP packet 12304ea43905SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4u); 12317856c818Smatthias.ringwald } else { 12320ca847afS[email protected] 12330ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 12340ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 12350ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 12360ca847afS[email protected] return; 12370ca847afS[email protected] } 12380ca847afS[email protected] 12397856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 12406535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 12414ea43905SMatthias Ringwald packet, acl_length + 4u); 12424ea43905SMatthias Ringwald conn->acl_recombination_pos = acl_length + 4u; 12437856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 12444ea43905SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2u, l2cap_length +4u); 12457856c818Smatthias.ringwald } 12467856c818Smatthias.ringwald break; 12477856c818Smatthias.ringwald 12487856c818Smatthias.ringwald } 12497856c818Smatthias.ringwald default: 1250c3b46f5aSMatthias Ringwald log_error( "acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 12517856c818Smatthias.ringwald return; 12527856c818Smatthias.ringwald } 125394ab26f8Smatthias.ringwald 125494ab26f8Smatthias.ringwald // execute main loop 125594ab26f8Smatthias.ringwald hci_run(); 125616833f0aSmatthias.ringwald } 125722909952Smatthias.ringwald 12581ab2dc58SMatthias Ringwald static void hci_connection_stop_timer(hci_connection_t * conn){ 12591ab2dc58SMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 12601ab2dc58SMatthias Ringwald #ifdef ENABLE_CLASSIC 12611ab2dc58SMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout_sco); 12621ab2dc58SMatthias Ringwald #endif 12631ab2dc58SMatthias Ringwald } 12641ab2dc58SMatthias Ringwald 126567a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 12669da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 12673c4d4b90Smatthias.ringwald 1268b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1269cb70c5abSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(HAVE_SCO_TRANSPORT) 1270cb70c5abSMatthias Ringwald bd_addr_type_t addr_type = conn->address_type; 1271cb70c5abSMatthias Ringwald #endif 1272cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1273cb70c5abSMatthias Ringwald hci_con_handle_t con_handle = conn->con_handle; 1274ee752bb8SMatthias Ringwald #endif 1275b3264428SMatthias Ringwald #endif 1276ee752bb8SMatthias Ringwald 12771ab2dc58SMatthias Ringwald hci_connection_stop_timer(conn); 1278c785ef68Smatthias.ringwald 1279665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1280a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 12813c4d4b90Smatthias.ringwald 12823c4d4b90Smatthias.ringwald // now it's gone 1283c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 1284ee752bb8SMatthias Ringwald 1285b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1286034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1287ee752bb8SMatthias Ringwald // update SCO 1288cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->hci_transport != NULL) && (hci_stack->hci_transport->set_sco_config != NULL)){ 1289ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1290ee752bb8SMatthias Ringwald } 1291034e9b53SMatthias Ringwald #endif 1292cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1293cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->sco_transport != NULL)){ 1294cb70c5abSMatthias Ringwald hci_stack->sco_transport->close(con_handle); 1295cb70c5abSMatthias Ringwald } 1296cb70c5abSMatthias Ringwald #endif 1297b3264428SMatthias Ringwald #endif 1298c7e0c5f6Smatthias.ringwald } 1299c7e0c5f6Smatthias.ringwald 130035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 130135454696SMatthias Ringwald 13020c042179S[email protected] static const uint16_t packet_type_sizes[] = { 13038f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 13048f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 13058f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 13068f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 13078f8108aaSmatthias.ringwald }; 130865389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 130965389bfcS[email protected] 0, // 3 slot packets 131065389bfcS[email protected] 1, // 5 slot packets 131165389bfcS[email protected] 25, // EDR 2 mpbs 131265389bfcS[email protected] 26, // EDR 3 mbps 131365389bfcS[email protected] 39, // 3 slot EDR packts 131465389bfcS[email protected] 40, // 5 slot EDR packet 131565389bfcS[email protected] }; 131665389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 131765389bfcS[email protected] 0x0f00, // 3 slot packets 131865389bfcS[email protected] 0xf000, // 5 slot packets 131965389bfcS[email protected] 0x1102, // EDR 2 mpbs 132065389bfcS[email protected] 0x2204, // EDR 3 mbps 132165389bfcS[email protected] 0x0300, // 3 slot EDR packts 132265389bfcS[email protected] 0x3000, // 5 slot EDR packet 132365389bfcS[email protected] }; 13248f8108aaSmatthias.ringwald 132565389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 132665389bfcS[email protected] // enable packet types based on size 13278f8108aaSmatthias.ringwald uint16_t packet_types = 0; 1328f16a69bbS[email protected] unsigned int i; 13298f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 13308f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 13318f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 13328f8108aaSmatthias.ringwald packet_types |= 1 << i; 13338f8108aaSmatthias.ringwald } 13348f8108aaSmatthias.ringwald } 133565389bfcS[email protected] // disable packet types due to missing local supported features 133665389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 1337f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 133865389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 133965389bfcS[email protected] if (feature_set) continue; 134065389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 134165389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 134265389bfcS[email protected] } 13438f8108aaSmatthias.ringwald // flip bits for "may not be used" 13448f8108aaSmatthias.ringwald packet_types ^= 0x3306; 13458f8108aaSmatthias.ringwald return packet_types; 13468f8108aaSmatthias.ringwald } 13478f8108aaSmatthias.ringwald 13488f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 13493a9fb326S[email protected] return hci_stack->packet_types; 13508f8108aaSmatthias.ringwald } 135135454696SMatthias Ringwald #endif 13528f8108aaSmatthias.ringwald 1353facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 13547dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 13553a9fb326S[email protected] return hci_stack->hci_packet_buffer; 13567dc17943Smatthias.ringwald } 13577dc17943Smatthias.ringwald 1358f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 13593a9fb326S[email protected] return hci_stack->acl_data_packet_length; 13607dc17943Smatthias.ringwald } 13617dc17943Smatthias.ringwald 136206b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 136320dcdd22SMatthias Ringwald bool hci_extended_sco_link_supported(void){ 13643e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 13653e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 13663e68d23dSMatthias Ringwald } 136706b9e820SMatthias Ringwald #endif 13683e68d23dSMatthias Ringwald 136920dcdd22SMatthias Ringwald bool hci_non_flushable_packet_boundary_flag_supported(void){ 13706ac9a97eS[email protected] // No. 54, byte 6, bit 6 13714ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 6u)) != 0u; 13726ac9a97eS[email protected] } 13736ac9a97eS[email protected] 13749885fb2dSMatthias Ringwald #ifdef ENABLE_CLASSIC 137515a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 13766ac9a97eS[email protected] // No. 51, byte 6, bit 3 13774ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 3u)) != 0u; 1378f5d8d141S[email protected] } 13799885fb2dSMatthias Ringwald #endif 1380f5d8d141S[email protected] 13817f02f414SMatthias Ringwald static int hci_classic_supported(void){ 138235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 13836ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 13843a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 138535454696SMatthias Ringwald #else 138635454696SMatthias Ringwald return 0; 138735454696SMatthias Ringwald #endif 1388f5d8d141S[email protected] } 1389f5d8d141S[email protected] 13907f02f414SMatthias Ringwald static int hci_le_supported(void){ 1391a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 13926ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 13934ea43905SMatthias Ringwald return (hci_stack->local_supported_features[4u] & (1u << 6u)) != 0u; 1394f5d8d141S[email protected] #else 1395f5d8d141S[email protected] return 0; 1396f5d8d141S[email protected] #endif 1397f5d8d141S[email protected] } 1398f5d8d141S[email protected] 13991ffa425cSMatthias Ringwald static bool hci_command_supported(uint8_t command_index){ 14001ffa425cSMatthias Ringwald return (hci_stack->local_supported_commands & (1LU << command_index)) != 0; 14011ffa425cSMatthias Ringwald } 14021ffa425cSMatthias Ringwald 1403b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 1404b95a5a35SMatthias Ringwald 140563671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 140663671e69SMatthias Ringwald static bool hci_extended_advertising_supported(void){ 140763671e69SMatthias Ringwald return hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE); 140863671e69SMatthias Ringwald } 140963671e69SMatthias Ringwald #endif 141063671e69SMatthias Ringwald 1411f5873674SMatthias Ringwald static void hci_get_own_address_for_addr_type(uint8_t own_addr_type, bd_addr_t own_addr){ 14126bcfa632SMatthias Ringwald if (own_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 14136bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->local_bd_addr, 6); 141469a97523S[email protected] } else { 14156bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->le_random_address, 6); 141669a97523S[email protected] } 141769a97523S[email protected] } 141869a97523S[email protected] 14196bcfa632SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 14206bcfa632SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 14216bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_own_addr_type, addr); 14226bcfa632SMatthias Ringwald } 14236bcfa632SMatthias Ringwald 14246bcfa632SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 14256bcfa632SMatthias Ringwald void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr){ 14266bcfa632SMatthias Ringwald *addr_type = hci_stack->le_advertisements_own_addr_type; 14276bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, addr); 14286bcfa632SMatthias Ringwald }; 14296bcfa632SMatthias Ringwald #endif 14306bcfa632SMatthias Ringwald 1431e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 14326bcfa632SMatthias Ringwald 14336bcfa632SMatthias Ringwald /** 14346bcfa632SMatthias Ringwald * @brief Get own addr type and address used for LE connections (Central) 14356bcfa632SMatthias Ringwald */ 14366bcfa632SMatthias Ringwald void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){ 14376bcfa632SMatthias Ringwald *addr_type = hci_stack->le_connection_own_addr_type; 14386bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, addr); 14396bcfa632SMatthias Ringwald } 14406bcfa632SMatthias Ringwald 1441384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size){ 14429ec2630cSMatthias Ringwald 1443a0698cb4SMatthias Ringwald uint16_t offset = 3; 1444a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset]; 1445d1dc057bS[email protected] offset += 1; 1446d1dc057bS[email protected] 1447a0698cb4SMatthias Ringwald uint16_t i; 144803fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1449a1df452eSMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 145033e6948bSMatthias Ringwald // sanity checks on data_length: 145133e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 145233e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 14534ea43905SMatthias Ringwald if ((offset + 9u + data_length + 1u) > size) return; 145433e6948bSMatthias Ringwald // setup event 14554ea43905SMatthias Ringwald uint8_t event_size = 10u + data_length; 1456a0698cb4SMatthias Ringwald uint16_t pos = 0; 1457045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 145857c9da5bS[email protected] event[pos++] = event_size; 14596535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address 1460d1dc057bS[email protected] offset += 8; 1461d1dc057bS[email protected] pos += 8; 1462d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 146333e6948bSMatthias Ringwald event[pos++] = data_length; 146433e6948bSMatthias Ringwald offset++; 14656535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], data_length); 146657c9da5bS[email protected] pos += data_length; 14674ea43905SMatthias Ringwald offset += data_length + 1u; // rssi 1468d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 146957c9da5bS[email protected] } 147057c9da5bS[email protected] } 1471a0698cb4SMatthias Ringwald 1472a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 1473a0698cb4SMatthias Ringwald void le_handle_extended_advertisement_report(uint8_t *packet, uint16_t size) { 1474a0698cb4SMatthias Ringwald uint16_t offset = 3; 1475a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset++]; 1476c95a0fc7SMatthias Ringwald uint8_t event[2 + 255]; // use upper bound to avoid var size automatic var 1477a0698cb4SMatthias Ringwald uint8_t i; 1478a0698cb4SMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 1479a0698cb4SMatthias Ringwald // sanity checks on data_length: 1480a0698cb4SMatthias Ringwald uint16_t data_length = packet[offset + 23]; 1481a0698cb4SMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 1482a0698cb4SMatthias Ringwald if ((offset + 24u + data_length) > size) return; 1483a0698cb4SMatthias Ringwald uint16_t event_type = little_endian_read_16(packet, offset); 1484a0698cb4SMatthias Ringwald offset += 2; 1485a0698cb4SMatthias Ringwald if ((event_type & 0x10) != 0) { 1486a0698cb4SMatthias Ringwald // setup legacy event 1487a0698cb4SMatthias Ringwald uint8_t legacy_event_type; 1488a0698cb4SMatthias Ringwald switch (event_type){ 1489a0698cb4SMatthias Ringwald case 0b0010011: 1490a0698cb4SMatthias Ringwald // ADV_IND 1491a0698cb4SMatthias Ringwald legacy_event_type = 0; 1492a0698cb4SMatthias Ringwald break; 1493a0698cb4SMatthias Ringwald case 0b0010101: 1494a0698cb4SMatthias Ringwald // ADV_DIRECT_IND 1495a0698cb4SMatthias Ringwald legacy_event_type = 1; 1496a0698cb4SMatthias Ringwald break; 1497a0698cb4SMatthias Ringwald case 0b0010010: 1498a0698cb4SMatthias Ringwald // ADV_SCAN_IND 1499a0698cb4SMatthias Ringwald legacy_event_type = 2; 1500a0698cb4SMatthias Ringwald break; 1501a0698cb4SMatthias Ringwald case 0b0010000: 1502a0698cb4SMatthias Ringwald // ADV_NONCONN_IND 1503a0698cb4SMatthias Ringwald legacy_event_type = 3; 1504a0698cb4SMatthias Ringwald break; 1505a0698cb4SMatthias Ringwald case 0b0011011: 1506a0698cb4SMatthias Ringwald case 0b0011010: 1507a0698cb4SMatthias Ringwald // SCAN_RSP 1508a0698cb4SMatthias Ringwald legacy_event_type = 4; 1509a0698cb4SMatthias Ringwald break; 1510a0698cb4SMatthias Ringwald default: 1511a0698cb4SMatthias Ringwald legacy_event_type = 0; 1512a0698cb4SMatthias Ringwald break; 1513a0698cb4SMatthias Ringwald } 1514a0698cb4SMatthias Ringwald uint16_t pos = 0; 1515a0698cb4SMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 1516a0698cb4SMatthias Ringwald event[pos++] = 10u + data_length; 1517a0698cb4SMatthias Ringwald event[pos++] = legacy_event_type; 1518a0698cb4SMatthias Ringwald // copy address type + address 1519a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + 6); 1520a0698cb4SMatthias Ringwald offset += 7; 1521a0698cb4SMatthias Ringwald pos += 7; 1522a0698cb4SMatthias Ringwald // skip primary_phy, secondary_phy, advertising_sid, tx_power 1523a0698cb4SMatthias Ringwald offset += 4; 1524a0698cb4SMatthias Ringwald // copy rssi 1525a0698cb4SMatthias Ringwald event[pos++] = packet[offset++]; 1526a0698cb4SMatthias Ringwald // skip periodic advertising interval and direct address 1527a0698cb4SMatthias Ringwald offset += 9; 1528a0698cb4SMatthias Ringwald // copy data len + data; 1529a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + data_length); 1530a0698cb4SMatthias Ringwald pos += 1 +data_length; 1531a0698cb4SMatthias Ringwald offset += 1+ data_length; 1532a0698cb4SMatthias Ringwald hci_emit_event(event, pos, 1); 1533a0698cb4SMatthias Ringwald } else { 1534c95a0fc7SMatthias Ringwald event[0] = GAP_EVENT_EXTENDED_ADVERTISING_REPORT; 1535c95a0fc7SMatthias Ringwald uint8_t report_len = 24 + data_length; 1536c95a0fc7SMatthias Ringwald event[1] = report_len; 15370f7610adSMatthias Ringwald little_endian_store_16(event, 2, event_type); 15380f7610adSMatthias Ringwald memcpy(&event[4], &packet[offset], report_len); 1539c95a0fc7SMatthias Ringwald offset += report_len; 1540c95a0fc7SMatthias Ringwald hci_emit_event(event, 2 + report_len, 1); 1541a0698cb4SMatthias Ringwald } 1542a0698cb4SMatthias Ringwald } 1543a0698cb4SMatthias Ringwald } 1544a0698cb4SMatthias Ringwald #endif 1545a0698cb4SMatthias Ringwald 1546b2f949feS[email protected] #endif 1547e8c8828eSMatthias Ringwald #endif 154857c9da5bS[email protected] 15492b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 15502b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1551bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1552a408e724SMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ENABLED) != 0){ 15532b6ab3e6SMatthias Ringwald // get number of active le slave connections 15542b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 15552b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 15562b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 15572b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 15582b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 15592b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 15602b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 15612b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 15622b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 15632b6ab3e6SMatthias Ringwald num_slave_connections++; 15642b6ab3e6SMatthias Ringwald } 15652b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1566bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1567bbc366e6SMatthias Ringwald } else { 1568bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 15692b6ab3e6SMatthias Ringwald } 15702b6ab3e6SMatthias Ringwald } 15712b6ab3e6SMatthias Ringwald #endif 15722b6ab3e6SMatthias Ringwald #endif 15732b6ab3e6SMatthias Ringwald 157459d59ecfSMatthias Ringwald #ifdef ENABLE_CLASSIC 157559d59ecfSMatthias Ringwald static void gap_run_set_local_name(void){ 157659d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 157759d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 157859d59ecfSMatthias Ringwald // construct HCI Command and send 157959d59ecfSMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 158059d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 158159d59ecfSMatthias Ringwald packet[0] = opcode & 0xff; 158259d59ecfSMatthias Ringwald packet[1] = opcode >> 8; 158359d59ecfSMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 158459d59ecfSMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 158559d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 158659d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 158759d59ecfSMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 158859d59ecfSMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 158959d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 159059d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 159159d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 159259d59ecfSMatthias Ringwald } 159359d59ecfSMatthias Ringwald 159459d59ecfSMatthias Ringwald static void gap_run_set_eir_data(void){ 159559d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 159659d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 159759d59ecfSMatthias Ringwald // construct HCI Command in-place and send 159859d59ecfSMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 159959d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 160059d59ecfSMatthias Ringwald uint16_t offset = 0; 160159d59ecfSMatthias Ringwald packet[offset++] = opcode & 0xff; 160259d59ecfSMatthias Ringwald packet[offset++] = opcode >> 8; 160359d59ecfSMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 160459d59ecfSMatthias Ringwald packet[offset++] = 0; // FEC not required 160559d59ecfSMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 160659d59ecfSMatthias Ringwald if (hci_stack->eir_data){ 160759d59ecfSMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 160859d59ecfSMatthias Ringwald ad_context_t context; 160959d59ecfSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 161059d59ecfSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 161159d59ecfSMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 161259d59ecfSMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 161359d59ecfSMatthias Ringwald // copy item 161459d59ecfSMatthias Ringwald packet[offset++] = size + 1; 161559d59ecfSMatthias Ringwald packet[offset++] = data_type; 161659d59ecfSMatthias Ringwald memcpy(&packet[offset], data, size); 161759d59ecfSMatthias Ringwald // update name item 161859d59ecfSMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 161959d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 162059d59ecfSMatthias Ringwald } 162159d59ecfSMatthias Ringwald offset += size; 162259d59ecfSMatthias Ringwald } 162359d59ecfSMatthias Ringwald } else { 162459d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 162559d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 162659d59ecfSMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 162759d59ecfSMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 162859d59ecfSMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 162959d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 163059d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 163159d59ecfSMatthias Ringwald } 163259d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 163359d59ecfSMatthias Ringwald } 1634ab4831a3SMatthias Ringwald 1635ab4831a3SMatthias Ringwald static void hci_run_gap_tasks_classic(void){ 1636baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_CLASS_OF_DEVICE) != 0) { 1637baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_CLASS_OF_DEVICE; 1638ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1639ab4831a3SMatthias Ringwald return; 1640ab4831a3SMatthias Ringwald } 1641baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_LOCAL_NAME) != 0) { 1642baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_LOCAL_NAME; 1643ab4831a3SMatthias Ringwald gap_run_set_local_name(); 1644ab4831a3SMatthias Ringwald return; 1645ab4831a3SMatthias Ringwald } 1646baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_EIR_DATA) != 0) { 1647baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_EIR_DATA; 1648ab4831a3SMatthias Ringwald gap_run_set_eir_data(); 1649ab4831a3SMatthias Ringwald return; 1650ab4831a3SMatthias Ringwald } 1651baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_DEFAULT_LINK_POLICY) != 0) { 1652baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_DEFAULT_LINK_POLICY; 1653ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1654ab4831a3SMatthias Ringwald return; 1655ab4831a3SMatthias Ringwald } 1656ab4831a3SMatthias Ringwald // write page scan activity 1657baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY) != 0) { 1658baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 1659ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 1660ab4831a3SMatthias Ringwald return; 1661ab4831a3SMatthias Ringwald } 1662ab4831a3SMatthias Ringwald // write page scan type 1663baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_TYPE) != 0) { 1664baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_TYPE; 1665ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 1666ab4831a3SMatthias Ringwald return; 1667ab4831a3SMatthias Ringwald } 166832a12730SMatthias Ringwald // write page timeout 1669baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_TIMEOUT) != 0) { 1670baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_TIMEOUT; 167132a12730SMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, hci_stack->page_timeout); 167232a12730SMatthias Ringwald return; 167332a12730SMatthias Ringwald } 1674ab4831a3SMatthias Ringwald // send scan enable 1675baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_SCAN_ENABLE) != 0) { 1676baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_SCAN_ENABLE; 1677ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 1678ab4831a3SMatthias Ringwald return; 1679ab4831a3SMatthias Ringwald } 1680c0c8a829SMatthias Ringwald // send write scan activity 1681baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY) != 0) { 1682baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 1683c0c8a829SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_scan_activity, hci_stack->inquiry_scan_interval, hci_stack->inquiry_scan_window); 1684c0c8a829SMatthias Ringwald return; 1685c0c8a829SMatthias Ringwald } 1686ab4831a3SMatthias Ringwald } 168759d59ecfSMatthias Ringwald #endif 168859d59ecfSMatthias Ringwald 16896fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 169006b9e820SMatthias Ringwald 169196b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 169296b53536SMatthias Ringwald if (!hci_stack->config) return 0; 16939796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 169496b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1695a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 169696b53536SMatthias Ringwald baud_rate = 3000000; 169796b53536SMatthias Ringwald } 169896b53536SMatthias Ringwald return baud_rate; 169996b53536SMatthias Ringwald } 170096b53536SMatthias Ringwald 1701ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 17029ec2630cSMatthias Ringwald UNUSED(ds); 17039ec2630cSMatthias Ringwald 17040305bdeaSMatthias Ringwald switch (hci_stack->substate){ 17050305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 17067b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 17070305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 17087b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 17090305bdeaSMatthias Ringwald hci_run(); 17100305bdeaSMatthias Ringwald break; 17119f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 17129f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 17139f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 17149f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 17159f007422SMatthias Ringwald } 1716cf373d3aSMatthias Ringwald 1717cf373d3aSMatthias Ringwald /* fall through */ 1718cf373d3aSMatthias Ringwald 1719e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1720e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1721e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1722e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1723e47e68c7SMatthias Ringwald hci_run(); 1724688c2635SMatthias Ringwald break; 17257224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 17267224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 172796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1728cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 17297dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 17307224be7eSMatthias Ringwald } 1731834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 173261f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1733834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1734834bce8cSMatthias Ringwald log_info("Link Reset"); 1735834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1736834bce8cSMatthias Ringwald } 1737772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1738772a36d3SMatthias Ringwald hci_run(); 1739772a36d3SMatthias Ringwald } 17404696bddbSMatthias Ringwald break; 1741559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1742559961d0SMatthias Ringwald // otherwise continue 1743559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1744559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1745559961d0SMatthias Ringwald break; 17460305bdeaSMatthias Ringwald default: 17470305bdeaSMatthias Ringwald break; 17480305bdeaSMatthias Ringwald } 17490305bdeaSMatthias Ringwald } 175006b9e820SMatthias Ringwald #endif 17510305bdeaSMatthias Ringwald 175271de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 175374b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 175474b323a9SMatthias Ringwald } 175574b323a9SMatthias Ringwald 1756f795c86eSMatthias Ringwald static void hci_init_done(void){ 1757f795c86eSMatthias Ringwald // done. tell the app 1758f795c86eSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1759f795c86eSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1760f795c86eSMatthias Ringwald hci_emit_state(); 1761f795c86eSMatthias Ringwald } 1762f795c86eSMatthias Ringwald 176374b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 176471de195eSMatthias Ringwald static void hci_initializing_run(void){ 1765148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 1766f4c579d4SMatthias Ringwald 17670d37aff3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 17680d37aff3SMatthias Ringwald 1769f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1770f9fd20c2SMatthias Ringwald bool need_baud_change = hci_stack->config 1771f4c579d4SMatthias Ringwald && hci_stack->chipset 1772f4c579d4SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1773f4c579d4SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 1774f4c579d4SMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1775f4c579d4SMatthias Ringwald #endif 1776f4c579d4SMatthias Ringwald 177774b323a9SMatthias Ringwald switch (hci_stack->substate){ 177874b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 177974b323a9SMatthias Ringwald hci_state_reset(); 1780a0cf2f3fSMatthias Ringwald 17816fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 17820305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1783659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1784528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1785528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1786a0cf2f3fSMatthias Ringwald #endif 17870305bdeaSMatthias Ringwald // send command 178874b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 17890305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 179074b323a9SMatthias Ringwald break; 179176fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 179276fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 179376fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 179476fcb19bSMatthias Ringwald break; 1795e28e41c0SMatthias Ringwald 1796e28e41c0SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1797e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1798e47e68c7SMatthias Ringwald hci_state_reset(); 1799e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1800659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1801528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1802528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1803e47e68c7SMatthias Ringwald // send command 1804e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1805e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1806e47e68c7SMatthias Ringwald break; 18078d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 18088d29070eSMatthias Ringwald hci_state_reset(); 18098d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 18108d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 18118d29070eSMatthias Ringwald break; 1812f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 1813f4c579d4SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1814f4c579d4SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1815f4c579d4SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1816f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1817f4c579d4SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1818f4c579d4SMatthias Ringwald break; 1819f4c579d4SMatthias Ringwald } 1820c97af506SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 1821c97af506SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 1822c97af506SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1823c97af506SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1824c97af506SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 1825c97af506SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1826c97af506SMatthias Ringwald break; 18278250c242SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 18288250c242SMatthias Ringwald #ifdef ENABLE_CLASSIC 18298250c242SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 18308250c242SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 18318250c242SMatthias Ringwald break; 18328250c242SMatthias Ringwald #endif 18338250c242SMatthias Ringwald /* fall through */ 18348250c242SMatthias Ringwald 1835f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: 1836f4c579d4SMatthias Ringwald if (need_baud_change) { 183796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 18383fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1839f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 184074b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 18414ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 18424696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 18434696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 184461f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1845659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1846528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 18474696bddbSMatthias Ringwald } 184874b323a9SMatthias Ringwald break; 1849fab26ab3SMatthias Ringwald } 1850f4c579d4SMatthias Ringwald 1851f4c579d4SMatthias Ringwald /* fall through */ 1852f4c579d4SMatthias Ringwald 185374b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 185474b323a9SMatthias Ringwald // Custom initialization 18553fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1856ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 18571979f09cSMatthias Ringwald bool send_cmd = false; 1858ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1859f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 18601979f09cSMatthias Ringwald send_cmd = true; 1861e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1862e47e68c7SMatthias Ringwald break; 1863f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 18641979f09cSMatthias Ringwald send_cmd = true; 1865f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1866e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1867659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1868528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1869528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1870a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1871772a36d3SMatthias Ringwald && hci_stack->config 18723fb36a29SMatthias Ringwald && hci_stack->chipset 18733fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1874772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 18752caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1876772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1877772a36d3SMatthias Ringwald } else { 18789f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1879772a36d3SMatthias Ringwald } 1880e47e68c7SMatthias Ringwald break; 1881f41911edSMatthias Ringwald default: 1882f41911edSMatthias Ringwald break; 1883e47e68c7SMatthias Ringwald } 1884ee720f3aSMatthias Ringwald 1885ee720f3aSMatthias Ringwald if (send_cmd){ 18864ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1887ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1888ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 18890305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 189074b323a9SMatthias Ringwald break; 189174b323a9SMatthias Ringwald } 1892148ca237SMatthias Ringwald log_info("Init script done"); 189392a0d36dSMatthias Ringwald 1894559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1895ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1896a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1897a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1898e021ff1eSMatthias Ringwald 1899559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 190092a0d36dSMatthias Ringwald if (need_baud_change) { 19019796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1902cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 190392a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 190492a0d36dSMatthias Ringwald } 1905559961d0SMatthias Ringwald 1906f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1907f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1908f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1909f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1910559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1911f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1912559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1913559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1914559961d0SMatthias Ringwald break; 191592a0d36dSMatthias Ringwald } 191674b323a9SMatthias Ringwald } 191706b9e820SMatthias Ringwald #endif 1918521bd5ffSMatthias Ringwald /* fall through */ 191906b9e820SMatthias Ringwald 192006b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 192106b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 192206b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 192306b9e820SMatthias Ringwald break; 192453860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 192553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 192653860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 192753860077SMatthias Ringwald break; 192874b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 19295ffe9d0bSMatthias Ringwald // only read buffer size if supported 19301ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE)){ 193174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 19320305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 193374b323a9SMatthias Ringwald break; 19345ffe9d0bSMatthias Ringwald } 1935521bd5ffSMatthias Ringwald 19365ffe9d0bSMatthias Ringwald /* fall through */ 19375ffe9d0bSMatthias Ringwald 193853860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 193953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 19400305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 194174b323a9SMatthias Ringwald break; 19422b838201SMatthias Ringwald 19432b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 19442b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 19452b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 19462b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 19472b838201SMatthias Ringwald break; 19482b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 19492b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 19502b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 19512b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 19522b838201SMatthias Ringwald break; 19532b838201SMatthias Ringwald #endif 19542b838201SMatthias Ringwald 195574b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 19560305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 195774b323a9SMatthias Ringwald if (hci_le_supported()){ 19585ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x3FFFFFFFU); 195974b323a9SMatthias Ringwald } else { 196074b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 19615ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x1FFFFFFFU); 196274b323a9SMatthias Ringwald } 196374b323a9SMatthias Ringwald break; 19642b838201SMatthias Ringwald 1965ff7d1758SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK_2: 19668254e329SMatthias Ringwald // On Bluettooth PTS dongle (BL 654) with PacketCraft HCI Firmware (LMP subversion) 0x5244, 19678254e329SMatthias Ringwald // setting Event Mask 2 causes Controller to drop Encryption Change events. 19688254e329SMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_SET_EVENT_MASK_PAGE_2) 19698254e329SMatthias Ringwald && (hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC)){ 1970ff7d1758SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK_2; 1971ff7d1758SMatthias Ringwald // Encryption Change Event v2 - bit 25 1972ff7d1758SMatthias Ringwald hci_send_cmd(&hci_set_event_mask_2,0x02000000U, 0x0); 1973ff7d1758SMatthias Ringwald break; 1974ff7d1758SMatthias Ringwald } 1975ff7d1758SMatthias Ringwald 197635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1977ff7d1758SMatthias Ringwald /* fall through */ 1978ff7d1758SMatthias Ringwald 197974b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 1980f795c86eSMatthias Ringwald if (hci_classic_supported() && gap_ssp_supported()){ 198174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 19820305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 198374b323a9SMatthias Ringwald break; 1984e7c662faSMatthias Ringwald } 1985521bd5ffSMatthias Ringwald 1986e7c662faSMatthias Ringwald /* fall through */ 1987e7c662faSMatthias Ringwald 1988f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1989f795c86eSMatthias Ringwald if (hci_classic_supported()){ 1990f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 19918a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1992f6858d14SMatthias Ringwald break; 1993f795c86eSMatthias Ringwald } 1994521bd5ffSMatthias Ringwald 1995f795c86eSMatthias Ringwald /* fall through */ 1996f795c86eSMatthias Ringwald 19975d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 1998f3052906SMatthias Ringwald // skip write secure connections host support if not supported or disabled 19991ffa425cSMatthias Ringwald if (hci_classic_supported() && hci_stack->secure_connections_enable 20001ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST)) { 2001c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 20025d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 20031ffa425cSMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 20045d23aae8SMatthias Ringwald break; 2005f3052906SMatthias Ringwald } 2006521bd5ffSMatthias Ringwald 2007a391128dSMatthias Ringwald /* fall through */ 2008a391128dSMatthias Ringwald 2009a391128dSMatthias Ringwald case HCI_INIT_SET_MIN_ENCRYPTION_KEY_SIZE: 2010a391128dSMatthias Ringwald // skip set min encryption key size 2011a391128dSMatthias Ringwald if (hci_classic_supported() && hci_command_supported(SUPPORTED_HCI_COMMAND_SET_MIN_ENCRYPTION_KEY_SIZE)) { 2012a391128dSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_MIN_ENCRYPTION_KEY_SIZE; 2013a391128dSMatthias Ringwald hci_send_cmd(&hci_set_min_encryption_key_size, hci_stack->gap_required_encyrption_key_size); 2014a391128dSMatthias Ringwald break; 2015a391128dSMatthias Ringwald } 2016a391128dSMatthias Ringwald 2017e4c6114dSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2018521bd5ffSMatthias Ringwald /* fall through */ 2019521bd5ffSMatthias Ringwald 2020483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 2021729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 2022e4c6114dSMatthias Ringwald // skip write synchronous flow control if not supported 20231ffa425cSMatthias Ringwald if (hci_classic_supported() 20241ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)) { 2025729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 2026729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 2027729ed62eSMatthias Ringwald break; 2028f795c86eSMatthias Ringwald } 2029f795c86eSMatthias Ringwald /* fall through */ 2030f795c86eSMatthias Ringwald 2031483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 2032e4c6114dSMatthias Ringwald // skip write default erroneous data reporting if not supported 20331ffa425cSMatthias Ringwald if (hci_classic_supported() 20341ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING)) { 2035483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 2036483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 2037483c5078SMatthias Ringwald break; 2038f795c86eSMatthias Ringwald } 2039e4c6114dSMatthias Ringwald #endif 2040f795c86eSMatthias Ringwald 2041f795c86eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(ENABLE_SCO_OVER_PCM) 2042521bd5ffSMatthias Ringwald /* fall through */ 2043521bd5ffSMatthias Ringwald 20448051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 2045a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 2046e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 2047a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 20488051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2049a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 2050a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 20518051253fSMatthias Ringwald #endif 20528051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 20538051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 20541d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2055f795c86eSMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 16 kHz 20561d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 20571d2bbd54SMatthias Ringwald #else 20581d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 20591d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 20601d2bbd54SMatthias Ringwald #endif 20618051253fSMatthias Ringwald #endif 2062a42798c3SMatthias Ringwald break; 2063f795c86eSMatthias Ringwald } 2064f795c86eSMatthias Ringwald #endif 2065f795c86eSMatthias Ringwald 20664e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 2067521bd5ffSMatthias Ringwald /* fall through */ 2068521bd5ffSMatthias Ringwald 20694e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 2070e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 20714e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 20724e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 20731d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 20741d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 20751d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 20761d2bbd54SMatthias Ringwald #else 20771d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 20781d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 20791d2bbd54SMatthias Ringwald #endif 20804e821764SMatthias Ringwald break; 2081f795c86eSMatthias Ringwald } 208235454696SMatthias Ringwald #endif 20834e821764SMatthias Ringwald #endif 20844e821764SMatthias Ringwald 2085a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2086521bd5ffSMatthias Ringwald /* fall through */ 2087521bd5ffSMatthias Ringwald 208874b323a9SMatthias Ringwald // LE INIT 208974b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 2090f795c86eSMatthias Ringwald if (hci_le_supported()){ 209174b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 209279b7ea2dSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_BUFFER_SIZE_V2)){ 209379b7ea2dSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size_v2); 209479b7ea2dSMatthias Ringwald } else { 20950305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 209679b7ea2dSMatthias Ringwald } 209774b323a9SMatthias Ringwald break; 2098f795c86eSMatthias Ringwald } 2099521bd5ffSMatthias Ringwald 2100f795c86eSMatthias Ringwald /* fall through */ 2101f795c86eSMatthias Ringwald 2102699a5fcaSMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 2103699a5fcaSMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 21041ffa425cSMatthias Ringwald if (hci_le_supported() 21051ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED)) { 2106699a5fcaSMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 2107699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 2108699a5fcaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 2109daabb8b8SMatthias Ringwald break; 2110f795c86eSMatthias Ringwald } 2111521bd5ffSMatthias Ringwald 2112f795c86eSMatthias Ringwald /* fall through */ 2113f795c86eSMatthias Ringwald 2114699a5fcaSMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 2115f795c86eSMatthias Ringwald if (hci_le_supported()){ 2116699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 211791d9e5d0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0xfffffdff, 0x07); // all events from core v5.3 without LE Enhanced Connection Complete 211874b323a9SMatthias Ringwald break; 2119f795c86eSMatthias Ringwald } 2120b435e062SMatthias Ringwald #endif 2121b435e062SMatthias Ringwald 2122b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2123521bd5ffSMatthias Ringwald /* fall through */ 2124521bd5ffSMatthias Ringwald 2125dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 21261ffa425cSMatthias Ringwald if (hci_le_supported() 21271ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH)) { 2128dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 2129dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 2130dcd678baSMatthias Ringwald break; 2131f795c86eSMatthias Ringwald } 2132521bd5ffSMatthias Ringwald 2133f795c86eSMatthias Ringwald /* fall through */ 2134f795c86eSMatthias Ringwald 2135dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 21361ffa425cSMatthias Ringwald if (hci_le_supported() 21371ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)) { 2138dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 2139b435e062SMatthias 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); 2140dcd678baSMatthias Ringwald break; 2141f795c86eSMatthias Ringwald } 2142b435e062SMatthias Ringwald #endif 2143b435e062SMatthias Ringwald 2144b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2145521bd5ffSMatthias Ringwald /* fall through */ 2146521bd5ffSMatthias Ringwald 21473b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 2148f795c86eSMatthias Ringwald if (hci_le_supported()){ 21493b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 21503b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 21513b6d4121SMatthias Ringwald break; 2152f795c86eSMatthias Ringwald } 2153521bd5ffSMatthias Ringwald 215474b323a9SMatthias Ringwald #endif 21554f982f31SMatthias Ringwald 21563a74541eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 21573a74541eSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 21583a74541eSMatthias Ringwald /* fall through */ 21593a74541eSMatthias Ringwald 21603a74541eSMatthias Ringwald case HCI_INIT_LE_READ_MAX_ADV_DATA_LEN: 21613a74541eSMatthias Ringwald if (hci_extended_advertising_supported()){ 21623a74541eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_ADV_DATA_LEN; 21633a74541eSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_advertising_data_length); 21643a74541eSMatthias Ringwald break; 21653a74541eSMatthias Ringwald } 21663a74541eSMatthias Ringwald #endif 21673a74541eSMatthias Ringwald #endif 2168521bd5ffSMatthias Ringwald /* fall through */ 2169521bd5ffSMatthias Ringwald 2170c63ee49aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 2171c63ee49aSMatthias Ringwald case HCI_INIT_LE_SET_HOST_FEATURE_CONNECTED_ISO_STREAMS: 2172c63ee49aSMatthias Ringwald if (hci_le_supported()) { 2173c63ee49aSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_HOST_FEATURE_CONNECTED_ISO_STREAMS; 2174c63ee49aSMatthias Ringwald hci_send_cmd(&hci_le_set_host_feature, 32, 1); 2175c63ee49aSMatthias Ringwald break; 2176c63ee49aSMatthias Ringwald } 2177c63ee49aSMatthias Ringwald #endif 2178c63ee49aSMatthias Ringwald 2179c63ee49aSMatthias Ringwald /* fall through */ 2180c63ee49aSMatthias Ringwald 2181f795c86eSMatthias Ringwald case HCI_INIT_DONE: 21824f982f31SMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 218373799937SMatthias Ringwald // main init sequence complete 21844f982f31SMatthias Ringwald #ifdef ENABLE_CLASSIC 218573799937SMatthias Ringwald // check if initial Classic GAP Tasks are completed 2186baa4881eSMatthias Ringwald if (hci_classic_supported() && (hci_stack->gap_tasks_classic != 0)) { 21874f982f31SMatthias Ringwald hci_run_gap_tasks_classic(); 21884f982f31SMatthias Ringwald break; 21894f982f31SMatthias Ringwald } 21904f982f31SMatthias Ringwald #endif 219173799937SMatthias Ringwald #ifdef ENABLE_BLE 219273799937SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 219373799937SMatthias Ringwald // check if initial LE GAP Tasks are completed 219473799937SMatthias Ringwald if (hci_le_supported() && hci_stack->le_scanning_param_update) { 219573799937SMatthias Ringwald hci_run_general_gap_le(); 219673799937SMatthias Ringwald break; 219773799937SMatthias Ringwald } 219873799937SMatthias Ringwald #endif 219973799937SMatthias Ringwald #endif 2200f795c86eSMatthias Ringwald hci_init_done(); 2201f795c86eSMatthias Ringwald break; 2202f795c86eSMatthias Ringwald 220374b323a9SMatthias Ringwald default: 220474b323a9SMatthias Ringwald return; 220574b323a9SMatthias Ringwald } 220655975f88SMatthias Ringwald } 220755975f88SMatthias Ringwald 220807fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 220907fd2f31SMatthias Ringwald bool command_completed = false; 22100e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 2211f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 22126155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 221307fd2f31SMatthias Ringwald command_completed = true; 2214148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 22156155b3d3S[email protected] } else { 2216d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 22176155b3d3S[email protected] } 22186155b3d3S[email protected] } 22190f97eae7SMatthias Ringwald 22200e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 22216155b3d3S[email protected] uint8_t status = packet[2]; 2222f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 22236155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 22246155b3d3S[email protected] if (status){ 222507fd2f31SMatthias Ringwald command_completed = true; 2226148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 22276155b3d3S[email protected] } else { 22286155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 22296155b3d3S[email protected] } 22306155b3d3S[email protected] } else { 2231148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 22326155b3d3S[email protected] } 22336155b3d3S[email protected] } 22346fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2235e47e68c7SMatthias Ringwald // Vendor == CSR 22360e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 2237e47e68c7SMatthias Ringwald // TODO: track actual command 223807fd2f31SMatthias Ringwald command_completed = true; 2239e47e68c7SMatthias Ringwald } 2240a2481739S[email protected] 22414e9daa6fSMatthias Ringwald // Vendor == Toshiba 22420e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 22434e9daa6fSMatthias Ringwald // TODO: track actual command 224407fd2f31SMatthias Ringwald command_completed = true; 2245004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 2246004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 22474e9daa6fSMatthias Ringwald } 224807fd2f31SMatthias Ringwald #endif 224907fd2f31SMatthias Ringwald 225007fd2f31SMatthias Ringwald return command_completed; 225107fd2f31SMatthias Ringwald } 225207fd2f31SMatthias Ringwald 225307fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 225407fd2f31SMatthias Ringwald 225507fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 225607fd2f31SMatthias Ringwald 225707fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 225807fd2f31SMatthias Ringwald 22596fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 22604e9daa6fSMatthias Ringwald 22610f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 22620f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 22630f97eae7SMatthias Ringwald // 22640f97eae7SMatthias Ringwald // HCI Reset 22650f97eae7SMatthias Ringwald // Timeout 100 ms 22660f97eae7SMatthias Ringwald // HCI Reset 22670f97eae7SMatthias Ringwald // Command Complete Reset 22680f97eae7SMatthias Ringwald // HCI Read Local Version Information 22690f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 22700f97eae7SMatthias Ringwald // hang... 22710f97eae7SMatthias Ringwald // 22720f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 22730f97eae7SMatthias Ringwald if (!command_completed 2274a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 22750e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 22760f97eae7SMatthias Ringwald 2277f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 22780f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 22790f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 22800f97eae7SMatthias Ringwald return; 22810f97eae7SMatthias Ringwald } 22820f97eae7SMatthias Ringwald } 22830f97eae7SMatthias Ringwald 22849f007422SMatthias Ringwald // CSR & H5 22859f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 22869f007422SMatthias Ringwald if (!command_completed 2287a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 22880e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 22899f007422SMatthias Ringwald 22909f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 22919f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 22929f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 22939f007422SMatthias Ringwald return; 22949f007422SMatthias Ringwald } 22959f007422SMatthias Ringwald } 22969f007422SMatthias Ringwald 22979f007422SMatthias 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 22989f007422SMatthias Ringwald // fix: Correct substate and behave as command below 22999f007422SMatthias Ringwald if (command_completed){ 23009f007422SMatthias Ringwald switch (hci_stack->substate){ 23019f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 23029f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 23039f007422SMatthias Ringwald break; 23049f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 23059f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 23069f007422SMatthias Ringwald break; 23079f007422SMatthias Ringwald default: 23089f007422SMatthias Ringwald break; 23099f007422SMatthias Ringwald } 23109f007422SMatthias Ringwald } 23110f97eae7SMatthias Ringwald 231206b9e820SMatthias Ringwald #endif 23130f97eae7SMatthias Ringwald 2314a2481739S[email protected] if (!command_completed) return; 2315a2481739S[email protected] 231607fd2f31SMatthias Ringwald bool need_baud_change = false; 231707fd2f31SMatthias Ringwald bool need_addr_change = false; 231806b9e820SMatthias Ringwald 23196fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 232006b9e820SMatthias Ringwald need_baud_change = hci_stack->config 23213fb36a29SMatthias Ringwald && hci_stack->chipset 23223fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 2323db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 23249796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 2325db8bc6ffSMatthias Ringwald 232606b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 23273fb36a29SMatthias Ringwald && hci_stack->chipset 23283fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 232906b9e820SMatthias Ringwald #endif 2330a80162e9SMatthias Ringwald 23315c363727SMatthias Ringwald switch(hci_stack->substate){ 233206b9e820SMatthias Ringwald 23336fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 23349f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 2335d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 23369f007422SMatthias Ringwald // fix: just correct substate and behave as command below 2337f4c579d4SMatthias Ringwald 2338f4c579d4SMatthias Ringwald /* fall through */ 2339f4c579d4SMatthias Ringwald #endif 2340f4c579d4SMatthias Ringwald 234174b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 2342528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2343f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 23442f48d920SMatthias Ringwald return; 2345f4c579d4SMatthias Ringwald 2346f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2347a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 23484696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 2349fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 235061f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 235196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2352cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 2353fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 23544696bddbSMatthias Ringwald } 235574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 235674b323a9SMatthias Ringwald return; 2357a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 2358528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2359a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 2360a80162e9SMatthias Ringwald return; 236174b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 236274b323a9SMatthias Ringwald // repeat custom init 236374b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 236474b323a9SMatthias Ringwald return; 236506b9e820SMatthias Ringwald #endif 236606b9e820SMatthias Ringwald 2367a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 23680e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 2369efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 2370efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 2371eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 2372eb3a5314SMatthias Ringwald return; 2373eb3a5314SMatthias Ringwald } 237453860077SMatthias Ringwald if (need_addr_change){ 237553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 237653860077SMatthias Ringwald return; 237753860077SMatthias Ringwald } 237853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 237953860077SMatthias Ringwald return; 23806fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 23817224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 23827224be7eSMatthias Ringwald if (need_baud_change){ 238396b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2384cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 2385fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 23867224be7eSMatthias Ringwald } 2387eb3a5314SMatthias Ringwald if (need_addr_change){ 2388eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 2389eb3a5314SMatthias Ringwald return; 2390eb3a5314SMatthias Ringwald } 2391eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 2392eb3a5314SMatthias Ringwald return; 239353860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 23946ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 23956ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 23966ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 239753860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 239853860077SMatthias Ringwald return; 239953860077SMatthias Ringwald } 240053860077SMatthias Ringwald // skipping st warm boot 240153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 240253860077SMatthias Ringwald return; 240353860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 240453860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 240553860077SMatthias Ringwald return; 240606b9e820SMatthias Ringwald #endif 2407e7c662faSMatthias Ringwald 2408f795c86eSMatthias Ringwald case HCI_INIT_DONE: 2409eb8d95caSMatthias Ringwald // set state if we came here by fall through 2410eb8d95caSMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 2411f795c86eSMatthias Ringwald return; 2412a650ba4dSMatthias Ringwald 24136155b3d3S[email protected] default: 241474b323a9SMatthias Ringwald break; 24156155b3d3S[email protected] } 241655975f88SMatthias Ringwald hci_initializing_next_state(); 24176155b3d3S[email protected] } 24186155b3d3S[email protected] 24190bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 24203bb5ff27SMatthias Ringwald // CC2564C might emit Connection Complete for rejected incoming SCO connection 24211a4fdac4SMatthias Ringwald // To prevent accidentally free'ing the HCI connection for the ACL connection, 24221a4fdac4SMatthias Ringwald // check if we have been aware of the HCI connection 24231f34df2cSMatthias Ringwald switch (conn->state){ 24241a4fdac4SMatthias Ringwald case SENT_CREATE_CONNECTION: 24251f34df2cSMatthias Ringwald case RECEIVED_CONNECTION_REQUEST: 24261f34df2cSMatthias Ringwald break; 24271f34df2cSMatthias Ringwald default: 24281f34df2cSMatthias Ringwald return; 24291f34df2cSMatthias Ringwald } 24303bb5ff27SMatthias Ringwald 2431229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 24320bbba85bSMatthias Ringwald bd_addr_t bd_address; 24336535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 24340bbba85bSMatthias Ringwald 24356bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 24366bc9fa5eSMatthias Ringwald // cache needed data 24376bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 24386bc9fa5eSMatthias Ringwald #endif 24396bc9fa5eSMatthias Ringwald 24400bbba85bSMatthias Ringwald // connection failed, remove entry 24410bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 24420bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 24430bbba85bSMatthias Ringwald 24446bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 24450bbba85bSMatthias Ringwald // notify client if dedicated bonding 24460bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 24470bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 24480bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 24490bbba85bSMatthias Ringwald } 24500bbba85bSMatthias Ringwald 24510bbba85bSMatthias Ringwald // if authentication error, also delete link key 24520bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 24530bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 24540bbba85bSMatthias Ringwald } 24551c79b5e3SMatthias Ringwald #else 24561c79b5e3SMatthias Ringwald UNUSED(status); 24576bc9fa5eSMatthias Ringwald #endif 24580bbba85bSMatthias Ringwald } 24590bbba85bSMatthias Ringwald 2460be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 24612f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 24622f5c44baSMatthias Ringwald // SSP Controller 24632f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 24642f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 24652f5c44baSMatthias Ringwald } 24662f5c44baSMatthias Ringwald // eSCO 24672f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 24682f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 24692f5c44baSMatthias Ringwald } 24702f5c44baSMatthias Ringwald // Extended features 24712f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 24722f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 24732f5c44baSMatthias Ringwald } 24742f5c44baSMatthias Ringwald } 24752f5c44baSMatthias Ringwald 24762f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 24772f5c44baSMatthias Ringwald // SSP Host 24782f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 24792f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 24802f5c44baSMatthias Ringwald } 248150c51a77SMatthias Ringwald // SC Host 248250c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 248350c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 248450c51a77SMatthias Ringwald } 248550c51a77SMatthias Ringwald } 248650c51a77SMatthias Ringwald 248750c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 248850c51a77SMatthias Ringwald // SC Controller 248950c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 249050c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 249150c51a77SMatthias Ringwald } 24922f5c44baSMatthias Ringwald } 24932f5c44baSMatthias Ringwald 2494de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2495461a2bc4SMatthias Ringwald conn->bonding_flags &= ~BONDING_REMOTE_FEATURES_QUERY_ACTIVE; 2496de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 249749bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2498de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2499de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2500de0df013SMatthias Ringwald } 2501de0df013SMatthias Ringwald } 2502128825c3SMatthias Ringwald static bool hci_remote_sc_enabled(hci_connection_t * connection){ 2503128825c3SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 2504128825c3SMatthias Ringwald return (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 2505128825c3SMatthias Ringwald } 2506128825c3SMatthias Ringwald 2507be500194SMatthias Ringwald #endif 2508de0df013SMatthias Ringwald 250967c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 251067c6c9dcSMatthias Ringwald // handle BT initialization 251167c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 251267c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 251367c6c9dcSMatthias Ringwald } 251467c6c9dcSMatthias Ringwald 251567c6c9dcSMatthias Ringwald // help with BT sleep 251667c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 251767c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 251872a2585aSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 251972a2585aSMatthias Ringwald && (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_WRITE_SCAN_ENABLE)){ 252067c6c9dcSMatthias Ringwald hci_initializing_next_state(); 252167c6c9dcSMatthias Ringwald } 252267c6c9dcSMatthias Ringwald } 252367c6c9dcSMatthias Ringwald 25249866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 25259866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 25268daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 25279866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2528de9f0299SMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_connection(conn); 2529de9f0299SMatthias Ringwald 2530de9f0299SMatthias Ringwald // trigger disconnect for dedicated bonding, skip emit security level as disconnect is pending 2531de9f0299SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 2532de9f0299SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 2533de9f0299SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 2534de9f0299SMatthias Ringwald conn->bonding_status = security_level == 0 ? ERROR_CODE_INSUFFICIENT_SECURITY : ERROR_CODE_SUCCESS; 2535de9f0299SMatthias Ringwald return; 2536de9f0299SMatthias Ringwald } 2537abdad579SMatthias Ringwald 25388daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) != 0) { 25391cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 2540de9f0299SMatthias Ringwald hci_emit_security_level(conn->con_handle, security_level); 2541abdad579SMatthias Ringwald return; 2542abdad579SMatthias Ringwald } 2543abdad579SMatthias Ringwald 2544d54424c3SMatthias Ringwald // Request remote features if not already done 2545dbe0d85cSMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 2546d54424c3SMatthias Ringwald 2547abdad579SMatthias Ringwald // Request Authentication if not already done 2548abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2549abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 25509866fdc7SMatthias Ringwald } 25519866fdc7SMatthias Ringwald #endif 25529866fdc7SMatthias Ringwald 25533b631737SMatthias Ringwald static void hci_store_local_supported_commands(const uint8_t * packet){ 255425e46151SMatthias Ringwald // create mapping table 255525e46151SMatthias Ringwald #define X(name, offset, bit) { offset, bit }, 255625e46151SMatthias Ringwald static struct { 255725e46151SMatthias Ringwald uint8_t byte_offset; 255825e46151SMatthias Ringwald uint8_t bit_position; 255925e46151SMatthias Ringwald } supported_hci_commands_map [] = { 256025e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 256125e46151SMatthias Ringwald }; 256225e46151SMatthias Ringwald #undef X 256325e46151SMatthias Ringwald 256425e46151SMatthias Ringwald // create names for debug purposes 256525e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 256625e46151SMatthias Ringwald #define X(name, offset, bit) #name, 256725e46151SMatthias Ringwald static const char * command_names[] = { 256825e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 256925e46151SMatthias Ringwald }; 257025e46151SMatthias Ringwald #undef X 257125e46151SMatthias Ringwald #endif 257225e46151SMatthias Ringwald 25731ffa425cSMatthias Ringwald hci_stack->local_supported_commands = 0; 257425e46151SMatthias Ringwald const uint8_t * commands_map = &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1]; 257525e46151SMatthias Ringwald uint16_t i; 257625e46151SMatthias Ringwald for (i = 0 ; i < SUPPORTED_HCI_COMMANDS_COUNT ; i++){ 257725e46151SMatthias Ringwald if ((commands_map[supported_hci_commands_map[i].byte_offset] & (1 << supported_hci_commands_map[i].bit_position)) != 0){ 257825e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 257925e46151SMatthias 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); 258025e46151SMatthias Ringwald #else 25811ffa425cSMatthias Ringwald log_info("Command 0x%02x supported %u/%u", i, supported_hci_commands_map[i].byte_offset, supported_hci_commands_map[i].bit_position); 258225e46151SMatthias Ringwald #endif 25831ffa425cSMatthias Ringwald hci_stack->local_supported_commands |= (1LU << i); 258425e46151SMatthias Ringwald } 258525e46151SMatthias Ringwald } 25861ffa425cSMatthias Ringwald log_info("Local supported commands summary %04x", hci_stack->local_supported_commands); 25873b631737SMatthias Ringwald } 25883b631737SMatthias Ringwald 25894f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2590b08371a9SMilanka Ringwald UNUSED(size); 2591e76a89eeS[email protected] 25929cbd2215SMatthias Ringwald uint16_t manufacturer; 25939cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2594fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 25951f7b95a1Smatthias.ringwald hci_connection_t * conn; 2596c7a108afSMatthias Ringwald #endif 2597c7a108afSMatthias Ringwald #if defined(ENABLE_CLASSIC) || (defined(ENABLE_BLE) && defined(ENABLE_LE_ISOCHRONOUS_STREAMS)) 2598645b7c25SMatthias Ringwald uint8_t status; 25999cbd2215SMatthias Ringwald #endif 26006bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 26016bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 26027ec5eeaaSmatthias.ringwald 2603645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2604645b7c25SMatthias Ringwald switch (opcode){ 2605645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2606645b7c25SMatthias Ringwald if (packet[5]) break; 26079e263bd7SMatthias Ringwald // terminate, name 248 chars 26089e263bd7SMatthias Ringwald packet[6+248] = 0; 26099e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2610645b7c25SMatthias Ringwald break; 2611645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 26121d279b20Smatthias.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" 2613c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2614429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2615429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2616429122ccSMatthias Ringwald 2617429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2618429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2619429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2620429122ccSMatthias Ringwald 2621b1c141dbSMatthias Ringwald hci_stack->acl_packets_total_num = (uint8_t) little_endian_read_16(packet, 9); 2622b1c141dbSMatthias Ringwald hci_stack->sco_packets_total_num = (uint8_t) little_endian_read_16(packet, 11); 2623a8b12447S[email protected] 2624429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2625429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 26261a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2627c3b46f5aSMatthias Ringwald } 2628645b7c25SMatthias Ringwald break; 2629645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2630645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2631891b9fc2SMatthias Ringwald uint8_t event[5]; 2632891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2633891b9fc2SMatthias Ringwald event[1] = 3; 26346535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2635891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2636891b9fc2SMatthias Ringwald } 2637645b7c25SMatthias Ringwald break; 2638a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 263979b7ea2dSMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE_V2: 264079b7ea2dSMatthias Ringwald hci_stack->le_iso_packets_length = little_endian_read_16(packet, 9); 264179b7ea2dSMatthias Ringwald hci_stack->le_iso_packets_total_num = packet[11]; 264279b7ea2dSMatthias Ringwald log_info("hci_le_read_buffer_size_v2: iso size %u, iso count %u", 264379b7ea2dSMatthias Ringwald hci_stack->le_iso_packets_length, hci_stack->le_iso_packets_total_num); 264479b7ea2dSMatthias Ringwald 264579b7ea2dSMatthias Ringwald /* fall through */ 264679b7ea2dSMatthias Ringwald 2647645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2648f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2649ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 26506c26b087S[email protected] // determine usable ACL payload size 26516c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 26526c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 26536c26b087S[email protected] } 265479b7ea2dSMatthias Ringwald log_info("hci_le_read_buffer_size: acl size %u, acl count %u", hci_stack->le_data_packets_length, hci_stack->le_acl_packets_total_num); 2655645b7c25SMatthias Ringwald break; 2656b435e062SMatthias Ringwald #endif 2657b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2658645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2659dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2660dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2661dcd678baSMatthias 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); 2662645b7c25SMatthias Ringwald break; 2663b435e062SMatthias Ringwald #endif 2664d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2665645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2666e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 266715d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2668645b7c25SMatthias Ringwald break; 266965a46ef3S[email protected] #endif 26703a74541eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 26713a74541eSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 26723a74541eSMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH: 26733a74541eSMatthias Ringwald hci_stack->le_maximum_advertising_data_length = little_endian_read_16(packet, 6); 26743a74541eSMatthias Ringwald break; 267525df6c61SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_EXTENDED_ADVERTISING_PARAMETERS: 267625df6c61SMatthias Ringwald if (hci_stack->le_advertising_set_in_current_command != 0) { 267725df6c61SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(hci_stack->le_advertising_set_in_current_command); 267825df6c61SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 267925df6c61SMatthias Ringwald if (advertising_set == NULL) break; 268025df6c61SMatthias Ringwald uint8_t adv_status = packet[6]; 268125df6c61SMatthias Ringwald uint8_t tx_power = packet[7]; 268225df6c61SMatthias Ringwald uint8_t event[] = { HCI_EVENT_META_GAP, 4, GAP_SUBEVENT_ADVERTISING_SET_INSTALLED, hci_stack->le_advertising_set_in_current_command, adv_status, tx_power }; 268325df6c61SMatthias Ringwald if (adv_status == 0){ 268425df6c61SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PARAMS_SET; 268525df6c61SMatthias Ringwald } 268625df6c61SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 268725df6c61SMatthias Ringwald } 268825df6c61SMatthias Ringwald break; 268925df6c61SMatthias Ringwald case HCI_OPCODE_HCI_LE_REMOVE_ADVERTISING_SET: 269025df6c61SMatthias Ringwald if (hci_stack->le_advertising_set_in_current_command != 0) { 269125df6c61SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(hci_stack->le_advertising_set_in_current_command); 269225df6c61SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 269325df6c61SMatthias Ringwald if (advertising_set == NULL) break; 269425df6c61SMatthias Ringwald uint8_t adv_status = packet[5]; 269525df6c61SMatthias Ringwald uint8_t event[] = { HCI_EVENT_META_GAP, 3, GAP_SUBEVENT_ADVERTISING_SET_REMOVED, hci_stack->le_advertising_set_in_current_command, adv_status }; 269625df6c61SMatthias Ringwald if (adv_status == 0){ 269725df6c61SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_advertising_sets, (btstack_linked_item_t *) advertising_set); 269825df6c61SMatthias Ringwald } 269925df6c61SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 270025df6c61SMatthias Ringwald } 270125df6c61SMatthias Ringwald break; 27023a74541eSMatthias Ringwald #endif 27033a74541eSMatthias Ringwald #endif 2704645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2705645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2706645b7c25SMatthias 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)); 270733373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 27081624665aSMatthias Ringwald if (hci_stack->link_key_db){ 27091624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 27101624665aSMatthias Ringwald } 271133373e40SMatthias Ringwald #endif 2712645b7c25SMatthias Ringwald break; 271335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2714645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 271571fd255dSMatthias Ringwald hci_emit_scan_mode_changed(hci_stack->discoverable, hci_stack->connectable); 2716645b7c25SMatthias Ringwald break; 27173d1f2d24SMatthias Ringwald case HCI_OPCODE_HCI_PERIODIC_INQUIRY_MODE: 27183d1f2d24SMatthias Ringwald status = hci_event_command_complete_get_return_parameters(packet)[0]; 27193d1f2d24SMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 27203d1f2d24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_PERIODIC; 27213d1f2d24SMatthias Ringwald } else { 27223d1f2d24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 27233d1f2d24SMatthias Ringwald } 27243d1f2d24SMatthias Ringwald break; 2725645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 272630199e24SMatthias Ringwald case HCI_OPCODE_HCI_EXIT_PERIODIC_INQUIRY_MODE: 2727f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2728f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2729f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2730f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2731f5875de5SMatthias Ringwald } 2732645b7c25SMatthias Ringwald break; 273335454696SMatthias Ringwald #endif 2734645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 27354f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 273665389bfcS[email protected] 273735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2738a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2739a5a23fc2S[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]); 27408b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 274135454696SMatthias Ringwald #endif 2742f5d8d141S[email protected] // Classic/LE 2743f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2744645b7c25SMatthias Ringwald break; 2745645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2746645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2747c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2748c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2749c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2750c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2751c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2752c21d89f3SMatthias Ringwald } 2753c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 27544696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2755645b7c25SMatthias Ringwald break; 2756645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 27573b631737SMatthias Ringwald hci_store_local_supported_commands(packet); 2758645b7c25SMatthias Ringwald break; 2759e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2760645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 27614f781026SMatthias Ringwald if (packet[5]) return; 27625b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2763645b7c25SMatthias Ringwald break; 2764645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2765645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2766573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2767573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2768c3b46f5aSMatthias Ringwald if (conn != NULL) { 27699866fdc7SMatthias Ringwald uint8_t key_size = 0; 2770573897a0SMatthias Ringwald if (status == 0){ 27719866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 27724a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2773170fafaeSMatthias Ringwald } else { 2774e9f98c4aSMatthias Ringwald key_size = 1; 27759866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2776573897a0SMatthias Ringwald } 27779866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2778c3b46f5aSMatthias Ringwald } 2779645b7c25SMatthias Ringwald break; 2780cc15bb2cSMatthias Ringwald // assert pairing complete event is emitted. 2781cc15bb2cSMatthias Ringwald // note: for SSP, Simple Pairing Complete Event is sufficient, but we want to be more robust 2782cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_NEGATIVE_REPLY: 2783cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_NEGATIVE_REPLY: 2784cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: 27858cc1507eSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 2786cc15bb2cSMatthias Ringwald // lookup connection by gap pairing addr 2787cc15bb2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->gap_pairing_addr, BD_ADDR_TYPE_ACL); 2788cc15bb2cSMatthias Ringwald if (conn == NULL) break; 2789cc15bb2cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 2790cc15bb2cSMatthias Ringwald break; 2791cc15bb2cSMatthias Ringwald 279275a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 279375a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 279475a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 279575a8e4faSMatthias Ringwald uint8_t event[67]; 279675a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 279775a8e4faSMatthias Ringwald event[1] = 65; 279875a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 279975a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 280075a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 280175a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 280275a8e4faSMatthias Ringwald event[2] = 3; 280375a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 280475a8e4faSMatthias Ringwald } else { 280575a8e4faSMatthias Ringwald event[2] = 1; 280675a8e4faSMatthias Ringwald } 280775a8e4faSMatthias Ringwald } 280875a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 280975a8e4faSMatthias Ringwald break; 281075a8e4faSMatthias Ringwald } 28111ae74bf3SMatthias Ringwald 28121ae74bf3SMatthias Ringwald // note: only needed if user does not provide OOB data 28131ae74bf3SMatthias Ringwald case HCI_OPCODE_HCI_REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: 28141ae74bf3SMatthias Ringwald conn = hci_connection_for_handle(hci_stack->classic_oob_con_handle); 28151ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 28161ae74bf3SMatthias Ringwald if (conn == NULL) break; 28171ae74bf3SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 28181ae74bf3SMatthias Ringwald break; 2819e8c8828eSMatthias Ringwald #endif 282075a8e4faSMatthias Ringwald #endif 28214ae8623eSMatthias Ringwald #ifdef ENABLE_BLE 28224ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 28234ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 28244ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 28254ae8623eSMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 28264ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2827a90016deSMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 28284ae8623eSMatthias Ringwald } 28294ae8623eSMatthias Ringwald break; 28307aa35f6aSMatthias Ringwald case HCI_OPCODE_HCI_LE_SETUP_ISO_DATA_PATH: { 28317aa35f6aSMatthias Ringwald // lookup BIG by state 28327aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 28337aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 28347aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 28357aa35f6aSMatthias Ringwald le_audio_big_t *big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 28367aa35f6aSMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH){ 28377aa35f6aSMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 28387aa35f6aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 28397aa35f6aSMatthias Ringwald big->state_vars.next_bis++; 28407aa35f6aSMatthias Ringwald if (big->state_vars.next_bis == big->num_bis){ 28417aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_ACTIVE; 28427aa35f6aSMatthias Ringwald hci_emit_big_created(big, ERROR_CODE_SUCCESS); 28437aa35f6aSMatthias Ringwald } else { 28447aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 28457aa35f6aSMatthias Ringwald } 28467aa35f6aSMatthias Ringwald } else { 28477aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED; 28487aa35f6aSMatthias Ringwald big->state_vars.status = status; 28492a6c0f82SMatthias Ringwald } 28502a6c0f82SMatthias Ringwald return; 28512a6c0f82SMatthias Ringwald } 28522a6c0f82SMatthias Ringwald } 28532a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 28542a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 28552a6c0f82SMatthias Ringwald le_audio_big_sync_t *big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 28562a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH){ 28572a6c0f82SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 28582a6c0f82SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 28592a6c0f82SMatthias Ringwald big_sync->state_vars.next_bis++; 28602a6c0f82SMatthias Ringwald if (big_sync->state_vars.next_bis == big_sync->num_bis){ 28612a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_ACTIVE; 28622a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, ERROR_CODE_SUCCESS); 28632a6c0f82SMatthias Ringwald } else { 28642a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 28652a6c0f82SMatthias Ringwald } 28662a6c0f82SMatthias Ringwald } else { 28672a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED; 28682a6c0f82SMatthias Ringwald big_sync->state_vars.status = status; 28692a6c0f82SMatthias Ringwald } 28702a6c0f82SMatthias Ringwald return; 28712a6c0f82SMatthias Ringwald } 28722a6c0f82SMatthias Ringwald } 28732a6c0f82SMatthias Ringwald break; 28742a6c0f82SMatthias Ringwald } 28752a6c0f82SMatthias Ringwald case HCI_OPCODE_HCI_LE_BIG_TERMINATE_SYNC: { 28762a6c0f82SMatthias Ringwald // lookup BIG by state 28772a6c0f82SMatthias Ringwald btstack_linked_list_iterator_t it; 28782a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 28792a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 28802a6c0f82SMatthias Ringwald le_audio_big_sync_t *big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 2881d04a9afcSMatthias Ringwald uint8_t big_handle = big_sync->big_handle; 28822a6c0f82SMatthias Ringwald switch (big_sync->state){ 28832a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED: 2884d04a9afcSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 28852a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, big_sync->state_vars.status); 2886d04a9afcSMatthias Ringwald return; 28872a6c0f82SMatthias Ringwald default: 2888d04a9afcSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 2889d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 28907aa35f6aSMatthias Ringwald return; 28917aa35f6aSMatthias Ringwald } 28927aa35f6aSMatthias Ringwald } 28937aa35f6aSMatthias Ringwald break; 28947aa35f6aSMatthias Ringwald } 28954ae8623eSMatthias Ringwald #endif 28964ae8623eSMatthias Ringwald #endif 28976f35bb46SMatthias Ringwald default: 28986f35bb46SMatthias Ringwald break; 28994f781026SMatthias Ringwald } 2900645b7c25SMatthias Ringwald } 29014f781026SMatthias Ringwald 2902afbf1cd7SMatthias Ringwald static void handle_command_status_event(uint8_t * packet, uint16_t size) { 2903afbf1cd7SMatthias Ringwald UNUSED(size); 2904afbf1cd7SMatthias Ringwald 2905afbf1cd7SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 2906afbf1cd7SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2907afbf1cd7SMatthias Ringwald 2908c70df5f8SMatthias Ringwald // get opcode and command status 2909c70df5f8SMatthias Ringwald uint16_t opcode = hci_event_command_status_get_command_opcode(packet); 2910c70df5f8SMatthias Ringwald 2911c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) || defined(ENABLE_LE_ISOCHRONOUS_STREAMS) 2912c70df5f8SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2913c70df5f8SMatthias Ringwald #endif 2914c70df5f8SMatthias Ringwald 2915c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) 2916c70df5f8SMatthias Ringwald bd_addr_type_t addr_type; 2917c70df5f8SMatthias Ringwald #endif 2918c70df5f8SMatthias Ringwald 2919c70df5f8SMatthias Ringwald switch (opcode){ 2920afbf1cd7SMatthias Ringwald #ifdef ENABLE_CLASSIC 2921c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 2922c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 2923afbf1cd7SMatthias Ringwald #endif 2924afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2925c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 2926afbf1cd7SMatthias Ringwald #endif 2927c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) 2928afbf1cd7SMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 2929afbf1cd7SMatthias Ringwald 2930afbf1cd7SMatthias Ringwald // reset outgoing address info 2931afbf1cd7SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2932afbf1cd7SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2933afbf1cd7SMatthias Ringwald 2934afbf1cd7SMatthias Ringwald // on error 2935afbf1cd7SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2936afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2937afbf1cd7SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 2938afbf1cd7SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2939afbf1cd7SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 2940afbf1cd7SMatthias Ringwald } 2941afbf1cd7SMatthias Ringwald #endif 2942afbf1cd7SMatthias Ringwald // error => outgoing connection failed 2943c70df5f8SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 2944afbf1cd7SMatthias Ringwald if (conn != NULL){ 2945afbf1cd7SMatthias Ringwald hci_handle_connection_failed(conn, status); 2946afbf1cd7SMatthias Ringwald } 2947afbf1cd7SMatthias Ringwald } 2948c70df5f8SMatthias Ringwald break; 2949c70df5f8SMatthias Ringwald #endif 2950afbf1cd7SMatthias Ringwald #ifdef ENABLE_CLASSIC 2951c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY: 2952afbf1cd7SMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 2953afbf1cd7SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 2954afbf1cd7SMatthias Ringwald } else { 2955afbf1cd7SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2956afbf1cd7SMatthias Ringwald } 2957c70df5f8SMatthias Ringwald break; 2958c70df5f8SMatthias Ringwald #endif 2959afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 2960c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 2961c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 2962afbf1cd7SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 2963a90016deSMatthias Ringwald hci_iso_stream_requested_confirm(0xff); 2964afbf1cd7SMatthias Ringwald } else { 2965a90016deSMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 2966afbf1cd7SMatthias Ringwald } 2967c70df5f8SMatthias Ringwald break; 2968afbf1cd7SMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 2969c70df5f8SMatthias Ringwald default: 2970c70df5f8SMatthias Ringwald break; 2971c70df5f8SMatthias Ringwald } 2972afbf1cd7SMatthias Ringwald } 2973afbf1cd7SMatthias Ringwald 29740ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 29750ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 29760ce3f217SMatthias Ringwald bd_addr_t addr; 29770ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 29780ce3f217SMatthias Ringwald hci_connection_t * conn; 29790ce3f217SMatthias Ringwald 29800ce3f217SMatthias Ringwald // Connection management 29810ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 29820ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 29830ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 29840ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 29850ce3f217SMatthias Ringwald 29860ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 29870ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 29880ce3f217SMatthias Ringwald if (packet[3]){ 29890ce3f217SMatthias Ringwald 29900ce3f217SMatthias Ringwald // handle cancelled outgoing connection 29910ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 29920ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 29930ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 29940ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 29951f468175SMatthias Ringwald // reset state 29960ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 29971f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 29980ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 29990ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 30000ce3f217SMatthias Ringwald } 30010ce3f217SMatthias Ringwald 30020ce3f217SMatthias Ringwald // outgoing le connection establishment is done 30030ce3f217SMatthias Ringwald if (conn){ 30040ce3f217SMatthias Ringwald // remove entry 30050ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 30060ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 30070ce3f217SMatthias Ringwald } 30080ce3f217SMatthias Ringwald return; 30090ce3f217SMatthias Ringwald } 30100ce3f217SMatthias Ringwald #endif 30110ce3f217SMatthias Ringwald 30120ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 30130ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 30140ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30150ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 30160ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 30170ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 30180ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 30190ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 30200ce3f217SMatthias Ringwald } 30210ce3f217SMatthias Ringwald #endif 30220ce3f217SMatthias Ringwald } else { 30230ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 30240ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 3025935aa76eSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 30260ce3f217SMatthias Ringwald #endif 30270ce3f217SMatthias Ringwald } 30280ce3f217SMatthias Ringwald 30290ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 30300ce3f217SMatthias Ringwald if (!conn){ 30310ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 30320ce3f217SMatthias Ringwald } 30330ce3f217SMatthias Ringwald 30340ce3f217SMatthias Ringwald // no memory, sorry. 30350ce3f217SMatthias Ringwald if (!conn){ 30360ce3f217SMatthias Ringwald return; 30370ce3f217SMatthias Ringwald } 30380ce3f217SMatthias Ringwald 30390ce3f217SMatthias Ringwald conn->state = OPEN; 30400ce3f217SMatthias Ringwald conn->role = packet[6]; 30410ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 30420ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 30430ce3f217SMatthias Ringwald 30440ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 30450ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 30460ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 30470ce3f217SMatthias Ringwald } 30480ce3f217SMatthias Ringwald #endif 30490ce3f217SMatthias Ringwald 3050dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 3051dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 3052dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 3053dde9ff1eSMatthias Ringwald 30540ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 30550ce3f217SMatthias Ringwald 30560ce3f217SMatthias Ringwald // restart timer 30570ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 30580ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 30590ce3f217SMatthias Ringwald 30600ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 30610ce3f217SMatthias Ringwald 30620ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 30630ce3f217SMatthias Ringwald } 30640ce3f217SMatthias Ringwald #endif 30650ce3f217SMatthias Ringwald 306617c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 306717c6fe5cSMatthias 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){ 306817c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 306917c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 30709a8f78c1SMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities if Authenticated is possible 30719a8f78c1SMatthias Ringwald // @see: Core Spec v5.3, Vol 3, Part C, Table 5.7 307217c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 30739a8f78c1SMatthias Ringwald // MITM not possible without keyboard or display 307417c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 307517c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 30769a8f78c1SMatthias Ringwald 30779a8f78c1SMatthias Ringwald // MITM possible if one side has keyboard and the other has keyboard or display 30789a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 30799a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 30809a8f78c1SMatthias Ringwald 30819a8f78c1SMatthias Ringwald // MITM not possible if one side has only display and other side has no keyboard 30829a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 30839a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 308417c6fe5cSMatthias Ringwald } 308517c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 308617c6fe5cSMatthias Ringwald return true; 308717c6fe5cSMatthias Ringwald } 30883817f9dfSMatthias Ringwald 30893817f9dfSMatthias Ringwald static bool btstack_is_null(uint8_t * data, uint16_t size){ 30903817f9dfSMatthias Ringwald uint16_t i; 30913817f9dfSMatthias Ringwald for (i=0; i < size ; i++){ 30923817f9dfSMatthias Ringwald if (data[i] != 0) { 30933817f9dfSMatthias Ringwald return false; 30943817f9dfSMatthias Ringwald } 30953817f9dfSMatthias Ringwald } 30963817f9dfSMatthias Ringwald return true; 30973817f9dfSMatthias Ringwald } 30983817f9dfSMatthias Ringwald 3099b3c4163bSMatthias Ringwald static void hci_ssp_assess_security_on_io_cap_request(hci_connection_t * conn){ 31002dd8985bSMatthias Ringwald // get requested security level 31012dd8985bSMatthias Ringwald gap_security_level_t requested_security_level = conn->requested_security_level; 31022dd8985bSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 31032dd8985bSMatthias Ringwald requested_security_level = LEVEL_4; 31042dd8985bSMatthias Ringwald } 31052dd8985bSMatthias Ringwald 3106b3c4163bSMatthias Ringwald // assess security: LEVEL 4 requires SC 31072dd8985bSMatthias Ringwald // skip this preliminary test if remote features are not available yet to work around potential issue in ESP32 controller 31082dd8985bSMatthias Ringwald if ((requested_security_level == LEVEL_4) && 31092dd8985bSMatthias Ringwald ((conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0) && 31102dd8985bSMatthias Ringwald !hci_remote_sc_enabled(conn)){ 3111b3c4163bSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 3112b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3113b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3114b3c4163bSMatthias Ringwald return; 3115b3c4163bSMatthias Ringwald } 3116b3c4163bSMatthias Ringwald 3117b3c4163bSMatthias Ringwald // assess security based on io capabilities 3118b3c4163bSMatthias Ringwald if (conn->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 3119b3c4163bSMatthias Ringwald // responder: fully validate io caps of both sides as well as OOB data 3120b3c4163bSMatthias Ringwald bool security_possible = false; 3121b3c4163bSMatthias Ringwald security_possible = hci_ssp_security_level_possible_for_io_cap(requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io); 3122b3c4163bSMatthias Ringwald 3123b3c4163bSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3124b3c4163bSMatthias Ringwald // We assume that both Controller can reach LEVEL 4, if one side has received P-192 and the other has received P-256, 3125b3c4163bSMatthias Ringwald // so we merge the OOB data availability 3126b3c4163bSMatthias Ringwald uint8_t have_oob_data = conn->io_cap_response_oob_data; 3127b3c4163bSMatthias Ringwald if (conn->classic_oob_c_192 != NULL){ 3128b3c4163bSMatthias Ringwald have_oob_data |= 1; 3129b3c4163bSMatthias Ringwald } 3130b3c4163bSMatthias Ringwald if (conn->classic_oob_c_256 != NULL){ 3131b3c4163bSMatthias Ringwald have_oob_data |= 2; 3132b3c4163bSMatthias Ringwald } 3133b3c4163bSMatthias Ringwald // for up to Level 3, either P-192 as well as P-256 will do 3134b3c4163bSMatthias 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 3135b3c4163bSMatthias Ringwald // if remote does not SC, we should not receive P-256 data either 3136b3c4163bSMatthias Ringwald if ((requested_security_level <= LEVEL_3) && (have_oob_data != 0)){ 3137b3c4163bSMatthias Ringwald security_possible = true; 3138b3c4163bSMatthias Ringwald } 3139b3c4163bSMatthias Ringwald // for Level 4, P-256 is needed 3140b3c4163bSMatthias Ringwald if ((requested_security_level == LEVEL_4 && ((have_oob_data & 2) != 0))){ 3141b3c4163bSMatthias Ringwald security_possible = true; 3142b3c4163bSMatthias Ringwald } 3143b3c4163bSMatthias Ringwald #endif 3144b3c4163bSMatthias Ringwald 3145b3c4163bSMatthias Ringwald if (security_possible == false){ 31462dd8985bSMatthias Ringwald log_info("IOCap/OOB insufficient for level %u -> abort", requested_security_level); 3147b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3148b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3149b3c4163bSMatthias Ringwald return; 3150b3c4163bSMatthias Ringwald } 3151b3c4163bSMatthias Ringwald } else { 3152b3c4163bSMatthias Ringwald // initiator: remote io cap not yet, only check if we have ability for MITM protection if requested and OOB is not supported 31531fe968f5SMatthias Ringwald #ifndef ENABLE_CLASSIC_PAIRING_OOB 31541fe968f5SMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 3155b3c4163bSMatthias Ringwald if ((conn->requested_security_level >= LEVEL_3) && (hci_stack->ssp_io_capability >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT)){ 3156b3c4163bSMatthias Ringwald log_info("Level 3+ required, but no input/output -> abort"); 3157b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3158b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3159b3c4163bSMatthias Ringwald return; 3160b3c4163bSMatthias Ringwald } 3161b3c4163bSMatthias Ringwald #endif 31621fe968f5SMatthias Ringwald #endif 3163b3c4163bSMatthias Ringwald } 3164b3c4163bSMatthias Ringwald 3165b3c4163bSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 3166b3c4163bSMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 3167b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 3168b3c4163bSMatthias Ringwald } else { 3169b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3170b3c4163bSMatthias Ringwald } 3171b3c4163bSMatthias Ringwald #endif 3172b3c4163bSMatthias Ringwald } 3173b3c4163bSMatthias Ringwald 317417c6fe5cSMatthias Ringwald #endif 317517c6fe5cSMatthias Ringwald 3176c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 31774f781026SMatthias Ringwald 31784f781026SMatthias Ringwald uint16_t event_length = packet[1]; 31794f781026SMatthias Ringwald 31804f781026SMatthias Ringwald // assert packet is complete 31814ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 31824f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 31834f781026SMatthias Ringwald return; 31844f781026SMatthias Ringwald } 31854f781026SMatthias Ringwald 31864f781026SMatthias Ringwald hci_con_handle_t handle; 31874f781026SMatthias Ringwald hci_connection_t * conn; 31884f781026SMatthias Ringwald int i; 31894f781026SMatthias Ringwald 31904f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 31915e91d96cSMatthias Ringwald hci_link_type_t link_type; 319248f33f37SMatthias Ringwald bd_addr_t addr; 3193cef94710SMatthias Ringwald bd_addr_type_t addr_type; 31944f781026SMatthias Ringwald #endif 31954ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 31964ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream; 31977aa35f6aSMatthias Ringwald le_audio_big_t * big; 31982a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync; 31994ae8623eSMatthias Ringwald #endif 32004f781026SMatthias Ringwald 32014f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 32024f781026SMatthias Ringwald 32034f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 32044f781026SMatthias Ringwald 32054f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 32064f781026SMatthias Ringwald handle_command_complete_event(packet, size); 320756cf178bSmatthias.ringwald break; 320856cf178bSmatthias.ringwald 32097ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 3210afbf1cd7SMatthias Ringwald handle_command_status_event(packet, size); 32117ec5eeaaSmatthias.ringwald break; 32127ec5eeaaSmatthias.ringwald 32132e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 32149784dac1SMatthias Ringwald if (size < 3) return; 32159784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 32164ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 3217d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3218d9a1d8edSMatthias Ringwald bool notify_sco = false; 3219d9a1d8edSMatthias Ringwald #endif 3220d9a1d8edSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3221d9a1d8edSMatthias Ringwald bool notify_iso = false; 3222d9a1d8edSMatthias Ringwald #endif 32239784dac1SMatthias Ringwald uint16_t offset = 3; 32249784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 32254ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 32264ea43905SMatthias Ringwald offset += 2u; 3227f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 32284ea43905SMatthias Ringwald offset += 2u; 32292e440c8aS[email protected] 32305061f3afS[email protected] conn = hci_connection_for_handle(handle); 3231e5413aa0SMatthias Ringwald if (conn != NULL) { 323223bed257S[email protected] 3233ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets) { 3234ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 3235e35edcc1S[email protected] } else { 3236ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 3237ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 3238e35edcc1S[email protected] } 3239ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 3240d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3241d9a1d8edSMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO){ 3242d9a1d8edSMatthias Ringwald notify_sco = true; 3243d9a1d8edSMatthias Ringwald } 3244d9a1d8edSMatthias Ringwald #endif 3245e5413aa0SMatthias Ringwald } 3246e5413aa0SMatthias Ringwald 32478ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 32488ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 32498ad9cf99SMatthias Ringwald #endif 32508ad9cf99SMatthias Ringwald 3251e5413aa0SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3252e5413aa0SMatthias Ringwald if (conn == NULL){ 3253e5413aa0SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(handle); 3254e5413aa0SMatthias Ringwald if (iso_stream != NULL){ 3255e5413aa0SMatthias Ringwald if (iso_stream->num_packets_sent >= num_packets) { 3256e5413aa0SMatthias Ringwald iso_stream->num_packets_sent -= num_packets; 3257e5413aa0SMatthias Ringwald } else { 3258e5413aa0SMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 3259e5413aa0SMatthias Ringwald iso_stream->num_packets_sent = 0; 3260e5413aa0SMatthias Ringwald } 32618d72db10SMatthias Ringwald if (iso_stream->iso_type == HCI_ISO_TYPE_BIS){ 32628d72db10SMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(iso_stream->group_id); 3263fe977b37SMatthias Ringwald if (big != NULL){ 3264fe977b37SMatthias Ringwald big->num_completed_timestamp_current_valid = true; 3265fe977b37SMatthias Ringwald big->num_completed_timestamp_current_ms = btstack_run_loop_get_time_ms(); 3266fe977b37SMatthias Ringwald } 3267fe977b37SMatthias Ringwald } 3268e5413aa0SMatthias Ringwald log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", 3269e5413aa0SMatthias Ringwald num_packets, handle, iso_stream->num_packets_sent); 3270d9a1d8edSMatthias Ringwald notify_iso = true; 3271e5413aa0SMatthias Ringwald } 3272d9a1d8edSMatthias Ringwald } 3273d9a1d8edSMatthias Ringwald #endif 3274d9a1d8edSMatthias Ringwald } 3275d9a1d8edSMatthias Ringwald 3276d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3277d9a1d8edSMatthias Ringwald if (notify_sco){ 3278d9a1d8edSMatthias Ringwald hci_notify_if_sco_can_send_now(); 3279d9a1d8edSMatthias Ringwald } 3280d9a1d8edSMatthias Ringwald #endif 3281d9a1d8edSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3282d9a1d8edSMatthias Ringwald if (notify_iso){ 32835ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 3284e5413aa0SMatthias Ringwald } 3285e5413aa0SMatthias Ringwald #endif 32866772a24cSmatthias.ringwald break; 32872e440c8aS[email protected] } 328835454696SMatthias Ringwald 328935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 32909afaa4d4SMatthias Ringwald case HCI_EVENT_FLUSH_OCCURRED: 32919afaa4d4SMatthias Ringwald // flush occurs only if automatic flush has been enabled by gap_enable_link_watchdog() 32929afaa4d4SMatthias Ringwald handle = hci_event_flush_occurred_get_handle(packet); 32939afaa4d4SMatthias Ringwald conn = hci_connection_for_handle(handle); 32949afaa4d4SMatthias Ringwald if (conn) { 32959afaa4d4SMatthias Ringwald log_info("Flush occurred, disconnect 0x%04x", handle); 32969afaa4d4SMatthias Ringwald conn->state = SEND_DISCONNECT; 32979afaa4d4SMatthias Ringwald } 32989afaa4d4SMatthias Ringwald break; 32999afaa4d4SMatthias Ringwald 3300f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 3301f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 3302f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 3303f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 3304f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3305f5875de5SMatthias Ringwald } 3306f5875de5SMatthias Ringwald break; 3307b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 3308b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 3309b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 3310b7f1ee76SMatthias Ringwald } 3311b7f1ee76SMatthias Ringwald break; 33121f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 3313724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 33145e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 331572cf8859SMatthias Ringwald 331672cf8859SMatthias Ringwald // CVE-2020-26555: reject incoming connection from device with same BD ADDR 331772cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0){ 331872cf8859SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 331972cf8859SMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 332072cf8859SMatthias Ringwald break; 332172cf8859SMatthias Ringwald } 332272cf8859SMatthias Ringwald 332307e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 33245e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 3325d152b6c6SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS; 332607e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 332707e010b6SMilanka Ringwald break; 332807e010b6SMilanka Ringwald } 332907e010b6SMilanka Ringwald } 333007e010b6SMilanka Ringwald 333137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 33325e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 33335e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 33342e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 33351f7b95a1Smatthias.ringwald if (!conn) { 33365293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 33371f7b95a1Smatthias.ringwald } 3338ce4c8fabSmatthias.ringwald if (!conn) { 3339ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 33404536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 3341058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 3342f23819bcSMatthias Ringwald hci_run(); 3343f23819bcSMatthias Ringwald // avoid event to higher layer 3344f23819bcSMatthias Ringwald return; 3345ce4c8fabSmatthias.ringwald } 33465cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 334732ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 3348f3a16b9aSMatthias Ringwald // store info about eSCO 33495e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 335076ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 3351f3a16b9aSMatthias Ringwald } 335232ab9390Smatthias.ringwald hci_run(); 33531f7b95a1Smatthias.ringwald break; 33541f7b95a1Smatthias.ringwald 33556772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 3356fe1ed1b8Smatthias.ringwald // Connection management 3357724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 33589da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 3359f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 33602e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3361fe1ed1b8Smatthias.ringwald if (conn) { 3362b448a0e7Smatthias.ringwald if (!packet[2]){ 3363c8e4258aSmatthias.ringwald conn->state = OPEN; 3364f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 33656909f064SMatthias Ringwald 336622df2fe2SMatthias Ringwald // trigger write supervision timeout if we're master 3367d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 336822df2fe2SMatthias Ringwald conn->gap_connection_tasks |= GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT; 33696909f064SMatthias Ringwald } 33706909f064SMatthias Ringwald 337122df2fe2SMatthias Ringwald // trigger write automatic flush timeout 33729afaa4d4SMatthias Ringwald if (hci_stack->automatic_flush_timeout != 0){ 33739afaa4d4SMatthias Ringwald conn->gap_connection_tasks |= GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT; 33749afaa4d4SMatthias Ringwald } 33759afaa4d4SMatthias Ringwald 3376c785ef68Smatthias.ringwald // restart timer 3377528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 3378528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 3379c785ef68Smatthias.ringwald 338018c4a0a3SMatthias Ringwald // trigger remote features for dedicated bonding 338118c4a0a3SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 338218c4a0a3SMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 338318c4a0a3SMatthias Ringwald } 338418c4a0a3SMatthias Ringwald 33859da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 338643bfb1bdSmatthias.ringwald 338743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 3388b448a0e7Smatthias.ringwald } else { 33890bbba85bSMatthias Ringwald // connection failed 33900bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 3391fe1ed1b8Smatthias.ringwald } 3392fe1ed1b8Smatthias.ringwald } 33936772a24cSmatthias.ringwald break; 3394fe1ed1b8Smatthias.ringwald 339544d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 3396724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 33971f34df2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 33981f34df2cSMatthias Ringwald log_info("Synchronous Connection Complete for %p (status=%u) %s", conn, packet[2], bd_addr_to_str(addr)); 33991a06f663S[email protected] if (packet[2]){ 340044d0e3d5S[email protected] // connection failed 34011f34df2cSMatthias Ringwald if (conn){ 34021f34df2cSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 34031f34df2cSMatthias Ringwald } 340444d0e3d5S[email protected] break; 340544d0e3d5S[email protected] } 3406e35edcc1S[email protected] if (!conn) { 3407e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 3408e35edcc1S[email protected] } 3409e35edcc1S[email protected] if (!conn) { 3410e35edcc1S[email protected] break; 3411e35edcc1S[email protected] } 34121a06f663S[email protected] conn->state = OPEN; 3413f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 3414ee752bb8SMatthias Ringwald 3415ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3416ee752bb8SMatthias Ringwald // update SCO 3417ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 3418ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 3419ee752bb8SMatthias Ringwald } 3420f234b250SMatthias Ringwald // trigger can send now 3421f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 34221972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 3423f234b250SMatthias Ringwald } 3424ee752bb8SMatthias Ringwald #endif 3425cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3426cb70c5abSMatthias Ringwald // configure sco transport 3427cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 3428cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 3429cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 3430cb70c5abSMatthias Ringwald } 3431cb70c5abSMatthias Ringwald #endif 343244d0e3d5S[email protected] break; 343344d0e3d5S[email protected] 3434afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 3435f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 3436afd4e962S[email protected] conn = hci_connection_for_handle(handle); 3437afd4e962S[email protected] if (!conn) break; 3438afd4e962S[email protected] if (!packet[2]){ 34392f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 34402f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 34412f5c44baSMatthias Ringwald 34425ccef624SMatthias Ringwald // read extended features if possible 34431ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES) 34441ffa425cSMatthias Ringwald && ((conn->remote_supported_features[0] & 2) != 0)) { 34455ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 34465ccef624SMatthias Ringwald break; 34475ccef624SMatthias Ringwald } 34485ccef624SMatthias Ringwald } 34495ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 34505ccef624SMatthias Ringwald break; 34515ccef624SMatthias Ringwald 34525ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 34535ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 34545ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 34555ccef624SMatthias Ringwald if (!conn) break; 34565ccef624SMatthias Ringwald // status = ok, page = 1 345750c51a77SMatthias Ringwald if (!packet[2]) { 345850c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 345950c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 34605ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 346150c51a77SMatthias Ringwald bool done = false; 346250c51a77SMatthias Ringwald switch (page_number){ 346350c51a77SMatthias Ringwald case 1: 34642f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 346550c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 346650c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 346750c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 346850c51a77SMatthias Ringwald } else { 346950c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 347050c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 347150c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 347250c51a77SMatthias Ringwald } 347350c51a77SMatthias Ringwald done = true; 347450c51a77SMatthias Ringwald } 347550c51a77SMatthias Ringwald break; 347650c51a77SMatthias Ringwald case 2: 347750c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 347850c51a77SMatthias Ringwald done = true; 347950c51a77SMatthias Ringwald break; 348050c51a77SMatthias Ringwald default: 348150c51a77SMatthias Ringwald break; 348250c51a77SMatthias Ringwald } 348350c51a77SMatthias Ringwald if (!done) break; 3484afd4e962S[email protected] } 3485de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 3486afd4e962S[email protected] break; 3487afd4e962S[email protected] 34887fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 3489308eeaffSMatthias Ringwald #ifndef ENABLE_EXPLICIT_LINK_KEY_REPLY 349063c0b2fdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 349163c0b2fdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 349263c0b2fdSMatthias Ringwald if (!conn) break; 349363c0b2fdSMatthias Ringwald 349463c0b2fdSMatthias Ringwald // lookup link key in db if not cached 349563c0b2fdSMatthias Ringwald if ((conn->link_key_type == INVALID_LINK_KEY) && (hci_stack->link_key_db != NULL)){ 349663c0b2fdSMatthias Ringwald hci_stack->link_key_db->get_link_key(conn->address, conn->link_key, &conn->link_key_type); 349763c0b2fdSMatthias Ringwald } 349863c0b2fdSMatthias Ringwald 349963c0b2fdSMatthias Ringwald // response sent by hci_run() 350063c0b2fdSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_HANDLE_LINK_KEY_REQUEST; 350163c0b2fdSMatthias Ringwald #endif 3502608f51bbSMatthias Ringwald break; 35037fde4af9Smatthias.ringwald 35049ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 35051714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 3506f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 35079ab95c90S[email protected] if (!conn) break; 35081714cbbdSMatthias Ringwald 35091714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 35101714cbbdSMatthias Ringwald 35113817f9dfSMatthias Ringwald // CVE-2020-26555: ignore NULL link key 35123817f9dfSMatthias Ringwald // default link_key_type = INVALID_LINK_KEY asserts that NULL key won't be used for encryption 35133817f9dfSMatthias Ringwald if (btstack_is_null(&packet[8], 16)) break; 35143817f9dfSMatthias Ringwald 35157bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 35169ab95c90S[email protected] // Change Connection Encryption keeps link key type 35179ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 35189ab95c90S[email protected] conn->link_key_type = link_key_type; 35199ab95c90S[email protected] } 35203817f9dfSMatthias Ringwald 3521e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 3522e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 3523e9f98c4aSMatthias Ringwald 3524bec5f683SMatthias Ringwald // only store link key: 3525bec5f683SMatthias Ringwald // - if bondable enabled 3526bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 35276edaed7fSMatthias Ringwald // - if security level sufficient 35286edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 3529bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 3530bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 3531532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 3532532454f9SMatthias Ringwald if (!remote_bonding){ 3533bec5f683SMatthias Ringwald break; 3534bec5f683SMatthias Ringwald } 3535bec5f683SMatthias Ringwald } 353655597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 35377fde4af9Smatthias.ringwald break; 35389ab95c90S[email protected] } 35397fde4af9Smatthias.ringwald 35407fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 35411714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 35421714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 35431714cbbdSMatthias Ringwald if (!conn) break; 35441714cbbdSMatthias Ringwald 35451714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 3546a800d95eSMatthias Ringwald // abort pairing if: non-bondable mode (pin code request is not forwarded to app) 35473a9fb326S[email protected] if (!hci_stack->bondable ){ 35488daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 35491714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 3550f8fb5f6eS[email protected] hci_run(); 3551f8fb5f6eS[email protected] return; 35524c57c146S[email protected] } 3553a800d95eSMatthias Ringwald // abort pairing if: LEVEL_4 required (pin code request is not forwarded to app) 3554a800d95eSMatthias Ringwald if ((hci_stack->gap_secure_connections_only_mode) || (conn->requested_security_level == LEVEL_4)){ 3555a800d95eSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 355650dcc63cSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 355750dcc63cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 355850dcc63cSMatthias Ringwald hci_run(); 355950dcc63cSMatthias Ringwald return; 356050dcc63cSMatthias Ringwald } 35617fde4af9Smatthias.ringwald break; 35627fde4af9Smatthias.ringwald 356350d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 356450d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 356550d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 356650d7398cSMatthias Ringwald if (!conn) break; 35671714cbbdSMatthias Ringwald 35688daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE); 35691714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 357050d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 3571e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 3572d22b82d6SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3573d22b82d6SMatthias Ringwald conn->io_cap_response_oob_data = hci_event_io_capability_response_get_oob_data_present(packet); 3574d22b82d6SMatthias Ringwald #endif 357550d7398cSMatthias Ringwald break; 357650d7398cSMatthias Ringwald 35771d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 35781714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 35791714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 35801714cbbdSMatthias Ringwald if (!conn) break; 35811714cbbdSMatthias Ringwald 3582c950c316SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 35839671e615SMatthias Ringwald hci_connection_timestamp(conn); 35841714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 3585dbe1a790S[email protected] break; 3586dbe1a790S[email protected] 35871849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 35881849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 35897ca4a7edSMatthias Ringwald hci_event_remote_oob_data_request_get_bd_addr(packet, addr); 35907ca4a7edSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 35917ca4a7edSMatthias Ringwald if (!conn) break; 35927ca4a7edSMatthias Ringwald 35937ca4a7edSMatthias Ringwald hci_connection_timestamp(conn); 35947ca4a7edSMatthias Ringwald 35957ca4a7edSMatthias Ringwald hci_pairing_started(conn, true); 35967ca4a7edSMatthias Ringwald 35977ca4a7edSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 35981849becdSMatthias Ringwald break; 35991849becdSMatthias Ringwald #endif 36001849becdSMatthias Ringwald 3601dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 360217c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 3603367aedc0SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3604367aedc0SMatthias Ringwald if (!conn) break; 3605367aedc0SMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 3606367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 36078daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 3608367aedc0SMatthias Ringwald }; 3609367aedc0SMatthias Ringwald } else { 3610367aedc0SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3611367aedc0SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 3612367aedc0SMatthias Ringwald // don't forward event to app 3613367aedc0SMatthias Ringwald hci_run(); 3614367aedc0SMatthias Ringwald return; 3615367aedc0SMatthias Ringwald } 3616dbe1a790S[email protected] break; 3617dbe1a790S[email protected] 3618dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 3619367aedc0SMatthias Ringwald // Pairing using Passkey results in MITM protection. If Level 4 is required, support for SC is validated on IO Cap Request 3620367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 36218daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 3622367aedc0SMatthias Ringwald }; 36231d6b20aeS[email protected] break; 36241849becdSMatthias Ringwald 36253dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 36263dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 36273dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 36283dce6128SMatthias Ringwald if (!conn) break; 36293dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 36303dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 36313dce6128SMatthias Ringwald break; 363235454696SMatthias Ringwald #endif 36331d6b20aeS[email protected] 3634f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 3635a355334eSMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 3636254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 3637f0944df2S[email protected] conn = hci_connection_for_handle(handle); 3638f0944df2S[email protected] if (!conn) break; 36394ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 3640254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 3641254b78eeSMatthias Ringwald if (encryption_enabled){ 3642573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 3643573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 36448daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 3645573897a0SMatthias Ringwald } 3646573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 3647573897a0SMatthias Ringwald else { 3648fcaf38b9SMatthias Ringwald 3649254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 365036c3546bSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type); 3651254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 3652edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 3653254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 36549ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 3655254b78eeSMatthias Ringwald break; 3656254b78eeSMatthias Ringwald } 3657254b78eeSMatthias Ringwald 3658aa2fe986SMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 3659aa2fe986SMatthias Ringwald if (connected_uses_aes_ccm){ 3660aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3661aa2fe986SMatthias Ringwald } 3662aa2fe986SMatthias Ringwald 3663aa2fe986SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3664aa2fe986SMatthias Ringwald // work around for issue with PTS dongle 3665aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3666aa2fe986SMatthias Ringwald #endif 3667a355334eSMatthias Ringwald // validate encryption key size 3668a355334eSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE_V2) { 3669a355334eSMatthias Ringwald uint8_t encryption_key_size = hci_event_encryption_change_v2_get_encryption_key_size(packet); 3670a355334eSMatthias Ringwald // already got encryption key size 3671a355334eSMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, encryption_key_size); 3672a355334eSMatthias Ringwald } else { 36731ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE)) { 36746e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 3675573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 36766e058d3fSMatthias Ringwald } else { 36776e058d3fSMatthias Ringwald // if not, pretend everything is perfect 36789866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 36796e058d3fSMatthias Ringwald } 3680573897a0SMatthias Ringwald } 3681a355334eSMatthias Ringwald } 3682573897a0SMatthias Ringwald #endif 3683f0944df2S[email protected] } else { 36848daf94bcSMatthias Ringwald conn->authentication_flags &= ~AUTH_FLAG_CONNECTION_ENCRYPTED; 3685f0944df2S[email protected] } 36865999ee11SMatthias Ringwald } else { 36875999ee11SMatthias Ringwald uint8_t status = hci_event_encryption_change_get_status(packet); 36885999ee11SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 36895999ee11SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 36905999ee11SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 36915999ee11SMatthias Ringwald conn->bonding_status = status; 36925999ee11SMatthias Ringwald } 3693ad83dc6aS[email protected] } 3694573897a0SMatthias Ringwald 3695f0944df2S[email protected] break; 3696f0944df2S[email protected] 369735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 36981eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 3699abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 37001eb2563eS[email protected] conn = hci_connection_for_handle(handle); 37011eb2563eS[email protected] if (!conn) break; 3702ad83dc6aS[email protected] 3703dbd5dcc3SMatthias Ringwald // clear authentication active flag 3704dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 37051714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 3706dbd5dcc3SMatthias Ringwald 3707abdad579SMatthias Ringwald // authenticated only if auth status == 0 3708abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 3709abdad579SMatthias Ringwald // authenticated 37108daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3711abdad579SMatthias Ringwald 3712131ef17aSMatthias Ringwald // If not already encrypted, start encryption 37138daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0){ 37141eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3715ad83dc6aS[email protected] break; 3716ad83dc6aS[email protected] } 3717abdad579SMatthias Ringwald } 3718abdad579SMatthias Ringwald 3719abdad579SMatthias Ringwald // emit updated security level 37201eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 37211eb2563eS[email protected] break; 37221714cbbdSMatthias Ringwald 37231714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 37241714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 37251714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 37261714cbbdSMatthias Ringwald if (!conn) break; 37271714cbbdSMatthias Ringwald 3728aa2fe986SMatthias Ringwald // treat successfully paired connection as authenticated 3729aa2fe986SMatthias Ringwald if (hci_event_simple_pairing_complete_get_status(packet) == ERROR_CODE_SUCCESS){ 3730f3aafff1SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3731aa2fe986SMatthias Ringwald } 3732aa2fe986SMatthias Ringwald 37331714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 37341714cbbdSMatthias Ringwald break; 373535454696SMatthias Ringwald #endif 373634d2123cS[email protected] 373786805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 3738ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 3739ccda6e14S[email protected] // see end of function, too. 3740a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 3741a4f30ec0S[email protected] if (packet[2]) break; // status != 0 3742f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 374381d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 37444ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3745c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 37464ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 374781d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 3748c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 3749c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 375081d2bdb2SMatthias Ringwald if (release_buffer){ 375181d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 375281d2bdb2SMatthias Ringwald } 3753c6a37cfdSMatthias Ringwald } 3754c6a37cfdSMatthias Ringwald } 375535454696SMatthias Ringwald 37564ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3757098c2716SMatthias Ringwald // drop outgoing ISO fragments if it is for closed connection and release buffer if tx not active 3758098c2716SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size > 0u) { 3759098c2716SMatthias Ringwald if (handle == READ_ISO_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 3760098c2716SMatthias Ringwald int release_buffer = hci_stack->iso_fragmentation_tx_active == 0u; 3761098c2716SMatthias Ringwald log_info("drop fragmented ISO data for closed connection, release buffer %u", release_buffer); 3762098c2716SMatthias Ringwald hci_stack->iso_fragmentation_total_size = 0; 3763098c2716SMatthias Ringwald hci_stack->iso_fragmentation_pos = 0; 3764098c2716SMatthias Ringwald if (release_buffer){ 3765098c2716SMatthias Ringwald hci_release_packet_buffer(); 3766098c2716SMatthias Ringwald } 3767098c2716SMatthias Ringwald } 3768098c2716SMatthias Ringwald } 3769098c2716SMatthias Ringwald 37704ae8623eSMatthias Ringwald // finalize iso stream if handle matches 377115a15be3SMatthias Ringwald iso_stream = hci_iso_stream_for_con_handle(handle); 37724ae8623eSMatthias Ringwald if (iso_stream != NULL){ 37734ae8623eSMatthias Ringwald hci_iso_stream_finalize(iso_stream); 37744ae8623eSMatthias Ringwald break; 37754ae8623eSMatthias Ringwald } 37764ae8623eSMatthias Ringwald #endif 37774ae8623eSMatthias Ringwald 3778c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 3779c6a37cfdSMatthias Ringwald if (!conn) break; 37801714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 37811714cbbdSMatthias Ringwald // pairing failed if it was ongoing 37821714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 37831714cbbdSMatthias Ringwald #endif 3784046ec007SMatthias Ringwald 3785046ec007SMatthias Ringwald // emit dedicatd bonding event 3786046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 3787046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 3788046ec007SMatthias Ringwald } 3789046ec007SMatthias Ringwald 3790459d27b9SMatthias Ringwald // mark connection for shutdown, stop timers, reset state 3791459d27b9SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 3792459d27b9SMatthias Ringwald hci_connection_stop_timer(conn); 3793459d27b9SMatthias Ringwald hci_connection_init(conn); 3794459d27b9SMatthias Ringwald 379535454696SMatthias Ringwald #ifdef ENABLE_BLE 3796d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3797046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 37982b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 3799bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 38009a2e4658SMatthias Ringwald } 380135454696SMatthias Ringwald #endif 3802d70217a2SMatthias Ringwald #endif 3803ccda6e14S[email protected] break; 38046772a24cSmatthias.ringwald 3805c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 3806313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 3807d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 3808c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 38097586ee35S[email protected] } else { 38107586ee35S[email protected] // if no special requests, just reboot stack 38117586ee35S[email protected] hci_power_control_off(); 38127586ee35S[email protected] hci_power_control_on(); 3813c68bdf90Smatthias.ringwald } 3814c68bdf90Smatthias.ringwald break; 3815c68bdf90Smatthias.ringwald 38160e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 38175cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 38185cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 3819c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 3820f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 3821c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3822c4c88f1bSJakob Krantz if (!conn) break; 38235cf766e8SMatthias Ringwald conn->role = packet[9]; 38245cf766e8SMatthias Ringwald break; 38250e6f3837SMatthias Ringwald #endif 38265cf766e8SMatthias Ringwald 382763fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 3828098c2716SMatthias Ringwald // release packet buffer only for asynchronous transport and if there are not further fragments 38294fa24b5fS[email protected] if (hci_transport_synchronous()) { 383063fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 38314fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 38324fa24b5fS[email protected] } 383381d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 3834098c2716SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3835098c2716SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = 0; 3836098c2716SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size) break; 3837098c2716SMatthias Ringwald #endif 3838d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 3839d051460cS[email protected] hci_release_packet_buffer(); 3840701e3307SMatthias Ringwald 38415ade6657SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 38425ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 38435ade6657SMatthias Ringwald #endif 384463fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 384563fa3374SMatthias Ringwald 384635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 384763fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 3848701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 384935454696SMatthias Ringwald #endif 38506b4af23dS[email protected] break; 38516b4af23dS[email protected] 385235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 385363fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 385463fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 38551972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 385663fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 385763fa3374SMatthias Ringwald return; 38581cfb383eSMatthias Ringwald 38591cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 38601cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 38611cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 38621cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 38639784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 38641cfb383eSMatthias Ringwald break; 386535454696SMatthias Ringwald #endif 386663fa3374SMatthias Ringwald 3867a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 38685909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 38695909f7f2Smatthias.ringwald switch (packet[2]){ 3870d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 387157c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 38726fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 387357c9da5bS[email protected] le_handle_advertisement_report(packet, size); 38747bdc6798S[email protected] break; 3875a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 3876a0698cb4SMatthias Ringwald case HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT: 3877a0698cb4SMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 3878a0698cb4SMatthias Ringwald le_handle_extended_advertisement_report(packet, size); 3879a0698cb4SMatthias Ringwald break; 3880ce56b945SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 3881ce56b945SMatthias Ringwald hci_stack->le_periodic_sync_request = LE_CONNECTING_IDLE; 3882ce56b945SMatthias Ringwald hci_stack->le_periodic_sync_state = LE_CONNECTING_IDLE; 3883ce56b945SMatthias Ringwald break; 3884a0698cb4SMatthias Ringwald #endif 3885d70217a2SMatthias Ringwald #endif 38865909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 38870ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 38885909f7f2Smatthias.ringwald break; 38895909f7f2Smatthias.ringwald 3890f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3891c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3892c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3893c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3894c9db5c21SMilanka Ringwald if (!conn) break; 3895c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3896c9db5c21SMilanka Ringwald break; 389765a46ef3S[email protected] 389873cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 389973cd8a2aSMatthias Ringwald // connection 390073cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 390173cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 390273cd8a2aSMatthias Ringwald if (conn) { 390373cd8a2aSMatthias Ringwald // read arguments 390473cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 390573cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 390673cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 390773cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 390873cd8a2aSMatthias Ringwald 390973cd8a2aSMatthias Ringwald // validate against current connection parameter range 391073cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 391173cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 391273cd8a2aSMatthias 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); 391373cd8a2aSMatthias Ringwald if (update_parameter){ 391473cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 391573cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 391673cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 391773cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 391873cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 391973cd8a2aSMatthias Ringwald } else { 3920c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 392173cd8a2aSMatthias Ringwald } 392273cd8a2aSMatthias Ringwald } 392373cd8a2aSMatthias Ringwald break; 39240f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 39250f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 39260f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 39270f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 39280f3b27c5SMatthias Ringwald if (conn) { 39290f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 39300f3b27c5SMatthias Ringwald } 39310f3b27c5SMatthias Ringwald break; 39320f3b27c5SMatthias Ringwald #endif 39334ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 39344ae8623eSMatthias Ringwald case HCI_SUBEVENT_LE_CIS_ESTABLISHED: 39354ae8623eSMatthias Ringwald handle = hci_subevent_le_cis_established_get_connection_handle(packet); 393615a15be3SMatthias Ringwald iso_stream = hci_iso_stream_for_con_handle(handle); 39374ae8623eSMatthias Ringwald if (iso_stream){ 39384ae8623eSMatthias Ringwald uint8_t status = hci_subevent_le_cis_established_get_status(packet); 39394ae8623eSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 39404ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_ESTABLISHED; 39414ae8623eSMatthias Ringwald } else { 39424ae8623eSMatthias Ringwald hci_iso_stream_finalize(iso_stream); 39434ae8623eSMatthias Ringwald } 39444ae8623eSMatthias Ringwald } 39457aa35f6aSMatthias Ringwald break; 39467aa35f6aSMatthias Ringwald case HCI_SUBEVENT_LE_CREATE_BIG_COMPLETE: 394740f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = 0xff; 39487aa35f6aSMatthias Ringwald big = hci_big_for_handle(packet[4]); 39497aa35f6aSMatthias Ringwald if (big != NULL){ 39507aa35f6aSMatthias Ringwald uint8_t status = packet[3]; 39517aa35f6aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 39527aa35f6aSMatthias Ringwald // store bis_con_handles and trigger iso path setup 39537aa35f6aSMatthias Ringwald uint8_t num_bis = btstack_min(MAX_NR_BIS, packet[20]); 39547aa35f6aSMatthias Ringwald uint8_t i; 39557aa35f6aSMatthias Ringwald for (i=0;i<num_bis;i++){ 3956fb16346aSMatthias Ringwald hci_con_handle_t bis_handle = (hci_con_handle_t) little_endian_read_16(packet, 21 + (2 * i)); 3957fb16346aSMatthias Ringwald big->bis_con_handles[i] = bis_handle; 3958fb16346aSMatthias Ringwald // assign bis handle 3959fb16346aSMatthias Ringwald btstack_linked_list_iterator_t it; 3960fb16346aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 3961fb16346aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3962fb16346aSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 3963fb16346aSMatthias Ringwald if ((iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) && 39648d72db10SMatthias Ringwald (iso_stream->group_id == big->big_handle)){ 3965fb16346aSMatthias Ringwald iso_stream->con_handle = bis_handle; 3966fb16346aSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_ESTABLISHED; 3967cd1139aaSMatthias Ringwald break; 3968fb16346aSMatthias Ringwald } 3969fb16346aSMatthias Ringwald } 39707aa35f6aSMatthias Ringwald } 39712a6c0f82SMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 39727aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 39737aa35f6aSMatthias Ringwald big->state_vars.next_bis = 0; 39742a6c0f82SMatthias Ringwald } 39757aa35f6aSMatthias Ringwald } else { 39762a6c0f82SMatthias Ringwald // create BIG failed or has been stopped by us 3977*778463d0SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_BIS, big->big_handle); 39787aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 39792a6c0f82SMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED){ 39807aa35f6aSMatthias Ringwald hci_emit_big_created(big, status); 39812a6c0f82SMatthias Ringwald } else { 39822a6c0f82SMatthias Ringwald hci_emit_big_terminated(big); 39832a6c0f82SMatthias Ringwald } 39847aa35f6aSMatthias Ringwald } 39857aa35f6aSMatthias Ringwald } 39867aa35f6aSMatthias Ringwald break; 39877aa35f6aSMatthias Ringwald case HCI_SUBEVENT_LE_TERMINATE_BIG_COMPLETE: 39887aa35f6aSMatthias Ringwald big = hci_big_for_handle(hci_subevent_le_terminate_big_complete_get_big_handle(packet)); 39897aa35f6aSMatthias Ringwald if (big != NULL){ 3990fb16346aSMatthias Ringwald // finalize associated ISO streams 3991fb16346aSMatthias Ringwald btstack_linked_list_iterator_t it; 3992fb16346aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 3993fb16346aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 3994fb16346aSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 39958d72db10SMatthias Ringwald if (iso_stream->group_id == big->big_handle){ 39968d72db10SMatthias Ringwald log_info("BIG Terminated, big_handle 0x%02x, con handle 0x%04x", iso_stream->group_id, iso_stream->con_handle); 3997fb16346aSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 3998fb16346aSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 3999fb16346aSMatthias Ringwald } 4000fb16346aSMatthias Ringwald } 40017aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 40027aa35f6aSMatthias Ringwald switch (big->state){ 40037aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED: 40047aa35f6aSMatthias Ringwald hci_emit_big_created(big, big->state_vars.status); 40057aa35f6aSMatthias Ringwald break; 40067aa35f6aSMatthias Ringwald default: 40077aa35f6aSMatthias Ringwald hci_emit_big_terminated(big); 40087aa35f6aSMatthias Ringwald break; 40097aa35f6aSMatthias Ringwald } 40107aa35f6aSMatthias Ringwald } 40117aa35f6aSMatthias Ringwald break; 40122a6c0f82SMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_ESTABLISHED: 40132a6c0f82SMatthias Ringwald big_sync = hci_big_sync_for_handle(packet[4]); 40142a6c0f82SMatthias Ringwald if (big_sync != NULL){ 40152a6c0f82SMatthias Ringwald uint8_t status = packet[3]; 4016d04a9afcSMatthias Ringwald uint8_t big_handle = packet[4]; 40172a6c0f82SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 40182a6c0f82SMatthias Ringwald // store bis_con_handles and trigger iso path setup 40192a6c0f82SMatthias Ringwald uint8_t num_bis = btstack_min(MAX_NR_BIS, packet[16]); 40202a6c0f82SMatthias Ringwald uint8_t i; 40212a6c0f82SMatthias Ringwald for (i=0;i<num_bis;i++){ 40222a6c0f82SMatthias Ringwald big_sync->bis_con_handles[i] = little_endian_read_16(packet, 17 + (2 * i)); 40232a6c0f82SMatthias Ringwald } 40242a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 40252a6c0f82SMatthias Ringwald // trigger iso path setup 40262a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 40272a6c0f82SMatthias Ringwald big_sync->state_vars.next_bis = 0; 40282a6c0f82SMatthias Ringwald } 40292a6c0f82SMatthias Ringwald } else { 40302a6c0f82SMatthias Ringwald // create BIG Sync failed or has been stopped by us 40312a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 40322a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 40332a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, status); 40342a6c0f82SMatthias Ringwald } else { 4035d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 40362a6c0f82SMatthias Ringwald } 40372a6c0f82SMatthias Ringwald } 40382a6c0f82SMatthias Ringwald } 40392a6c0f82SMatthias Ringwald break; 40402a6c0f82SMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 40412a6c0f82SMatthias Ringwald big_sync = hci_big_sync_for_handle(packet[4]); 40422a6c0f82SMatthias Ringwald if (big_sync != NULL){ 4043d04a9afcSMatthias Ringwald uint8_t big_handle = packet[4]; 40442a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 4045d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 40462a6c0f82SMatthias Ringwald } 40472a6c0f82SMatthias Ringwald break; 40484ae8623eSMatthias Ringwald #endif 40495909f7f2Smatthias.ringwald default: 40505909f7f2Smatthias.ringwald break; 40515909f7f2Smatthias.ringwald } 40525909f7f2Smatthias.ringwald break; 40535909f7f2Smatthias.ringwald #endif 40540b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 40550b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 40560b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 40570b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 40580b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 40590b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 40600b36101dSMatthias Ringwald break; 40610b36101dSMatthias Ringwald default: 40620b36101dSMatthias Ringwald break; 40630b36101dSMatthias Ringwald } 40640b36101dSMatthias Ringwald break; 40656772a24cSmatthias.ringwald default: 40666772a24cSmatthias.ringwald break; 4067fe1ed1b8Smatthias.ringwald } 4068fe1ed1b8Smatthias.ringwald 406967c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 407089db417bSmatthias.ringwald 407186805605S[email protected] // notify upper stack 4072d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 407394ab26f8Smatthias.ringwald 407486805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 4075797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 4076f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 407705ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 4078046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 4079797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 408005ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 4081b0136355SMatthias Ringwald } 40828ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 40838ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 40848ad9cf99SMatthias Ringwald #endif 408586805605S[email protected] } 408686805605S[email protected] 408794ab26f8Smatthias.ringwald // execute main loop 408894ab26f8Smatthias.ringwald hci_run(); 408916833f0aSmatthias.ringwald } 409016833f0aSmatthias.ringwald 409135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 40926f28d2eeSMatthias Ringwald 40936be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 40946f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 40956f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 40966f28d2eeSMatthias Ringwald 40976f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 409859c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 40995a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 41006f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 41016f28d2eeSMatthias Ringwald if (!conn) return; 41026f28d2eeSMatthias Ringwald 4103afaddd67SMatthias Ringwald // trigger send 4104afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 4105e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 4106a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 4107e4157653SMatthias Ringwald conn->sco_tx_ready++; 4108e4157653SMatthias Ringwald } 4109afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 41106f28d2eeSMatthias Ringwald } 41116f28d2eeSMatthias Ringwald 411259c33575SMatthias Ringwald 411359c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 411459c33575SMatthias Ringwald 41156f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 411659c33575SMatthias Ringwald 41176f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 411859c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 411959c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 412059c33575SMatthias Ringwald 412159c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 412259c33575SMatthias Ringwald 412359c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 4124ddbec4ceSMatthias Ringwald btstack_run_loop_remove_timer(timer); 412559c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 412659c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 412759c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 412859c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 41296f28d2eeSMatthias Ringwald } 41306be3cf7fSMatthias Ringwald #endif 41316f28d2eeSMatthias Ringwald 4132c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 41330b3f95dfSMatthias Ringwald // lookup connection struct 41342b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 41352b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 41360b3f95dfSMatthias Ringwald if (!conn) return; 41370b3f95dfSMatthias Ringwald 41386be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 4139760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 4140760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 4141a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 4142760b20efSMatthias Ringwald packet[2] = 0x3c; 4143760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 4144760b20efSMatthias Ringwald size = 63; 41450b3f95dfSMatthias Ringwald } 41460b3f95dfSMatthias Ringwald } 4147760b20efSMatthias Ringwald 4148f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 4149f234b250SMatthias Ringwald // Nothing to do 4150f234b250SMatthias Ringwald } else { 415149205f5dSMatthias 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); 4152760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 41536f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 415459c33575SMatthias Ringwald 415559c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 415659c33575SMatthias Ringwald // ignore first 10 packets 41576f28d2eeSMatthias Ringwald conn->sco_rx_count++; 415859c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 415959c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 416059c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 41616f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 41626f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 416359c33575SMatthias Ringwald conn->sco_rx_count = 0; 416459c33575SMatthias Ringwald sco_schedule_tx(conn); 416559c33575SMatthias Ringwald } 416659c33575SMatthias Ringwald } else { 416759c33575SMatthias Ringwald // track expected arrival timme 416859c33575SMatthias Ringwald conn->sco_rx_count++; 416959c33575SMatthias Ringwald conn->sco_rx_ms += 7; 417059c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 417159c33575SMatthias Ringwald if (delta > 0){ 417259c33575SMatthias Ringwald conn->sco_rx_ms++; 417359c33575SMatthias Ringwald } 417459c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 41756f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 41766f28d2eeSMatthias Ringwald } 4177760b20efSMatthias Ringwald } 4178f234b250SMatthias Ringwald } 41796be3cf7fSMatthias Ringwald #endif 41806be3cf7fSMatthias Ringwald 41810b3f95dfSMatthias Ringwald // deliver to app 41820b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 41830b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 41840b3f95dfSMatthias Ringwald } 41850b3f95dfSMatthias Ringwald 4186ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 4187ed325439SMatthias Ringwald // We can send one packet for each received packet 4188ed325439SMatthias Ringwald conn->sco_tx_ready++; 4189ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 4190ed325439SMatthias Ringwald #endif 4191ed325439SMatthias Ringwald 41920b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 41932b838201SMatthias Ringwald conn->num_packets_completed++; 41942b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 41952b838201SMatthias Ringwald hci_run(); 41962b838201SMatthias Ringwald #endif 4197c91d150bS[email protected] } 419835454696SMatthias Ringwald #endif 4199c91d150bS[email protected] 42000a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 42015bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 420210e830c9Smatthias.ringwald switch (packet_type) { 420310e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 420410e830c9Smatthias.ringwald event_handler(packet, size); 420510e830c9Smatthias.ringwald break; 420610e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 420710e830c9Smatthias.ringwald acl_handler(packet, size); 420810e830c9Smatthias.ringwald break; 420935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4210c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 4211c91d150bS[email protected] sco_handler(packet, size); 4212202c8a4cSMatthias Ringwald break; 421335454696SMatthias Ringwald #endif 42140a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 42155027c498SMatthias Ringwald case HCI_ISO_DATA_PACKET: 4216d39e4f95SMatthias Ringwald hci_iso_packet_handler(packet, size); 42175027c498SMatthias Ringwald break; 42185027c498SMatthias Ringwald #endif 421910e830c9Smatthias.ringwald default: 422010e830c9Smatthias.ringwald break; 422110e830c9Smatthias.ringwald } 422210e830c9Smatthias.ringwald } 422310e830c9Smatthias.ringwald 4224d6b06661SMatthias Ringwald /** 4225d6b06661SMatthias Ringwald * @brief Add event packet handler. 4226d6b06661SMatthias Ringwald */ 4227d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 4228d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 4229d6b06661SMatthias Ringwald } 4230d6b06661SMatthias Ringwald 423167f708e0SMatthias Ringwald /** 423267f708e0SMatthias Ringwald * @brief Remove event packet handler. 423367f708e0SMatthias Ringwald */ 423467f708e0SMatthias Ringwald void hci_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){ 423567f708e0SMatthias Ringwald btstack_linked_list_remove(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 423667f708e0SMatthias Ringwald } 4237d6b06661SMatthias Ringwald 4238fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 42393d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 4240fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 424116833f0aSmatthias.ringwald } 424216833f0aSmatthias.ringwald 424335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 42448abbe8b5SMatthias Ringwald /** 42458abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 42468abbe8b5SMatthias Ringwald */ 42473d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 42488abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 42498abbe8b5SMatthias Ringwald } 425035454696SMatthias Ringwald #endif 42518abbe8b5SMatthias Ringwald 42520a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 42535027c498SMatthias Ringwald void hci_register_iso_packet_handler(btstack_packet_handler_t handler){ 42545027c498SMatthias Ringwald hci_stack->iso_packet_handler = handler; 42555027c498SMatthias Ringwald } 42565027c498SMatthias Ringwald #endif 42575027c498SMatthias Ringwald 425871de195eSMatthias Ringwald static void hci_state_reset(void){ 4259595bdbfbS[email protected] // no connections yet 4260595bdbfbS[email protected] hci_stack->connections = NULL; 426174308b23Smatthias.ringwald 426274308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 426374308b23Smatthias.ringwald // hci_stack->discoverable = 0; 426474308b23Smatthias.ringwald // hci_stack->connectable = 0; 426574308b23Smatthias.ringwald // hci_stack->bondable = 1; 4266b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 4267595bdbfbS[email protected] 426844935e40S[email protected] // buffer is free 426902c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 427044935e40S[email protected] 4271595bdbfbS[email protected] // no pending cmds 4272595bdbfbS[email protected] hci_stack->decline_reason = 0; 4273595bdbfbS[email protected] 4274c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 4275c214d65bSMatthias Ringwald 4276bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 4277496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 427832a12730SMatthias Ringwald hci_stack->page_timeout = 0x6000; // ca. 15 sec 427932a12730SMatthias Ringwald 4280baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic = 428159d59ecfSMatthias Ringwald GAP_TASK_SET_DEFAULT_LINK_POLICY | 428259d59ecfSMatthias Ringwald GAP_TASK_SET_CLASS_OF_DEVICE | 428359d59ecfSMatthias Ringwald GAP_TASK_SET_LOCAL_NAME | 4284bc2dcc03SMatthias Ringwald GAP_TASK_SET_EIR_DATA | 428532a12730SMatthias Ringwald GAP_TASK_WRITE_SCAN_ENABLE | 428632a12730SMatthias Ringwald GAP_TASK_WRITE_PAGE_TIMEOUT; 4287bea424a5SMatthias Ringwald #endif 4288bea424a5SMatthias Ringwald 4289cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 42908d5f0979SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 42911ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 4292cf01e888SMatthias Ringwald #endif 4293cf01e888SMatthias Ringwald 4294595bdbfbS[email protected] // LE 4295b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 4296b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 4297b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 4298d70217a2SMatthias Ringwald #endif 4299d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4300a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 430173799937SMatthias Ringwald hci_stack->le_scanning_param_update = true; 4302b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 4303d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 4304e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 43054bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 43064bafedd5SMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = HCI_CON_HANDLE_INVALID; 43074bafedd5SMatthias Ringwald #endif 4308d70217a2SMatthias Ringwald #endif 4309a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4310935aa76eSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 43116bf435a6SMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_PARAMS_SET) != 0){ 4312a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 4313a61834b6SMatthias Ringwald } 4314a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 4315a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 4316a61834b6SMatthias Ringwald } 4317a61834b6SMatthias Ringwald #endif 431840f86dfdSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 431940f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = 0xff; 432040f86dfdSMatthias Ringwald #endif 4321595bdbfbS[email protected] } 4322595bdbfbS[email protected] 432335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 43242d5e09d6SMatthias Ringwald /** 43252d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 43262d5e09d6SMatthias Ringwald */ 43272d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 43282d5e09d6SMatthias Ringwald // store and open remote device db 43292d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 43302d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 43312d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 43322d5e09d6SMatthias Ringwald } 43332d5e09d6SMatthias Ringwald } 433435454696SMatthias Ringwald #endif 43352d5e09d6SMatthias Ringwald 43362d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 4337475c8125Smatthias.ringwald 43383a9fb326S[email protected] #ifdef HAVE_MALLOC 43393a9fb326S[email protected] if (!hci_stack) { 43403a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 43413a9fb326S[email protected] } 43423a9fb326S[email protected] #else 43433a9fb326S[email protected] hci_stack = &hci_stack_static; 43443a9fb326S[email protected] #endif 434566fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 43463a9fb326S[email protected] 4347475c8125Smatthias.ringwald // reference to use transport layer implementation 43483a9fb326S[email protected] hci_stack->hci_transport = transport; 4349475c8125Smatthias.ringwald 435011e23e5fSmatthias.ringwald // reference to used config 43513a9fb326S[email protected] hci_stack->config = config; 435211e23e5fSmatthias.ringwald 435326a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 435426a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 435526a9b6daSMatthias Ringwald 43568fcba05dSmatthias.ringwald // max acl payload size defined in config.h 43573a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 43588fcba05dSmatthias.ringwald 435916833f0aSmatthias.ringwald // register packet handlers with transport 436010e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 4361f5454fc6Smatthias.ringwald 43623a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 4363e2386ba1S[email protected] 4364e2386ba1S[email protected] // class of device 43653a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 4366a45d6b9fS[email protected] 4367f20168b8Smatthias.ringwald // bondable by default 4368f20168b8Smatthias.ringwald hci_stack->bondable = 1; 4369f20168b8Smatthias.ringwald 4370e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 437163168530SMatthias Ringwald // classic name 437263168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 4373c4c88f1bSJakob Krantz 4374c4c88f1bSJakob Krantz // Master slave policy 4375c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 4376170fafaeSMatthias Ringwald 4377b4eb4420SMatthias Ringwald // Allow Role Switch 4378b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 4379b4eb4420SMatthias Ringwald 438078315a58SMatthias Ringwald // Default / minimum security level = 2 438178315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 438278315a58SMatthias Ringwald 43835dbec6b3SMatthias Ringwald // Default Security Mode 4 43845dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 43855dbec6b3SMatthias Ringwald 4386cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 4387cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 4388d821984bSMatthias Ringwald 4389d821984bSMatthias Ringwald // Link Supervision Timeout 4390d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 4391d821984bSMatthias Ringwald 4392e9f343c8SMatthias Ringwald #endif 439363168530SMatthias Ringwald 439463048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 43953a9fb326S[email protected] hci_stack->ssp_enable = 1; 43963a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 43973a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 43983a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 439969a97523S[email protected] 44005d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 44015d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 44025d23aae8SMatthias Ringwald 4403fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 4404fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 4405d950d659SMatthias Ringwald 4406831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4407831711daSMatthias Ringwald // connection parameter to use for outgoing connections 4408cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 4409cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 4410831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 4411831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 4412831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 4413831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 4414831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 4415831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 44167261e5d8SMatthias Ringwald 44177261e5d8SMatthias Ringwald // default LE Scanning 44188b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 44198b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 44208b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 4421831711daSMatthias Ringwald #endif 4422831711daSMatthias Ringwald 44232b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 44242b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 44252b6ab3e6SMatthias Ringwald #endif 44262b6ab3e6SMatthias Ringwald 4427831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 4428831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 4429831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 4430831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 4431831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 4432831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 4433831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 4434831711daSMatthias Ringwald 443501d4f05fSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 443601d4f05fSMatthias Ringwald hci_stack->iso_packets_to_queue = 1; 443701d4f05fSMatthias Ringwald #endif 443801d4f05fSMatthias Ringwald 4439595bdbfbS[email protected] hci_state_reset(); 4440475c8125Smatthias.ringwald } 4441475c8125Smatthias.ringwald 44424688f216SMatthias Ringwald void hci_deinit(void){ 4443b02cfc15SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 44444688f216SMatthias Ringwald #ifdef HAVE_MALLOC 44454688f216SMatthias Ringwald if (hci_stack) { 44464688f216SMatthias Ringwald free(hci_stack); 44474688f216SMatthias Ringwald } 44484688f216SMatthias Ringwald #endif 44494688f216SMatthias Ringwald hci_stack = NULL; 4450b5bbcbf4SMatthias Ringwald 4451b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 44524688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 4453b5bbcbf4SMatthias Ringwald #endif 44544688f216SMatthias Ringwald } 44554688f216SMatthias Ringwald 44563fb36a29SMatthias Ringwald /** 44573fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 44583fb36a29SMatthias Ringwald */ 44593fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 44603fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 44613fb36a29SMatthias Ringwald 44623fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 44633fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 44643fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 44653fb36a29SMatthias Ringwald } 44663fb36a29SMatthias Ringwald } 44673fb36a29SMatthias Ringwald 4468fb55bd0aSMatthias Ringwald /** 4469d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 4470fb55bd0aSMatthias Ringwald */ 4471fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 4472fb55bd0aSMatthias Ringwald // references to used control implementation 4473fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 4474d0b87befSMatthias Ringwald // init with transport config 4475d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 4476fb55bd0aSMatthias Ringwald } 4477fb55bd0aSMatthias Ringwald 44781f9eb2ccSMatthias Ringwald static void hci_discard_connections(void){ 4479346f8c2cSMatthias Ringwald btstack_linked_list_iterator_t it; 4480346f8c2cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4481346f8c2cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 44821f9eb2ccSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 4483346f8c2cSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 44841f9eb2ccSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 44851f9eb2ccSMatthias Ringwald hci_shutdown_connection(connection); 44861f9eb2ccSMatthias Ringwald } 4487346f8c2cSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 4488346f8c2cSMatthias Ringwald while (hci_stack->iso_streams != NULL){ 4489346f8c2cSMatthias Ringwald hci_iso_stream_finalize((hci_iso_stream_t *) hci_stack->iso_streams); 4490346f8c2cSMatthias Ringwald } 4491346f8c2cSMatthias Ringwald #endif 44921f9eb2ccSMatthias Ringwald } 44931f9eb2ccSMatthias Ringwald 449471de195eSMatthias Ringwald void hci_close(void){ 4495e6d6524dSMatthias Ringwald 4496e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 4497404843c1Smatthias.ringwald // close remote device db 4498a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 4499a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 4500404843c1Smatthias.ringwald } 4501e6d6524dSMatthias Ringwald #endif 45027224be7eSMatthias Ringwald 45031f9eb2ccSMatthias Ringwald hci_discard_connections(); 45047224be7eSMatthias Ringwald 4505f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 45063a9fb326S[email protected] 45073a9fb326S[email protected] #ifdef HAVE_MALLOC 45083a9fb326S[email protected] free(hci_stack); 45093a9fb326S[email protected] #endif 45103a9fb326S[email protected] hci_stack = NULL; 4511404843c1Smatthias.ringwald } 4512404843c1Smatthias.ringwald 4513cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 4514cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 4515cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 4516cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 4517cb70c5abSMatthias Ringwald } 4518cb70c5abSMatthias Ringwald #endif 4519cb70c5abSMatthias Ringwald 452035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4521170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 4522170fafaeSMatthias Ringwald // validate ranage and set 4523170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 4524170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 4525170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 4526170fafaeSMatthias Ringwald } 452778315a58SMatthias Ringwald 4528137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 4529137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 45305dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 4531137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 4532137715ebSMatthias Ringwald } else { 4533137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 4534137715ebSMatthias Ringwald } 45355dbec6b3SMatthias Ringwald } 45365dbec6b3SMatthias Ringwald 45375dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 45385dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 45395dbec6b3SMatthias Ringwald } 45405dbec6b3SMatthias Ringwald 454178315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 454278315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 454378315a58SMatthias Ringwald } 454478315a58SMatthias Ringwald 454578315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 4546d7387af3SMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 4547d7387af3SMatthias Ringwald return LEVEL_4; 4548d7387af3SMatthias Ringwald } 454978315a58SMatthias Ringwald return hci_stack->gap_security_level; 455078315a58SMatthias Ringwald } 455130cdf3c6SMatthias Ringwald 45528ad4dfffSMatthias Ringwald void gap_set_minimal_service_security_level(gap_security_level_t security_level){ 45538ad4dfffSMatthias Ringwald hci_stack->gap_minimal_service_security_level = security_level; 45548ad4dfffSMatthias Ringwald } 45558ad4dfffSMatthias Ringwald 455630cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 455730cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 455830cdf3c6SMatthias Ringwald } 455930cdf3c6SMatthias Ringwald 456030cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 456130cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 456230cdf3c6SMatthias Ringwald } 4563170fafaeSMatthias Ringwald #endif 4564170fafaeSMatthias Ringwald 4565170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 456660b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 45679e61646fS[email protected] hci_stack->class_of_device = class_of_device; 4568baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_CLASS_OF_DEVICE; 456959d59ecfSMatthias Ringwald hci_run(); 45709e61646fS[email protected] } 457176f27cffSMatthias Ringwald 4572c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 4573c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 4574baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_DEFAULT_LINK_POLICY; 457559d59ecfSMatthias Ringwald hci_run(); 4576c33e56d3SMatthias Ringwald } 4577c33e56d3SMatthias Ringwald 4578b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 4579b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 4580b4eb4420SMatthias Ringwald } 4581b4eb4420SMatthias Ringwald 4582b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 4583b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 4584b4eb4420SMatthias Ringwald } 4585b4eb4420SMatthias Ringwald 45860c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 45870c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 45880c3eb48dSMatthias Ringwald } 45890c3eb48dSMatthias Ringwald 45909afaa4d4SMatthias Ringwald void gap_enable_link_watchdog(uint16_t timeout_ms){ 45919afaa4d4SMatthias Ringwald hci_stack->automatic_flush_timeout = btstack_min(timeout_ms, 1280) * 8 / 5; // divide by 0.625 45929afaa4d4SMatthias Ringwald } 45939afaa4d4SMatthias Ringwald 45949afaa4d4SMatthias Ringwald uint16_t hci_automatic_flush_timeout(void){ 45959afaa4d4SMatthias Ringwald return hci_stack->automatic_flush_timeout; 45969afaa4d4SMatthias Ringwald } 45979afaa4d4SMatthias Ringwald 459876f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 459976f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 460076f27cffSMatthias Ringwald } 460135454696SMatthias Ringwald #endif 46029e61646fS[email protected] 46036fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 4604f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 4605f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 46066535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 4607f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 4608f456b2d0S[email protected] } 460976f27cffSMatthias Ringwald #endif 4610f456b2d0S[email protected] 46118d213e1aSmatthias.ringwald // State-Module-Driver overview 46128d213e1aSmatthias.ringwald // state module low-level 46138d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 46148d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 46158d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 46168d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 4617d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 4618d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 4619c7e0c5f6Smatthias.ringwald 462040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 46217301ad89Smatthias.ringwald 4622038bc64cSmatthias.ringwald // power on 4623f9a30166Smatthias.ringwald int err = 0; 46243a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 4625d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 4626f9a30166Smatthias.ringwald } 4627038bc64cSmatthias.ringwald if (err){ 46289da54300S[email protected] log_error( "POWER_ON failed"); 4629038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 4630038bc64cSmatthias.ringwald return err; 4631038bc64cSmatthias.ringwald } 4632038bc64cSmatthias.ringwald 463324b3c629SMatthias Ringwald // int chipset driver 46343fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 46353fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 46363fb36a29SMatthias Ringwald } 46373fb36a29SMatthias Ringwald 463824b3c629SMatthias Ringwald // init transport 463924b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 464024b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 464124b3c629SMatthias Ringwald } 464224b3c629SMatthias Ringwald 464324b3c629SMatthias Ringwald // open transport 464424b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 4645038bc64cSmatthias.ringwald if (err){ 46469da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 46473a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4648d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4649f9a30166Smatthias.ringwald } 4650038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 4651038bc64cSmatthias.ringwald return err; 4652038bc64cSmatthias.ringwald } 46538d213e1aSmatthias.ringwald return 0; 46548d213e1aSmatthias.ringwald } 4655038bc64cSmatthias.ringwald 465640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 46578d213e1aSmatthias.ringwald 46589da54300S[email protected] log_info("hci_power_control_off"); 46599418f9c9Smatthias.ringwald 46608d213e1aSmatthias.ringwald // close low-level device 466124b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 46628d213e1aSmatthias.ringwald 46639da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 46649418f9c9Smatthias.ringwald 46658d213e1aSmatthias.ringwald // power off 46663a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4667d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 46688d213e1aSmatthias.ringwald } 46699418f9c9Smatthias.ringwald 46709da54300S[email protected] log_info("hci_power_control_off - control closed"); 46719418f9c9Smatthias.ringwald 46723a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 467372ea5239Smatthias.ringwald } 467472ea5239Smatthias.ringwald 467540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 467672ea5239Smatthias.ringwald 46779da54300S[email protected] log_info("hci_power_control_sleep"); 46783144bce4Smatthias.ringwald 4679b429b9b7Smatthias.ringwald #if 0 4680b429b9b7Smatthias.ringwald // don't close serial port during sleep 4681b429b9b7Smatthias.ringwald 468272ea5239Smatthias.ringwald // close low-level device 46833a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 4684b429b9b7Smatthias.ringwald #endif 468572ea5239Smatthias.ringwald 468672ea5239Smatthias.ringwald // sleep mode 46873a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 4688d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 468972ea5239Smatthias.ringwald } 4690b429b9b7Smatthias.ringwald 46913a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 46928d213e1aSmatthias.ringwald } 46938d213e1aSmatthias.ringwald 469440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 4695b429b9b7Smatthias.ringwald 46969da54300S[email protected] log_info("hci_power_control_wake"); 4697b429b9b7Smatthias.ringwald 4698b429b9b7Smatthias.ringwald // wake on 46993a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 4700d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 4701b429b9b7Smatthias.ringwald } 4702b429b9b7Smatthias.ringwald 4703b429b9b7Smatthias.ringwald #if 0 4704b429b9b7Smatthias.ringwald // open low-level device 47053a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 4706b429b9b7Smatthias.ringwald if (err){ 47079da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 47083a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4709d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4710b429b9b7Smatthias.ringwald } 4711b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 4712b429b9b7Smatthias.ringwald return err; 4713b429b9b7Smatthias.ringwald } 4714b429b9b7Smatthias.ringwald #endif 4715b429b9b7Smatthias.ringwald 4716b429b9b7Smatthias.ringwald return 0; 4717b429b9b7Smatthias.ringwald } 4718b429b9b7Smatthias.ringwald 47190c88d5efSMatthias Ringwald static void hci_power_enter_initializing_state(void){ 472044935e40S[email protected] // set up state machine 472144935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 472202c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 472344935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 47245c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 472544935e40S[email protected] } 4726b429b9b7Smatthias.ringwald 47270c88d5efSMatthias Ringwald static void hci_power_enter_halting_state(void){ 47280c88d5efSMatthias Ringwald #ifdef ENABLE_BLE 47290c88d5efSMatthias Ringwald hci_whitelist_free(); 4730a2c9d908SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 4731a2c9d908SMatthias Ringwald hci_periodic_advertiser_list_free(); 4732a2c9d908SMatthias Ringwald #endif 47330c88d5efSMatthias Ringwald #endif 4734184dbe2fSMatthias Ringwald // see hci_run 4735184dbe2fSMatthias Ringwald hci_stack->state = HCI_STATE_HALTING; 47360c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_CLASSIC_STOP; 4737184dbe2fSMatthias Ringwald // setup watchdog timer for disconnect - only triggers if Controller does not respond anymore 4738184dbe2fSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 1000); 4739184dbe2fSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4740184dbe2fSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 4741184dbe2fSMatthias Ringwald } 4742184dbe2fSMatthias Ringwald 474342bd3d77SMatthias Ringwald // returns error 474442bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 474542bd3d77SMatthias Ringwald int err; 47468d213e1aSmatthias.ringwald switch (power_mode){ 47478d213e1aSmatthias.ringwald case HCI_POWER_ON: 47488d213e1aSmatthias.ringwald err = hci_power_control_on(); 474942bd3d77SMatthias Ringwald if (err != 0) { 4750f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 475197b61c7bS[email protected] return err; 475297b61c7bS[email protected] } 47530c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 47548d213e1aSmatthias.ringwald break; 47558d213e1aSmatthias.ringwald case HCI_POWER_OFF: 47568d213e1aSmatthias.ringwald // do nothing 47578d213e1aSmatthias.ringwald break; 47588d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4759b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 47608d213e1aSmatthias.ringwald break; 47617bbeb3adSMilanka Ringwald default: 47627bbeb3adSMilanka Ringwald btstack_assert(false); 47637bbeb3adSMilanka Ringwald break; 47648d213e1aSmatthias.ringwald } 476542bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 476642bd3d77SMatthias Ringwald } 47677301ad89Smatthias.ringwald 476842bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 47698d213e1aSmatthias.ringwald switch (power_mode){ 47708d213e1aSmatthias.ringwald case HCI_POWER_ON: 47718d213e1aSmatthias.ringwald // do nothing 47728d213e1aSmatthias.ringwald break; 47738d213e1aSmatthias.ringwald case HCI_POWER_OFF: 47748d213e1aSmatthias.ringwald // no connections yet, just turn it off 47758d213e1aSmatthias.ringwald hci_power_control_off(); 47768d213e1aSmatthias.ringwald break; 47778d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4778b546ac54Smatthias.ringwald // no connections yet, just turn it off 477972ea5239Smatthias.ringwald hci_power_control_sleep(); 47808d213e1aSmatthias.ringwald break; 47817bbeb3adSMilanka Ringwald default: 47827bbeb3adSMilanka Ringwald btstack_assert(false); 47837bbeb3adSMilanka Ringwald break; 47848d213e1aSmatthias.ringwald } 478542bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 478642bd3d77SMatthias Ringwald } 47877301ad89Smatthias.ringwald 478842bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 47898d213e1aSmatthias.ringwald switch (power_mode){ 47908d213e1aSmatthias.ringwald case HCI_POWER_ON: 47918d213e1aSmatthias.ringwald // do nothing 47928d213e1aSmatthias.ringwald break; 47938d213e1aSmatthias.ringwald case HCI_POWER_OFF: 47940c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 47958d213e1aSmatthias.ringwald break; 47968d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4797b546ac54Smatthias.ringwald // see hci_run 47983a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 479974b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 48008d213e1aSmatthias.ringwald break; 48017bbeb3adSMilanka Ringwald default: 48027bbeb3adSMilanka Ringwald btstack_assert(false); 48037bbeb3adSMilanka Ringwald break; 48048d213e1aSmatthias.ringwald } 480542bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 480642bd3d77SMatthias Ringwald } 48077301ad89Smatthias.ringwald 480842bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 48098d213e1aSmatthias.ringwald switch (power_mode){ 48108d213e1aSmatthias.ringwald case HCI_POWER_ON: 48110c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 48128d213e1aSmatthias.ringwald break; 48138d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48148d213e1aSmatthias.ringwald // do nothing 48158d213e1aSmatthias.ringwald break; 48168d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4817b546ac54Smatthias.ringwald // see hci_run 48183a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 481974b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 48208d213e1aSmatthias.ringwald break; 48217bbeb3adSMilanka Ringwald default: 48227bbeb3adSMilanka Ringwald btstack_assert(false); 48237bbeb3adSMilanka Ringwald break; 48248d213e1aSmatthias.ringwald } 482500278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 482642bd3d77SMatthias Ringwald } 48278d213e1aSmatthias.ringwald 482842bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 48298d213e1aSmatthias.ringwald switch (power_mode){ 48308d213e1aSmatthias.ringwald case HCI_POWER_ON: 48310c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 48328d213e1aSmatthias.ringwald break; 48338d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48340c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 48358d213e1aSmatthias.ringwald break; 48368d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4837b546ac54Smatthias.ringwald // do nothing 48388d213e1aSmatthias.ringwald break; 48397bbeb3adSMilanka Ringwald default: 48407bbeb3adSMilanka Ringwald btstack_assert(false); 48417bbeb3adSMilanka Ringwald break; 48428d213e1aSmatthias.ringwald } 484342bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 484442bd3d77SMatthias Ringwald } 48458d213e1aSmatthias.ringwald 484642bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 484742bd3d77SMatthias Ringwald int err; 48488d213e1aSmatthias.ringwald switch (power_mode){ 48498d213e1aSmatthias.ringwald case HCI_POWER_ON: 48503144bce4Smatthias.ringwald err = hci_power_control_wake(); 48513144bce4Smatthias.ringwald if (err) return err; 48520c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 48538d213e1aSmatthias.ringwald break; 48548d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48550c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 48568d213e1aSmatthias.ringwald break; 48578d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4858b546ac54Smatthias.ringwald // do nothing 48598d213e1aSmatthias.ringwald break; 48607bbeb3adSMilanka Ringwald default: 48617bbeb3adSMilanka Ringwald btstack_assert(false); 48627bbeb3adSMilanka Ringwald break; 48638d213e1aSmatthias.ringwald } 486442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 486542bd3d77SMatthias Ringwald } 48667bbeb3adSMilanka Ringwald 486742bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 486842bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 486924f87726SMatthias Ringwald int err = 0; 487042bd3d77SMatthias Ringwald switch (hci_stack->state){ 487142bd3d77SMatthias Ringwald case HCI_STATE_OFF: 487242bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 487342bd3d77SMatthias Ringwald break; 487442bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 487542bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 487642bd3d77SMatthias Ringwald break; 487742bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 487842bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 487942bd3d77SMatthias Ringwald break; 488042bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 488142bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 488242bd3d77SMatthias Ringwald break; 488342bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 488442bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 488542bd3d77SMatthias Ringwald break; 488642bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 488742bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 488842bd3d77SMatthias Ringwald break; 48897bbeb3adSMilanka Ringwald default: 48907bbeb3adSMilanka Ringwald btstack_assert(false); 48917bbeb3adSMilanka Ringwald break; 489211e23e5fSmatthias.ringwald } 489324f87726SMatthias Ringwald if (err != 0){ 489442bd3d77SMatthias Ringwald return err; 489542bd3d77SMatthias Ringwald } 489668d92d03Smatthias.ringwald 4897038bc64cSmatthias.ringwald // create internal event 4898ee8bf225Smatthias.ringwald hci_emit_state(); 4899ee8bf225Smatthias.ringwald 490068d92d03Smatthias.ringwald // trigger next/first action 490168d92d03Smatthias.ringwald hci_run(); 490268d92d03Smatthias.ringwald 4903475c8125Smatthias.ringwald return 0; 4904475c8125Smatthias.ringwald } 4905475c8125Smatthias.ringwald 490635454696SMatthias Ringwald 4907091e35e3SMatthias Ringwald static void hci_halting_run(void) { 4908091e35e3SMatthias Ringwald 4909091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 4910091e35e3SMatthias Ringwald 4911091e35e3SMatthias Ringwald hci_connection_t *connection; 4912d5f71a7eSMatthias Ringwald #ifdef ENABLE_BLE 4913d5f71a7eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4914911801caSMatthias Ringwald bool stop_advertismenets; 4915d5f71a7eSMatthias Ringwald #endif 4916d5f71a7eSMatthias Ringwald #endif 4917091e35e3SMatthias Ringwald 4918091e35e3SMatthias Ringwald switch (hci_stack->substate) { 49190c88d5efSMatthias Ringwald case HCI_HALTING_CLASSIC_STOP: 49200c88d5efSMatthias Ringwald #ifdef ENABLE_CLASSIC 4921911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4922911801caSMatthias Ringwald 49230c88d5efSMatthias Ringwald if (hci_stack->connectable || hci_stack->discoverable){ 49240c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_ADV_STOP; 49250c88d5efSMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, 0); 49260c88d5efSMatthias Ringwald return; 49270c88d5efSMatthias Ringwald } 49280c88d5efSMatthias Ringwald #endif 49290c88d5efSMatthias Ringwald /* fall through */ 49300c88d5efSMatthias Ringwald 49310c88d5efSMatthias Ringwald case HCI_HALTING_LE_ADV_STOP: 49320c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_ADV_STOP; 49330c88d5efSMatthias Ringwald 49340c88d5efSMatthias Ringwald #ifdef ENABLE_BLE 49350c88d5efSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4936911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4937911801caSMatthias Ringwald 4938911801caSMatthias Ringwald stop_advertismenets = (hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0; 4939911801caSMatthias Ringwald 4940911801caSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 4941911801caSMatthias Ringwald if (hci_extended_advertising_supported()){ 4942c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 4943911801caSMatthias Ringwald btstack_linked_list_iterator_t it; 4944911801caSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 4945911801caSMatthias Ringwald // stop all periodic advertisements and check if an extended set is active 4946911801caSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4947911801caSMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 4948911801caSMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) != 0) { 4949911801caSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 4950911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, 0, advertising_set->advertising_handle); 49510c88d5efSMatthias Ringwald return; 49520c88d5efSMatthias Ringwald } 4953911801caSMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0) { 4954911801caSMatthias Ringwald stop_advertismenets = true; 4955911801caSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 4956911801caSMatthias Ringwald } 4957911801caSMatthias Ringwald } 4958c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 4959911801caSMatthias Ringwald if (stop_advertismenets){ 4960911801caSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 4961911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 0, NULL, NULL, NULL); 4962911801caSMatthias Ringwald return; 4963911801caSMatthias Ringwald } 4964911801caSMatthias Ringwald } 4965911801caSMatthias Ringwald else 4966a2c9d908SMatthias Ringwald #else /* ENABLE_LE_PERIPHERAL */ 4967911801caSMatthias Ringwald { 4968911801caSMatthias Ringwald if (stop_advertismenets) { 4969911801caSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 4970911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 4971911801caSMatthias Ringwald return; 4972911801caSMatthias Ringwald } 4973911801caSMatthias Ringwald } 4974911801caSMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING*/ 4975911801caSMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 4976911801caSMatthias Ringwald #endif /* ENABLE_BLE */ 4977911801caSMatthias Ringwald 49780c88d5efSMatthias Ringwald /* fall through */ 49790c88d5efSMatthias Ringwald 49800c88d5efSMatthias Ringwald case HCI_HALTING_LE_SCAN_STOP: 49810c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_SCAN_STOP; 4982911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 4983091e35e3SMatthias Ringwald 4984091e35e3SMatthias Ringwald #ifdef ENABLE_BLE 4985091e35e3SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 49860c88d5efSMatthias Ringwald if (hci_stack->le_scanning_active){ 4987c42da3bcSMatthias Ringwald hci_le_scan_stop(); 49880c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL; 49890c88d5efSMatthias Ringwald return; 49900c88d5efSMatthias Ringwald } 4991091e35e3SMatthias Ringwald #endif 4992091e35e3SMatthias Ringwald #endif 49930c88d5efSMatthias Ringwald 49940c88d5efSMatthias Ringwald /* fall through */ 49950c88d5efSMatthias Ringwald 49960c88d5efSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL: 49970c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL; 4998911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 49990c88d5efSMatthias Ringwald 5000091e35e3SMatthias Ringwald // close all open connections 5001091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 5002091e35e3SMatthias Ringwald if (connection) { 5003091e35e3SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 5004091e35e3SMatthias Ringwald 5005091e35e3SMatthias Ringwald // check state 5006091e35e3SMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 5007091e35e3SMatthias Ringwald connection->state = SENT_DISCONNECT; 5008091e35e3SMatthias Ringwald 5009091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 5010091e35e3SMatthias Ringwald 5011091e35e3SMatthias Ringwald // finally, send the disconnect command 5012091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5013091e35e3SMatthias Ringwald return; 5014091e35e3SMatthias Ringwald } 5015091e35e3SMatthias Ringwald 5016346f8c2cSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 5017346f8c2cSMatthias Ringwald // stop BIGs and BIG Syncs 5018346f8c2cSMatthias Ringwald if (hci_stack->le_audio_bigs != NULL){ 5019346f8c2cSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t*) hci_stack->le_audio_bigs; 5020346f8c2cSMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_TERMINATED) return; 5021346f8c2cSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 5022346f8c2cSMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle); 5023346f8c2cSMatthias Ringwald return; 5024346f8c2cSMatthias Ringwald } 5025346f8c2cSMatthias Ringwald if (hci_stack->le_audio_big_syncs != NULL){ 5026346f8c2cSMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t*) hci_stack->le_audio_big_syncs; 5027346f8c2cSMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_TERMINATED) return; 5028346f8c2cSMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 5029346f8c2cSMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 5030346f8c2cSMatthias Ringwald return; 5031346f8c2cSMatthias Ringwald } 5032346f8c2cSMatthias Ringwald #endif 5033346f8c2cSMatthias Ringwald 5034091e35e3SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 5035091e35e3SMatthias Ringwald 5036091e35e3SMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 5037091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 50380c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_CLOSE_TIMER; 5039091e35e3SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 5040091e35e3SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 5041091e35e3SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 5042091e35e3SMatthias Ringwald break; 5043091e35e3SMatthias Ringwald 5044091e35e3SMatthias Ringwald case HCI_HALTING_CLOSE: 5045091e35e3SMatthias Ringwald // close left over connections (that had not been properly closed before) 5046091e35e3SMatthias Ringwald hci_discard_connections(); 5047091e35e3SMatthias Ringwald 5048091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling off"); 5049091e35e3SMatthias Ringwald 5050091e35e3SMatthias Ringwald // switch mode 5051091e35e3SMatthias Ringwald hci_power_control_off(); 5052091e35e3SMatthias Ringwald 5053091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, emitting state"); 5054091e35e3SMatthias Ringwald hci_emit_state(); 5055091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, done"); 5056091e35e3SMatthias Ringwald break; 5057091e35e3SMatthias Ringwald 50580c88d5efSMatthias Ringwald case HCI_HALTING_W4_CLOSE_TIMER: 5059091e35e3SMatthias Ringwald // keep waiting 5060091e35e3SMatthias Ringwald 5061091e35e3SMatthias Ringwald break; 5062091e35e3SMatthias Ringwald default: 5063091e35e3SMatthias Ringwald break; 5064091e35e3SMatthias Ringwald } 5065091e35e3SMatthias Ringwald }; 5066091e35e3SMatthias Ringwald 5067091e35e3SMatthias Ringwald static void hci_falling_asleep_run(void){ 5068091e35e3SMatthias Ringwald hci_connection_t * connection; 5069091e35e3SMatthias Ringwald switch(hci_stack->substate) { 5070091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 5071091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP"); 5072091e35e3SMatthias Ringwald // close all open connections 5073091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 5074091e35e3SMatthias Ringwald if (connection){ 5075091e35e3SMatthias Ringwald 5076091e35e3SMatthias Ringwald // send disconnect 5077091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5078091e35e3SMatthias Ringwald 5079091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 5080091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5081091e35e3SMatthias Ringwald 5082091e35e3SMatthias Ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 5083091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 5084091e35e3SMatthias Ringwald return; 5085091e35e3SMatthias Ringwald } 5086091e35e3SMatthias Ringwald 5087091e35e3SMatthias Ringwald if (hci_classic_supported()){ 5088091e35e3SMatthias Ringwald // disable page and inquiry scan 5089091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5090091e35e3SMatthias Ringwald 5091091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, disabling inq scans"); 5092091e35e3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 5093091e35e3SMatthias Ringwald 5094091e35e3SMatthias Ringwald // continue in next sub state 5095091e35e3SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 5096091e35e3SMatthias Ringwald break; 5097091e35e3SMatthias Ringwald } 5098091e35e3SMatthias Ringwald 5099091e35e3SMatthias Ringwald /* fall through */ 5100091e35e3SMatthias Ringwald 5101091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 5102091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling sleep"); 5103091e35e3SMatthias Ringwald // switch mode 5104091e35e3SMatthias Ringwald hci_power_control_sleep(); // changes hci_stack->state to SLEEP 5105091e35e3SMatthias Ringwald hci_emit_state(); 5106091e35e3SMatthias Ringwald break; 5107091e35e3SMatthias Ringwald 5108091e35e3SMatthias Ringwald default: 5109091e35e3SMatthias Ringwald break; 5110091e35e3SMatthias Ringwald } 5111091e35e3SMatthias Ringwald } 5112091e35e3SMatthias Ringwald 511335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 511435454696SMatthias Ringwald 5115758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 5116758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 5117a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 5118baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_SCAN_ENABLE; 5119758b46ceSmatthias.ringwald hci_run(); 5120758b46ceSmatthias.ringwald } 5121758b46ceSmatthias.ringwald 512215a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 5123381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 5124381fbed8Smatthias.ringwald 51253a9fb326S[email protected] if (hci_stack->discoverable == enable){ 512671fd255dSMatthias Ringwald hci_emit_scan_mode_changed(hci_stack->discoverable, hci_stack->connectable); 5127381fbed8Smatthias.ringwald return; 5128381fbed8Smatthias.ringwald } 5129381fbed8Smatthias.ringwald 51303a9fb326S[email protected] hci_stack->discoverable = enable; 5131758b46ceSmatthias.ringwald hci_update_scan_enable(); 5132758b46ceSmatthias.ringwald } 5133b031bebbSmatthias.ringwald 513415a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 5135758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 5136758b46ceSmatthias.ringwald 5137758b46ceSmatthias.ringwald // don't emit event 51383a9fb326S[email protected] if (hci_stack->connectable == enable) return; 5139758b46ceSmatthias.ringwald 51403a9fb326S[email protected] hci_stack->connectable = enable; 5141758b46ceSmatthias.ringwald hci_update_scan_enable(); 5142381fbed8Smatthias.ringwald } 514335454696SMatthias Ringwald #endif 5144381fbed8Smatthias.ringwald 514515a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 51466535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 51475061f3afS[email protected] } 51485061f3afS[email protected] 51492b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 51502b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 51512b838201SMatthias Ringwald 51522b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 51532b838201SMatthias Ringwald hci_reserve_packet_buffer(); 51542b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 51552b838201SMatthias Ringwald 51562b838201SMatthias Ringwald uint16_t size = 0; 51572b838201SMatthias Ringwald uint16_t num_handles = 0; 51582b838201SMatthias Ringwald packet[size++] = 0x35; 51592b838201SMatthias Ringwald packet[size++] = 0x0c; 51602b838201SMatthias Ringwald size++; // skip param len 51612b838201SMatthias Ringwald size++; // skip num handles 51622b838201SMatthias Ringwald 51632b838201SMatthias Ringwald // add { handle, packets } entries 51642b838201SMatthias Ringwald btstack_linked_item_t * it; 51652b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 51662b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 51672b838201SMatthias Ringwald if (connection->num_packets_completed){ 51682b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 51692b838201SMatthias Ringwald size += 2; 51702b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 51712b838201SMatthias Ringwald size += 2; 51722b838201SMatthias Ringwald // 51732b838201SMatthias Ringwald num_handles++; 51742b838201SMatthias Ringwald connection->num_packets_completed = 0; 51752b838201SMatthias Ringwald } 51762b838201SMatthias Ringwald } 51772b838201SMatthias Ringwald 51782b838201SMatthias Ringwald packet[2] = size - 3; 51792b838201SMatthias Ringwald packet[3] = num_handles; 51802b838201SMatthias Ringwald 51812b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 51822b838201SMatthias Ringwald 51832b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 51842b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 51852b838201SMatthias Ringwald 51862b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 51872b838201SMatthias Ringwald if (hci_transport_synchronous()){ 5188e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5189068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 51902b838201SMatthias Ringwald } 51912b838201SMatthias Ringwald } 51922b838201SMatthias Ringwald #endif 51932b838201SMatthias Ringwald 519426fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 519526fe9592SMatthias Ringwald UNUSED(ds); 519626fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 5197beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 5198beceeddeSMatthias Ringwald hci_emit_state(); 519926fe9592SMatthias Ringwald hci_run(); 520026fe9592SMatthias Ringwald } 520126fe9592SMatthias Ringwald 5202f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 52034ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 5204b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 5205b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 5206b5d8b22bS[email protected] if (connection) { 520728a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 5208b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 5209f30077b7SMatthias Ringwald return true; 5210b5d8b22bS[email protected] } 521128a0332dSMatthias Ringwald } else { 5212b5d8b22bS[email protected] // connection gone -> discard further fragments 521328a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 5214b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 5215b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 5216b5d8b22bS[email protected] } 5217b5d8b22bS[email protected] } 5218f30077b7SMatthias Ringwald return false; 52192b838201SMatthias Ringwald } 5220b031bebbSmatthias.ringwald 522168ab6207SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 522268ab6207SMatthias Ringwald static bool hci_run_iso_fragments(void){ 522368ab6207SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size > 0u) { 522468ab6207SMatthias Ringwald // TODO: flow control 522568ab6207SMatthias Ringwald if (hci_transport_can_send_prepared_packet_now(HCI_ISO_DATA_PACKET)){ 522668ab6207SMatthias Ringwald hci_send_iso_packet_fragments(); 522768ab6207SMatthias Ringwald return true; 522868ab6207SMatthias Ringwald } 522968ab6207SMatthias Ringwald } 523068ab6207SMatthias Ringwald return false; 523168ab6207SMatthias Ringwald } 523268ab6207SMatthias Ringwald #endif 523368ab6207SMatthias Ringwald 523435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 52356c0d5426SMatthias Ringwald 52366c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 52376c0d5426SMatthias Ringwald static bool hci_classic_operation_active(void) { 52386c0d5426SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_STATE_W4_ACTIVE){ 52396c0d5426SMatthias Ringwald return true; 52406c0d5426SMatthias Ringwald } 52416c0d5426SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 52426c0d5426SMatthias Ringwald return true; 52436c0d5426SMatthias Ringwald } 52446c0d5426SMatthias Ringwald btstack_linked_item_t * it; 52456c0d5426SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next) { 52466c0d5426SMatthias Ringwald hci_connection_t *connection = (hci_connection_t *) it; 52476c0d5426SMatthias Ringwald switch (connection->state) { 52486c0d5426SMatthias Ringwald case SENT_CREATE_CONNECTION: 52496c0d5426SMatthias Ringwald case SENT_CANCEL_CONNECTION: 52506c0d5426SMatthias Ringwald case SENT_DISCONNECT: 52516c0d5426SMatthias Ringwald return true; 52526c0d5426SMatthias Ringwald default: 52536c0d5426SMatthias Ringwald break; 52546c0d5426SMatthias Ringwald } 52556c0d5426SMatthias Ringwald } 52566c0d5426SMatthias Ringwald return false; 52576c0d5426SMatthias Ringwald } 52586c0d5426SMatthias Ringwald #endif 52596c0d5426SMatthias Ringwald 5260f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 5261f30077b7SMatthias Ringwald 526259d59ecfSMatthias Ringwald // assert stack is working and classic is active 526359d59ecfSMatthias Ringwald if (hci_classic_supported() == false) return false; 526459d59ecfSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 526559d59ecfSMatthias Ringwald 5266b031bebbSmatthias.ringwald // decline incoming connections 52673a9fb326S[email protected] if (hci_stack->decline_reason){ 52683a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 52693a9fb326S[email protected] hci_stack->decline_reason = 0; 52703a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 5271f30077b7SMatthias Ringwald return true; 5272ce4c8fabSmatthias.ringwald } 527359d59ecfSMatthias Ringwald 5274baa4881eSMatthias Ringwald if (hci_stack->gap_tasks_classic != 0){ 5275ab4831a3SMatthias Ringwald hci_run_gap_tasks_classic(); 5276f30077b7SMatthias Ringwald return true; 5277b031bebbSmatthias.ringwald } 527827741fe7SMatthias Ringwald 5279f5875de5SMatthias Ringwald // start/stop inquiry 5280a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 52816c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 52826c0d5426SMatthias Ringwald if (hci_classic_operation_active() == false) 52836c0d5426SMatthias Ringwald #endif 52846c0d5426SMatthias Ringwald { 5285f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 5286beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 528730199e24SMatthias Ringwald if (hci_stack->inquiry_max_period_length != 0){ 528830199e24SMatthias Ringwald hci_send_cmd(&hci_periodic_inquiry_mode, hci_stack->inquiry_max_period_length, hci_stack->inquiry_min_period_length, hci_stack->inquiry_lap, duration, 0); 528930199e24SMatthias Ringwald } else { 5290496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 529130199e24SMatthias Ringwald } 5292f30077b7SMatthias Ringwald return true; 5293f5875de5SMatthias Ringwald } 52946c0d5426SMatthias Ringwald } 5295f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 5296f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 5297f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 5298f30077b7SMatthias Ringwald return true; 5299f5875de5SMatthias Ringwald } 530030199e24SMatthias Ringwald 530130199e24SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_EXIT_PERIODIC){ 5302bc06f564SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 530330199e24SMatthias Ringwald hci_send_cmd(&hci_exit_periodic_inquiry_mode); 530430199e24SMatthias Ringwald return true; 530530199e24SMatthias Ringwald } 530630199e24SMatthias Ringwald 5307b7f1ee76SMatthias Ringwald // remote name request 5308b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 53096c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 53106c0d5426SMatthias Ringwald if (hci_classic_operation_active() == false) 53116c0d5426SMatthias Ringwald #endif 53126c0d5426SMatthias Ringwald { 5313b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 5314b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 5315ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 5316f30077b7SMatthias Ringwald return true; 5317b7f1ee76SMatthias Ringwald } 53186c0d5426SMatthias Ringwald } 5319cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 5320cf01e888SMatthias Ringwald // Local OOB data 532159d59ecfSMatthias Ringwald if (hci_stack->classic_read_local_oob_data){ 5322cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 53231e83575aSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND)){ 5324cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 5325cf01e888SMatthias Ringwald } else { 5326cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 5327cf01e888SMatthias Ringwald } 5328cf01e888SMatthias Ringwald } 5329cf01e888SMatthias Ringwald #endif 53300a51f88bSMatthias Ringwald // pairing 53310a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 53320a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 53333f659ee4SMilanka Ringwald uint8_t pin_code[16]; 53340a51f88bSMatthias Ringwald switch (state){ 53350a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 5336cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 53373f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 53383f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 53393f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 53400a51f88bSMatthias Ringwald break; 53410a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 5342cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 53430a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 53440a51f88bSMatthias Ringwald break; 53450a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 5346cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 5347d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 53480a51f88bSMatthias Ringwald break; 53490a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 5350cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 53510a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 53520a51f88bSMatthias Ringwald break; 53530a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 5354cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 53550a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 53560a51f88bSMatthias Ringwald break; 53570a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 5358cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 53590a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 53600a51f88bSMatthias Ringwald break; 53610a51f88bSMatthias Ringwald default: 53620a51f88bSMatthias Ringwald break; 53630a51f88bSMatthias Ringwald } 5364f30077b7SMatthias Ringwald return true; 5365f30077b7SMatthias Ringwald } 5366f30077b7SMatthias Ringwald return false; 53670a51f88bSMatthias Ringwald } 536835454696SMatthias Ringwald #endif 5369b031bebbSmatthias.ringwald 5370a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5371be7ef9d8SMatthias Ringwald 5372c42da3bcSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5373c42da3bcSMatthias Ringwald static void hci_le_scan_stop(void){ 5374c42da3bcSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5375c42da3bcSMatthias Ringwald if (hci_extended_advertising_supported()) { 5376c42da3bcSMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 0, 0, 0, 0); 5377c42da3bcSMatthias Ringwald } else 5378c42da3bcSMatthias Ringwald #endif 5379c42da3bcSMatthias Ringwald { 5380c42da3bcSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 5381c42da3bcSMatthias Ringwald } 5382c42da3bcSMatthias Ringwald } 5383c42da3bcSMatthias Ringwald #endif 5384c42da3bcSMatthias Ringwald 5385be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5386be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5387be7ef9d8SMatthias Ringwald uint8_t hci_le_extended_advertising_operation_for_chunk(uint16_t pos, uint16_t len){ 5388be7ef9d8SMatthias Ringwald uint8_t operation = 0; 5389be7ef9d8SMatthias Ringwald if (pos == 0){ 5390be7ef9d8SMatthias Ringwald // first fragment or complete data 5391be7ef9d8SMatthias Ringwald operation |= 1; 5392be7ef9d8SMatthias Ringwald } 5393be7ef9d8SMatthias Ringwald if (pos + LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN >= len){ 5394be7ef9d8SMatthias Ringwald // last fragment or complete data 5395be7ef9d8SMatthias Ringwald operation |= 2; 5396be7ef9d8SMatthias Ringwald } 5397be7ef9d8SMatthias Ringwald return operation; 5398be7ef9d8SMatthias Ringwald } 5399be7ef9d8SMatthias Ringwald #endif 5400be7ef9d8SMatthias Ringwald #endif 5401be7ef9d8SMatthias Ringwald 5402f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 5403f30077b7SMatthias Ringwald 5404f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t lit; 5405f40c73b4SMatthias Ringwald 5406a41310b7SMatthias Ringwald // Phase 1: collect what to stop 5407a41310b7SMatthias Ringwald 540849a6c69cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5409a41310b7SMatthias Ringwald bool scanning_stop = false; 5410a41310b7SMatthias Ringwald bool connecting_stop = false; 5411be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5412c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 54134bafedd5SMatthias Ringwald bool periodic_sync_stop = false; 54144bafedd5SMatthias Ringwald #endif 5415be7ef9d8SMatthias Ringwald #endif 5416c814a904SMatthias Ringwald #endif 5417be7ef9d8SMatthias Ringwald 541849a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 541949a6c69cSMatthias Ringwald bool advertising_stop = false; 542049a6c69cSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 542149a6c69cSMatthias Ringwald le_advertising_set_t * advertising_stop_set = NULL; 542249a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 542349a6c69cSMatthias Ringwald bool periodic_advertising_stop = false; 5424a41310b7SMatthias Ringwald #endif 542549a6c69cSMatthias Ringwald #endif 5426a41310b7SMatthias Ringwald #endif 5427a41310b7SMatthias Ringwald 54289136bde8SMatthias Ringwald // check if own address changes 54299136bde8SMatthias Ringwald bool random_address_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 54309136bde8SMatthias Ringwald 543129c24bebSMatthias Ringwald // check if whitelist needs modification 543229c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 543329c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 543429c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 543529c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 543629c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 543729c24bebSMatthias Ringwald whitelist_modification_pending = true; 543829c24bebSMatthias Ringwald break; 543929c24bebSMatthias Ringwald } 544029c24bebSMatthias Ringwald } 5441f40c73b4SMatthias Ringwald 544221debf25SMatthias Ringwald // check if resolving list needs modification 544321debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 544421debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 54451ffa425cSMatthias Ringwald bool resolving_list_supported = hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE); 5446ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 544721debf25SMatthias Ringwald resolving_list_modification_pending = true; 544821debf25SMatthias Ringwald } 544921debf25SMatthias Ringwald #endif 545029c24bebSMatthias Ringwald 5451f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 545249a6c69cSMatthias Ringwald 5453f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 545449a6c69cSMatthias Ringwald // check if periodic advertiser list needs modification 5455f40c73b4SMatthias Ringwald bool periodic_list_modification_pending = false; 5456f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 5457f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 5458f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 5459f40c73b4SMatthias Ringwald if (entry->state & (LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER | LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER)){ 5460f40c73b4SMatthias Ringwald periodic_list_modification_pending = true; 5461f40c73b4SMatthias Ringwald break; 5462f40c73b4SMatthias Ringwald } 5463f40c73b4SMatthias Ringwald } 5464f40c73b4SMatthias Ringwald #endif 5465f40c73b4SMatthias Ringwald 5466fde725feSMatthias Ringwald // scanning control 54673251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 546829c24bebSMatthias Ringwald // stop if: 546929c24bebSMatthias Ringwald // - parameter change required 547029c24bebSMatthias Ringwald // - it's disabled 547129c24bebSMatthias Ringwald // - whitelist change required but used for scanning 547221debf25SMatthias Ringwald // - resolving list modified 547351e51a58SMatthias Ringwald // - own address changes 547429c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 547521debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 547621debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 5477d54b2c94SMatthias Ringwald (scanning_uses_whitelist && whitelist_modification_pending) || 547851e51a58SMatthias Ringwald resolving_list_modification_pending || 547951e51a58SMatthias Ringwald random_address_change){ 548021debf25SMatthias Ringwald 54812d5c2a27SMatthias Ringwald scanning_stop = true; 5482fde725feSMatthias Ringwald } 5483fde725feSMatthias Ringwald } 5484fde725feSMatthias Ringwald 548529c24bebSMatthias Ringwald // connecting control 5486f496d06eSMatthias Ringwald bool connecting_with_whitelist; 5487f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 5488f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 5489f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 5490af64f147SMatthias Ringwald // stop connecting if: 5491af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 5492af64f147SMatthias Ringwald // - if it got disabled 549321debf25SMatthias Ringwald // - resolving list modified 549451e51a58SMatthias Ringwald // - own address changes 5495f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 5496f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 549721debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 549851e51a58SMatthias Ringwald resolving_list_modification_pending || 549951e51a58SMatthias Ringwald random_address_change) { 550021debf25SMatthias Ringwald 550129c24bebSMatthias Ringwald connecting_stop = true; 550229c24bebSMatthias Ringwald } 5503f496d06eSMatthias Ringwald break; 5504f496d06eSMatthias Ringwald default: 5505f496d06eSMatthias Ringwald break; 550629c24bebSMatthias Ringwald } 550749a6c69cSMatthias Ringwald 55084bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 550949a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 55104bafedd5SMatthias Ringwald // periodic sync control 55114bafedd5SMatthias Ringwald bool sync_with_advertiser_list; 55124bafedd5SMatthias Ringwald switch(hci_stack->le_periodic_sync_state){ 55134bafedd5SMatthias Ringwald case LE_CONNECTING_DIRECT: 55144bafedd5SMatthias Ringwald case LE_CONNECTING_WHITELIST: 551549a6c69cSMatthias Ringwald // stop sync if: 55164bafedd5SMatthias Ringwald // - sync with advertiser list and advertiser list modification pending 551749a6c69cSMatthias Ringwald // - if it got disabled 55184bafedd5SMatthias Ringwald sync_with_advertiser_list = hci_stack->le_periodic_sync_state == LE_CONNECTING_WHITELIST; 55194bafedd5SMatthias Ringwald if ((sync_with_advertiser_list && periodic_list_modification_pending) || 55204bafedd5SMatthias Ringwald (hci_stack->le_periodic_sync_request == LE_CONNECTING_IDLE)){ 55214bafedd5SMatthias Ringwald periodic_sync_stop = true; 55224bafedd5SMatthias Ringwald } 55234bafedd5SMatthias Ringwald break; 55244bafedd5SMatthias Ringwald default: 55254bafedd5SMatthias Ringwald break; 55264bafedd5SMatthias Ringwald } 55274bafedd5SMatthias Ringwald #endif 5528d70217a2SMatthias Ringwald #endif 552929c24bebSMatthias Ringwald 553049a6c69cSMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 553149a6c69cSMatthias Ringwald 5532d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 553345c102fdSMatthias Ringwald // le advertisement control 5534935aa76eSMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0){ 553529c24bebSMatthias Ringwald // stop if: 553629c24bebSMatthias Ringwald // - parameter change required 55379136bde8SMatthias Ringwald // - random address used in advertising and changes 553829c24bebSMatthias Ringwald // - it's disabled 5539ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 554021debf25SMatthias Ringwald // - resolving list modified 554151e51a58SMatthias Ringwald // - own address changes 5542a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 55439136bde8SMatthias Ringwald bool advertising_uses_random_address = hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC; 55444e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 5545a61834b6SMatthias Ringwald if (advertising_change || 55469136bde8SMatthias Ringwald (advertising_uses_random_address && random_address_change) || 5547a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 55489136bde8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 554951e51a58SMatthias Ringwald resolving_list_modification_pending || 555051e51a58SMatthias Ringwald random_address_change) { 555121debf25SMatthias Ringwald 5552fde725feSMatthias Ringwald advertising_stop = true; 5553fde725feSMatthias Ringwald } 5554fde725feSMatthias Ringwald } 5555be7ef9d8SMatthias Ringwald 5556be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5557be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported() && (advertising_stop == false)){ 5558be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 5559be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5560be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5561be7ef9d8SMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 556253f240c0SMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0) { 5563be7ef9d8SMatthias Ringwald // stop if: 5564be7ef9d8SMatthias Ringwald // - parameter change required 5565be7ef9d8SMatthias Ringwald // - random address used in connectable advertising and changes 5566be7ef9d8SMatthias Ringwald // - it's disabled 5567be7ef9d8SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 5568be7ef9d8SMatthias Ringwald // - resolving list modified 5569be7ef9d8SMatthias Ringwald // - own address changes 5570be7ef9d8SMatthias Ringwald // - advertisement set will be removed 557157a04237SMatthias Ringwald bool advertising_uses_whitelist = advertising_set->extended_params.advertising_filter_policy != 0; 557257a04237SMatthias Ringwald bool advertising_connectable = (advertising_set->extended_params.advertising_event_properties & 1) != 0; 557353f240c0SMatthias Ringwald bool advertising_uses_random_address = 557453f240c0SMatthias Ringwald (advertising_set->extended_params.own_address_type != BD_ADDR_TYPE_LE_PUBLIC) && 557553f240c0SMatthias Ringwald advertising_connectable; 5576be7ef9d8SMatthias Ringwald bool advertising_parameter_change = (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 5577be7ef9d8SMatthias Ringwald bool advertising_enabled = (advertising_set->state & LE_ADVERTISEMENT_STATE_ENABLED) != 0; 557853f240c0SMatthias Ringwald bool advertising_set_random_address_change = 557953f240c0SMatthias Ringwald (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 558053f240c0SMatthias Ringwald bool advertising_set_will_be_removed = 558153f240c0SMatthias Ringwald (advertising_set->state & LE_ADVERTISEMENT_TASKS_REMOVE_SET) != 0; 5582be7ef9d8SMatthias Ringwald if (advertising_parameter_change || 5583be7ef9d8SMatthias Ringwald (advertising_uses_random_address && advertising_set_random_address_change) || 5584be7ef9d8SMatthias Ringwald (advertising_enabled == false) || 5585be7ef9d8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 5586be7ef9d8SMatthias Ringwald resolving_list_modification_pending || 5587be7ef9d8SMatthias Ringwald advertising_set_will_be_removed) { 5588be7ef9d8SMatthias Ringwald 5589be7ef9d8SMatthias Ringwald advertising_stop = true; 5590be7ef9d8SMatthias Ringwald advertising_stop_set = advertising_set; 5591be7ef9d8SMatthias Ringwald break; 5592be7ef9d8SMatthias Ringwald } 5593be7ef9d8SMatthias Ringwald } 5594c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 559553f240c0SMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) != 0) { 559653f240c0SMatthias Ringwald // stop if: 559753f240c0SMatthias Ringwald // - it's disabled 559853f240c0SMatthias Ringwald // - parameter change required 559953f240c0SMatthias Ringwald bool periodic_enabled = (advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED) != 0; 560053f240c0SMatthias Ringwald bool periodic_parameter_change = (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS) != 0; 560153f240c0SMatthias Ringwald if ((periodic_enabled == false) || periodic_parameter_change){ 560249a6c69cSMatthias Ringwald periodic_advertising_stop = true; 560353f240c0SMatthias Ringwald advertising_stop_set = advertising_set; 560453f240c0SMatthias Ringwald } 560553f240c0SMatthias Ringwald } 560649a6c69cSMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 560753f240c0SMatthias Ringwald } 5608be7ef9d8SMatthias Ringwald } 5609be7ef9d8SMatthias Ringwald #endif 5610be7ef9d8SMatthias Ringwald 5611a41310b7SMatthias Ringwald #endif 5612fde725feSMatthias Ringwald 5613a41310b7SMatthias Ringwald 5614a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 5615a41310b7SMatthias Ringwald 5616131d4778SMatthias Ringwald 5617131d4778SMatthias Ringwald // 2.1 Outgoing connection 5618131d4778SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5619131d4778SMatthias Ringwald if (connecting_stop){ 5620131d4778SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 5621131d4778SMatthias Ringwald return true; 5622131d4778SMatthias Ringwald } 5623131d4778SMatthias Ringwald #endif 5624131d4778SMatthias Ringwald 5625131d4778SMatthias Ringwald // 2.2 Scanning 5626a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5627a41310b7SMatthias Ringwald if (scanning_stop){ 56285226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 5629c42da3bcSMatthias Ringwald hci_le_scan_stop(); 5630a41310b7SMatthias Ringwald return true; 5631a41310b7SMatthias Ringwald } 5632a41310b7SMatthias Ringwald 5633131d4778SMatthias Ringwald // 2.3 Periodic Sync 56344bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 563549a6c69cSMatthias Ringwald if (hci_stack->le_periodic_terminate_sync_handle != HCI_CON_HANDLE_INVALID){ 563649a6c69cSMatthias Ringwald uint16_t sync_handle = hci_stack->le_periodic_terminate_sync_handle; 563749a6c69cSMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = HCI_CON_HANDLE_INVALID; 563849a6c69cSMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_terminate_sync, sync_handle); 563949a6c69cSMatthias Ringwald return true; 564049a6c69cSMatthias Ringwald } 564149a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 56424bafedd5SMatthias Ringwald if (periodic_sync_stop){ 56434bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_state = LE_CONNECTING_CANCEL; 56444bafedd5SMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_create_sync_cancel); 56454bafedd5SMatthias Ringwald return true; 56464bafedd5SMatthias Ringwald } 564749a6c69cSMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5648a8016f96SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 5649a8016f96SMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 5650a41310b7SMatthias Ringwald 5651131d4778SMatthias Ringwald // 2.4 Advertising: legacy, extended, periodic 5652a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5653fde725feSMatthias Ringwald if (advertising_stop){ 5654e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5655e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 5656be7ef9d8SMatthias Ringwald uint8_t advertising_stop_handle; 5657be7ef9d8SMatthias Ringwald if (advertising_stop_set != NULL){ 5658be7ef9d8SMatthias Ringwald advertising_stop_handle = advertising_stop_set->advertising_handle; 5659be7ef9d8SMatthias Ringwald advertising_stop_set->state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5660be7ef9d8SMatthias Ringwald } else { 5661be7ef9d8SMatthias Ringwald advertising_stop_handle = 0; 5662be7ef9d8SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5663be7ef9d8SMatthias Ringwald } 5664be7ef9d8SMatthias Ringwald const uint8_t advertising_handles[] = { advertising_stop_handle }; 5665e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 5666e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 5667e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 1, advertising_handles, durations, max_events); 5668e464cd48SMatthias Ringwald } else 5669e464cd48SMatthias Ringwald #endif 5670e464cd48SMatthias Ringwald { 5671be7ef9d8SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 567245c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 5673e464cd48SMatthias Ringwald } 5674f30077b7SMatthias Ringwald return true; 567545c102fdSMatthias Ringwald } 567653f240c0SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5677c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 567849a6c69cSMatthias Ringwald if (periodic_advertising_stop){ 567953f240c0SMatthias Ringwald advertising_stop_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 568053f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, 0, advertising_stop_set->advertising_handle); 568153f240c0SMatthias Ringwald return true; 568253f240c0SMatthias Ringwald } 5683c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5684c814a904SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 5685a8016f96SMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 5686fde725feSMatthias Ringwald 5687131d4778SMatthias Ringwald 5688a41310b7SMatthias Ringwald // Phase 3: modify 5689a41310b7SMatthias Ringwald 56909136bde8SMatthias Ringwald if (random_address_change){ 56919136bde8SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 5692e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5693e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 5694e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, 0, hci_stack->le_random_address); 5695e464cd48SMatthias Ringwald } 5696e464cd48SMatthias Ringwald #endif 5697e464cd48SMatthias Ringwald { 56989136bde8SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, hci_stack->le_random_address); 5699e464cd48SMatthias Ringwald } 57009136bde8SMatthias Ringwald return true; 57019136bde8SMatthias Ringwald } 57029136bde8SMatthias Ringwald 5703a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5704a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 5705a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 570663671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 570763671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 570863671e69SMatthias Ringwald // prepare arrays for all PHYs 570963671e69SMatthias Ringwald uint8_t scan_types[1] = { hci_stack->le_scan_type }; 571063671e69SMatthias Ringwald uint16_t scan_intervals[1] = { hci_stack->le_scan_interval }; 571163671e69SMatthias Ringwald uint16_t scan_windows[1] = { hci_stack->le_scan_window }; 571263671e69SMatthias Ringwald uint8_t scanning_phys = 1; // LE 1M PHY 571363671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_parameters, hci_stack->le_own_addr_type, 571463671e69SMatthias Ringwald hci_stack->le_scan_filter_policy, scanning_phys, scan_types, scan_intervals, scan_windows); 571563671e69SMatthias Ringwald } else 571663671e69SMatthias Ringwald #endif 571763671e69SMatthias Ringwald { 5718a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 5719a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 572063671e69SMatthias Ringwald } 5721a41310b7SMatthias Ringwald return true; 5722a41310b7SMatthias Ringwald } 5723a41310b7SMatthias Ringwald #endif 5724a41310b7SMatthias Ringwald 5725a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 572645c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 572745c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 57286bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 5729e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5730e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5731e464cd48SMatthias Ringwald // map advertisment type to advertising event properties 5732e464cd48SMatthias Ringwald uint16_t adv_event_properties = 0; 5733e464cd48SMatthias Ringwald const uint16_t mapping[] = { 0b00010011, 0b00010101, 0b00011101, 0b00010010, 0b00010000}; 5734e464cd48SMatthias Ringwald if (hci_stack->le_advertisements_type < (sizeof(mapping)/sizeof(uint16_t))){ 5735e464cd48SMatthias Ringwald adv_event_properties = mapping[hci_stack->le_advertisements_type]; 5736e464cd48SMatthias Ringwald } 5737174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5738e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 5739e464cd48SMatthias Ringwald 0, 5740e464cd48SMatthias Ringwald adv_event_properties, 5741e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_min, 5742e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_max, 5743e464cd48SMatthias Ringwald hci_stack->le_advertisements_channel_map, 5744e464cd48SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 5745e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 5746e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address, 5747e464cd48SMatthias Ringwald hci_stack->le_advertisements_filter_policy, 5748e464cd48SMatthias Ringwald 0x7f, // tx power: no preference 5749e464cd48SMatthias Ringwald 0x01, // primary adv phy: LE 1M 5750e464cd48SMatthias Ringwald 0, // secondary adv max skip 5751e464cd48SMatthias Ringwald 0, // secondary adv phy 5752e464cd48SMatthias Ringwald 0, // adv sid 5753e464cd48SMatthias Ringwald 0 // scan request notification 5754e464cd48SMatthias Ringwald ); 5755e464cd48SMatthias Ringwald } 5756e464cd48SMatthias Ringwald #endif 5757e464cd48SMatthias Ringwald { 575845c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 575945c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 576045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 576145c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 57626bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 576345c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 576445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 576545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 576645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 5767e464cd48SMatthias Ringwald } 5768f30077b7SMatthias Ringwald return true; 576945c102fdSMatthias Ringwald } 5770be7ef9d8SMatthias Ringwald 5771501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 5772501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 57737a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 57747a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 57756535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 57766535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 57773c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 5778e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5779e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5780174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5781e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, 0, 0x03, 0x01, hci_stack->le_advertisements_data_len, adv_data_clean); 5782e464cd48SMatthias Ringwald } else 5783e464cd48SMatthias Ringwald #endif 5784e464cd48SMatthias Ringwald { 57857a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 5786e464cd48SMatthias Ringwald } 5787f30077b7SMatthias Ringwald return true; 578845c102fdSMatthias Ringwald } 5789be7ef9d8SMatthias Ringwald 5790501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 5791501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5792f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 5793f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 57946535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 57956535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 57963c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 5797e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5798e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5799174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5800e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, 0, 0x03, 0x01, hci_stack->le_scan_response_data_len, scan_data_clean); 5801e464cd48SMatthias Ringwald } else 5802e464cd48SMatthias Ringwald #endif 5803e464cd48SMatthias Ringwald { 5804214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 5805e464cd48SMatthias Ringwald } 5806f30077b7SMatthias Ringwald return true; 5807501f56b3SMatthias Ringwald } 5808be7ef9d8SMatthias Ringwald 5809be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5810be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported()) { 5811be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 5812be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5813be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5814be7ef9d8SMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 5815be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_REMOVE_SET) != 0) { 5816be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_REMOVE_SET; 5817174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5818be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_remove_advertising_set, advertising_set->advertising_handle); 5819be7ef9d8SMatthias Ringwald return true; 5820be7ef9d8SMatthias Ringwald } 5821f66adbdeSMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0){ 5822f66adbdeSMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 5823f66adbdeSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, advertising_set->advertising_handle, advertising_set->random_address); 5824f66adbdeSMatthias Ringwald return true; 5825f66adbdeSMatthias Ringwald } 5826be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0){ 5827be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 5828174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5829be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 5830be7ef9d8SMatthias Ringwald advertising_set->advertising_handle, 583157a04237SMatthias Ringwald advertising_set->extended_params.advertising_event_properties, 583257a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_interval_min, 583357a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_interval_max, 583457a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_channel_map, 583557a04237SMatthias Ringwald advertising_set->extended_params.own_address_type, 583657a04237SMatthias Ringwald advertising_set->extended_params.peer_address_type, 583757a04237SMatthias Ringwald advertising_set->extended_params.peer_address, 583857a04237SMatthias Ringwald advertising_set->extended_params.advertising_filter_policy, 583957a04237SMatthias Ringwald advertising_set->extended_params.advertising_tx_power, 584057a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_phy, 584157a04237SMatthias Ringwald advertising_set->extended_params.secondary_advertising_max_skip, 584257a04237SMatthias Ringwald advertising_set->extended_params.secondary_advertising_phy, 584357a04237SMatthias Ringwald advertising_set->extended_params.advertising_sid, 584457a04237SMatthias Ringwald advertising_set->extended_params.scan_request_notification_enable 5845be7ef9d8SMatthias Ringwald ); 5846be7ef9d8SMatthias Ringwald return true; 5847be7ef9d8SMatthias Ringwald } 5848be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA) != 0) { 5849be7ef9d8SMatthias Ringwald uint16_t pos = advertising_set->adv_data_pos; 5850be7ef9d8SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->adv_data_len); 5851be7ef9d8SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->adv_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 5852be7ef9d8SMatthias Ringwald if ((operation & 0x02) != 0){ 5853be7ef9d8SMatthias Ringwald // last fragment or complete data 5854be7ef9d8SMatthias Ringwald operation |= 2; 5855be7ef9d8SMatthias Ringwald advertising_set->adv_data_pos = 0; 5856be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 5857be7ef9d8SMatthias Ringwald } else { 5858be7ef9d8SMatthias Ringwald advertising_set->adv_data_pos += data_to_upload; 5859be7ef9d8SMatthias Ringwald } 5860174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 58610cbe4690SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, advertising_set->advertising_handle, operation, 0x01, data_to_upload, &advertising_set->adv_data[pos]); 5862be7ef9d8SMatthias Ringwald return true; 5863be7ef9d8SMatthias Ringwald } 5864be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA) != 0) { 5865be7ef9d8SMatthias Ringwald uint16_t pos = advertising_set->scan_data_pos; 5866be7ef9d8SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->scan_data_len); 5867be7ef9d8SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->scan_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 5868be7ef9d8SMatthias Ringwald if ((operation & 0x02) != 0){ 5869be7ef9d8SMatthias Ringwald advertising_set->scan_data_pos = 0; 5870be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5871be7ef9d8SMatthias Ringwald } else { 5872be7ef9d8SMatthias Ringwald advertising_set->scan_data_pos += data_to_upload; 5873be7ef9d8SMatthias Ringwald } 5874174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5875be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, operation, 0x03, 0x01, data_to_upload, &advertising_set->scan_data[pos]); 5876be7ef9d8SMatthias Ringwald return true; 5877be7ef9d8SMatthias Ringwald } 5878c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 587953f240c0SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS) != 0){ 588053f240c0SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS; 588153f240c0SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 588253f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_parameters, 588353f240c0SMatthias Ringwald advertising_set->advertising_handle, 588453f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_interval_min, 588553f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_interval_max, 588653f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_properties); 588753f240c0SMatthias Ringwald return true; 588853f240c0SMatthias Ringwald } 588953f240c0SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA) != 0) { 58900cbe4690SMatthias Ringwald uint16_t pos = advertising_set->periodic_data_pos; 589153f240c0SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->periodic_data_len); 589253f240c0SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->periodic_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 589353f240c0SMatthias Ringwald if ((operation & 0x02) != 0){ 589453f240c0SMatthias Ringwald // last fragment or complete data 589553f240c0SMatthias Ringwald operation |= 2; 58960cbe4690SMatthias Ringwald advertising_set->periodic_data_pos = 0; 589753f240c0SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA; 589853f240c0SMatthias Ringwald } else { 58990cbe4690SMatthias Ringwald advertising_set->periodic_data_pos += data_to_upload; 590053f240c0SMatthias Ringwald } 590153f240c0SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 59020cbe4690SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_data, advertising_set->advertising_handle, operation, data_to_upload, &advertising_set->periodic_data[pos]); 590353f240c0SMatthias Ringwald return true; 590453f240c0SMatthias Ringwald } 5905c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5906be7ef9d8SMatthias Ringwald } 5907be7ef9d8SMatthias Ringwald } 5908be7ef9d8SMatthias Ringwald #endif 5909be7ef9d8SMatthias Ringwald 5910d70217a2SMatthias Ringwald #endif 59119956955bSMatthias Ringwald 5912057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5913a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 5914a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 59154bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 59164bafedd5SMatthias Ringwald // if periodic sync with advertiser list was active and is not cancelled yet, wait until next time 59174bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_state == LE_CONNECTING_CANCEL) return false; 59184bafedd5SMatthias Ringwald #endif 5919057ab60cSMatthias Ringwald #endif 5920057ab60cSMatthias Ringwald 5921a41310b7SMatthias Ringwald // LE Whitelist Management 5922a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 59239956955bSMatthias Ringwald // add/remove entries 5924665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 5925665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 5926665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 5927453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 5928453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 5929453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 5930453459ddSMatthias Ringwald return true; 5931453459ddSMatthias Ringwald } 59329956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 5933453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 5934453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 59359956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 5936f30077b7SMatthias Ringwald return true; 59379956955bSMatthias Ringwald } 5938453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 5939665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 59409956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 59419956955bSMatthias Ringwald } 59429956955bSMatthias Ringwald } 594391915b0bSMatthias Ringwald } 59449956955bSMatthias Ringwald 594521debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 594621debf25SMatthias Ringwald // LE Resolving List Management 5947ea151974SMatthias Ringwald if (resolving_list_supported) { 594821debf25SMatthias Ringwald uint16_t i; 594921debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 595021debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 595121debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 595221debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 595321debf25SMatthias Ringwald return true; 595421debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 595521debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 595621debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 595721debf25SMatthias Ringwald return true; 595821debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 595902b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 5960ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 5961ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 5962ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 5963ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 596421debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 596521debf25SMatthias Ringwald return true; 596602b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 596702b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 596802b02cffSMatthias Ringwald uint8_t offset = i >> 3; 596902b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 597002b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 597102b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 597202b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 597302b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 597402b02cffSMatthias Ringwald sm_key_t peer_irk; 597502b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 597602b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 5977f5228c62SMatthias Ringwald 5978f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 5979f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 5980f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 5981f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 5982f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 5983f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 5984f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 5985f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 5986f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 5987f5228c62SMatthias Ringwald } 5988f5228c62SMatthias Ringwald #endif 5989f5228c62SMatthias Ringwald 5990ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 5991ea151974SMatthias Ringwald peer_identity_addreses); 599202b02cffSMatthias Ringwald return true; 599302b02cffSMatthias Ringwald } 599402b02cffSMatthias Ringwald 599502b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 599602b02cffSMatthias Ringwald 599702b02cffSMatthias Ringwald /* fall through */ 599802b02cffSMatthias Ringwald 599921debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 600021debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 600121debf25SMatthias Ringwald uint8_t offset = i >> 3; 600221debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 600302b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 600402b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 600521debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 600621debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 600721debf25SMatthias Ringwald sm_key_t peer_irk; 600821debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 600921debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 6010e938c062SMatthias Ringwald if (btstack_is_null(peer_irk, 16)) continue; 601121debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 601221debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 601321debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 601421debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 601521debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 601621debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 6017ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 6018ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 601921debf25SMatthias Ringwald return true; 602021debf25SMatthias Ringwald } 602102b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 602221debf25SMatthias Ringwald break; 602302b02cffSMatthias Ringwald 602421debf25SMatthias Ringwald default: 602521debf25SMatthias Ringwald break; 602621debf25SMatthias Ringwald } 6027ea151974SMatthias Ringwald } 602821debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 602921debf25SMatthias Ringwald #endif 6030a41310b7SMatthias Ringwald 6031f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6032f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6033f40c73b4SMatthias Ringwald // LE Whitelist Management 6034f40c73b4SMatthias Ringwald if (periodic_list_modification_pending){ 6035f40c73b4SMatthias Ringwald // add/remove entries 6036f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 6037f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 6038f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 6039f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER){ 6040f40c73b4SMatthias Ringwald entry->state &= ~LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 6041f40c73b4SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_periodic_advertiser_list, entry->address_type, entry->address); 6042f40c73b4SMatthias Ringwald return true; 6043f40c73b4SMatthias Ringwald } 6044f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER){ 6045f40c73b4SMatthias Ringwald entry->state &= ~LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 6046f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER; 60479e88556bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_periodic_advertiser_list, entry->address_type, entry->address, entry->sid); 6048f40c73b4SMatthias Ringwald return true; 6049f40c73b4SMatthias Ringwald } 6050f40c73b4SMatthias Ringwald if ((entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER) == 0){ 6051f40c73b4SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t *) entry); 6052f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 6053f40c73b4SMatthias Ringwald } 6054f40c73b4SMatthias Ringwald } 6055f40c73b4SMatthias Ringwald } 6056f40c73b4SMatthias Ringwald #endif 6057f40c73b4SMatthias Ringwald #endif 6058f40c73b4SMatthias Ringwald 605973799937SMatthias Ringwald // post-pone all actions until stack is fully working 606073799937SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 606173799937SMatthias Ringwald 606273799937SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 606373799937SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 606473799937SMatthias Ringwald 6065a41310b7SMatthias Ringwald // Phase 4: restore state 606629c24bebSMatthias Ringwald 606729c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6068af64f147SMatthias Ringwald // re-start scanning 606929c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 607029c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 607163671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 607263671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 607363671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 1, 0, 0, 0); 607463671e69SMatthias Ringwald } else 607563671e69SMatthias Ringwald #endif 607663671e69SMatthias Ringwald { 607729c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 607863671e69SMatthias Ringwald } 607929c24bebSMatthias Ringwald return true; 608029c24bebSMatthias Ringwald } 608129c24bebSMatthias Ringwald #endif 608229c24bebSMatthias Ringwald 608313eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6084af64f147SMatthias Ringwald // re-start connecting 6085af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 60869956955bSMatthias Ringwald bd_addr_t null_addr; 60879956955bSMatthias Ringwald memset(null_addr, 0, 6); 60886bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 60896bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 60909956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 6091cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 6092cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 60939956955bSMatthias Ringwald 1, // use whitelist 60949956955bSMatthias Ringwald 0, // peer address type 60959956955bSMatthias Ringwald null_addr, // peer bd addr 60966bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 609773044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 609873044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 609973044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 610073044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 610173044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 610273044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 61039956955bSMatthias Ringwald ); 6104f30077b7SMatthias Ringwald return true; 61059956955bSMatthias Ringwald } 61064bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 61074bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_state == LE_CONNECTING_IDLE){ 61084bafedd5SMatthias Ringwald switch(hci_stack->le_periodic_sync_request){ 61094bafedd5SMatthias Ringwald case LE_CONNECTING_DIRECT: 61104bafedd5SMatthias Ringwald case LE_CONNECTING_WHITELIST: 61114bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_state = ((hci_stack->le_periodic_sync_options & 1) != 0) ? LE_CONNECTING_WHITELIST : LE_CONNECTING_DIRECT; 61124bafedd5SMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_create_sync, 61134bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_options, 61144bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertising_sid, 61154bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address_type, 61164bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address, 61174bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_skip, 61184bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_timeout, 61194bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_cte_type); 61204bafedd5SMatthias Ringwald return true; 61214bafedd5SMatthias Ringwald default: 61224bafedd5SMatthias Ringwald break; 61234bafedd5SMatthias Ringwald } 61244bafedd5SMatthias Ringwald } 61254bafedd5SMatthias Ringwald #endif 6126d70217a2SMatthias Ringwald #endif 6127a41310b7SMatthias Ringwald 6128a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 6129a41310b7SMatthias Ringwald // re-start advertising 6130935aa76eSMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) == 0)){ 6131a41310b7SMatthias Ringwald // check if advertisements should be enabled given 6132935aa76eSMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_ACTIVE; 6133b892db1cSMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, hci_stack->le_advertisements_own_address); 6134e464cd48SMatthias Ringwald 6135e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6136e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 6137e464cd48SMatthias Ringwald const uint8_t advertising_handles[] = { 0 }; 6138e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 6139e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 6140e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 6141e464cd48SMatthias Ringwald } else 6142e464cd48SMatthias Ringwald #endif 6143e464cd48SMatthias Ringwald { 6144a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 6145e464cd48SMatthias Ringwald } 6146a41310b7SMatthias Ringwald return true; 6147a41310b7SMatthias Ringwald } 6148be7ef9d8SMatthias Ringwald 6149be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6150be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported()) { 6151be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 6152be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 6153be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 6154be7ef9d8SMatthias Ringwald le_advertising_set_t *advertising_set = (le_advertising_set_t *) btstack_linked_list_iterator_next(&it); 6155be7ef9d8SMatthias Ringwald if (((advertising_set->state & LE_ADVERTISEMENT_STATE_ENABLED) != 0) && ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) == 0)){ 61568caa405cSMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_ACTIVE; 6157be7ef9d8SMatthias Ringwald const uint8_t advertising_handles[] = { advertising_set->advertising_handle }; 6158be7ef9d8SMatthias Ringwald const uint16_t durations[] = { advertising_set->enable_timeout }; 6159be7ef9d8SMatthias Ringwald const uint16_t max_events[] = { advertising_set->enable_max_scan_events }; 6160be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 6161be7ef9d8SMatthias Ringwald return true; 6162be7ef9d8SMatthias Ringwald } 6163c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 616453f240c0SMatthias Ringwald if (((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED) != 0) && ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) == 0)){ 616553f240c0SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 616653f240c0SMatthias Ringwald uint8_t enable = 1; 616753f240c0SMatthias Ringwald if (advertising_set->periodic_include_adi){ 616853f240c0SMatthias Ringwald enable |= 2; 616953f240c0SMatthias Ringwald } 617053f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, enable, advertising_set->advertising_handle); 617153f240c0SMatthias Ringwald return true; 617253f240c0SMatthias Ringwald } 6173c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 6174be7ef9d8SMatthias Ringwald } 6175be7ef9d8SMatthias Ringwald } 6176be7ef9d8SMatthias Ringwald #endif 6177a41310b7SMatthias Ringwald #endif 6178a41310b7SMatthias Ringwald 6179f30077b7SMatthias Ringwald return false; 61807bdc6798S[email protected] } 61817aa35f6aSMatthias Ringwald 61827aa35f6aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 61837aa35f6aSMatthias Ringwald static bool hci_run_iso_tasks(void){ 61847aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 61852a6c0f82SMatthias Ringwald 618640f86dfdSMatthias Ringwald if (hci_stack->iso_active_operation_group_id != 0xff) { 618740f86dfdSMatthias Ringwald return false; 618840f86dfdSMatthias Ringwald } 618940f86dfdSMatthias Ringwald 61902a6c0f82SMatthias Ringwald // BIG 61917aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 61927aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 61937aa35f6aSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 61947aa35f6aSMatthias Ringwald switch (big->state){ 61957aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 619640f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = big->params->big_handle; 619740f86dfdSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_BIS; 61987aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_ESTABLISHED; 61997aa35f6aSMatthias Ringwald hci_send_cmd(&hci_le_create_big, 62007aa35f6aSMatthias Ringwald big->params->big_handle, 62017aa35f6aSMatthias Ringwald big->params->advertising_handle, 62027aa35f6aSMatthias Ringwald big->params->num_bis, 62037aa35f6aSMatthias Ringwald big->params->sdu_interval_us, 62047aa35f6aSMatthias Ringwald big->params->max_sdu, 62057aa35f6aSMatthias Ringwald big->params->max_transport_latency_ms, 62067aa35f6aSMatthias Ringwald big->params->rtn, 62077aa35f6aSMatthias Ringwald big->params->phy, 62087aa35f6aSMatthias Ringwald big->params->packing, 62097aa35f6aSMatthias Ringwald big->params->framing, 62107aa35f6aSMatthias Ringwald big->params->encryption, 62117aa35f6aSMatthias Ringwald big->params->broadcast_code); 62127aa35f6aSMatthias Ringwald return true; 62137aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 62147aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH; 62157aa35f6aSMatthias Ringwald hci_send_cmd(&hci_le_setup_iso_data_path, big->bis_con_handles[big->state_vars.next_bis], 0, 0, 0, 0, 0, 0, 0, NULL); 62167aa35f6aSMatthias Ringwald return true; 62177aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED: 62187aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED; 62192a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle, big->state_vars.status); 6220c1f83111SMatthias Ringwald return true; 62217aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_TERMINATE: 62227aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 62232a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle, ERROR_CODE_SUCCESS); 62247aa35f6aSMatthias Ringwald return true; 62257aa35f6aSMatthias Ringwald default: 62267aa35f6aSMatthias Ringwald break; 62277aa35f6aSMatthias Ringwald } 62287aa35f6aSMatthias Ringwald } 62292a6c0f82SMatthias Ringwald 62302a6c0f82SMatthias Ringwald // BIG Sync 62312a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 62322a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 62332a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 62342a6c0f82SMatthias Ringwald switch (big_sync->state){ 62352a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 623640f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = big_sync->params->big_handle; 623740f86dfdSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_BIS; 62382a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_ESTABLISHED; 62392a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_create_sync, 62402a6c0f82SMatthias Ringwald big_sync->params->big_handle, 62412a6c0f82SMatthias Ringwald big_sync->params->sync_handle, 62422a6c0f82SMatthias Ringwald big_sync->params->encryption, 62432a6c0f82SMatthias Ringwald big_sync->params->broadcast_code, 62442a6c0f82SMatthias Ringwald big_sync->params->mse, 62452a6c0f82SMatthias Ringwald big_sync->params->big_sync_timeout_10ms, 62462a6c0f82SMatthias Ringwald big_sync->params->num_bis, 62472a6c0f82SMatthias Ringwald big_sync->params->bis_indices); 62482a6c0f82SMatthias Ringwald return true; 62492a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 62502a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH; 62512a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_setup_iso_data_path, big_sync->bis_con_handles[big_sync->state_vars.next_bis], 1, 0, 0, 0, 0, 0, 0, NULL); 62522a6c0f82SMatthias Ringwald return true; 62532a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED: 62542a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED; 62552a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 6256c1f83111SMatthias Ringwald return true; 62572a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_TERMINATE: 62582a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 62592a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 62602a6c0f82SMatthias Ringwald return true; 62612a6c0f82SMatthias Ringwald default: 62622a6c0f82SMatthias Ringwald break; 62632a6c0f82SMatthias Ringwald } 62642a6c0f82SMatthias Ringwald } 62652a6c0f82SMatthias Ringwald 62667aa35f6aSMatthias Ringwald return false; 62677aa35f6aSMatthias Ringwald } 62687aa35f6aSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 6269b2f949feS[email protected] #endif 62707bdc6798S[email protected] 627188a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 6272f30077b7SMatthias Ringwald btstack_linked_item_t * it; 6273a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 627405ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 627532ab9390Smatthias.ringwald 62760bf6344aS[email protected] switch(connection->state){ 62770bf6344aS[email protected] case SEND_CREATE_CONNECTION: 62784f3229d8S[email protected] switch(connection->address_type){ 627935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6280f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 62819da54300S[email protected] log_info("sending hci_create_connection"); 6282b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 62834f3229d8S[email protected] break; 628435454696SMatthias Ringwald #endif 62854f3229d8S[email protected] default: 6286a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 6287d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 62889da54300S[email protected] log_info("sending hci_le_create_connection"); 62896bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 62906bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 6291e90f848dSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6292e90f848dSMatthias Ringwald if (hci_extended_advertising_supported()) { 6293e90f848dSMatthias Ringwald uint16_t le_connection_scan_interval[1] = { hci_stack->le_connection_scan_interval }; 6294e90f848dSMatthias Ringwald uint16_t le_connection_scan_window[1] = { hci_stack->le_connection_scan_window }; 6295e90f848dSMatthias Ringwald uint16_t le_connection_interval_min[1] = { hci_stack->le_connection_interval_min }; 6296e90f848dSMatthias Ringwald uint16_t le_connection_interval_max[1] = { hci_stack->le_connection_interval_max }; 6297e90f848dSMatthias Ringwald uint16_t le_connection_latency[1] = { hci_stack->le_connection_latency }; 6298e90f848dSMatthias Ringwald uint16_t le_supervision_timeout[1] = { hci_stack->le_supervision_timeout }; 6299e90f848dSMatthias Ringwald uint16_t le_minimum_ce_length[1] = { hci_stack->le_minimum_ce_length }; 6300e90f848dSMatthias Ringwald uint16_t le_maximum_ce_length[1] = { hci_stack->le_maximum_ce_length }; 6301e90f848dSMatthias Ringwald hci_send_cmd(&hci_le_extended_create_connection, 6302e90f848dSMatthias Ringwald 0, // don't use whitelist 6303e90f848dSMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 6304e90f848dSMatthias Ringwald connection->address_type, // peer address type 6305e90f848dSMatthias Ringwald connection->address, // peer bd addr 6306e90f848dSMatthias Ringwald 1, // initiating PHY - 1M 6307e90f848dSMatthias Ringwald le_connection_scan_interval, // conn scan interval 6308e90f848dSMatthias Ringwald le_connection_scan_window, // conn scan windows 6309e90f848dSMatthias Ringwald le_connection_interval_min, // conn interval min 6310e90f848dSMatthias Ringwald le_connection_interval_max, // conn interval max 6311e90f848dSMatthias Ringwald le_connection_latency, // conn latency 6312e90f848dSMatthias Ringwald le_supervision_timeout, // conn latency 6313e90f848dSMatthias Ringwald le_minimum_ce_length, // min ce length 6314e90f848dSMatthias Ringwald le_maximum_ce_length // max ce length 6315e90f848dSMatthias Ringwald ); } 6316e90f848dSMatthias Ringwald else 6317e90f848dSMatthias Ringwald #endif 6318e90f848dSMatthias Ringwald { 63194f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 6320cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 6321cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 63224f3229d8S[email protected] 0, // don't use whitelist 63234f3229d8S[email protected] connection->address_type, // peer address type 63244f3229d8S[email protected] connection->address, // peer bd addr 63256bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 632673044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 632773044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 632873044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 632973044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 633073044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 633173044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 63324f3229d8S[email protected] ); 6333e90f848dSMatthias Ringwald } 63344f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 6335b2f949feS[email protected] #endif 6336d70217a2SMatthias Ringwald #endif 63374f3229d8S[email protected] break; 63384f3229d8S[email protected] } 6339f30077b7SMatthias Ringwald return true; 6340ad83dc6aS[email protected] 634135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 63420bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 63435cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 6344f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 634576ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 6346895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 6347c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 6348f30077b7SMatthias Ringwald return true; 6349ee025741SMatthias Ringwald } 6350ee025741SMatthias Ringwald break; 635135454696SMatthias Ringwald #endif 63520bf6344aS[email protected] case SEND_DISCONNECT: 63530bf6344aS[email protected] connection->state = SENT_DISCONNECT; 63546ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 6355f30077b7SMatthias Ringwald return true; 63560bf6344aS[email protected] 63570bf6344aS[email protected] default: 63580bf6344aS[email protected] break; 6359c7e0c5f6Smatthias.ringwald } 6360c7e0c5f6Smatthias.ringwald 6361cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 6362cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 6363cabf004eSMatthias Ringwald 636494418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 636594418890SMatthias Ringwald 6366ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 636750f49832SMatthias Ringwald if (((connection->authentication_flags & AUTH_FLAG_HANDLE_LINK_KEY_REQUEST) != 0)){ 6368608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 63698daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 637030e72d78SMatthias Ringwald 6371e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 6372e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 637330e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 6374e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 637532ab9390Smatthias.ringwald } else { 637632ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 637732ab9390Smatthias.ringwald } 6378f30077b7SMatthias Ringwald return true; 637932ab9390Smatthias.ringwald } 63801d6b20aeS[email protected] 63818daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_DENY_PIN_CODE_REQUEST){ 63829da54300S[email protected] log_info("denying to pin request"); 63838daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_DENY_PIN_CODE_REQUEST); 638434d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 6385f30077b7SMatthias Ringwald return true; 63864c57c146S[email protected] } 63874c57c146S[email protected] 6388c950c316SMatthias Ringwald // security assessment requires remote features 63892dd8985bSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST) != 0){ 6390c950c316SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 6391c950c316SMatthias Ringwald hci_ssp_assess_security_on_io_cap_request(connection); 6392c950c316SMatthias 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 6393c950c316SMatthias Ringwald } 6394c950c316SMatthias Ringwald 63958daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY){ 63968daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 6397a8d20135SMatthias Ringwald // set authentication requirements: 6398a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 6399532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 6400a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 64019faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 64029faad3abS[email protected] authreq |= 1; 6403106d6d11S[email protected] } 6404532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 64058daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 6406532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 6407532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 6408532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 6409532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 6410532454f9SMatthias Ringwald bonding = false; 6411532454f9SMatthias Ringwald } 6412532454f9SMatthias Ringwald } 6413532454f9SMatthias Ringwald if (bonding){ 6414a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 6415a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 6416532454f9SMatthias Ringwald } else { 6417a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 6418a8d20135SMatthias Ringwald } 6419532454f9SMatthias Ringwald } 64201849becdSMatthias Ringwald uint8_t have_oob_data = 0; 64211849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 64221849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 64231849becdSMatthias Ringwald have_oob_data |= 1; 64241849becdSMatthias Ringwald } 64251849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 64261849becdSMatthias Ringwald have_oob_data |= 2; 64271849becdSMatthias Ringwald } 64281849becdSMatthias Ringwald #endif 64291849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 643011b03efaSMatthias Ringwald return true; 6431f8fb5f6eS[email protected] } 643211b03efaSMatthias Ringwald 64338daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 64348daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 643511b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 6436f30077b7SMatthias Ringwald return true; 643732ab9390Smatthias.ringwald } 643832ab9390Smatthias.ringwald 64391849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 64407ca4a7edSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY){ 64417ca4a7edSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 64421849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 64431849becdSMatthias Ringwald const uint8_t * r_192 = zero; 64441849becdSMatthias Ringwald const uint8_t * c_192 = zero; 64451849becdSMatthias Ringwald const uint8_t * r_256 = zero; 64461849becdSMatthias Ringwald const uint8_t * c_256 = zero; 64471849becdSMatthias Ringwald // verify P-256 OOB 64481e83575aSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && hci_command_supported(SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)) { 64491849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 64501849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 64511849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 64521849becdSMatthias Ringwald } 64531849becdSMatthias Ringwald } 64541849becdSMatthias Ringwald // verify P-192 OOB 64551849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 64561849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 64571849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 64581849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 64591849becdSMatthias Ringwald } 64601849becdSMatthias Ringwald } 64617ca4a7edSMatthias Ringwald 64627ca4a7edSMatthias Ringwald // assess security 64637ca4a7edSMatthias Ringwald bool need_level_4 = hci_stack->gap_secure_connections_only_mode || (connection->requested_security_level == LEVEL_4); 64647ca4a7edSMatthias Ringwald bool can_reach_level_4 = hci_remote_sc_enabled(connection) && (c_256 != NULL); 64657ca4a7edSMatthias Ringwald if (need_level_4 && !can_reach_level_4){ 64667ca4a7edSMatthias Ringwald log_info("Level 4 required, but not possible -> abort"); 64677ca4a7edSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_INSUFFICIENT_SECURITY); 64687ca4a7edSMatthias Ringwald // send oob negative reply 64697ca4a7edSMatthias Ringwald c_256 = NULL; 64707ca4a7edSMatthias Ringwald c_192 = NULL; 64717ca4a7edSMatthias Ringwald } 64727ca4a7edSMatthias Ringwald 64731849becdSMatthias Ringwald // Reply 64741849becdSMatthias Ringwald if (c_256 != zero) { 64751849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 64761849becdSMatthias Ringwald } else if (c_192 != zero){ 64771849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 64781849becdSMatthias Ringwald } else { 64791ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = connection->con_handle; 64801849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 64811849becdSMatthias Ringwald } 64821849becdSMatthias Ringwald return true; 64831849becdSMatthias Ringwald } 64841849becdSMatthias Ringwald #endif 64851849becdSMatthias Ringwald 64868daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_REPLY){ 64878daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 648834d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 6489f30077b7SMatthias Ringwald return true; 6490dbe1a790S[email protected] } 6491dbe1a790S[email protected] 6492367aedc0SMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY){ 6493367aedc0SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 6494367aedc0SMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, &connection->address); 6495367aedc0SMatthias Ringwald return true; 6496367aedc0SMatthias Ringwald } 6497367aedc0SMatthias Ringwald 64988daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_PASSKEY_REPLY){ 64998daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 650034d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 6501f30077b7SMatthias Ringwald return true; 6502dbe1a790S[email protected] } 6503afd4e962S[email protected] 6504ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 6505ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 65061bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 65076ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 65086ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 6509f30077b7SMatthias Ringwald return true; 6510ad83dc6aS[email protected] } 651176f27cffSMatthias Ringwald 65122a75353aSMatthias Ringwald if ((connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 651334d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 6514abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 651534d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 6516f30077b7SMatthias Ringwald return true; 6517afd4e962S[email protected] } 651876f27cffSMatthias Ringwald 6519dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 6520dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 6521dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 6522f30077b7SMatthias Ringwald return true; 6523dce78009S[email protected] } 6524447016ffSMatthias Ringwald 6525573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 6526573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 6527573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 6528f30077b7SMatthias Ringwald return true; 6529573897a0SMatthias Ringwald } 6530447016ffSMatthias Ringwald 6531447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 6532447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 6533447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 6534447016ffSMatthias Ringwald return true; 6535447016ffSMatthias Ringwald } 6536447016ffSMatthias Ringwald 6537447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 6538447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 6539447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 6540447016ffSMatthias Ringwald return true; 6541447016ffSMatthias Ringwald } 6542447016ffSMatthias Ringwald 6543447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 6544447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 6545447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 6546447016ffSMatthias Ringwald return true; 6547447016ffSMatthias Ringwald } 654876f27cffSMatthias Ringwald #endif 654976f27cffSMatthias Ringwald 655076f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 655176f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 65521714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 65531714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 65541714cbbdSMatthias Ringwald #endif 65556ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 65566ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 65576ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 6558f30077b7SMatthias Ringwald return true; 655976f27cffSMatthias Ringwald } 65606ef3696aSMatthias Ringwald } 6561da886c03S[email protected] 65626cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 6563f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 6564f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 6565f8ee3071SMatthias Ringwald case 0: 6566f8ee3071SMatthias Ringwald break; 6567f8ee3071SMatthias Ringwald case 0xffff: 6568f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 6569f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 6570f30077b7SMatthias Ringwald return true; 6571f8ee3071SMatthias Ringwald default: 6572f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 6573f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 6574f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 6575f30077b7SMatthias Ringwald return true; 6576f8ee3071SMatthias Ringwald } 657788a03c8dSMatthias Ringwald 6578140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 6579140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 6580140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 6581140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 6582140c0557SMatthias Ringwald return true; 6583140c0557SMatthias Ringwald } 6584140c0557SMatthias Ringwald 6585965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 6586278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 6587965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 6588278ff8a9SMatthias 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); 6589278ff8a9SMatthias Ringwald return true; 6590278ff8a9SMatthias Ringwald } 6591278ff8a9SMatthias Ringwald 659288a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 659388a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 659488a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 659588a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 659688a03c8dSMatthias Ringwald return true; 659788a03c8dSMatthias Ringwald } 659822df2fe2SMatthias Ringwald #endif 65999afaa4d4SMatthias Ringwald 66009afaa4d4SMatthias Ringwald if (connection->gap_connection_tasks != 0){ 660122df2fe2SMatthias Ringwald #ifdef ENABLE_CLASSIC 66029afaa4d4SMatthias Ringwald if ((connection->gap_connection_tasks & GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT) != 0){ 66039afaa4d4SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT; 66049afaa4d4SMatthias Ringwald hci_send_cmd(&hci_write_automatic_flush_timeout, connection->con_handle, hci_stack->automatic_flush_timeout); 66059afaa4d4SMatthias Ringwald return true; 66069afaa4d4SMatthias Ringwald } 660722df2fe2SMatthias Ringwald if (connection->gap_connection_tasks & GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT){ 660822df2fe2SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT; 660922df2fe2SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 661022df2fe2SMatthias Ringwald return true; 66119afaa4d4SMatthias Ringwald } 66126cdc2862SMatthias Ringwald #endif 661322df2fe2SMatthias Ringwald if (connection->gap_connection_tasks & GAP_CONNECTION_TASK_READ_RSSI){ 661422df2fe2SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_READ_RSSI; 661522df2fe2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 661622df2fe2SMatthias Ringwald return true; 661722df2fe2SMatthias Ringwald } 661822df2fe2SMatthias Ringwald } 6619f8ee3071SMatthias Ringwald 6620a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 662173cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 662273cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 662373cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 6624da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 662573cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 6626c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 6627c37a3166S[email protected] 0x0000, 0xffff); 6628f30077b7SMatthias Ringwald return true; 662973cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 663073cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 663173cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 663273cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 663373cd8a2aSMatthias Ringwald 0x0000, 0xffff); 6634f30077b7SMatthias Ringwald return true; 663573cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 663673cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 66371e0338ebSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, connection->con_handle, 66381e0338ebSMatthias Ringwald ERROR_CODE_UNACCEPTABLE_CONNECTION_PARAMETERS); 6639f30077b7SMatthias Ringwald return true; 664073cd8a2aSMatthias Ringwald default: 664173cd8a2aSMatthias Ringwald break; 6642c37a3166S[email protected] } 66434ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 6644b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 6645b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 6646b90f6e0aSMatthias 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); 6647f30077b7SMatthias Ringwald return true; 6648b90f6e0aSMatthias Ringwald } 6649c37a3166S[email protected] #endif 6650dbe1a790S[email protected] } 6651f30077b7SMatthias Ringwald return false; 6652f30077b7SMatthias Ringwald } 6653c7e0c5f6Smatthias.ringwald 6654f30077b7SMatthias Ringwald static void hci_run(void){ 6655f30077b7SMatthias Ringwald 6656df9e2780SMatthias Ringwald // stack state sub statemachines 6657df9e2780SMatthias Ringwald switch (hci_stack->state) { 6658df9e2780SMatthias Ringwald case HCI_STATE_INITIALIZING: 6659df9e2780SMatthias Ringwald hci_initializing_run(); 6660d0dea5fbSMatthias Ringwald break; 6661df9e2780SMatthias Ringwald case HCI_STATE_HALTING: 6662df9e2780SMatthias Ringwald hci_halting_run(); 6663d0dea5fbSMatthias Ringwald break; 6664df9e2780SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 6665df9e2780SMatthias Ringwald hci_falling_asleep_run(); 6666d0dea5fbSMatthias Ringwald break; 6667df9e2780SMatthias Ringwald default: 6668df9e2780SMatthias Ringwald break; 6669df9e2780SMatthias Ringwald } 6670df9e2780SMatthias Ringwald 6671d0dea5fbSMatthias Ringwald // allow to run after initialization to working transition 6672d0dea5fbSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING){ 6673d0dea5fbSMatthias Ringwald return; 6674d0dea5fbSMatthias Ringwald } 6675d0dea5fbSMatthias Ringwald 6676f30077b7SMatthias Ringwald bool done; 6677f30077b7SMatthias Ringwald 6678f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 6679f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 6680f30077b7SMatthias Ringwald if (done) return; 6681f30077b7SMatthias Ringwald 668268ab6207SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 668368ab6207SMatthias Ringwald done = hci_run_iso_fragments(); 668468ab6207SMatthias Ringwald if (done) return; 668568ab6207SMatthias Ringwald #endif 668668ab6207SMatthias Ringwald 6687f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 6688f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 6689f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 6690f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 6691f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 6692f30077b7SMatthias Ringwald return; 6693f30077b7SMatthias Ringwald } 6694f30077b7SMatthias Ringwald #endif 6695f30077b7SMatthias Ringwald 6696f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 6697f30077b7SMatthias Ringwald 6698f30077b7SMatthias Ringwald // global/non-connection oriented commands 6699f30077b7SMatthias Ringwald 6700f30077b7SMatthias Ringwald 6701f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 6702f30077b7SMatthias Ringwald // general gap classic 6703f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 6704f30077b7SMatthias Ringwald if (done) return; 6705f30077b7SMatthias Ringwald #endif 6706f30077b7SMatthias Ringwald 6707f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 6708f30077b7SMatthias Ringwald // general gap le 6709f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 6710f30077b7SMatthias Ringwald if (done) return; 67117aa35f6aSMatthias Ringwald 67127aa35f6aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 67137aa35f6aSMatthias Ringwald // ISO related tasks, e.g. BIG create/terminate/sync 67147aa35f6aSMatthias Ringwald done = hci_run_iso_tasks(); 67157aa35f6aSMatthias Ringwald if (done) return; 67167aa35f6aSMatthias Ringwald #endif 6717f30077b7SMatthias Ringwald #endif 6718f30077b7SMatthias Ringwald 6719f30077b7SMatthias Ringwald // send pending HCI commands 6720df9e2780SMatthias Ringwald hci_run_general_pending_commands(); 67213429f56bSmatthias.ringwald } 672216833f0aSmatthias.ringwald 67233e2050f7SMatthias Ringwald uint8_t hci_send_cmd_packet(uint8_t *packet, int size){ 672435454696SMatthias Ringwald // house-keeping 672535454696SMatthias Ringwald 672635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6727c8e4258aSmatthias.ringwald bd_addr_t addr; 6728c8e4258aSmatthias.ringwald hci_connection_t * conn; 6729c123d999SMatthias Ringwald #endif 6730c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 67316f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 67329cbd2215SMatthias Ringwald #endif 67334ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 67344ae8623eSMatthias Ringwald uint8_t i; 67354ae8623eSMatthias Ringwald uint8_t num_cis; 67364ae8623eSMatthias Ringwald hci_con_handle_t cis_handle; 67374ae8623eSMatthias Ringwald uint8_t status; 67384ae8623eSMatthias Ringwald #endif 6739c8e4258aSmatthias.ringwald 67406f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 67416f35bb46SMatthias Ringwald switch (opcode) { 67429cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 67439cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 67449cbd2215SMatthias Ringwald break; 67459cbd2215SMatthias Ringwald 67469cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 67476f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 6748724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 67499da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 6750c8e4258aSmatthias.ringwald 675172cf8859SMatthias Ringwald // CVE-2020-26555: reject outgoing connection to device with same BD ADDR 675272cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0) { 675379e0fa07SMatthias Ringwald hci_emit_connection_complete(addr, 0, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR); 67543e2050f7SMatthias Ringwald return ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 675572cf8859SMatthias Ringwald } 675672cf8859SMatthias Ringwald 6757f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 6758ad83dc6aS[email protected] if (!conn) { 6759f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 676017f1ba2aSmatthias.ringwald if (!conn) { 676117f1ba2aSmatthias.ringwald // notify client that alloc failed 67622deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 67633e2050f7SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller 676417f1ba2aSmatthias.ringwald } 6765ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 6766f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 6767ad83dc6aS[email protected] } 6768aee5d5c1SMatthias Ringwald 6769ad83dc6aS[email protected] log_info("conn state %u", conn->state); 67703e2050f7SMatthias Ringwald // TODO: L2CAP should not send create connection command, instead a (new) gap function should be used 6771ad83dc6aS[email protected] switch (conn->state) { 6772ad83dc6aS[email protected] // if connection active exists 6773ad83dc6aS[email protected] case OPEN: 6774f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 677572cf8859SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, ERROR_CODE_SUCCESS); 67763e2050f7SMatthias Ringwald // packet not sent to controller 67773e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 6778672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 6779672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 6780672e36abSMatthias Ringwald break; 6781ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 67826c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 67836c0d5426SMatthias Ringwald if (hci_classic_operation_active()){ 67846c0d5426SMatthias Ringwald return ERROR_CODE_SUCCESS; 67856c0d5426SMatthias Ringwald } 67866c0d5426SMatthias Ringwald #endif 6787532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 6788532f91a5SMatthias Ringwald break; 6789ad83dc6aS[email protected] default: 6790ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 67913e2050f7SMatthias Ringwald // packet not sent to controller 67923e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 6793ad83dc6aS[email protected] } 6794c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 6795229331c6SMatthias Ringwald 6796229331c6SMatthias Ringwald // track outgoing connection 6797f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 67986535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 67996f35bb46SMatthias Ringwald break; 6800ee752bb8SMatthias Ringwald 68015b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 68026f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 6803ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 6804ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 6805ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 68066f35bb46SMatthias Ringwald break; 68076f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 6808c91e6e4fSMatthias Ringwald // accept_synchronous_connection? Voice setting at offset 18 6809ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 6810ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 68111f34df2cSMatthias Ringwald // track outgoing connection 68121f34df2cSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_SCO; 68131f34df2cSMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->outgoing_addr); 68146f35bb46SMatthias Ringwald break; 6815ee752bb8SMatthias Ringwald #endif 681635454696SMatthias Ringwald #endif 68174b3e1e19SMatthias Ringwald 6818a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 6819d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 68206f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 6821b04dfa37SMatthias Ringwald // white list used? 68226f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 6823b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 6824b04dfa37SMatthias Ringwald case 0: 6825b04dfa37SMatthias Ringwald // whitelist not used 6826b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 6827b04dfa37SMatthias Ringwald break; 6828b04dfa37SMatthias Ringwald case 1: 6829b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 6830b04dfa37SMatthias Ringwald break; 6831b04dfa37SMatthias Ringwald default: 6832b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 6833b04dfa37SMatthias Ringwald break; 6834b04dfa37SMatthias Ringwald } 6835c163146eSMatthias Ringwald // track outgoing connection 683605002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 6837c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 68386f35bb46SMatthias Ringwald break; 68396f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 68406ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 68416f35bb46SMatthias Ringwald break; 68429cbd2215SMatthias Ringwald #endif 68434ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 68444ae8623eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 68454ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 68464ae8623eSMatthias Ringwald status = ERROR_CODE_SUCCESS; 68474ae8623eSMatthias Ringwald num_cis = packet[3]; 68484ae8623eSMatthias Ringwald // setup hci_iso_streams 68494ae8623eSMatthias Ringwald for (i=0;i<num_cis;i++){ 68504ae8623eSMatthias Ringwald cis_handle = (hci_con_handle_t) little_endian_read_16(packet, 4 + (4 * i)); 68518d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, cis_handle, 0xff); 68524ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 68534ae8623eSMatthias Ringwald break; 68544ae8623eSMatthias Ringwald } 68554ae8623eSMatthias Ringwald } 68564ae8623eSMatthias Ringwald // free structs on error 68574ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 6858a90016deSMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 68594ae8623eSMatthias Ringwald return status; 68604ae8623eSMatthias Ringwald } 68614ae8623eSMatthias Ringwald break; 68624ae8623eSMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 68634ae8623eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 68644ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 68654ae8623eSMatthias Ringwald cis_handle = (hci_con_handle_t) little_endian_read_16(packet, 3); 68668d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, cis_handle, 0xff); 68674ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 68684ae8623eSMatthias Ringwald return status; 68694ae8623eSMatthias Ringwald } 68704ae8623eSMatthias Ringwald break; 68714ae8623eSMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 68724ae8623eSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 68734ae8623eSMatthias Ringwald #endif /* ENABLE_BLE */ 68746f35bb46SMatthias Ringwald default: 68756f35bb46SMatthias Ringwald break; 6876b04dfa37SMatthias Ringwald } 687769a97523S[email protected] 68783a9fb326S[email protected] hci_stack->num_cmd_packets--; 68795bb5bc3eS[email protected] 68805bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 68813e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 68823e2050f7SMatthias Ringwald if (err != 0){ 68833e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 68843e2050f7SMatthias Ringwald } 68853e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 688631452debSmatthias.ringwald } 68878adf0ddaSmatthias.ringwald 68882bd8b7e7S[email protected] // disconnect because of security block 68892bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 68902bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 68912bd8b7e7S[email protected] if (!connection) return; 68922bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 68932bd8b7e7S[email protected] } 68942bd8b7e7S[email protected] 68952bd8b7e7S[email protected] 6896dbe1a790S[email protected] // Configure Secure Simple Pairing 6897dbe1a790S[email protected] 689835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 689935454696SMatthias Ringwald 6900dbe1a790S[email protected] // enable will enable SSP during init 690115a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 69023a9fb326S[email protected] hci_stack->ssp_enable = enable; 6903dbe1a790S[email protected] } 6904dbe1a790S[email protected] 690595d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 690615a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 69072bd8b7e7S[email protected] } 69082bd8b7e7S[email protected] 6909dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 691015a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 69113a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 6912dbe1a790S[email protected] } 691315a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 69143a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 6915dbe1a790S[email protected] } 6916dbe1a790S[email protected] 6917dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 691815a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 69193a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 6920dbe1a790S[email protected] } 69215d23aae8SMatthias Ringwald 69225d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 69235d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 69245d23aae8SMatthias Ringwald } 692522d445d8SMatthias Ringwald bool gap_secure_connections_active(void){ 6926eaf85bc8SMatthias Ringwald return hci_stack->secure_connections_active; 692722d445d8SMatthias Ringwald } 69285d23aae8SMatthias Ringwald 692935454696SMatthias Ringwald #endif 6930dbe1a790S[email protected] 693194be1a66SMatthias Ringwald // va_list part of hci_send_cmd 69323e2050f7SMatthias Ringwald uint8_t hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 6933d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 69349d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 69353e2050f7SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 69369d14b626S[email protected] } 69379d14b626S[email protected] 69385127cc62S[email protected] // for HCI INITIALIZATION 69399da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 69405127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 69415127cc62S[email protected] 69429d14b626S[email protected] hci_reserve_packet_buffer(); 69439d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 694494be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 69453e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_packet(packet, size); 6946bfea0222SMatthias Ringwald 6947593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 69483e2050f7SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) || hci_transport_synchronous()){ 6949e2d22487SMatthias Ringwald hci_release_packet_buffer(); 6950068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 6951bfea0222SMatthias Ringwald } 6952bfea0222SMatthias Ringwald 69533e2050f7SMatthias Ringwald return status; 695494be1a66SMatthias Ringwald } 69559d14b626S[email protected] 695694be1a66SMatthias Ringwald /** 695794be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 695894be1a66SMatthias Ringwald */ 69593e2050f7SMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t * cmd, ...){ 69601cd208adSmatthias.ringwald va_list argptr; 69611cd208adSmatthias.ringwald va_start(argptr, cmd); 69623e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_va_arg(cmd, argptr); 69631cd208adSmatthias.ringwald va_end(argptr); 69643e2050f7SMatthias Ringwald return status; 696593b8dc03Smatthias.ringwald } 6966c8e4258aSmatthias.ringwald 6967ee091cf1Smatthias.ringwald // Create various non-HCI events. 6968ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 6969ee091cf1Smatthias.ringwald 6970d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 6971fb37a842SMatthias Ringwald // dump packet 6972d6b06661SMatthias Ringwald if (dump) { 697392da54c4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 1, event, size); 6974d6b06661SMatthias Ringwald } 69751ef6bb52SMatthias Ringwald 6976fb37a842SMatthias Ringwald // dispatch to all event handlers 69771ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 69781ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 69791ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 69801ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 6981d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 69821ef6bb52SMatthias Ringwald } 6983d6b06661SMatthias Ringwald } 6984d6b06661SMatthias Ringwald 6985d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 6986fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 69873d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 6988d6b06661SMatthias Ringwald } 6989d6b06661SMatthias Ringwald 699035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6991701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 69923bc639ceSMatthias Ringwald // notify SCO sender if waiting 6993701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 6994701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 69953bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 6996701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 6997701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 69983d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 69993bc639ceSMatthias Ringwald } 70003bc639ceSMatthias Ringwald } 70011cfb383eSMatthias Ringwald 70021cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 70039784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 7004ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 70051cfb383eSMatthias Ringwald 70061cfb383eSMatthias Ringwald uint8_t * eir_data; 70071cfb383eSMatthias Ringwald ad_context_t context; 70081cfb383eSMatthias Ringwald const uint8_t * name; 70091cfb383eSMatthias Ringwald uint8_t name_len; 70101cfb383eSMatthias Ringwald 70119784dac1SMatthias Ringwald if (size < 3) return; 70129784dac1SMatthias Ringwald 70131cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 7014a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 70151cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 70161cfb383eSMatthias Ringwald 70179784dac1SMatthias Ringwald switch (event_type){ 70189784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 70199784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 70209784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 70219784dac1SMatthias Ringwald break; 70229784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 70239784dac1SMatthias Ringwald if (size != 257) return; 70249784dac1SMatthias Ringwald if (num_responses != 1) return; 70259784dac1SMatthias Ringwald break; 70269784dac1SMatthias Ringwald default: 70279784dac1SMatthias Ringwald return; 70289784dac1SMatthias Ringwald } 70299784dac1SMatthias Ringwald 70301cfb383eSMatthias Ringwald // event[1] is set at the end 70311cfb383eSMatthias Ringwald int i; 70321cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 70331cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 70341cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 7035ac973fd6SMatthias Ringwald uint8_t event_size = 27; // if name is not set by EIR 70361cfb383eSMatthias Ringwald 70376535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 70380e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 70396535961aSMatthias Ringwald (void)memcpy(&event[9], 70406535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 70416535961aSMatthias Ringwald 3); // class of device 70426535961aSMatthias Ringwald (void)memcpy(&event[12], 70436535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 70446535961aSMatthias Ringwald 2); // clock offset 70451cfb383eSMatthias Ringwald 70461cfb383eSMatthias Ringwald switch (event_type){ 70471cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 70481cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 70491cfb383eSMatthias Ringwald break; 70501cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 70511cfb383eSMatthias Ringwald event[14] = 1; 7052a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 70531cfb383eSMatthias Ringwald // 16,17 = 0, size 18 70541cfb383eSMatthias Ringwald break; 70551cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 70561cfb383eSMatthias Ringwald event[14] = 1; 7057a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 705879186815SMatthias Ringwald // EIR packets only contain a single inquiry response 70591cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 70601cfb383eSMatthias Ringwald name = NULL; 7061a8c4e5adSMatthias Ringwald // Iterate over EIR data 7062a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 70631cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 70641cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 70651cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 7066ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 70671cfb383eSMatthias Ringwald switch (data_type){ 70681cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 70691cfb383eSMatthias Ringwald if (name) continue; 7070cf373d3aSMatthias Ringwald /* fall through */ 70711cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 70721cfb383eSMatthias Ringwald name = data; 70731cfb383eSMatthias Ringwald name_len = data_size; 70741cfb383eSMatthias Ringwald break; 7075ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 7076ac9136ccSMatthias Ringwald if (data_size != 8) break; 7077ac9136ccSMatthias Ringwald event[16] = 1; 70783c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 7079ac9136ccSMatthias Ringwald break; 70801cfb383eSMatthias Ringwald default: 70811cfb383eSMatthias Ringwald break; 70821cfb383eSMatthias Ringwald } 70831cfb383eSMatthias Ringwald } 70841cfb383eSMatthias Ringwald if (name){ 7085ac9136ccSMatthias Ringwald event[25] = 1; 70861cfb383eSMatthias Ringwald // truncate name if needed 70871cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 7088ac9136ccSMatthias Ringwald event[26] = len; 7089ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 70901cfb383eSMatthias Ringwald event_size += len; 70911cfb383eSMatthias Ringwald } 70921cfb383eSMatthias Ringwald break; 70937bbeb3adSMilanka Ringwald default: 70947bbeb3adSMilanka Ringwald return; 70951cfb383eSMatthias Ringwald } 70961cfb383eSMatthias Ringwald event[1] = event_size - 2; 70971cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 70981cfb383eSMatthias Ringwald } 70991cfb383eSMatthias Ringwald } 710035454696SMatthias Ringwald #endif 71013bc639ceSMatthias Ringwald 710271de195eSMatthias Ringwald void hci_emit_state(void){ 71033a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 7104425d1371Smatthias.ringwald uint8_t event[3]; 710580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 71064ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 71073a9fb326S[email protected] event[2] = hci_stack->state; 7108d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7109c8e4258aSmatthias.ringwald } 7110c8e4258aSmatthias.ringwald 711135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 71122deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 7113425d1371Smatthias.ringwald uint8_t event[13]; 7114c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 7115425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 711617f1ba2aSmatthias.ringwald event[2] = status; 71172deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 71182deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 7119c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 7120c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 7121d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7122c8e4258aSmatthias.ringwald } 712352db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 712452db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 712552db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 712652db98b2SMatthias Ringwald uint8_t event[4]; 712752db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 712852db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 712952db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 713052db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 713152db98b2SMatthias Ringwald } 713235454696SMatthias Ringwald #endif 7133c8e4258aSmatthias.ringwald 713435454696SMatthias Ringwald #ifdef ENABLE_BLE 7135d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7136667ba9d1SMatthias 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){ 71374f3229d8S[email protected] uint8_t event[21]; 71384f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 71394ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 71404f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 71414f3229d8S[email protected] event[3] = status; 7142fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 71434f3229d8S[email protected] event[6] = 0; // TODO: role 71446e2e9a6bS[email protected] event[7] = address_type; 7145724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 7146f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 7147f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 7148f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 71494f3229d8S[email protected] event[20] = 0; // master clock accuracy 7150d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 71514f3229d8S[email protected] } 715235454696SMatthias Ringwald #endif 7153d70217a2SMatthias Ringwald #endif 71544f3229d8S[email protected] 7155fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 7156fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 7157fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 7158fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 7159fd43c0e0SMatthias Ringwald } 7160fd43c0e0SMatthias Ringwald 7161fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 7162425d1371Smatthias.ringwald uint8_t event[6]; 71633c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 71644ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 71653c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 7166fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 71673c4d4b90Smatthias.ringwald event[5] = reason; 7168d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 71693c4d4b90Smatthias.ringwald } 71703c4d4b90Smatthias.ringwald 7171b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 7172e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 7173425d1371Smatthias.ringwald uint8_t event[3]; 717480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 71754ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 717643bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 7177d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 717843bfb1bdSmatthias.ringwald } 7179038bc64cSmatthias.ringwald 7180b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 7181e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 7182425d1371Smatthias.ringwald uint8_t event[2]; 718380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 71844ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 7185d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7186038bc64cSmatthias.ringwald } 71871b0e3922Smatthias.ringwald 718835454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 718935454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 719035454696SMatthias Ringwald uint8_t event[9]; 719135454696SMatthias Ringwald int pos = 0; 719235454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 71934ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 719435454696SMatthias Ringwald event[pos++] = status; 719535454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 7196d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7197381fbed8Smatthias.ringwald } 7198458bf4e8S[email protected] 719935454696SMatthias Ringwald 720035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 720135454696SMatthias Ringwald 7202b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 7203df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 7204a00031e2S[email protected] uint8_t event[5]; 7205e00caf9cS[email protected] int pos = 0; 72065611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 7207e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 7208f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 7209e00caf9cS[email protected] pos += 2; 7210e00caf9cS[email protected] event[pos++] = level; 7211d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7212e00caf9cS[email protected] } 7213e00caf9cS[email protected] 721435454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 721535454696SMatthias Ringwald if (!connection) return LEVEL_0; 72168daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 7217fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 72188daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 7219170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 72204051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 7221170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 72220e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 7223170fafaeSMatthias Ringwald security_level = LEVEL_3; 7224170fafaeSMatthias Ringwald } 7225170fafaeSMatthias Ringwald return security_level; 722635454696SMatthias Ringwald } 722735454696SMatthias Ringwald 722871fd255dSMatthias Ringwald static void hci_emit_scan_mode_changed(uint8_t discoverable, uint8_t connectable){ 722971fd255dSMatthias Ringwald uint8_t event[4]; 723071fd255dSMatthias Ringwald event[0] = BTSTACK_EVENT_SCAN_MODE_CHANGED; 723135454696SMatthias Ringwald event[1] = sizeof(event) - 2; 723271fd255dSMatthias Ringwald event[2] = discoverable; 723371fd255dSMatthias Ringwald event[3] = connectable; 7234d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7235ad83dc6aS[email protected] } 7236ad83dc6aS[email protected] 723798a2fd1cSMatthias Ringwald // query if remote side supports eSCO 723820dcdd22SMatthias Ringwald bool hci_remote_esco_supported(hci_con_handle_t con_handle){ 723998a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 724020dcdd22SMatthias Ringwald if (!connection) return false; 724176ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 724298a2fd1cSMatthias Ringwald } 724398a2fd1cSMatthias Ringwald 724467aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 724567aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 724667aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 724767aae551SMatthias Ringwald } 724867aae551SMatthias Ringwald 72492bd8b7e7S[email protected] // query if remote side supports SSP 725020dcdd22SMatthias Ringwald bool hci_remote_ssp_supported(hci_con_handle_t con_handle){ 72512bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 725220dcdd22SMatthias Ringwald if (!connection) return false; 725367aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 72542bd8b7e7S[email protected] } 72552bd8b7e7S[email protected] 725620dcdd22SMatthias Ringwald bool gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 7257df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 7258df3354fcS[email protected] } 7259df3354fcS[email protected] 72606ffc78ceSMatthias Ringwald /** 72616ffc78ceSMatthias Ringwald * Check if remote supported features query has completed 72626ffc78ceSMatthias Ringwald */ 72636ffc78ceSMatthias Ringwald bool hci_remote_features_available(hci_con_handle_t handle){ 72646ffc78ceSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 72656ffc78ceSMatthias Ringwald if (!connection) return false; 72666ffc78ceSMatthias Ringwald return (connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0; 72676ffc78ceSMatthias Ringwald } 72686ffc78ceSMatthias Ringwald 7269d6596031SMatthias Ringwald /** 7270d6596031SMatthias Ringwald * Trigger remote supported features query 7271d6596031SMatthias Ringwald */ 7272c9742cd1SMatthias Ringwald 7273c9742cd1SMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection){ 7274c9742cd1SMatthias Ringwald if ((connection->bonding_flags & (BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_RECEIVED_REMOTE_FEATURES)) == 0){ 7275c9742cd1SMatthias Ringwald connection->bonding_flags |= BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 7276c9742cd1SMatthias Ringwald } 7277c9742cd1SMatthias Ringwald } 7278c9742cd1SMatthias Ringwald 7279d6596031SMatthias Ringwald void hci_remote_features_query(hci_con_handle_t con_handle){ 7280d6596031SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 7281d6596031SMatthias Ringwald if (!connection) return; 7282c9742cd1SMatthias Ringwald hci_trigger_remote_features_for_connection(connection); 7283d6596031SMatthias Ringwald hci_run(); 7284d6596031SMatthias Ringwald } 7285d6596031SMatthias Ringwald 7286458bf4e8S[email protected] // GAP API 7287458bf4e8S[email protected] /** 7288458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 7289458bf4e8S[email protected] * @praram enabled 7290458bf4e8S[email protected] */ 72914c57c146S[email protected] void gap_set_bondable_mode(int enable){ 72923a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 7293458bf4e8S[email protected] } 72944ef6443cSMatthias Ringwald /** 72954ef6443cSMatthias Ringwald * @brief Get bondable mode. 72964ef6443cSMatthias Ringwald * @return 1 if bondable 72974ef6443cSMatthias Ringwald */ 72984ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 72994ef6443cSMatthias Ringwald return hci_stack->bondable; 73004ef6443cSMatthias Ringwald } 7301cb230b9dS[email protected] 7302cb230b9dS[email protected] /** 730334d2123cS[email protected] * @brief map link keys to security levels 7304cb230b9dS[email protected] */ 730534d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 730634d2123cS[email protected] switch (link_key_type){ 73073c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 73083c68dfa9S[email protected] return LEVEL_4; 73093c68dfa9S[email protected] case COMBINATION_KEY: 73103c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 73113c68dfa9S[email protected] return LEVEL_3; 73123c68dfa9S[email protected] default: 73133c68dfa9S[email protected] return LEVEL_2; 73143c68dfa9S[email protected] } 7315cb230b9dS[email protected] } 7316cb230b9dS[email protected] 73178b35e16aSMatthias Ringwald /** 73188b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 73198b35e16aSMatthias Ringwald */ 732036c3546bSMatthias Ringwald bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 73218b35e16aSMatthias Ringwald switch (link_key_type){ 73228b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 73238b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 732436c3546bSMatthias Ringwald return true; 73258b35e16aSMatthias Ringwald default: 732636c3546bSMatthias Ringwald return false; 73278b35e16aSMatthias Ringwald } 73288b35e16aSMatthias Ringwald } 73298b35e16aSMatthias Ringwald 73308b35e16aSMatthias Ringwald /** 73318b35e16aSMatthias Ringwald * @brief map link keys to authenticated 73328b35e16aSMatthias Ringwald */ 73337040ba26SMatthias Ringwald bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 73348b35e16aSMatthias Ringwald switch (link_key_type){ 73358b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 73368b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 73377040ba26SMatthias Ringwald return true; 73388b35e16aSMatthias Ringwald default: 73397040ba26SMatthias Ringwald return false; 73408b35e16aSMatthias Ringwald } 73418b35e16aSMatthias Ringwald } 73428b35e16aSMatthias Ringwald 7343552b9260SMatthias Ringwald bool gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 73445127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 7345106d6d11S[email protected] return level > LEVEL_2; 7346106d6d11S[email protected] } 7347106d6d11S[email protected] 734834d2123cS[email protected] /** 734934d2123cS[email protected] * @brief get current security level 735034d2123cS[email protected] */ 735134d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 735234d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 735334d2123cS[email protected] if (!connection) return LEVEL_0; 735434d2123cS[email protected] return gap_security_level_for_connection(connection); 735534d2123cS[email protected] } 735634d2123cS[email protected] 7357cb230b9dS[email protected] /** 7358cb230b9dS[email protected] * @brief request connection to device to 7359cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 7360cb230b9dS[email protected] */ 736134d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 736234d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 736334d2123cS[email protected] if (!connection){ 7364a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 736534d2123cS[email protected] return; 736634d2123cS[email protected] } 7367defbf200SMatthias Ringwald 7368defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 7369defbf200SMatthias Ringwald 7370bc00e12cSMatthias 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) 7371bc00e12cSMatthias Ringwald // available on the BR/EDR physical transport require Security Mode 4, Level 4 " 7372bc00e12cSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode && (requested_level != LEVEL_0)){ 7373bc00e12cSMatthias Ringwald requested_level = LEVEL_4; 7374bc00e12cSMatthias Ringwald } 7375bc00e12cSMatthias Ringwald 737634d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 737783d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 737883d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 737983d08d7cSMatthias Ringwald 7380dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 7381dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 7382dbd5dcc3SMatthias Ringwald if (authentication_active){ 73831cf0a6c8SMatthias Ringwald // authentication already active 738483d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 738583d08d7cSMatthias Ringwald // increase requested level as new level is higher 738683d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 738783d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 738883d08d7cSMatthias Ringwald } 73891cf0a6c8SMatthias Ringwald } else { 739083d08d7cSMatthias Ringwald // no request active, notify if security sufficient 739183d08d7cSMatthias Ringwald if (requested_level <= current_level){ 7392a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 739334d2123cS[email protected] return; 739434d2123cS[email protected] } 7395a00031e2S[email protected] 7396e060c07dSMatthias Ringwald // store request 739734d2123cS[email protected] connection->requested_security_level = requested_level; 7398a00031e2S[email protected] 73991cf0a6c8SMatthias Ringwald // start to authenticate connection 74001eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 74017fdbaf79SMatthias Ringwald 74027fdbaf79SMatthias Ringwald // request remote features if not already active, also trigger hci_run 74037fdbaf79SMatthias Ringwald hci_remote_features_query(con_handle); 7404e00caf9cS[email protected] } 74051cf0a6c8SMatthias Ringwald } 7406ad83dc6aS[email protected] 7407ad83dc6aS[email protected] /** 7408ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 7409ad83dc6aS[email protected] * @param device 7410ad83dc6aS[email protected] * @param request MITM protection 7411ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 7412ad83dc6aS[email protected] */ 7413ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 7414ad83dc6aS[email protected] 7415ad83dc6aS[email protected] // create connection state machine 7416f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 7417ad83dc6aS[email protected] 7418ad83dc6aS[email protected] if (!connection){ 7419ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 7420ad83dc6aS[email protected] } 7421ad83dc6aS[email protected] 7422ad83dc6aS[email protected] // delete linkn key 742315a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 7424ad83dc6aS[email protected] 7425ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 7426ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 7427ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 7428f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 7429ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 7430ad83dc6aS[email protected] 7431ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 7432ad83dc6aS[email protected] 7433ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 7434ad83dc6aS[email protected] // handle: authentication failure 7435ad83dc6aS[email protected] // handle: disconnect on done 7436ad83dc6aS[email protected] 7437ad83dc6aS[email protected] hci_run(); 7438ad83dc6aS[email protected] 7439ad83dc6aS[email protected] return 0; 7440ad83dc6aS[email protected] } 74418e618f72S[email protected] 74428e618f72S[email protected] void gap_set_local_name(const char * local_name){ 74438e618f72S[email protected] hci_stack->local_name = local_name; 7444baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_LOCAL_NAME; 744559d59ecfSMatthias Ringwald // also update EIR if not set by user 744659d59ecfSMatthias Ringwald if (hci_stack->eir_data == NULL){ 7447baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 744859d59ecfSMatthias Ringwald } 744959d59ecfSMatthias Ringwald hci_run(); 74508e618f72S[email protected] } 74511aeec2ebSMatthias Ringwald #endif 74528e618f72S[email protected] 745335454696SMatthias Ringwald 745435454696SMatthias Ringwald #ifdef ENABLE_BLE 745535454696SMatthias Ringwald 7456d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7457d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 7458fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 74597bdc6798S[email protected] hci_run(); 74607bdc6798S[email protected] } 74618e618f72S[email protected] 7462d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 7463fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 74647bdc6798S[email protected] hci_run(); 74657bdc6798S[email protected] } 74664f3229d8S[email protected] 7467a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 7468ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 7469a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 7470ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 7471ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 74728b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 7473ef11999fSmatthias.ringwald hci_run(); 7474ef11999fSmatthias.ringwald } 74754f3229d8S[email protected] 7476a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 7477a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 7478a7a719e9SMatthias Ringwald } 7479a7a719e9SMatthias Ringwald 7480667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 74814f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 74824f3229d8S[email protected] if (!conn){ 7483d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 7484d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 7485d32b3f05SMatthias Ringwald log_error("le connection already active"); 7486d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 7487d32b3f05SMatthias Ringwald } 7488d32b3f05SMatthias Ringwald 7489d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 74902e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 74914f3229d8S[email protected] if (!conn){ 74924f3229d8S[email protected] // notify client that alloc failed 74936e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 7494d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 7495472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 74964f3229d8S[email protected] } 7497d5b1a89eSMatthias Ringwald 7498d5b1a89eSMatthias Ringwald // set le connecting state 7499d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 7500d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 7501d5b1a89eSMatthias Ringwald } 7502d5b1a89eSMatthias Ringwald 75034f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 7504d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 7505564fca32S[email protected] hci_run(); 7506b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 75074f3229d8S[email protected] } 75080bf6344aS[email protected] 75090bf6344aS[email protected] if (!hci_is_le_connection(conn) || 7510a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 75110e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 75122e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 7513d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 7514616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 75150bf6344aS[email protected] } 75160bf6344aS[email protected] 7517b0136355SMatthias Ringwald // check if connection was just disconnected 7518b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 7519b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 7520b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 7521b0136355SMatthias Ringwald hci_run(); 7522b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 7523b0136355SMatthias Ringwald } 7524b0136355SMatthias Ringwald 7525d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 7526d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 75274f3229d8S[email protected] hci_run(); 7528b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 75294f3229d8S[email protected] } 75304f3229d8S[email protected] 75317851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 7532d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 7533665d90f2SMatthias Ringwald btstack_linked_item_t *it; 7534a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 75350bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 75360bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 75370bf6344aS[email protected] switch (conn->state){ 7538a6725849S[email protected] case SEND_CREATE_CONNECTION: 75397851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 75407851196eSmatthias.ringwald return conn; 75417851196eSmatthias.ringwald default: 75427851196eSmatthias.ringwald break; 75437851196eSmatthias.ringwald }; 75447851196eSmatthias.ringwald } 75457851196eSmatthias.ringwald return NULL; 75467851196eSmatthias.ringwald } 75477851196eSmatthias.ringwald 7548d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 754914f2d8f0SMatthias Ringwald hci_connection_t * conn; 755014f2d8f0SMatthias Ringwald switch (hci_stack->le_connecting_request){ 755114f2d8f0SMatthias Ringwald case LE_CONNECTING_IDLE: 755214f2d8f0SMatthias Ringwald break; 755314f2d8f0SMatthias Ringwald case LE_CONNECTING_WHITELIST: 755414f2d8f0SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 755514f2d8f0SMatthias Ringwald hci_run(); 755614f2d8f0SMatthias Ringwald break; 755714f2d8f0SMatthias Ringwald case LE_CONNECTING_DIRECT: 755814f2d8f0SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 755914f2d8f0SMatthias Ringwald conn = gap_get_outgoing_connection(); 756014f2d8f0SMatthias Ringwald if (conn == NULL){ 756114f2d8f0SMatthias Ringwald hci_run(); 756214f2d8f0SMatthias Ringwald } else { 75637851196eSmatthias.ringwald switch (conn->state){ 75647851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 75657851196eSmatthias.ringwald // skip sending create connection and emit event instead 75662e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 7567665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 75687851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 75690bf6344aS[email protected] break; 7570a6725849S[email protected] case SENT_CREATE_CONNECTION: 7571ea04fbd8SMatthias Ringwald // let hci_run_general_gap_le cancel outgoing connection 75720bf6344aS[email protected] hci_run(); 75730bf6344aS[email protected] break; 75740bf6344aS[email protected] default: 75750bf6344aS[email protected] break; 75760bf6344aS[email protected] } 757714f2d8f0SMatthias Ringwald } 757814f2d8f0SMatthias Ringwald break; 757943dfd98aSMatthias Ringwald default: 758014f2d8f0SMatthias Ringwald btstack_unreachable(); 758114f2d8f0SMatthias Ringwald break; 758214f2d8f0SMatthias Ringwald } 758314f2d8f0SMatthias Ringwald return ERROR_CODE_SUCCESS; 7584e31f89a7S[email protected] } 75854f3229d8S[email protected] 7586c37a3166S[email protected] /** 75876012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 7588cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 7589cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 75906012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 75916012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 75926012052bSMatthias Ringwald * @param conn_latency, default: 4 75936012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 75946012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 75956012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 75966012052bSMatthias Ringwald */ 75976012052bSMatthias Ringwald 7598cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 7599cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 7600cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 7601cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 7602cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 76036012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 76046012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 76056012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 76066012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 76076012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 76086012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 76096012052bSMatthias Ringwald } 761013e645d2SMatthias Ringwald #endif 76116012052bSMatthias Ringwald 76126012052bSMatthias Ringwald /** 7613c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 7614c37a3166S[email protected] * @param handle 7615c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 7616c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 7617c37a3166S[email protected] * @param conn_latency 7618c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 76196b65794dSMilanka Ringwald * @return 0 if ok 7620c37a3166S[email protected] */ 7621c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 7622c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 7623c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 7624c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7625c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 7626c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 7627c37a3166S[email protected] connection->le_conn_latency = conn_latency; 7628c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 762984cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 7630cfc59f1bSMatthias Ringwald hci_run(); 7631c37a3166S[email protected] return 0; 7632c37a3166S[email protected] } 7633c37a3166S[email protected] 763445c102fdSMatthias Ringwald /** 7635b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 7636b68d7bc3SMatthias Ringwald * @param handle 7637b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 7638b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 7639b68d7bc3SMatthias Ringwald * @param conn_latency 7640b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 76416b65794dSMilanka Ringwald * @return 0 if ok 7642b68d7bc3SMatthias Ringwald */ 7643b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 7644b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 7645b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 7646b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7647b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 7648b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 7649b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 7650b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 7651b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 765209c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 765309c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 7654b68d7bc3SMatthias Ringwald return 0; 7655b68d7bc3SMatthias Ringwald } 7656b68d7bc3SMatthias Ringwald 7657d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 7658d70217a2SMatthias Ringwald 7659b68d7bc3SMatthias Ringwald /** 766045c102fdSMatthias Ringwald * @brief Set Advertisement Data 766145c102fdSMatthias Ringwald * @param advertising_data_length 766245c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 766345c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 766445c102fdSMatthias Ringwald */ 766545c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 766645c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 766745c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 7668501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 7669bbc366e6SMatthias Ringwald hci_run(); 767045c102fdSMatthias Ringwald } 7671501f56b3SMatthias Ringwald 7672501f56b3SMatthias Ringwald /** 7673501f56b3SMatthias Ringwald * @brief Set Scan Response Data 7674501f56b3SMatthias Ringwald * @param advertising_data_length 7675501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 7676501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 7677501f56b3SMatthias Ringwald */ 7678501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 7679501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 7680501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 7681501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 7682bbc366e6SMatthias Ringwald hci_run(); 768345c102fdSMatthias Ringwald } 768445c102fdSMatthias Ringwald 768545c102fdSMatthias Ringwald /** 768645c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 768745c102fdSMatthias Ringwald * @param adv_int_min 768845c102fdSMatthias Ringwald * @param adv_int_max 768945c102fdSMatthias Ringwald * @param adv_type 769045c102fdSMatthias Ringwald * @param direct_address_type 769145c102fdSMatthias Ringwald * @param direct_address 769245c102fdSMatthias Ringwald * @param channel_map 769345c102fdSMatthias Ringwald * @param filter_policy 769445c102fdSMatthias Ringwald * 769545c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 769645c102fdSMatthias Ringwald */ 769745c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 7698b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 769945c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 770045c102fdSMatthias Ringwald 770145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 770245c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 770345c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 770445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 770545c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 770645c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 77076535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 77086535961aSMatthias Ringwald 6); 770945c102fdSMatthias Ringwald 77106bf435a6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 77116bf435a6SMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_PARAMS_SET; 7712bbc366e6SMatthias Ringwald hci_run(); 771345c102fdSMatthias Ringwald } 771445c102fdSMatthias Ringwald 771545c102fdSMatthias Ringwald /** 771645c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 771745c102fdSMatthias Ringwald * @param enabled 771845c102fdSMatthias Ringwald */ 771945c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 7720a408e724SMatthias Ringwald if (enabled == 0){ 7721a408e724SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ENABLED; 7722a408e724SMatthias Ringwald } else { 7723a408e724SMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_ENABLED; 7724a408e724SMatthias Ringwald } 7725bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 7726cfc59f1bSMatthias Ringwald hci_run(); 772745c102fdSMatthias Ringwald } 772845c102fdSMatthias Ringwald 7729a5770ecbSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 7730a5770ecbSMatthias Ringwald static le_advertising_set_t * hci_advertising_set_for_handle(uint8_t advertising_handle){ 7731a5770ecbSMatthias Ringwald btstack_linked_list_iterator_t it; 7732a5770ecbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 7733a5770ecbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7734a5770ecbSMatthias Ringwald le_advertising_set_t * item = (le_advertising_set_t *) btstack_linked_list_iterator_next(&it); 7735a5770ecbSMatthias Ringwald if ( item->advertising_handle == advertising_handle ) { 7736a5770ecbSMatthias Ringwald return item; 7737a5770ecbSMatthias Ringwald } 7738a5770ecbSMatthias Ringwald } 7739a5770ecbSMatthias Ringwald return NULL; 7740a5770ecbSMatthias Ringwald } 7741a5770ecbSMatthias Ringwald 7742a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_setup(le_advertising_set_t * storage, const le_extended_advertising_parameters_t * advertising_parameters, uint8_t * out_advertising_handle){ 7743a5770ecbSMatthias Ringwald // find free advertisement handle 7744a5770ecbSMatthias Ringwald uint8_t advertisement_handle; 7745a5770ecbSMatthias Ringwald for (advertisement_handle = 1; advertisement_handle <= LE_EXTENDED_ADVERTISING_MAX_HANDLE; advertisement_handle++){ 7746a5770ecbSMatthias Ringwald if (hci_advertising_set_for_handle(advertisement_handle) == NULL) break; 7747a5770ecbSMatthias Ringwald } 7748a5770ecbSMatthias Ringwald if (advertisement_handle > LE_EXTENDED_ADVERTISING_MAX_HANDLE) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 7749a5770ecbSMatthias Ringwald // clear 7750a5770ecbSMatthias Ringwald memset(storage, 0, sizeof(le_advertising_set_t)); 7751a5770ecbSMatthias Ringwald // copy params 77520cbe4690SMatthias Ringwald storage->advertising_handle = advertisement_handle; 775357a04237SMatthias Ringwald memcpy(&storage->extended_params, advertising_parameters, sizeof(le_extended_advertising_parameters_t)); 7754a5770ecbSMatthias Ringwald // add to list 7755a5770ecbSMatthias Ringwald bool add_ok = btstack_linked_list_add(&hci_stack->le_advertising_sets, (btstack_linked_item_t *) storage); 7756a5770ecbSMatthias Ringwald if (!add_ok) return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 77570cbe4690SMatthias Ringwald *out_advertising_handle = advertisement_handle; 7758a5770ecbSMatthias Ringwald // set tasks and start 7759a5770ecbSMatthias Ringwald storage->tasks = LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7760a5770ecbSMatthias Ringwald hci_run(); 7761a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7762a5770ecbSMatthias Ringwald } 7763a5770ecbSMatthias Ringwald 7764a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_set_params(uint8_t advertising_handle, const le_extended_advertising_parameters_t * advertising_parameters){ 7765a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7766a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 776757a04237SMatthias Ringwald memcpy(&advertising_set->extended_params, advertising_parameters, sizeof(le_extended_advertising_parameters_t)); 7768a5770ecbSMatthias Ringwald // set tasks and start 7769a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7770a5770ecbSMatthias Ringwald hci_run(); 7771a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7772a5770ecbSMatthias Ringwald } 7773a5770ecbSMatthias Ringwald 7774a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_get_params(uint8_t advertising_handle, le_extended_advertising_parameters_t * advertising_parameters){ 7775a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7776a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 777757a04237SMatthias Ringwald memcpy(advertising_parameters, &advertising_set->extended_params, sizeof(le_extended_advertising_parameters_t)); 7778a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7779a5770ecbSMatthias Ringwald } 7780a5770ecbSMatthias Ringwald 7781a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_set_random_address(uint8_t advertising_handle, bd_addr_t random_address){ 7782a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7783a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7784a5770ecbSMatthias Ringwald memcpy(advertising_set->random_address, random_address, 6); 7785a5770ecbSMatthias Ringwald // set tasks and start 7786a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 7787a5770ecbSMatthias Ringwald hci_run(); 7788a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7789a5770ecbSMatthias Ringwald } 7790a5770ecbSMatthias Ringwald 77912e4aaf70SMatthias Ringwald uint8_t gap_extended_advertising_set_adv_data(uint8_t advertising_handle, uint16_t advertising_data_length, const uint8_t * advertising_data){ 7792a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7793a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7794a5770ecbSMatthias Ringwald advertising_set->adv_data = advertising_data; 7795a5770ecbSMatthias Ringwald advertising_set->adv_data_len = advertising_data_length; 7796a5770ecbSMatthias Ringwald // set tasks and start 7797a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 7798a5770ecbSMatthias Ringwald hci_run(); 7799a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7800a5770ecbSMatthias Ringwald } 7801a5770ecbSMatthias Ringwald 78022e4aaf70SMatthias Ringwald uint8_t gap_extended_advertising_set_scan_response_data(uint8_t advertising_handle, uint16_t scan_response_data_length, const uint8_t * scan_response_data){ 7803a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7804a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7805a5770ecbSMatthias Ringwald advertising_set->scan_data = scan_response_data; 7806a5770ecbSMatthias Ringwald advertising_set->scan_data_len = scan_response_data_length; 7807a5770ecbSMatthias Ringwald // set tasks and start 7808a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 7809a5770ecbSMatthias Ringwald hci_run(); 7810a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7811a5770ecbSMatthias Ringwald } 7812a5770ecbSMatthias Ringwald 7813a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_start(uint8_t advertising_handle, uint16_t timeout, uint8_t num_extended_advertising_events){ 7814a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7815a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7816a5770ecbSMatthias Ringwald advertising_set->enable_timeout = timeout; 7817a5770ecbSMatthias Ringwald advertising_set->enable_max_scan_events = num_extended_advertising_events; 7818a5770ecbSMatthias Ringwald // set tasks and start 7819a5770ecbSMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_ENABLED; 7820a5770ecbSMatthias Ringwald hci_run(); 7821a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7822a5770ecbSMatthias Ringwald } 7823a5770ecbSMatthias Ringwald 7824a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_stop(uint8_t advertising_handle){ 7825a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7826a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7827a5770ecbSMatthias Ringwald // set tasks and start 7828a5770ecbSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_ENABLED; 7829a5770ecbSMatthias Ringwald hci_run(); 7830a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7831a5770ecbSMatthias Ringwald } 7832a5770ecbSMatthias Ringwald 7833c814a904SMatthias Ringwald uint8_t gap_extended_advertising_remove(uint8_t advertising_handle){ 7834c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7835c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7836c814a904SMatthias Ringwald // set tasks and start 7837c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_REMOVE_SET; 7838c814a904SMatthias Ringwald hci_run(); 7839c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 7840c814a904SMatthias Ringwald } 7841c814a904SMatthias Ringwald 7842c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 7843c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_set_params(uint8_t advertising_handle, const le_periodic_advertising_parameters_t * advertising_parameters){ 7844c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7845c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7846c814a904SMatthias Ringwald // periodic advertising requires neither connectable, scannable, legacy or anonymous 7847c814a904SMatthias Ringwald if ((advertising_set->extended_params.advertising_event_properties & 0x1f) != 0) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 7848c814a904SMatthias Ringwald memcpy(&advertising_set->periodic_params, advertising_parameters, sizeof(le_periodic_advertising_parameters_t)); 7849c814a904SMatthias Ringwald // set tasks and start 7850c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS; 7851c814a904SMatthias Ringwald hci_run(); 7852c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 7853c814a904SMatthias Ringwald } 7854c814a904SMatthias Ringwald 7855c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_get_params(uint8_t advertising_handle, le_periodic_advertising_parameters_t * advertising_parameters){ 7856c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7857c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7858c814a904SMatthias Ringwald memcpy(advertising_parameters, &advertising_set->extended_params, sizeof(le_periodic_advertising_parameters_t)); 7859c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 7860c814a904SMatthias Ringwald } 7861c814a904SMatthias Ringwald 7862c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_set_data(uint8_t advertising_handle, uint16_t periodic_data_length, const uint8_t * periodic_data){ 7863c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7864c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7865c814a904SMatthias Ringwald advertising_set->periodic_data = periodic_data; 7866c814a904SMatthias Ringwald advertising_set->periodic_data_len = periodic_data_length; 7867c814a904SMatthias Ringwald // set tasks and start 7868c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA; 7869c814a904SMatthias Ringwald hci_run(); 7870c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 7871c814a904SMatthias Ringwald } 7872c814a904SMatthias Ringwald 787353f240c0SMatthias Ringwald uint8_t gap_periodic_advertising_start(uint8_t advertising_handle, bool include_adi){ 787453f240c0SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 787553f240c0SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 787653f240c0SMatthias Ringwald // set tasks and start 787753f240c0SMatthias Ringwald advertising_set->periodic_include_adi = include_adi; 787853f240c0SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED; 787953f240c0SMatthias Ringwald hci_run(); 788053f240c0SMatthias Ringwald return ERROR_CODE_SUCCESS; 788153f240c0SMatthias Ringwald } 788253f240c0SMatthias Ringwald 788353f240c0SMatthias Ringwald uint8_t gap_periodic_advertising_stop(uint8_t advertising_handle){ 788453f240c0SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 788553f240c0SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 788653f240c0SMatthias Ringwald // set tasks and start 788753f240c0SMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED; 788853f240c0SMatthias Ringwald hci_run(); 788953f240c0SMatthias Ringwald return ERROR_CODE_SUCCESS; 789053f240c0SMatthias Ringwald } 7891c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 789253f240c0SMatthias Ringwald 7893a5770ecbSMatthias Ringwald #endif 7894a5770ecbSMatthias Ringwald 789535454696SMatthias Ringwald #endif 789606e5cf96SMatthias Ringwald 789706e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 789806e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 789906e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 790006e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 790106e5cf96SMatthias Ringwald 790264068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 790306e5cf96SMatthias Ringwald // update advertisement parameters, too 790406e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7905bbc366e6SMatthias Ringwald hci_run(); 790664068776SMatthias Ringwald #endif 790764068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 790806e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 790964068776SMatthias Ringwald #endif 791006e5cf96SMatthias Ringwald } 791106e5cf96SMatthias Ringwald 79129136bde8SMatthias Ringwald void hci_le_random_address_set(const bd_addr_t random_address){ 79139136bde8SMatthias Ringwald memcpy(hci_stack->le_random_address, random_address, 6); 79149136bde8SMatthias Ringwald hci_stack->le_random_address_set = true; 79159136bde8SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 79169136bde8SMatthias Ringwald hci_run(); 79179136bde8SMatthias Ringwald } 79189136bde8SMatthias Ringwald 7919d70217a2SMatthias Ringwald #endif 792045c102fdSMatthias Ringwald 7921616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 79225917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 79235917a5c5S[email protected] if (!conn){ 79247851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 7925616edd56SMatthias Ringwald return 0; 79265917a5c5S[email protected] } 79277fd7aa6fSMatthias Ringwald // ignore if already disconnected 79287fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 79297fd7aa6fSMatthias Ringwald return 0; 79307fd7aa6fSMatthias Ringwald } 79315917a5c5S[email protected] conn->state = SEND_DISCONNECT; 79325917a5c5S[email protected] hci_run(); 7933616edd56SMatthias Ringwald return 0; 79344f3229d8S[email protected] } 793504a6ef8cSmatthias.ringwald 7936228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 7937228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 7938228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 793922df2fe2SMatthias Ringwald hci_connection->gap_connection_tasks |= GAP_CONNECTION_TASK_READ_RSSI; 7940228e430cSMatthias Ringwald hci_run(); 7941228e430cSMatthias Ringwald return 1; 7942228e430cSMatthias Ringwald } 7943228e430cSMatthias Ringwald 7944a1bf5ae7SMatthias Ringwald /** 7945a1bf5ae7SMatthias Ringwald * @brief Get connection type 7946a1bf5ae7SMatthias Ringwald * @param con_handle 7947a1bf5ae7SMatthias Ringwald * @result connection_type 7948a1bf5ae7SMatthias Ringwald */ 7949a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 7950a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 7951a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 7952a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 7953a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 7954a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 7955a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 7956a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 7957a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 7958f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 7959a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 7960a1bf5ae7SMatthias Ringwald default: 7961a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 7962a1bf5ae7SMatthias Ringwald } 7963a1bf5ae7SMatthias Ringwald } 7964a1bf5ae7SMatthias Ringwald 79652dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 79662dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 79672dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 79682dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 79692dceb1d6SMatthias Ringwald } 79702dceb1d6SMatthias Ringwald 79712dceb1d6SMatthias Ringwald 797244f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 7973667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 797488a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 797588a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 797688a03c8dSMatthias Ringwald conn->request_role = role; 797788a03c8dSMatthias Ringwald hci_run(); 7978d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 797988a03c8dSMatthias Ringwald } 798044f858f3SMatthias Ringwald #endif 798188a03c8dSMatthias Ringwald 7982a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 79834f551432SMatthias Ringwald 7984b45b7749SMilanka 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){ 7985b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 7986b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7987b90f6e0aSMatthias Ringwald 7988b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 7989b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 7990b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 7991b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 7992b90f6e0aSMatthias Ringwald 7993b90f6e0aSMatthias Ringwald hci_run(); 7994b90f6e0aSMatthias Ringwald 7995b90f6e0aSMatthias Ringwald return 0; 7996b90f6e0aSMatthias Ringwald } 7997b90f6e0aSMatthias Ringwald 7998667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 79997a92a9dbSMatthias Ringwald // check if already in list 80007a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 80017a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 80027a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 80037a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 80047a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 80057a92a9dbSMatthias Ringwald continue; 80067a92a9dbSMatthias Ringwald } 80077a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 80087a92a9dbSMatthias Ringwald continue; 80097a92a9dbSMatthias Ringwald } 8010287379ccSMatthias Ringwald // disallow if already scheduled to add 8011287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 80127a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 80137a92a9dbSMatthias Ringwald } 8014287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 8015287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 8016287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 8017287379ccSMatthias Ringwald } 80187a92a9dbSMatthias Ringwald // alloc and add to list 8019e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 8020e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 8021e83201bcSMatthias Ringwald entry->address_type = address_type; 80226535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 8023e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 8024665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 8025226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 8026ac9c45e0SMatthias Ringwald } 8027ac9c45e0SMatthias Ringwald 8028667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 8029665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8030665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 8031665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8032665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 80337a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 80347a92a9dbSMatthias Ringwald continue; 80357a92a9dbSMatthias Ringwald } 80367a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 80377a92a9dbSMatthias Ringwald continue; 80387a92a9dbSMatthias Ringwald } 8039e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 8040e83201bcSMatthias Ringwald // remove from controller if already present 8041e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 8042a3b69fdeSMatthias Ringwald } else { 8043226db5efSMatthias Ringwald // directly remove entry from whitelist 8044665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8045e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 8046e83201bcSMatthias Ringwald } 8047a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8048a3b69fdeSMatthias Ringwald } 8049a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 805042ff5ba1SMatthias Ringwald } 805142ff5ba1SMatthias Ringwald 8052226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 8053665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8054665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 8055665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8056665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 8057e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 8058e83201bcSMatthias Ringwald // remove from controller if already present 8059e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 8060e83201bcSMatthias Ringwald continue; 8061e83201bcSMatthias Ringwald } 806291915b0bSMatthias Ringwald // directly remove entry from whitelist 8063665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8064e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 8065e83201bcSMatthias Ringwald } 8066226db5efSMatthias Ringwald } 8067226db5efSMatthias Ringwald 806883dc1a98SMatthias Ringwald // free all entries unconditionally 806983dc1a98SMatthias Ringwald static void hci_whitelist_free(void){ 807083dc1a98SMatthias Ringwald btstack_linked_list_iterator_t lit; 807183dc1a98SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 807283dc1a98SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 807383dc1a98SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 807483dc1a98SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 807583dc1a98SMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 807683dc1a98SMatthias Ringwald } 807783dc1a98SMatthias Ringwald } 807883dc1a98SMatthias Ringwald 8079a3b69fdeSMatthias Ringwald /** 8080a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 80816b65794dSMilanka Ringwald * @return 0 if ok 8082a3b69fdeSMatthias Ringwald */ 8083a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 8084a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 8085a3b69fdeSMatthias Ringwald hci_run(); 8086a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8087a3b69fdeSMatthias Ringwald } 8088a3b69fdeSMatthias Ringwald 8089a3b69fdeSMatthias Ringwald /** 8090a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 8091a3b69fdeSMatthias Ringwald * @param address_typ 8092a3b69fdeSMatthias Ringwald * @param address 80936b65794dSMilanka Ringwald * @return 0 if ok 8094a3b69fdeSMatthias Ringwald */ 8095667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 8096a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 8097a3b69fdeSMatthias Ringwald if (status){ 8098a3b69fdeSMatthias Ringwald return status; 8099a3b69fdeSMatthias Ringwald } 8100a3b69fdeSMatthias Ringwald hci_run(); 8101a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8102a3b69fdeSMatthias Ringwald } 8103a3b69fdeSMatthias Ringwald 8104a3b69fdeSMatthias Ringwald /** 8105a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 8106a3b69fdeSMatthias Ringwald * @param address_typ 8107a3b69fdeSMatthias Ringwald * @param address 81086b65794dSMilanka Ringwald * @return 0 if ok 8109a3b69fdeSMatthias Ringwald */ 8110667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 8111a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 8112a3b69fdeSMatthias Ringwald if (status){ 8113a3b69fdeSMatthias Ringwald return status; 8114a3b69fdeSMatthias Ringwald } 8115a3b69fdeSMatthias Ringwald hci_run(); 8116a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8117a3b69fdeSMatthias Ringwald } 8118a3b69fdeSMatthias Ringwald 8119226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 8120226db5efSMatthias Ringwald /** 812195e257d9SMatthias Ringwald * @brief Connect with Whitelist 812295e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 81236b65794dSMilanka Ringwald * @return - if ok 812495e257d9SMatthias Ringwald */ 812595e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 812695e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 812795e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 812895e257d9SMatthias Ringwald } 812995e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 813095e257d9SMatthias Ringwald hci_run(); 813195e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 813295e257d9SMatthias Ringwald } 813395e257d9SMatthias Ringwald 813495e257d9SMatthias Ringwald /** 8135226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 8136226db5efSMatthias Ringwald * @param address_typ 8137226db5efSMatthias Ringwald * @param address 81386b65794dSMilanka Ringwald * @return 0 if ok 8139226db5efSMatthias Ringwald */ 8140667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 814195e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 8142226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 8143226db5efSMatthias Ringwald } 8144226db5efSMatthias Ringwald 8145226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 814663f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 8147226db5efSMatthias Ringwald return status; 8148226db5efSMatthias Ringwald } 8149226db5efSMatthias Ringwald 8150226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 8151226db5efSMatthias Ringwald 8152226db5efSMatthias Ringwald hci_run(); 815395e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8154226db5efSMatthias Ringwald } 8155226db5efSMatthias Ringwald 8156226db5efSMatthias Ringwald /** 8157226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 8158226db5efSMatthias Ringwald * @param address_typ 8159226db5efSMatthias Ringwald * @param address 81606b65794dSMilanka Ringwald * @return 0 if ok 8161226db5efSMatthias Ringwald */ 8162667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 816395e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 816495e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 816595e257d9SMatthias Ringwald } 816695e257d9SMatthias Ringwald 8167226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 816895e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 816995e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 817095e257d9SMatthias Ringwald } 8171226db5efSMatthias Ringwald hci_run(); 8172226db5efSMatthias Ringwald return 0; 8173226db5efSMatthias Ringwald } 8174226db5efSMatthias Ringwald 8175226db5efSMatthias Ringwald /** 8176226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 8177226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 8178226db5efSMatthias Ringwald */ 817995e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 818095e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 818195e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 818295e257d9SMatthias Ringwald } 8183226db5efSMatthias Ringwald hci_whitelist_clear(); 8184226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 8185e83201bcSMatthias Ringwald hci_run(); 818695e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8187ac9c45e0SMatthias Ringwald } 8188c9db5c21SMilanka Ringwald 8189b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 8190b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 8191c9db5c21SMilanka Ringwald if (!conn) return 0; 8192c9db5c21SMilanka Ringwald return conn->le_connection_interval; 8193c9db5c21SMilanka Ringwald } 8194d70217a2SMatthias Ringwald #endif 81954f551432SMatthias Ringwald #endif 81964f551432SMatthias Ringwald 819735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 8198ac9c45e0SMatthias Ringwald /** 8199ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 8200a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 8201ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 8202ff00ed1cSMatthias Ringwald */ 8203ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 8204ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 8205baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 820659d59ecfSMatthias Ringwald hci_run(); 8207ff00ed1cSMatthias Ringwald } 8208ff00ed1cSMatthias Ringwald 8209ff00ed1cSMatthias Ringwald /** 8210f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 8211f5875de5SMatthias Ringwald * @param duration in 1.28s units 8212f5875de5SMatthias Ringwald * @return 0 if ok 8213f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 8214f5875de5SMatthias Ringwald */ 8215f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 821699449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 8217f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8218a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 8219f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 8220f5875de5SMatthias Ringwald } 8221f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 822230199e24SMatthias Ringwald hci_stack->inquiry_max_period_length = 0; 822330199e24SMatthias Ringwald hci_stack->inquiry_min_period_length = 0; 822430199e24SMatthias Ringwald hci_run(); 822530199e24SMatthias Ringwald return 0; 822630199e24SMatthias Ringwald } 822730199e24SMatthias Ringwald 822830199e24SMatthias Ringwald uint8_t gap_inquiry_periodic_start(uint8_t duration, uint16_t max_period_length, uint16_t min_period_length){ 822930199e24SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 823030199e24SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 823130199e24SMatthias Ringwald if (duration < GAP_INQUIRY_DURATION_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 823230199e24SMatthias Ringwald if (duration > GAP_INQUIRY_DURATION_MAX) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 823330199e24SMatthias Ringwald if (max_period_length < GAP_INQUIRY_MAX_PERIODIC_LEN_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;; 823430199e24SMatthias Ringwald if (min_period_length < GAP_INQUIRY_MIN_PERIODIC_LEN_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;; 823530199e24SMatthias Ringwald 823630199e24SMatthias Ringwald hci_stack->inquiry_state = duration; 823730199e24SMatthias Ringwald hci_stack->inquiry_max_period_length = max_period_length; 823830199e24SMatthias Ringwald hci_stack->inquiry_min_period_length = min_period_length; 8239f5875de5SMatthias Ringwald hci_run(); 8240f5875de5SMatthias Ringwald return 0; 8241f5875de5SMatthias Ringwald } 8242f5875de5SMatthias Ringwald 8243f5875de5SMatthias Ringwald /** 8244f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 82456b65794dSMilanka Ringwald * @return 0 if ok 8246f5875de5SMatthias Ringwald */ 8247f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 8248a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 8249f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 8250f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 8251f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 8252f5875de5SMatthias Ringwald return 0; 8253f5875de5SMatthias Ringwald } 825430199e24SMatthias Ringwald switch (hci_stack->inquiry_state){ 825530199e24SMatthias Ringwald case GAP_INQUIRY_STATE_ACTIVE: 8256f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 8257f5875de5SMatthias Ringwald hci_run(); 825830199e24SMatthias Ringwald return ERROR_CODE_SUCCESS; 825930199e24SMatthias Ringwald case GAP_INQUIRY_STATE_PERIODIC: 826030199e24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_EXIT_PERIODIC; 826130199e24SMatthias Ringwald hci_run(); 826230199e24SMatthias Ringwald return ERROR_CODE_SUCCESS; 826330199e24SMatthias Ringwald default: 826430199e24SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 826530199e24SMatthias Ringwald } 8266f5875de5SMatthias Ringwald } 8267f5875de5SMatthias Ringwald 8268496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 8269496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 8270496bb884SMatthias Ringwald } 8271496bb884SMatthias Ringwald 82721dc973e7SMatthias Ringwald void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window){ 8273c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_interval = inquiry_scan_interval; 8274c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_window = inquiry_scan_window; 8275baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 8276c0c8a829SMatthias Ringwald hci_run(); 8277c0c8a829SMatthias Ringwald } 8278c0c8a829SMatthias Ringwald 8279b7f1ee76SMatthias Ringwald 8280b7f1ee76SMatthias Ringwald /** 8281b7f1ee76SMatthias Ringwald * @brief Remote Name Request 8282b7f1ee76SMatthias Ringwald * @param addr 8283b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 8284b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 8285b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 8286b7f1ee76SMatthias Ringwald */ 8287667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 8288b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 82896535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 8290b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 8291b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 8292b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 8293b7f1ee76SMatthias Ringwald hci_run(); 8294b7f1ee76SMatthias Ringwald return 0; 8295b7f1ee76SMatthias Ringwald } 8296b7f1ee76SMatthias Ringwald 8297667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 82980a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 82996535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 83000a51f88bSMatthias Ringwald hci_run(); 83010a51f88bSMatthias Ringwald return 0; 83020a51f88bSMatthias Ringwald } 83030a51f88bSMatthias Ringwald 83040a51f88bSMatthias Ringwald /** 8305aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 8306aad97216SMatthias Ringwald * @param addr 8307aad97216SMatthias Ringwald * @param pin_data 8308aad97216SMatthias Ringwald * @param pin_len 8309aad97216SMatthias Ringwald * @return 0 if ok 8310aad97216SMatthias Ringwald */ 8311667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 8312aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8313aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 8314aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 8315aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 8316aad97216SMatthias Ringwald } 8317aad97216SMatthias Ringwald 8318aad97216SMatthias Ringwald /** 83190a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 83200a51f88bSMatthias Ringwald * @param addr 83210a51f88bSMatthias Ringwald * @param pin 83220a51f88bSMatthias Ringwald * @return 0 if ok 83230a51f88bSMatthias Ringwald */ 8324667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 83259d78a7ecSMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, (uint8_t) strlen(pin)); 83260a51f88bSMatthias Ringwald } 83270a51f88bSMatthias Ringwald 83280a51f88bSMatthias Ringwald /** 83290a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 83300a51f88bSMatthias Ringwald * @param addr 83310a51f88bSMatthias Ringwald * @param pin 83320a51f88bSMatthias Ringwald * @return 0 if ok 83330a51f88bSMatthias Ringwald */ 83340a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 8335cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 83360a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 83370a51f88bSMatthias Ringwald } 83380a51f88bSMatthias Ringwald 83390a51f88bSMatthias Ringwald /** 83400a51f88bSMatthias Ringwald * @brief SSP Passkey Response 83410a51f88bSMatthias Ringwald * @param addr 83420a51f88bSMatthias Ringwald * @param passkey 83430a51f88bSMatthias Ringwald * @return 0 if ok 83440a51f88bSMatthias Ringwald */ 8345667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 8346cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8347d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 83480a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 83490a51f88bSMatthias Ringwald } 83500a51f88bSMatthias Ringwald 83510a51f88bSMatthias Ringwald /** 83520a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 83530a51f88bSMatthias Ringwald * @param addr 83540a51f88bSMatthias Ringwald * @param pin 83550a51f88bSMatthias Ringwald * @return 0 if ok 83560a51f88bSMatthias Ringwald */ 8357667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 8358cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 83590a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 83600a51f88bSMatthias Ringwald } 83610a51f88bSMatthias Ringwald 83620a51f88bSMatthias Ringwald /** 83630a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 83640a51f88bSMatthias Ringwald * @param addr 83650a51f88bSMatthias Ringwald * @param passkey 83660a51f88bSMatthias Ringwald * @return 0 if ok 83670a51f88bSMatthias Ringwald */ 8368667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 8369cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 83700a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 83710a51f88bSMatthias Ringwald } 83720a51f88bSMatthias Ringwald 83730a51f88bSMatthias Ringwald /** 83740a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 83750a51f88bSMatthias Ringwald * @param addr 83760a51f88bSMatthias Ringwald * @param pin 83770a51f88bSMatthias Ringwald * @return 0 if ok 83780a51f88bSMatthias Ringwald */ 8379667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 8380cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 83810a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 83820a51f88bSMatthias Ringwald } 83830a51f88bSMatthias Ringwald 8384308eeaffSMatthias Ringwald #if defined(ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY) || defined(ENABLE_EXPLICIT_LINK_KEY_REPLY) 838544565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 838644565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 838744565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 838844565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 838944565b0cSMatthias Ringwald hci_run(); 839044565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 839144565b0cSMatthias Ringwald } 8392308eeaffSMatthias Ringwald #endif 839344565b0cSMatthias Ringwald 8394308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 839544565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 83967ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 839744565b0cSMatthias Ringwald } 839844565b0cSMatthias Ringwald 839944565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 84007ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 840144565b0cSMatthias Ringwald } 840244565b0cSMatthias Ringwald #endif 840344565b0cSMatthias Ringwald 84041849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 84051849becdSMatthias Ringwald /** 84061849becdSMatthias Ringwald * @brief Report Remote OOB Data 84071849becdSMatthias Ringwald * @param bd_addr 84081849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 84091849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 84101849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 84111849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 84121849becdSMatthias Ringwald */ 84131849becdSMatthias 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){ 84141849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 84151849becdSMatthias Ringwald if (connection == NULL) { 84161849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 84171849becdSMatthias Ringwald } 84181849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 84191849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 8420204e8f1dSMatthias Ringwald 8421204e8f1dSMatthias Ringwald // ignore P-256 if not supported by us 8422204e8f1dSMatthias Ringwald if (hci_stack->secure_connections_active){ 84231849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 84241849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 8425204e8f1dSMatthias Ringwald } 8426204e8f1dSMatthias Ringwald 84271849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 84281849becdSMatthias Ringwald } 8429cf01e888SMatthias Ringwald /** 8430cf01e888SMatthias Ringwald * @brief Generate new OOB data 8431cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 8432cf01e888SMatthias Ringwald */ 8433cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 8434cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 8435cf01e888SMatthias Ringwald hci_run(); 8436cf01e888SMatthias Ringwald } 8437cf01e888SMatthias Ringwald 84381849becdSMatthias Ringwald #endif 84391849becdSMatthias Ringwald 8440308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_LINK_KEY_REPLY 8441308eeaffSMatthias Ringwald uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 8442308eeaffSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 8443308eeaffSMatthias Ringwald if (connection == NULL) { 8444308eeaffSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8445308eeaffSMatthias Ringwald } 8446308eeaffSMatthias Ringwald 8447308eeaffSMatthias Ringwald memcpy(connection->link_key, link_key, sizeof(link_key_t)); 8448308eeaffSMatthias Ringwald connection->link_key_type = type; 8449308eeaffSMatthias Ringwald 8450308eeaffSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 8451308eeaffSMatthias Ringwald } 8452308eeaffSMatthias Ringwald 8453308eeaffSMatthias Ringwald #endif // ENABLE_EXPLICIT_LINK_KEY_REPLY 8454f5875de5SMatthias Ringwald /** 8455f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 8456f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 8457f6858d14SMatthias Ringwald */ 8458b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 8459b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 8460f6858d14SMatthias Ringwald } 8461f6858d14SMatthias Ringwald 8462f6858d14SMatthias Ringwald /** 8463d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 8464d950d659SMatthias Ringwald */ 8465d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 8466d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 8467d950d659SMatthias Ringwald } 8468d950d659SMatthias Ringwald 8469d950d659SMatthias Ringwald /** 8470d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 8471d950d659SMatthias Ringwald * @return current voice setting 8472d950d659SMatthias Ringwald */ 84730cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 8474d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 8475d950d659SMatthias Ringwald } 8476d950d659SMatthias Ringwald 8477400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 8478400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 8479400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 8480400ff5abSMatthias Ringwald if (!transport_name) return 0; 8481400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 8482400ff5abSMatthias Ringwald } 8483400ff5abSMatthias Ringwald 8484b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 8485b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 8486b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 8487b3aad8daSMatthias Ringwald */ 848820dcdd22SMatthias Ringwald uint16_t hci_get_sco_packet_length(void){ 848920dcdd22SMatthias Ringwald uint16_t sco_packet_length = 0; 8490cf119f3bSMatthias Ringwald 84911e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 84926f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 84931431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 8494cf119f3bSMatthias Ringwald 8495400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 8496400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 8497cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 849852e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 849952e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 8500400ff5abSMatthias Ringwald } else { 8501400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 8502400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 8503d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 8504ad915196SMatthias Ringwald if (sco_packet_length > (hci_stack->sco_data_packet_length + 3)){ 8505d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 8506d966a453SMatthias Ringwald } 8507400ff5abSMatthias Ringwald } 8508a3069afeSMatthias Ringwald #endif 85095b7087c7SMatthias Ringwald 85105b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 85115b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 85121e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 85135b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 85145b7087c7SMatthias Ringwald #endif 8515a3069afeSMatthias Ringwald return sco_packet_length; 8516b3aad8daSMatthias Ringwald } 8517b3aad8daSMatthias Ringwald 8518c4c88f1bSJakob Krantz /** 8519c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 8520c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 8521c4c88f1bSJakob Krantz */ 8522c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 8523c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 8524c4c88f1bSJakob Krantz } 8525c4c88f1bSJakob Krantz 8526c4c88f1bSJakob Krantz #endif 8527ec111c8bSMatthias Ringwald 8528ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 8529ec111c8bSMatthias Ringwald return hci_stack->state; 8530ec111c8bSMatthias Ringwald } 8531ec111c8bSMatthias Ringwald 853207e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 85335e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 853407e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 853507e010b6SMilanka Ringwald } 853607e010b6SMilanka Ringwald #endif 8537ec111c8bSMatthias Ringwald 8538d950d659SMatthias Ringwald /** 8539d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 8540d23838ecSMatthias Ringwald */ 8541c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 8542d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 8543d23838ecSMatthias Ringwald } 8544d23838ecSMatthias Ringwald 854571de195eSMatthias Ringwald void hci_disconnect_all(void){ 8546665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8547665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 8548665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8549665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 855004a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 855104a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 855204a6ef8cSmatthias.ringwald } 8553d31fba26S[email protected] hci_run(); 855404a6ef8cSmatthias.ringwald } 855533373e40SMatthias Ringwald 855633373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 855733373e40SMatthias Ringwald return hci_stack->manufacturer; 855833373e40SMatthias Ringwald } 85599c6e867eSMatthias Ringwald 85603e329ddfSandryblack #ifdef ENABLE_BLE 85619c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 85629c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 85639c6e867eSMatthias Ringwald if (!hci_con) return NULL; 85649c6e867eSMatthias Ringwald return &hci_con->sm_connection; 85659c6e867eSMatthias Ringwald } 85669c6e867eSMatthias Ringwald 85679c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 85689c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 85696a79f6baSMatthias Ringwald #endif 85709c6e867eSMatthias Ringwald 85716d105c71SMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 8572c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8573c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 8574c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 85756a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 8576c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 8577c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 85789c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 85799c6e867eSMatthias Ringwald } 85806a79f6baSMatthias Ringwald #endif 85816a79f6baSMatthias Ringwald } else { 8582c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 85838daf94bcSMatthias Ringwald if ((hci_connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED)){ 8584c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 8585c5fb5ca4SMatthias Ringwald } 8586c5fb5ca4SMatthias Ringwald #endif 85876a79f6baSMatthias Ringwald } 8588c5fb5ca4SMatthias Ringwald return 0; 8589c5fb5ca4SMatthias Ringwald } 85909c6e867eSMatthias Ringwald 8591ff3f1026SMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 8592c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8593ff3f1026SMatthias Ringwald if (hci_connection == NULL) return false; 85945f3981bfSMatthias Ringwald 8595c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 85966a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 85975f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 85985f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8599c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 8600ff3f1026SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated != 0; 86016a79f6baSMatthias Ringwald #endif 860259a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 86035f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 8604f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8605ff3f1026SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 860659a1a47aSMatthias Ringwald #endif 86075f3981bfSMatthias Ringwald default: 8608ff3f1026SMatthias Ringwald return false; 86095f3981bfSMatthias Ringwald } 86109c6e867eSMatthias Ringwald } 86119c6e867eSMatthias Ringwald 8612f461b3dfSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 8613c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8614c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 86158b35e16aSMatthias Ringwald 8616c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 86176a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 86188b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 86198b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8620f461b3dfSMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return false; // unencrypted connection cannot be authenticated 8621f461b3dfSMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc != 0; 86226a79f6baSMatthias Ringwald #endif 862359a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 86248b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 8625f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8626f461b3dfSMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 862759a1a47aSMatthias Ringwald #endif 86288b35e16aSMatthias Ringwald default: 8629f461b3dfSMatthias Ringwald return false; 86308b35e16aSMatthias Ringwald } 8631f1dfbe18SMatthias Ringwald } 8632f1dfbe18SMatthias Ringwald 86331e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 86341e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 86351e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 86361e122704SMatthias Ringwald 863748f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 86381e122704SMatthias Ringwald link_key_t link_key; 86391e122704SMatthias Ringwald link_key_type_t link_key_type; 864048f33f37SMatthias Ringwald #endif 86411e122704SMatthias Ringwald switch (hci_connection->address_type){ 86426a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 86431e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 86441e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 86451e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 86466a79f6baSMatthias Ringwald #endif 86471e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 86481e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 86491e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 86501e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 86511e122704SMatthias Ringwald #endif 86521e122704SMatthias Ringwald default: 86531e122704SMatthias Ringwald return false; 86541e122704SMatthias Ringwald } 86551e122704SMatthias Ringwald } 86561e122704SMatthias Ringwald 86576a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 86589c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 86599c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 86609c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 86619c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 86629c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 86639c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 86649c6e867eSMatthias Ringwald } 86659c6e867eSMatthias Ringwald #endif 8666f8ee3071SMatthias Ringwald 8667f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 8668f8ee3071SMatthias 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){ 8669f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 86707f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8671f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 8672f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 8673f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 8674f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 8675f8ee3071SMatthias Ringwald hci_run(); 8676f8ee3071SMatthias Ringwald return 0; 8677f8ee3071SMatthias Ringwald } 8678f8ee3071SMatthias Ringwald 8679f8ee3071SMatthias Ringwald /** 8680f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 8681f8ee3071SMatthias Ringwald * @param con_handle 8682f8ee3071SMatthias Ringwald @ @return 0 if ok 8683f8ee3071SMatthias Ringwald */ 8684f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 8685f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 86867f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8687f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 8688f8ee3071SMatthias Ringwald hci_run(); 8689f8ee3071SMatthias Ringwald return 0; 8690f8ee3071SMatthias Ringwald } 8691bea424a5SMatthias Ringwald 8692140c0557SMatthias 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){ 8693140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 86947f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8695140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 8696140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 8697140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 8698140c0557SMatthias Ringwald hci_run(); 8699dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 8700140c0557SMatthias Ringwald } 8701140c0557SMatthias Ringwald 8702278ff8a9SMatthias 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){ 8703278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 87047f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8705278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 8706278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 8707278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 8708278ff8a9SMatthias Ringwald conn->qos_latency = latency; 8709278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 8710278ff8a9SMatthias Ringwald hci_run(); 8711278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8712278ff8a9SMatthias Ringwald } 8713278ff8a9SMatthias Ringwald 8714bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 8715bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 8716bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 8717baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 8718bea424a5SMatthias Ringwald hci_run(); 8719bea424a5SMatthias Ringwald } 8720bea424a5SMatthias Ringwald 8721bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 8722bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 8723baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_TYPE; 8724bea424a5SMatthias Ringwald hci_run(); 8725bea424a5SMatthias Ringwald } 8726bea424a5SMatthias Ringwald 872732a12730SMatthias Ringwald void gap_set_page_timeout(uint16_t page_timeout){ 872832a12730SMatthias Ringwald hci_stack->page_timeout = page_timeout; 8729baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_TIMEOUT; 873032a12730SMatthias Ringwald hci_run(); 873132a12730SMatthias Ringwald } 873232a12730SMatthias Ringwald 8733f8ee3071SMatthias Ringwald #endif 8734beceeddeSMatthias Ringwald 873521debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 873621debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 873721debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 873821debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 873921debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 874021debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 874102b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 874221debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 874302b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 874402b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 874502b02cffSMatthias Ringwald } 874602b02cffSMatthias Ringwald } 874702b02cffSMatthias Ringwald 874802b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 874902b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 875002b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 875102b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 875202b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 875302b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 875402b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 875502b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 875621debf25SMatthias Ringwald } 875721debf25SMatthias Ringwald } 8758cf38a091SMatthias Ringwald 8759cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 87601ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE) == false){ 8761cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 8762cf38a091SMatthias Ringwald } 8763cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 8764cf38a091SMatthias Ringwald // restart le resolving list update 8765cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 8766cf38a091SMatthias Ringwald } 8767cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 8768cf38a091SMatthias Ringwald } 876921debf25SMatthias Ringwald #endif 877021debf25SMatthias Ringwald 8771f40c73b4SMatthias Ringwald #ifdef ENABLE_BLE 8772f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 8773f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 8774f40c73b4SMatthias Ringwald 8775f40c73b4SMatthias Ringwald static uint8_t hci_periodic_advertiser_list_add(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8776f40c73b4SMatthias Ringwald // check if already in list 8777f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8778f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8779f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 8780f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t *entry = (periodic_advertiser_list_entry_t *) btstack_linked_list_iterator_next(&it); 8781f40c73b4SMatthias Ringwald if (entry->sid != advertising_sid) { 8782f40c73b4SMatthias Ringwald continue; 8783f40c73b4SMatthias Ringwald } 8784f40c73b4SMatthias Ringwald if (entry->address_type != address_type) { 8785f40c73b4SMatthias Ringwald continue; 8786f40c73b4SMatthias Ringwald } 8787f40c73b4SMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 8788f40c73b4SMatthias Ringwald continue; 8789f40c73b4SMatthias Ringwald } 8790f40c73b4SMatthias Ringwald // disallow if already scheduled to add 8791f40c73b4SMatthias Ringwald if ((entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER) != 0){ 8792f40c73b4SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 8793f40c73b4SMatthias Ringwald } 8794f40c73b4SMatthias Ringwald // still on controller, but scheduled to remove -> re-add 8795f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 8796f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8797f40c73b4SMatthias Ringwald } 8798f40c73b4SMatthias Ringwald // alloc and add to list 8799f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = btstack_memory_periodic_advertiser_list_entry_get(); 8800f40c73b4SMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 8801f40c73b4SMatthias Ringwald entry->sid = advertising_sid; 8802f40c73b4SMatthias Ringwald entry->address_type = address_type; 8803f40c73b4SMatthias Ringwald (void)memcpy(entry->address, address, 6); 8804f40c73b4SMatthias Ringwald entry->state = LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 8805f40c73b4SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t*) entry); 8806f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8807f40c73b4SMatthias Ringwald } 8808f40c73b4SMatthias Ringwald 8809f40c73b4SMatthias Ringwald static uint8_t hci_periodic_advertiser_list_remove(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8810f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8811f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8812f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8813f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&it); 8814f40c73b4SMatthias Ringwald if (entry->sid != advertising_sid) { 8815f40c73b4SMatthias Ringwald continue; 8816f40c73b4SMatthias Ringwald } 8817f40c73b4SMatthias Ringwald if (entry->address_type != address_type) { 8818f40c73b4SMatthias Ringwald continue; 8819f40c73b4SMatthias Ringwald } 8820f40c73b4SMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 8821f40c73b4SMatthias Ringwald continue; 8822f40c73b4SMatthias Ringwald } 8823f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER){ 8824f40c73b4SMatthias Ringwald // remove from controller if already present 8825f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 8826f40c73b4SMatthias Ringwald } else { 8827f40c73b4SMatthias Ringwald // directly remove entry from whitelist 8828f40c73b4SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8829f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 8830f40c73b4SMatthias Ringwald } 8831f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8832f40c73b4SMatthias Ringwald } 8833f40c73b4SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8834f40c73b4SMatthias Ringwald } 8835f40c73b4SMatthias Ringwald 8836f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_clear(void){ 8837f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8838f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8839f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8840f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&it); 8841f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER){ 8842f40c73b4SMatthias Ringwald // remove from controller if already present 8843f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 8844f40c73b4SMatthias Ringwald continue; 8845f40c73b4SMatthias Ringwald } 8846f40c73b4SMatthias Ringwald // directly remove entry from whitelist 8847f40c73b4SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8848f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 8849f40c73b4SMatthias Ringwald } 8850f40c73b4SMatthias Ringwald } 8851f40c73b4SMatthias Ringwald 8852f40c73b4SMatthias Ringwald // free all entries unconditionally 8853f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_free(void){ 8854f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t lit; 8855f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 8856f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 8857f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 8858f40c73b4SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t *) entry); 8859f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 8860f40c73b4SMatthias Ringwald } 8861f40c73b4SMatthias Ringwald } 8862f40c73b4SMatthias Ringwald 8863f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_clear(void){ 8864f40c73b4SMatthias Ringwald hci_periodic_advertiser_list_clear(); 8865f40c73b4SMatthias Ringwald hci_run(); 8866f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8867f40c73b4SMatthias Ringwald } 8868f40c73b4SMatthias Ringwald 8869f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_add(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8870f40c73b4SMatthias Ringwald uint8_t status = hci_periodic_advertiser_list_add(address_type, address, advertising_sid); 8871f40c73b4SMatthias Ringwald if (status){ 8872f40c73b4SMatthias Ringwald return status; 8873f40c73b4SMatthias Ringwald } 8874f40c73b4SMatthias Ringwald hci_run(); 8875f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8876f40c73b4SMatthias Ringwald } 8877f40c73b4SMatthias Ringwald 8878f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_remove(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8879f40c73b4SMatthias Ringwald uint8_t status = hci_periodic_advertiser_list_remove(address_type, address, advertising_sid); 8880f40c73b4SMatthias Ringwald if (status){ 8881f40c73b4SMatthias Ringwald return status; 8882f40c73b4SMatthias Ringwald } 8883f40c73b4SMatthias Ringwald hci_run(); 8884f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8885f40c73b4SMatthias Ringwald } 88864bafedd5SMatthias Ringwald 88874bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_create_sync(uint8_t options, uint8_t advertising_sid, bd_addr_type_t advertiser_address_type, 88884bafedd5SMatthias Ringwald bd_addr_t advertiser_address, uint16_t skip, uint16_t sync_timeout, uint8_t sync_cte_type){ 88894bafedd5SMatthias Ringwald // abort if already active 88904bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_request != LE_CONNECTING_IDLE) { 88914bafedd5SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 88924bafedd5SMatthias Ringwald } 88934bafedd5SMatthias Ringwald // store request 88944bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_request = ((options & 0) != 0) ? LE_CONNECTING_WHITELIST : LE_CONNECTING_DIRECT; 88954bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_options = options; 88964bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertising_sid = advertising_sid; 88974bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address_type = advertiser_address_type; 88984bafedd5SMatthias Ringwald memcpy(hci_stack->le_periodic_sync_advertiser_address, advertiser_address, 6); 88994bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_skip = skip; 89004bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_timeout = sync_timeout; 89014bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_cte_type = sync_cte_type; 89024bafedd5SMatthias Ringwald 89034bafedd5SMatthias Ringwald hci_run(); 89044bafedd5SMatthias Ringwald return ERROR_CODE_SUCCESS; 89054bafedd5SMatthias Ringwald } 89064bafedd5SMatthias Ringwald 89074bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_create_sync_cancel(void){ 89084bafedd5SMatthias Ringwald // abort if not requested 89094bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_request == LE_CONNECTING_IDLE) { 89104bafedd5SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 89114bafedd5SMatthias Ringwald } 89124bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_request = LE_CONNECTING_IDLE; 89134bafedd5SMatthias Ringwald hci_run(); 89144bafedd5SMatthias Ringwald return ERROR_CODE_SUCCESS; 89154bafedd5SMatthias Ringwald } 89164bafedd5SMatthias Ringwald 89174bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_terminate_sync(uint16_t sync_handle){ 891849a6c69cSMatthias Ringwald if (hci_stack->le_periodic_terminate_sync_handle != HCI_CON_HANDLE_INVALID){ 891949a6c69cSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 892049a6c69cSMatthias Ringwald } 892149a6c69cSMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = sync_handle; 892249a6c69cSMatthias Ringwald hci_run(); 892349a6c69cSMatthias Ringwald return ERROR_CODE_SUCCESS; 89244bafedd5SMatthias Ringwald } 89254bafedd5SMatthias Ringwald 8926f40c73b4SMatthias Ringwald #endif 8927f40c73b4SMatthias Ringwald #endif 89284ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 89298d72db10SMatthias Ringwald static uint8_t hci_iso_stream_create(hci_iso_type_t iso_type, hci_con_handle_t con_handle, uint8_t group_id) { 89304ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = btstack_memory_hci_iso_stream_get(); 89314ae8623eSMatthias Ringwald if (iso_stream == NULL){ 89324ae8623eSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 89334ae8623eSMatthias Ringwald } else { 89348d72db10SMatthias Ringwald iso_stream->iso_type = iso_type; 89354ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_REQUESTED; 893615a15be3SMatthias Ringwald iso_stream->con_handle = con_handle; 89378d72db10SMatthias Ringwald iso_stream->group_id = group_id; 89384ae8623eSMatthias Ringwald btstack_linked_list_add(&hci_stack->iso_streams, (btstack_linked_item_t*) iso_stream); 89394ae8623eSMatthias Ringwald return ERROR_CODE_SUCCESS; 89404ae8623eSMatthias Ringwald } 89414ae8623eSMatthias Ringwald } 89424ae8623eSMatthias Ringwald 894315a15be3SMatthias Ringwald static hci_iso_stream_t * hci_iso_stream_for_con_handle(hci_con_handle_t con_handle){ 89444ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 89454ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 89464ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 89474ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 894815a15be3SMatthias Ringwald if (iso_stream->con_handle == con_handle ) { 89494ae8623eSMatthias Ringwald return iso_stream; 89504ae8623eSMatthias Ringwald } 89514ae8623eSMatthias Ringwald } 89524ae8623eSMatthias Ringwald return NULL; 89534ae8623eSMatthias Ringwald } 89544ae8623eSMatthias Ringwald 89554ae8623eSMatthias Ringwald static void hci_iso_stream_finalize(hci_iso_stream_t * iso_stream){ 89568d72db10SMatthias Ringwald log_info("hci_iso_stream_finalize con_handle 0x%04x, group_id 0x%02x", iso_stream->con_handle, iso_stream->group_id); 89574ae8623eSMatthias Ringwald btstack_linked_list_remove(&hci_stack->iso_streams, (btstack_linked_item_t*) iso_stream); 89584ae8623eSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 89594ae8623eSMatthias Ringwald } 89604ae8623eSMatthias Ringwald 8961*778463d0SMatthias Ringwald static void hci_iso_stream_finalize_by_type_and_group_id(hci_iso_type_t iso_type, uint8_t group_id) { 8962*778463d0SMatthias Ringwald btstack_linked_list_iterator_t it; 8963*778463d0SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 8964*778463d0SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8965*778463d0SMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 8966*778463d0SMatthias Ringwald if ((iso_stream->group_id == group_id) && 8967*778463d0SMatthias Ringwald (iso_stream->iso_type == iso_type)){ 8968*778463d0SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8969*778463d0SMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 8970*778463d0SMatthias Ringwald } 8971*778463d0SMatthias Ringwald } 8972*778463d0SMatthias Ringwald } 8973*778463d0SMatthias Ringwald 89748d72db10SMatthias Ringwald static void hci_iso_stream_requested_finalize(uint8_t group_id) { 89754ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 89764ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 89774ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 89784ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 8979a90016deSMatthias Ringwald if ((iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) && 89808d72db10SMatthias Ringwald (iso_stream->group_id == group_id)){ 89814ae8623eSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 89824ae8623eSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 89834ae8623eSMatthias Ringwald } 89844ae8623eSMatthias Ringwald } 89854ae8623eSMatthias Ringwald } 8986a90016deSMatthias Ringwald static void hci_iso_stream_requested_confirm(uint8_t big_handle){ 89874ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 89884ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 89894ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 89904ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 89914ae8623eSMatthias Ringwald if ( iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) { 89924ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_W4_ESTABLISHED; 89934ae8623eSMatthias Ringwald } 89944ae8623eSMatthias Ringwald } 89954ae8623eSMatthias Ringwald } 8996d39e4f95SMatthias Ringwald 8997d39e4f95SMatthias Ringwald static bool hci_iso_sdu_complete(uint8_t * packet, uint16_t size){ 8998d39e4f95SMatthias Ringwald uint8_t sdu_ts_flag = (packet[1] >> 6) & 1; 8999d39e4f95SMatthias Ringwald uint16_t sdu_len_offset = 6 + (sdu_ts_flag * 4); 9000d39e4f95SMatthias Ringwald uint16_t sdu_len = little_endian_read_16(packet, sdu_len_offset) & 0x0fff; 9001d39e4f95SMatthias Ringwald return (sdu_len_offset + 2 + sdu_len) == size; 9002d39e4f95SMatthias Ringwald } 9003d39e4f95SMatthias Ringwald 9004d39e4f95SMatthias Ringwald static void hci_iso_packet_handler(uint8_t * packet, uint16_t size){ 9005d39e4f95SMatthias Ringwald if (hci_stack->iso_packet_handler == NULL) { 9006d39e4f95SMatthias Ringwald return; 9007d39e4f95SMatthias Ringwald } 9008d39e4f95SMatthias Ringwald if (size < 4) { 9009d39e4f95SMatthias Ringwald return; 9010d39e4f95SMatthias Ringwald } 9011d39e4f95SMatthias Ringwald 9012d39e4f95SMatthias Ringwald // parse header 9013d39e4f95SMatthias Ringwald uint16_t conn_handle_and_flags = little_endian_read_16(packet, 0); 9014d39e4f95SMatthias Ringwald uint16_t iso_data_len = little_endian_read_16(packet, 2); 9015d39e4f95SMatthias Ringwald hci_con_handle_t cis_handle = (hci_con_handle_t) (conn_handle_and_flags & 0xfff); 901615a15be3SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(cis_handle); 9017d39e4f95SMatthias Ringwald uint8_t pb_flag = (conn_handle_and_flags >> 12) & 3; 9018d39e4f95SMatthias Ringwald 9019d39e4f95SMatthias Ringwald // assert packet is complete 9020d39e4f95SMatthias Ringwald if ((iso_data_len + 4u) != size){ 9021d39e4f95SMatthias Ringwald return; 9022d39e4f95SMatthias Ringwald } 9023d39e4f95SMatthias Ringwald 9024d39e4f95SMatthias Ringwald if ((pb_flag & 0x01) == 0){ 9025d39e4f95SMatthias Ringwald if (pb_flag == 0x02){ 9026d39e4f95SMatthias Ringwald // The ISO_Data_Load field contains a header and a complete SDU. 9027d39e4f95SMatthias Ringwald if (hci_iso_sdu_complete(packet, size)) { 9028d39e4f95SMatthias Ringwald (hci_stack->iso_packet_handler)(HCI_ISO_DATA_PACKET, 0, packet, size); 9029d39e4f95SMatthias Ringwald } 9030d39e4f95SMatthias Ringwald } else { 9031d39e4f95SMatthias Ringwald // The ISO_Data_Load field contains a header and the first fragment of a fragmented SDU. 9032d39e4f95SMatthias Ringwald if (iso_stream == NULL){ 9033d39e4f95SMatthias Ringwald return; 9034d39e4f95SMatthias Ringwald } 9035d39e4f95SMatthias Ringwald if (size > HCI_ISO_PAYLOAD_SIZE){ 9036d39e4f95SMatthias Ringwald return; 9037d39e4f95SMatthias Ringwald } 9038d39e4f95SMatthias Ringwald memcpy(iso_stream->reassembly_buffer, packet, size); 9039d39e4f95SMatthias Ringwald // fix pb_flag 9040d39e4f95SMatthias Ringwald iso_stream->reassembly_buffer[1] = (iso_stream->reassembly_buffer[1] & 0xcf) | 0x20; 9041d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = size; 9042d39e4f95SMatthias Ringwald } 9043d39e4f95SMatthias Ringwald } else { 9044d39e4f95SMatthias Ringwald // iso_data_load contains continuation or last fragment of an SDU 9045d39e4f95SMatthias Ringwald uint8_t ts_flag = (conn_handle_and_flags >> 14) & 1; 9046d39e4f95SMatthias Ringwald if (ts_flag != 0){ 9047d39e4f95SMatthias Ringwald return; 9048d39e4f95SMatthias Ringwald } 9049d39e4f95SMatthias Ringwald // append fragment 9050d39e4f95SMatthias Ringwald if (iso_stream == NULL){ 9051d39e4f95SMatthias Ringwald return; 9052d39e4f95SMatthias Ringwald } 9053d39e4f95SMatthias Ringwald if (iso_stream->reassembly_pos == 0){ 9054d39e4f95SMatthias Ringwald return; 9055d39e4f95SMatthias Ringwald } 9056d39e4f95SMatthias Ringwald if ((iso_stream->reassembly_pos + iso_data_len) > size){ 9057d39e4f95SMatthias Ringwald // reset reassembly buffer 9058d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = 0; 9059d39e4f95SMatthias Ringwald return; 9060d39e4f95SMatthias Ringwald } 9061d39e4f95SMatthias Ringwald memcpy(&iso_stream->reassembly_buffer[iso_stream->reassembly_pos], &packet[4], iso_data_len); 9062d39e4f95SMatthias Ringwald iso_stream->reassembly_pos += iso_data_len; 9063d39e4f95SMatthias Ringwald 9064d39e4f95SMatthias Ringwald // deliver if last fragment and SDU complete 9065d39e4f95SMatthias Ringwald if (pb_flag == 0x03){ 9066d39e4f95SMatthias Ringwald if (hci_iso_sdu_complete(iso_stream->reassembly_buffer, iso_stream->reassembly_pos)){ 9067d39e4f95SMatthias Ringwald (hci_stack->iso_packet_handler)(HCI_ISO_DATA_PACKET, 0, iso_stream->reassembly_buffer, iso_stream->reassembly_pos); 9068d39e4f95SMatthias Ringwald } 9069d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = 0; 9070d39e4f95SMatthias Ringwald } 9071d39e4f95SMatthias Ringwald } 9072d39e4f95SMatthias Ringwald } 9073d39e4f95SMatthias Ringwald 90747aa35f6aSMatthias Ringwald static void hci_emit_big_created(const le_audio_big_t * big, uint8_t status){ 90757aa35f6aSMatthias Ringwald uint8_t event [6 + (MAX_NR_BIS * 2)]; 90767aa35f6aSMatthias Ringwald uint16_t pos = 0; 90777aa35f6aSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 90787aa35f6aSMatthias Ringwald event[pos++] = 4 + (2 * big->num_bis); 90797aa35f6aSMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_CREATED; 90807aa35f6aSMatthias Ringwald event[pos++] = status; 90817aa35f6aSMatthias Ringwald event[pos++] = big->big_handle; 90827aa35f6aSMatthias Ringwald event[pos++] = big->num_bis; 90837aa35f6aSMatthias Ringwald uint8_t i; 90847aa35f6aSMatthias Ringwald for (i=0;i<big->num_bis;i++){ 90857aa35f6aSMatthias Ringwald little_endian_store_16(event, pos, big->bis_con_handles[i]); 90867aa35f6aSMatthias Ringwald pos += 2; 90877aa35f6aSMatthias Ringwald } 90887aa35f6aSMatthias Ringwald hci_emit_event(event, pos, 0); 90897aa35f6aSMatthias Ringwald } 90907aa35f6aSMatthias Ringwald 90917aa35f6aSMatthias Ringwald static void hci_emit_big_terminated(const le_audio_big_t * big){ 90927aa35f6aSMatthias Ringwald uint8_t event [4]; 90937aa35f6aSMatthias Ringwald uint16_t pos = 0; 90947aa35f6aSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 90957aa35f6aSMatthias Ringwald event[pos++] = 2; 90967aa35f6aSMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_TERMINATED; 90977aa35f6aSMatthias Ringwald event[pos++] = big->big_handle; 90987aa35f6aSMatthias Ringwald hci_emit_event(event, pos, 0); 90997aa35f6aSMatthias Ringwald } 91007aa35f6aSMatthias Ringwald 91012a6c0f82SMatthias Ringwald static void hci_emit_big_sync_created(const le_audio_big_sync_t * big_sync, uint8_t status){ 91022a6c0f82SMatthias Ringwald uint8_t event [6 + (MAX_NR_BIS * 2)]; 91032a6c0f82SMatthias Ringwald uint16_t pos = 0; 91042a6c0f82SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 91052a6c0f82SMatthias Ringwald event[pos++] = 4; 91062a6c0f82SMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_SYNC_CREATED; 91072a6c0f82SMatthias Ringwald event[pos++] = status; 91082a6c0f82SMatthias Ringwald event[pos++] = big_sync->big_handle; 91092a6c0f82SMatthias Ringwald event[pos++] = big_sync->num_bis; 91102a6c0f82SMatthias Ringwald uint8_t i; 91112a6c0f82SMatthias Ringwald for (i=0;i<big_sync->num_bis;i++){ 91122a6c0f82SMatthias Ringwald little_endian_store_16(event, pos, big_sync->bis_con_handles[i]); 91132a6c0f82SMatthias Ringwald pos += 2; 91142a6c0f82SMatthias Ringwald } 91152a6c0f82SMatthias Ringwald hci_emit_event(event, pos, 0); 91162a6c0f82SMatthias Ringwald } 91172a6c0f82SMatthias Ringwald 9118d04a9afcSMatthias Ringwald static void hci_emit_big_sync_stopped(uint8_t big_handle){ 91192a6c0f82SMatthias Ringwald uint8_t event [4]; 91202a6c0f82SMatthias Ringwald uint16_t pos = 0; 91212a6c0f82SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 91222a6c0f82SMatthias Ringwald event[pos++] = 2; 91232a6c0f82SMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_SYNC_STOPPED; 9124d04a9afcSMatthias Ringwald event[pos++] = big_handle; 91252a6c0f82SMatthias Ringwald hci_emit_event(event, pos, 0); 91262a6c0f82SMatthias Ringwald } 91272a6c0f82SMatthias Ringwald 91285ade6657SMatthias Ringwald static void hci_emit_bis_can_send_now(const le_audio_big_t *big, uint8_t bis_index) { 91295ade6657SMatthias Ringwald uint8_t event[6]; 91305ade6657SMatthias Ringwald uint16_t pos = 0; 91315ade6657SMatthias Ringwald event[pos++] = HCI_EVENT_BIS_CAN_SEND_NOW; 91325ade6657SMatthias Ringwald event[pos++] = sizeof(event) - 2; 91335ade6657SMatthias Ringwald event[pos++] = big->big_handle; 91345ade6657SMatthias Ringwald event[pos++] = bis_index; 91355ade6657SMatthias Ringwald little_endian_store_16(event, pos, big->bis_con_handles[bis_index]); 91365ade6657SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 91375ade6657SMatthias Ringwald } 91385ade6657SMatthias Ringwald 91397aa35f6aSMatthias Ringwald static le_audio_big_t * hci_big_for_handle(uint8_t big_handle){ 91407aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 91417aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 91427aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91437aa35f6aSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 91447aa35f6aSMatthias Ringwald if ( big->big_handle == big_handle ) { 91457aa35f6aSMatthias Ringwald return big; 91467aa35f6aSMatthias Ringwald } 91477aa35f6aSMatthias Ringwald } 91487aa35f6aSMatthias Ringwald return NULL; 91497aa35f6aSMatthias Ringwald } 91507aa35f6aSMatthias Ringwald 91512a6c0f82SMatthias Ringwald static le_audio_big_sync_t * hci_big_sync_for_handle(uint8_t big_handle){ 91522a6c0f82SMatthias Ringwald btstack_linked_list_iterator_t it; 91532a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 91542a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91552a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 91562a6c0f82SMatthias Ringwald if ( big_sync->big_handle == big_handle ) { 91572a6c0f82SMatthias Ringwald return big_sync; 91582a6c0f82SMatthias Ringwald } 91592a6c0f82SMatthias Ringwald } 91602a6c0f82SMatthias Ringwald return NULL; 91612a6c0f82SMatthias Ringwald } 91622a6c0f82SMatthias Ringwald 916301d4f05fSMatthias Ringwald void hci_set_num_iso_packets_to_queue(uint8_t num_packets){ 916401d4f05fSMatthias Ringwald hci_stack->iso_packets_to_queue = num_packets; 916501d4f05fSMatthias Ringwald } 916601d4f05fSMatthias Ringwald 91675ade6657SMatthias Ringwald static void hci_iso_notify_can_send_now(void){ 91685ade6657SMatthias Ringwald btstack_linked_list_iterator_t it; 91695ade6657SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 91705ade6657SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91715ade6657SMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 9172aacb1081SMatthias Ringwald // track number completed packet timestamps 9173aacb1081SMatthias Ringwald if (big->num_completed_timestamp_current_valid){ 9174aacb1081SMatthias Ringwald big->num_completed_timestamp_current_valid = false; 9175aacb1081SMatthias Ringwald if (big->num_completed_timestamp_previous_valid){ 9176aacb1081SMatthias Ringwald // detect delayed sending of all BIS: tolerate up to 50% delayed event handling 9177aacb1081SMatthias Ringwald uint32_t iso_interval_missed_threshold_ms = big->params->sdu_interval_us * 3 / 2000; 9178aacb1081SMatthias Ringwald int32_t num_completed_timestamp_delta_ms = btstack_time_delta(big->num_completed_timestamp_current_ms, 9179aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_ms); 9180aacb1081SMatthias Ringwald if (num_completed_timestamp_delta_ms > iso_interval_missed_threshold_ms){ 9181aacb1081SMatthias Ringwald // to catch up, skip packet on all BIS 9182aacb1081SMatthias Ringwald uint8_t i; 9183aacb1081SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 9184aacb1081SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 9185aacb1081SMatthias Ringwald if (iso_stream){ 9186aacb1081SMatthias Ringwald iso_stream->num_packets_to_skip++; 9187aacb1081SMatthias Ringwald } 9188aacb1081SMatthias Ringwald } 9189aacb1081SMatthias Ringwald } 9190aacb1081SMatthias Ringwald } 9191aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_valid = true; 9192aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_ms = big->num_completed_timestamp_current_ms; 9193aacb1081SMatthias Ringwald } 9194aacb1081SMatthias Ringwald 91955ade6657SMatthias Ringwald if (big->can_send_now_requested){ 9196d4e5d4faSMatthias Ringwald // check if no outgoing iso packets pending and no can send now have to be emitted 91975ade6657SMatthias Ringwald uint8_t i; 91985ade6657SMatthias Ringwald bool can_send = true; 9199aacb1081SMatthias Ringwald uint8_t num_iso_queued_minimum = 0; 92005ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 92015ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 9202aacb1081SMatthias Ringwald if (iso_stream == NULL) continue; 9203aacb1081SMatthias Ringwald // handle case where individual ISO packet was sent too late: 9204aacb1081SMatthias Ringwald // for each additionally queued packet, a new one needs to get skipped 9205aacb1081SMatthias Ringwald if (i==0){ 9206aacb1081SMatthias Ringwald num_iso_queued_minimum = iso_stream->num_packets_sent; 9207aacb1081SMatthias Ringwald } else if (iso_stream->num_packets_sent > num_iso_queued_minimum){ 9208aacb1081SMatthias Ringwald uint8_t num_packets_to_skip = iso_stream->num_packets_sent - num_iso_queued_minimum; 9209aacb1081SMatthias Ringwald iso_stream->num_packets_to_skip += num_packets_to_skip; 9210aacb1081SMatthias Ringwald iso_stream->num_packets_sent -= num_packets_to_skip; 9211aacb1081SMatthias Ringwald } 9212aacb1081SMatthias Ringwald // check if we can send now 9213aacb1081SMatthias Ringwald if ((iso_stream->num_packets_sent >= hci_stack->iso_packets_to_queue) || (iso_stream->emit_ready_to_send)){ 92145ade6657SMatthias Ringwald can_send = false; 92155ade6657SMatthias Ringwald break; 92165ade6657SMatthias Ringwald } 92175ade6657SMatthias Ringwald } 92185ade6657SMatthias Ringwald if (can_send){ 92195ade6657SMatthias Ringwald // propagate can send now to individual streams 92205ade6657SMatthias Ringwald big->can_send_now_requested = false; 92215ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 92225ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 92235ade6657SMatthias Ringwald iso_stream->emit_ready_to_send = true; 92245ade6657SMatthias Ringwald } 92255ade6657SMatthias Ringwald } 92265ade6657SMatthias Ringwald } 92275ade6657SMatthias Ringwald } 92285ade6657SMatthias Ringwald 92295ade6657SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return; 92305ade6657SMatthias Ringwald 92315ade6657SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 92325ade6657SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 92335ade6657SMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 92345ade6657SMatthias Ringwald // report bis ready 92355ade6657SMatthias Ringwald uint8_t i; 92365ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 92375ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 92385ade6657SMatthias Ringwald if ((iso_stream != NULL) && iso_stream->emit_ready_to_send){ 92395ade6657SMatthias Ringwald iso_stream->emit_ready_to_send = false; 92405ade6657SMatthias Ringwald hci_emit_bis_can_send_now(big, i); 92415ade6657SMatthias Ringwald break; 92425ade6657SMatthias Ringwald } 92435ade6657SMatthias Ringwald } 92445ade6657SMatthias Ringwald } 92455ade6657SMatthias Ringwald } 92465ade6657SMatthias Ringwald 92477aa35f6aSMatthias Ringwald uint8_t gap_big_create(le_audio_big_t * storage, le_audio_big_params_t * big_params){ 92487aa35f6aSMatthias Ringwald if (hci_big_for_handle(big_params->big_handle) != NULL){ 92497aa35f6aSMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 92507aa35f6aSMatthias Ringwald } 92517aa35f6aSMatthias Ringwald if (big_params->num_bis == 0){ 92527aa35f6aSMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 92537aa35f6aSMatthias Ringwald } 92547aa35f6aSMatthias Ringwald if (big_params->num_bis > MAX_NR_BIS){ 92557aa35f6aSMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 92567aa35f6aSMatthias Ringwald } 92577aa35f6aSMatthias Ringwald 9258fb16346aSMatthias Ringwald // reserve ISO Streams 9259fb16346aSMatthias Ringwald uint8_t i; 9260fb16346aSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 9261fb16346aSMatthias Ringwald for (i=0;i<big_params->num_bis;i++){ 92628d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, HCI_CON_HANDLE_INVALID, big_params->big_handle); 9263fb16346aSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 9264fb16346aSMatthias Ringwald break; 9265fb16346aSMatthias Ringwald } 9266fb16346aSMatthias Ringwald } 9267fb16346aSMatthias Ringwald 9268fb16346aSMatthias Ringwald // free structs on error 9269fb16346aSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 9270*778463d0SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_BIS, big_params->big_handle); 9271fb16346aSMatthias Ringwald return status; 9272fb16346aSMatthias Ringwald } 9273fb16346aSMatthias Ringwald 92747aa35f6aSMatthias Ringwald le_audio_big_t * big = storage; 92757aa35f6aSMatthias Ringwald big->big_handle = big_params->big_handle; 92767aa35f6aSMatthias Ringwald big->params = big_params; 92777aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_CREATE; 92787aa35f6aSMatthias Ringwald big->num_bis = big_params->num_bis; 92797aa35f6aSMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 92807aa35f6aSMatthias Ringwald 92817aa35f6aSMatthias Ringwald hci_run(); 92827aa35f6aSMatthias Ringwald 92837aa35f6aSMatthias Ringwald return ERROR_CODE_SUCCESS; 92847aa35f6aSMatthias Ringwald } 92857aa35f6aSMatthias Ringwald 92862a6c0f82SMatthias Ringwald uint8_t gap_big_sync_create(le_audio_big_sync_t * storage, le_audio_big_sync_params_t * big_sync_params){ 92872a6c0f82SMatthias Ringwald if (hci_big_sync_for_handle(big_sync_params->big_handle) != NULL){ 92882a6c0f82SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 92892a6c0f82SMatthias Ringwald } 92902a6c0f82SMatthias Ringwald if (big_sync_params->num_bis == 0){ 92912a6c0f82SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 92922a6c0f82SMatthias Ringwald } 92932a6c0f82SMatthias Ringwald if (big_sync_params->num_bis > MAX_NR_BIS){ 92942a6c0f82SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 92952a6c0f82SMatthias Ringwald } 92962a6c0f82SMatthias Ringwald 92972a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = storage; 92982a6c0f82SMatthias Ringwald big_sync->big_handle = big_sync_params->big_handle; 92992a6c0f82SMatthias Ringwald big_sync->params = big_sync_params; 93002a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_CREATE; 93012a6c0f82SMatthias Ringwald big_sync->num_bis = big_sync_params->num_bis; 93022a6c0f82SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 93032a6c0f82SMatthias Ringwald 93042a6c0f82SMatthias Ringwald hci_run(); 93052a6c0f82SMatthias Ringwald 93062a6c0f82SMatthias Ringwald return ERROR_CODE_SUCCESS; 93072a6c0f82SMatthias Ringwald } 93082a6c0f82SMatthias Ringwald 93097aa35f6aSMatthias Ringwald uint8_t gap_big_terminate(uint8_t big_handle){ 93107aa35f6aSMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(big_handle); 93117aa35f6aSMatthias Ringwald if (big == NULL){ 93127aa35f6aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 93137aa35f6aSMatthias Ringwald } 93147aa35f6aSMatthias Ringwald switch (big->state){ 93157aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 93167aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 93172a6c0f82SMatthias Ringwald hci_emit_big_terminated(big); 93182a6c0f82SMatthias Ringwald break; 93192a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH: 93202a6c0f82SMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH_THEN_TERMINATE; 93217aa35f6aSMatthias Ringwald break; 93227aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_ESTABLISHED: 93237aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 93247aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_ACTIVE: 93257aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_TERMINATE; 93267aa35f6aSMatthias Ringwald hci_run(); 93277aa35f6aSMatthias Ringwald break; 93282a6c0f82SMatthias Ringwald default: 93292a6c0f82SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 93302a6c0f82SMatthias Ringwald } 93312a6c0f82SMatthias Ringwald return ERROR_CODE_SUCCESS; 93322a6c0f82SMatthias Ringwald } 93332a6c0f82SMatthias Ringwald 93342a6c0f82SMatthias Ringwald uint8_t gap_big_sync_terminate(uint8_t big_handle){ 93352a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = hci_big_sync_for_handle(big_handle); 93362a6c0f82SMatthias Ringwald if (big_sync == NULL){ 93372a6c0f82SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 93382a6c0f82SMatthias Ringwald } 93392a6c0f82SMatthias Ringwald switch (big_sync->state){ 93402a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 93412a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 9342d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 93432a6c0f82SMatthias Ringwald break; 93447aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH: 93452a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH_THEN_TERMINATE; 93462a6c0f82SMatthias Ringwald break; 93472a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_ESTABLISHED: 93482a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 93492a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_ACTIVE: 93502a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_TERMINATE; 93512a6c0f82SMatthias Ringwald hci_run(); 93527aa35f6aSMatthias Ringwald break; 93537aa35f6aSMatthias Ringwald default: 93547aa35f6aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 93557aa35f6aSMatthias Ringwald } 93567aa35f6aSMatthias Ringwald return ERROR_CODE_SUCCESS; 93577aa35f6aSMatthias Ringwald } 9358d39e4f95SMatthias Ringwald 93595ade6657SMatthias Ringwald uint8_t hci_request_bis_can_send_now_events(uint8_t big_handle){ 93605ade6657SMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(big_handle); 93615ade6657SMatthias Ringwald if (big == NULL){ 93625ade6657SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 93635ade6657SMatthias Ringwald } 93645ade6657SMatthias Ringwald if (big->state != LE_AUDIO_BIG_STATE_ACTIVE){ 93655ade6657SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 93665ade6657SMatthias Ringwald } 93675ade6657SMatthias Ringwald big->can_send_now_requested = true; 93685ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 93695ade6657SMatthias Ringwald return ERROR_CODE_SUCCESS; 93705ade6657SMatthias Ringwald } 9371f40c73b4SMatthias Ringwald #endif 93724ae8623eSMatthias Ringwald #endif /* ENABLE_BLE */ 9373f40c73b4SMatthias Ringwald 9374eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 937518976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 937618976c74SMatthias Ringwald hci_connection_t * conn; 937718976c74SMatthias Ringwald 937818976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 937918976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 938018976c74SMatthias Ringwald 93817d33cb26SMilanka Ringwald // setup Controller info 93827d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 93837d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 93847d33cb26SMilanka Ringwald 938518976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 938618976c74SMatthias Ringwald addr[5] = 0x01; 938718976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 938818976c74SMatthias Ringwald conn->con_handle = addr[5]; 938918976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 939018976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 93917d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 939218976c74SMatthias Ringwald 939318976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 939418976c74SMatthias Ringwald addr[5] = 0x02; 939518976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 939618976c74SMatthias Ringwald conn->con_handle = addr[5]; 939718976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 939818976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 93997d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 940018976c74SMatthias Ringwald 940118976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 940218976c74SMatthias Ringwald addr[5] = 0x03; 940318976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 940418976c74SMatthias Ringwald conn->con_handle = addr[5]; 940518976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 940618976c74SMatthias Ringwald conn->state = OPEN; 94077d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 940818976c74SMatthias Ringwald 940918976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 941018976c74SMatthias Ringwald addr[5] = 0x04; 941118976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 941218976c74SMatthias Ringwald conn->con_handle = addr[5]; 941318976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 941418976c74SMatthias Ringwald conn->state = OPEN; 94157d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 941618976c74SMatthias Ringwald 941718976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 941818976c74SMatthias Ringwald addr[5] = 0x05; 941918976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 942018976c74SMatthias Ringwald conn->con_handle = addr[5]; 942118976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 942218976c74SMatthias Ringwald conn->state = OPEN; 94237d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 94248046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 942518976c74SMatthias Ringwald } 942618976c74SMatthias Ringwald 9427eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 9428eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 9429eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 9430eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9431eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 9432eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9433eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 9434eddac615SMatthias Ringwald } 9435eddac615SMatthias Ringwald } 94361470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 9437b6ffb67fSMatthias Ringwald hci_stack->le_scanning_param_update = false; 94381470db0cSMatthias Ringwald hci_init_done(); 94391470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 94401470db0cSMatthias Ringwald } 9441eddac615SMatthias Ringwald #endif 9442