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); 233778463d0SMatthias 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); 239d3a89d91SMatthias Ringwald static le_audio_cig_t * hci_cig_for_id(uint8_t cig_id); 2405ade6657SMatthias Ringwald static void hci_iso_notify_can_send_now(void); 2417aa35f6aSMatthias Ringwald static void hci_emit_big_created(const le_audio_big_t * big, uint8_t status); 2427aa35f6aSMatthias Ringwald static void hci_emit_big_terminated(const le_audio_big_t * big); 2432a6c0f82SMatthias Ringwald static void hci_emit_big_sync_created(const le_audio_big_sync_t * big_sync, uint8_t status); 244d04a9afcSMatthias Ringwald static void hci_emit_big_sync_stopped(uint8_t big_handle); 245d3a89d91SMatthias Ringwald static void hci_emit_cig_created(const le_audio_cig_t * cig, uint8_t status); 2463abcd16cSMatthias Ringwald static void 2473abcd16cSMatthias Ringwald hci_emit_cis_created(uint8_t cig_id, hci_con_handle_t cis_con_handle, uint8_t status); 2482a6c0f82SMatthias Ringwald static le_audio_big_sync_t * hci_big_sync_for_handle(uint8_t big_handle); 2494ae8623eSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 250ee54aff5SMatthias Ringwald #endif /* ENABLE_BLE */ 251758b46ceSmatthias.ringwald 25206b35ec0Smatthias.ringwald // the STACK is here 2533a9fb326S[email protected] #ifndef HAVE_MALLOC 2543a9fb326S[email protected] static hci_stack_t hci_stack_static; 2553a9fb326S[email protected] #endif 2563a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 25716833f0aSmatthias.ringwald 2581c9e5e9dSMatthias Ringwald #ifdef ENABLE_CLASSIC 25963168530SMatthias Ringwald // default name 26063168530SMatthias Ringwald static const char * default_classic_name = "BTstack 00:00:00:00:00:00"; 26163168530SMatthias Ringwald 26266fb9560S[email protected] // test helper 26366fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 2641c9e5e9dSMatthias Ringwald #endif 26566fb9560S[email protected] 266aee5d5c1SMatthias Ringwald // reset connection state on create and on reconnect 267aee5d5c1SMatthias Ringwald // don't overwrite addr, con handle, role 268aee5d5c1SMatthias Ringwald static void hci_connection_init(hci_connection_t * conn){ 2698daf94bcSMatthias Ringwald conn->authentication_flags = AUTH_FLAG_NONE; 27096a45072S[email protected] conn->bonding_flags = 0; 27196a45072S[email protected] conn->requested_security_level = LEVEL_0; 27252db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 27388a03c8dSMatthias Ringwald conn->request_role = HCI_ROLE_INVALID; 274140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = 0xffff; 275965a4ccfSMatthias Ringwald conn->qos_service_type = HCI_SERVICE_TYPE_INVALID; 276e9f98c4aSMatthias Ringwald conn->link_key_type = INVALID_LINK_KEY; 27791a977e8SMatthias Ringwald btstack_run_loop_set_timer_handler(&conn->timeout, hci_connection_timeout_handler); 27891a977e8SMatthias Ringwald btstack_run_loop_set_timer_context(&conn->timeout, conn); 27996a45072S[email protected] hci_connection_timestamp(conn); 28052db98b2SMatthias Ringwald #endif 28196a45072S[email protected] conn->acl_recombination_length = 0; 28296a45072S[email protected] conn->acl_recombination_pos = 0; 283ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 284760b20efSMatthias Ringwald 285da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 286b90f6e0aSMatthias Ringwald #ifdef ENABLE_BLE 287b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = 0xff; 288b90f6e0aSMatthias Ringwald #endif 2890f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 2900f3b27c5SMatthias Ringwald conn->le_max_tx_octets = 27; 2910f3b27c5SMatthias Ringwald #endif 292cb439cf1SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 293cb439cf1SMatthias Ringwald conn->classic_oob_c_192 = NULL; 294cb439cf1SMatthias Ringwald conn->classic_oob_r_192 = NULL; 295cb439cf1SMatthias Ringwald conn->classic_oob_c_256 = NULL; 296cb439cf1SMatthias Ringwald conn->classic_oob_r_256 = NULL; 297cb439cf1SMatthias Ringwald #endif 298aee5d5c1SMatthias Ringwald } 299aee5d5c1SMatthias Ringwald 300aee5d5c1SMatthias Ringwald /** 301aee5d5c1SMatthias Ringwald * create connection for given address 302aee5d5c1SMatthias Ringwald * 303aee5d5c1SMatthias Ringwald * @return connection OR NULL, if no memory left 304aee5d5c1SMatthias Ringwald */ 305aee5d5c1SMatthias Ringwald static hci_connection_t * create_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 306aee5d5c1SMatthias Ringwald log_info("create_connection_for_addr %s, type %x", bd_addr_to_str(addr), addr_type); 307aee5d5c1SMatthias Ringwald 308aee5d5c1SMatthias Ringwald hci_connection_t * conn = btstack_memory_hci_connection_get(); 309aee5d5c1SMatthias Ringwald if (!conn) return NULL; 310aee5d5c1SMatthias Ringwald hci_connection_init(conn); 311aee5d5c1SMatthias Ringwald 312aee5d5c1SMatthias Ringwald bd_addr_copy(conn->address, addr); 313aee5d5c1SMatthias Ringwald conn->address_type = addr_type; 314aee5d5c1SMatthias Ringwald conn->con_handle = HCI_CON_HANDLE_INVALID; 315aee5d5c1SMatthias Ringwald conn->role = HCI_ROLE_INVALID; 316aee5d5c1SMatthias Ringwald 317665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->connections, (btstack_linked_item_t *) conn); 318aee5d5c1SMatthias Ringwald 31996a45072S[email protected] return conn; 32096a45072S[email protected] } 32166fb9560S[email protected] 322da886c03S[email protected] 323da886c03S[email protected] /** 324da886c03S[email protected] * get le connection parameter range 325da886c03S[email protected] * 326da886c03S[email protected] * @return le connection parameter range struct 327da886c03S[email protected] */ 3284ced4e8cSMatthias Ringwald void gap_get_connection_parameter_range(le_connection_parameter_range_t * range){ 3294ced4e8cSMatthias Ringwald *range = hci_stack->le_connection_parameter_range; 330da886c03S[email protected] } 331da886c03S[email protected] 332da886c03S[email protected] /** 333da886c03S[email protected] * set le connection parameter range 334da886c03S[email protected] * 335da886c03S[email protected] */ 336da886c03S[email protected] 3374ced4e8cSMatthias Ringwald void gap_set_connection_parameter_range(le_connection_parameter_range_t *range){ 3384ced4e8cSMatthias Ringwald hci_stack->le_connection_parameter_range = *range; 339da886c03S[email protected] } 340da886c03S[email protected] 341da886c03S[email protected] /** 34273cd8a2aSMatthias Ringwald * @brief Test if connection parameters are inside in existing rage 34373cd8a2aSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 34473cd8a2aSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 34573cd8a2aSMatthias Ringwald * @param conn_latency 34673cd8a2aSMatthias Ringwald * @param supervision_timeout (unit: 10ms) 3476b65794dSMilanka Ringwald * @return 1 if included 34873cd8a2aSMatthias Ringwald */ 34973cd8a2aSMatthias 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){ 35073cd8a2aSMatthias Ringwald if (le_conn_interval_min < existing_range->le_conn_interval_min) return 0; 35173cd8a2aSMatthias Ringwald if (le_conn_interval_max > existing_range->le_conn_interval_max) return 0; 35273cd8a2aSMatthias Ringwald 35373cd8a2aSMatthias Ringwald if (le_conn_latency < existing_range->le_conn_latency_min) return 0; 35473cd8a2aSMatthias Ringwald if (le_conn_latency > existing_range->le_conn_latency_max) return 0; 35573cd8a2aSMatthias Ringwald 35673cd8a2aSMatthias Ringwald if (le_supervision_timeout < existing_range->le_supervision_timeout_min) return 0; 35773cd8a2aSMatthias Ringwald if (le_supervision_timeout > existing_range->le_supervision_timeout_max) return 0; 35873cd8a2aSMatthias Ringwald 35973cd8a2aSMatthias Ringwald return 1; 36073cd8a2aSMatthias Ringwald } 36173cd8a2aSMatthias Ringwald 36273cd8a2aSMatthias Ringwald /** 3632b6ab3e6SMatthias Ringwald * @brief Set max number of connections in LE Peripheral role (if Bluetooth Controller supports it) 3642b6ab3e6SMatthias Ringwald * @note: default: 1 3652b6ab3e6SMatthias Ringwald * @param max_peripheral_connections 3662b6ab3e6SMatthias Ringwald */ 367d4e4907bSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3682b6ab3e6SMatthias Ringwald void gap_set_max_number_peripheral_connections(int max_peripheral_connections){ 3692b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = max_peripheral_connections; 3702b6ab3e6SMatthias Ringwald } 371d4e4907bSMatthias Ringwald #endif 3722b6ab3e6SMatthias Ringwald 3732b6ab3e6SMatthias Ringwald /** 374da886c03S[email protected] * get hci connections iterator 375da886c03S[email protected] * 376da886c03S[email protected] * @return hci connections iterator 377da886c03S[email protected] */ 378da886c03S[email protected] 379665d90f2SMatthias Ringwald void hci_connections_get_iterator(btstack_linked_list_iterator_t *it){ 380665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(it, &hci_stack->connections); 381da886c03S[email protected] } 382da886c03S[email protected] 38397addcc5Smatthias.ringwald /** 384ee091cf1Smatthias.ringwald * get connection for a given handle 385ee091cf1Smatthias.ringwald * 386ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 387ee091cf1Smatthias.ringwald */ 3885061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 389665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 390665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 391665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 392665d90f2SMatthias Ringwald hci_connection_t * item = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 3933ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 394da886c03S[email protected] return item; 395ee091cf1Smatthias.ringwald } 396ee091cf1Smatthias.ringwald } 397ee091cf1Smatthias.ringwald return NULL; 398ee091cf1Smatthias.ringwald } 399ee091cf1Smatthias.ringwald 40096a45072S[email protected] /** 40196a45072S[email protected] * get connection for given address 40296a45072S[email protected] * 40396a45072S[email protected] * @return connection OR NULL, if not found 40496a45072S[email protected] */ 405667ba9d1SMatthias Ringwald hci_connection_t * hci_connection_for_bd_addr_and_type(const bd_addr_t addr, bd_addr_type_t addr_type){ 406665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 407665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 408665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 409665d90f2SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 41096a45072S[email protected] if (connection->address_type != addr_type) continue; 41196a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 41262bda3fbS[email protected] return connection; 41362bda3fbS[email protected] } 41462bda3fbS[email protected] return NULL; 41562bda3fbS[email protected] } 41662bda3fbS[email protected] 417d5f71a7eSMatthias Ringwald #ifdef ENABLE_CLASSIC 418d5f71a7eSMatthias Ringwald 4193e5e0926SMatthias Ringwald inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 4203e5e0926SMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 4213e5e0926SMatthias Ringwald } 42252db98b2SMatthias Ringwald 423228e430cSMatthias Ringwald inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 424228e430cSMatthias Ringwald conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 425228e430cSMatthias Ringwald } 426228e430cSMatthias Ringwald 427ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 428ee752bb8SMatthias Ringwald static int hci_number_sco_connections(void){ 429ee752bb8SMatthias Ringwald int connections = 0; 430ee752bb8SMatthias Ringwald btstack_linked_list_iterator_t it; 431ee752bb8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 432ee752bb8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 433ee752bb8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 434ee752bb8SMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 435ee752bb8SMatthias Ringwald connections++; 436ee752bb8SMatthias Ringwald } 437ee752bb8SMatthias Ringwald return connections; 438ee752bb8SMatthias Ringwald } 439ee752bb8SMatthias Ringwald #endif 440ee752bb8SMatthias Ringwald 441ec820d77SMatthias Ringwald static void hci_connection_timeout_handler(btstack_timer_source_t *timer){ 44291a977e8SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_run_loop_get_timer_context(timer); 443aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 444528a4a3bSMatthias Ringwald if (btstack_run_loop_embedded_get_ticks() > connection->timestamp + btstack_run_loop_embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 445c785ef68Smatthias.ringwald // connections might be timed out 446c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 447c785ef68Smatthias.ringwald } 448f316a845SMatthias Ringwald #else 449c1ab6cc1SMatthias Ringwald if (btstack_run_loop_get_time_ms() > (connection->timestamp + HCI_CONNECTION_TIMEOUT_MS)){ 4505f26aadcSMatthias Ringwald // connections might be timed out 4515f26aadcSMatthias Ringwald hci_emit_l2cap_check_timeout(connection); 4525f26aadcSMatthias Ringwald } 4535f26aadcSMatthias Ringwald #endif 454c785ef68Smatthias.ringwald } 455ee091cf1Smatthias.ringwald 456ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 457aec7654dSMatthias Ringwald #ifdef HAVE_EMBEDDED_TICK 458528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_embedded_get_ticks(); 459f316a845SMatthias Ringwald #else 460528a4a3bSMatthias Ringwald connection->timestamp = btstack_run_loop_get_time_ms(); 4615f26aadcSMatthias Ringwald #endif 462ee091cf1Smatthias.ringwald } 463ee091cf1Smatthias.ringwald 46443bfb1bdSmatthias.ringwald /** 46580ca58a0Smatthias.ringwald * add authentication flags and reset timer 46696a45072S[email protected] * @note: assumes classic connection 4672e77e513S[email protected] * @note: bd_addr is passed in as litle endian uint8_t * as it is called from parsing packets 4687fde4af9Smatthias.ringwald */ 4697fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 4707fde4af9Smatthias.ringwald bd_addr_t addr; 471724d70a2SMatthias Ringwald reverse_bd_addr(bd_addr, addr); 472f16129ceSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 4737fde4af9Smatthias.ringwald if (conn) { 47428ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 47580ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 4767fde4af9Smatthias.ringwald } 4777fde4af9Smatthias.ringwald } 4787fde4af9Smatthias.ringwald 4791714cbbdSMatthias Ringwald static bool hci_pairing_active(hci_connection_t * hci_connection){ 4808daf94bcSMatthias Ringwald return (hci_connection->authentication_flags & AUTH_FLAG_PAIRING_ACTIVE_MASK) != 0; 4811714cbbdSMatthias Ringwald } 4821714cbbdSMatthias Ringwald 4831714cbbdSMatthias Ringwald static void hci_pairing_started(hci_connection_t * hci_connection, bool ssp){ 4841714cbbdSMatthias Ringwald if (hci_pairing_active(hci_connection)) return; 4851714cbbdSMatthias Ringwald if (ssp){ 4868daf94bcSMatthias Ringwald hci_connection->authentication_flags |= AUTH_FLAG_SSP_PAIRING_ACTIVE; 4871714cbbdSMatthias Ringwald } else { 4888daf94bcSMatthias Ringwald hci_connection->authentication_flags |= AUTH_FLAG_LEGACY_PAIRING_ACTIVE; 4891714cbbdSMatthias Ringwald } 4901714cbbdSMatthias Ringwald // if we are initiator, we have sent an HCI Authenticate Request 4911714cbbdSMatthias Ringwald bool initiator = (hci_connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0; 4921714cbbdSMatthias Ringwald 4935a561920SMatthias Ringwald // if we are responder, use minimal service security level as required level 4945a561920SMatthias Ringwald if (!initiator){ 495acadfdd0SMatthias 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); 4965a561920SMatthias Ringwald } 4975a561920SMatthias Ringwald 4985a561920SMatthias Ringwald log_info("pairing started, ssp %u, initiator %u, requested level %u", (int) ssp, (int) initiator, hci_connection->requested_security_level); 49977208d90SMatthias Ringwald 50077208d90SMatthias Ringwald uint8_t event[12]; 50177208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_STARTED; 50277208d90SMatthias Ringwald event[1] = 10; 503bfaf6993SMatthias Ringwald little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle); 5044159a80bSMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[4]); 50577208d90SMatthias Ringwald event[10] = (uint8_t) ssp; 50677208d90SMatthias Ringwald event[11] = (uint8_t) initiator; 50777208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5081714cbbdSMatthias Ringwald } 5091714cbbdSMatthias Ringwald 5101714cbbdSMatthias Ringwald static void hci_pairing_complete(hci_connection_t * hci_connection, uint8_t status){ 5113c439ac4SMatthias Ringwald hci_connection->requested_security_level = LEVEL_0; 5121714cbbdSMatthias Ringwald if (!hci_pairing_active(hci_connection)) return; 5138daf94bcSMatthias Ringwald hci_connection->authentication_flags &= ~AUTH_FLAG_PAIRING_ACTIVE_MASK; 5141e7371deSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 515cb439cf1SMatthias Ringwald hci_connection->classic_oob_c_192 = NULL; 516cb439cf1SMatthias Ringwald hci_connection->classic_oob_r_192 = NULL; 517cb439cf1SMatthias Ringwald hci_connection->classic_oob_c_256 = NULL; 518cb439cf1SMatthias Ringwald hci_connection->classic_oob_r_256 = NULL; 5191e7371deSMatthias Ringwald #endif 5201714cbbdSMatthias Ringwald log_info("pairing complete, status %02x", status); 52177208d90SMatthias Ringwald 5223176c896SMatthias Ringwald uint8_t event[11]; 52377208d90SMatthias Ringwald event[0] = GAP_EVENT_PAIRING_COMPLETE; 52477208d90SMatthias Ringwald event[1] = 9; 525bfaf6993SMatthias Ringwald little_endian_store_16(event, 2, (uint16_t) hci_connection->con_handle); 5264159a80bSMatthias Ringwald reverse_bd_addr(hci_connection->address, &event[4]); 52777208d90SMatthias Ringwald event[10] = status; 52877208d90SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 5295999ee11SMatthias Ringwald 5305999ee11SMatthias Ringwald // emit dedicated bonding done on failure, otherwise verify that connection can be encrypted 5315999ee11SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) && ((hci_connection->bonding_flags & BONDING_DEDICATED) != 0)){ 5325999ee11SMatthias Ringwald hci_connection->bonding_flags &= ~BONDING_DEDICATED; 5335999ee11SMatthias Ringwald hci_connection->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 5345999ee11SMatthias Ringwald hci_connection->bonding_status = status; 5355999ee11SMatthias Ringwald } 5361714cbbdSMatthias Ringwald } 5371714cbbdSMatthias Ringwald 53820dcdd22SMatthias Ringwald bool hci_authentication_active_for_handle(hci_con_handle_t handle){ 5395061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 54020dcdd22SMatthias Ringwald if (!conn) return false; 54120dcdd22SMatthias Ringwald return hci_pairing_active(conn); 54280ca58a0Smatthias.ringwald } 54380ca58a0Smatthias.ringwald 54415a95bd5SMatthias Ringwald void gap_drop_link_key_for_bd_addr(bd_addr_t addr){ 54555597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 5462bacf595SMatthias Ringwald log_info("gap_drop_link_key_for_bd_addr: %s", bd_addr_to_str(addr)); 547a98592bcSMatthias Ringwald hci_stack->link_key_db->delete_link_key(addr); 548c12e46e7Smatthias.ringwald } 54955597469SMatthias Ringwald 55055597469SMatthias Ringwald void gap_store_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 55155597469SMatthias Ringwald if (!hci_stack->link_key_db) return; 5522bacf595SMatthias Ringwald log_info("gap_store_link_key_for_bd_addr: %s, type %u", bd_addr_to_str(addr), type); 55355597469SMatthias Ringwald hci_stack->link_key_db->put_link_key(addr, link_key, type); 554c12e46e7Smatthias.ringwald } 5551b6fb31bSMatthias Ringwald 556e8ad470fSMatthias Ringwald bool gap_get_link_key_for_bd_addr(bd_addr_t addr, link_key_t link_key, link_key_type_t * type){ 557e8ad470fSMatthias Ringwald if (!hci_stack->link_key_db) return false; 55876b0318eSMatthias Ringwald int result = hci_stack->link_key_db->get_link_key(addr, link_key, type) != 0; 55976b0318eSMatthias Ringwald log_info("link key for %s available %u, type %u", bd_addr_to_str(addr), result, (int) *type); 56076b0318eSMatthias Ringwald return result; 561e8ad470fSMatthias Ringwald } 562e8ad470fSMatthias Ringwald 563ceecb9d9SMatthias Ringwald void gap_delete_all_link_keys(void){ 564ceecb9d9SMatthias Ringwald bd_addr_t addr; 565ceecb9d9SMatthias Ringwald link_key_t link_key; 566ceecb9d9SMatthias Ringwald link_key_type_t type; 567ceecb9d9SMatthias Ringwald btstack_link_key_iterator_t it; 568ceecb9d9SMatthias Ringwald int ok = gap_link_key_iterator_init(&it); 569ceecb9d9SMatthias Ringwald if (!ok) { 570ceecb9d9SMatthias Ringwald log_error("could not initialize iterator"); 571ceecb9d9SMatthias Ringwald return; 572ceecb9d9SMatthias Ringwald } 573ceecb9d9SMatthias Ringwald while (gap_link_key_iterator_get_next(&it, addr, link_key, &type)){ 574ceecb9d9SMatthias Ringwald gap_drop_link_key_for_bd_addr(addr); 575ceecb9d9SMatthias Ringwald } 576ceecb9d9SMatthias Ringwald gap_link_key_iterator_done(&it); 577ceecb9d9SMatthias Ringwald } 578ceecb9d9SMatthias Ringwald 5791b6fb31bSMatthias Ringwald int gap_link_key_iterator_init(btstack_link_key_iterator_t * it){ 5801b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 5811b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db->iterator_init) return 0; 5821b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_init(it); 5831b6fb31bSMatthias Ringwald } 5841b6fb31bSMatthias 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){ 5851b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return 0; 5861b6fb31bSMatthias Ringwald return hci_stack->link_key_db->iterator_get_next(it, bd_addr, link_key, type); 5871b6fb31bSMatthias Ringwald } 5881b6fb31bSMatthias Ringwald void gap_link_key_iterator_done(btstack_link_key_iterator_t * it){ 5891b6fb31bSMatthias Ringwald if (!hci_stack->link_key_db) return; 5901b6fb31bSMatthias Ringwald hci_stack->link_key_db->iterator_done(it); 5911b6fb31bSMatthias Ringwald } 59235454696SMatthias Ringwald #endif 593c12e46e7Smatthias.ringwald 594eb8076ddSMatthias Ringwald static bool hci_is_le_connection_type(bd_addr_type_t address_type){ 595eb8076ddSMatthias Ringwald switch (address_type){ 596ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 597ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 598ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_PUBLIC: 599ce41473eSMatthias Ringwald case BD_ADDR_TYPE_LE_PRIVAT_FALLBACK_RANDOM: 600eb8076ddSMatthias Ringwald return true; 601ce41473eSMatthias Ringwald default: 602eb8076ddSMatthias Ringwald return false; 603ce41473eSMatthias Ringwald } 6040bf6344aS[email protected] } 6050bf6344aS[email protected] 606eb8076ddSMatthias Ringwald static int hci_is_le_connection(hci_connection_t * connection){ 607eb8076ddSMatthias Ringwald return hci_is_le_connection_type(connection->address_type); 608eb8076ddSMatthias Ringwald } 609eb8076ddSMatthias Ringwald 6107fde4af9Smatthias.ringwald /** 61143bfb1bdSmatthias.ringwald * count connections 61243bfb1bdSmatthias.ringwald */ 61340d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 61456c253c9Smatthias.ringwald int count = 0; 615665d90f2SMatthias Ringwald btstack_linked_item_t *it; 616a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL ; it = it->next){ 61715a27967SMatthias Ringwald count++; 61815a27967SMatthias Ringwald } 61943bfb1bdSmatthias.ringwald return count; 62043bfb1bdSmatthias.ringwald } 621c8e4258aSmatthias.ringwald 6228f733c6fSMatthias Ringwald uint16_t hci_number_free_acl_slots_for_connection_type(bd_addr_type_t address_type){ 623ee303eddS[email protected] 624f04a0c31SMatthias Ringwald unsigned int num_packets_sent_classic = 0; 625f04a0c31SMatthias Ringwald unsigned int num_packets_sent_le = 0; 626ee303eddS[email protected] 627665d90f2SMatthias Ringwald btstack_linked_item_t *it; 628a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 629998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 630ce41473eSMatthias Ringwald if (hci_is_le_connection(connection)){ 631ce41473eSMatthias Ringwald num_packets_sent_le += connection->num_packets_sent; 632ce41473eSMatthias Ringwald } 633f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 634ce41473eSMatthias Ringwald num_packets_sent_classic += connection->num_packets_sent; 635ee303eddS[email protected] } 636ee303eddS[email protected] } 637d999b54eSMatthias Ringwald log_debug("ACL classic buffers: %u used of %u", num_packets_sent_classic, hci_stack->acl_packets_total_num); 638ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 639ee303eddS[email protected] int free_slots_le = 0; 640ee303eddS[email protected] 641ee303eddS[email protected] if (free_slots_classic < 0){ 6429da54300S[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); 643998906cdSmatthias.ringwald return 0; 644998906cdSmatthias.ringwald } 645ee303eddS[email protected] 646ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 647ee303eddS[email protected] // if we have LE slots, they are used 648ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 649ee303eddS[email protected] if (free_slots_le < 0){ 6509da54300S[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); 651ee303eddS[email protected] return 0; 652998906cdSmatthias.ringwald } 653ee303eddS[email protected] } else { 654ee303eddS[email protected] // otherwise, classic slots are used for LE, too 655ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 656ee303eddS[email protected] if (free_slots_classic < 0){ 6579da54300S[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); 658ee303eddS[email protected] return 0; 659ee303eddS[email protected] } 660ee303eddS[email protected] } 661ee303eddS[email protected] 662ee303eddS[email protected] switch (address_type){ 663ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 6642125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: unknown address type"); 665ee303eddS[email protected] return 0; 666ee303eddS[email protected] 667f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 6688f733c6fSMatthias Ringwald return (uint16_t) free_slots_classic; 669ee303eddS[email protected] 670ee303eddS[email protected] default: 6718f733c6fSMatthias Ringwald if (hci_stack->le_acl_packets_total_num > 0){ 6728f733c6fSMatthias Ringwald return (uint16_t) free_slots_le; 673ee303eddS[email protected] } 6748f733c6fSMatthias Ringwald return (uint16_t) free_slots_classic; 675cb00d3aaS[email protected] } 676998906cdSmatthias.ringwald } 677998906cdSmatthias.ringwald 6782125de09SMatthias Ringwald int hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 6792125de09SMatthias Ringwald // get connection type 6802125de09SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 6812125de09SMatthias Ringwald if (!connection){ 6822125de09SMatthias Ringwald log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 6832125de09SMatthias Ringwald return 0; 6842125de09SMatthias Ringwald } 6852125de09SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(connection->address_type); 6862125de09SMatthias Ringwald } 6872125de09SMatthias Ringwald 68835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 689701e3307SMatthias Ringwald static int hci_number_free_sco_slots(void){ 690f04a0c31SMatthias Ringwald unsigned int num_sco_packets_sent = 0; 691665d90f2SMatthias Ringwald btstack_linked_item_t *it; 692760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 693760b20efSMatthias Ringwald // explicit flow control 694665d90f2SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 695e35edcc1S[email protected] hci_connection_t * connection = (hci_connection_t *) it; 696ce41473eSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 697ce41473eSMatthias Ringwald num_sco_packets_sent += connection->num_packets_sent; 698e35edcc1S[email protected] } 699e35edcc1S[email protected] if (num_sco_packets_sent > hci_stack->sco_packets_total_num){ 700701e3307SMatthias Ringwald log_info("hci_number_free_sco_slots:packets (%u) > total packets (%u)", num_sco_packets_sent, hci_stack->sco_packets_total_num); 70144d0e3d5S[email protected] return 0; 70244d0e3d5S[email protected] } 703e35edcc1S[email protected] return hci_stack->sco_packets_total_num - num_sco_packets_sent; 70449205f5dSMatthias Ringwald } else { 70549205f5dSMatthias Ringwald // implicit flow control -- TODO 7066f28d2eeSMatthias Ringwald int num_ready = 0; 7076f28d2eeSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 7086f28d2eeSMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 7096f28d2eeSMatthias Ringwald if (connection->address_type != BD_ADDR_TYPE_SCO) continue; 7106f28d2eeSMatthias Ringwald if (connection->sco_tx_ready == 0) continue; 7116f28d2eeSMatthias Ringwald num_ready++; 71249205f5dSMatthias Ringwald } 7136f28d2eeSMatthias Ringwald return num_ready; 7146f28d2eeSMatthias Ringwald } 715e35edcc1S[email protected] } 71635454696SMatthias Ringwald #endif 71744d0e3d5S[email protected] 7182b838201SMatthias Ringwald // only used to send HCI Host Number Completed Packets 7192b838201SMatthias Ringwald static int hci_can_send_comand_packet_transport(void){ 720ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 721ac928cc2S[email protected] 722ac928cc2S[email protected] // check for async hci transport implementations 723ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 724ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 725ac928cc2S[email protected] return 0; 726ac928cc2S[email protected] } 727ac928cc2S[email protected] } 7282b838201SMatthias Ringwald return 1; 7292b838201SMatthias Ringwald } 730ac928cc2S[email protected] 7312b838201SMatthias Ringwald // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 7321972f31fSMatthias Ringwald bool hci_can_send_command_packet_now(void){ 7331972f31fSMatthias Ringwald if (hci_can_send_comand_packet_transport() == 0) return false; 7344ea43905SMatthias Ringwald return hci_stack->num_cmd_packets > 0u; 735ac928cc2S[email protected] } 736ac928cc2S[email protected] 7379d04d3a7SMatthias Ringwald static int hci_transport_can_send_prepared_packet_now(uint8_t packet_type){ 738ac928cc2S[email protected] // check for async hci transport implementations 7391972f31fSMatthias Ringwald if (!hci_stack->hci_transport->can_send_packet_now) return true; 7409d04d3a7SMatthias Ringwald return hci_stack->hci_transport->can_send_packet_now(packet_type); 741ac928cc2S[email protected] } 7429d04d3a7SMatthias Ringwald 7431972f31fSMatthias Ringwald static bool hci_can_send_prepared_acl_packet_for_address_type(bd_addr_type_t address_type){ 7441972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false; 7459d04d3a7SMatthias Ringwald return hci_number_free_acl_slots_for_connection_type(address_type) > 0; 746ac928cc2S[email protected] } 7479d04d3a7SMatthias Ringwald 7481972f31fSMatthias Ringwald bool hci_can_send_acl_le_packet_now(void){ 7491972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 7509d04d3a7SMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_LE_PUBLIC); 7519d04d3a7SMatthias Ringwald } 7529d04d3a7SMatthias Ringwald 7531972f31fSMatthias Ringwald bool hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 7541972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ACL_DATA_PACKET)) return false; 755e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 756ac928cc2S[email protected] } 757ac928cc2S[email protected] 7581972f31fSMatthias Ringwald bool hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 7591972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 760ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 7616b4af23dS[email protected] } 7626b4af23dS[email protected] 76335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 7641972f31fSMatthias Ringwald bool hci_can_send_acl_classic_packet_now(void){ 7651972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 766f16129ceSMatthias Ringwald return hci_can_send_prepared_acl_packet_for_address_type(BD_ADDR_TYPE_ACL); 76735454696SMatthias Ringwald } 76835454696SMatthias Ringwald 7691972f31fSMatthias Ringwald bool hci_can_send_prepared_sco_packet_now(void){ 7701972f31fSMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_SCO_DATA_PACKET)) return false; 771f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 772f234b250SMatthias Ringwald return hci_stack->sco_can_send_now; 773f234b250SMatthias Ringwald } else { 774701e3307SMatthias Ringwald return hci_number_free_sco_slots() > 0; 77544d0e3d5S[email protected] } 776f234b250SMatthias Ringwald } 77744d0e3d5S[email protected] 7781972f31fSMatthias Ringwald bool hci_can_send_sco_packet_now(void){ 7791972f31fSMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return false; 780701e3307SMatthias Ringwald return hci_can_send_prepared_sco_packet_now(); 78144d0e3d5S[email protected] } 78244d0e3d5S[email protected] 783d057580eSMatthias Ringwald void hci_request_sco_can_send_now_event(void){ 784d057580eSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 1; 785d057580eSMatthias Ringwald hci_notify_if_sco_can_send_now(); 786d057580eSMatthias Ringwald } 78735454696SMatthias Ringwald #endif 788d057580eSMatthias Ringwald 78995d71764SMatthias Ringwald // used for internal checks in l2cap.c 79002c7fc01SMatthias Ringwald bool hci_is_packet_buffer_reserved(void){ 791c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 792c8b9416aS[email protected] } 793c8b9416aS[email protected] 7946b65794dSMilanka Ringwald // reserves outgoing packet buffer. 7956b65794dSMilanka Ringwald // @return 1 if successful 796cafc12e8SMatthias Ringwald bool hci_reserve_packet_buffer(void){ 7979d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 7989d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 799cafc12e8SMatthias Ringwald return false; 8009d14b626S[email protected] } 80102c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = true; 802cafc12e8SMatthias Ringwald return true; 8036b4af23dS[email protected] } 8046b4af23dS[email protected] 80568a0fcf7S[email protected] void hci_release_packet_buffer(void){ 80602c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 80768a0fcf7S[email protected] } 80868a0fcf7S[email protected] 8096b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 8107f02f414SMatthias Ringwald static int hci_transport_synchronous(void){ 8116b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 8126b4af23dS[email protected] } 8136b4af23dS[email protected] 8148ad9cf99SMatthias Ringwald // used for debugging 8158ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 8168ad9cf99SMatthias Ringwald static void hci_controller_dump_packets(void){ 8178ad9cf99SMatthias Ringwald // format: "{handle:04x}:{count:02d} " 8188ad9cf99SMatthias Ringwald char summaries[3][7 * 8 + 1]; 8198ad9cf99SMatthias Ringwald uint16_t totals[3]; 8208ad9cf99SMatthias Ringwald uint8_t index; 8218ad9cf99SMatthias Ringwald for (index = 0 ; index < 3 ; index++){ 8228ad9cf99SMatthias Ringwald summaries[index][0] = 0; 8238ad9cf99SMatthias Ringwald totals[index] = 0; 8248ad9cf99SMatthias Ringwald } 8258ad9cf99SMatthias Ringwald btstack_linked_item_t *it; 8268ad9cf99SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 8278ad9cf99SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 8288ad9cf99SMatthias Ringwald switch (connection->address_type){ 8298ad9cf99SMatthias Ringwald case BD_ADDR_TYPE_ACL: 8308ad9cf99SMatthias Ringwald index = 0; 8318ad9cf99SMatthias Ringwald break; 8328ad9cf99SMatthias Ringwald case BD_ADDR_TYPE_SCO: 8338ad9cf99SMatthias Ringwald index = 2; 8348ad9cf99SMatthias Ringwald break; 8358ad9cf99SMatthias Ringwald default: 8368ad9cf99SMatthias Ringwald index = 1; 837ce7ca7c8SMatthias Ringwald break; 8388ad9cf99SMatthias Ringwald } 8398ad9cf99SMatthias Ringwald totals[index] += connection->num_packets_sent; 8408ad9cf99SMatthias Ringwald char item_text[10]; 8418ad9cf99SMatthias Ringwald sprintf(item_text, "%04x:%02d ", connection->con_handle,connection->num_packets_sent); 8428ad9cf99SMatthias Ringwald btstack_strcat(summaries[index], sizeof(summaries[0]), item_text); 8438ad9cf99SMatthias Ringwald } 8448ad9cf99SMatthias Ringwald for (index = 0 ; index < 3 ; index++){ 8458ad9cf99SMatthias Ringwald if (summaries[index][0] == 0){ 8468ad9cf99SMatthias Ringwald summaries[index][0] = '-'; 8478ad9cf99SMatthias Ringwald summaries[index][1] = 0; 8488ad9cf99SMatthias Ringwald } 8498ad9cf99SMatthias Ringwald } 8508ad9cf99SMatthias 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]); 8518ad9cf99SMatthias Ringwald } 8528ad9cf99SMatthias Ringwald #endif 8538ad9cf99SMatthias Ringwald 8543e2050f7SMatthias Ringwald static uint8_t hci_send_acl_packet_fragments(hci_connection_t *connection){ 855452cf3bbS[email protected] 856452cf3bbS[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); 857452cf3bbS[email protected] 858452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 859452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 8604ea43905SMatthias Ringwald if (hci_is_le_connection(connection) && (hci_stack->le_data_packets_length > 0u)){ 861452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 862452cf3bbS[email protected] } 863452cf3bbS[email protected] 8640f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 865fcf88f47SMatthias Ringwald if (hci_is_le_connection(connection) && (connection->le_max_tx_octets < max_acl_data_packet_length)){ 8660f3b27c5SMatthias Ringwald max_acl_data_packet_length = connection->le_max_tx_octets; 8670f3b27c5SMatthias Ringwald } 8680f3b27c5SMatthias Ringwald #endif 869452cf3bbS[email protected] 870d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments entered"); 871d999b54eSMatthias Ringwald 8723e2050f7SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 873452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 874ff3cc4a5SMatthias Ringwald while (true){ 875452cf3bbS[email protected] 876d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop entered"); 877d999b54eSMatthias Ringwald 878452cf3bbS[email protected] // get current data 8794ea43905SMatthias Ringwald const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4u; 880452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 8811979f09cSMatthias Ringwald bool more_fragments = false; 882452cf3bbS[email protected] 883452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 884452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 8851979f09cSMatthias Ringwald more_fragments = true; 886452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 887452cf3bbS[email protected] } 888452cf3bbS[email protected] 889452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 8904ea43905SMatthias Ringwald if (acl_header_pos > 0u){ 891f8fbdce0SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 8924ea43905SMatthias Ringwald handle_and_flags = (handle_and_flags & 0xcfffu) | (1u << 12u); 893f8fbdce0SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 894452cf3bbS[email protected] } 895452cf3bbS[email protected] 896452cf3bbS[email protected] // update header len 8974ea43905SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2u, current_acl_data_packet_length); 898452cf3bbS[email protected] 899452cf3bbS[email protected] // count packet 900ce41473eSMatthias Ringwald connection->num_packets_sent++; 9011979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop before send (more fragments %d)", (int) more_fragments); 902d999b54eSMatthias Ringwald 903d999b54eSMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 904d999b54eSMatthias Ringwald if (more_fragments){ 905d999b54eSMatthias Ringwald // update start of next fragment to send 906d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 907d999b54eSMatthias Ringwald } else { 908d999b54eSMatthias Ringwald // done 909d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 910d999b54eSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 911d999b54eSMatthias Ringwald } 912452cf3bbS[email protected] 913452cf3bbS[email protected] // send packet 914452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 915452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 9165bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 91781d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 1; 9183e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 9193e2050f7SMatthias Ringwald if (err != 0){ 9203e2050f7SMatthias Ringwald // no error from HCI Transport expected 9213e2050f7SMatthias Ringwald status = ERROR_CODE_HARDWARE_FAILURE; 9223e2050f7SMatthias Ringwald } 923452cf3bbS[email protected] 9248ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 9258ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 9268ad9cf99SMatthias Ringwald #endif 9278ad9cf99SMatthias Ringwald 9281979f09cSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop after send (more fragments %d)", (int) more_fragments); 929d999b54eSMatthias Ringwald 930452cf3bbS[email protected] // done yet? 931452cf3bbS[email protected] if (!more_fragments) break; 932452cf3bbS[email protected] 933452cf3bbS[email protected] // can send more? 9343e2050f7SMatthias Ringwald if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return status; 935452cf3bbS[email protected] } 936452cf3bbS[email protected] 937d999b54eSMatthias Ringwald log_debug("hci_send_acl_packet_fragments loop over"); 938452cf3bbS[email protected] 939d051460cS[email protected] // release buffer now for synchronous transport 940203bace6S[email protected] if (hci_transport_synchronous()){ 94181d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 942452cf3bbS[email protected] hci_release_packet_buffer(); 943fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 944452cf3bbS[email protected] } 945452cf3bbS[email protected] 9463e2050f7SMatthias Ringwald return status; 947452cf3bbS[email protected] } 948452cf3bbS[email protected] 949826f7347S[email protected] // pre: caller has reserved the packet buffer 9503e2050f7SMatthias Ringwald uint8_t hci_send_acl_packet_buffer(int size){ 9513e2050f7SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 952826f7347S[email protected] 953d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 954d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 955d713a683S[email protected] 956826f7347S[email protected] // check for free places on Bluetooth module 957d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 958826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 95997b61c7bS[email protected] hci_release_packet_buffer(); 960068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 96197b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 96297b61c7bS[email protected] } 9636218e6f1Smatthias.ringwald 9645061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 96597b61c7bS[email protected] if (!connection) { 9665fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 96797b61c7bS[email protected] hci_release_packet_buffer(); 968068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 9693e2050f7SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 97097b61c7bS[email protected] } 97152db98b2SMatthias Ringwald 97252db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 97356cf178bSmatthias.ringwald hci_connection_timestamp(connection); 97452db98b2SMatthias Ringwald #endif 97556cf178bSmatthias.ringwald 976452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 9777856c818Smatthias.ringwald 978452cf3bbS[email protected] // setup data 979452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 980452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 9816218e6f1Smatthias.ringwald 982452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 983ee091cf1Smatthias.ringwald } 984ee091cf1Smatthias.ringwald 98535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 98644d0e3d5S[email protected] // pre: caller has reserved the packet buffer 9873e2050f7SMatthias Ringwald uint8_t hci_send_sco_packet_buffer(int size){ 9883e2050f7SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 98944d0e3d5S[email protected] 99044d0e3d5S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 9914b3e1e19SMatthias Ringwald 9924b3e1e19SMatthias Ringwald // skip checks in loopback mode 9934b3e1e19SMatthias Ringwald if (!hci_stack->loopback_mode){ 99444d0e3d5S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); // same for ACL and SCO 99544d0e3d5S[email protected] 99644d0e3d5S[email protected] // check for free places on Bluetooth module 997701e3307SMatthias Ringwald if (!hci_can_send_prepared_sco_packet_now()) { 998cbf638a9SMatthias Ringwald log_error("hci_send_sco_packet_buffer called but no free SCO buffers on controller"); 99944d0e3d5S[email protected] hci_release_packet_buffer(); 1000068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 100144d0e3d5S[email protected] return BTSTACK_ACL_BUFFERS_FULL; 100244d0e3d5S[email protected] } 100344d0e3d5S[email protected] 1004e35edcc1S[email protected] // track send packet in connection struct 1005e35edcc1S[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 1006e35edcc1S[email protected] if (!connection) { 1007e35edcc1S[email protected] log_error("hci_send_sco_packet_buffer called but no connection for handle 0x%04x", con_handle); 1008e35edcc1S[email protected] hci_release_packet_buffer(); 1009068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 10103e2050f7SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1011e35edcc1S[email protected] } 1012f234b250SMatthias Ringwald 1013f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 1014f234b250SMatthias Ringwald // token used 10151972f31fSMatthias Ringwald hci_stack->sco_can_send_now = false; 1016f234b250SMatthias Ringwald } else { 1017760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled){ 1018ce41473eSMatthias Ringwald connection->num_packets_sent++; 10196f28d2eeSMatthias Ringwald } else { 1020e4157653SMatthias Ringwald connection->sco_tx_ready--; 1021760b20efSMatthias Ringwald } 10224b3e1e19SMatthias Ringwald } 1023f234b250SMatthias Ringwald } 102444d0e3d5S[email protected] 102544d0e3d5S[email protected] hci_dump_packet( HCI_SCO_DATA_PACKET, 0, packet, size); 1026543e835cSMatthias Ringwald 102743149fc9SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 102843149fc9SMatthias Ringwald hci_stack->sco_transport->send_packet(packet, size); 102943149fc9SMatthias Ringwald hci_release_packet_buffer(); 103043149fc9SMatthias Ringwald hci_emit_transport_packet_sent(); 103143149fc9SMatthias Ringwald 103243149fc9SMatthias Ringwald return 0; 103343149fc9SMatthias Ringwald #else 103443149fc9SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_SCO_DATA_PACKET, packet, size); 1035543e835cSMatthias Ringwald if (hci_transport_synchronous()){ 1036543e835cSMatthias Ringwald hci_release_packet_buffer(); 1037fd43c0e0SMatthias Ringwald hci_emit_transport_packet_sent(); 1038543e835cSMatthias Ringwald } 1039543e835cSMatthias Ringwald 10403e2050f7SMatthias Ringwald if (err != 0){ 10413e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 10423e2050f7SMatthias Ringwald } 10433e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 104443149fc9SMatthias Ringwald #endif 104544d0e3d5S[email protected] } 104635454696SMatthias Ringwald #endif 104744d0e3d5S[email protected] 10480a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 104968ab6207SMatthias Ringwald static uint8_t hci_send_iso_packet_fragments(void){ 105068ab6207SMatthias Ringwald 105168ab6207SMatthias Ringwald uint16_t max_iso_data_packet_length = hci_stack->le_iso_packets_length; 105268ab6207SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 105368ab6207SMatthias Ringwald // multiple packets could be send on a synchronous HCI transport 105468ab6207SMatthias Ringwald while (true){ 105568ab6207SMatthias Ringwald 105668ab6207SMatthias Ringwald // get current data 105768ab6207SMatthias Ringwald const uint16_t iso_header_pos = hci_stack->iso_fragmentation_pos - 4u; 105868ab6207SMatthias Ringwald int current_iso_data_packet_length = hci_stack->iso_fragmentation_total_size - hci_stack->iso_fragmentation_pos; 105968ab6207SMatthias Ringwald bool more_fragments = false; 106068ab6207SMatthias Ringwald 106168ab6207SMatthias Ringwald // if ISO packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 106268ab6207SMatthias Ringwald if (current_iso_data_packet_length > max_iso_data_packet_length){ 106368ab6207SMatthias Ringwald more_fragments = true; 106468ab6207SMatthias Ringwald current_iso_data_packet_length = max_iso_data_packet_length; 106568ab6207SMatthias Ringwald } 106668ab6207SMatthias Ringwald 106768ab6207SMatthias Ringwald // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 106868ab6207SMatthias Ringwald uint16_t handle_and_flags = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 106968ab6207SMatthias Ringwald uint8_t pb_flags; 107068ab6207SMatthias Ringwald if (iso_header_pos == 0u){ 107168ab6207SMatthias Ringwald // first fragment, keep TS field 107268ab6207SMatthias Ringwald pb_flags = more_fragments ? 0x00 : 0x02; 107368ab6207SMatthias Ringwald handle_and_flags = (handle_and_flags & 0x4fffu) | (pb_flags << 12u); 107468ab6207SMatthias Ringwald } else { 107568ab6207SMatthias Ringwald // later fragment, drop TS field 107668ab6207SMatthias Ringwald pb_flags = more_fragments ? 0x01 : 0x03; 107768ab6207SMatthias Ringwald handle_and_flags = (handle_and_flags & 0x0fffu) | (pb_flags << 12u); 107868ab6207SMatthias Ringwald } 107968ab6207SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, iso_header_pos, handle_and_flags); 108068ab6207SMatthias Ringwald 108168ab6207SMatthias Ringwald // update header len 108268ab6207SMatthias Ringwald little_endian_store_16(hci_stack->hci_packet_buffer, iso_header_pos + 2u, current_iso_data_packet_length); 108368ab6207SMatthias Ringwald 108468ab6207SMatthias Ringwald // update state for next fragment (if any) as "transport done" might be sent during send_packet already 108568ab6207SMatthias Ringwald if (more_fragments){ 108668ab6207SMatthias Ringwald // update start of next fragment to send 108768ab6207SMatthias Ringwald hci_stack->iso_fragmentation_pos += current_iso_data_packet_length; 108868ab6207SMatthias Ringwald } else { 108968ab6207SMatthias Ringwald // done 109068ab6207SMatthias Ringwald hci_stack->iso_fragmentation_pos = 0; 109168ab6207SMatthias Ringwald hci_stack->iso_fragmentation_total_size = 0; 109268ab6207SMatthias Ringwald } 109368ab6207SMatthias Ringwald 109468ab6207SMatthias Ringwald // send packet 109568ab6207SMatthias Ringwald uint8_t * packet = &hci_stack->hci_packet_buffer[iso_header_pos]; 109668ab6207SMatthias Ringwald const int size = current_iso_data_packet_length + 4; 109768ab6207SMatthias Ringwald hci_dump_packet(HCI_ISO_DATA_PACKET, 0, packet, size); 109868ab6207SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = true; 109968ab6207SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_ISO_DATA_PACKET, packet, size); 110068ab6207SMatthias Ringwald if (err != 0){ 110168ab6207SMatthias Ringwald // no error from HCI Transport expected 110268ab6207SMatthias Ringwald status = ERROR_CODE_HARDWARE_FAILURE; 110368ab6207SMatthias Ringwald } 110468ab6207SMatthias Ringwald 110568ab6207SMatthias Ringwald // done yet? 110668ab6207SMatthias Ringwald if (!more_fragments) break; 110768ab6207SMatthias Ringwald 110868ab6207SMatthias Ringwald // can send more? 110968ab6207SMatthias Ringwald if (!hci_transport_can_send_prepared_packet_now(HCI_ISO_DATA_PACKET)) return false; 111068ab6207SMatthias Ringwald } 111168ab6207SMatthias Ringwald 111268ab6207SMatthias Ringwald // release buffer now for synchronous transport 111368ab6207SMatthias Ringwald if (hci_transport_synchronous()){ 111468ab6207SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = false; 111568ab6207SMatthias Ringwald hci_release_packet_buffer(); 111668ab6207SMatthias Ringwald hci_emit_transport_packet_sent(); 111768ab6207SMatthias Ringwald } 111868ab6207SMatthias Ringwald 111968ab6207SMatthias Ringwald return status; 112068ab6207SMatthias Ringwald } 112168ab6207SMatthias Ringwald 1122ace08712SMatthias Ringwald uint8_t hci_send_iso_packet_buffer(uint16_t size){ 1123ace08712SMatthias Ringwald btstack_assert(hci_stack->hci_packet_buffer_reserved); 1124ace08712SMatthias Ringwald 1125e5413aa0SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) little_endian_read_16(hci_stack->hci_packet_buffer, 0) & 0xfff; 1126e5413aa0SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(con_handle); 1127e5413aa0SMatthias Ringwald if (iso_stream == NULL){ 1128977f918dSMatthias Ringwald hci_release_packet_buffer(); 1129977f918dSMatthias Ringwald hci_iso_notify_can_send_now(); 1130e5413aa0SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1131e5413aa0SMatthias Ringwald } 1132e5413aa0SMatthias Ringwald 1133e5413aa0SMatthias Ringwald // TODO: check for space on controller 1134e5413aa0SMatthias Ringwald 1135977f918dSMatthias Ringwald // skip iso packets if needed 1136977f918dSMatthias Ringwald if (iso_stream->num_packets_to_skip > 0){ 1137977f918dSMatthias Ringwald iso_stream->num_packets_to_skip--; 1138977f918dSMatthias Ringwald // pretend it was processed and trigger next one 1139977f918dSMatthias Ringwald hci_release_packet_buffer(); 1140977f918dSMatthias Ringwald hci_iso_notify_can_send_now(); 1141977f918dSMatthias Ringwald return ERROR_CODE_SUCCESS; 1142977f918dSMatthias Ringwald } 1143977f918dSMatthias Ringwald 1144e5413aa0SMatthias Ringwald // track outgoing packet sent 1145e5413aa0SMatthias Ringwald log_info("Outgoing ISO packet for con handle 0x%04x", con_handle); 1146e5413aa0SMatthias Ringwald iso_stream->num_packets_sent++; 1147e5413aa0SMatthias Ringwald 114868ab6207SMatthias Ringwald // setup data 114968ab6207SMatthias Ringwald hci_stack->iso_fragmentation_total_size = size; 115068ab6207SMatthias Ringwald hci_stack->iso_fragmentation_pos = 4; // start of L2CAP packet 115168ab6207SMatthias Ringwald 115268ab6207SMatthias Ringwald return hci_send_iso_packet_fragments(); 1153ace08712SMatthias Ringwald } 1154ace08712SMatthias Ringwald #endif 1155ace08712SMatthias Ringwald 1156c3b46f5aSMatthias Ringwald static void acl_handler(uint8_t *packet, uint16_t size){ 1157e76a89eeS[email protected] 11587856c818Smatthias.ringwald // get info 11597856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 11605061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 11617856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 11627856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 11637856c818Smatthias.ringwald 11647856c818Smatthias.ringwald // ignore non-registered handle 11657856c818Smatthias.ringwald if (!conn){ 1166c3b46f5aSMatthias Ringwald log_error("acl_handler called with non-registered handle %u!" , con_handle); 11677856c818Smatthias.ringwald return; 11687856c818Smatthias.ringwald } 11697856c818Smatthias.ringwald 1170e76a89eeS[email protected] // assert packet is complete 11714ea43905SMatthias Ringwald if ((acl_length + 4u) != size){ 1172c3b46f5aSMatthias Ringwald log_error("acl_handler called with ACL packet of wrong size %d, expected %u => dropping packet", size, acl_length + 4); 1173e76a89eeS[email protected] return; 1174e76a89eeS[email protected] } 1175e76a89eeS[email protected] 117652db98b2SMatthias Ringwald #ifdef ENABLE_CLASSIC 11777856c818Smatthias.ringwald // update idle timestamp 11787856c818Smatthias.ringwald hci_connection_timestamp(conn); 117952db98b2SMatthias Ringwald #endif 11807856c818Smatthias.ringwald 11812b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 11822b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 11832b838201SMatthias Ringwald conn->num_packets_completed++; 11842b838201SMatthias Ringwald #endif 11852b838201SMatthias Ringwald 11867856c818Smatthias.ringwald // handle different packet types 11874ea43905SMatthias Ringwald switch (acl_flags & 0x03u) { 11887856c818Smatthias.ringwald 11897856c818Smatthias.ringwald case 0x01: // continuation fragment 11907856c818Smatthias.ringwald 11910ca847afS[email protected] // sanity checks 11924ea43905SMatthias Ringwald if (conn->acl_recombination_pos == 0u) { 11939da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 11947856c818Smatthias.ringwald return; 11957856c818Smatthias.ringwald } 11964ea43905SMatthias Ringwald if ((conn->acl_recombination_pos + acl_length) > (4u + HCI_ACL_BUFFER_SIZE)){ 11970ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 11980ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 11990ca847afS[email protected] conn->acl_recombination_pos = 0; 12000ca847afS[email protected] return; 12010ca847afS[email protected] } 12027856c818Smatthias.ringwald 12037856c818Smatthias.ringwald // append fragment payload (header already stored) 12046535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], 12056535961aSMatthias Ringwald &packet[4], acl_length); 12067856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 12077856c818Smatthias.ringwald 12087856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 12094ea43905SMatthias Ringwald if (conn->acl_recombination_pos >= (conn->acl_recombination_length + 4u + 4u)){ // pos already incl. ACL header 1210d6b06661SMatthias Ringwald hci_emit_acl_packet(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 12117856c818Smatthias.ringwald // reset recombination buffer 12127856c818Smatthias.ringwald conn->acl_recombination_length = 0; 12137856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 12147856c818Smatthias.ringwald } 12157856c818Smatthias.ringwald break; 12167856c818Smatthias.ringwald 12177856c818Smatthias.ringwald case 0x02: { // first fragment 12187856c818Smatthias.ringwald 121923a77e1aS[email protected] // sanity check 122023a77e1aS[email protected] if (conn->acl_recombination_pos) { 122160737d2bSMatthias Ringwald // we just received the first fragment, but still have data. Only warn if the packet wasn't a flushable packet 122260737d2bSMatthias Ringwald if ((conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE+1] >> 4) != 0x02){ 122360737d2bSMatthias Ringwald log_error( "ACL First Fragment but %u bytes in buffer for handle 0x%02x, dropping stale fragments", conn->acl_recombination_pos, con_handle); 122460737d2bSMatthias Ringwald } 122523a77e1aS[email protected] conn->acl_recombination_pos = 0; 122623a77e1aS[email protected] } 122723a77e1aS[email protected] 12287856c818Smatthias.ringwald // peek into L2CAP packet! 12297856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 12307856c818Smatthias.ringwald 12317856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 12324ea43905SMatthias Ringwald if (acl_length >= (l2cap_length + 4u)){ 12337856c818Smatthias.ringwald // forward fragment as L2CAP packet 12344ea43905SMatthias Ringwald hci_emit_acl_packet(packet, acl_length + 4u); 12357856c818Smatthias.ringwald } else { 12360ca847afS[email protected] 12370ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 12380ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 12390ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 12400ca847afS[email protected] return; 12410ca847afS[email protected] } 12420ca847afS[email protected] 12437856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 12446535961aSMatthias Ringwald (void)memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], 12454ea43905SMatthias Ringwald packet, acl_length + 4u); 12464ea43905SMatthias Ringwald conn->acl_recombination_pos = acl_length + 4u; 12477856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 12484ea43905SMatthias Ringwald little_endian_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2u, l2cap_length +4u); 12497856c818Smatthias.ringwald } 12507856c818Smatthias.ringwald break; 12517856c818Smatthias.ringwald 12527856c818Smatthias.ringwald } 12537856c818Smatthias.ringwald default: 1254c3b46f5aSMatthias Ringwald log_error( "acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 12557856c818Smatthias.ringwald return; 12567856c818Smatthias.ringwald } 125794ab26f8Smatthias.ringwald 125894ab26f8Smatthias.ringwald // execute main loop 125994ab26f8Smatthias.ringwald hci_run(); 126016833f0aSmatthias.ringwald } 126122909952Smatthias.ringwald 12621ab2dc58SMatthias Ringwald static void hci_connection_stop_timer(hci_connection_t * conn){ 12631ab2dc58SMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout); 12641ab2dc58SMatthias Ringwald #ifdef ENABLE_CLASSIC 12651ab2dc58SMatthias Ringwald btstack_run_loop_remove_timer(&conn->timeout_sco); 12661ab2dc58SMatthias Ringwald #endif 12671ab2dc58SMatthias Ringwald } 12681ab2dc58SMatthias Ringwald 126967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 12709da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 12713c4d4b90Smatthias.ringwald 1272b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1273cb70c5abSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(HAVE_SCO_TRANSPORT) 1274cb70c5abSMatthias Ringwald bd_addr_type_t addr_type = conn->address_type; 1275cb70c5abSMatthias Ringwald #endif 1276cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1277cb70c5abSMatthias Ringwald hci_con_handle_t con_handle = conn->con_handle; 1278ee752bb8SMatthias Ringwald #endif 1279b3264428SMatthias Ringwald #endif 1280ee752bb8SMatthias Ringwald 12811ab2dc58SMatthias Ringwald hci_connection_stop_timer(conn); 1282c785ef68Smatthias.ringwald 1283665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 1284a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 12853c4d4b90Smatthias.ringwald 12863c4d4b90Smatthias.ringwald // now it's gone 1287c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 1288ee752bb8SMatthias Ringwald 1289b3264428SMatthias Ringwald #ifdef ENABLE_CLASSIC 1290034e9b53SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 1291ee752bb8SMatthias Ringwald // update SCO 1292cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->hci_transport != NULL) && (hci_stack->hci_transport->set_sco_config != NULL)){ 1293ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 1294ee752bb8SMatthias Ringwald } 1295034e9b53SMatthias Ringwald #endif 1296cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 1297cb70c5abSMatthias Ringwald if ((addr_type == BD_ADDR_TYPE_SCO) && (hci_stack->sco_transport != NULL)){ 1298cb70c5abSMatthias Ringwald hci_stack->sco_transport->close(con_handle); 1299cb70c5abSMatthias Ringwald } 1300cb70c5abSMatthias Ringwald #endif 1301b3264428SMatthias Ringwald #endif 1302c7e0c5f6Smatthias.ringwald } 1303c7e0c5f6Smatthias.ringwald 130435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 130535454696SMatthias Ringwald 13060c042179S[email protected] static const uint16_t packet_type_sizes[] = { 13078f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 13088f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 13098f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 13108f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 13118f8108aaSmatthias.ringwald }; 131265389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 131365389bfcS[email protected] 0, // 3 slot packets 131465389bfcS[email protected] 1, // 5 slot packets 131565389bfcS[email protected] 25, // EDR 2 mpbs 131665389bfcS[email protected] 26, // EDR 3 mbps 131765389bfcS[email protected] 39, // 3 slot EDR packts 131865389bfcS[email protected] 40, // 5 slot EDR packet 131965389bfcS[email protected] }; 132065389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 132165389bfcS[email protected] 0x0f00, // 3 slot packets 132265389bfcS[email protected] 0xf000, // 5 slot packets 132365389bfcS[email protected] 0x1102, // EDR 2 mpbs 132465389bfcS[email protected] 0x2204, // EDR 3 mbps 132565389bfcS[email protected] 0x0300, // 3 slot EDR packts 132665389bfcS[email protected] 0x3000, // 5 slot EDR packet 132765389bfcS[email protected] }; 13288f8108aaSmatthias.ringwald 132965389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 133065389bfcS[email protected] // enable packet types based on size 13318f8108aaSmatthias.ringwald uint16_t packet_types = 0; 1332f16a69bbS[email protected] unsigned int i; 13338f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 13348f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 13358f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 13368f8108aaSmatthias.ringwald packet_types |= 1 << i; 13378f8108aaSmatthias.ringwald } 13388f8108aaSmatthias.ringwald } 133965389bfcS[email protected] // disable packet types due to missing local supported features 134065389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 1341f04a0c31SMatthias Ringwald unsigned int bit_idx = packet_type_feature_requirement_bit[i]; 134265389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 134365389bfcS[email protected] if (feature_set) continue; 134465389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 134565389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 134665389bfcS[email protected] } 13478f8108aaSmatthias.ringwald // flip bits for "may not be used" 13488f8108aaSmatthias.ringwald packet_types ^= 0x3306; 13498f8108aaSmatthias.ringwald return packet_types; 13508f8108aaSmatthias.ringwald } 13518f8108aaSmatthias.ringwald 13528f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 13533a9fb326S[email protected] return hci_stack->packet_types; 13548f8108aaSmatthias.ringwald } 135535454696SMatthias Ringwald #endif 13568f8108aaSmatthias.ringwald 1357facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 13587dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 13593a9fb326S[email protected] return hci_stack->hci_packet_buffer; 13607dc17943Smatthias.ringwald } 13617dc17943Smatthias.ringwald 1362f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 13633a9fb326S[email protected] return hci_stack->acl_data_packet_length; 13647dc17943Smatthias.ringwald } 13657dc17943Smatthias.ringwald 136606b9e820SMatthias Ringwald #ifdef ENABLE_CLASSIC 136720dcdd22SMatthias Ringwald bool hci_extended_sco_link_supported(void){ 13683e68d23dSMatthias Ringwald // No. 31, byte 3, bit 7 13693e68d23dSMatthias Ringwald return (hci_stack->local_supported_features[3] & (1 << 7)) != 0; 13703e68d23dSMatthias Ringwald } 137106b9e820SMatthias Ringwald #endif 13723e68d23dSMatthias Ringwald 137320dcdd22SMatthias Ringwald bool hci_non_flushable_packet_boundary_flag_supported(void){ 13746ac9a97eS[email protected] // No. 54, byte 6, bit 6 13754ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 6u)) != 0u; 13766ac9a97eS[email protected] } 13776ac9a97eS[email protected] 13789885fb2dSMatthias Ringwald #ifdef ENABLE_CLASSIC 137915a95bd5SMatthias Ringwald static int gap_ssp_supported(void){ 13806ac9a97eS[email protected] // No. 51, byte 6, bit 3 13814ea43905SMatthias Ringwald return (hci_stack->local_supported_features[6u] & (1u << 3u)) != 0u; 1382f5d8d141S[email protected] } 13839885fb2dSMatthias Ringwald #endif 1384f5d8d141S[email protected] 13857f02f414SMatthias Ringwald static int hci_classic_supported(void){ 138635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 13876ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 13883a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 138935454696SMatthias Ringwald #else 139035454696SMatthias Ringwald return 0; 139135454696SMatthias Ringwald #endif 1392f5d8d141S[email protected] } 1393f5d8d141S[email protected] 13947f02f414SMatthias Ringwald static int hci_le_supported(void){ 1395a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 13966ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 13974ea43905SMatthias Ringwald return (hci_stack->local_supported_features[4u] & (1u << 6u)) != 0u; 1398f5d8d141S[email protected] #else 1399f5d8d141S[email protected] return 0; 1400f5d8d141S[email protected] #endif 1401f5d8d141S[email protected] } 1402f5d8d141S[email protected] 14031ffa425cSMatthias Ringwald static bool hci_command_supported(uint8_t command_index){ 14041ffa425cSMatthias Ringwald return (hci_stack->local_supported_commands & (1LU << command_index)) != 0; 14051ffa425cSMatthias Ringwald } 14061ffa425cSMatthias Ringwald 1407b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 1408b95a5a35SMatthias Ringwald 140963671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 141063671e69SMatthias Ringwald static bool hci_extended_advertising_supported(void){ 141163671e69SMatthias Ringwald return hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_EXTENDED_ADVERTISING_ENABLE); 141263671e69SMatthias Ringwald } 141363671e69SMatthias Ringwald #endif 141463671e69SMatthias Ringwald 1415f5873674SMatthias Ringwald static void hci_get_own_address_for_addr_type(uint8_t own_addr_type, bd_addr_t own_addr){ 14166bcfa632SMatthias Ringwald if (own_addr_type == BD_ADDR_TYPE_LE_PUBLIC){ 14176bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->local_bd_addr, 6); 141869a97523S[email protected] } else { 14196bcfa632SMatthias Ringwald (void)memcpy(own_addr, hci_stack->le_random_address, 6); 142069a97523S[email protected] } 142169a97523S[email protected] } 142269a97523S[email protected] 14236bcfa632SMatthias Ringwald void gap_le_get_own_address(uint8_t * addr_type, bd_addr_t addr){ 14246bcfa632SMatthias Ringwald *addr_type = hci_stack->le_own_addr_type; 14256bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_own_addr_type, addr); 14266bcfa632SMatthias Ringwald } 14276bcfa632SMatthias Ringwald 14286bcfa632SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 14296bcfa632SMatthias Ringwald void gap_le_get_own_advertisements_address(uint8_t * addr_type, bd_addr_t addr){ 14306bcfa632SMatthias Ringwald *addr_type = hci_stack->le_advertisements_own_addr_type; 14316bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, addr); 14326bcfa632SMatthias Ringwald }; 14336bcfa632SMatthias Ringwald #endif 14346bcfa632SMatthias Ringwald 1435e8c8828eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 14366bcfa632SMatthias Ringwald 14376bcfa632SMatthias Ringwald /** 14386bcfa632SMatthias Ringwald * @brief Get own addr type and address used for LE connections (Central) 14396bcfa632SMatthias Ringwald */ 14406bcfa632SMatthias Ringwald void gap_le_get_own_connection_address(uint8_t * addr_type, bd_addr_t addr){ 14416bcfa632SMatthias Ringwald *addr_type = hci_stack->le_connection_own_addr_type; 14426bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, addr); 14436bcfa632SMatthias Ringwald } 14446bcfa632SMatthias Ringwald 1445384b59deSMatthias Ringwald void le_handle_advertisement_report(uint8_t *packet, uint16_t size){ 14469ec2630cSMatthias Ringwald 1447a0698cb4SMatthias Ringwald uint16_t offset = 3; 1448a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset]; 1449d1dc057bS[email protected] offset += 1; 1450d1dc057bS[email protected] 1451a0698cb4SMatthias Ringwald uint16_t i; 145203fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 1453a1df452eSMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 145433e6948bSMatthias Ringwald // sanity checks on data_length: 145533e6948bSMatthias Ringwald uint8_t data_length = packet[offset + 8]; 145633e6948bSMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 14574ea43905SMatthias Ringwald if ((offset + 9u + data_length + 1u) > size) return; 145833e6948bSMatthias Ringwald // setup event 14594ea43905SMatthias Ringwald uint8_t event_size = 10u + data_length; 1460a0698cb4SMatthias Ringwald uint16_t pos = 0; 1461045013feSMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 146257c9da5bS[email protected] event[pos++] = event_size; 14636535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], 1 + 1 + 6); // event type + address type + address 1464d1dc057bS[email protected] offset += 8; 1465d1dc057bS[email protected] pos += 8; 1466d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 146733e6948bSMatthias Ringwald event[pos++] = data_length; 146833e6948bSMatthias Ringwald offset++; 14696535961aSMatthias Ringwald (void)memcpy(&event[pos], &packet[offset], data_length); 147057c9da5bS[email protected] pos += data_length; 14714ea43905SMatthias Ringwald offset += data_length + 1u; // rssi 1472d6b06661SMatthias Ringwald hci_emit_event(event, pos, 1); 147357c9da5bS[email protected] } 147457c9da5bS[email protected] } 1475a0698cb4SMatthias Ringwald 1476a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 1477a0698cb4SMatthias Ringwald void le_handle_extended_advertisement_report(uint8_t *packet, uint16_t size) { 1478a0698cb4SMatthias Ringwald uint16_t offset = 3; 1479a0698cb4SMatthias Ringwald uint8_t num_reports = packet[offset++]; 1480c95a0fc7SMatthias Ringwald uint8_t event[2 + 255]; // use upper bound to avoid var size automatic var 1481a0698cb4SMatthias Ringwald uint8_t i; 1482a0698cb4SMatthias Ringwald for (i=0; (i<num_reports) && (offset < size);i++){ 1483a0698cb4SMatthias Ringwald // sanity checks on data_length: 1484a0698cb4SMatthias Ringwald uint16_t data_length = packet[offset + 23]; 1485a0698cb4SMatthias Ringwald if (data_length > LE_ADVERTISING_DATA_SIZE) return; 1486a0698cb4SMatthias Ringwald if ((offset + 24u + data_length) > size) return; 1487a0698cb4SMatthias Ringwald uint16_t event_type = little_endian_read_16(packet, offset); 1488a0698cb4SMatthias Ringwald offset += 2; 1489a0698cb4SMatthias Ringwald if ((event_type & 0x10) != 0) { 1490a0698cb4SMatthias Ringwald // setup legacy event 1491a0698cb4SMatthias Ringwald uint8_t legacy_event_type; 1492a0698cb4SMatthias Ringwald switch (event_type){ 1493a0698cb4SMatthias Ringwald case 0b0010011: 1494a0698cb4SMatthias Ringwald // ADV_IND 1495a0698cb4SMatthias Ringwald legacy_event_type = 0; 1496a0698cb4SMatthias Ringwald break; 1497a0698cb4SMatthias Ringwald case 0b0010101: 1498a0698cb4SMatthias Ringwald // ADV_DIRECT_IND 1499a0698cb4SMatthias Ringwald legacy_event_type = 1; 1500a0698cb4SMatthias Ringwald break; 1501a0698cb4SMatthias Ringwald case 0b0010010: 1502a0698cb4SMatthias Ringwald // ADV_SCAN_IND 1503a0698cb4SMatthias Ringwald legacy_event_type = 2; 1504a0698cb4SMatthias Ringwald break; 1505a0698cb4SMatthias Ringwald case 0b0010000: 1506a0698cb4SMatthias Ringwald // ADV_NONCONN_IND 1507a0698cb4SMatthias Ringwald legacy_event_type = 3; 1508a0698cb4SMatthias Ringwald break; 1509a0698cb4SMatthias Ringwald case 0b0011011: 1510a0698cb4SMatthias Ringwald case 0b0011010: 1511a0698cb4SMatthias Ringwald // SCAN_RSP 1512a0698cb4SMatthias Ringwald legacy_event_type = 4; 1513a0698cb4SMatthias Ringwald break; 1514a0698cb4SMatthias Ringwald default: 1515a0698cb4SMatthias Ringwald legacy_event_type = 0; 1516a0698cb4SMatthias Ringwald break; 1517a0698cb4SMatthias Ringwald } 1518a0698cb4SMatthias Ringwald uint16_t pos = 0; 1519a0698cb4SMatthias Ringwald event[pos++] = GAP_EVENT_ADVERTISING_REPORT; 1520a0698cb4SMatthias Ringwald event[pos++] = 10u + data_length; 1521a0698cb4SMatthias Ringwald event[pos++] = legacy_event_type; 1522a0698cb4SMatthias Ringwald // copy address type + address 1523a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + 6); 1524a0698cb4SMatthias Ringwald offset += 7; 1525a0698cb4SMatthias Ringwald pos += 7; 1526a0698cb4SMatthias Ringwald // skip primary_phy, secondary_phy, advertising_sid, tx_power 1527a0698cb4SMatthias Ringwald offset += 4; 1528a0698cb4SMatthias Ringwald // copy rssi 1529a0698cb4SMatthias Ringwald event[pos++] = packet[offset++]; 1530a0698cb4SMatthias Ringwald // skip periodic advertising interval and direct address 1531a0698cb4SMatthias Ringwald offset += 9; 1532a0698cb4SMatthias Ringwald // copy data len + data; 1533a0698cb4SMatthias Ringwald (void) memcpy(&event[pos], &packet[offset], 1 + data_length); 1534a0698cb4SMatthias Ringwald pos += 1 +data_length; 1535a0698cb4SMatthias Ringwald offset += 1+ data_length; 1536a0698cb4SMatthias Ringwald hci_emit_event(event, pos, 1); 1537a0698cb4SMatthias Ringwald } else { 1538c95a0fc7SMatthias Ringwald event[0] = GAP_EVENT_EXTENDED_ADVERTISING_REPORT; 1539c95a0fc7SMatthias Ringwald uint8_t report_len = 24 + data_length; 1540c95a0fc7SMatthias Ringwald event[1] = report_len; 15410f7610adSMatthias Ringwald little_endian_store_16(event, 2, event_type); 15420f7610adSMatthias Ringwald memcpy(&event[4], &packet[offset], report_len); 1543c95a0fc7SMatthias Ringwald offset += report_len; 1544c95a0fc7SMatthias Ringwald hci_emit_event(event, 2 + report_len, 1); 1545a0698cb4SMatthias Ringwald } 1546a0698cb4SMatthias Ringwald } 1547a0698cb4SMatthias Ringwald } 1548a0698cb4SMatthias Ringwald #endif 1549a0698cb4SMatthias Ringwald 1550b2f949feS[email protected] #endif 1551e8c8828eSMatthias Ringwald #endif 155257c9da5bS[email protected] 15532b6ab3e6SMatthias Ringwald #ifdef ENABLE_BLE 15542b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 1555bbc366e6SMatthias Ringwald static void hci_update_advertisements_enabled_for_current_roles(void){ 1556a408e724SMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ENABLED) != 0){ 15572b6ab3e6SMatthias Ringwald // get number of active le slave connections 15582b6ab3e6SMatthias Ringwald int num_slave_connections = 0; 15592b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_t it; 15602b6ab3e6SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 15612b6ab3e6SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 15622b6ab3e6SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 15632b6ab3e6SMatthias Ringwald log_info("state %u, role %u, le_con %u", con->state, con->role, hci_is_le_connection(con)); 15642b6ab3e6SMatthias Ringwald if (con->state != OPEN) continue; 15652b6ab3e6SMatthias Ringwald if (con->role != HCI_ROLE_SLAVE) continue; 15662b6ab3e6SMatthias Ringwald if (!hci_is_le_connection(con)) continue; 15672b6ab3e6SMatthias Ringwald num_slave_connections++; 15682b6ab3e6SMatthias Ringwald } 15692b6ab3e6SMatthias Ringwald log_info("Num LE Peripheral roles: %u of %u", num_slave_connections, hci_stack->le_max_number_peripheral_connections); 1570bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = num_slave_connections < hci_stack->le_max_number_peripheral_connections; 1571bbc366e6SMatthias Ringwald } else { 1572bbc366e6SMatthias Ringwald hci_stack->le_advertisements_enabled_for_current_roles = false; 15732b6ab3e6SMatthias Ringwald } 15742b6ab3e6SMatthias Ringwald } 15752b6ab3e6SMatthias Ringwald #endif 15762b6ab3e6SMatthias Ringwald #endif 15772b6ab3e6SMatthias Ringwald 157859d59ecfSMatthias Ringwald #ifdef ENABLE_CLASSIC 157959d59ecfSMatthias Ringwald static void gap_run_set_local_name(void){ 158059d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 158159d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 158259d59ecfSMatthias Ringwald // construct HCI Command and send 158359d59ecfSMatthias Ringwald uint16_t opcode = hci_write_local_name.opcode; 158459d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 158559d59ecfSMatthias Ringwald packet[0] = opcode & 0xff; 158659d59ecfSMatthias Ringwald packet[1] = opcode >> 8; 158759d59ecfSMatthias Ringwald packet[2] = DEVICE_NAME_LEN; 158859d59ecfSMatthias Ringwald memset(&packet[3], 0, DEVICE_NAME_LEN); 158959d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 159059d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, DEVICE_NAME_LEN); 159159d59ecfSMatthias Ringwald // if shorter than DEVICE_NAME_LEN, it's implicitly NULL-terminated by memset call 159259d59ecfSMatthias Ringwald (void)memcpy(&packet[3], hci_stack->local_name, bytes_to_copy); 159359d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 159459d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[3], bytes_to_copy, hci_stack->local_bd_addr); 159559d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + DEVICE_NAME_LEN); 159659d59ecfSMatthias Ringwald } 159759d59ecfSMatthias Ringwald 159859d59ecfSMatthias Ringwald static void gap_run_set_eir_data(void){ 159959d59ecfSMatthias Ringwald hci_reserve_packet_buffer(); 160059d59ecfSMatthias Ringwald uint8_t * packet = hci_stack->hci_packet_buffer; 160159d59ecfSMatthias Ringwald // construct HCI Command in-place and send 160259d59ecfSMatthias Ringwald uint16_t opcode = hci_write_extended_inquiry_response.opcode; 160359d59ecfSMatthias Ringwald hci_stack->last_cmd_opcode = opcode; 160459d59ecfSMatthias Ringwald uint16_t offset = 0; 160559d59ecfSMatthias Ringwald packet[offset++] = opcode & 0xff; 160659d59ecfSMatthias Ringwald packet[offset++] = opcode >> 8; 160759d59ecfSMatthias Ringwald packet[offset++] = 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN; 160859d59ecfSMatthias Ringwald packet[offset++] = 0; // FEC not required 160959d59ecfSMatthias Ringwald memset(&packet[offset], 0, EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 161059d59ecfSMatthias Ringwald if (hci_stack->eir_data){ 161159d59ecfSMatthias Ringwald // copy items and expand '00:00:00:00:00:00' in name with bd_addr 161259d59ecfSMatthias Ringwald ad_context_t context; 161359d59ecfSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, hci_stack->eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)) { 161459d59ecfSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 161559d59ecfSMatthias Ringwald uint8_t size = ad_iterator_get_data_len(&context); 161659d59ecfSMatthias Ringwald const uint8_t *data = ad_iterator_get_data(&context); 161759d59ecfSMatthias Ringwald // copy item 161859d59ecfSMatthias Ringwald packet[offset++] = size + 1; 161959d59ecfSMatthias Ringwald packet[offset++] = data_type; 162059d59ecfSMatthias Ringwald memcpy(&packet[offset], data, size); 162159d59ecfSMatthias Ringwald // update name item 162259d59ecfSMatthias Ringwald if ((data_type == BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME) || (data_type == BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME)){ 162359d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], size, hci_stack->local_bd_addr); 162459d59ecfSMatthias Ringwald } 162559d59ecfSMatthias Ringwald offset += size; 162659d59ecfSMatthias Ringwald } 162759d59ecfSMatthias Ringwald } else { 162859d59ecfSMatthias Ringwald uint16_t name_len = (uint16_t) strlen(hci_stack->local_name); 162959d59ecfSMatthias Ringwald uint16_t bytes_to_copy = btstack_min(name_len, EXTENDED_INQUIRY_RESPONSE_DATA_LEN - 2); 163059d59ecfSMatthias Ringwald packet[offset++] = bytes_to_copy + 1; 163159d59ecfSMatthias Ringwald packet[offset++] = BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME; 163259d59ecfSMatthias Ringwald (void)memcpy(&packet[6], hci_stack->local_name, bytes_to_copy); 163359d59ecfSMatthias Ringwald // expand '00:00:00:00:00:00' in name with bd_addr 163459d59ecfSMatthias Ringwald btstack_replace_bd_addr_placeholder(&packet[offset], bytes_to_copy, hci_stack->local_bd_addr); 163559d59ecfSMatthias Ringwald } 163659d59ecfSMatthias Ringwald hci_send_cmd_packet(packet, HCI_CMD_HEADER_SIZE + 1 + EXTENDED_INQUIRY_RESPONSE_DATA_LEN); 163759d59ecfSMatthias Ringwald } 1638ab4831a3SMatthias Ringwald 1639ab4831a3SMatthias Ringwald static void hci_run_gap_tasks_classic(void){ 1640baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_CLASS_OF_DEVICE) != 0) { 1641baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_CLASS_OF_DEVICE; 1642ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 1643ab4831a3SMatthias Ringwald return; 1644ab4831a3SMatthias Ringwald } 1645baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_LOCAL_NAME) != 0) { 1646baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_LOCAL_NAME; 1647ab4831a3SMatthias Ringwald gap_run_set_local_name(); 1648ab4831a3SMatthias Ringwald return; 1649ab4831a3SMatthias Ringwald } 1650baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_EIR_DATA) != 0) { 1651baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_EIR_DATA; 1652ab4831a3SMatthias Ringwald gap_run_set_eir_data(); 1653ab4831a3SMatthias Ringwald return; 1654ab4831a3SMatthias Ringwald } 1655baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_SET_DEFAULT_LINK_POLICY) != 0) { 1656baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_SET_DEFAULT_LINK_POLICY; 1657ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_default_link_policy_setting, hci_stack->default_link_policy_settings); 1658ab4831a3SMatthias Ringwald return; 1659ab4831a3SMatthias Ringwald } 1660ab4831a3SMatthias Ringwald // write page scan activity 1661baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY) != 0) { 1662baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 1663ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_activity, hci_stack->new_page_scan_interval, hci_stack->new_page_scan_window); 1664ab4831a3SMatthias Ringwald return; 1665ab4831a3SMatthias Ringwald } 1666ab4831a3SMatthias Ringwald // write page scan type 1667baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_SCAN_TYPE) != 0) { 1668baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_SCAN_TYPE; 1669ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_page_scan_type, hci_stack->new_page_scan_type); 1670ab4831a3SMatthias Ringwald return; 1671ab4831a3SMatthias Ringwald } 167232a12730SMatthias Ringwald // write page timeout 1673baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_PAGE_TIMEOUT) != 0) { 1674baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_PAGE_TIMEOUT; 167532a12730SMatthias Ringwald hci_send_cmd(&hci_write_page_timeout, hci_stack->page_timeout); 167632a12730SMatthias Ringwald return; 167732a12730SMatthias Ringwald } 1678ab4831a3SMatthias Ringwald // send scan enable 1679baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_SCAN_ENABLE) != 0) { 1680baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_SCAN_ENABLE; 1681ab4831a3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 1682ab4831a3SMatthias Ringwald return; 1683ab4831a3SMatthias Ringwald } 1684c0c8a829SMatthias Ringwald // send write scan activity 1685baa4881eSMatthias Ringwald if ((hci_stack->gap_tasks_classic & GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY) != 0) { 1686baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic &= ~GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 1687c0c8a829SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_scan_activity, hci_stack->inquiry_scan_interval, hci_stack->inquiry_scan_window); 1688c0c8a829SMatthias Ringwald return; 1689c0c8a829SMatthias Ringwald } 1690ab4831a3SMatthias Ringwald } 169159d59ecfSMatthias Ringwald #endif 169259d59ecfSMatthias Ringwald 16936fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 169406b9e820SMatthias Ringwald 169596b53536SMatthias Ringwald static uint32_t hci_transport_uart_get_main_baud_rate(void){ 169696b53536SMatthias Ringwald if (!hci_stack->config) return 0; 16979796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 169896b53536SMatthias Ringwald // Limit baud rate for Broadcom chipsets to 3 mbps 1699a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) && (baud_rate > 3000000)){ 170096b53536SMatthias Ringwald baud_rate = 3000000; 170196b53536SMatthias Ringwald } 170296b53536SMatthias Ringwald return baud_rate; 170396b53536SMatthias Ringwald } 170496b53536SMatthias Ringwald 1705ec820d77SMatthias Ringwald static void hci_initialization_timeout_handler(btstack_timer_source_t * ds){ 17069ec2630cSMatthias Ringwald UNUSED(ds); 17079ec2630cSMatthias Ringwald 17080305bdeaSMatthias Ringwald switch (hci_stack->substate){ 17090305bdeaSMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 17107b0d7667SMatthias Ringwald log_info("Resend HCI Reset"); 17110305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 17127b0d7667SMatthias Ringwald hci_stack->num_cmd_packets = 1; 17130305bdeaSMatthias Ringwald hci_run(); 17140305bdeaSMatthias Ringwald break; 17159f007422SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET: 17169f007422SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot with Link Reset"); 17179f007422SMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 17189f007422SMatthias Ringwald hci_stack->hci_transport->reset_link(); 17199f007422SMatthias Ringwald } 1720cf373d3aSMatthias Ringwald 1721cf373d3aSMatthias Ringwald /* fall through */ 1722cf373d3aSMatthias Ringwald 1723e47e68c7SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 1724e47e68c7SMatthias Ringwald log_info("Resend HCI Reset - CSR Warm Boot"); 1725e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1726e47e68c7SMatthias Ringwald hci_stack->num_cmd_packets = 1; 1727e47e68c7SMatthias Ringwald hci_run(); 1728688c2635SMatthias Ringwald break; 17297224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 17307224be7eSMatthias Ringwald if (hci_stack->hci_transport->set_baudrate){ 173196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1732cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(timeout handler)", baud_rate); 17337dd9d0ecSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 17347224be7eSMatthias Ringwald } 1735834bce8cSMatthias Ringwald // For CSR, HCI Reset is sent on new baud rate. Don't forget to reset link for H5/BCSP 173661f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 1737834bce8cSMatthias Ringwald if (hci_stack->hci_transport->reset_link){ 1738834bce8cSMatthias Ringwald log_info("Link Reset"); 1739834bce8cSMatthias Ringwald hci_stack->hci_transport->reset_link(); 1740834bce8cSMatthias Ringwald } 1741772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_CSR_WARM_BOOT; 1742772a36d3SMatthias Ringwald hci_run(); 1743772a36d3SMatthias Ringwald } 17444696bddbSMatthias Ringwald break; 1745559961d0SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY: 1746559961d0SMatthias Ringwald // otherwise continue 1747559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 1748559961d0SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 1749559961d0SMatthias Ringwald break; 17500305bdeaSMatthias Ringwald default: 17510305bdeaSMatthias Ringwald break; 17520305bdeaSMatthias Ringwald } 17530305bdeaSMatthias Ringwald } 175406b9e820SMatthias Ringwald #endif 17550305bdeaSMatthias Ringwald 175671de195eSMatthias Ringwald static void hci_initializing_next_state(void){ 175774b323a9SMatthias Ringwald hci_stack->substate = (hci_substate_t )( ((int) hci_stack->substate) + 1); 175874b323a9SMatthias Ringwald } 175974b323a9SMatthias Ringwald 1760f795c86eSMatthias Ringwald static void hci_init_done(void){ 1761f795c86eSMatthias Ringwald // done. tell the app 1762f795c86eSMatthias Ringwald log_info("hci_init_done -> HCI_STATE_WORKING"); 1763f795c86eSMatthias Ringwald hci_stack->state = HCI_STATE_WORKING; 1764f795c86eSMatthias Ringwald hci_emit_state(); 1765f795c86eSMatthias Ringwald } 1766f795c86eSMatthias Ringwald 176774b323a9SMatthias Ringwald // assumption: hci_can_send_command_packet_now() == true 176871de195eSMatthias Ringwald static void hci_initializing_run(void){ 1769148ca237SMatthias Ringwald log_debug("hci_initializing_run: substate %u, can send %u", hci_stack->substate, hci_can_send_command_packet_now()); 1770f4c579d4SMatthias Ringwald 17710d37aff3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 17720d37aff3SMatthias Ringwald 1773f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1774f9fd20c2SMatthias Ringwald bool need_baud_change = hci_stack->config 1775f4c579d4SMatthias Ringwald && hci_stack->chipset 1776f4c579d4SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 1777f4c579d4SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 1778f4c579d4SMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 1779f4c579d4SMatthias Ringwald #endif 1780f4c579d4SMatthias Ringwald 178174b323a9SMatthias Ringwald switch (hci_stack->substate){ 178274b323a9SMatthias Ringwald case HCI_INIT_SEND_RESET: 178374b323a9SMatthias Ringwald hci_state_reset(); 1784a0cf2f3fSMatthias Ringwald 17856fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 17860305bdeaSMatthias Ringwald // prepare reset if command complete not received in 100ms 1787659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1788528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1789528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1790a0cf2f3fSMatthias Ringwald #endif 17910305bdeaSMatthias Ringwald // send command 179274b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 17930305bdeaSMatthias Ringwald hci_send_cmd(&hci_reset); 179474b323a9SMatthias Ringwald break; 179576fcb19bSMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION: 179676fcb19bSMatthias Ringwald hci_send_cmd(&hci_read_local_version_information); 179776fcb19bSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION; 179876fcb19bSMatthias Ringwald break; 1799e28e41c0SMatthias Ringwald 1800e28e41c0SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 1801e47e68c7SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 1802e47e68c7SMatthias Ringwald hci_state_reset(); 1803e47e68c7SMatthias Ringwald // prepare reset if command complete not received in 100ms 1804659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1805528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1806528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1807e47e68c7SMatthias Ringwald // send command 1808e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 1809e47e68c7SMatthias Ringwald hci_send_cmd(&hci_reset); 1810e47e68c7SMatthias Ringwald break; 18118d29070eSMatthias Ringwald case HCI_INIT_SEND_RESET_ST_WARM_BOOT: 18128d29070eSMatthias Ringwald hci_state_reset(); 18138d29070eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT; 18148d29070eSMatthias Ringwald hci_send_cmd(&hci_reset); 18158d29070eSMatthias Ringwald break; 1816f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE_BCM: { 1817f4c579d4SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 1818f4c579d4SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1819f4c579d4SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1820f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE_BCM; 1821f4c579d4SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1822f4c579d4SMatthias Ringwald break; 1823f4c579d4SMatthias Ringwald } 1824c97af506SMatthias Ringwald case HCI_INIT_SET_BD_ADDR: 1825c97af506SMatthias Ringwald log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 1826c97af506SMatthias Ringwald hci_stack->chipset->set_bd_addr_command(hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 1827c97af506SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1828c97af506SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_BD_ADDR; 1829c97af506SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 1830c97af506SMatthias Ringwald break; 18318250c242SMatthias Ringwald case HCI_INIT_SEND_READ_LOCAL_NAME: 18328250c242SMatthias Ringwald #ifdef ENABLE_CLASSIC 18338250c242SMatthias Ringwald hci_send_cmd(&hci_read_local_name); 18348250c242SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_READ_LOCAL_NAME; 18358250c242SMatthias Ringwald break; 18368250c242SMatthias Ringwald #endif 18378250c242SMatthias Ringwald /* fall through */ 18388250c242SMatthias Ringwald 1839f4c579d4SMatthias Ringwald case HCI_INIT_SEND_BAUD_CHANGE: 1840f4c579d4SMatthias Ringwald if (need_baud_change) { 184196b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 18423fb36a29SMatthias Ringwald hci_stack->chipset->set_baudrate_command(baud_rate, hci_stack->hci_packet_buffer); 1843f8fbdce0SMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 184474b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 18454ea43905SMatthias Ringwald hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3u + hci_stack->hci_packet_buffer[2u]); 18464696bddbSMatthias Ringwald // STLC25000D: baudrate change happens within 0.5 s after command was send, 18474696bddbSMatthias Ringwald // use timer to update baud rate after 100 ms (knowing exactly, when command was sent is non-trivial) 184861f37892SMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS){ 1849659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1850528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 18514696bddbSMatthias Ringwald } 185274b323a9SMatthias Ringwald break; 1853fab26ab3SMatthias Ringwald } 1854f4c579d4SMatthias Ringwald 1855f4c579d4SMatthias Ringwald /* fall through */ 1856f4c579d4SMatthias Ringwald 185774b323a9SMatthias Ringwald case HCI_INIT_CUSTOM_INIT: 185874b323a9SMatthias Ringwald // Custom initialization 18593fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->next_command){ 1860ae334e9eSMatthias Ringwald hci_stack->chipset_result = (*hci_stack->chipset->next_command)(hci_stack->hci_packet_buffer); 18611979f09cSMatthias Ringwald bool send_cmd = false; 1862ae334e9eSMatthias Ringwald switch (hci_stack->chipset_result){ 1863f41911edSMatthias Ringwald case BTSTACK_CHIPSET_VALID_COMMAND: 18641979f09cSMatthias Ringwald send_cmd = true; 1865e47e68c7SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT; 1866e47e68c7SMatthias Ringwald break; 1867f41911edSMatthias Ringwald case BTSTACK_CHIPSET_WARMSTART_REQUIRED: 18681979f09cSMatthias Ringwald send_cmd = true; 1869f41911edSMatthias Ringwald // CSR Warm Boot: Wait a bit, then send HCI Reset until HCI Command Complete 1870e47e68c7SMatthias Ringwald log_info("CSR Warm Boot"); 1871659d758cSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, HCI_RESET_RESEND_TIMEOUT_MS); 1872528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1873528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1874a1df452eSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO) 1875772a36d3SMatthias Ringwald && hci_stack->config 18763fb36a29SMatthias Ringwald && hci_stack->chipset 18773fb36a29SMatthias Ringwald // && hci_stack->chipset->set_baudrate_command -- there's no such command 1878772a36d3SMatthias Ringwald && hci_stack->hci_transport->set_baudrate 18792caefae9SMatthias Ringwald && hci_transport_uart_get_main_baud_rate()){ 1880772a36d3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_BAUD_CHANGE; 1881772a36d3SMatthias Ringwald } else { 18829f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT_LINK_RESET; 1883772a36d3SMatthias Ringwald } 1884e47e68c7SMatthias Ringwald break; 1885f41911edSMatthias Ringwald default: 1886f41911edSMatthias Ringwald break; 1887e47e68c7SMatthias Ringwald } 1888ee720f3aSMatthias Ringwald 1889ee720f3aSMatthias Ringwald if (send_cmd){ 18904ea43905SMatthias Ringwald int size = 3u + hci_stack->hci_packet_buffer[2u]; 1891ee720f3aSMatthias Ringwald hci_stack->last_cmd_opcode = little_endian_read_16(hci_stack->hci_packet_buffer, 0); 1892ee720f3aSMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 18930305bdeaSMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 189474b323a9SMatthias Ringwald break; 189574b323a9SMatthias Ringwald } 1896148ca237SMatthias Ringwald log_info("Init script done"); 189792a0d36dSMatthias Ringwald 1898559961d0SMatthias Ringwald // Init script download on Broadcom chipsets causes: 1899ae334e9eSMatthias Ringwald if ( (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 1900a1df452eSMatthias Ringwald ( (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) 1901a1df452eSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA)) ){ 1902e021ff1eSMatthias Ringwald 1903559961d0SMatthias Ringwald // - baud rate to reset, restore UART baud rate if needed 190492a0d36dSMatthias Ringwald if (need_baud_change) { 19059796ebeaSMatthias Ringwald uint32_t baud_rate = ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_init; 1906cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 " after init script (bcm)", baud_rate); 190792a0d36dSMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 190892a0d36dSMatthias Ringwald } 1909559961d0SMatthias Ringwald 1910f19b3c9eSMatthias Ringwald uint16_t bcm_delay_ms = 300; 1911f19b3c9eSMatthias Ringwald // - UART may or may not be disabled during update and Controller RTS may or may not be high during this time 1912f19b3c9eSMatthias Ringwald // -> Work around: wait here. 1913f19b3c9eSMatthias Ringwald log_info("BCM delay (%u ms) after init script", bcm_delay_ms); 1914559961d0SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_BCM_DELAY; 1915f19b3c9eSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, bcm_delay_ms); 1916559961d0SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_initialization_timeout_handler); 1917559961d0SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 1918559961d0SMatthias Ringwald break; 191992a0d36dSMatthias Ringwald } 192074b323a9SMatthias Ringwald } 192106b9e820SMatthias Ringwald #endif 1922521bd5ffSMatthias Ringwald /* fall through */ 192306b9e820SMatthias Ringwald 192406b9e820SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS: 192506b9e820SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS; 192606b9e820SMatthias Ringwald hci_send_cmd(&hci_read_local_supported_commands); 192706b9e820SMatthias Ringwald break; 192853860077SMatthias Ringwald case HCI_INIT_READ_BD_ADDR: 192953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BD_ADDR; 193053860077SMatthias Ringwald hci_send_cmd(&hci_read_bd_addr); 193153860077SMatthias Ringwald break; 193274b323a9SMatthias Ringwald case HCI_INIT_READ_BUFFER_SIZE: 19335ffe9d0bSMatthias Ringwald // only read buffer size if supported 19341ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_BUFFER_SIZE)){ 193574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_BUFFER_SIZE; 19360305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_buffer_size); 193774b323a9SMatthias Ringwald break; 19385ffe9d0bSMatthias Ringwald } 1939521bd5ffSMatthias Ringwald 19405ffe9d0bSMatthias Ringwald /* fall through */ 19415ffe9d0bSMatthias Ringwald 194253860077SMatthias Ringwald case HCI_INIT_READ_LOCAL_SUPPORTED_FEATURES: 194353860077SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_LOCAL_SUPPORTED_FEATURES; 19440305bdeaSMatthias Ringwald hci_send_cmd(&hci_read_local_supported_features); 194574b323a9SMatthias Ringwald break; 19462b838201SMatthias Ringwald 19472b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 19482b838201SMatthias Ringwald case HCI_INIT_SET_CONTROLLER_TO_HOST_FLOW_CONTROL: 19492b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_CONTROLLER_TO_HOST_FLOW_CONTROL; 19502b838201SMatthias Ringwald hci_send_cmd(&hci_set_controller_to_host_flow_control, 3); // ACL + SCO Flow Control 19512b838201SMatthias Ringwald break; 19522b838201SMatthias Ringwald case HCI_INIT_HOST_BUFFER_SIZE: 19532b838201SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_HOST_BUFFER_SIZE; 19542b838201SMatthias Ringwald hci_send_cmd(&hci_host_buffer_size, HCI_HOST_ACL_PACKET_LEN, HCI_HOST_SCO_PACKET_LEN, 19552b838201SMatthias Ringwald HCI_HOST_ACL_PACKET_NUM, HCI_HOST_SCO_PACKET_NUM); 19562b838201SMatthias Ringwald break; 19572b838201SMatthias Ringwald #endif 19582b838201SMatthias Ringwald 195974b323a9SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK: 19600305bdeaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK; 196174b323a9SMatthias Ringwald if (hci_le_supported()){ 19625ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x3FFFFFFFU); 196374b323a9SMatthias Ringwald } else { 196474b323a9SMatthias Ringwald // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 19655ce1359eSMatthias Ringwald hci_send_cmd(&hci_set_event_mask,0xFFFFFFFFU, 0x1FFFFFFFU); 196674b323a9SMatthias Ringwald } 196774b323a9SMatthias Ringwald break; 19682b838201SMatthias Ringwald 1969ff7d1758SMatthias Ringwald case HCI_INIT_SET_EVENT_MASK_2: 19708254e329SMatthias Ringwald // On Bluettooth PTS dongle (BL 654) with PacketCraft HCI Firmware (LMP subversion) 0x5244, 19718254e329SMatthias Ringwald // setting Event Mask 2 causes Controller to drop Encryption Change events. 19728254e329SMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_SET_EVENT_MASK_PAGE_2) 19738254e329SMatthias Ringwald && (hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_PACKETCRAFT_INC)){ 1974ff7d1758SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_EVENT_MASK_2; 1975ff7d1758SMatthias Ringwald // Encryption Change Event v2 - bit 25 1976ff7d1758SMatthias Ringwald hci_send_cmd(&hci_set_event_mask_2,0x02000000U, 0x0); 1977ff7d1758SMatthias Ringwald break; 1978ff7d1758SMatthias Ringwald } 1979ff7d1758SMatthias Ringwald 198035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 1981ff7d1758SMatthias Ringwald /* fall through */ 1982ff7d1758SMatthias Ringwald 198374b323a9SMatthias Ringwald case HCI_INIT_WRITE_SIMPLE_PAIRING_MODE: 1984f795c86eSMatthias Ringwald if (hci_classic_supported() && gap_ssp_supported()){ 198574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SIMPLE_PAIRING_MODE; 19860305bdeaSMatthias Ringwald hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 198774b323a9SMatthias Ringwald break; 1988e7c662faSMatthias Ringwald } 1989521bd5ffSMatthias Ringwald 1990e7c662faSMatthias Ringwald /* fall through */ 1991e7c662faSMatthias Ringwald 1992f6858d14SMatthias Ringwald case HCI_INIT_WRITE_INQUIRY_MODE: 1993f795c86eSMatthias Ringwald if (hci_classic_supported()){ 1994f6858d14SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_INQUIRY_MODE; 19958a114470SMatthias Ringwald hci_send_cmd(&hci_write_inquiry_mode, (int) hci_stack->inquiry_mode); 1996f6858d14SMatthias Ringwald break; 1997f795c86eSMatthias Ringwald } 1998521bd5ffSMatthias Ringwald 1999f795c86eSMatthias Ringwald /* fall through */ 2000f795c86eSMatthias Ringwald 20015d23aae8SMatthias Ringwald case HCI_INIT_WRITE_SECURE_CONNECTIONS_HOST_ENABLE: 2002f3052906SMatthias Ringwald // skip write secure connections host support if not supported or disabled 20031ffa425cSMatthias Ringwald if (hci_classic_supported() && hci_stack->secure_connections_enable 20041ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SECURE_CONNECTIONS_HOST)) { 2005c214d65bSMatthias Ringwald hci_stack->secure_connections_active = true; 20065d23aae8SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SECURE_CONNECTIONS_HOST_ENABLE; 20071ffa425cSMatthias Ringwald hci_send_cmd(&hci_write_secure_connections_host_support, 1); 20085d23aae8SMatthias Ringwald break; 2009f3052906SMatthias Ringwald } 2010521bd5ffSMatthias Ringwald 2011a391128dSMatthias Ringwald /* fall through */ 2012a391128dSMatthias Ringwald 2013a391128dSMatthias Ringwald case HCI_INIT_SET_MIN_ENCRYPTION_KEY_SIZE: 2014a391128dSMatthias Ringwald // skip set min encryption key size 2015a391128dSMatthias Ringwald if (hci_classic_supported() && hci_command_supported(SUPPORTED_HCI_COMMAND_SET_MIN_ENCRYPTION_KEY_SIZE)) { 2016a391128dSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SET_MIN_ENCRYPTION_KEY_SIZE; 2017a391128dSMatthias Ringwald hci_send_cmd(&hci_set_min_encryption_key_size, hci_stack->gap_required_encyrption_key_size); 2018a391128dSMatthias Ringwald break; 2019a391128dSMatthias Ringwald } 2020a391128dSMatthias Ringwald 2021e4c6114dSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2022521bd5ffSMatthias Ringwald /* fall through */ 2023521bd5ffSMatthias Ringwald 2024483c5078SMatthias Ringwald // only sent if ENABLE_SCO_OVER_HCI is defined 2025729ed62eSMatthias Ringwald case HCI_INIT_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 2026e4c6114dSMatthias Ringwald // skip write synchronous flow control if not supported 20271ffa425cSMatthias Ringwald if (hci_classic_supported() 20281ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE)) { 2029729ed62eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE; 2030729ed62eSMatthias Ringwald hci_send_cmd(&hci_write_synchronous_flow_control_enable, 1); // SCO tracking enabled 2031729ed62eSMatthias Ringwald break; 2032f795c86eSMatthias Ringwald } 2033f795c86eSMatthias Ringwald /* fall through */ 2034f795c86eSMatthias Ringwald 2035483c5078SMatthias Ringwald case HCI_INIT_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING: 2036e4c6114dSMatthias Ringwald // skip write default erroneous data reporting if not supported 20371ffa425cSMatthias Ringwald if (hci_classic_supported() 20381ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING)) { 2039483c5078SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_DEFAULT_ERRONEOUS_DATA_REPORTING; 2040483c5078SMatthias Ringwald hci_send_cmd(&hci_write_default_erroneous_data_reporting, 1); 2041483c5078SMatthias Ringwald break; 2042f795c86eSMatthias Ringwald } 2043e4c6114dSMatthias Ringwald #endif 2044f795c86eSMatthias Ringwald 2045f795c86eSMatthias Ringwald #if defined(ENABLE_SCO_OVER_HCI) || defined(ENABLE_SCO_OVER_PCM) 2046521bd5ffSMatthias Ringwald /* fall through */ 2047521bd5ffSMatthias Ringwald 20488051253fSMatthias Ringwald // only sent if manufacturer is Broadcom and ENABLE_SCO_OVER_HCI or ENABLE_SCO_OVER_PCM is defined 2049a42798c3SMatthias Ringwald case HCI_INIT_BCM_WRITE_SCO_PCM_INT: 2050e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 2051a42798c3SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_SCO_PCM_INT; 20528051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 2053a42798c3SMatthias Ringwald log_info("BCM: Route SCO data via HCI transport"); 2054a42798c3SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 1, 0, 0, 0, 0); 20558051253fSMatthias Ringwald #endif 20568051253fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 20578051253fSMatthias Ringwald log_info("BCM: Route SCO data via PCM interface"); 20581d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 2059f795c86eSMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 16 kHz 20601d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 2, 0, 1, 1); 20611d2bbd54SMatthias Ringwald #else 20621d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 20631d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_sco_pcm_int, 0, 1, 0, 1, 1); 20641d2bbd54SMatthias Ringwald #endif 20658051253fSMatthias Ringwald #endif 2066a42798c3SMatthias Ringwald break; 2067f795c86eSMatthias Ringwald } 2068f795c86eSMatthias Ringwald #endif 2069f795c86eSMatthias Ringwald 20704e821764SMatthias Ringwald #ifdef ENABLE_SCO_OVER_PCM 2071521bd5ffSMatthias Ringwald /* fall through */ 2072521bd5ffSMatthias Ringwald 20734e821764SMatthias Ringwald case HCI_INIT_BCM_WRITE_I2SPCM_INTERFACE_PARAM: 2074e4c6114dSMatthias Ringwald if (hci_classic_supported() && (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION)){ 20754e821764SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_BCM_WRITE_I2SPCM_INTERFACE_PARAM; 20764e821764SMatthias Ringwald log_info("BCM: Config PCM interface for I2S"); 20771d2bbd54SMatthias Ringwald #ifdef ENABLE_BCM_PCM_WBS 20781d2bbd54SMatthias Ringwald // 512 kHz bit clock for 2 channels x 16 bit x 8 kHz 20791d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 2); 20801d2bbd54SMatthias Ringwald #else 20811d2bbd54SMatthias Ringwald // 256 kHz bit clock for 2 channels x 16 bit x 8 kHz 20821d2bbd54SMatthias Ringwald hci_send_cmd(&hci_bcm_write_i2spcm_interface_param, 1, 1, 0, 1); 20831d2bbd54SMatthias Ringwald #endif 20844e821764SMatthias Ringwald break; 2085f795c86eSMatthias Ringwald } 208635454696SMatthias Ringwald #endif 20874e821764SMatthias Ringwald #endif 20884e821764SMatthias Ringwald 2089a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 2090521bd5ffSMatthias Ringwald /* fall through */ 2091521bd5ffSMatthias Ringwald 209274b323a9SMatthias Ringwald // LE INIT 209374b323a9SMatthias Ringwald case HCI_INIT_LE_READ_BUFFER_SIZE: 2094f795c86eSMatthias Ringwald if (hci_le_supported()){ 209574b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_BUFFER_SIZE; 209679b7ea2dSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_BUFFER_SIZE_V2)){ 209779b7ea2dSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size_v2); 209879b7ea2dSMatthias Ringwald } else { 20990305bdeaSMatthias Ringwald hci_send_cmd(&hci_le_read_buffer_size); 210079b7ea2dSMatthias Ringwald } 210174b323a9SMatthias Ringwald break; 2102f795c86eSMatthias Ringwald } 2103521bd5ffSMatthias Ringwald 2104f795c86eSMatthias Ringwald /* fall through */ 2105f795c86eSMatthias Ringwald 2106699a5fcaSMatthias Ringwald case HCI_INIT_WRITE_LE_HOST_SUPPORTED: 2107699a5fcaSMatthias Ringwald // skip write le host if not supported (e.g. on LE only EM9301) 21081ffa425cSMatthias Ringwald if (hci_le_supported() 21091ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_WRITE_LE_HOST_SUPPORTED)) { 2110699a5fcaSMatthias Ringwald // LE Supported Host = 1, Simultaneous Host = 0 2111699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_WRITE_LE_HOST_SUPPORTED; 2112699a5fcaSMatthias Ringwald hci_send_cmd(&hci_write_le_host_supported, 1, 0); 2113daabb8b8SMatthias Ringwald break; 2114f795c86eSMatthias Ringwald } 2115521bd5ffSMatthias Ringwald 2116f795c86eSMatthias Ringwald /* fall through */ 2117f795c86eSMatthias Ringwald 2118699a5fcaSMatthias Ringwald case HCI_INIT_LE_SET_EVENT_MASK: 2119f795c86eSMatthias Ringwald if (hci_le_supported()){ 2120699a5fcaSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_EVENT_MASK; 212191d9e5d0SMatthias Ringwald hci_send_cmd(&hci_le_set_event_mask, 0xfffffdff, 0x07); // all events from core v5.3 without LE Enhanced Connection Complete 212274b323a9SMatthias Ringwald break; 2123f795c86eSMatthias Ringwald } 2124b435e062SMatthias Ringwald #endif 2125b435e062SMatthias Ringwald 2126b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2127521bd5ffSMatthias Ringwald /* fall through */ 2128521bd5ffSMatthias Ringwald 2129dcd678baSMatthias Ringwald case HCI_INIT_LE_READ_MAX_DATA_LENGTH: 21301ffa425cSMatthias Ringwald if (hci_le_supported() 21311ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_READ_MAXIMUM_DATA_LENGTH)) { 2132dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_DATA_LENGTH; 2133dcd678baSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_data_length); 2134dcd678baSMatthias Ringwald break; 2135f795c86eSMatthias Ringwald } 2136521bd5ffSMatthias Ringwald 2137f795c86eSMatthias Ringwald /* fall through */ 2138f795c86eSMatthias Ringwald 2139dcd678baSMatthias Ringwald case HCI_INIT_LE_WRITE_SUGGESTED_DATA_LENGTH: 21401ffa425cSMatthias Ringwald if (hci_le_supported() 21411ffa425cSMatthias Ringwald && hci_command_supported(SUPPORTED_HCI_COMMAND_LE_WRITE_SUGGESTED_DEFAULT_DATA_LENGTH)) { 2142dcd678baSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_WRITE_SUGGESTED_DATA_LENGTH; 2143b435e062SMatthias 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); 2144dcd678baSMatthias Ringwald break; 2145f795c86eSMatthias Ringwald } 2146b435e062SMatthias Ringwald #endif 2147b435e062SMatthias Ringwald 2148b95a5a35SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2149521bd5ffSMatthias Ringwald /* fall through */ 2150521bd5ffSMatthias Ringwald 21513b6d4121SMatthias Ringwald case HCI_INIT_READ_WHITE_LIST_SIZE: 2152f795c86eSMatthias Ringwald if (hci_le_supported()){ 21533b6d4121SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_READ_WHITE_LIST_SIZE; 21543b6d4121SMatthias Ringwald hci_send_cmd(&hci_le_read_white_list_size); 21553b6d4121SMatthias Ringwald break; 2156f795c86eSMatthias Ringwald } 2157521bd5ffSMatthias Ringwald 215874b323a9SMatthias Ringwald #endif 21594f982f31SMatthias Ringwald 21603a74541eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 21613a74541eSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 21623a74541eSMatthias Ringwald /* fall through */ 21633a74541eSMatthias Ringwald 21643a74541eSMatthias Ringwald case HCI_INIT_LE_READ_MAX_ADV_DATA_LEN: 21653a74541eSMatthias Ringwald if (hci_extended_advertising_supported()){ 21663a74541eSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_READ_MAX_ADV_DATA_LEN; 21673a74541eSMatthias Ringwald hci_send_cmd(&hci_le_read_maximum_advertising_data_length); 21683a74541eSMatthias Ringwald break; 21693a74541eSMatthias Ringwald } 21703a74541eSMatthias Ringwald #endif 21713a74541eSMatthias Ringwald #endif 2172521bd5ffSMatthias Ringwald /* fall through */ 2173521bd5ffSMatthias Ringwald 2174c63ee49aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 2175c63ee49aSMatthias Ringwald case HCI_INIT_LE_SET_HOST_FEATURE_CONNECTED_ISO_STREAMS: 2176c63ee49aSMatthias Ringwald if (hci_le_supported()) { 2177c63ee49aSMatthias Ringwald hci_stack->substate = HCI_INIT_W4_LE_SET_HOST_FEATURE_CONNECTED_ISO_STREAMS; 2178c63ee49aSMatthias Ringwald hci_send_cmd(&hci_le_set_host_feature, 32, 1); 2179c63ee49aSMatthias Ringwald break; 2180c63ee49aSMatthias Ringwald } 2181c63ee49aSMatthias Ringwald #endif 2182c63ee49aSMatthias Ringwald 2183c63ee49aSMatthias Ringwald /* fall through */ 2184c63ee49aSMatthias Ringwald 2185f795c86eSMatthias Ringwald case HCI_INIT_DONE: 21864f982f31SMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 218773799937SMatthias Ringwald // main init sequence complete 21884f982f31SMatthias Ringwald #ifdef ENABLE_CLASSIC 218973799937SMatthias Ringwald // check if initial Classic GAP Tasks are completed 2190baa4881eSMatthias Ringwald if (hci_classic_supported() && (hci_stack->gap_tasks_classic != 0)) { 21914f982f31SMatthias Ringwald hci_run_gap_tasks_classic(); 21924f982f31SMatthias Ringwald break; 21934f982f31SMatthias Ringwald } 21944f982f31SMatthias Ringwald #endif 219573799937SMatthias Ringwald #ifdef ENABLE_BLE 219673799937SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 219773799937SMatthias Ringwald // check if initial LE GAP Tasks are completed 219873799937SMatthias Ringwald if (hci_le_supported() && hci_stack->le_scanning_param_update) { 219973799937SMatthias Ringwald hci_run_general_gap_le(); 220073799937SMatthias Ringwald break; 220173799937SMatthias Ringwald } 220273799937SMatthias Ringwald #endif 220373799937SMatthias Ringwald #endif 2204f795c86eSMatthias Ringwald hci_init_done(); 2205f795c86eSMatthias Ringwald break; 2206f795c86eSMatthias Ringwald 220774b323a9SMatthias Ringwald default: 220874b323a9SMatthias Ringwald return; 220974b323a9SMatthias Ringwald } 221055975f88SMatthias Ringwald } 221155975f88SMatthias Ringwald 221207fd2f31SMatthias Ringwald static bool hci_initializing_event_handler_command_completed(const uint8_t * packet){ 221307fd2f31SMatthias Ringwald bool command_completed = false; 22140e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE){ 2215f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 22166155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 221707fd2f31SMatthias Ringwald command_completed = true; 2218148ca237SMatthias Ringwald log_debug("Command complete for expected opcode %04x at substate %u", opcode, hci_stack->substate); 22196155b3d3S[email protected] } else { 2220d58dd308SMatthias Ringwald log_info("Command complete for different opcode %04x, expected %04x, at substate %u", opcode, hci_stack->last_cmd_opcode, hci_stack->substate); 22216155b3d3S[email protected] } 22226155b3d3S[email protected] } 22230f97eae7SMatthias Ringwald 22240e2df43fSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_STATUS){ 22256155b3d3S[email protected] uint8_t status = packet[2]; 2226f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,4); 22276155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 22286155b3d3S[email protected] if (status){ 222907fd2f31SMatthias Ringwald command_completed = true; 2230148ca237SMatthias Ringwald log_debug("Command status error 0x%02x for expected opcode %04x at substate %u", status, opcode, hci_stack->substate); 22316155b3d3S[email protected] } else { 22326155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 22336155b3d3S[email protected] } 22346155b3d3S[email protected] } else { 2235148ca237SMatthias Ringwald log_debug("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 22366155b3d3S[email protected] } 22376155b3d3S[email protected] } 22386fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2239e47e68c7SMatthias Ringwald // Vendor == CSR 22400e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_CUSTOM_INIT) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 2241e47e68c7SMatthias Ringwald // TODO: track actual command 224207fd2f31SMatthias Ringwald command_completed = true; 2243e47e68c7SMatthias Ringwald } 2244a2481739S[email protected] 22454e9daa6fSMatthias Ringwald // Vendor == Toshiba 22460e588213SMatthias Ringwald if ((hci_stack->substate == HCI_INIT_W4_SEND_BAUD_CHANGE) && (hci_event_packet_get_type(packet) == HCI_EVENT_VENDOR_SPECIFIC)){ 22474e9daa6fSMatthias Ringwald // TODO: track actual command 224807fd2f31SMatthias Ringwald command_completed = true; 2249004902f1SMatthias Ringwald // Fix: no HCI Command Complete received, so num_cmd_packets not reset 2250004902f1SMatthias Ringwald hci_stack->num_cmd_packets = 1; 22514e9daa6fSMatthias Ringwald } 225207fd2f31SMatthias Ringwald #endif 225307fd2f31SMatthias Ringwald 225407fd2f31SMatthias Ringwald return command_completed; 225507fd2f31SMatthias Ringwald } 225607fd2f31SMatthias Ringwald 225707fd2f31SMatthias Ringwald static void hci_initializing_event_handler(const uint8_t * packet, uint16_t size){ 225807fd2f31SMatthias Ringwald 225907fd2f31SMatthias Ringwald UNUSED(size); // ok: less than 6 bytes are read from our buffer 226007fd2f31SMatthias Ringwald 226107fd2f31SMatthias Ringwald bool command_completed = hci_initializing_event_handler_command_completed(packet); 226207fd2f31SMatthias Ringwald 22636fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 22644e9daa6fSMatthias Ringwald 22650f97eae7SMatthias Ringwald // Late response (> 100 ms) for HCI Reset e.g. on Toshiba TC35661: 22660f97eae7SMatthias Ringwald // Command complete for HCI Reset arrives after we've resent the HCI Reset command 22670f97eae7SMatthias Ringwald // 22680f97eae7SMatthias Ringwald // HCI Reset 22690f97eae7SMatthias Ringwald // Timeout 100 ms 22700f97eae7SMatthias Ringwald // HCI Reset 22710f97eae7SMatthias Ringwald // Command Complete Reset 22720f97eae7SMatthias Ringwald // HCI Read Local Version Information 22730f97eae7SMatthias Ringwald // Command Complete Reset - but we expected Command Complete Read Local Version Information 22740f97eae7SMatthias Ringwald // hang... 22750f97eae7SMatthias Ringwald // 22760f97eae7SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 22770f97eae7SMatthias Ringwald if (!command_completed 2278a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 22790e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION)){ 22800f97eae7SMatthias Ringwald 2281f8fbdce0SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 22820f97eae7SMatthias Ringwald if (opcode == hci_reset.opcode){ 22830f97eae7SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 22840f97eae7SMatthias Ringwald return; 22850f97eae7SMatthias Ringwald } 22860f97eae7SMatthias Ringwald } 22870f97eae7SMatthias Ringwald 22889f007422SMatthias Ringwald // CSR & H5 22899f007422SMatthias Ringwald // Fix: Command Complete for HCI Reset in HCI_INIT_W4_SEND_READ_LOCAL_VERSION_INFORMATION trigger resend 22909f007422SMatthias Ringwald if (!command_completed 2291a1df452eSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 22920e588213SMatthias Ringwald && (hci_stack->substate == HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS)){ 22939f007422SMatthias Ringwald 22949f007422SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet,3); 22959f007422SMatthias Ringwald if (opcode == hci_reset.opcode){ 22969f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_LOCAL_SUPPORTED_COMMANDS; 22979f007422SMatthias Ringwald return; 22989f007422SMatthias Ringwald } 22999f007422SMatthias Ringwald } 23009f007422SMatthias Ringwald 23019f007422SMatthias 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 23029f007422SMatthias Ringwald // fix: Correct substate and behave as command below 23039f007422SMatthias Ringwald if (command_completed){ 23049f007422SMatthias Ringwald switch (hci_stack->substate){ 23059f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 23069f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_SEND_RESET; 23079f007422SMatthias Ringwald break; 23089f007422SMatthias Ringwald case HCI_INIT_SEND_RESET_CSR_WARM_BOOT: 23099f007422SMatthias Ringwald hci_stack->substate = HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT; 23109f007422SMatthias Ringwald break; 23119f007422SMatthias Ringwald default: 23129f007422SMatthias Ringwald break; 23139f007422SMatthias Ringwald } 23149f007422SMatthias Ringwald } 23150f97eae7SMatthias Ringwald 231606b9e820SMatthias Ringwald #endif 23170f97eae7SMatthias Ringwald 2318a2481739S[email protected] if (!command_completed) return; 2319a2481739S[email protected] 232007fd2f31SMatthias Ringwald bool need_baud_change = false; 232107fd2f31SMatthias Ringwald bool need_addr_change = false; 232206b9e820SMatthias Ringwald 23236fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 232406b9e820SMatthias Ringwald need_baud_change = hci_stack->config 23253fb36a29SMatthias Ringwald && hci_stack->chipset 23263fb36a29SMatthias Ringwald && hci_stack->chipset->set_baudrate_command 2327db8bc6ffSMatthias Ringwald && hci_stack->hci_transport->set_baudrate 23289796ebeaSMatthias Ringwald && ((hci_transport_config_uart_t *)hci_stack->config)->baudrate_main; 2329db8bc6ffSMatthias Ringwald 233006b9e820SMatthias Ringwald need_addr_change = hci_stack->custom_bd_addr_set 23313fb36a29SMatthias Ringwald && hci_stack->chipset 23323fb36a29SMatthias Ringwald && hci_stack->chipset->set_bd_addr_command; 233306b9e820SMatthias Ringwald #endif 2334a80162e9SMatthias Ringwald 23355c363727SMatthias Ringwald switch(hci_stack->substate){ 233606b9e820SMatthias Ringwald 23376fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 23389f007422SMatthias Ringwald case HCI_INIT_SEND_RESET: 2339d58dd308SMatthias Ringwald // on CSR with BCSP/H5, resend triggers resend of HCI Reset and leads to substate == HCI_INIT_SEND_RESET 23409f007422SMatthias Ringwald // fix: just correct substate and behave as command below 2341f4c579d4SMatthias Ringwald 2342f4c579d4SMatthias Ringwald /* fall through */ 2343f4c579d4SMatthias Ringwald #endif 2344f4c579d4SMatthias Ringwald 234574b323a9SMatthias Ringwald case HCI_INIT_W4_SEND_RESET: 2346528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2347f4c579d4SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_READ_LOCAL_VERSION_INFORMATION; 23482f48d920SMatthias Ringwald return; 2349f4c579d4SMatthias Ringwald 2350f4c579d4SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 2351a80162e9SMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE: 23524696bddbSMatthias Ringwald // for STLC2500D, baud rate change already happened. 2353fab26ab3SMatthias Ringwald // for others, baud rate gets changed now 235461f37892SMatthias Ringwald if ((hci_stack->manufacturer != BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) && need_baud_change){ 235596b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2356cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change)", baud_rate); 2357fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 23584696bddbSMatthias Ringwald } 235974b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 236074b323a9SMatthias Ringwald return; 2361a80162e9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT_CSR_WARM_BOOT: 2362528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 2363a80162e9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 2364a80162e9SMatthias Ringwald return; 236574b323a9SMatthias Ringwald case HCI_INIT_W4_CUSTOM_INIT: 236674b323a9SMatthias Ringwald // repeat custom init 236774b323a9SMatthias Ringwald hci_stack->substate = HCI_INIT_CUSTOM_INIT; 236874b323a9SMatthias Ringwald return; 236906b9e820SMatthias Ringwald #endif 237006b9e820SMatthias Ringwald 2371a828a756SMatthias Ringwald case HCI_INIT_W4_READ_LOCAL_SUPPORTED_COMMANDS: 23720e588213SMatthias Ringwald if (need_baud_change && (hci_stack->chipset_result != BTSTACK_CHIPSET_NO_INIT_SCRIPT) && 2373efd3b327SMatthias Ringwald ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION) || 2374efd3b327SMatthias Ringwald (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_EM_MICROELECTRONIC_MARIN_SA))) { 2375eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_BAUD_CHANGE_BCM; 2376eb3a5314SMatthias Ringwald return; 2377eb3a5314SMatthias Ringwald } 237853860077SMatthias Ringwald if (need_addr_change){ 237953860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 238053860077SMatthias Ringwald return; 238153860077SMatthias Ringwald } 238253860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 238353860077SMatthias Ringwald return; 23846fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 23857224be7eSMatthias Ringwald case HCI_INIT_W4_SEND_BAUD_CHANGE_BCM: 23867224be7eSMatthias Ringwald if (need_baud_change){ 238796b53536SMatthias Ringwald uint32_t baud_rate = hci_transport_uart_get_main_baud_rate(); 2388cd724cb7SMatthias Ringwald log_info("Local baud rate change to %" PRIu32 "(w4_send_baud_change_bcm))", baud_rate); 2389fab26ab3SMatthias Ringwald hci_stack->hci_transport->set_baudrate(baud_rate); 23907224be7eSMatthias Ringwald } 2391eb3a5314SMatthias Ringwald if (need_addr_change){ 2392eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_SET_BD_ADDR; 2393eb3a5314SMatthias Ringwald return; 2394eb3a5314SMatthias Ringwald } 2395eb3a5314SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 2396eb3a5314SMatthias Ringwald return; 239753860077SMatthias Ringwald case HCI_INIT_W4_SET_BD_ADDR: 23986ca9a99aSMatthias Ringwald // for STLC2500D + ATWILC3000, bd addr change only gets active after sending reset command 23996ca9a99aSMatthias Ringwald if ((hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ST_MICROELECTRONICS) 24006ca9a99aSMatthias Ringwald || (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_ATMEL_CORPORATION)){ 240153860077SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET_ST_WARM_BOOT; 240253860077SMatthias Ringwald return; 240353860077SMatthias Ringwald } 240453860077SMatthias Ringwald // skipping st warm boot 240553860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 240653860077SMatthias Ringwald return; 240753860077SMatthias Ringwald case HCI_INIT_W4_SEND_RESET_ST_WARM_BOOT: 240853860077SMatthias Ringwald hci_stack->substate = HCI_INIT_READ_BD_ADDR; 240953860077SMatthias Ringwald return; 241006b9e820SMatthias Ringwald #endif 2411e7c662faSMatthias Ringwald 2412f795c86eSMatthias Ringwald case HCI_INIT_DONE: 2413eb8d95caSMatthias Ringwald // set state if we came here by fall through 2414eb8d95caSMatthias Ringwald hci_stack->substate = HCI_INIT_DONE; 2415f795c86eSMatthias Ringwald return; 2416a650ba4dSMatthias Ringwald 24176155b3d3S[email protected] default: 241874b323a9SMatthias Ringwald break; 24196155b3d3S[email protected] } 242055975f88SMatthias Ringwald hci_initializing_next_state(); 24216155b3d3S[email protected] } 24226155b3d3S[email protected] 24230bbba85bSMatthias Ringwald static void hci_handle_connection_failed(hci_connection_t * conn, uint8_t status){ 24243bb5ff27SMatthias Ringwald // CC2564C might emit Connection Complete for rejected incoming SCO connection 24251a4fdac4SMatthias Ringwald // To prevent accidentally free'ing the HCI connection for the ACL connection, 24261a4fdac4SMatthias Ringwald // check if we have been aware of the HCI connection 24271f34df2cSMatthias Ringwald switch (conn->state){ 24281a4fdac4SMatthias Ringwald case SENT_CREATE_CONNECTION: 24291f34df2cSMatthias Ringwald case RECEIVED_CONNECTION_REQUEST: 24301f34df2cSMatthias Ringwald break; 24311f34df2cSMatthias Ringwald default: 24321f34df2cSMatthias Ringwald return; 24331f34df2cSMatthias Ringwald } 24343bb5ff27SMatthias Ringwald 2435229331c6SMatthias Ringwald log_info("Outgoing connection to %s failed", bd_addr_to_str(conn->address)); 24360bbba85bSMatthias Ringwald bd_addr_t bd_address; 24376535961aSMatthias Ringwald (void)memcpy(&bd_address, conn->address, 6); 24380bbba85bSMatthias Ringwald 24396bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 24406bc9fa5eSMatthias Ringwald // cache needed data 24416bc9fa5eSMatthias Ringwald int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 24426bc9fa5eSMatthias Ringwald #endif 24436bc9fa5eSMatthias Ringwald 24440bbba85bSMatthias Ringwald // connection failed, remove entry 24450bbba85bSMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 24460bbba85bSMatthias Ringwald btstack_memory_hci_connection_free( conn ); 24470bbba85bSMatthias Ringwald 24486bc9fa5eSMatthias Ringwald #ifdef ENABLE_CLASSIC 24490bbba85bSMatthias Ringwald // notify client if dedicated bonding 24500bbba85bSMatthias Ringwald if (notify_dedicated_bonding_failed){ 24510bbba85bSMatthias Ringwald log_info("hci notify_dedicated_bonding_failed"); 24520bbba85bSMatthias Ringwald hci_emit_dedicated_bonding_result(bd_address, status); 24530bbba85bSMatthias Ringwald } 24540bbba85bSMatthias Ringwald 24550bbba85bSMatthias Ringwald // if authentication error, also delete link key 24560bbba85bSMatthias Ringwald if (status == ERROR_CODE_AUTHENTICATION_FAILURE) { 24570bbba85bSMatthias Ringwald gap_drop_link_key_for_bd_addr(bd_address); 24580bbba85bSMatthias Ringwald } 24591c79b5e3SMatthias Ringwald #else 24601c79b5e3SMatthias Ringwald UNUSED(status); 24616bc9fa5eSMatthias Ringwald #endif 24620bbba85bSMatthias Ringwald } 24630bbba85bSMatthias Ringwald 2464be500194SMatthias Ringwald #ifdef ENABLE_CLASSIC 24652f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_0(hci_connection_t * conn, const uint8_t * features){ 24662f5c44baSMatthias Ringwald // SSP Controller 24672f5c44baSMatthias Ringwald if (features[6] & (1 << 3)){ 24682f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER; 24692f5c44baSMatthias Ringwald } 24702f5c44baSMatthias Ringwald // eSCO 24712f5c44baSMatthias Ringwald if (features[3] & (1<<7)){ 24722f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 1; 24732f5c44baSMatthias Ringwald } 24742f5c44baSMatthias Ringwald // Extended features 24752f5c44baSMatthias Ringwald if (features[7] & (1<<7)){ 24762f5c44baSMatthias Ringwald conn->remote_supported_features[0] |= 2; 24772f5c44baSMatthias Ringwald } 24782f5c44baSMatthias Ringwald } 24792f5c44baSMatthias Ringwald 24802f5c44baSMatthias Ringwald static void hci_handle_remote_features_page_1(hci_connection_t * conn, const uint8_t * features){ 24812f5c44baSMatthias Ringwald // SSP Host 24822f5c44baSMatthias Ringwald if (features[0] & (1 << 0)){ 24832f5c44baSMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP_HOST; 24842f5c44baSMatthias Ringwald } 248550c51a77SMatthias Ringwald // SC Host 248650c51a77SMatthias Ringwald if (features[0] & (1 << 3)){ 248750c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_HOST; 248850c51a77SMatthias Ringwald } 248950c51a77SMatthias Ringwald } 249050c51a77SMatthias Ringwald 249150c51a77SMatthias Ringwald static void hci_handle_remote_features_page_2(hci_connection_t * conn, const uint8_t * features){ 249250c51a77SMatthias Ringwald // SC Controller 249350c51a77SMatthias Ringwald if (features[1] & (1 << 0)){ 249450c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 249550c51a77SMatthias Ringwald } 24962f5c44baSMatthias Ringwald } 24972f5c44baSMatthias Ringwald 2498de0df013SMatthias Ringwald static void hci_handle_remote_features_received(hci_connection_t * conn){ 2499461a2bc4SMatthias Ringwald conn->bonding_flags &= ~BONDING_REMOTE_FEATURES_QUERY_ACTIVE; 2500de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 250149bafb5eSMatthias Ringwald log_info("Remote features %02x, bonding flags %x", conn->remote_supported_features[0], conn->bonding_flags); 2502de0df013SMatthias Ringwald if (conn->bonding_flags & BONDING_DEDICATED){ 2503de0df013SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2504de0df013SMatthias Ringwald } 2505de0df013SMatthias Ringwald } 2506128825c3SMatthias Ringwald static bool hci_remote_sc_enabled(hci_connection_t * connection){ 2507128825c3SMatthias Ringwald const uint16_t sc_enabled_mask = BONDING_REMOTE_SUPPORTS_SC_HOST | BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 2508128825c3SMatthias Ringwald return (connection->bonding_flags & sc_enabled_mask) == sc_enabled_mask; 2509128825c3SMatthias Ringwald } 2510128825c3SMatthias Ringwald 2511be500194SMatthias Ringwald #endif 2512de0df013SMatthias Ringwald 251367c6c9dcSMatthias Ringwald static void handle_event_for_current_stack_state(const uint8_t * packet, uint16_t size) { 251467c6c9dcSMatthias Ringwald // handle BT initialization 251567c6c9dcSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 251667c6c9dcSMatthias Ringwald hci_initializing_event_handler(packet, size); 251767c6c9dcSMatthias Ringwald } 251867c6c9dcSMatthias Ringwald 251967c6c9dcSMatthias Ringwald // help with BT sleep 252067c6c9dcSMatthias Ringwald if ((hci_stack->state == HCI_STATE_FALLING_ASLEEP) 252167c6c9dcSMatthias Ringwald && (hci_stack->substate == HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE) 252272a2585aSMatthias Ringwald && (hci_event_packet_get_type(packet) == HCI_EVENT_COMMAND_COMPLETE) 252372a2585aSMatthias Ringwald && (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_WRITE_SCAN_ENABLE)){ 252467c6c9dcSMatthias Ringwald hci_initializing_next_state(); 252567c6c9dcSMatthias Ringwald } 252667c6c9dcSMatthias Ringwald } 252767c6c9dcSMatthias Ringwald 25289866fdc7SMatthias Ringwald #ifdef ENABLE_CLASSIC 25299866fdc7SMatthias Ringwald static void hci_handle_read_encryption_key_size_complete(hci_connection_t * conn, uint8_t encryption_key_size) { 25308daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 25319866fdc7SMatthias Ringwald conn->encryption_key_size = encryption_key_size; 2532de9f0299SMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_connection(conn); 2533de9f0299SMatthias Ringwald 2534de9f0299SMatthias Ringwald // trigger disconnect for dedicated bonding, skip emit security level as disconnect is pending 2535de9f0299SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 2536de9f0299SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 2537de9f0299SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 2538de9f0299SMatthias Ringwald conn->bonding_status = security_level == 0 ? ERROR_CODE_INSUFFICIENT_SECURITY : ERROR_CODE_SUCCESS; 2539de9f0299SMatthias Ringwald return; 2540de9f0299SMatthias Ringwald } 2541abdad579SMatthias Ringwald 25428daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) != 0) { 25431cf0a6c8SMatthias Ringwald conn->requested_security_level = LEVEL_0; 2544de9f0299SMatthias Ringwald hci_emit_security_level(conn->con_handle, security_level); 2545abdad579SMatthias Ringwald return; 2546abdad579SMatthias Ringwald } 2547abdad579SMatthias Ringwald 2548d54424c3SMatthias Ringwald // Request remote features if not already done 2549dbe0d85cSMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 2550d54424c3SMatthias Ringwald 2551abdad579SMatthias Ringwald // Request Authentication if not already done 2552abdad579SMatthias Ringwald if ((conn->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) return; 2553abdad579SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 25549866fdc7SMatthias Ringwald } 25559866fdc7SMatthias Ringwald #endif 25569866fdc7SMatthias Ringwald 25573b631737SMatthias Ringwald static void hci_store_local_supported_commands(const uint8_t * packet){ 255825e46151SMatthias Ringwald // create mapping table 255925e46151SMatthias Ringwald #define X(name, offset, bit) { offset, bit }, 256025e46151SMatthias Ringwald static struct { 256125e46151SMatthias Ringwald uint8_t byte_offset; 256225e46151SMatthias Ringwald uint8_t bit_position; 256325e46151SMatthias Ringwald } supported_hci_commands_map [] = { 256425e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 256525e46151SMatthias Ringwald }; 256625e46151SMatthias Ringwald #undef X 256725e46151SMatthias Ringwald 256825e46151SMatthias Ringwald // create names for debug purposes 256925e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 257025e46151SMatthias Ringwald #define X(name, offset, bit) #name, 257125e46151SMatthias Ringwald static const char * command_names[] = { 257225e46151SMatthias Ringwald SUPPORTED_HCI_COMMANDS 257325e46151SMatthias Ringwald }; 257425e46151SMatthias Ringwald #undef X 257525e46151SMatthias Ringwald #endif 257625e46151SMatthias Ringwald 25771ffa425cSMatthias Ringwald hci_stack->local_supported_commands = 0; 257825e46151SMatthias Ringwald const uint8_t * commands_map = &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1]; 257925e46151SMatthias Ringwald uint16_t i; 258025e46151SMatthias Ringwald for (i = 0 ; i < SUPPORTED_HCI_COMMANDS_COUNT ; i++){ 258125e46151SMatthias Ringwald if ((commands_map[supported_hci_commands_map[i].byte_offset] & (1 << supported_hci_commands_map[i].bit_position)) != 0){ 258225e46151SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG 258325e46151SMatthias 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); 258425e46151SMatthias Ringwald #else 25851ffa425cSMatthias Ringwald log_info("Command 0x%02x supported %u/%u", i, supported_hci_commands_map[i].byte_offset, supported_hci_commands_map[i].bit_position); 258625e46151SMatthias Ringwald #endif 25871ffa425cSMatthias Ringwald hci_stack->local_supported_commands |= (1LU << i); 258825e46151SMatthias Ringwald } 258925e46151SMatthias Ringwald } 25901ffa425cSMatthias Ringwald log_info("Local supported commands summary %04x", hci_stack->local_supported_commands); 25913b631737SMatthias Ringwald } 25923b631737SMatthias Ringwald 25934f781026SMatthias Ringwald static void handle_command_complete_event(uint8_t * packet, uint16_t size){ 2594b08371a9SMilanka Ringwald UNUSED(size); 2595e76a89eeS[email protected] 25969cbd2215SMatthias Ringwald uint16_t manufacturer; 25979cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 2598fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 25991f7b95a1Smatthias.ringwald hci_connection_t * conn; 2600c7a108afSMatthias Ringwald #endif 2601c7a108afSMatthias Ringwald #if defined(ENABLE_CLASSIC) || (defined(ENABLE_BLE) && defined(ENABLE_LE_ISOCHRONOUS_STREAMS)) 2602645b7c25SMatthias Ringwald uint8_t status; 26039cbd2215SMatthias Ringwald #endif 2604d3a89d91SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 2605d3a89d91SMatthias Ringwald le_audio_cig_t * cig; 2606d3a89d91SMatthias Ringwald #endif 2607d3a89d91SMatthias Ringwald 26086bef4003SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 26096bef4003SMatthias Ringwald hci_stack->num_cmd_packets = packet[2] ? 1 : 0; 26107ec5eeaaSmatthias.ringwald 2611645b7c25SMatthias Ringwald uint16_t opcode = hci_event_command_complete_get_command_opcode(packet); 2612645b7c25SMatthias Ringwald switch (opcode){ 2613645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_NAME: 2614645b7c25SMatthias Ringwald if (packet[5]) break; 26159e263bd7SMatthias Ringwald // terminate, name 248 chars 26169e263bd7SMatthias Ringwald packet[6+248] = 0; 26179e263bd7SMatthias Ringwald log_info("local name: %s", &packet[6]); 2618645b7c25SMatthias Ringwald break; 2619645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BUFFER_SIZE: 26201d279b20Smatthias.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" 2621c3b46f5aSMatthias Ringwald if (hci_stack->state == HCI_STATE_INITIALIZING) { 2622429122ccSMatthias Ringwald uint16_t acl_len = little_endian_read_16(packet, 6); 2623429122ccSMatthias Ringwald uint16_t sco_len = packet[8]; 2624429122ccSMatthias Ringwald 2625429122ccSMatthias Ringwald // determine usable ACL/SCO payload size 2626429122ccSMatthias Ringwald hci_stack->acl_data_packet_length = btstack_min(acl_len, HCI_ACL_PAYLOAD_SIZE); 2627429122ccSMatthias Ringwald hci_stack->sco_data_packet_length = btstack_min(sco_len, HCI_ACL_PAYLOAD_SIZE); 2628429122ccSMatthias Ringwald 2629b1c141dbSMatthias Ringwald hci_stack->acl_packets_total_num = (uint8_t) little_endian_read_16(packet, 9); 2630b1c141dbSMatthias Ringwald hci_stack->sco_packets_total_num = (uint8_t) little_endian_read_16(packet, 11); 2631a8b12447S[email protected] 2632429122ccSMatthias Ringwald log_info("hci_read_buffer_size: ACL size module %u -> used %u, count %u / SCO size %u, count %u", 2633429122ccSMatthias Ringwald acl_len, hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num, 26341a06f663S[email protected] hci_stack->sco_data_packet_length, hci_stack->sco_packets_total_num); 2635c3b46f5aSMatthias Ringwald } 2636645b7c25SMatthias Ringwald break; 2637645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_RSSI: 2638645b7c25SMatthias Ringwald if (packet[5] == ERROR_CODE_SUCCESS){ 2639891b9fc2SMatthias Ringwald uint8_t event[5]; 2640891b9fc2SMatthias Ringwald event[0] = GAP_EVENT_RSSI_MEASUREMENT; 2641891b9fc2SMatthias Ringwald event[1] = 3; 26426535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[6], 3); 2643891b9fc2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2644891b9fc2SMatthias Ringwald } 2645645b7c25SMatthias Ringwald break; 2646a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 264779b7ea2dSMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE_V2: 264879b7ea2dSMatthias Ringwald hci_stack->le_iso_packets_length = little_endian_read_16(packet, 9); 264979b7ea2dSMatthias Ringwald hci_stack->le_iso_packets_total_num = packet[11]; 265079b7ea2dSMatthias Ringwald log_info("hci_le_read_buffer_size_v2: iso size %u, iso count %u", 265179b7ea2dSMatthias Ringwald hci_stack->le_iso_packets_length, hci_stack->le_iso_packets_total_num); 265279b7ea2dSMatthias Ringwald 265379b7ea2dSMatthias Ringwald /* fall through */ 265479b7ea2dSMatthias Ringwald 2655645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_BUFFER_SIZE: 2656f8fbdce0SMatthias Ringwald hci_stack->le_data_packets_length = little_endian_read_16(packet, 6); 2657ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 26586c26b087S[email protected] // determine usable ACL payload size 26596c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 26606c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 26616c26b087S[email protected] } 266279b7ea2dSMatthias 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); 2663645b7c25SMatthias Ringwald break; 2664b435e062SMatthias Ringwald #endif 2665b435e062SMatthias Ringwald #ifdef ENABLE_LE_DATA_LENGTH_EXTENSION 2666645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_DATA_LENGTH: 2667dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_octets = little_endian_read_16(packet, 6); 2668dcd678baSMatthias Ringwald hci_stack->le_supported_max_tx_time = little_endian_read_16(packet, 8); 2669dcd678baSMatthias 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); 2670645b7c25SMatthias Ringwald break; 2671b435e062SMatthias Ringwald #endif 2672d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2673645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_WHITE_LIST_SIZE: 2674e691bb38SMatthias Ringwald hci_stack->le_whitelist_capacity = packet[6]; 267515d0a15bSMatthias Ringwald log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); 2676645b7c25SMatthias Ringwald break; 267765a46ef3S[email protected] #endif 26783a74541eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 26793a74541eSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 26803a74541eSMatthias Ringwald case HCI_OPCODE_HCI_LE_READ_MAXIMUM_ADVERTISING_DATA_LENGTH: 26813a74541eSMatthias Ringwald hci_stack->le_maximum_advertising_data_length = little_endian_read_16(packet, 6); 26823a74541eSMatthias Ringwald break; 268325df6c61SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_EXTENDED_ADVERTISING_PARAMETERS: 268425df6c61SMatthias Ringwald if (hci_stack->le_advertising_set_in_current_command != 0) { 268525df6c61SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(hci_stack->le_advertising_set_in_current_command); 268625df6c61SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 268725df6c61SMatthias Ringwald if (advertising_set == NULL) break; 268825df6c61SMatthias Ringwald uint8_t adv_status = packet[6]; 268925df6c61SMatthias Ringwald uint8_t tx_power = packet[7]; 269025df6c61SMatthias 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 }; 269125df6c61SMatthias Ringwald if (adv_status == 0){ 269225df6c61SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PARAMS_SET; 269325df6c61SMatthias Ringwald } 269425df6c61SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 269525df6c61SMatthias Ringwald } 269625df6c61SMatthias Ringwald break; 269725df6c61SMatthias Ringwald case HCI_OPCODE_HCI_LE_REMOVE_ADVERTISING_SET: 269825df6c61SMatthias Ringwald if (hci_stack->le_advertising_set_in_current_command != 0) { 269925df6c61SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(hci_stack->le_advertising_set_in_current_command); 270025df6c61SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 270125df6c61SMatthias Ringwald if (advertising_set == NULL) break; 270225df6c61SMatthias Ringwald uint8_t adv_status = packet[5]; 270325df6c61SMatthias Ringwald uint8_t event[] = { HCI_EVENT_META_GAP, 3, GAP_SUBEVENT_ADVERTISING_SET_REMOVED, hci_stack->le_advertising_set_in_current_command, adv_status }; 270425df6c61SMatthias Ringwald if (adv_status == 0){ 270525df6c61SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_advertising_sets, (btstack_linked_item_t *) advertising_set); 270625df6c61SMatthias Ringwald } 270725df6c61SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 270825df6c61SMatthias Ringwald } 270925df6c61SMatthias Ringwald break; 27103a74541eSMatthias Ringwald #endif 27113a74541eSMatthias Ringwald #endif 2712645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_BD_ADDR: 2713645b7c25SMatthias Ringwald reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); 2714645b7c25SMatthias 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)); 271533373e40SMatthias Ringwald #ifdef ENABLE_CLASSIC 27161624665aSMatthias Ringwald if (hci_stack->link_key_db){ 27171624665aSMatthias Ringwald hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); 27181624665aSMatthias Ringwald } 271933373e40SMatthias Ringwald #endif 2720645b7c25SMatthias Ringwald break; 272135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2722645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SCAN_ENABLE: 272371fd255dSMatthias Ringwald hci_emit_scan_mode_changed(hci_stack->discoverable, hci_stack->connectable); 2724645b7c25SMatthias Ringwald break; 27253d1f2d24SMatthias Ringwald case HCI_OPCODE_HCI_PERIODIC_INQUIRY_MODE: 27263d1f2d24SMatthias Ringwald status = hci_event_command_complete_get_return_parameters(packet)[0]; 27273d1f2d24SMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 27283d1f2d24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_PERIODIC; 27293d1f2d24SMatthias Ringwald } else { 27303d1f2d24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 27313d1f2d24SMatthias Ringwald } 27323d1f2d24SMatthias Ringwald break; 2733645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY_CANCEL: 273430199e24SMatthias Ringwald case HCI_OPCODE_HCI_EXIT_PERIODIC_INQUIRY_MODE: 2735f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W4_CANCELLED){ 2736f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 2737f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 2738f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 2739f5875de5SMatthias Ringwald } 2740645b7c25SMatthias Ringwald break; 274135454696SMatthias Ringwald #endif 2742645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_FEATURES: 27434f781026SMatthias Ringwald (void)memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], 8); 274465389bfcS[email protected] 274535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 2746a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 2747a5a23fc2S[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]); 27488b96126aSMatthias Ringwald log_info("Packet types %04x, eSCO %u", hci_stack->packet_types, hci_extended_sco_link_supported()); 274935454696SMatthias Ringwald #endif 2750f5d8d141S[email protected] // Classic/LE 2751f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 2752645b7c25SMatthias Ringwald break; 2753645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_VERSION_INFORMATION: 2754645b7c25SMatthias Ringwald manufacturer = little_endian_read_16(packet, 10); 2755c21d89f3SMatthias Ringwald // map Cypress to Broadcom 2756c21d89f3SMatthias Ringwald if (manufacturer == BLUETOOTH_COMPANY_ID_CYPRESS_SEMICONDUCTOR){ 2757c21d89f3SMatthias Ringwald log_info("Treat Cypress as Broadcom"); 2758c21d89f3SMatthias Ringwald manufacturer = BLUETOOTH_COMPANY_ID_BROADCOM_CORPORATION; 2759c21d89f3SMatthias Ringwald little_endian_store_16(packet, 10, manufacturer); 2760c21d89f3SMatthias Ringwald } 2761c21d89f3SMatthias Ringwald hci_stack->manufacturer = manufacturer; 27624696bddbSMatthias Ringwald log_info("Manufacturer: 0x%04x", hci_stack->manufacturer); 2763645b7c25SMatthias Ringwald break; 2764645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_SUPPORTED_COMMANDS: 27653b631737SMatthias Ringwald hci_store_local_supported_commands(packet); 2766645b7c25SMatthias Ringwald break; 2767e8c8828eSMatthias Ringwald #ifdef ENABLE_CLASSIC 2768645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_SYNCHRONOUS_FLOW_CONTROL_ENABLE: 27694f781026SMatthias Ringwald if (packet[5]) return; 27705b9b590fSMatthias Ringwald hci_stack->synchronous_flow_control_enabled = 1; 2771645b7c25SMatthias Ringwald break; 2772645b7c25SMatthias Ringwald case HCI_OPCODE_HCI_READ_ENCRYPTION_KEY_SIZE: 2773645b7c25SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2774573897a0SMatthias Ringwald handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1); 2775573897a0SMatthias Ringwald conn = hci_connection_for_handle(handle); 2776c3b46f5aSMatthias Ringwald if (conn != NULL) { 27779866fdc7SMatthias Ringwald uint8_t key_size = 0; 2778573897a0SMatthias Ringwald if (status == 0){ 27799866fdc7SMatthias Ringwald key_size = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3]; 27804a659b0eSMatthias Ringwald log_info("Handle %04x key Size: %u", handle, key_size); 2781170fafaeSMatthias Ringwald } else { 2782e9f98c4aSMatthias Ringwald key_size = 1; 27839866fdc7SMatthias Ringwald log_info("Read Encryption Key Size failed 0x%02x-> assuming insecure connection with key size of 1", status); 2784573897a0SMatthias Ringwald } 27859866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, key_size); 2786c3b46f5aSMatthias Ringwald } 2787645b7c25SMatthias Ringwald break; 2788cc15bb2cSMatthias Ringwald // assert pairing complete event is emitted. 2789cc15bb2cSMatthias Ringwald // note: for SSP, Simple Pairing Complete Event is sufficient, but we want to be more robust 2790cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_PIN_CODE_REQUEST_NEGATIVE_REPLY: 2791cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_PASSKEY_REQUEST_NEGATIVE_REPLY: 2792cc15bb2cSMatthias Ringwald case HCI_OPCODE_HCI_USER_CONFIRMATION_REQUEST_NEGATIVE_REPLY: 27938cc1507eSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 2794cc15bb2cSMatthias Ringwald // lookup connection by gap pairing addr 2795cc15bb2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(hci_stack->gap_pairing_addr, BD_ADDR_TYPE_ACL); 2796cc15bb2cSMatthias Ringwald if (conn == NULL) break; 2797cc15bb2cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 2798cc15bb2cSMatthias Ringwald break; 2799cc15bb2cSMatthias Ringwald 280075a8e4faSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 280175a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_OOB_DATA: 280275a8e4faSMatthias Ringwald case HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA:{ 280375a8e4faSMatthias Ringwald uint8_t event[67]; 280475a8e4faSMatthias Ringwald event[0] = GAP_EVENT_LOCAL_OOB_DATA; 280575a8e4faSMatthias Ringwald event[1] = 65; 280675a8e4faSMatthias Ringwald (void)memset(&event[2], 0, 65); 280775a8e4faSMatthias Ringwald if (packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE] == ERROR_CODE_SUCCESS){ 280875a8e4faSMatthias Ringwald (void)memcpy(&event[3], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 32); 280975a8e4faSMatthias Ringwald if (opcode == HCI_OPCODE_HCI_READ_LOCAL_EXTENDED_OOB_DATA){ 281075a8e4faSMatthias Ringwald event[2] = 3; 281175a8e4faSMatthias Ringwald (void)memcpy(&event[35], &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+33], 32); 281275a8e4faSMatthias Ringwald } else { 281375a8e4faSMatthias Ringwald event[2] = 1; 281475a8e4faSMatthias Ringwald } 281575a8e4faSMatthias Ringwald } 281675a8e4faSMatthias Ringwald hci_emit_event(event, sizeof(event), 0); 281775a8e4faSMatthias Ringwald break; 281875a8e4faSMatthias Ringwald } 28191ae74bf3SMatthias Ringwald 28201ae74bf3SMatthias Ringwald // note: only needed if user does not provide OOB data 28211ae74bf3SMatthias Ringwald case HCI_OPCODE_HCI_REMOTE_OOB_DATA_REQUEST_NEGATIVE_REPLY: 28221ae74bf3SMatthias Ringwald conn = hci_connection_for_handle(hci_stack->classic_oob_con_handle); 28231ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 28241ae74bf3SMatthias Ringwald if (conn == NULL) break; 28251ae74bf3SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_AUTHENTICATION_FAILURE); 28261ae74bf3SMatthias Ringwald break; 2827e8c8828eSMatthias Ringwald #endif 282875a8e4faSMatthias Ringwald #endif 28294ae8623eSMatthias Ringwald #ifdef ENABLE_BLE 28304ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 2831d3a89d91SMatthias Ringwald case HCI_OPCODE_HCI_LE_SET_CIG_PARAMETERS: 2832d3a89d91SMatthias Ringwald // lookup CIG 2833d3a89d91SMatthias Ringwald cig = hci_cig_for_id(hci_stack->iso_active_operation_group_id); 2834d3a89d91SMatthias Ringwald if (cig != NULL){ 2835d3a89d91SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 2836d3a89d91SMatthias Ringwald uint8_t i = 0; 2837d3a89d91SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 2838d3a89d91SMatthias Ringwald // assign CIS handles to pre-allocated CIS 2839d3a89d91SMatthias Ringwald btstack_linked_list_iterator_t it; 2840d3a89d91SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 2841d3a89d91SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it) && (i < cig->num_cis)) { 2842d3a89d91SMatthias Ringwald hci_iso_stream_t *iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 2843d3a89d91SMatthias Ringwald if ((iso_stream->group_id == hci_stack->iso_active_operation_group_id) && 2844d3a89d91SMatthias Ringwald (iso_stream->iso_type == HCI_ISO_TYPE_CIS)){ 2845d3a89d91SMatthias Ringwald hci_con_handle_t cis_handle = little_endian_read_16(packet, OFFSET_OF_DATA_IN_COMMAND_COMPLETE+3+(2*i)); 2846d3a89d91SMatthias Ringwald iso_stream->con_handle = cis_handle; 2847d3a89d91SMatthias Ringwald cig->cis_con_handles[i] = cis_handle; 2848d3a89d91SMatthias Ringwald i++; 2849d3a89d91SMatthias Ringwald } 2850d3a89d91SMatthias Ringwald } 2851444e371eSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_CIS_REQUEST; 2852d3a89d91SMatthias Ringwald hci_emit_cig_created(cig, status); 2853d3a89d91SMatthias Ringwald } else { 2854d3a89d91SMatthias Ringwald hci_emit_cig_created(cig, status); 2855d3a89d91SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_cigs, (btstack_linked_item_t *) cig); 2856d3a89d91SMatthias Ringwald } 2857d3a89d91SMatthias Ringwald } 2858d3a89d91SMatthias Ringwald hci_stack->iso_active_operation_group_id = 0xff; 2859d3a89d91SMatthias Ringwald break; 28604ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 28614ae8623eSMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 28624ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2863a90016deSMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 28644ae8623eSMatthias Ringwald } 28654ae8623eSMatthias Ringwald break; 286642529435SMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 286742529435SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 286842529435SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 286942529435SMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 287042529435SMatthias Ringwald } 287142529435SMatthias Ringwald break; 28727aa35f6aSMatthias Ringwald case HCI_OPCODE_HCI_LE_SETUP_ISO_DATA_PATH: { 28737aa35f6aSMatthias Ringwald // lookup BIG by state 28747aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 28757aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 28767aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 28777aa35f6aSMatthias Ringwald le_audio_big_t *big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 28787aa35f6aSMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH){ 28797aa35f6aSMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 28807aa35f6aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 28817aa35f6aSMatthias Ringwald big->state_vars.next_bis++; 28827aa35f6aSMatthias Ringwald if (big->state_vars.next_bis == big->num_bis){ 28837aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_ACTIVE; 28847aa35f6aSMatthias Ringwald hci_emit_big_created(big, ERROR_CODE_SUCCESS); 28857aa35f6aSMatthias Ringwald } else { 28867aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 28877aa35f6aSMatthias Ringwald } 28887aa35f6aSMatthias Ringwald } else { 28897aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED; 28907aa35f6aSMatthias Ringwald big->state_vars.status = status; 28912a6c0f82SMatthias Ringwald } 28922a6c0f82SMatthias Ringwald return; 28932a6c0f82SMatthias Ringwald } 28942a6c0f82SMatthias Ringwald } 28952a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 28962a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 28972a6c0f82SMatthias Ringwald le_audio_big_sync_t *big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 28982a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH){ 28992a6c0f82SMatthias Ringwald status = packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE]; 29002a6c0f82SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 29012a6c0f82SMatthias Ringwald big_sync->state_vars.next_bis++; 29022a6c0f82SMatthias Ringwald if (big_sync->state_vars.next_bis == big_sync->num_bis){ 29032a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_ACTIVE; 29042a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, ERROR_CODE_SUCCESS); 29052a6c0f82SMatthias Ringwald } else { 29062a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 29072a6c0f82SMatthias Ringwald } 29082a6c0f82SMatthias Ringwald } else { 29092a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED; 29102a6c0f82SMatthias Ringwald big_sync->state_vars.status = status; 29112a6c0f82SMatthias Ringwald } 29122a6c0f82SMatthias Ringwald return; 29132a6c0f82SMatthias Ringwald } 29142a6c0f82SMatthias Ringwald } 2915*f9306a1aSMatthias Ringwald // Lookup CIS via active group operation 2916*f9306a1aSMatthias Ringwald if (hci_stack->iso_active_operation_type == HCI_ISO_TYPE_CIS){ 2917*f9306a1aSMatthias Ringwald cig = hci_cig_for_id(hci_stack->iso_active_operation_group_id); 2918*f9306a1aSMatthias Ringwald if (cig != NULL) { 2919*f9306a1aSMatthias Ringwald // emit cis created if all ISO Paths have been created 2920*f9306a1aSMatthias Ringwald // assume we are central 2921*f9306a1aSMatthias Ringwald uint8_t cis_index = cig->state_vars.next_cis >> 1; 2922*f9306a1aSMatthias Ringwald uint8_t cis_direction = cig->state_vars.next_cis & 1; 2923*f9306a1aSMatthias Ringwald bool outgoing_needed = cig->params->cis_params[cis_index].max_sdu_p_to_c > 0; 2924*f9306a1aSMatthias Ringwald // if outgoing has been setup, or incoming was setup but outgoing not required 2925*f9306a1aSMatthias Ringwald if ((cis_direction == 1) || (outgoing_needed == false)){ 2926*f9306a1aSMatthias Ringwald hci_emit_cis_created(cig->cig_id, cig->cis_con_handles[cis_index], status); 2927*f9306a1aSMatthias Ringwald } 2928*f9306a1aSMatthias Ringwald // next state 2929*f9306a1aSMatthias Ringwald cig->state_vars.next_cis++; 2930*f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_SETUP_ISO_PATH; 2931*f9306a1aSMatthias Ringwald } 2932*f9306a1aSMatthias Ringwald } 29332a6c0f82SMatthias Ringwald break; 29342a6c0f82SMatthias Ringwald } 29352a6c0f82SMatthias Ringwald case HCI_OPCODE_HCI_LE_BIG_TERMINATE_SYNC: { 29362a6c0f82SMatthias Ringwald // lookup BIG by state 29372a6c0f82SMatthias Ringwald btstack_linked_list_iterator_t it; 29382a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 29392a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 29402a6c0f82SMatthias Ringwald le_audio_big_sync_t *big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 2941d04a9afcSMatthias Ringwald uint8_t big_handle = big_sync->big_handle; 29422a6c0f82SMatthias Ringwald switch (big_sync->state){ 29432a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED: 2944d04a9afcSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 29452a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, big_sync->state_vars.status); 2946d04a9afcSMatthias Ringwald return; 29472a6c0f82SMatthias Ringwald default: 2948d04a9afcSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 2949d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 29507aa35f6aSMatthias Ringwald return; 29517aa35f6aSMatthias Ringwald } 29527aa35f6aSMatthias Ringwald } 29537aa35f6aSMatthias Ringwald break; 29547aa35f6aSMatthias Ringwald } 29554ae8623eSMatthias Ringwald #endif 29564ae8623eSMatthias Ringwald #endif 29576f35bb46SMatthias Ringwald default: 29586f35bb46SMatthias Ringwald break; 29594f781026SMatthias Ringwald } 2960645b7c25SMatthias Ringwald } 29614f781026SMatthias Ringwald 2962afbf1cd7SMatthias Ringwald static void handle_command_status_event(uint8_t * packet, uint16_t size) { 2963afbf1cd7SMatthias Ringwald UNUSED(size); 2964afbf1cd7SMatthias Ringwald 2965afbf1cd7SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 2966afbf1cd7SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2967afbf1cd7SMatthias Ringwald 2968c70df5f8SMatthias Ringwald // get opcode and command status 2969c70df5f8SMatthias Ringwald uint16_t opcode = hci_event_command_status_get_command_opcode(packet); 2970c70df5f8SMatthias Ringwald 2971c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) || defined(ENABLE_LE_ISOCHRONOUS_STREAMS) 2972c70df5f8SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2973c70df5f8SMatthias Ringwald #endif 2974c70df5f8SMatthias Ringwald 2975c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) 2976c70df5f8SMatthias Ringwald bd_addr_type_t addr_type; 2977c70df5f8SMatthias Ringwald #endif 2978c70df5f8SMatthias Ringwald 2979c70df5f8SMatthias Ringwald switch (opcode){ 2980afbf1cd7SMatthias Ringwald #ifdef ENABLE_CLASSIC 2981c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 2982c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 2983afbf1cd7SMatthias Ringwald #endif 2984afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2985c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 2986afbf1cd7SMatthias Ringwald #endif 2987c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) 2988afbf1cd7SMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 2989afbf1cd7SMatthias Ringwald 2990afbf1cd7SMatthias Ringwald // reset outgoing address info 2991afbf1cd7SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2992afbf1cd7SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2993afbf1cd7SMatthias Ringwald 2994afbf1cd7SMatthias Ringwald // on error 2995afbf1cd7SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2996afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2997afbf1cd7SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 2998afbf1cd7SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 2999afbf1cd7SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3000afbf1cd7SMatthias Ringwald } 3001afbf1cd7SMatthias Ringwald #endif 3002afbf1cd7SMatthias Ringwald // error => outgoing connection failed 3003c70df5f8SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 3004afbf1cd7SMatthias Ringwald if (conn != NULL){ 3005afbf1cd7SMatthias Ringwald hci_handle_connection_failed(conn, status); 3006afbf1cd7SMatthias Ringwald } 3007afbf1cd7SMatthias Ringwald } 3008c70df5f8SMatthias Ringwald break; 3009c70df5f8SMatthias Ringwald #endif 3010afbf1cd7SMatthias Ringwald #ifdef ENABLE_CLASSIC 3011c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY: 3012afbf1cd7SMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 3013afbf1cd7SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 3014afbf1cd7SMatthias Ringwald } else { 3015afbf1cd7SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 3016afbf1cd7SMatthias Ringwald } 3017c70df5f8SMatthias Ringwald break; 3018c70df5f8SMatthias Ringwald #endif 3019afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3020c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 3021c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 3022afbf1cd7SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 3023a90016deSMatthias Ringwald hci_iso_stream_requested_confirm(0xff); 3024afbf1cd7SMatthias Ringwald } else { 3025a90016deSMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 3026afbf1cd7SMatthias Ringwald } 3027c70df5f8SMatthias Ringwald break; 3028afbf1cd7SMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 3029c70df5f8SMatthias Ringwald default: 3030c70df5f8SMatthias Ringwald break; 3031c70df5f8SMatthias Ringwald } 3032afbf1cd7SMatthias Ringwald } 3033afbf1cd7SMatthias Ringwald 30340ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 30350ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 30360ce3f217SMatthias Ringwald bd_addr_t addr; 30370ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 30380ce3f217SMatthias Ringwald hci_connection_t * conn; 30390ce3f217SMatthias Ringwald 30400ce3f217SMatthias Ringwald // Connection management 30410ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 30420ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 30430ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 30440ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 30450ce3f217SMatthias Ringwald 30460ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30470ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 30480ce3f217SMatthias Ringwald if (packet[3]){ 30490ce3f217SMatthias Ringwald 30500ce3f217SMatthias Ringwald // handle cancelled outgoing connection 30510ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 30520ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 30530ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 30540ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 30551f468175SMatthias Ringwald // reset state 30560ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 30571f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 30580ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 30590ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 30600ce3f217SMatthias Ringwald } 30610ce3f217SMatthias Ringwald 30620ce3f217SMatthias Ringwald // outgoing le connection establishment is done 30630ce3f217SMatthias Ringwald if (conn){ 30640ce3f217SMatthias Ringwald // remove entry 30650ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 30660ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 30670ce3f217SMatthias Ringwald } 30680ce3f217SMatthias Ringwald return; 30690ce3f217SMatthias Ringwald } 30700ce3f217SMatthias Ringwald #endif 30710ce3f217SMatthias Ringwald 30720ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 30730ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 30740ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30750ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 30760ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 30770ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 30780ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 30790ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 30800ce3f217SMatthias Ringwald } 30810ce3f217SMatthias Ringwald #endif 30820ce3f217SMatthias Ringwald } else { 30830ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 30840ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 3085935aa76eSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 30860ce3f217SMatthias Ringwald #endif 30870ce3f217SMatthias Ringwald } 30880ce3f217SMatthias Ringwald 30890ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 30900ce3f217SMatthias Ringwald if (!conn){ 30910ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 30920ce3f217SMatthias Ringwald } 30930ce3f217SMatthias Ringwald 30940ce3f217SMatthias Ringwald // no memory, sorry. 30950ce3f217SMatthias Ringwald if (!conn){ 30960ce3f217SMatthias Ringwald return; 30970ce3f217SMatthias Ringwald } 30980ce3f217SMatthias Ringwald 30990ce3f217SMatthias Ringwald conn->state = OPEN; 31000ce3f217SMatthias Ringwald conn->role = packet[6]; 31010ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 31020ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 31030ce3f217SMatthias Ringwald 31040ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 31050ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 31060ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 31070ce3f217SMatthias Ringwald } 31080ce3f217SMatthias Ringwald #endif 31090ce3f217SMatthias Ringwald 3110dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 3111dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 3112dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 3113dde9ff1eSMatthias Ringwald 31140ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 31150ce3f217SMatthias Ringwald 31160ce3f217SMatthias Ringwald // restart timer 31170ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 31180ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 31190ce3f217SMatthias Ringwald 31200ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 31210ce3f217SMatthias Ringwald 31220ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 31230ce3f217SMatthias Ringwald } 31240ce3f217SMatthias Ringwald #endif 31250ce3f217SMatthias Ringwald 312617c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 312717c6fe5cSMatthias 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){ 312817c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 312917c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 31309a8f78c1SMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities if Authenticated is possible 31319a8f78c1SMatthias Ringwald // @see: Core Spec v5.3, Vol 3, Part C, Table 5.7 313217c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 31339a8f78c1SMatthias Ringwald // MITM not possible without keyboard or display 313417c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 313517c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 31369a8f78c1SMatthias Ringwald 31379a8f78c1SMatthias Ringwald // MITM possible if one side has keyboard and the other has keyboard or display 31389a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 31399a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 31409a8f78c1SMatthias Ringwald 31419a8f78c1SMatthias Ringwald // MITM not possible if one side has only display and other side has no keyboard 31429a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 31439a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 314417c6fe5cSMatthias Ringwald } 314517c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 314617c6fe5cSMatthias Ringwald return true; 314717c6fe5cSMatthias Ringwald } 31483817f9dfSMatthias Ringwald 31493817f9dfSMatthias Ringwald static bool btstack_is_null(uint8_t * data, uint16_t size){ 31503817f9dfSMatthias Ringwald uint16_t i; 31513817f9dfSMatthias Ringwald for (i=0; i < size ; i++){ 31523817f9dfSMatthias Ringwald if (data[i] != 0) { 31533817f9dfSMatthias Ringwald return false; 31543817f9dfSMatthias Ringwald } 31553817f9dfSMatthias Ringwald } 31563817f9dfSMatthias Ringwald return true; 31573817f9dfSMatthias Ringwald } 31583817f9dfSMatthias Ringwald 3159b3c4163bSMatthias Ringwald static void hci_ssp_assess_security_on_io_cap_request(hci_connection_t * conn){ 31602dd8985bSMatthias Ringwald // get requested security level 31612dd8985bSMatthias Ringwald gap_security_level_t requested_security_level = conn->requested_security_level; 31622dd8985bSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 31632dd8985bSMatthias Ringwald requested_security_level = LEVEL_4; 31642dd8985bSMatthias Ringwald } 31652dd8985bSMatthias Ringwald 3166b3c4163bSMatthias Ringwald // assess security: LEVEL 4 requires SC 31672dd8985bSMatthias Ringwald // skip this preliminary test if remote features are not available yet to work around potential issue in ESP32 controller 31682dd8985bSMatthias Ringwald if ((requested_security_level == LEVEL_4) && 31692dd8985bSMatthias Ringwald ((conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0) && 31702dd8985bSMatthias Ringwald !hci_remote_sc_enabled(conn)){ 3171b3c4163bSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 3172b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3173b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3174b3c4163bSMatthias Ringwald return; 3175b3c4163bSMatthias Ringwald } 3176b3c4163bSMatthias Ringwald 3177b3c4163bSMatthias Ringwald // assess security based on io capabilities 3178b3c4163bSMatthias Ringwald if (conn->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 3179b3c4163bSMatthias Ringwald // responder: fully validate io caps of both sides as well as OOB data 3180b3c4163bSMatthias Ringwald bool security_possible = false; 3181b3c4163bSMatthias Ringwald security_possible = hci_ssp_security_level_possible_for_io_cap(requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io); 3182b3c4163bSMatthias Ringwald 3183b3c4163bSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3184b3c4163bSMatthias Ringwald // We assume that both Controller can reach LEVEL 4, if one side has received P-192 and the other has received P-256, 3185b3c4163bSMatthias Ringwald // so we merge the OOB data availability 3186b3c4163bSMatthias Ringwald uint8_t have_oob_data = conn->io_cap_response_oob_data; 3187b3c4163bSMatthias Ringwald if (conn->classic_oob_c_192 != NULL){ 3188b3c4163bSMatthias Ringwald have_oob_data |= 1; 3189b3c4163bSMatthias Ringwald } 3190b3c4163bSMatthias Ringwald if (conn->classic_oob_c_256 != NULL){ 3191b3c4163bSMatthias Ringwald have_oob_data |= 2; 3192b3c4163bSMatthias Ringwald } 3193b3c4163bSMatthias Ringwald // for up to Level 3, either P-192 as well as P-256 will do 3194b3c4163bSMatthias 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 3195b3c4163bSMatthias Ringwald // if remote does not SC, we should not receive P-256 data either 3196b3c4163bSMatthias Ringwald if ((requested_security_level <= LEVEL_3) && (have_oob_data != 0)){ 3197b3c4163bSMatthias Ringwald security_possible = true; 3198b3c4163bSMatthias Ringwald } 3199b3c4163bSMatthias Ringwald // for Level 4, P-256 is needed 3200b3c4163bSMatthias Ringwald if ((requested_security_level == LEVEL_4 && ((have_oob_data & 2) != 0))){ 3201b3c4163bSMatthias Ringwald security_possible = true; 3202b3c4163bSMatthias Ringwald } 3203b3c4163bSMatthias Ringwald #endif 3204b3c4163bSMatthias Ringwald 3205b3c4163bSMatthias Ringwald if (security_possible == false){ 32062dd8985bSMatthias Ringwald log_info("IOCap/OOB insufficient for level %u -> abort", requested_security_level); 3207b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3208b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3209b3c4163bSMatthias Ringwald return; 3210b3c4163bSMatthias Ringwald } 3211b3c4163bSMatthias Ringwald } else { 3212b3c4163bSMatthias Ringwald // initiator: remote io cap not yet, only check if we have ability for MITM protection if requested and OOB is not supported 32131fe968f5SMatthias Ringwald #ifndef ENABLE_CLASSIC_PAIRING_OOB 32141fe968f5SMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 3215b3c4163bSMatthias Ringwald if ((conn->requested_security_level >= LEVEL_3) && (hci_stack->ssp_io_capability >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT)){ 3216b3c4163bSMatthias Ringwald log_info("Level 3+ required, but no input/output -> abort"); 3217b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3218b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3219b3c4163bSMatthias Ringwald return; 3220b3c4163bSMatthias Ringwald } 3221b3c4163bSMatthias Ringwald #endif 32221fe968f5SMatthias Ringwald #endif 3223b3c4163bSMatthias Ringwald } 3224b3c4163bSMatthias Ringwald 3225b3c4163bSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 3226b3c4163bSMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 3227b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 3228b3c4163bSMatthias Ringwald } else { 3229b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3230b3c4163bSMatthias Ringwald } 3231b3c4163bSMatthias Ringwald #endif 3232b3c4163bSMatthias Ringwald } 3233b3c4163bSMatthias Ringwald 323417c6fe5cSMatthias Ringwald #endif 323517c6fe5cSMatthias Ringwald 3236c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 32374f781026SMatthias Ringwald 32384f781026SMatthias Ringwald uint16_t event_length = packet[1]; 32394f781026SMatthias Ringwald 32404f781026SMatthias Ringwald // assert packet is complete 32414ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 32424f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 32434f781026SMatthias Ringwald return; 32444f781026SMatthias Ringwald } 32454f781026SMatthias Ringwald 32464f781026SMatthias Ringwald hci_con_handle_t handle; 32474f781026SMatthias Ringwald hci_connection_t * conn; 32484f781026SMatthias Ringwald int i; 32494f781026SMatthias Ringwald 32504f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 32515e91d96cSMatthias Ringwald hci_link_type_t link_type; 325248f33f37SMatthias Ringwald bd_addr_t addr; 3253cef94710SMatthias Ringwald bd_addr_type_t addr_type; 32544f781026SMatthias Ringwald #endif 32554ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 32564ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream; 32577aa35f6aSMatthias Ringwald le_audio_big_t * big; 32582a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync; 32594ae8623eSMatthias Ringwald #endif 32604f781026SMatthias Ringwald 32614f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 32624f781026SMatthias Ringwald 32634f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 32644f781026SMatthias Ringwald 32654f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 32664f781026SMatthias Ringwald handle_command_complete_event(packet, size); 326756cf178bSmatthias.ringwald break; 326856cf178bSmatthias.ringwald 32697ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 3270afbf1cd7SMatthias Ringwald handle_command_status_event(packet, size); 32717ec5eeaaSmatthias.ringwald break; 32727ec5eeaaSmatthias.ringwald 32732e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 32749784dac1SMatthias Ringwald if (size < 3) return; 32759784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 32764ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 3277d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3278d9a1d8edSMatthias Ringwald bool notify_sco = false; 3279d9a1d8edSMatthias Ringwald #endif 3280d9a1d8edSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3281d9a1d8edSMatthias Ringwald bool notify_iso = false; 3282d9a1d8edSMatthias Ringwald #endif 32839784dac1SMatthias Ringwald uint16_t offset = 3; 32849784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 32854ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 32864ea43905SMatthias Ringwald offset += 2u; 3287f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 32884ea43905SMatthias Ringwald offset += 2u; 32892e440c8aS[email protected] 32905061f3afS[email protected] conn = hci_connection_for_handle(handle); 3291e5413aa0SMatthias Ringwald if (conn != NULL) { 329223bed257S[email protected] 3293ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets) { 3294ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 3295e35edcc1S[email protected] } else { 3296ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 3297ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 3298e35edcc1S[email protected] } 3299ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 3300d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3301d9a1d8edSMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO){ 3302d9a1d8edSMatthias Ringwald notify_sco = true; 3303d9a1d8edSMatthias Ringwald } 3304d9a1d8edSMatthias Ringwald #endif 3305e5413aa0SMatthias Ringwald } 3306e5413aa0SMatthias Ringwald 33078ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 33088ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 33098ad9cf99SMatthias Ringwald #endif 33108ad9cf99SMatthias Ringwald 3311e5413aa0SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3312e5413aa0SMatthias Ringwald if (conn == NULL){ 3313e5413aa0SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(handle); 3314e5413aa0SMatthias Ringwald if (iso_stream != NULL){ 3315e5413aa0SMatthias Ringwald if (iso_stream->num_packets_sent >= num_packets) { 3316e5413aa0SMatthias Ringwald iso_stream->num_packets_sent -= num_packets; 3317e5413aa0SMatthias Ringwald } else { 3318e5413aa0SMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 3319e5413aa0SMatthias Ringwald iso_stream->num_packets_sent = 0; 3320e5413aa0SMatthias Ringwald } 33218d72db10SMatthias Ringwald if (iso_stream->iso_type == HCI_ISO_TYPE_BIS){ 33228d72db10SMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(iso_stream->group_id); 3323fe977b37SMatthias Ringwald if (big != NULL){ 3324fe977b37SMatthias Ringwald big->num_completed_timestamp_current_valid = true; 3325fe977b37SMatthias Ringwald big->num_completed_timestamp_current_ms = btstack_run_loop_get_time_ms(); 3326fe977b37SMatthias Ringwald } 3327fe977b37SMatthias Ringwald } 3328e5413aa0SMatthias Ringwald log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", 3329e5413aa0SMatthias Ringwald num_packets, handle, iso_stream->num_packets_sent); 3330d9a1d8edSMatthias Ringwald notify_iso = true; 3331e5413aa0SMatthias Ringwald } 3332d9a1d8edSMatthias Ringwald } 3333d9a1d8edSMatthias Ringwald #endif 3334d9a1d8edSMatthias Ringwald } 3335d9a1d8edSMatthias Ringwald 3336d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3337d9a1d8edSMatthias Ringwald if (notify_sco){ 3338d9a1d8edSMatthias Ringwald hci_notify_if_sco_can_send_now(); 3339d9a1d8edSMatthias Ringwald } 3340d9a1d8edSMatthias Ringwald #endif 3341d9a1d8edSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3342d9a1d8edSMatthias Ringwald if (notify_iso){ 33435ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 3344e5413aa0SMatthias Ringwald } 3345e5413aa0SMatthias Ringwald #endif 33466772a24cSmatthias.ringwald break; 33472e440c8aS[email protected] } 334835454696SMatthias Ringwald 334935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 33509afaa4d4SMatthias Ringwald case HCI_EVENT_FLUSH_OCCURRED: 33519afaa4d4SMatthias Ringwald // flush occurs only if automatic flush has been enabled by gap_enable_link_watchdog() 33529afaa4d4SMatthias Ringwald handle = hci_event_flush_occurred_get_handle(packet); 33539afaa4d4SMatthias Ringwald conn = hci_connection_for_handle(handle); 33549afaa4d4SMatthias Ringwald if (conn) { 33559afaa4d4SMatthias Ringwald log_info("Flush occurred, disconnect 0x%04x", handle); 33569afaa4d4SMatthias Ringwald conn->state = SEND_DISCONNECT; 33579afaa4d4SMatthias Ringwald } 33589afaa4d4SMatthias Ringwald break; 33599afaa4d4SMatthias Ringwald 3360f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 3361f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 3362f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 3363f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 3364f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3365f5875de5SMatthias Ringwald } 3366f5875de5SMatthias Ringwald break; 3367b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 3368b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 3369b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 3370b7f1ee76SMatthias Ringwald } 3371b7f1ee76SMatthias Ringwald break; 33721f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 3373724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 33745e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 337572cf8859SMatthias Ringwald 337672cf8859SMatthias Ringwald // CVE-2020-26555: reject incoming connection from device with same BD ADDR 337772cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0){ 337872cf8859SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 337972cf8859SMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 338072cf8859SMatthias Ringwald break; 338172cf8859SMatthias Ringwald } 338272cf8859SMatthias Ringwald 338307e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 33845e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 3385d152b6c6SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS; 338607e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 338707e010b6SMilanka Ringwald break; 338807e010b6SMilanka Ringwald } 338907e010b6SMilanka Ringwald } 339007e010b6SMilanka Ringwald 339137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 33925e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 33935e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 33942e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 33951f7b95a1Smatthias.ringwald if (!conn) { 33965293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 33971f7b95a1Smatthias.ringwald } 3398ce4c8fabSmatthias.ringwald if (!conn) { 3399ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 34004536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 3401058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 3402f23819bcSMatthias Ringwald hci_run(); 3403f23819bcSMatthias Ringwald // avoid event to higher layer 3404f23819bcSMatthias Ringwald return; 3405ce4c8fabSmatthias.ringwald } 34065cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 340732ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 3408f3a16b9aSMatthias Ringwald // store info about eSCO 34095e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 341076ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 3411f3a16b9aSMatthias Ringwald } 341232ab9390Smatthias.ringwald hci_run(); 34131f7b95a1Smatthias.ringwald break; 34141f7b95a1Smatthias.ringwald 34156772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 3416fe1ed1b8Smatthias.ringwald // Connection management 3417724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 34189da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 3419f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 34202e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3421fe1ed1b8Smatthias.ringwald if (conn) { 3422b448a0e7Smatthias.ringwald if (!packet[2]){ 3423c8e4258aSmatthias.ringwald conn->state = OPEN; 3424f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 34256909f064SMatthias Ringwald 342622df2fe2SMatthias Ringwald // trigger write supervision timeout if we're master 3427d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 342822df2fe2SMatthias Ringwald conn->gap_connection_tasks |= GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT; 34296909f064SMatthias Ringwald } 34306909f064SMatthias Ringwald 343122df2fe2SMatthias Ringwald // trigger write automatic flush timeout 34329afaa4d4SMatthias Ringwald if (hci_stack->automatic_flush_timeout != 0){ 34339afaa4d4SMatthias Ringwald conn->gap_connection_tasks |= GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT; 34349afaa4d4SMatthias Ringwald } 34359afaa4d4SMatthias Ringwald 3436c785ef68Smatthias.ringwald // restart timer 3437528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 3438528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 3439c785ef68Smatthias.ringwald 344018c4a0a3SMatthias Ringwald // trigger remote features for dedicated bonding 344118c4a0a3SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 344218c4a0a3SMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 344318c4a0a3SMatthias Ringwald } 344418c4a0a3SMatthias Ringwald 34459da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 344643bfb1bdSmatthias.ringwald 344743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 3448b448a0e7Smatthias.ringwald } else { 34490bbba85bSMatthias Ringwald // connection failed 34500bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 3451fe1ed1b8Smatthias.ringwald } 3452fe1ed1b8Smatthias.ringwald } 34536772a24cSmatthias.ringwald break; 3454fe1ed1b8Smatthias.ringwald 345544d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 3456724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 34571f34df2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 34581f34df2cSMatthias Ringwald log_info("Synchronous Connection Complete for %p (status=%u) %s", conn, packet[2], bd_addr_to_str(addr)); 34591a06f663S[email protected] if (packet[2]){ 346044d0e3d5S[email protected] // connection failed 34611f34df2cSMatthias Ringwald if (conn){ 34621f34df2cSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 34631f34df2cSMatthias Ringwald } 346444d0e3d5S[email protected] break; 346544d0e3d5S[email protected] } 3466e35edcc1S[email protected] if (!conn) { 3467e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 3468e35edcc1S[email protected] } 3469e35edcc1S[email protected] if (!conn) { 3470e35edcc1S[email protected] break; 3471e35edcc1S[email protected] } 34721a06f663S[email protected] conn->state = OPEN; 3473f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 3474ee752bb8SMatthias Ringwald 3475ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3476ee752bb8SMatthias Ringwald // update SCO 3477ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 3478ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 3479ee752bb8SMatthias Ringwald } 3480f234b250SMatthias Ringwald // trigger can send now 3481f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 34821972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 3483f234b250SMatthias Ringwald } 3484ee752bb8SMatthias Ringwald #endif 3485cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3486cb70c5abSMatthias Ringwald // configure sco transport 3487cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 3488cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 3489cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 3490cb70c5abSMatthias Ringwald } 3491cb70c5abSMatthias Ringwald #endif 349244d0e3d5S[email protected] break; 349344d0e3d5S[email protected] 3494afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 3495f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 3496afd4e962S[email protected] conn = hci_connection_for_handle(handle); 3497afd4e962S[email protected] if (!conn) break; 3498afd4e962S[email protected] if (!packet[2]){ 34992f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 35002f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 35012f5c44baSMatthias Ringwald 35025ccef624SMatthias Ringwald // read extended features if possible 35031ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES) 35041ffa425cSMatthias Ringwald && ((conn->remote_supported_features[0] & 2) != 0)) { 35055ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 35065ccef624SMatthias Ringwald break; 35075ccef624SMatthias Ringwald } 35085ccef624SMatthias Ringwald } 35095ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 35105ccef624SMatthias Ringwald break; 35115ccef624SMatthias Ringwald 35125ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 35135ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 35145ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 35155ccef624SMatthias Ringwald if (!conn) break; 35165ccef624SMatthias Ringwald // status = ok, page = 1 351750c51a77SMatthias Ringwald if (!packet[2]) { 351850c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 351950c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 35205ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 352150c51a77SMatthias Ringwald bool done = false; 352250c51a77SMatthias Ringwald switch (page_number){ 352350c51a77SMatthias Ringwald case 1: 35242f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 352550c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 352650c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 352750c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 352850c51a77SMatthias Ringwald } else { 352950c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 353050c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 353150c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 353250c51a77SMatthias Ringwald } 353350c51a77SMatthias Ringwald done = true; 353450c51a77SMatthias Ringwald } 353550c51a77SMatthias Ringwald break; 353650c51a77SMatthias Ringwald case 2: 353750c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 353850c51a77SMatthias Ringwald done = true; 353950c51a77SMatthias Ringwald break; 354050c51a77SMatthias Ringwald default: 354150c51a77SMatthias Ringwald break; 354250c51a77SMatthias Ringwald } 354350c51a77SMatthias Ringwald if (!done) break; 3544afd4e962S[email protected] } 3545de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 3546afd4e962S[email protected] break; 3547afd4e962S[email protected] 35487fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 3549308eeaffSMatthias Ringwald #ifndef ENABLE_EXPLICIT_LINK_KEY_REPLY 355063c0b2fdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 355163c0b2fdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 355263c0b2fdSMatthias Ringwald if (!conn) break; 355363c0b2fdSMatthias Ringwald 355463c0b2fdSMatthias Ringwald // lookup link key in db if not cached 355563c0b2fdSMatthias Ringwald if ((conn->link_key_type == INVALID_LINK_KEY) && (hci_stack->link_key_db != NULL)){ 355663c0b2fdSMatthias Ringwald hci_stack->link_key_db->get_link_key(conn->address, conn->link_key, &conn->link_key_type); 355763c0b2fdSMatthias Ringwald } 355863c0b2fdSMatthias Ringwald 355963c0b2fdSMatthias Ringwald // response sent by hci_run() 356063c0b2fdSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_HANDLE_LINK_KEY_REQUEST; 356163c0b2fdSMatthias Ringwald #endif 3562608f51bbSMatthias Ringwald break; 35637fde4af9Smatthias.ringwald 35649ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 35651714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 3566f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 35679ab95c90S[email protected] if (!conn) break; 35681714cbbdSMatthias Ringwald 35691714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 35701714cbbdSMatthias Ringwald 35713817f9dfSMatthias Ringwald // CVE-2020-26555: ignore NULL link key 35723817f9dfSMatthias Ringwald // default link_key_type = INVALID_LINK_KEY asserts that NULL key won't be used for encryption 35733817f9dfSMatthias Ringwald if (btstack_is_null(&packet[8], 16)) break; 35743817f9dfSMatthias Ringwald 35757bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 35769ab95c90S[email protected] // Change Connection Encryption keeps link key type 35779ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 35789ab95c90S[email protected] conn->link_key_type = link_key_type; 35799ab95c90S[email protected] } 35803817f9dfSMatthias Ringwald 3581e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 3582e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 3583e9f98c4aSMatthias Ringwald 3584bec5f683SMatthias Ringwald // only store link key: 3585bec5f683SMatthias Ringwald // - if bondable enabled 3586bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 35876edaed7fSMatthias Ringwald // - if security level sufficient 35886edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 3589bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 3590bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 3591532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 3592532454f9SMatthias Ringwald if (!remote_bonding){ 3593bec5f683SMatthias Ringwald break; 3594bec5f683SMatthias Ringwald } 3595bec5f683SMatthias Ringwald } 359655597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 35977fde4af9Smatthias.ringwald break; 35989ab95c90S[email protected] } 35997fde4af9Smatthias.ringwald 36007fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 36011714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 36021714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 36031714cbbdSMatthias Ringwald if (!conn) break; 36041714cbbdSMatthias Ringwald 36051714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 3606a800d95eSMatthias Ringwald // abort pairing if: non-bondable mode (pin code request is not forwarded to app) 36073a9fb326S[email protected] if (!hci_stack->bondable ){ 36088daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 36091714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 3610f8fb5f6eS[email protected] hci_run(); 3611f8fb5f6eS[email protected] return; 36124c57c146S[email protected] } 3613a800d95eSMatthias Ringwald // abort pairing if: LEVEL_4 required (pin code request is not forwarded to app) 3614a800d95eSMatthias Ringwald if ((hci_stack->gap_secure_connections_only_mode) || (conn->requested_security_level == LEVEL_4)){ 3615a800d95eSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 361650dcc63cSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 361750dcc63cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 361850dcc63cSMatthias Ringwald hci_run(); 361950dcc63cSMatthias Ringwald return; 362050dcc63cSMatthias Ringwald } 36217fde4af9Smatthias.ringwald break; 36227fde4af9Smatthias.ringwald 362350d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 362450d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 362550d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 362650d7398cSMatthias Ringwald if (!conn) break; 36271714cbbdSMatthias Ringwald 36288daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE); 36291714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 363050d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 3631e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 3632d22b82d6SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3633d22b82d6SMatthias Ringwald conn->io_cap_response_oob_data = hci_event_io_capability_response_get_oob_data_present(packet); 3634d22b82d6SMatthias Ringwald #endif 363550d7398cSMatthias Ringwald break; 363650d7398cSMatthias Ringwald 36371d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 36381714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 36391714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 36401714cbbdSMatthias Ringwald if (!conn) break; 36411714cbbdSMatthias Ringwald 3642c950c316SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 36439671e615SMatthias Ringwald hci_connection_timestamp(conn); 36441714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 3645dbe1a790S[email protected] break; 3646dbe1a790S[email protected] 36471849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 36481849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 36497ca4a7edSMatthias Ringwald hci_event_remote_oob_data_request_get_bd_addr(packet, addr); 36507ca4a7edSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 36517ca4a7edSMatthias Ringwald if (!conn) break; 36527ca4a7edSMatthias Ringwald 36537ca4a7edSMatthias Ringwald hci_connection_timestamp(conn); 36547ca4a7edSMatthias Ringwald 36557ca4a7edSMatthias Ringwald hci_pairing_started(conn, true); 36567ca4a7edSMatthias Ringwald 36577ca4a7edSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 36581849becdSMatthias Ringwald break; 36591849becdSMatthias Ringwald #endif 36601849becdSMatthias Ringwald 3661dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 366217c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 3663367aedc0SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3664367aedc0SMatthias Ringwald if (!conn) break; 3665367aedc0SMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 3666367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 36678daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 3668367aedc0SMatthias Ringwald }; 3669367aedc0SMatthias Ringwald } else { 3670367aedc0SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3671367aedc0SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 3672367aedc0SMatthias Ringwald // don't forward event to app 3673367aedc0SMatthias Ringwald hci_run(); 3674367aedc0SMatthias Ringwald return; 3675367aedc0SMatthias Ringwald } 3676dbe1a790S[email protected] break; 3677dbe1a790S[email protected] 3678dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 3679367aedc0SMatthias Ringwald // Pairing using Passkey results in MITM protection. If Level 4 is required, support for SC is validated on IO Cap Request 3680367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 36818daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 3682367aedc0SMatthias Ringwald }; 36831d6b20aeS[email protected] break; 36841849becdSMatthias Ringwald 36853dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 36863dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 36873dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 36883dce6128SMatthias Ringwald if (!conn) break; 36893dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 36903dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 36913dce6128SMatthias Ringwald break; 369235454696SMatthias Ringwald #endif 36931d6b20aeS[email protected] 3694f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 3695a355334eSMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 3696254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 3697f0944df2S[email protected] conn = hci_connection_for_handle(handle); 3698f0944df2S[email protected] if (!conn) break; 36994ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 3700254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 3701254b78eeSMatthias Ringwald if (encryption_enabled){ 3702573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 3703573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 37048daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 3705573897a0SMatthias Ringwald } 3706573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 3707573897a0SMatthias Ringwald else { 3708fcaf38b9SMatthias Ringwald 3709254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 371036c3546bSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type); 3711254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 3712edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 3713254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 37149ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 3715254b78eeSMatthias Ringwald break; 3716254b78eeSMatthias Ringwald } 3717254b78eeSMatthias Ringwald 3718aa2fe986SMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 3719aa2fe986SMatthias Ringwald if (connected_uses_aes_ccm){ 3720aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3721aa2fe986SMatthias Ringwald } 3722aa2fe986SMatthias Ringwald 3723aa2fe986SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3724aa2fe986SMatthias Ringwald // work around for issue with PTS dongle 3725aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3726aa2fe986SMatthias Ringwald #endif 3727a355334eSMatthias Ringwald // validate encryption key size 3728a355334eSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE_V2) { 3729a355334eSMatthias Ringwald uint8_t encryption_key_size = hci_event_encryption_change_v2_get_encryption_key_size(packet); 3730a355334eSMatthias Ringwald // already got encryption key size 3731a355334eSMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, encryption_key_size); 3732a355334eSMatthias Ringwald } else { 37331ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE)) { 37346e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 3735573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 37366e058d3fSMatthias Ringwald } else { 37376e058d3fSMatthias Ringwald // if not, pretend everything is perfect 37389866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 37396e058d3fSMatthias Ringwald } 3740573897a0SMatthias Ringwald } 3741a355334eSMatthias Ringwald } 3742573897a0SMatthias Ringwald #endif 3743f0944df2S[email protected] } else { 37448daf94bcSMatthias Ringwald conn->authentication_flags &= ~AUTH_FLAG_CONNECTION_ENCRYPTED; 3745f0944df2S[email protected] } 37465999ee11SMatthias Ringwald } else { 37475999ee11SMatthias Ringwald uint8_t status = hci_event_encryption_change_get_status(packet); 37485999ee11SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 37495999ee11SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 37505999ee11SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 37515999ee11SMatthias Ringwald conn->bonding_status = status; 37525999ee11SMatthias Ringwald } 3753ad83dc6aS[email protected] } 3754573897a0SMatthias Ringwald 3755f0944df2S[email protected] break; 3756f0944df2S[email protected] 375735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 37581eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 3759abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 37601eb2563eS[email protected] conn = hci_connection_for_handle(handle); 37611eb2563eS[email protected] if (!conn) break; 3762ad83dc6aS[email protected] 3763dbd5dcc3SMatthias Ringwald // clear authentication active flag 3764dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 37651714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 3766dbd5dcc3SMatthias Ringwald 3767abdad579SMatthias Ringwald // authenticated only if auth status == 0 3768abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 3769abdad579SMatthias Ringwald // authenticated 37708daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3771abdad579SMatthias Ringwald 3772131ef17aSMatthias Ringwald // If not already encrypted, start encryption 37738daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0){ 37741eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3775ad83dc6aS[email protected] break; 3776ad83dc6aS[email protected] } 3777abdad579SMatthias Ringwald } 3778abdad579SMatthias Ringwald 3779abdad579SMatthias Ringwald // emit updated security level 37801eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 37811eb2563eS[email protected] break; 37821714cbbdSMatthias Ringwald 37831714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 37841714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 37851714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 37861714cbbdSMatthias Ringwald if (!conn) break; 37871714cbbdSMatthias Ringwald 3788aa2fe986SMatthias Ringwald // treat successfully paired connection as authenticated 3789aa2fe986SMatthias Ringwald if (hci_event_simple_pairing_complete_get_status(packet) == ERROR_CODE_SUCCESS){ 3790f3aafff1SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3791aa2fe986SMatthias Ringwald } 3792aa2fe986SMatthias Ringwald 37931714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 37941714cbbdSMatthias Ringwald break; 379535454696SMatthias Ringwald #endif 379634d2123cS[email protected] 379786805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 3798ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 3799ccda6e14S[email protected] // see end of function, too. 3800a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 3801a4f30ec0S[email protected] if (packet[2]) break; // status != 0 3802f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 380381d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 38044ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3805c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 38064ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 380781d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 3808c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 3809c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 381081d2bdb2SMatthias Ringwald if (release_buffer){ 381181d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 381281d2bdb2SMatthias Ringwald } 3813c6a37cfdSMatthias Ringwald } 3814c6a37cfdSMatthias Ringwald } 381535454696SMatthias Ringwald 38164ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3817098c2716SMatthias Ringwald // drop outgoing ISO fragments if it is for closed connection and release buffer if tx not active 3818098c2716SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size > 0u) { 3819098c2716SMatthias Ringwald if (handle == READ_ISO_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 3820098c2716SMatthias Ringwald int release_buffer = hci_stack->iso_fragmentation_tx_active == 0u; 3821098c2716SMatthias Ringwald log_info("drop fragmented ISO data for closed connection, release buffer %u", release_buffer); 3822098c2716SMatthias Ringwald hci_stack->iso_fragmentation_total_size = 0; 3823098c2716SMatthias Ringwald hci_stack->iso_fragmentation_pos = 0; 3824098c2716SMatthias Ringwald if (release_buffer){ 3825098c2716SMatthias Ringwald hci_release_packet_buffer(); 3826098c2716SMatthias Ringwald } 3827098c2716SMatthias Ringwald } 3828098c2716SMatthias Ringwald } 3829098c2716SMatthias Ringwald 38304ae8623eSMatthias Ringwald // finalize iso stream if handle matches 383115a15be3SMatthias Ringwald iso_stream = hci_iso_stream_for_con_handle(handle); 38324ae8623eSMatthias Ringwald if (iso_stream != NULL){ 38334ae8623eSMatthias Ringwald hci_iso_stream_finalize(iso_stream); 38344ae8623eSMatthias Ringwald break; 38354ae8623eSMatthias Ringwald } 38364ae8623eSMatthias Ringwald #endif 38374ae8623eSMatthias Ringwald 3838c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 3839c6a37cfdSMatthias Ringwald if (!conn) break; 38401714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 38411714cbbdSMatthias Ringwald // pairing failed if it was ongoing 38421714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 38431714cbbdSMatthias Ringwald #endif 3844046ec007SMatthias Ringwald 3845046ec007SMatthias Ringwald // emit dedicatd bonding event 3846046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 3847046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 3848046ec007SMatthias Ringwald } 3849046ec007SMatthias Ringwald 3850459d27b9SMatthias Ringwald // mark connection for shutdown, stop timers, reset state 3851459d27b9SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 3852459d27b9SMatthias Ringwald hci_connection_stop_timer(conn); 3853459d27b9SMatthias Ringwald hci_connection_init(conn); 3854459d27b9SMatthias Ringwald 385535454696SMatthias Ringwald #ifdef ENABLE_BLE 3856d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3857046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 38582b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 3859bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 38609a2e4658SMatthias Ringwald } 386135454696SMatthias Ringwald #endif 3862d70217a2SMatthias Ringwald #endif 3863ccda6e14S[email protected] break; 38646772a24cSmatthias.ringwald 3865c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 3866313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 3867d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 3868c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 38697586ee35S[email protected] } else { 38707586ee35S[email protected] // if no special requests, just reboot stack 38717586ee35S[email protected] hci_power_control_off(); 38727586ee35S[email protected] hci_power_control_on(); 3873c68bdf90Smatthias.ringwald } 3874c68bdf90Smatthias.ringwald break; 3875c68bdf90Smatthias.ringwald 38760e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 38775cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 38785cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 3879c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 3880f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 3881c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3882c4c88f1bSJakob Krantz if (!conn) break; 38835cf766e8SMatthias Ringwald conn->role = packet[9]; 38845cf766e8SMatthias Ringwald break; 38850e6f3837SMatthias Ringwald #endif 38865cf766e8SMatthias Ringwald 388763fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 3888098c2716SMatthias Ringwald // release packet buffer only for asynchronous transport and if there are not further fragments 38894fa24b5fS[email protected] if (hci_transport_synchronous()) { 389063fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 38914fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 38924fa24b5fS[email protected] } 389381d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 3894098c2716SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3895098c2716SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = 0; 3896098c2716SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size) break; 3897098c2716SMatthias Ringwald #endif 3898d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 3899d051460cS[email protected] hci_release_packet_buffer(); 3900701e3307SMatthias Ringwald 39015ade6657SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 39025ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 39035ade6657SMatthias Ringwald #endif 390463fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 390563fa3374SMatthias Ringwald 390635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 390763fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 3908701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 390935454696SMatthias Ringwald #endif 39106b4af23dS[email protected] break; 39116b4af23dS[email protected] 391235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 391363fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 391463fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 39151972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 391663fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 391763fa3374SMatthias Ringwald return; 39181cfb383eSMatthias Ringwald 39191cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 39201cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 39211cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 39221cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 39239784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 39241cfb383eSMatthias Ringwald break; 392535454696SMatthias Ringwald #endif 392663fa3374SMatthias Ringwald 3927a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 39285909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 39295909f7f2Smatthias.ringwald switch (packet[2]){ 3930d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 393157c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 39326fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 393357c9da5bS[email protected] le_handle_advertisement_report(packet, size); 39347bdc6798S[email protected] break; 3935a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 3936a0698cb4SMatthias Ringwald case HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT: 3937a0698cb4SMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 3938a0698cb4SMatthias Ringwald le_handle_extended_advertisement_report(packet, size); 3939a0698cb4SMatthias Ringwald break; 3940ce56b945SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 3941ce56b945SMatthias Ringwald hci_stack->le_periodic_sync_request = LE_CONNECTING_IDLE; 3942ce56b945SMatthias Ringwald hci_stack->le_periodic_sync_state = LE_CONNECTING_IDLE; 3943ce56b945SMatthias Ringwald break; 3944a0698cb4SMatthias Ringwald #endif 3945d70217a2SMatthias Ringwald #endif 39465909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 39470ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 39485909f7f2Smatthias.ringwald break; 39495909f7f2Smatthias.ringwald 3950f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3951c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3952c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3953c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3954c9db5c21SMilanka Ringwald if (!conn) break; 3955c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3956c9db5c21SMilanka Ringwald break; 395765a46ef3S[email protected] 395873cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 395973cd8a2aSMatthias Ringwald // connection 396073cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 396173cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 396273cd8a2aSMatthias Ringwald if (conn) { 396373cd8a2aSMatthias Ringwald // read arguments 396473cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 396573cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 396673cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 396773cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 396873cd8a2aSMatthias Ringwald 396973cd8a2aSMatthias Ringwald // validate against current connection parameter range 397073cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 397173cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 397273cd8a2aSMatthias 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); 397373cd8a2aSMatthias Ringwald if (update_parameter){ 397473cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 397573cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 397673cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 397773cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 397873cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 397973cd8a2aSMatthias Ringwald } else { 3980c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 398173cd8a2aSMatthias Ringwald } 398273cd8a2aSMatthias Ringwald } 398373cd8a2aSMatthias Ringwald break; 39840f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 39850f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 39860f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 39870f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 39880f3b27c5SMatthias Ringwald if (conn) { 39890f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 39900f3b27c5SMatthias Ringwald } 39910f3b27c5SMatthias Ringwald break; 39920f3b27c5SMatthias Ringwald #endif 39934ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 39944ae8623eSMatthias Ringwald case HCI_SUBEVENT_LE_CIS_ESTABLISHED: 39954ae8623eSMatthias Ringwald handle = hci_subevent_le_cis_established_get_connection_handle(packet); 399615a15be3SMatthias Ringwald iso_stream = hci_iso_stream_for_con_handle(handle); 39974ae8623eSMatthias Ringwald if (iso_stream){ 39984ae8623eSMatthias Ringwald uint8_t status = hci_subevent_le_cis_established_get_status(packet); 39993abcd16cSMatthias Ringwald uint8_t cig_id = iso_stream->group_id; 40004ae8623eSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 40014ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_ESTABLISHED; 40024ae8623eSMatthias Ringwald } else { 40034ae8623eSMatthias Ringwald hci_iso_stream_finalize(iso_stream); 40044ae8623eSMatthias Ringwald } 4005f06bb90fSMatthias Ringwald // CIG/CIS 4006f06bb90fSMatthias Ringwald if (iso_stream->iso_type == HCI_ISO_TYPE_CIS){ 40073abcd16cSMatthias Ringwald le_audio_cig_t * cig = hci_cig_for_id(cig_id); 4008f06bb90fSMatthias Ringwald if (cig){ 4009f06bb90fSMatthias Ringwald uint8_t i; 4010f06bb90fSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 4011f06bb90fSMatthias Ringwald if (cig->cis_con_handles[i] == handle){ 40123abcd16cSMatthias Ringwald cig->cis_setup_active[i] = false; 4013*f9306a1aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 4014*f9306a1aSMatthias Ringwald cig->cis_established[i] = true; 4015*f9306a1aSMatthias Ringwald } else { 40163abcd16cSMatthias Ringwald hci_emit_cis_created(cig_id, handle, status); 4017f06bb90fSMatthias Ringwald } 4018f06bb90fSMatthias Ringwald } 4019f06bb90fSMatthias Ringwald } 4020*f9306a1aSMatthias Ringwald // check if complete 4021*f9306a1aSMatthias Ringwald bool complete = true; 4022*f9306a1aSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 4023*f9306a1aSMatthias Ringwald complete &= cig->cis_setup_active[i]; 4024*f9306a1aSMatthias Ringwald } 4025*f9306a1aSMatthias Ringwald cig->state_vars.next_cis = 0; 4026*f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_SETUP_ISO_PATH; 4027*f9306a1aSMatthias Ringwald } 4028f06bb90fSMatthias Ringwald } 40294ae8623eSMatthias Ringwald } 40307aa35f6aSMatthias Ringwald break; 40317aa35f6aSMatthias Ringwald case HCI_SUBEVENT_LE_CREATE_BIG_COMPLETE: 403240f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = 0xff; 40337aa35f6aSMatthias Ringwald big = hci_big_for_handle(packet[4]); 40347aa35f6aSMatthias Ringwald if (big != NULL){ 40357aa35f6aSMatthias Ringwald uint8_t status = packet[3]; 40367aa35f6aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 40377aa35f6aSMatthias Ringwald // store bis_con_handles and trigger iso path setup 40387aa35f6aSMatthias Ringwald uint8_t num_bis = btstack_min(MAX_NR_BIS, packet[20]); 40397aa35f6aSMatthias Ringwald uint8_t i; 40407aa35f6aSMatthias Ringwald for (i=0;i<num_bis;i++){ 4041fb16346aSMatthias Ringwald hci_con_handle_t bis_handle = (hci_con_handle_t) little_endian_read_16(packet, 21 + (2 * i)); 4042fb16346aSMatthias Ringwald big->bis_con_handles[i] = bis_handle; 4043fb16346aSMatthias Ringwald // assign bis handle 4044fb16346aSMatthias Ringwald btstack_linked_list_iterator_t it; 4045fb16346aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 4046fb16346aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4047fb16346aSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 4048fb16346aSMatthias Ringwald if ((iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) && 40498d72db10SMatthias Ringwald (iso_stream->group_id == big->big_handle)){ 4050fb16346aSMatthias Ringwald iso_stream->con_handle = bis_handle; 4051fb16346aSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_ESTABLISHED; 4052cd1139aaSMatthias Ringwald break; 4053fb16346aSMatthias Ringwald } 4054fb16346aSMatthias Ringwald } 40557aa35f6aSMatthias Ringwald } 40562a6c0f82SMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 40577aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 40587aa35f6aSMatthias Ringwald big->state_vars.next_bis = 0; 40592a6c0f82SMatthias Ringwald } 40607aa35f6aSMatthias Ringwald } else { 40612a6c0f82SMatthias Ringwald // create BIG failed or has been stopped by us 4062778463d0SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_BIS, big->big_handle); 40637aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 40642a6c0f82SMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED){ 40657aa35f6aSMatthias Ringwald hci_emit_big_created(big, status); 40662a6c0f82SMatthias Ringwald } else { 40672a6c0f82SMatthias Ringwald hci_emit_big_terminated(big); 40682a6c0f82SMatthias Ringwald } 40697aa35f6aSMatthias Ringwald } 40707aa35f6aSMatthias Ringwald } 40717aa35f6aSMatthias Ringwald break; 40727aa35f6aSMatthias Ringwald case HCI_SUBEVENT_LE_TERMINATE_BIG_COMPLETE: 40737aa35f6aSMatthias Ringwald big = hci_big_for_handle(hci_subevent_le_terminate_big_complete_get_big_handle(packet)); 40747aa35f6aSMatthias Ringwald if (big != NULL){ 4075fb16346aSMatthias Ringwald // finalize associated ISO streams 4076fb16346aSMatthias Ringwald btstack_linked_list_iterator_t it; 4077fb16346aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 4078fb16346aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4079fb16346aSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 40808d72db10SMatthias Ringwald if (iso_stream->group_id == big->big_handle){ 40818d72db10SMatthias Ringwald log_info("BIG Terminated, big_handle 0x%02x, con handle 0x%04x", iso_stream->group_id, iso_stream->con_handle); 4082fb16346aSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4083fb16346aSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 4084fb16346aSMatthias Ringwald } 4085fb16346aSMatthias Ringwald } 40867aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 40877aa35f6aSMatthias Ringwald switch (big->state){ 40887aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED: 40897aa35f6aSMatthias Ringwald hci_emit_big_created(big, big->state_vars.status); 40907aa35f6aSMatthias Ringwald break; 40917aa35f6aSMatthias Ringwald default: 40927aa35f6aSMatthias Ringwald hci_emit_big_terminated(big); 40937aa35f6aSMatthias Ringwald break; 40947aa35f6aSMatthias Ringwald } 40957aa35f6aSMatthias Ringwald } 40967aa35f6aSMatthias Ringwald break; 40972a6c0f82SMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_ESTABLISHED: 40982a6c0f82SMatthias Ringwald big_sync = hci_big_sync_for_handle(packet[4]); 40992a6c0f82SMatthias Ringwald if (big_sync != NULL){ 41002a6c0f82SMatthias Ringwald uint8_t status = packet[3]; 4101d04a9afcSMatthias Ringwald uint8_t big_handle = packet[4]; 41022a6c0f82SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 41032a6c0f82SMatthias Ringwald // store bis_con_handles and trigger iso path setup 41042a6c0f82SMatthias Ringwald uint8_t num_bis = btstack_min(MAX_NR_BIS, packet[16]); 41052a6c0f82SMatthias Ringwald uint8_t i; 41062a6c0f82SMatthias Ringwald for (i=0;i<num_bis;i++){ 41072a6c0f82SMatthias Ringwald big_sync->bis_con_handles[i] = little_endian_read_16(packet, 17 + (2 * i)); 41082a6c0f82SMatthias Ringwald } 41092a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 41102a6c0f82SMatthias Ringwald // trigger iso path setup 41112a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 41122a6c0f82SMatthias Ringwald big_sync->state_vars.next_bis = 0; 41132a6c0f82SMatthias Ringwald } 41142a6c0f82SMatthias Ringwald } else { 41152a6c0f82SMatthias Ringwald // create BIG Sync failed or has been stopped by us 41162a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 41172a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 41182a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, status); 41192a6c0f82SMatthias Ringwald } else { 4120d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 41212a6c0f82SMatthias Ringwald } 41222a6c0f82SMatthias Ringwald } 41232a6c0f82SMatthias Ringwald } 41242a6c0f82SMatthias Ringwald break; 41252a6c0f82SMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 41262a6c0f82SMatthias Ringwald big_sync = hci_big_sync_for_handle(packet[4]); 41272a6c0f82SMatthias Ringwald if (big_sync != NULL){ 4128d04a9afcSMatthias Ringwald uint8_t big_handle = packet[4]; 41292a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 4130d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 41312a6c0f82SMatthias Ringwald } 41322a6c0f82SMatthias Ringwald break; 41334ae8623eSMatthias Ringwald #endif 41345909f7f2Smatthias.ringwald default: 41355909f7f2Smatthias.ringwald break; 41365909f7f2Smatthias.ringwald } 41375909f7f2Smatthias.ringwald break; 41385909f7f2Smatthias.ringwald #endif 41390b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 41400b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 41410b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 41420b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 41430b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 41440b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 41450b36101dSMatthias Ringwald break; 41460b36101dSMatthias Ringwald default: 41470b36101dSMatthias Ringwald break; 41480b36101dSMatthias Ringwald } 41490b36101dSMatthias Ringwald break; 41506772a24cSmatthias.ringwald default: 41516772a24cSmatthias.ringwald break; 4152fe1ed1b8Smatthias.ringwald } 4153fe1ed1b8Smatthias.ringwald 415467c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 415589db417bSmatthias.ringwald 415686805605S[email protected] // notify upper stack 4157d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 415894ab26f8Smatthias.ringwald 415986805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 4160797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 4161f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 416205ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 4163046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 4164797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 416505ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 4166b0136355SMatthias Ringwald } 41678ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 41688ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 41698ad9cf99SMatthias Ringwald #endif 417086805605S[email protected] } 417186805605S[email protected] 417294ab26f8Smatthias.ringwald // execute main loop 417394ab26f8Smatthias.ringwald hci_run(); 417416833f0aSmatthias.ringwald } 417516833f0aSmatthias.ringwald 417635454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 41776f28d2eeSMatthias Ringwald 41786be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 41796f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 41806f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 41816f28d2eeSMatthias Ringwald 41826f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 418359c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 41845a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 41856f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 41866f28d2eeSMatthias Ringwald if (!conn) return; 41876f28d2eeSMatthias Ringwald 4188afaddd67SMatthias Ringwald // trigger send 4189afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 4190e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 4191a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 4192e4157653SMatthias Ringwald conn->sco_tx_ready++; 4193e4157653SMatthias Ringwald } 4194afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 41956f28d2eeSMatthias Ringwald } 41966f28d2eeSMatthias Ringwald 419759c33575SMatthias Ringwald 419859c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 419959c33575SMatthias Ringwald 42006f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 420159c33575SMatthias Ringwald 42026f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 420359c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 420459c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 420559c33575SMatthias Ringwald 420659c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 420759c33575SMatthias Ringwald 420859c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 4209ddbec4ceSMatthias Ringwald btstack_run_loop_remove_timer(timer); 421059c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 421159c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 421259c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 421359c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 42146f28d2eeSMatthias Ringwald } 42156be3cf7fSMatthias Ringwald #endif 42166f28d2eeSMatthias Ringwald 4217c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 42180b3f95dfSMatthias Ringwald // lookup connection struct 42192b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 42202b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 42210b3f95dfSMatthias Ringwald if (!conn) return; 42220b3f95dfSMatthias Ringwald 42236be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 4224760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 4225760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 4226a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 4227760b20efSMatthias Ringwald packet[2] = 0x3c; 4228760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 4229760b20efSMatthias Ringwald size = 63; 42300b3f95dfSMatthias Ringwald } 42310b3f95dfSMatthias Ringwald } 4232760b20efSMatthias Ringwald 4233f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 4234f234b250SMatthias Ringwald // Nothing to do 4235f234b250SMatthias Ringwald } else { 423649205f5dSMatthias 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); 4237760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 42386f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 423959c33575SMatthias Ringwald 424059c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 424159c33575SMatthias Ringwald // ignore first 10 packets 42426f28d2eeSMatthias Ringwald conn->sco_rx_count++; 424359c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 424459c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 424559c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 42466f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 42476f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 424859c33575SMatthias Ringwald conn->sco_rx_count = 0; 424959c33575SMatthias Ringwald sco_schedule_tx(conn); 425059c33575SMatthias Ringwald } 425159c33575SMatthias Ringwald } else { 425259c33575SMatthias Ringwald // track expected arrival timme 425359c33575SMatthias Ringwald conn->sco_rx_count++; 425459c33575SMatthias Ringwald conn->sco_rx_ms += 7; 425559c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 425659c33575SMatthias Ringwald if (delta > 0){ 425759c33575SMatthias Ringwald conn->sco_rx_ms++; 425859c33575SMatthias Ringwald } 425959c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 42606f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 42616f28d2eeSMatthias Ringwald } 4262760b20efSMatthias Ringwald } 4263f234b250SMatthias Ringwald } 42646be3cf7fSMatthias Ringwald #endif 42656be3cf7fSMatthias Ringwald 42660b3f95dfSMatthias Ringwald // deliver to app 42670b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 42680b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 42690b3f95dfSMatthias Ringwald } 42700b3f95dfSMatthias Ringwald 4271ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 4272ed325439SMatthias Ringwald // We can send one packet for each received packet 4273ed325439SMatthias Ringwald conn->sco_tx_ready++; 4274ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 4275ed325439SMatthias Ringwald #endif 4276ed325439SMatthias Ringwald 42770b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 42782b838201SMatthias Ringwald conn->num_packets_completed++; 42792b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 42802b838201SMatthias Ringwald hci_run(); 42812b838201SMatthias Ringwald #endif 4282c91d150bS[email protected] } 428335454696SMatthias Ringwald #endif 4284c91d150bS[email protected] 42850a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 42865bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 428710e830c9Smatthias.ringwald switch (packet_type) { 428810e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 428910e830c9Smatthias.ringwald event_handler(packet, size); 429010e830c9Smatthias.ringwald break; 429110e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 429210e830c9Smatthias.ringwald acl_handler(packet, size); 429310e830c9Smatthias.ringwald break; 429435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4295c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 4296c91d150bS[email protected] sco_handler(packet, size); 4297202c8a4cSMatthias Ringwald break; 429835454696SMatthias Ringwald #endif 42990a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 43005027c498SMatthias Ringwald case HCI_ISO_DATA_PACKET: 4301d39e4f95SMatthias Ringwald hci_iso_packet_handler(packet, size); 43025027c498SMatthias Ringwald break; 43035027c498SMatthias Ringwald #endif 430410e830c9Smatthias.ringwald default: 430510e830c9Smatthias.ringwald break; 430610e830c9Smatthias.ringwald } 430710e830c9Smatthias.ringwald } 430810e830c9Smatthias.ringwald 4309d6b06661SMatthias Ringwald /** 4310d6b06661SMatthias Ringwald * @brief Add event packet handler. 4311d6b06661SMatthias Ringwald */ 4312d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 4313d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 4314d6b06661SMatthias Ringwald } 4315d6b06661SMatthias Ringwald 431667f708e0SMatthias Ringwald /** 431767f708e0SMatthias Ringwald * @brief Remove event packet handler. 431867f708e0SMatthias Ringwald */ 431967f708e0SMatthias Ringwald void hci_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){ 432067f708e0SMatthias Ringwald btstack_linked_list_remove(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 432167f708e0SMatthias Ringwald } 4322d6b06661SMatthias Ringwald 4323fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 43243d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 4325fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 432616833f0aSmatthias.ringwald } 432716833f0aSmatthias.ringwald 432835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 43298abbe8b5SMatthias Ringwald /** 43308abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 43318abbe8b5SMatthias Ringwald */ 43323d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 43338abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 43348abbe8b5SMatthias Ringwald } 433535454696SMatthias Ringwald #endif 43368abbe8b5SMatthias Ringwald 43370a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 43385027c498SMatthias Ringwald void hci_register_iso_packet_handler(btstack_packet_handler_t handler){ 43395027c498SMatthias Ringwald hci_stack->iso_packet_handler = handler; 43405027c498SMatthias Ringwald } 43415027c498SMatthias Ringwald #endif 43425027c498SMatthias Ringwald 434371de195eSMatthias Ringwald static void hci_state_reset(void){ 4344595bdbfbS[email protected] // no connections yet 4345595bdbfbS[email protected] hci_stack->connections = NULL; 434674308b23Smatthias.ringwald 434774308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 434874308b23Smatthias.ringwald // hci_stack->discoverable = 0; 434974308b23Smatthias.ringwald // hci_stack->connectable = 0; 435074308b23Smatthias.ringwald // hci_stack->bondable = 1; 4351b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 4352595bdbfbS[email protected] 435344935e40S[email protected] // buffer is free 435402c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 435544935e40S[email protected] 4356595bdbfbS[email protected] // no pending cmds 4357595bdbfbS[email protected] hci_stack->decline_reason = 0; 4358595bdbfbS[email protected] 4359c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 4360c214d65bSMatthias Ringwald 4361bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 4362496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 436332a12730SMatthias Ringwald hci_stack->page_timeout = 0x6000; // ca. 15 sec 436432a12730SMatthias Ringwald 4365baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic = 436659d59ecfSMatthias Ringwald GAP_TASK_SET_DEFAULT_LINK_POLICY | 436759d59ecfSMatthias Ringwald GAP_TASK_SET_CLASS_OF_DEVICE | 436859d59ecfSMatthias Ringwald GAP_TASK_SET_LOCAL_NAME | 4369bc2dcc03SMatthias Ringwald GAP_TASK_SET_EIR_DATA | 437032a12730SMatthias Ringwald GAP_TASK_WRITE_SCAN_ENABLE | 437132a12730SMatthias Ringwald GAP_TASK_WRITE_PAGE_TIMEOUT; 4372bea424a5SMatthias Ringwald #endif 4373bea424a5SMatthias Ringwald 4374cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 43758d5f0979SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 43761ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 4377cf01e888SMatthias Ringwald #endif 4378cf01e888SMatthias Ringwald 4379595bdbfbS[email protected] // LE 4380b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 4381b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 4382b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 4383d70217a2SMatthias Ringwald #endif 4384d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4385a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 438673799937SMatthias Ringwald hci_stack->le_scanning_param_update = true; 4387b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 4388d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 4389e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 43904bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 43914bafedd5SMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = HCI_CON_HANDLE_INVALID; 43924bafedd5SMatthias Ringwald #endif 4393d70217a2SMatthias Ringwald #endif 4394a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4395935aa76eSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 43966bf435a6SMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_PARAMS_SET) != 0){ 4397a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 4398a61834b6SMatthias Ringwald } 4399a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 4400a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 4401a61834b6SMatthias Ringwald } 4402a61834b6SMatthias Ringwald #endif 440340f86dfdSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 440440f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = 0xff; 440540f86dfdSMatthias Ringwald #endif 4406595bdbfbS[email protected] } 4407595bdbfbS[email protected] 440835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 44092d5e09d6SMatthias Ringwald /** 44102d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 44112d5e09d6SMatthias Ringwald */ 44122d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 44132d5e09d6SMatthias Ringwald // store and open remote device db 44142d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 44152d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 44162d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 44172d5e09d6SMatthias Ringwald } 44182d5e09d6SMatthias Ringwald } 441935454696SMatthias Ringwald #endif 44202d5e09d6SMatthias Ringwald 44212d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 4422475c8125Smatthias.ringwald 44233a9fb326S[email protected] #ifdef HAVE_MALLOC 44243a9fb326S[email protected] if (!hci_stack) { 44253a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 44263a9fb326S[email protected] } 44273a9fb326S[email protected] #else 44283a9fb326S[email protected] hci_stack = &hci_stack_static; 44293a9fb326S[email protected] #endif 443066fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 44313a9fb326S[email protected] 4432475c8125Smatthias.ringwald // reference to use transport layer implementation 44333a9fb326S[email protected] hci_stack->hci_transport = transport; 4434475c8125Smatthias.ringwald 443511e23e5fSmatthias.ringwald // reference to used config 44363a9fb326S[email protected] hci_stack->config = config; 443711e23e5fSmatthias.ringwald 443826a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 443926a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 444026a9b6daSMatthias Ringwald 44418fcba05dSmatthias.ringwald // max acl payload size defined in config.h 44423a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 44438fcba05dSmatthias.ringwald 444416833f0aSmatthias.ringwald // register packet handlers with transport 444510e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 4446f5454fc6Smatthias.ringwald 44473a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 4448e2386ba1S[email protected] 4449e2386ba1S[email protected] // class of device 44503a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 4451a45d6b9fS[email protected] 4452f20168b8Smatthias.ringwald // bondable by default 4453f20168b8Smatthias.ringwald hci_stack->bondable = 1; 4454f20168b8Smatthias.ringwald 4455e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 445663168530SMatthias Ringwald // classic name 445763168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 4458c4c88f1bSJakob Krantz 4459c4c88f1bSJakob Krantz // Master slave policy 4460c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 4461170fafaeSMatthias Ringwald 4462b4eb4420SMatthias Ringwald // Allow Role Switch 4463b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 4464b4eb4420SMatthias Ringwald 446578315a58SMatthias Ringwald // Default / minimum security level = 2 446678315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 446778315a58SMatthias Ringwald 44685dbec6b3SMatthias Ringwald // Default Security Mode 4 44695dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 44705dbec6b3SMatthias Ringwald 4471cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 4472cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 4473d821984bSMatthias Ringwald 4474d821984bSMatthias Ringwald // Link Supervision Timeout 4475d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 4476d821984bSMatthias Ringwald 4477e9f343c8SMatthias Ringwald #endif 447863168530SMatthias Ringwald 447963048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 44803a9fb326S[email protected] hci_stack->ssp_enable = 1; 44813a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 44823a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 44833a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 448469a97523S[email protected] 44855d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 44865d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 44875d23aae8SMatthias Ringwald 4488fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 4489fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 4490d950d659SMatthias Ringwald 4491831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4492831711daSMatthias Ringwald // connection parameter to use for outgoing connections 4493cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 4494cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 4495831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 4496831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 4497831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 4498831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 4499831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 4500831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 45017261e5d8SMatthias Ringwald 45027261e5d8SMatthias Ringwald // default LE Scanning 45038b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 45048b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 45058b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 4506831711daSMatthias Ringwald #endif 4507831711daSMatthias Ringwald 45082b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 45092b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 45102b6ab3e6SMatthias Ringwald #endif 45112b6ab3e6SMatthias Ringwald 4512831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 4513831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 4514831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 4515831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 4516831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 4517831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 4518831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 4519831711daSMatthias Ringwald 452001d4f05fSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 452101d4f05fSMatthias Ringwald hci_stack->iso_packets_to_queue = 1; 452201d4f05fSMatthias Ringwald #endif 452301d4f05fSMatthias Ringwald 4524595bdbfbS[email protected] hci_state_reset(); 4525475c8125Smatthias.ringwald } 4526475c8125Smatthias.ringwald 45274688f216SMatthias Ringwald void hci_deinit(void){ 4528b02cfc15SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 45294688f216SMatthias Ringwald #ifdef HAVE_MALLOC 45304688f216SMatthias Ringwald if (hci_stack) { 45314688f216SMatthias Ringwald free(hci_stack); 45324688f216SMatthias Ringwald } 45334688f216SMatthias Ringwald #endif 45344688f216SMatthias Ringwald hci_stack = NULL; 4535b5bbcbf4SMatthias Ringwald 4536b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 45374688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 4538b5bbcbf4SMatthias Ringwald #endif 45394688f216SMatthias Ringwald } 45404688f216SMatthias Ringwald 45413fb36a29SMatthias Ringwald /** 45423fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 45433fb36a29SMatthias Ringwald */ 45443fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 45453fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 45463fb36a29SMatthias Ringwald 45473fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 45483fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 45493fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 45503fb36a29SMatthias Ringwald } 45513fb36a29SMatthias Ringwald } 45523fb36a29SMatthias Ringwald 4553fb55bd0aSMatthias Ringwald /** 4554d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 4555fb55bd0aSMatthias Ringwald */ 4556fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 4557fb55bd0aSMatthias Ringwald // references to used control implementation 4558fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 4559d0b87befSMatthias Ringwald // init with transport config 4560d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 4561fb55bd0aSMatthias Ringwald } 4562fb55bd0aSMatthias Ringwald 45631f9eb2ccSMatthias Ringwald static void hci_discard_connections(void){ 4564346f8c2cSMatthias Ringwald btstack_linked_list_iterator_t it; 4565346f8c2cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4566346f8c2cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 45671f9eb2ccSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 4568346f8c2cSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 45691f9eb2ccSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 45701f9eb2ccSMatthias Ringwald hci_shutdown_connection(connection); 45711f9eb2ccSMatthias Ringwald } 4572346f8c2cSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 4573346f8c2cSMatthias Ringwald while (hci_stack->iso_streams != NULL){ 4574346f8c2cSMatthias Ringwald hci_iso_stream_finalize((hci_iso_stream_t *) hci_stack->iso_streams); 4575346f8c2cSMatthias Ringwald } 4576346f8c2cSMatthias Ringwald #endif 45771f9eb2ccSMatthias Ringwald } 45781f9eb2ccSMatthias Ringwald 457971de195eSMatthias Ringwald void hci_close(void){ 4580e6d6524dSMatthias Ringwald 4581e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 4582404843c1Smatthias.ringwald // close remote device db 4583a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 4584a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 4585404843c1Smatthias.ringwald } 4586e6d6524dSMatthias Ringwald #endif 45877224be7eSMatthias Ringwald 45881f9eb2ccSMatthias Ringwald hci_discard_connections(); 45897224be7eSMatthias Ringwald 4590f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 45913a9fb326S[email protected] 45923a9fb326S[email protected] #ifdef HAVE_MALLOC 45933a9fb326S[email protected] free(hci_stack); 45943a9fb326S[email protected] #endif 45953a9fb326S[email protected] hci_stack = NULL; 4596404843c1Smatthias.ringwald } 4597404843c1Smatthias.ringwald 4598cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 4599cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 4600cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 4601cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 4602cb70c5abSMatthias Ringwald } 4603cb70c5abSMatthias Ringwald #endif 4604cb70c5abSMatthias Ringwald 460535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4606170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 4607170fafaeSMatthias Ringwald // validate ranage and set 4608170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 4609170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 4610170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 4611170fafaeSMatthias Ringwald } 461278315a58SMatthias Ringwald 4613137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 4614137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 46155dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 4616137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 4617137715ebSMatthias Ringwald } else { 4618137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 4619137715ebSMatthias Ringwald } 46205dbec6b3SMatthias Ringwald } 46215dbec6b3SMatthias Ringwald 46225dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 46235dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 46245dbec6b3SMatthias Ringwald } 46255dbec6b3SMatthias Ringwald 462678315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 462778315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 462878315a58SMatthias Ringwald } 462978315a58SMatthias Ringwald 463078315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 4631d7387af3SMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 4632d7387af3SMatthias Ringwald return LEVEL_4; 4633d7387af3SMatthias Ringwald } 463478315a58SMatthias Ringwald return hci_stack->gap_security_level; 463578315a58SMatthias Ringwald } 463630cdf3c6SMatthias Ringwald 46378ad4dfffSMatthias Ringwald void gap_set_minimal_service_security_level(gap_security_level_t security_level){ 46388ad4dfffSMatthias Ringwald hci_stack->gap_minimal_service_security_level = security_level; 46398ad4dfffSMatthias Ringwald } 46408ad4dfffSMatthias Ringwald 464130cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 464230cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 464330cdf3c6SMatthias Ringwald } 464430cdf3c6SMatthias Ringwald 464530cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 464630cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 464730cdf3c6SMatthias Ringwald } 4648170fafaeSMatthias Ringwald #endif 4649170fafaeSMatthias Ringwald 4650170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 465160b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 46529e61646fS[email protected] hci_stack->class_of_device = class_of_device; 4653baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_CLASS_OF_DEVICE; 465459d59ecfSMatthias Ringwald hci_run(); 46559e61646fS[email protected] } 465676f27cffSMatthias Ringwald 4657c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 4658c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 4659baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_DEFAULT_LINK_POLICY; 466059d59ecfSMatthias Ringwald hci_run(); 4661c33e56d3SMatthias Ringwald } 4662c33e56d3SMatthias Ringwald 4663b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 4664b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 4665b4eb4420SMatthias Ringwald } 4666b4eb4420SMatthias Ringwald 4667b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 4668b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 4669b4eb4420SMatthias Ringwald } 4670b4eb4420SMatthias Ringwald 46710c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 46720c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 46730c3eb48dSMatthias Ringwald } 46740c3eb48dSMatthias Ringwald 46759afaa4d4SMatthias Ringwald void gap_enable_link_watchdog(uint16_t timeout_ms){ 46769afaa4d4SMatthias Ringwald hci_stack->automatic_flush_timeout = btstack_min(timeout_ms, 1280) * 8 / 5; // divide by 0.625 46779afaa4d4SMatthias Ringwald } 46789afaa4d4SMatthias Ringwald 46799afaa4d4SMatthias Ringwald uint16_t hci_automatic_flush_timeout(void){ 46809afaa4d4SMatthias Ringwald return hci_stack->automatic_flush_timeout; 46819afaa4d4SMatthias Ringwald } 46829afaa4d4SMatthias Ringwald 468376f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 468476f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 468576f27cffSMatthias Ringwald } 468635454696SMatthias Ringwald #endif 46879e61646fS[email protected] 46886fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 4689f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 4690f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 46916535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 4692f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 4693f456b2d0S[email protected] } 469476f27cffSMatthias Ringwald #endif 4695f456b2d0S[email protected] 46968d213e1aSmatthias.ringwald // State-Module-Driver overview 46978d213e1aSmatthias.ringwald // state module low-level 46988d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 46998d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 47008d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 47018d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 4702d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 4703d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 4704c7e0c5f6Smatthias.ringwald 470540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 47067301ad89Smatthias.ringwald 4707038bc64cSmatthias.ringwald // power on 4708f9a30166Smatthias.ringwald int err = 0; 47093a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 4710d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 4711f9a30166Smatthias.ringwald } 4712038bc64cSmatthias.ringwald if (err){ 47139da54300S[email protected] log_error( "POWER_ON failed"); 4714038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 4715038bc64cSmatthias.ringwald return err; 4716038bc64cSmatthias.ringwald } 4717038bc64cSmatthias.ringwald 471824b3c629SMatthias Ringwald // int chipset driver 47193fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 47203fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 47213fb36a29SMatthias Ringwald } 47223fb36a29SMatthias Ringwald 472324b3c629SMatthias Ringwald // init transport 472424b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 472524b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 472624b3c629SMatthias Ringwald } 472724b3c629SMatthias Ringwald 472824b3c629SMatthias Ringwald // open transport 472924b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 4730038bc64cSmatthias.ringwald if (err){ 47319da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 47323a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4733d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4734f9a30166Smatthias.ringwald } 4735038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 4736038bc64cSmatthias.ringwald return err; 4737038bc64cSmatthias.ringwald } 47388d213e1aSmatthias.ringwald return 0; 47398d213e1aSmatthias.ringwald } 4740038bc64cSmatthias.ringwald 474140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 47428d213e1aSmatthias.ringwald 47439da54300S[email protected] log_info("hci_power_control_off"); 47449418f9c9Smatthias.ringwald 47458d213e1aSmatthias.ringwald // close low-level device 474624b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 47478d213e1aSmatthias.ringwald 47489da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 47499418f9c9Smatthias.ringwald 47508d213e1aSmatthias.ringwald // power off 47513a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4752d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 47538d213e1aSmatthias.ringwald } 47549418f9c9Smatthias.ringwald 47559da54300S[email protected] log_info("hci_power_control_off - control closed"); 47569418f9c9Smatthias.ringwald 47573a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 475872ea5239Smatthias.ringwald } 475972ea5239Smatthias.ringwald 476040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 476172ea5239Smatthias.ringwald 47629da54300S[email protected] log_info("hci_power_control_sleep"); 47633144bce4Smatthias.ringwald 4764b429b9b7Smatthias.ringwald #if 0 4765b429b9b7Smatthias.ringwald // don't close serial port during sleep 4766b429b9b7Smatthias.ringwald 476772ea5239Smatthias.ringwald // close low-level device 47683a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 4769b429b9b7Smatthias.ringwald #endif 477072ea5239Smatthias.ringwald 477172ea5239Smatthias.ringwald // sleep mode 47723a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 4773d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 477472ea5239Smatthias.ringwald } 4775b429b9b7Smatthias.ringwald 47763a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 47778d213e1aSmatthias.ringwald } 47788d213e1aSmatthias.ringwald 477940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 4780b429b9b7Smatthias.ringwald 47819da54300S[email protected] log_info("hci_power_control_wake"); 4782b429b9b7Smatthias.ringwald 4783b429b9b7Smatthias.ringwald // wake on 47843a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 4785d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 4786b429b9b7Smatthias.ringwald } 4787b429b9b7Smatthias.ringwald 4788b429b9b7Smatthias.ringwald #if 0 4789b429b9b7Smatthias.ringwald // open low-level device 47903a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 4791b429b9b7Smatthias.ringwald if (err){ 47929da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 47933a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4794d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4795b429b9b7Smatthias.ringwald } 4796b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 4797b429b9b7Smatthias.ringwald return err; 4798b429b9b7Smatthias.ringwald } 4799b429b9b7Smatthias.ringwald #endif 4800b429b9b7Smatthias.ringwald 4801b429b9b7Smatthias.ringwald return 0; 4802b429b9b7Smatthias.ringwald } 4803b429b9b7Smatthias.ringwald 48040c88d5efSMatthias Ringwald static void hci_power_enter_initializing_state(void){ 480544935e40S[email protected] // set up state machine 480644935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 480702c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 480844935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 48095c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 481044935e40S[email protected] } 4811b429b9b7Smatthias.ringwald 48120c88d5efSMatthias Ringwald static void hci_power_enter_halting_state(void){ 48130c88d5efSMatthias Ringwald #ifdef ENABLE_BLE 48140c88d5efSMatthias Ringwald hci_whitelist_free(); 4815a2c9d908SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 4816a2c9d908SMatthias Ringwald hci_periodic_advertiser_list_free(); 4817a2c9d908SMatthias Ringwald #endif 48180c88d5efSMatthias Ringwald #endif 4819184dbe2fSMatthias Ringwald // see hci_run 4820184dbe2fSMatthias Ringwald hci_stack->state = HCI_STATE_HALTING; 48210c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_CLASSIC_STOP; 4822184dbe2fSMatthias Ringwald // setup watchdog timer for disconnect - only triggers if Controller does not respond anymore 4823184dbe2fSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 1000); 4824184dbe2fSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4825184dbe2fSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 4826184dbe2fSMatthias Ringwald } 4827184dbe2fSMatthias Ringwald 482842bd3d77SMatthias Ringwald // returns error 482942bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 483042bd3d77SMatthias Ringwald int err; 48318d213e1aSmatthias.ringwald switch (power_mode){ 48328d213e1aSmatthias.ringwald case HCI_POWER_ON: 48338d213e1aSmatthias.ringwald err = hci_power_control_on(); 483442bd3d77SMatthias Ringwald if (err != 0) { 4835f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 483697b61c7bS[email protected] return err; 483797b61c7bS[email protected] } 48380c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 48398d213e1aSmatthias.ringwald break; 48408d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48418d213e1aSmatthias.ringwald // do nothing 48428d213e1aSmatthias.ringwald break; 48438d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4844b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 48458d213e1aSmatthias.ringwald break; 48467bbeb3adSMilanka Ringwald default: 48477bbeb3adSMilanka Ringwald btstack_assert(false); 48487bbeb3adSMilanka Ringwald break; 48498d213e1aSmatthias.ringwald } 485042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 485142bd3d77SMatthias Ringwald } 48527301ad89Smatthias.ringwald 485342bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 48548d213e1aSmatthias.ringwald switch (power_mode){ 48558d213e1aSmatthias.ringwald case HCI_POWER_ON: 48568d213e1aSmatthias.ringwald // do nothing 48578d213e1aSmatthias.ringwald break; 48588d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48598d213e1aSmatthias.ringwald // no connections yet, just turn it off 48608d213e1aSmatthias.ringwald hci_power_control_off(); 48618d213e1aSmatthias.ringwald break; 48628d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4863b546ac54Smatthias.ringwald // no connections yet, just turn it off 486472ea5239Smatthias.ringwald hci_power_control_sleep(); 48658d213e1aSmatthias.ringwald break; 48667bbeb3adSMilanka Ringwald default: 48677bbeb3adSMilanka Ringwald btstack_assert(false); 48687bbeb3adSMilanka Ringwald break; 48698d213e1aSmatthias.ringwald } 487042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 487142bd3d77SMatthias Ringwald } 48727301ad89Smatthias.ringwald 487342bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 48748d213e1aSmatthias.ringwald switch (power_mode){ 48758d213e1aSmatthias.ringwald case HCI_POWER_ON: 48768d213e1aSmatthias.ringwald // do nothing 48778d213e1aSmatthias.ringwald break; 48788d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48790c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 48808d213e1aSmatthias.ringwald break; 48818d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4882b546ac54Smatthias.ringwald // see hci_run 48833a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 488474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 48858d213e1aSmatthias.ringwald break; 48867bbeb3adSMilanka Ringwald default: 48877bbeb3adSMilanka Ringwald btstack_assert(false); 48887bbeb3adSMilanka Ringwald break; 48898d213e1aSmatthias.ringwald } 489042bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 489142bd3d77SMatthias Ringwald } 48927301ad89Smatthias.ringwald 489342bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 48948d213e1aSmatthias.ringwald switch (power_mode){ 48958d213e1aSmatthias.ringwald case HCI_POWER_ON: 48960c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 48978d213e1aSmatthias.ringwald break; 48988d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48998d213e1aSmatthias.ringwald // do nothing 49008d213e1aSmatthias.ringwald break; 49018d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4902b546ac54Smatthias.ringwald // see hci_run 49033a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 490474b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 49058d213e1aSmatthias.ringwald break; 49067bbeb3adSMilanka Ringwald default: 49077bbeb3adSMilanka Ringwald btstack_assert(false); 49087bbeb3adSMilanka Ringwald break; 49098d213e1aSmatthias.ringwald } 491000278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 491142bd3d77SMatthias Ringwald } 49128d213e1aSmatthias.ringwald 491342bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 49148d213e1aSmatthias.ringwald switch (power_mode){ 49158d213e1aSmatthias.ringwald case HCI_POWER_ON: 49160c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 49178d213e1aSmatthias.ringwald break; 49188d213e1aSmatthias.ringwald case HCI_POWER_OFF: 49190c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 49208d213e1aSmatthias.ringwald break; 49218d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4922b546ac54Smatthias.ringwald // do nothing 49238d213e1aSmatthias.ringwald break; 49247bbeb3adSMilanka Ringwald default: 49257bbeb3adSMilanka Ringwald btstack_assert(false); 49267bbeb3adSMilanka Ringwald break; 49278d213e1aSmatthias.ringwald } 492842bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 492942bd3d77SMatthias Ringwald } 49308d213e1aSmatthias.ringwald 493142bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 493242bd3d77SMatthias Ringwald int err; 49338d213e1aSmatthias.ringwald switch (power_mode){ 49348d213e1aSmatthias.ringwald case HCI_POWER_ON: 49353144bce4Smatthias.ringwald err = hci_power_control_wake(); 49363144bce4Smatthias.ringwald if (err) return err; 49370c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 49388d213e1aSmatthias.ringwald break; 49398d213e1aSmatthias.ringwald case HCI_POWER_OFF: 49400c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 49418d213e1aSmatthias.ringwald break; 49428d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4943b546ac54Smatthias.ringwald // do nothing 49448d213e1aSmatthias.ringwald break; 49457bbeb3adSMilanka Ringwald default: 49467bbeb3adSMilanka Ringwald btstack_assert(false); 49477bbeb3adSMilanka Ringwald break; 49488d213e1aSmatthias.ringwald } 494942bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 495042bd3d77SMatthias Ringwald } 49517bbeb3adSMilanka Ringwald 495242bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 495342bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 495424f87726SMatthias Ringwald int err = 0; 495542bd3d77SMatthias Ringwald switch (hci_stack->state){ 495642bd3d77SMatthias Ringwald case HCI_STATE_OFF: 495742bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 495842bd3d77SMatthias Ringwald break; 495942bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 496042bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 496142bd3d77SMatthias Ringwald break; 496242bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 496342bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 496442bd3d77SMatthias Ringwald break; 496542bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 496642bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 496742bd3d77SMatthias Ringwald break; 496842bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 496942bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 497042bd3d77SMatthias Ringwald break; 497142bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 497242bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 497342bd3d77SMatthias Ringwald break; 49747bbeb3adSMilanka Ringwald default: 49757bbeb3adSMilanka Ringwald btstack_assert(false); 49767bbeb3adSMilanka Ringwald break; 497711e23e5fSmatthias.ringwald } 497824f87726SMatthias Ringwald if (err != 0){ 497942bd3d77SMatthias Ringwald return err; 498042bd3d77SMatthias Ringwald } 498168d92d03Smatthias.ringwald 4982038bc64cSmatthias.ringwald // create internal event 4983ee8bf225Smatthias.ringwald hci_emit_state(); 4984ee8bf225Smatthias.ringwald 498568d92d03Smatthias.ringwald // trigger next/first action 498668d92d03Smatthias.ringwald hci_run(); 498768d92d03Smatthias.ringwald 4988475c8125Smatthias.ringwald return 0; 4989475c8125Smatthias.ringwald } 4990475c8125Smatthias.ringwald 499135454696SMatthias Ringwald 4992091e35e3SMatthias Ringwald static void hci_halting_run(void) { 4993091e35e3SMatthias Ringwald 4994091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 4995091e35e3SMatthias Ringwald 4996091e35e3SMatthias Ringwald hci_connection_t *connection; 4997d5f71a7eSMatthias Ringwald #ifdef ENABLE_BLE 4998d5f71a7eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4999911801caSMatthias Ringwald bool stop_advertismenets; 5000d5f71a7eSMatthias Ringwald #endif 5001d5f71a7eSMatthias Ringwald #endif 5002091e35e3SMatthias Ringwald 5003091e35e3SMatthias Ringwald switch (hci_stack->substate) { 50040c88d5efSMatthias Ringwald case HCI_HALTING_CLASSIC_STOP: 50050c88d5efSMatthias Ringwald #ifdef ENABLE_CLASSIC 5006911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5007911801caSMatthias Ringwald 50080c88d5efSMatthias Ringwald if (hci_stack->connectable || hci_stack->discoverable){ 50090c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_ADV_STOP; 50100c88d5efSMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, 0); 50110c88d5efSMatthias Ringwald return; 50120c88d5efSMatthias Ringwald } 50130c88d5efSMatthias Ringwald #endif 50140c88d5efSMatthias Ringwald /* fall through */ 50150c88d5efSMatthias Ringwald 50160c88d5efSMatthias Ringwald case HCI_HALTING_LE_ADV_STOP: 50170c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_ADV_STOP; 50180c88d5efSMatthias Ringwald 50190c88d5efSMatthias Ringwald #ifdef ENABLE_BLE 50200c88d5efSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5021911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5022911801caSMatthias Ringwald 5023911801caSMatthias Ringwald stop_advertismenets = (hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0; 5024911801caSMatthias Ringwald 5025911801caSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5026911801caSMatthias Ringwald if (hci_extended_advertising_supported()){ 5027c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 5028911801caSMatthias Ringwald btstack_linked_list_iterator_t it; 5029911801caSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5030911801caSMatthias Ringwald // stop all periodic advertisements and check if an extended set is active 5031911801caSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5032911801caSMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 5033911801caSMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) != 0) { 5034911801caSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 5035911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, 0, advertising_set->advertising_handle); 50360c88d5efSMatthias Ringwald return; 50370c88d5efSMatthias Ringwald } 5038911801caSMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0) { 5039911801caSMatthias Ringwald stop_advertismenets = true; 5040911801caSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5041911801caSMatthias Ringwald } 5042911801caSMatthias Ringwald } 5043c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5044911801caSMatthias Ringwald if (stop_advertismenets){ 5045911801caSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5046911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 0, NULL, NULL, NULL); 5047911801caSMatthias Ringwald return; 5048911801caSMatthias Ringwald } 5049911801caSMatthias Ringwald } 5050911801caSMatthias Ringwald else 5051a2c9d908SMatthias Ringwald #else /* ENABLE_LE_PERIPHERAL */ 5052911801caSMatthias Ringwald { 5053911801caSMatthias Ringwald if (stop_advertismenets) { 5054911801caSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5055911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 5056911801caSMatthias Ringwald return; 5057911801caSMatthias Ringwald } 5058911801caSMatthias Ringwald } 5059911801caSMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING*/ 5060911801caSMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 5061911801caSMatthias Ringwald #endif /* ENABLE_BLE */ 5062911801caSMatthias Ringwald 50630c88d5efSMatthias Ringwald /* fall through */ 50640c88d5efSMatthias Ringwald 50650c88d5efSMatthias Ringwald case HCI_HALTING_LE_SCAN_STOP: 50660c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_SCAN_STOP; 5067911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5068091e35e3SMatthias Ringwald 5069091e35e3SMatthias Ringwald #ifdef ENABLE_BLE 5070091e35e3SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 50710c88d5efSMatthias Ringwald if (hci_stack->le_scanning_active){ 5072c42da3bcSMatthias Ringwald hci_le_scan_stop(); 50730c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL; 50740c88d5efSMatthias Ringwald return; 50750c88d5efSMatthias Ringwald } 5076091e35e3SMatthias Ringwald #endif 5077091e35e3SMatthias Ringwald #endif 50780c88d5efSMatthias Ringwald 50790c88d5efSMatthias Ringwald /* fall through */ 50800c88d5efSMatthias Ringwald 50810c88d5efSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL: 50820c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL; 5083911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 50840c88d5efSMatthias Ringwald 5085091e35e3SMatthias Ringwald // close all open connections 5086091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 5087091e35e3SMatthias Ringwald if (connection) { 5088091e35e3SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 5089091e35e3SMatthias Ringwald 5090091e35e3SMatthias Ringwald // check state 5091091e35e3SMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 5092091e35e3SMatthias Ringwald connection->state = SENT_DISCONNECT; 5093091e35e3SMatthias Ringwald 5094091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 5095091e35e3SMatthias Ringwald 5096091e35e3SMatthias Ringwald // finally, send the disconnect command 5097091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5098091e35e3SMatthias Ringwald return; 5099091e35e3SMatthias Ringwald } 5100091e35e3SMatthias Ringwald 5101346f8c2cSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 5102346f8c2cSMatthias Ringwald // stop BIGs and BIG Syncs 5103346f8c2cSMatthias Ringwald if (hci_stack->le_audio_bigs != NULL){ 5104346f8c2cSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t*) hci_stack->le_audio_bigs; 5105346f8c2cSMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_TERMINATED) return; 5106346f8c2cSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 5107346f8c2cSMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle); 5108346f8c2cSMatthias Ringwald return; 5109346f8c2cSMatthias Ringwald } 5110346f8c2cSMatthias Ringwald if (hci_stack->le_audio_big_syncs != NULL){ 5111346f8c2cSMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t*) hci_stack->le_audio_big_syncs; 5112346f8c2cSMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_TERMINATED) return; 5113346f8c2cSMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 5114346f8c2cSMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 5115346f8c2cSMatthias Ringwald return; 5116346f8c2cSMatthias Ringwald } 5117346f8c2cSMatthias Ringwald #endif 5118346f8c2cSMatthias Ringwald 5119091e35e3SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 5120091e35e3SMatthias Ringwald 5121091e35e3SMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 5122091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 51230c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_CLOSE_TIMER; 5124091e35e3SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 5125091e35e3SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 5126091e35e3SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 5127091e35e3SMatthias Ringwald break; 5128091e35e3SMatthias Ringwald 5129091e35e3SMatthias Ringwald case HCI_HALTING_CLOSE: 5130091e35e3SMatthias Ringwald // close left over connections (that had not been properly closed before) 5131091e35e3SMatthias Ringwald hci_discard_connections(); 5132091e35e3SMatthias Ringwald 5133091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling off"); 5134091e35e3SMatthias Ringwald 5135091e35e3SMatthias Ringwald // switch mode 5136091e35e3SMatthias Ringwald hci_power_control_off(); 5137091e35e3SMatthias Ringwald 5138091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, emitting state"); 5139091e35e3SMatthias Ringwald hci_emit_state(); 5140091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, done"); 5141091e35e3SMatthias Ringwald break; 5142091e35e3SMatthias Ringwald 51430c88d5efSMatthias Ringwald case HCI_HALTING_W4_CLOSE_TIMER: 5144091e35e3SMatthias Ringwald // keep waiting 5145091e35e3SMatthias Ringwald 5146091e35e3SMatthias Ringwald break; 5147091e35e3SMatthias Ringwald default: 5148091e35e3SMatthias Ringwald break; 5149091e35e3SMatthias Ringwald } 5150091e35e3SMatthias Ringwald }; 5151091e35e3SMatthias Ringwald 5152091e35e3SMatthias Ringwald static void hci_falling_asleep_run(void){ 5153091e35e3SMatthias Ringwald hci_connection_t * connection; 5154091e35e3SMatthias Ringwald switch(hci_stack->substate) { 5155091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 5156091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP"); 5157091e35e3SMatthias Ringwald // close all open connections 5158091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 5159091e35e3SMatthias Ringwald if (connection){ 5160091e35e3SMatthias Ringwald 5161091e35e3SMatthias Ringwald // send disconnect 5162091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5163091e35e3SMatthias Ringwald 5164091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 5165091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5166091e35e3SMatthias Ringwald 5167091e35e3SMatthias Ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 5168091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 5169091e35e3SMatthias Ringwald return; 5170091e35e3SMatthias Ringwald } 5171091e35e3SMatthias Ringwald 5172091e35e3SMatthias Ringwald if (hci_classic_supported()){ 5173091e35e3SMatthias Ringwald // disable page and inquiry scan 5174091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5175091e35e3SMatthias Ringwald 5176091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, disabling inq scans"); 5177091e35e3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 5178091e35e3SMatthias Ringwald 5179091e35e3SMatthias Ringwald // continue in next sub state 5180091e35e3SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 5181091e35e3SMatthias Ringwald break; 5182091e35e3SMatthias Ringwald } 5183091e35e3SMatthias Ringwald 5184091e35e3SMatthias Ringwald /* fall through */ 5185091e35e3SMatthias Ringwald 5186091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 5187091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling sleep"); 5188091e35e3SMatthias Ringwald // switch mode 5189091e35e3SMatthias Ringwald hci_power_control_sleep(); // changes hci_stack->state to SLEEP 5190091e35e3SMatthias Ringwald hci_emit_state(); 5191091e35e3SMatthias Ringwald break; 5192091e35e3SMatthias Ringwald 5193091e35e3SMatthias Ringwald default: 5194091e35e3SMatthias Ringwald break; 5195091e35e3SMatthias Ringwald } 5196091e35e3SMatthias Ringwald } 5197091e35e3SMatthias Ringwald 519835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 519935454696SMatthias Ringwald 5200758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 5201758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 5202a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 5203baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_SCAN_ENABLE; 5204758b46ceSmatthias.ringwald hci_run(); 5205758b46ceSmatthias.ringwald } 5206758b46ceSmatthias.ringwald 520715a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 5208381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 5209381fbed8Smatthias.ringwald 52103a9fb326S[email protected] if (hci_stack->discoverable == enable){ 521171fd255dSMatthias Ringwald hci_emit_scan_mode_changed(hci_stack->discoverable, hci_stack->connectable); 5212381fbed8Smatthias.ringwald return; 5213381fbed8Smatthias.ringwald } 5214381fbed8Smatthias.ringwald 52153a9fb326S[email protected] hci_stack->discoverable = enable; 5216758b46ceSmatthias.ringwald hci_update_scan_enable(); 5217758b46ceSmatthias.ringwald } 5218b031bebbSmatthias.ringwald 521915a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 5220758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 5221758b46ceSmatthias.ringwald 5222758b46ceSmatthias.ringwald // don't emit event 52233a9fb326S[email protected] if (hci_stack->connectable == enable) return; 5224758b46ceSmatthias.ringwald 52253a9fb326S[email protected] hci_stack->connectable = enable; 5226758b46ceSmatthias.ringwald hci_update_scan_enable(); 5227381fbed8Smatthias.ringwald } 522835454696SMatthias Ringwald #endif 5229381fbed8Smatthias.ringwald 523015a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 52316535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 52325061f3afS[email protected] } 52335061f3afS[email protected] 52342b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 52352b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 52362b838201SMatthias Ringwald 52372b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 52382b838201SMatthias Ringwald hci_reserve_packet_buffer(); 52392b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 52402b838201SMatthias Ringwald 52412b838201SMatthias Ringwald uint16_t size = 0; 52422b838201SMatthias Ringwald uint16_t num_handles = 0; 52432b838201SMatthias Ringwald packet[size++] = 0x35; 52442b838201SMatthias Ringwald packet[size++] = 0x0c; 52452b838201SMatthias Ringwald size++; // skip param len 52462b838201SMatthias Ringwald size++; // skip num handles 52472b838201SMatthias Ringwald 52482b838201SMatthias Ringwald // add { handle, packets } entries 52492b838201SMatthias Ringwald btstack_linked_item_t * it; 52502b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 52512b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 52522b838201SMatthias Ringwald if (connection->num_packets_completed){ 52532b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 52542b838201SMatthias Ringwald size += 2; 52552b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 52562b838201SMatthias Ringwald size += 2; 52572b838201SMatthias Ringwald // 52582b838201SMatthias Ringwald num_handles++; 52592b838201SMatthias Ringwald connection->num_packets_completed = 0; 52602b838201SMatthias Ringwald } 52612b838201SMatthias Ringwald } 52622b838201SMatthias Ringwald 52632b838201SMatthias Ringwald packet[2] = size - 3; 52642b838201SMatthias Ringwald packet[3] = num_handles; 52652b838201SMatthias Ringwald 52662b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 52672b838201SMatthias Ringwald 52682b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 52692b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 52702b838201SMatthias Ringwald 52712b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 52722b838201SMatthias Ringwald if (hci_transport_synchronous()){ 5273e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5274068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 52752b838201SMatthias Ringwald } 52762b838201SMatthias Ringwald } 52772b838201SMatthias Ringwald #endif 52782b838201SMatthias Ringwald 527926fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 528026fe9592SMatthias Ringwald UNUSED(ds); 528126fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 5282beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 5283beceeddeSMatthias Ringwald hci_emit_state(); 528426fe9592SMatthias Ringwald hci_run(); 528526fe9592SMatthias Ringwald } 528626fe9592SMatthias Ringwald 5287f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 52884ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 5289b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 5290b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 5291b5d8b22bS[email protected] if (connection) { 529228a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 5293b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 5294f30077b7SMatthias Ringwald return true; 5295b5d8b22bS[email protected] } 529628a0332dSMatthias Ringwald } else { 5297b5d8b22bS[email protected] // connection gone -> discard further fragments 529828a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 5299b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 5300b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 5301b5d8b22bS[email protected] } 5302b5d8b22bS[email protected] } 5303f30077b7SMatthias Ringwald return false; 53042b838201SMatthias Ringwald } 5305b031bebbSmatthias.ringwald 530668ab6207SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 530768ab6207SMatthias Ringwald static bool hci_run_iso_fragments(void){ 530868ab6207SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size > 0u) { 530968ab6207SMatthias Ringwald // TODO: flow control 531068ab6207SMatthias Ringwald if (hci_transport_can_send_prepared_packet_now(HCI_ISO_DATA_PACKET)){ 531168ab6207SMatthias Ringwald hci_send_iso_packet_fragments(); 531268ab6207SMatthias Ringwald return true; 531368ab6207SMatthias Ringwald } 531468ab6207SMatthias Ringwald } 531568ab6207SMatthias Ringwald return false; 531668ab6207SMatthias Ringwald } 531768ab6207SMatthias Ringwald #endif 531868ab6207SMatthias Ringwald 531935454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 53206c0d5426SMatthias Ringwald 53216c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 53226c0d5426SMatthias Ringwald static bool hci_classic_operation_active(void) { 53236c0d5426SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_STATE_W4_ACTIVE){ 53246c0d5426SMatthias Ringwald return true; 53256c0d5426SMatthias Ringwald } 53266c0d5426SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 53276c0d5426SMatthias Ringwald return true; 53286c0d5426SMatthias Ringwald } 53296c0d5426SMatthias Ringwald btstack_linked_item_t * it; 53306c0d5426SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next) { 53316c0d5426SMatthias Ringwald hci_connection_t *connection = (hci_connection_t *) it; 53326c0d5426SMatthias Ringwald switch (connection->state) { 53336c0d5426SMatthias Ringwald case SENT_CREATE_CONNECTION: 53346c0d5426SMatthias Ringwald case SENT_CANCEL_CONNECTION: 53356c0d5426SMatthias Ringwald case SENT_DISCONNECT: 53366c0d5426SMatthias Ringwald return true; 53376c0d5426SMatthias Ringwald default: 53386c0d5426SMatthias Ringwald break; 53396c0d5426SMatthias Ringwald } 53406c0d5426SMatthias Ringwald } 53416c0d5426SMatthias Ringwald return false; 53426c0d5426SMatthias Ringwald } 53436c0d5426SMatthias Ringwald #endif 53446c0d5426SMatthias Ringwald 5345f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 5346f30077b7SMatthias Ringwald 534759d59ecfSMatthias Ringwald // assert stack is working and classic is active 534859d59ecfSMatthias Ringwald if (hci_classic_supported() == false) return false; 534959d59ecfSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 535059d59ecfSMatthias Ringwald 5351b031bebbSmatthias.ringwald // decline incoming connections 53523a9fb326S[email protected] if (hci_stack->decline_reason){ 53533a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 53543a9fb326S[email protected] hci_stack->decline_reason = 0; 53553a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 5356f30077b7SMatthias Ringwald return true; 5357ce4c8fabSmatthias.ringwald } 535859d59ecfSMatthias Ringwald 5359baa4881eSMatthias Ringwald if (hci_stack->gap_tasks_classic != 0){ 5360ab4831a3SMatthias Ringwald hci_run_gap_tasks_classic(); 5361f30077b7SMatthias Ringwald return true; 5362b031bebbSmatthias.ringwald } 536327741fe7SMatthias Ringwald 5364f5875de5SMatthias Ringwald // start/stop inquiry 5365a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 53666c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 53676c0d5426SMatthias Ringwald if (hci_classic_operation_active() == false) 53686c0d5426SMatthias Ringwald #endif 53696c0d5426SMatthias Ringwald { 5370f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 5371beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 537230199e24SMatthias Ringwald if (hci_stack->inquiry_max_period_length != 0){ 537330199e24SMatthias 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); 537430199e24SMatthias Ringwald } else { 5375496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 537630199e24SMatthias Ringwald } 5377f30077b7SMatthias Ringwald return true; 5378f5875de5SMatthias Ringwald } 53796c0d5426SMatthias Ringwald } 5380f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 5381f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 5382f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 5383f30077b7SMatthias Ringwald return true; 5384f5875de5SMatthias Ringwald } 538530199e24SMatthias Ringwald 538630199e24SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_EXIT_PERIODIC){ 5387bc06f564SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 538830199e24SMatthias Ringwald hci_send_cmd(&hci_exit_periodic_inquiry_mode); 538930199e24SMatthias Ringwald return true; 539030199e24SMatthias Ringwald } 539130199e24SMatthias Ringwald 5392b7f1ee76SMatthias Ringwald // remote name request 5393b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 53946c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 53956c0d5426SMatthias Ringwald if (hci_classic_operation_active() == false) 53966c0d5426SMatthias Ringwald #endif 53976c0d5426SMatthias Ringwald { 5398b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 5399b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 5400ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 5401f30077b7SMatthias Ringwald return true; 5402b7f1ee76SMatthias Ringwald } 54036c0d5426SMatthias Ringwald } 5404cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 5405cf01e888SMatthias Ringwald // Local OOB data 540659d59ecfSMatthias Ringwald if (hci_stack->classic_read_local_oob_data){ 5407cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 54081e83575aSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND)){ 5409cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 5410cf01e888SMatthias Ringwald } else { 5411cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 5412cf01e888SMatthias Ringwald } 5413cf01e888SMatthias Ringwald } 5414cf01e888SMatthias Ringwald #endif 54150a51f88bSMatthias Ringwald // pairing 54160a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 54170a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 54183f659ee4SMilanka Ringwald uint8_t pin_code[16]; 54190a51f88bSMatthias Ringwald switch (state){ 54200a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 5421cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 54223f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 54233f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 54243f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 54250a51f88bSMatthias Ringwald break; 54260a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 5427cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 54280a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 54290a51f88bSMatthias Ringwald break; 54300a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 5431cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 5432d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 54330a51f88bSMatthias Ringwald break; 54340a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 5435cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 54360a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 54370a51f88bSMatthias Ringwald break; 54380a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 5439cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 54400a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 54410a51f88bSMatthias Ringwald break; 54420a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 5443cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 54440a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 54450a51f88bSMatthias Ringwald break; 54460a51f88bSMatthias Ringwald default: 54470a51f88bSMatthias Ringwald break; 54480a51f88bSMatthias Ringwald } 5449f30077b7SMatthias Ringwald return true; 5450f30077b7SMatthias Ringwald } 5451f30077b7SMatthias Ringwald return false; 54520a51f88bSMatthias Ringwald } 545335454696SMatthias Ringwald #endif 5454b031bebbSmatthias.ringwald 5455a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5456be7ef9d8SMatthias Ringwald 5457c42da3bcSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5458c42da3bcSMatthias Ringwald static void hci_le_scan_stop(void){ 5459c42da3bcSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5460c42da3bcSMatthias Ringwald if (hci_extended_advertising_supported()) { 5461c42da3bcSMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 0, 0, 0, 0); 5462c42da3bcSMatthias Ringwald } else 5463c42da3bcSMatthias Ringwald #endif 5464c42da3bcSMatthias Ringwald { 5465c42da3bcSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 5466c42da3bcSMatthias Ringwald } 5467c42da3bcSMatthias Ringwald } 5468c42da3bcSMatthias Ringwald #endif 5469c42da3bcSMatthias Ringwald 5470be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5471be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5472be7ef9d8SMatthias Ringwald uint8_t hci_le_extended_advertising_operation_for_chunk(uint16_t pos, uint16_t len){ 5473be7ef9d8SMatthias Ringwald uint8_t operation = 0; 5474be7ef9d8SMatthias Ringwald if (pos == 0){ 5475be7ef9d8SMatthias Ringwald // first fragment or complete data 5476be7ef9d8SMatthias Ringwald operation |= 1; 5477be7ef9d8SMatthias Ringwald } 5478be7ef9d8SMatthias Ringwald if (pos + LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN >= len){ 5479be7ef9d8SMatthias Ringwald // last fragment or complete data 5480be7ef9d8SMatthias Ringwald operation |= 2; 5481be7ef9d8SMatthias Ringwald } 5482be7ef9d8SMatthias Ringwald return operation; 5483be7ef9d8SMatthias Ringwald } 5484be7ef9d8SMatthias Ringwald #endif 5485be7ef9d8SMatthias Ringwald #endif 5486be7ef9d8SMatthias Ringwald 5487f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 5488f30077b7SMatthias Ringwald 5489f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t lit; 5490f40c73b4SMatthias Ringwald 5491a41310b7SMatthias Ringwald // Phase 1: collect what to stop 5492a41310b7SMatthias Ringwald 549349a6c69cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5494a41310b7SMatthias Ringwald bool scanning_stop = false; 5495a41310b7SMatthias Ringwald bool connecting_stop = false; 5496be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5497c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 54984bafedd5SMatthias Ringwald bool periodic_sync_stop = false; 54994bafedd5SMatthias Ringwald #endif 5500be7ef9d8SMatthias Ringwald #endif 5501c814a904SMatthias Ringwald #endif 5502be7ef9d8SMatthias Ringwald 550349a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 550449a6c69cSMatthias Ringwald bool advertising_stop = false; 550549a6c69cSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 550649a6c69cSMatthias Ringwald le_advertising_set_t * advertising_stop_set = NULL; 550749a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 550849a6c69cSMatthias Ringwald bool periodic_advertising_stop = false; 5509a41310b7SMatthias Ringwald #endif 551049a6c69cSMatthias Ringwald #endif 5511a41310b7SMatthias Ringwald #endif 5512a41310b7SMatthias Ringwald 55139136bde8SMatthias Ringwald // check if own address changes 55149136bde8SMatthias Ringwald bool random_address_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 55159136bde8SMatthias Ringwald 551629c24bebSMatthias Ringwald // check if whitelist needs modification 551729c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 551829c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 551929c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 552029c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 552129c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 552229c24bebSMatthias Ringwald whitelist_modification_pending = true; 552329c24bebSMatthias Ringwald break; 552429c24bebSMatthias Ringwald } 552529c24bebSMatthias Ringwald } 5526f40c73b4SMatthias Ringwald 552721debf25SMatthias Ringwald // check if resolving list needs modification 552821debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 552921debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 55301ffa425cSMatthias Ringwald bool resolving_list_supported = hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE); 5531ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 553221debf25SMatthias Ringwald resolving_list_modification_pending = true; 553321debf25SMatthias Ringwald } 553421debf25SMatthias Ringwald #endif 553529c24bebSMatthias Ringwald 5536f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 553749a6c69cSMatthias Ringwald 5538f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 553949a6c69cSMatthias Ringwald // check if periodic advertiser list needs modification 5540f40c73b4SMatthias Ringwald bool periodic_list_modification_pending = false; 5541f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 5542f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 5543f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 5544f40c73b4SMatthias Ringwald if (entry->state & (LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER | LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER)){ 5545f40c73b4SMatthias Ringwald periodic_list_modification_pending = true; 5546f40c73b4SMatthias Ringwald break; 5547f40c73b4SMatthias Ringwald } 5548f40c73b4SMatthias Ringwald } 5549f40c73b4SMatthias Ringwald #endif 5550f40c73b4SMatthias Ringwald 5551fde725feSMatthias Ringwald // scanning control 55523251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 555329c24bebSMatthias Ringwald // stop if: 555429c24bebSMatthias Ringwald // - parameter change required 555529c24bebSMatthias Ringwald // - it's disabled 555629c24bebSMatthias Ringwald // - whitelist change required but used for scanning 555721debf25SMatthias Ringwald // - resolving list modified 555851e51a58SMatthias Ringwald // - own address changes 555929c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 556021debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 556121debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 5562d54b2c94SMatthias Ringwald (scanning_uses_whitelist && whitelist_modification_pending) || 556351e51a58SMatthias Ringwald resolving_list_modification_pending || 556451e51a58SMatthias Ringwald random_address_change){ 556521debf25SMatthias Ringwald 55662d5c2a27SMatthias Ringwald scanning_stop = true; 5567fde725feSMatthias Ringwald } 5568fde725feSMatthias Ringwald } 5569fde725feSMatthias Ringwald 557029c24bebSMatthias Ringwald // connecting control 5571f496d06eSMatthias Ringwald bool connecting_with_whitelist; 5572f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 5573f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 5574f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 5575af64f147SMatthias Ringwald // stop connecting if: 5576af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 5577af64f147SMatthias Ringwald // - if it got disabled 557821debf25SMatthias Ringwald // - resolving list modified 557951e51a58SMatthias Ringwald // - own address changes 5580f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 5581f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 558221debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 558351e51a58SMatthias Ringwald resolving_list_modification_pending || 558451e51a58SMatthias Ringwald random_address_change) { 558521debf25SMatthias Ringwald 558629c24bebSMatthias Ringwald connecting_stop = true; 558729c24bebSMatthias Ringwald } 5588f496d06eSMatthias Ringwald break; 5589f496d06eSMatthias Ringwald default: 5590f496d06eSMatthias Ringwald break; 559129c24bebSMatthias Ringwald } 559249a6c69cSMatthias Ringwald 55934bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 559449a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 55954bafedd5SMatthias Ringwald // periodic sync control 55964bafedd5SMatthias Ringwald bool sync_with_advertiser_list; 55974bafedd5SMatthias Ringwald switch(hci_stack->le_periodic_sync_state){ 55984bafedd5SMatthias Ringwald case LE_CONNECTING_DIRECT: 55994bafedd5SMatthias Ringwald case LE_CONNECTING_WHITELIST: 560049a6c69cSMatthias Ringwald // stop sync if: 56014bafedd5SMatthias Ringwald // - sync with advertiser list and advertiser list modification pending 560249a6c69cSMatthias Ringwald // - if it got disabled 56034bafedd5SMatthias Ringwald sync_with_advertiser_list = hci_stack->le_periodic_sync_state == LE_CONNECTING_WHITELIST; 56044bafedd5SMatthias Ringwald if ((sync_with_advertiser_list && periodic_list_modification_pending) || 56054bafedd5SMatthias Ringwald (hci_stack->le_periodic_sync_request == LE_CONNECTING_IDLE)){ 56064bafedd5SMatthias Ringwald periodic_sync_stop = true; 56074bafedd5SMatthias Ringwald } 56084bafedd5SMatthias Ringwald break; 56094bafedd5SMatthias Ringwald default: 56104bafedd5SMatthias Ringwald break; 56114bafedd5SMatthias Ringwald } 56124bafedd5SMatthias Ringwald #endif 5613d70217a2SMatthias Ringwald #endif 561429c24bebSMatthias Ringwald 561549a6c69cSMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 561649a6c69cSMatthias Ringwald 5617d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 561845c102fdSMatthias Ringwald // le advertisement control 5619935aa76eSMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0){ 562029c24bebSMatthias Ringwald // stop if: 562129c24bebSMatthias Ringwald // - parameter change required 56229136bde8SMatthias Ringwald // - random address used in advertising and changes 562329c24bebSMatthias Ringwald // - it's disabled 5624ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 562521debf25SMatthias Ringwald // - resolving list modified 562651e51a58SMatthias Ringwald // - own address changes 5627a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 56289136bde8SMatthias Ringwald bool advertising_uses_random_address = hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC; 56294e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 5630a61834b6SMatthias Ringwald if (advertising_change || 56319136bde8SMatthias Ringwald (advertising_uses_random_address && random_address_change) || 5632a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 56339136bde8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 563451e51a58SMatthias Ringwald resolving_list_modification_pending || 563551e51a58SMatthias Ringwald random_address_change) { 563621debf25SMatthias Ringwald 5637fde725feSMatthias Ringwald advertising_stop = true; 5638fde725feSMatthias Ringwald } 5639fde725feSMatthias Ringwald } 5640be7ef9d8SMatthias Ringwald 5641be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5642be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported() && (advertising_stop == false)){ 5643be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 5644be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5645be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5646be7ef9d8SMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 564753f240c0SMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0) { 5648be7ef9d8SMatthias Ringwald // stop if: 5649be7ef9d8SMatthias Ringwald // - parameter change required 5650be7ef9d8SMatthias Ringwald // - random address used in connectable advertising and changes 5651be7ef9d8SMatthias Ringwald // - it's disabled 5652be7ef9d8SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 5653be7ef9d8SMatthias Ringwald // - resolving list modified 5654be7ef9d8SMatthias Ringwald // - own address changes 5655be7ef9d8SMatthias Ringwald // - advertisement set will be removed 565657a04237SMatthias Ringwald bool advertising_uses_whitelist = advertising_set->extended_params.advertising_filter_policy != 0; 565757a04237SMatthias Ringwald bool advertising_connectable = (advertising_set->extended_params.advertising_event_properties & 1) != 0; 565853f240c0SMatthias Ringwald bool advertising_uses_random_address = 565953f240c0SMatthias Ringwald (advertising_set->extended_params.own_address_type != BD_ADDR_TYPE_LE_PUBLIC) && 566053f240c0SMatthias Ringwald advertising_connectable; 5661be7ef9d8SMatthias Ringwald bool advertising_parameter_change = (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 5662be7ef9d8SMatthias Ringwald bool advertising_enabled = (advertising_set->state & LE_ADVERTISEMENT_STATE_ENABLED) != 0; 566353f240c0SMatthias Ringwald bool advertising_set_random_address_change = 566453f240c0SMatthias Ringwald (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 566553f240c0SMatthias Ringwald bool advertising_set_will_be_removed = 566653f240c0SMatthias Ringwald (advertising_set->state & LE_ADVERTISEMENT_TASKS_REMOVE_SET) != 0; 5667be7ef9d8SMatthias Ringwald if (advertising_parameter_change || 5668be7ef9d8SMatthias Ringwald (advertising_uses_random_address && advertising_set_random_address_change) || 5669be7ef9d8SMatthias Ringwald (advertising_enabled == false) || 5670be7ef9d8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 5671be7ef9d8SMatthias Ringwald resolving_list_modification_pending || 5672be7ef9d8SMatthias Ringwald advertising_set_will_be_removed) { 5673be7ef9d8SMatthias Ringwald 5674be7ef9d8SMatthias Ringwald advertising_stop = true; 5675be7ef9d8SMatthias Ringwald advertising_stop_set = advertising_set; 5676be7ef9d8SMatthias Ringwald break; 5677be7ef9d8SMatthias Ringwald } 5678be7ef9d8SMatthias Ringwald } 5679c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 568053f240c0SMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) != 0) { 568153f240c0SMatthias Ringwald // stop if: 568253f240c0SMatthias Ringwald // - it's disabled 568353f240c0SMatthias Ringwald // - parameter change required 568453f240c0SMatthias Ringwald bool periodic_enabled = (advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED) != 0; 568553f240c0SMatthias Ringwald bool periodic_parameter_change = (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS) != 0; 568653f240c0SMatthias Ringwald if ((periodic_enabled == false) || periodic_parameter_change){ 568749a6c69cSMatthias Ringwald periodic_advertising_stop = true; 568853f240c0SMatthias Ringwald advertising_stop_set = advertising_set; 568953f240c0SMatthias Ringwald } 569053f240c0SMatthias Ringwald } 569149a6c69cSMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 569253f240c0SMatthias Ringwald } 5693be7ef9d8SMatthias Ringwald } 5694be7ef9d8SMatthias Ringwald #endif 5695be7ef9d8SMatthias Ringwald 5696a41310b7SMatthias Ringwald #endif 5697fde725feSMatthias Ringwald 5698a41310b7SMatthias Ringwald 5699a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 5700a41310b7SMatthias Ringwald 5701131d4778SMatthias Ringwald 5702131d4778SMatthias Ringwald // 2.1 Outgoing connection 5703131d4778SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5704131d4778SMatthias Ringwald if (connecting_stop){ 5705131d4778SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 5706131d4778SMatthias Ringwald return true; 5707131d4778SMatthias Ringwald } 5708131d4778SMatthias Ringwald #endif 5709131d4778SMatthias Ringwald 5710131d4778SMatthias Ringwald // 2.2 Scanning 5711a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5712a41310b7SMatthias Ringwald if (scanning_stop){ 57135226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 5714c42da3bcSMatthias Ringwald hci_le_scan_stop(); 5715a41310b7SMatthias Ringwald return true; 5716a41310b7SMatthias Ringwald } 5717a41310b7SMatthias Ringwald 5718131d4778SMatthias Ringwald // 2.3 Periodic Sync 57194bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 572049a6c69cSMatthias Ringwald if (hci_stack->le_periodic_terminate_sync_handle != HCI_CON_HANDLE_INVALID){ 572149a6c69cSMatthias Ringwald uint16_t sync_handle = hci_stack->le_periodic_terminate_sync_handle; 572249a6c69cSMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = HCI_CON_HANDLE_INVALID; 572349a6c69cSMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_terminate_sync, sync_handle); 572449a6c69cSMatthias Ringwald return true; 572549a6c69cSMatthias Ringwald } 572649a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 57274bafedd5SMatthias Ringwald if (periodic_sync_stop){ 57284bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_state = LE_CONNECTING_CANCEL; 57294bafedd5SMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_create_sync_cancel); 57304bafedd5SMatthias Ringwald return true; 57314bafedd5SMatthias Ringwald } 573249a6c69cSMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5733a8016f96SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 5734a8016f96SMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 5735a41310b7SMatthias Ringwald 5736131d4778SMatthias Ringwald // 2.4 Advertising: legacy, extended, periodic 5737a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5738fde725feSMatthias Ringwald if (advertising_stop){ 5739e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5740e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 5741be7ef9d8SMatthias Ringwald uint8_t advertising_stop_handle; 5742be7ef9d8SMatthias Ringwald if (advertising_stop_set != NULL){ 5743be7ef9d8SMatthias Ringwald advertising_stop_handle = advertising_stop_set->advertising_handle; 5744be7ef9d8SMatthias Ringwald advertising_stop_set->state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5745be7ef9d8SMatthias Ringwald } else { 5746be7ef9d8SMatthias Ringwald advertising_stop_handle = 0; 5747be7ef9d8SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5748be7ef9d8SMatthias Ringwald } 5749be7ef9d8SMatthias Ringwald const uint8_t advertising_handles[] = { advertising_stop_handle }; 5750e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 5751e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 5752e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 1, advertising_handles, durations, max_events); 5753e464cd48SMatthias Ringwald } else 5754e464cd48SMatthias Ringwald #endif 5755e464cd48SMatthias Ringwald { 5756be7ef9d8SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 575745c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 5758e464cd48SMatthias Ringwald } 5759f30077b7SMatthias Ringwald return true; 576045c102fdSMatthias Ringwald } 576153f240c0SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5762c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 576349a6c69cSMatthias Ringwald if (periodic_advertising_stop){ 576453f240c0SMatthias Ringwald advertising_stop_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 576553f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, 0, advertising_stop_set->advertising_handle); 576653f240c0SMatthias Ringwald return true; 576753f240c0SMatthias Ringwald } 5768c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5769c814a904SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 5770a8016f96SMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 5771fde725feSMatthias Ringwald 5772131d4778SMatthias Ringwald 5773a41310b7SMatthias Ringwald // Phase 3: modify 5774a41310b7SMatthias Ringwald 57759136bde8SMatthias Ringwald if (random_address_change){ 57769136bde8SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 5777e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5778e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 5779e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, 0, hci_stack->le_random_address); 5780e464cd48SMatthias Ringwald } 5781e464cd48SMatthias Ringwald #endif 5782e464cd48SMatthias Ringwald { 57839136bde8SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, hci_stack->le_random_address); 5784e464cd48SMatthias Ringwald } 57859136bde8SMatthias Ringwald return true; 57869136bde8SMatthias Ringwald } 57879136bde8SMatthias Ringwald 5788a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5789a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 5790a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 579163671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 579263671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 579363671e69SMatthias Ringwald // prepare arrays for all PHYs 579463671e69SMatthias Ringwald uint8_t scan_types[1] = { hci_stack->le_scan_type }; 579563671e69SMatthias Ringwald uint16_t scan_intervals[1] = { hci_stack->le_scan_interval }; 579663671e69SMatthias Ringwald uint16_t scan_windows[1] = { hci_stack->le_scan_window }; 579763671e69SMatthias Ringwald uint8_t scanning_phys = 1; // LE 1M PHY 579863671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_parameters, hci_stack->le_own_addr_type, 579963671e69SMatthias Ringwald hci_stack->le_scan_filter_policy, scanning_phys, scan_types, scan_intervals, scan_windows); 580063671e69SMatthias Ringwald } else 580163671e69SMatthias Ringwald #endif 580263671e69SMatthias Ringwald { 5803a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 5804a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 580563671e69SMatthias Ringwald } 5806a41310b7SMatthias Ringwald return true; 5807a41310b7SMatthias Ringwald } 5808a41310b7SMatthias Ringwald #endif 5809a41310b7SMatthias Ringwald 5810a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 581145c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 581245c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 58136bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 5814e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5815e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5816e464cd48SMatthias Ringwald // map advertisment type to advertising event properties 5817e464cd48SMatthias Ringwald uint16_t adv_event_properties = 0; 5818e464cd48SMatthias Ringwald const uint16_t mapping[] = { 0b00010011, 0b00010101, 0b00011101, 0b00010010, 0b00010000}; 5819e464cd48SMatthias Ringwald if (hci_stack->le_advertisements_type < (sizeof(mapping)/sizeof(uint16_t))){ 5820e464cd48SMatthias Ringwald adv_event_properties = mapping[hci_stack->le_advertisements_type]; 5821e464cd48SMatthias Ringwald } 5822174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5823e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 5824e464cd48SMatthias Ringwald 0, 5825e464cd48SMatthias Ringwald adv_event_properties, 5826e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_min, 5827e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_max, 5828e464cd48SMatthias Ringwald hci_stack->le_advertisements_channel_map, 5829e464cd48SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 5830e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 5831e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address, 5832e464cd48SMatthias Ringwald hci_stack->le_advertisements_filter_policy, 5833e464cd48SMatthias Ringwald 0x7f, // tx power: no preference 5834e464cd48SMatthias Ringwald 0x01, // primary adv phy: LE 1M 5835e464cd48SMatthias Ringwald 0, // secondary adv max skip 5836e464cd48SMatthias Ringwald 0, // secondary adv phy 5837e464cd48SMatthias Ringwald 0, // adv sid 5838e464cd48SMatthias Ringwald 0 // scan request notification 5839e464cd48SMatthias Ringwald ); 5840e464cd48SMatthias Ringwald } 5841e464cd48SMatthias Ringwald #endif 5842e464cd48SMatthias Ringwald { 584345c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 584445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 584545c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 584645c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 58476bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 584845c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 584945c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 585045c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 585145c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 5852e464cd48SMatthias Ringwald } 5853f30077b7SMatthias Ringwald return true; 585445c102fdSMatthias Ringwald } 5855be7ef9d8SMatthias Ringwald 5856501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 5857501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 58587a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 58597a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 58606535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 58616535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 58623c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 5863e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5864e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5865174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5866e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, 0, 0x03, 0x01, hci_stack->le_advertisements_data_len, adv_data_clean); 5867e464cd48SMatthias Ringwald } else 5868e464cd48SMatthias Ringwald #endif 5869e464cd48SMatthias Ringwald { 58707a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 5871e464cd48SMatthias Ringwald } 5872f30077b7SMatthias Ringwald return true; 587345c102fdSMatthias Ringwald } 5874be7ef9d8SMatthias Ringwald 5875501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 5876501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5877f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 5878f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 58796535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 58806535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 58813c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 5882e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5883e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5884174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5885e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, 0, 0x03, 0x01, hci_stack->le_scan_response_data_len, scan_data_clean); 5886e464cd48SMatthias Ringwald } else 5887e464cd48SMatthias Ringwald #endif 5888e464cd48SMatthias Ringwald { 5889214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 5890e464cd48SMatthias Ringwald } 5891f30077b7SMatthias Ringwald return true; 5892501f56b3SMatthias Ringwald } 5893be7ef9d8SMatthias Ringwald 5894be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5895be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported()) { 5896be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 5897be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5898be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5899be7ef9d8SMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 5900be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_REMOVE_SET) != 0) { 5901be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_REMOVE_SET; 5902174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5903be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_remove_advertising_set, advertising_set->advertising_handle); 5904be7ef9d8SMatthias Ringwald return true; 5905be7ef9d8SMatthias Ringwald } 5906f66adbdeSMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0){ 5907f66adbdeSMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 5908f66adbdeSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, advertising_set->advertising_handle, advertising_set->random_address); 5909f66adbdeSMatthias Ringwald return true; 5910f66adbdeSMatthias Ringwald } 5911be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0){ 5912be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 5913174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5914be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 5915be7ef9d8SMatthias Ringwald advertising_set->advertising_handle, 591657a04237SMatthias Ringwald advertising_set->extended_params.advertising_event_properties, 591757a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_interval_min, 591857a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_interval_max, 591957a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_channel_map, 592057a04237SMatthias Ringwald advertising_set->extended_params.own_address_type, 592157a04237SMatthias Ringwald advertising_set->extended_params.peer_address_type, 592257a04237SMatthias Ringwald advertising_set->extended_params.peer_address, 592357a04237SMatthias Ringwald advertising_set->extended_params.advertising_filter_policy, 592457a04237SMatthias Ringwald advertising_set->extended_params.advertising_tx_power, 592557a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_phy, 592657a04237SMatthias Ringwald advertising_set->extended_params.secondary_advertising_max_skip, 592757a04237SMatthias Ringwald advertising_set->extended_params.secondary_advertising_phy, 592857a04237SMatthias Ringwald advertising_set->extended_params.advertising_sid, 592957a04237SMatthias Ringwald advertising_set->extended_params.scan_request_notification_enable 5930be7ef9d8SMatthias Ringwald ); 5931be7ef9d8SMatthias Ringwald return true; 5932be7ef9d8SMatthias Ringwald } 5933be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA) != 0) { 5934be7ef9d8SMatthias Ringwald uint16_t pos = advertising_set->adv_data_pos; 5935be7ef9d8SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->adv_data_len); 5936be7ef9d8SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->adv_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 5937be7ef9d8SMatthias Ringwald if ((operation & 0x02) != 0){ 5938be7ef9d8SMatthias Ringwald // last fragment or complete data 5939be7ef9d8SMatthias Ringwald operation |= 2; 5940be7ef9d8SMatthias Ringwald advertising_set->adv_data_pos = 0; 5941be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 5942be7ef9d8SMatthias Ringwald } else { 5943be7ef9d8SMatthias Ringwald advertising_set->adv_data_pos += data_to_upload; 5944be7ef9d8SMatthias Ringwald } 5945174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 59460cbe4690SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, advertising_set->advertising_handle, operation, 0x01, data_to_upload, &advertising_set->adv_data[pos]); 5947be7ef9d8SMatthias Ringwald return true; 5948be7ef9d8SMatthias Ringwald } 5949be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA) != 0) { 5950be7ef9d8SMatthias Ringwald uint16_t pos = advertising_set->scan_data_pos; 5951be7ef9d8SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->scan_data_len); 5952be7ef9d8SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->scan_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 5953be7ef9d8SMatthias Ringwald if ((operation & 0x02) != 0){ 5954be7ef9d8SMatthias Ringwald advertising_set->scan_data_pos = 0; 5955be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5956be7ef9d8SMatthias Ringwald } else { 5957be7ef9d8SMatthias Ringwald advertising_set->scan_data_pos += data_to_upload; 5958be7ef9d8SMatthias Ringwald } 5959174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5960be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, operation, 0x03, 0x01, data_to_upload, &advertising_set->scan_data[pos]); 5961be7ef9d8SMatthias Ringwald return true; 5962be7ef9d8SMatthias Ringwald } 5963c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 596453f240c0SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS) != 0){ 596553f240c0SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS; 596653f240c0SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 596753f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_parameters, 596853f240c0SMatthias Ringwald advertising_set->advertising_handle, 596953f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_interval_min, 597053f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_interval_max, 597153f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_properties); 597253f240c0SMatthias Ringwald return true; 597353f240c0SMatthias Ringwald } 597453f240c0SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA) != 0) { 59750cbe4690SMatthias Ringwald uint16_t pos = advertising_set->periodic_data_pos; 597653f240c0SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->periodic_data_len); 597753f240c0SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->periodic_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 597853f240c0SMatthias Ringwald if ((operation & 0x02) != 0){ 597953f240c0SMatthias Ringwald // last fragment or complete data 598053f240c0SMatthias Ringwald operation |= 2; 59810cbe4690SMatthias Ringwald advertising_set->periodic_data_pos = 0; 598253f240c0SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA; 598353f240c0SMatthias Ringwald } else { 59840cbe4690SMatthias Ringwald advertising_set->periodic_data_pos += data_to_upload; 598553f240c0SMatthias Ringwald } 598653f240c0SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 59870cbe4690SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_data, advertising_set->advertising_handle, operation, data_to_upload, &advertising_set->periodic_data[pos]); 598853f240c0SMatthias Ringwald return true; 598953f240c0SMatthias Ringwald } 5990c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5991be7ef9d8SMatthias Ringwald } 5992be7ef9d8SMatthias Ringwald } 5993be7ef9d8SMatthias Ringwald #endif 5994be7ef9d8SMatthias Ringwald 5995d70217a2SMatthias Ringwald #endif 59969956955bSMatthias Ringwald 5997057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5998a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 5999a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 60004bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 60014bafedd5SMatthias Ringwald // if periodic sync with advertiser list was active and is not cancelled yet, wait until next time 60024bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_state == LE_CONNECTING_CANCEL) return false; 60034bafedd5SMatthias Ringwald #endif 6004057ab60cSMatthias Ringwald #endif 6005057ab60cSMatthias Ringwald 6006a41310b7SMatthias Ringwald // LE Whitelist Management 6007a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 60089956955bSMatthias Ringwald // add/remove entries 6009665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 6010665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 6011665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 6012453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 6013453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6014453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 6015453459ddSMatthias Ringwald return true; 6016453459ddSMatthias Ringwald } 60179956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 6018453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 6019453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 60209956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 6021f30077b7SMatthias Ringwald return true; 60229956955bSMatthias Ringwald } 6023453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 6024665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 60259956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 60269956955bSMatthias Ringwald } 60279956955bSMatthias Ringwald } 602891915b0bSMatthias Ringwald } 60299956955bSMatthias Ringwald 603021debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 603121debf25SMatthias Ringwald // LE Resolving List Management 6032ea151974SMatthias Ringwald if (resolving_list_supported) { 603321debf25SMatthias Ringwald uint16_t i; 603421debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 603521debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 603621debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 603721debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 603821debf25SMatthias Ringwald return true; 603921debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 604021debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 604121debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 604221debf25SMatthias Ringwald return true; 604321debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 604402b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 6045ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 6046ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 6047ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 6048ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 604921debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 605021debf25SMatthias Ringwald return true; 605102b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 605202b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 605302b02cffSMatthias Ringwald uint8_t offset = i >> 3; 605402b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 605502b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 605602b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 605702b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 605802b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 605902b02cffSMatthias Ringwald sm_key_t peer_irk; 606002b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 606102b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 6062f5228c62SMatthias Ringwald 6063f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 6064f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 6065f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 6066f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 6067f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 6068f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 6069f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 6070f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 6071f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 6072f5228c62SMatthias Ringwald } 6073f5228c62SMatthias Ringwald #endif 6074f5228c62SMatthias Ringwald 6075ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 6076ea151974SMatthias Ringwald peer_identity_addreses); 607702b02cffSMatthias Ringwald return true; 607802b02cffSMatthias Ringwald } 607902b02cffSMatthias Ringwald 608002b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 608102b02cffSMatthias Ringwald 608202b02cffSMatthias Ringwald /* fall through */ 608302b02cffSMatthias Ringwald 608421debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 608521debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 608621debf25SMatthias Ringwald uint8_t offset = i >> 3; 608721debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 608802b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 608902b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 609021debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 609121debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 609221debf25SMatthias Ringwald sm_key_t peer_irk; 609321debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 609421debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 6095e938c062SMatthias Ringwald if (btstack_is_null(peer_irk, 16)) continue; 609621debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 609721debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 609821debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 609921debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 610021debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 610121debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 6102ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 6103ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 610421debf25SMatthias Ringwald return true; 610521debf25SMatthias Ringwald } 610602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 610721debf25SMatthias Ringwald break; 610802b02cffSMatthias Ringwald 610921debf25SMatthias Ringwald default: 611021debf25SMatthias Ringwald break; 611121debf25SMatthias Ringwald } 6112ea151974SMatthias Ringwald } 611321debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 611421debf25SMatthias Ringwald #endif 6115a41310b7SMatthias Ringwald 6116f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6117f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6118f40c73b4SMatthias Ringwald // LE Whitelist Management 6119f40c73b4SMatthias Ringwald if (periodic_list_modification_pending){ 6120f40c73b4SMatthias Ringwald // add/remove entries 6121f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 6122f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 6123f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 6124f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER){ 6125f40c73b4SMatthias Ringwald entry->state &= ~LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 6126f40c73b4SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_periodic_advertiser_list, entry->address_type, entry->address); 6127f40c73b4SMatthias Ringwald return true; 6128f40c73b4SMatthias Ringwald } 6129f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER){ 6130f40c73b4SMatthias Ringwald entry->state &= ~LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 6131f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER; 61329e88556bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_periodic_advertiser_list, entry->address_type, entry->address, entry->sid); 6133f40c73b4SMatthias Ringwald return true; 6134f40c73b4SMatthias Ringwald } 6135f40c73b4SMatthias Ringwald if ((entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER) == 0){ 6136f40c73b4SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t *) entry); 6137f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 6138f40c73b4SMatthias Ringwald } 6139f40c73b4SMatthias Ringwald } 6140f40c73b4SMatthias Ringwald } 6141f40c73b4SMatthias Ringwald #endif 6142f40c73b4SMatthias Ringwald #endif 6143f40c73b4SMatthias Ringwald 614473799937SMatthias Ringwald // post-pone all actions until stack is fully working 614573799937SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 614673799937SMatthias Ringwald 614773799937SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 614873799937SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 614973799937SMatthias Ringwald 6150a41310b7SMatthias Ringwald // Phase 4: restore state 615129c24bebSMatthias Ringwald 615229c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6153af64f147SMatthias Ringwald // re-start scanning 615429c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 615529c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 615663671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 615763671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 615863671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 1, 0, 0, 0); 615963671e69SMatthias Ringwald } else 616063671e69SMatthias Ringwald #endif 616163671e69SMatthias Ringwald { 616229c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 616363671e69SMatthias Ringwald } 616429c24bebSMatthias Ringwald return true; 616529c24bebSMatthias Ringwald } 616629c24bebSMatthias Ringwald #endif 616729c24bebSMatthias Ringwald 616813eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6169af64f147SMatthias Ringwald // re-start connecting 6170af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 61719956955bSMatthias Ringwald bd_addr_t null_addr; 61729956955bSMatthias Ringwald memset(null_addr, 0, 6); 61736bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 61746bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 61759956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 6176cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 6177cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 61789956955bSMatthias Ringwald 1, // use whitelist 61799956955bSMatthias Ringwald 0, // peer address type 61809956955bSMatthias Ringwald null_addr, // peer bd addr 61816bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 618273044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 618373044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 618473044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 618573044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 618673044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 618773044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 61889956955bSMatthias Ringwald ); 6189f30077b7SMatthias Ringwald return true; 61909956955bSMatthias Ringwald } 61914bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 61924bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_state == LE_CONNECTING_IDLE){ 61934bafedd5SMatthias Ringwald switch(hci_stack->le_periodic_sync_request){ 61944bafedd5SMatthias Ringwald case LE_CONNECTING_DIRECT: 61954bafedd5SMatthias Ringwald case LE_CONNECTING_WHITELIST: 61964bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_state = ((hci_stack->le_periodic_sync_options & 1) != 0) ? LE_CONNECTING_WHITELIST : LE_CONNECTING_DIRECT; 61974bafedd5SMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_create_sync, 61984bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_options, 61994bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertising_sid, 62004bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address_type, 62014bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address, 62024bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_skip, 62034bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_timeout, 62044bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_cte_type); 62054bafedd5SMatthias Ringwald return true; 62064bafedd5SMatthias Ringwald default: 62074bafedd5SMatthias Ringwald break; 62084bafedd5SMatthias Ringwald } 62094bafedd5SMatthias Ringwald } 62104bafedd5SMatthias Ringwald #endif 6211d70217a2SMatthias Ringwald #endif 6212a41310b7SMatthias Ringwald 6213a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 6214a41310b7SMatthias Ringwald // re-start advertising 6215935aa76eSMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) == 0)){ 6216a41310b7SMatthias Ringwald // check if advertisements should be enabled given 6217935aa76eSMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_ACTIVE; 6218b892db1cSMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, hci_stack->le_advertisements_own_address); 6219e464cd48SMatthias Ringwald 6220e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6221e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 6222e464cd48SMatthias Ringwald const uint8_t advertising_handles[] = { 0 }; 6223e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 6224e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 6225e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 6226e464cd48SMatthias Ringwald } else 6227e464cd48SMatthias Ringwald #endif 6228e464cd48SMatthias Ringwald { 6229a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 6230e464cd48SMatthias Ringwald } 6231a41310b7SMatthias Ringwald return true; 6232a41310b7SMatthias Ringwald } 6233be7ef9d8SMatthias Ringwald 6234be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6235be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported()) { 6236be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 6237be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 6238be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 6239be7ef9d8SMatthias Ringwald le_advertising_set_t *advertising_set = (le_advertising_set_t *) btstack_linked_list_iterator_next(&it); 6240be7ef9d8SMatthias Ringwald if (((advertising_set->state & LE_ADVERTISEMENT_STATE_ENABLED) != 0) && ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) == 0)){ 62418caa405cSMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_ACTIVE; 6242be7ef9d8SMatthias Ringwald const uint8_t advertising_handles[] = { advertising_set->advertising_handle }; 6243be7ef9d8SMatthias Ringwald const uint16_t durations[] = { advertising_set->enable_timeout }; 6244be7ef9d8SMatthias Ringwald const uint16_t max_events[] = { advertising_set->enable_max_scan_events }; 6245be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 6246be7ef9d8SMatthias Ringwald return true; 6247be7ef9d8SMatthias Ringwald } 6248c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 624953f240c0SMatthias Ringwald if (((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED) != 0) && ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) == 0)){ 625053f240c0SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 625153f240c0SMatthias Ringwald uint8_t enable = 1; 625253f240c0SMatthias Ringwald if (advertising_set->periodic_include_adi){ 625353f240c0SMatthias Ringwald enable |= 2; 625453f240c0SMatthias Ringwald } 625553f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, enable, advertising_set->advertising_handle); 625653f240c0SMatthias Ringwald return true; 625753f240c0SMatthias Ringwald } 6258c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 6259be7ef9d8SMatthias Ringwald } 6260be7ef9d8SMatthias Ringwald } 6261be7ef9d8SMatthias Ringwald #endif 6262a41310b7SMatthias Ringwald #endif 6263a41310b7SMatthias Ringwald 6264f30077b7SMatthias Ringwald return false; 62657bdc6798S[email protected] } 62667aa35f6aSMatthias Ringwald 62677aa35f6aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 62687aa35f6aSMatthias Ringwald static bool hci_run_iso_tasks(void){ 62697aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 62702a6c0f82SMatthias Ringwald 627140f86dfdSMatthias Ringwald if (hci_stack->iso_active_operation_group_id != 0xff) { 627240f86dfdSMatthias Ringwald return false; 627340f86dfdSMatthias Ringwald } 627440f86dfdSMatthias Ringwald 62752a6c0f82SMatthias Ringwald // BIG 62767aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 62777aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 62787aa35f6aSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 62797aa35f6aSMatthias Ringwald switch (big->state){ 62807aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 628140f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = big->params->big_handle; 628240f86dfdSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_BIS; 62837aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_ESTABLISHED; 62847aa35f6aSMatthias Ringwald hci_send_cmd(&hci_le_create_big, 62857aa35f6aSMatthias Ringwald big->params->big_handle, 62867aa35f6aSMatthias Ringwald big->params->advertising_handle, 62877aa35f6aSMatthias Ringwald big->params->num_bis, 62887aa35f6aSMatthias Ringwald big->params->sdu_interval_us, 62897aa35f6aSMatthias Ringwald big->params->max_sdu, 62907aa35f6aSMatthias Ringwald big->params->max_transport_latency_ms, 62917aa35f6aSMatthias Ringwald big->params->rtn, 62927aa35f6aSMatthias Ringwald big->params->phy, 62937aa35f6aSMatthias Ringwald big->params->packing, 62947aa35f6aSMatthias Ringwald big->params->framing, 62957aa35f6aSMatthias Ringwald big->params->encryption, 62967aa35f6aSMatthias Ringwald big->params->broadcast_code); 62977aa35f6aSMatthias Ringwald return true; 62987aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 62997aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH; 63007aa35f6aSMatthias 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); 63017aa35f6aSMatthias Ringwald return true; 63027aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED: 63037aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED; 63042a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle, big->state_vars.status); 6305c1f83111SMatthias Ringwald return true; 63067aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_TERMINATE: 63077aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 63082a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle, ERROR_CODE_SUCCESS); 63097aa35f6aSMatthias Ringwald return true; 63107aa35f6aSMatthias Ringwald default: 63117aa35f6aSMatthias Ringwald break; 63127aa35f6aSMatthias Ringwald } 63137aa35f6aSMatthias Ringwald } 63142a6c0f82SMatthias Ringwald 63152a6c0f82SMatthias Ringwald // BIG Sync 63162a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 63172a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 63182a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 63192a6c0f82SMatthias Ringwald switch (big_sync->state){ 63202a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 632140f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = big_sync->params->big_handle; 632240f86dfdSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_BIS; 63232a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_ESTABLISHED; 63242a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_create_sync, 63252a6c0f82SMatthias Ringwald big_sync->params->big_handle, 63262a6c0f82SMatthias Ringwald big_sync->params->sync_handle, 63272a6c0f82SMatthias Ringwald big_sync->params->encryption, 63282a6c0f82SMatthias Ringwald big_sync->params->broadcast_code, 63292a6c0f82SMatthias Ringwald big_sync->params->mse, 63302a6c0f82SMatthias Ringwald big_sync->params->big_sync_timeout_10ms, 63312a6c0f82SMatthias Ringwald big_sync->params->num_bis, 63322a6c0f82SMatthias Ringwald big_sync->params->bis_indices); 63332a6c0f82SMatthias Ringwald return true; 63342a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 63352a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH; 63362a6c0f82SMatthias 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); 63372a6c0f82SMatthias Ringwald return true; 63382a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED: 63392a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED; 63402a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 6341c1f83111SMatthias Ringwald return true; 63422a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_TERMINATE: 63432a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 63442a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 63452a6c0f82SMatthias Ringwald return true; 63462a6c0f82SMatthias Ringwald default: 63472a6c0f82SMatthias Ringwald break; 63482a6c0f82SMatthias Ringwald } 63492a6c0f82SMatthias Ringwald } 63502a6c0f82SMatthias Ringwald 6351d3a89d91SMatthias Ringwald // CIG 6352d3a89d91SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_cigs); 6353d3a89d91SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 6354d3a89d91SMatthias Ringwald le_audio_cig_t *cig = (le_audio_cig_t *) btstack_linked_list_iterator_next(&it); 6355d3a89d91SMatthias Ringwald uint8_t i; 6356444e371eSMatthias Ringwald // Set CIG Parameters 6357d3a89d91SMatthias Ringwald uint8_t cis_id[MAX_NR_CIS]; 6358d3a89d91SMatthias Ringwald uint16_t max_sdu_c_to_p[MAX_NR_CIS]; 6359d3a89d91SMatthias Ringwald uint16_t max_sdu_p_to_c[MAX_NR_CIS]; 6360d3a89d91SMatthias Ringwald uint8_t phy_c_to_p[MAX_NR_CIS]; 6361d3a89d91SMatthias Ringwald uint8_t phy_p_to_c[MAX_NR_CIS]; 6362d3a89d91SMatthias Ringwald uint8_t rtn_c_to_p[MAX_NR_CIS]; 6363d3a89d91SMatthias Ringwald uint8_t rtn_p_to_c[MAX_NR_CIS]; 6364d3a89d91SMatthias Ringwald switch (cig->state) { 6365d3a89d91SMatthias Ringwald case LE_AUDIO_CIG_STATE_CREATE: 6366d3a89d91SMatthias Ringwald hci_stack->iso_active_operation_group_id = cig->params->cig_id; 6367d3a89d91SMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_CIS; 6368d3a89d91SMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_ESTABLISHED; 6369d3a89d91SMatthias Ringwald le_audio_cig_params_t * params = cig->params; 6370d3a89d91SMatthias Ringwald for (i = 0; i < params->num_cis; i++) { 6371d3a89d91SMatthias Ringwald le_audio_cis_params_t * cis_params = &cig->params->cis_params[i]; 6372d3a89d91SMatthias Ringwald cis_id[i] = cis_params->cis_id; 6373d3a89d91SMatthias Ringwald max_sdu_c_to_p[i] = cis_params->max_sdu_c_to_p; 6374d3a89d91SMatthias Ringwald max_sdu_p_to_c[i] = cis_params->max_sdu_p_to_c; 6375d3a89d91SMatthias Ringwald phy_c_to_p[i] = cis_params->phy_c_to_p; 6376d3a89d91SMatthias Ringwald phy_p_to_c[i] = cis_params->phy_p_to_c; 6377d3a89d91SMatthias Ringwald rtn_c_to_p[i] = cis_params->rtn_c_to_p; 6378d3a89d91SMatthias Ringwald rtn_p_to_c[i] = cis_params->rtn_p_to_c; 6379d3a89d91SMatthias Ringwald } 6380d3a89d91SMatthias Ringwald hci_send_cmd(&hci_le_set_cig_parameters, 6381d3a89d91SMatthias Ringwald cig->cig_id, 6382d3a89d91SMatthias Ringwald params->sdu_interval_c_to_p, 6383d3a89d91SMatthias Ringwald params->sdu_interval_p_to_c, 6384d3a89d91SMatthias Ringwald params->worst_case_sca, 6385d3a89d91SMatthias Ringwald params->packing, 6386d3a89d91SMatthias Ringwald params->framing, 6387d3a89d91SMatthias Ringwald params->max_transport_latency_c_to_p, 6388d3a89d91SMatthias Ringwald params->max_transport_latency_p_to_c, 6389d3a89d91SMatthias Ringwald params->num_cis, 6390d3a89d91SMatthias Ringwald cis_id, 6391d3a89d91SMatthias Ringwald max_sdu_c_to_p, 6392d3a89d91SMatthias Ringwald max_sdu_p_to_c, 6393d3a89d91SMatthias Ringwald phy_c_to_p, 6394d3a89d91SMatthias Ringwald phy_p_to_c, 6395d3a89d91SMatthias Ringwald rtn_c_to_p, 6396d3a89d91SMatthias Ringwald rtn_p_to_c 6397d3a89d91SMatthias Ringwald ); 6398bb42b5c0SMatthias Ringwald return true; 6399444e371eSMatthias Ringwald case LE_AUDIO_CIG_STATE_CREATE_CIS: 6400bb42b5c0SMatthias Ringwald hci_stack->iso_active_operation_group_id = cig->params->cig_id; 6401bb42b5c0SMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_CIS; 6402444e371eSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_CREATE_CIS; 64033abcd16cSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 64043abcd16cSMatthias Ringwald cig->cis_setup_active[i] = true; 64053abcd16cSMatthias Ringwald } 6406444e371eSMatthias Ringwald hci_send_cmd(&hci_le_create_cis, cig->num_cis, cig->cis_con_handles, cig->acl_con_handles); 6407bb42b5c0SMatthias Ringwald return true; 6408*f9306a1aSMatthias Ringwald case LE_AUDIO_CIG_STATE_SETUP_ISO_PATH: 6409*f9306a1aSMatthias Ringwald while (cig->state_vars.next_cis < (cig->num_cis * 2)){ 6410*f9306a1aSMatthias Ringwald // find next path to setup 6411*f9306a1aSMatthias Ringwald uint8_t cis_index = cig->state_vars.next_cis >> 1; 6412*f9306a1aSMatthias Ringwald if (cig->cis_established[cis_index] == false) { 6413*f9306a1aSMatthias Ringwald continue; 6414*f9306a1aSMatthias Ringwald } 6415*f9306a1aSMatthias Ringwald uint8_t cis_direction = cig->state_vars.next_cis & 1; 6416*f9306a1aSMatthias Ringwald bool setup = true; 6417*f9306a1aSMatthias Ringwald if (cis_direction){ 6418*f9306a1aSMatthias Ringwald // 0 - input - host to controller 6419*f9306a1aSMatthias Ringwald // we are central => central to peripheral 6420*f9306a1aSMatthias Ringwald setup &= cig->params->cis_params[cis_index].max_sdu_c_to_p > 0; 6421*f9306a1aSMatthias Ringwald } else { 6422*f9306a1aSMatthias Ringwald // 1 - output - controller to host 6423*f9306a1aSMatthias Ringwald // we are central => peripheral to central 6424*f9306a1aSMatthias Ringwald setup &= cig->params->cis_params[cis_index].max_sdu_p_to_c > 0; 6425*f9306a1aSMatthias Ringwald } 6426*f9306a1aSMatthias Ringwald if (setup){ 6427*f9306a1aSMatthias Ringwald hci_stack->iso_active_operation_group_id = cig->params->cig_id; 6428*f9306a1aSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_CIS; 6429*f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_SETUP_ISO_PATH; 6430*f9306a1aSMatthias Ringwald return true; 6431*f9306a1aSMatthias Ringwald } 6432*f9306a1aSMatthias Ringwald } 6433*f9306a1aSMatthias Ringwald // emit done 6434*f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_ACTIVE; 6435d3a89d91SMatthias Ringwald default: 6436d3a89d91SMatthias Ringwald break; 6437d3a89d91SMatthias Ringwald } 6438d3a89d91SMatthias Ringwald } 6439d3a89d91SMatthias Ringwald 64407aa35f6aSMatthias Ringwald return false; 64417aa35f6aSMatthias Ringwald } 64427aa35f6aSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 6443b2f949feS[email protected] #endif 64447bdc6798S[email protected] 644588a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 6446f30077b7SMatthias Ringwald btstack_linked_item_t * it; 6447a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 644805ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 644932ab9390Smatthias.ringwald 64500bf6344aS[email protected] switch(connection->state){ 64510bf6344aS[email protected] case SEND_CREATE_CONNECTION: 64524f3229d8S[email protected] switch(connection->address_type){ 645335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6454f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 64559da54300S[email protected] log_info("sending hci_create_connection"); 6456b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 64574f3229d8S[email protected] break; 645835454696SMatthias Ringwald #endif 64594f3229d8S[email protected] default: 6460a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 6461d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 64629da54300S[email protected] log_info("sending hci_le_create_connection"); 64636bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 64646bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 6465e90f848dSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6466e90f848dSMatthias Ringwald if (hci_extended_advertising_supported()) { 6467e90f848dSMatthias Ringwald uint16_t le_connection_scan_interval[1] = { hci_stack->le_connection_scan_interval }; 6468e90f848dSMatthias Ringwald uint16_t le_connection_scan_window[1] = { hci_stack->le_connection_scan_window }; 6469e90f848dSMatthias Ringwald uint16_t le_connection_interval_min[1] = { hci_stack->le_connection_interval_min }; 6470e90f848dSMatthias Ringwald uint16_t le_connection_interval_max[1] = { hci_stack->le_connection_interval_max }; 6471e90f848dSMatthias Ringwald uint16_t le_connection_latency[1] = { hci_stack->le_connection_latency }; 6472e90f848dSMatthias Ringwald uint16_t le_supervision_timeout[1] = { hci_stack->le_supervision_timeout }; 6473e90f848dSMatthias Ringwald uint16_t le_minimum_ce_length[1] = { hci_stack->le_minimum_ce_length }; 6474e90f848dSMatthias Ringwald uint16_t le_maximum_ce_length[1] = { hci_stack->le_maximum_ce_length }; 6475e90f848dSMatthias Ringwald hci_send_cmd(&hci_le_extended_create_connection, 6476e90f848dSMatthias Ringwald 0, // don't use whitelist 6477e90f848dSMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 6478e90f848dSMatthias Ringwald connection->address_type, // peer address type 6479e90f848dSMatthias Ringwald connection->address, // peer bd addr 6480e90f848dSMatthias Ringwald 1, // initiating PHY - 1M 6481e90f848dSMatthias Ringwald le_connection_scan_interval, // conn scan interval 6482e90f848dSMatthias Ringwald le_connection_scan_window, // conn scan windows 6483e90f848dSMatthias Ringwald le_connection_interval_min, // conn interval min 6484e90f848dSMatthias Ringwald le_connection_interval_max, // conn interval max 6485e90f848dSMatthias Ringwald le_connection_latency, // conn latency 6486e90f848dSMatthias Ringwald le_supervision_timeout, // conn latency 6487e90f848dSMatthias Ringwald le_minimum_ce_length, // min ce length 6488e90f848dSMatthias Ringwald le_maximum_ce_length // max ce length 6489e90f848dSMatthias Ringwald ); } 6490e90f848dSMatthias Ringwald else 6491e90f848dSMatthias Ringwald #endif 6492e90f848dSMatthias Ringwald { 64934f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 6494cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 6495cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 64964f3229d8S[email protected] 0, // don't use whitelist 64974f3229d8S[email protected] connection->address_type, // peer address type 64984f3229d8S[email protected] connection->address, // peer bd addr 64996bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 650073044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 650173044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 650273044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 650373044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 650473044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 650573044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 65064f3229d8S[email protected] ); 6507e90f848dSMatthias Ringwald } 65084f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 6509b2f949feS[email protected] #endif 6510d70217a2SMatthias Ringwald #endif 65114f3229d8S[email protected] break; 65124f3229d8S[email protected] } 6513f30077b7SMatthias Ringwald return true; 6514ad83dc6aS[email protected] 651535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 65160bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 65175cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 6518f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 651976ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 6520895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 6521c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 6522f30077b7SMatthias Ringwald return true; 6523ee025741SMatthias Ringwald } 6524ee025741SMatthias Ringwald break; 652535454696SMatthias Ringwald #endif 65260bf6344aS[email protected] case SEND_DISCONNECT: 65270bf6344aS[email protected] connection->state = SENT_DISCONNECT; 65286ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 6529f30077b7SMatthias Ringwald return true; 65300bf6344aS[email protected] 65310bf6344aS[email protected] default: 65320bf6344aS[email protected] break; 6533c7e0c5f6Smatthias.ringwald } 6534c7e0c5f6Smatthias.ringwald 6535cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 6536cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 6537cabf004eSMatthias Ringwald 653894418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 653994418890SMatthias Ringwald 6540ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 654150f49832SMatthias Ringwald if (((connection->authentication_flags & AUTH_FLAG_HANDLE_LINK_KEY_REQUEST) != 0)){ 6542608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 65438daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 654430e72d78SMatthias Ringwald 6545e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 6546e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 654730e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 6548e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 654932ab9390Smatthias.ringwald } else { 655032ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 655132ab9390Smatthias.ringwald } 6552f30077b7SMatthias Ringwald return true; 655332ab9390Smatthias.ringwald } 65541d6b20aeS[email protected] 65558daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_DENY_PIN_CODE_REQUEST){ 65569da54300S[email protected] log_info("denying to pin request"); 65578daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_DENY_PIN_CODE_REQUEST); 655834d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 6559f30077b7SMatthias Ringwald return true; 65604c57c146S[email protected] } 65614c57c146S[email protected] 6562c950c316SMatthias Ringwald // security assessment requires remote features 65632dd8985bSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST) != 0){ 6564c950c316SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 6565c950c316SMatthias Ringwald hci_ssp_assess_security_on_io_cap_request(connection); 6566c950c316SMatthias 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 6567c950c316SMatthias Ringwald } 6568c950c316SMatthias Ringwald 65698daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY){ 65708daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 6571a8d20135SMatthias Ringwald // set authentication requirements: 6572a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 6573532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 6574a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 65759faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 65769faad3abS[email protected] authreq |= 1; 6577106d6d11S[email protected] } 6578532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 65798daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 6580532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 6581532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 6582532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 6583532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 6584532454f9SMatthias Ringwald bonding = false; 6585532454f9SMatthias Ringwald } 6586532454f9SMatthias Ringwald } 6587532454f9SMatthias Ringwald if (bonding){ 6588a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 6589a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 6590532454f9SMatthias Ringwald } else { 6591a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 6592a8d20135SMatthias Ringwald } 6593532454f9SMatthias Ringwald } 65941849becdSMatthias Ringwald uint8_t have_oob_data = 0; 65951849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 65961849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 65971849becdSMatthias Ringwald have_oob_data |= 1; 65981849becdSMatthias Ringwald } 65991849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 66001849becdSMatthias Ringwald have_oob_data |= 2; 66011849becdSMatthias Ringwald } 66021849becdSMatthias Ringwald #endif 66031849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 660411b03efaSMatthias Ringwald return true; 6605f8fb5f6eS[email protected] } 660611b03efaSMatthias Ringwald 66078daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 66088daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 660911b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 6610f30077b7SMatthias Ringwald return true; 661132ab9390Smatthias.ringwald } 661232ab9390Smatthias.ringwald 66131849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 66147ca4a7edSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY){ 66157ca4a7edSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 66161849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 66171849becdSMatthias Ringwald const uint8_t * r_192 = zero; 66181849becdSMatthias Ringwald const uint8_t * c_192 = zero; 66191849becdSMatthias Ringwald const uint8_t * r_256 = zero; 66201849becdSMatthias Ringwald const uint8_t * c_256 = zero; 66211849becdSMatthias Ringwald // verify P-256 OOB 66221e83575aSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && hci_command_supported(SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)) { 66231849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 66241849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 66251849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 66261849becdSMatthias Ringwald } 66271849becdSMatthias Ringwald } 66281849becdSMatthias Ringwald // verify P-192 OOB 66291849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 66301849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 66311849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 66321849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 66331849becdSMatthias Ringwald } 66341849becdSMatthias Ringwald } 66357ca4a7edSMatthias Ringwald 66367ca4a7edSMatthias Ringwald // assess security 66377ca4a7edSMatthias Ringwald bool need_level_4 = hci_stack->gap_secure_connections_only_mode || (connection->requested_security_level == LEVEL_4); 66387ca4a7edSMatthias Ringwald bool can_reach_level_4 = hci_remote_sc_enabled(connection) && (c_256 != NULL); 66397ca4a7edSMatthias Ringwald if (need_level_4 && !can_reach_level_4){ 66407ca4a7edSMatthias Ringwald log_info("Level 4 required, but not possible -> abort"); 66417ca4a7edSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_INSUFFICIENT_SECURITY); 66427ca4a7edSMatthias Ringwald // send oob negative reply 66437ca4a7edSMatthias Ringwald c_256 = NULL; 66447ca4a7edSMatthias Ringwald c_192 = NULL; 66457ca4a7edSMatthias Ringwald } 66467ca4a7edSMatthias Ringwald 66471849becdSMatthias Ringwald // Reply 66481849becdSMatthias Ringwald if (c_256 != zero) { 66491849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 66501849becdSMatthias Ringwald } else if (c_192 != zero){ 66511849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 66521849becdSMatthias Ringwald } else { 66531ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = connection->con_handle; 66541849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 66551849becdSMatthias Ringwald } 66561849becdSMatthias Ringwald return true; 66571849becdSMatthias Ringwald } 66581849becdSMatthias Ringwald #endif 66591849becdSMatthias Ringwald 66608daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_REPLY){ 66618daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 666234d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 6663f30077b7SMatthias Ringwald return true; 6664dbe1a790S[email protected] } 6665dbe1a790S[email protected] 6666367aedc0SMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY){ 6667367aedc0SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 6668367aedc0SMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, &connection->address); 6669367aedc0SMatthias Ringwald return true; 6670367aedc0SMatthias Ringwald } 6671367aedc0SMatthias Ringwald 66728daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_PASSKEY_REPLY){ 66738daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 667434d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 6675f30077b7SMatthias Ringwald return true; 6676dbe1a790S[email protected] } 6677afd4e962S[email protected] 6678ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 6679ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 66801bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 66816ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 66826ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 6683f30077b7SMatthias Ringwald return true; 6684ad83dc6aS[email protected] } 668576f27cffSMatthias Ringwald 66862a75353aSMatthias Ringwald if ((connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 668734d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 6688abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 668934d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 6690f30077b7SMatthias Ringwald return true; 6691afd4e962S[email protected] } 669276f27cffSMatthias Ringwald 6693dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 6694dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 6695dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 6696f30077b7SMatthias Ringwald return true; 6697dce78009S[email protected] } 6698447016ffSMatthias Ringwald 6699573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 6700573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 6701573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 6702f30077b7SMatthias Ringwald return true; 6703573897a0SMatthias Ringwald } 6704447016ffSMatthias Ringwald 6705447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 6706447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 6707447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 6708447016ffSMatthias Ringwald return true; 6709447016ffSMatthias Ringwald } 6710447016ffSMatthias Ringwald 6711447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 6712447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 6713447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 6714447016ffSMatthias Ringwald return true; 6715447016ffSMatthias Ringwald } 6716447016ffSMatthias Ringwald 6717447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 6718447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 6719447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 6720447016ffSMatthias Ringwald return true; 6721447016ffSMatthias Ringwald } 672276f27cffSMatthias Ringwald #endif 672376f27cffSMatthias Ringwald 672476f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 672576f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 67261714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 67271714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 67281714cbbdSMatthias Ringwald #endif 67296ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 67306ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 67316ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 6732f30077b7SMatthias Ringwald return true; 673376f27cffSMatthias Ringwald } 67346ef3696aSMatthias Ringwald } 6735da886c03S[email protected] 67366cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 6737f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 6738f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 6739f8ee3071SMatthias Ringwald case 0: 6740f8ee3071SMatthias Ringwald break; 6741f8ee3071SMatthias Ringwald case 0xffff: 6742f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 6743f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 6744f30077b7SMatthias Ringwald return true; 6745f8ee3071SMatthias Ringwald default: 6746f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 6747f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 6748f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 6749f30077b7SMatthias Ringwald return true; 6750f8ee3071SMatthias Ringwald } 675188a03c8dSMatthias Ringwald 6752140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 6753140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 6754140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 6755140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 6756140c0557SMatthias Ringwald return true; 6757140c0557SMatthias Ringwald } 6758140c0557SMatthias Ringwald 6759965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 6760278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 6761965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 6762278ff8a9SMatthias 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); 6763278ff8a9SMatthias Ringwald return true; 6764278ff8a9SMatthias Ringwald } 6765278ff8a9SMatthias Ringwald 676688a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 676788a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 676888a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 676988a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 677088a03c8dSMatthias Ringwald return true; 677188a03c8dSMatthias Ringwald } 677222df2fe2SMatthias Ringwald #endif 67739afaa4d4SMatthias Ringwald 67749afaa4d4SMatthias Ringwald if (connection->gap_connection_tasks != 0){ 677522df2fe2SMatthias Ringwald #ifdef ENABLE_CLASSIC 67769afaa4d4SMatthias Ringwald if ((connection->gap_connection_tasks & GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT) != 0){ 67779afaa4d4SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT; 67789afaa4d4SMatthias Ringwald hci_send_cmd(&hci_write_automatic_flush_timeout, connection->con_handle, hci_stack->automatic_flush_timeout); 67799afaa4d4SMatthias Ringwald return true; 67809afaa4d4SMatthias Ringwald } 678122df2fe2SMatthias Ringwald if (connection->gap_connection_tasks & GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT){ 678222df2fe2SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT; 678322df2fe2SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 678422df2fe2SMatthias Ringwald return true; 67859afaa4d4SMatthias Ringwald } 67866cdc2862SMatthias Ringwald #endif 678722df2fe2SMatthias Ringwald if (connection->gap_connection_tasks & GAP_CONNECTION_TASK_READ_RSSI){ 678822df2fe2SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_READ_RSSI; 678922df2fe2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 679022df2fe2SMatthias Ringwald return true; 679122df2fe2SMatthias Ringwald } 679222df2fe2SMatthias Ringwald } 6793f8ee3071SMatthias Ringwald 6794a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 679573cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 679673cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 679773cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 6798da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 679973cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 6800c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 6801c37a3166S[email protected] 0x0000, 0xffff); 6802f30077b7SMatthias Ringwald return true; 680373cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 680473cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 680573cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 680673cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 680773cd8a2aSMatthias Ringwald 0x0000, 0xffff); 6808f30077b7SMatthias Ringwald return true; 680973cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 681073cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 68111e0338ebSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, connection->con_handle, 68121e0338ebSMatthias Ringwald ERROR_CODE_UNACCEPTABLE_CONNECTION_PARAMETERS); 6813f30077b7SMatthias Ringwald return true; 681473cd8a2aSMatthias Ringwald default: 681573cd8a2aSMatthias Ringwald break; 6816c37a3166S[email protected] } 68174ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 6818b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 6819b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 6820b90f6e0aSMatthias 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); 6821f30077b7SMatthias Ringwald return true; 6822b90f6e0aSMatthias Ringwald } 6823c37a3166S[email protected] #endif 6824dbe1a790S[email protected] } 6825f30077b7SMatthias Ringwald return false; 6826f30077b7SMatthias Ringwald } 6827c7e0c5f6Smatthias.ringwald 6828f30077b7SMatthias Ringwald static void hci_run(void){ 6829f30077b7SMatthias Ringwald 6830df9e2780SMatthias Ringwald // stack state sub statemachines 6831df9e2780SMatthias Ringwald switch (hci_stack->state) { 6832df9e2780SMatthias Ringwald case HCI_STATE_INITIALIZING: 6833df9e2780SMatthias Ringwald hci_initializing_run(); 6834d0dea5fbSMatthias Ringwald break; 6835df9e2780SMatthias Ringwald case HCI_STATE_HALTING: 6836df9e2780SMatthias Ringwald hci_halting_run(); 6837d0dea5fbSMatthias Ringwald break; 6838df9e2780SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 6839df9e2780SMatthias Ringwald hci_falling_asleep_run(); 6840d0dea5fbSMatthias Ringwald break; 6841df9e2780SMatthias Ringwald default: 6842df9e2780SMatthias Ringwald break; 6843df9e2780SMatthias Ringwald } 6844df9e2780SMatthias Ringwald 6845d0dea5fbSMatthias Ringwald // allow to run after initialization to working transition 6846d0dea5fbSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING){ 6847d0dea5fbSMatthias Ringwald return; 6848d0dea5fbSMatthias Ringwald } 6849d0dea5fbSMatthias Ringwald 6850f30077b7SMatthias Ringwald bool done; 6851f30077b7SMatthias Ringwald 6852f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 6853f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 6854f30077b7SMatthias Ringwald if (done) return; 6855f30077b7SMatthias Ringwald 685668ab6207SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 685768ab6207SMatthias Ringwald done = hci_run_iso_fragments(); 685868ab6207SMatthias Ringwald if (done) return; 685968ab6207SMatthias Ringwald #endif 686068ab6207SMatthias Ringwald 6861f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 6862f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 6863f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 6864f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 6865f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 6866f30077b7SMatthias Ringwald return; 6867f30077b7SMatthias Ringwald } 6868f30077b7SMatthias Ringwald #endif 6869f30077b7SMatthias Ringwald 6870f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 6871f30077b7SMatthias Ringwald 6872f30077b7SMatthias Ringwald // global/non-connection oriented commands 6873f30077b7SMatthias Ringwald 6874f30077b7SMatthias Ringwald 6875f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 6876f30077b7SMatthias Ringwald // general gap classic 6877f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 6878f30077b7SMatthias Ringwald if (done) return; 6879f30077b7SMatthias Ringwald #endif 6880f30077b7SMatthias Ringwald 6881f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 6882f30077b7SMatthias Ringwald // general gap le 6883f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 6884f30077b7SMatthias Ringwald if (done) return; 68857aa35f6aSMatthias Ringwald 68867aa35f6aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 68877aa35f6aSMatthias Ringwald // ISO related tasks, e.g. BIG create/terminate/sync 68887aa35f6aSMatthias Ringwald done = hci_run_iso_tasks(); 68897aa35f6aSMatthias Ringwald if (done) return; 68907aa35f6aSMatthias Ringwald #endif 6891f30077b7SMatthias Ringwald #endif 6892f30077b7SMatthias Ringwald 6893f30077b7SMatthias Ringwald // send pending HCI commands 6894df9e2780SMatthias Ringwald hci_run_general_pending_commands(); 68953429f56bSmatthias.ringwald } 689616833f0aSmatthias.ringwald 68973e2050f7SMatthias Ringwald uint8_t hci_send_cmd_packet(uint8_t *packet, int size){ 689835454696SMatthias Ringwald // house-keeping 689935454696SMatthias Ringwald 690035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6901c8e4258aSmatthias.ringwald bd_addr_t addr; 6902c8e4258aSmatthias.ringwald hci_connection_t * conn; 6903c123d999SMatthias Ringwald #endif 6904c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 69056f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 69069cbd2215SMatthias Ringwald #endif 69074ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 69084ae8623eSMatthias Ringwald uint8_t i; 69094ae8623eSMatthias Ringwald uint8_t num_cis; 69104ae8623eSMatthias Ringwald hci_con_handle_t cis_handle; 69114ae8623eSMatthias Ringwald uint8_t status; 69124ae8623eSMatthias Ringwald #endif 6913c8e4258aSmatthias.ringwald 69146f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 69156f35bb46SMatthias Ringwald switch (opcode) { 69169cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 69179cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 69189cbd2215SMatthias Ringwald break; 69199cbd2215SMatthias Ringwald 69209cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 69216f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 6922724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 69239da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 6924c8e4258aSmatthias.ringwald 692572cf8859SMatthias Ringwald // CVE-2020-26555: reject outgoing connection to device with same BD ADDR 692672cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0) { 692779e0fa07SMatthias Ringwald hci_emit_connection_complete(addr, 0, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR); 69283e2050f7SMatthias Ringwald return ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 692972cf8859SMatthias Ringwald } 693072cf8859SMatthias Ringwald 6931f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 6932ad83dc6aS[email protected] if (!conn) { 6933f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 693417f1ba2aSmatthias.ringwald if (!conn) { 693517f1ba2aSmatthias.ringwald // notify client that alloc failed 69362deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 69373e2050f7SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller 693817f1ba2aSmatthias.ringwald } 6939ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 6940f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 6941ad83dc6aS[email protected] } 6942aee5d5c1SMatthias Ringwald 6943ad83dc6aS[email protected] log_info("conn state %u", conn->state); 69443e2050f7SMatthias Ringwald // TODO: L2CAP should not send create connection command, instead a (new) gap function should be used 6945ad83dc6aS[email protected] switch (conn->state) { 6946ad83dc6aS[email protected] // if connection active exists 6947ad83dc6aS[email protected] case OPEN: 6948f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 694972cf8859SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, ERROR_CODE_SUCCESS); 69503e2050f7SMatthias Ringwald // packet not sent to controller 69513e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 6952672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 6953672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 6954672e36abSMatthias Ringwald break; 6955ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 69566c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 69576c0d5426SMatthias Ringwald if (hci_classic_operation_active()){ 69586c0d5426SMatthias Ringwald return ERROR_CODE_SUCCESS; 69596c0d5426SMatthias Ringwald } 69606c0d5426SMatthias Ringwald #endif 6961532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 6962532f91a5SMatthias Ringwald break; 6963ad83dc6aS[email protected] default: 6964ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 69653e2050f7SMatthias Ringwald // packet not sent to controller 69663e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 6967ad83dc6aS[email protected] } 6968c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 6969229331c6SMatthias Ringwald 6970229331c6SMatthias Ringwald // track outgoing connection 6971f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 69726535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 69736f35bb46SMatthias Ringwald break; 6974ee752bb8SMatthias Ringwald 69755b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 69766f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 6977ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 6978ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 6979ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 69806f35bb46SMatthias Ringwald break; 69816f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 6982c91e6e4fSMatthias Ringwald // accept_synchronous_connection? Voice setting at offset 18 6983ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 6984ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 69851f34df2cSMatthias Ringwald // track outgoing connection 69861f34df2cSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_SCO; 69871f34df2cSMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->outgoing_addr); 69886f35bb46SMatthias Ringwald break; 6989ee752bb8SMatthias Ringwald #endif 699035454696SMatthias Ringwald #endif 69914b3e1e19SMatthias Ringwald 6992a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 6993d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 69946f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 6995b04dfa37SMatthias Ringwald // white list used? 69966f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 6997b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 6998b04dfa37SMatthias Ringwald case 0: 6999b04dfa37SMatthias Ringwald // whitelist not used 7000b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 7001b04dfa37SMatthias Ringwald break; 7002b04dfa37SMatthias Ringwald case 1: 7003b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 7004b04dfa37SMatthias Ringwald break; 7005b04dfa37SMatthias Ringwald default: 7006b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 7007b04dfa37SMatthias Ringwald break; 7008b04dfa37SMatthias Ringwald } 7009c163146eSMatthias Ringwald // track outgoing connection 701005002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 7011c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 70126f35bb46SMatthias Ringwald break; 70136f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 70146ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 70156f35bb46SMatthias Ringwald break; 70169cbd2215SMatthias Ringwald #endif 70174ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 70184ae8623eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 70194ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 70204ae8623eSMatthias Ringwald status = ERROR_CODE_SUCCESS; 70214ae8623eSMatthias Ringwald num_cis = packet[3]; 70224ae8623eSMatthias Ringwald // setup hci_iso_streams 70234ae8623eSMatthias Ringwald for (i=0;i<num_cis;i++){ 70244ae8623eSMatthias Ringwald cis_handle = (hci_con_handle_t) little_endian_read_16(packet, 4 + (4 * i)); 70258d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, cis_handle, 0xff); 70264ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 70274ae8623eSMatthias Ringwald break; 70284ae8623eSMatthias Ringwald } 70294ae8623eSMatthias Ringwald } 70304ae8623eSMatthias Ringwald // free structs on error 70314ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 7032a90016deSMatthias Ringwald hci_iso_stream_requested_finalize(0xff); 70334ae8623eSMatthias Ringwald return status; 70344ae8623eSMatthias Ringwald } 70354ae8623eSMatthias Ringwald break; 70364ae8623eSMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 70374ae8623eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 70384ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 70394ae8623eSMatthias Ringwald cis_handle = (hci_con_handle_t) little_endian_read_16(packet, 3); 70408d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, cis_handle, 0xff); 70414ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 70424ae8623eSMatthias Ringwald return status; 70434ae8623eSMatthias Ringwald } 70444ae8623eSMatthias Ringwald break; 70454ae8623eSMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 70464ae8623eSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 70474ae8623eSMatthias Ringwald #endif /* ENABLE_BLE */ 70486f35bb46SMatthias Ringwald default: 70496f35bb46SMatthias Ringwald break; 7050b04dfa37SMatthias Ringwald } 705169a97523S[email protected] 70523a9fb326S[email protected] hci_stack->num_cmd_packets--; 70535bb5bc3eS[email protected] 70545bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 70553e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 70563e2050f7SMatthias Ringwald if (err != 0){ 70573e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 70583e2050f7SMatthias Ringwald } 70593e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 706031452debSmatthias.ringwald } 70618adf0ddaSmatthias.ringwald 70622bd8b7e7S[email protected] // disconnect because of security block 70632bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 70642bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 70652bd8b7e7S[email protected] if (!connection) return; 70662bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 70672bd8b7e7S[email protected] } 70682bd8b7e7S[email protected] 70692bd8b7e7S[email protected] 7070dbe1a790S[email protected] // Configure Secure Simple Pairing 7071dbe1a790S[email protected] 707235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 707335454696SMatthias Ringwald 7074dbe1a790S[email protected] // enable will enable SSP during init 707515a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 70763a9fb326S[email protected] hci_stack->ssp_enable = enable; 7077dbe1a790S[email protected] } 7078dbe1a790S[email protected] 707995d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 708015a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 70812bd8b7e7S[email protected] } 70822bd8b7e7S[email protected] 7083dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 708415a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 70853a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 7086dbe1a790S[email protected] } 708715a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 70883a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 7089dbe1a790S[email protected] } 7090dbe1a790S[email protected] 7091dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 709215a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 70933a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 7094dbe1a790S[email protected] } 70955d23aae8SMatthias Ringwald 70965d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 70975d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 70985d23aae8SMatthias Ringwald } 709922d445d8SMatthias Ringwald bool gap_secure_connections_active(void){ 7100eaf85bc8SMatthias Ringwald return hci_stack->secure_connections_active; 710122d445d8SMatthias Ringwald } 71025d23aae8SMatthias Ringwald 710335454696SMatthias Ringwald #endif 7104dbe1a790S[email protected] 710594be1a66SMatthias Ringwald // va_list part of hci_send_cmd 71063e2050f7SMatthias Ringwald uint8_t hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 7107d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 71089d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 71093e2050f7SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 71109d14b626S[email protected] } 71119d14b626S[email protected] 71125127cc62S[email protected] // for HCI INITIALIZATION 71139da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 71145127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 71155127cc62S[email protected] 71169d14b626S[email protected] hci_reserve_packet_buffer(); 71179d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 711894be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 71193e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_packet(packet, size); 7120bfea0222SMatthias Ringwald 7121593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 71223e2050f7SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) || hci_transport_synchronous()){ 7123e2d22487SMatthias Ringwald hci_release_packet_buffer(); 7124068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 7125bfea0222SMatthias Ringwald } 7126bfea0222SMatthias Ringwald 71273e2050f7SMatthias Ringwald return status; 712894be1a66SMatthias Ringwald } 71299d14b626S[email protected] 713094be1a66SMatthias Ringwald /** 713194be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 713294be1a66SMatthias Ringwald */ 71333e2050f7SMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t * cmd, ...){ 71341cd208adSmatthias.ringwald va_list argptr; 71351cd208adSmatthias.ringwald va_start(argptr, cmd); 71363e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_va_arg(cmd, argptr); 71371cd208adSmatthias.ringwald va_end(argptr); 71383e2050f7SMatthias Ringwald return status; 713993b8dc03Smatthias.ringwald } 7140c8e4258aSmatthias.ringwald 7141ee091cf1Smatthias.ringwald // Create various non-HCI events. 7142ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 7143ee091cf1Smatthias.ringwald 7144d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 7145fb37a842SMatthias Ringwald // dump packet 7146d6b06661SMatthias Ringwald if (dump) { 714792da54c4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 1, event, size); 7148d6b06661SMatthias Ringwald } 71491ef6bb52SMatthias Ringwald 7150fb37a842SMatthias Ringwald // dispatch to all event handlers 71511ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 71521ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 71531ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 71541ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 7155d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 71561ef6bb52SMatthias Ringwald } 7157d6b06661SMatthias Ringwald } 7158d6b06661SMatthias Ringwald 7159d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 7160fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 71613d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 7162d6b06661SMatthias Ringwald } 7163d6b06661SMatthias Ringwald 716435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 7165701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 71663bc639ceSMatthias Ringwald // notify SCO sender if waiting 7167701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 7168701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 71693bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 7170701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 7171701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 71723d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 71733bc639ceSMatthias Ringwald } 71743bc639ceSMatthias Ringwald } 71751cfb383eSMatthias Ringwald 71761cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 71779784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 7178ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 71791cfb383eSMatthias Ringwald 71801cfb383eSMatthias Ringwald uint8_t * eir_data; 71811cfb383eSMatthias Ringwald ad_context_t context; 71821cfb383eSMatthias Ringwald const uint8_t * name; 71831cfb383eSMatthias Ringwald uint8_t name_len; 71841cfb383eSMatthias Ringwald 71859784dac1SMatthias Ringwald if (size < 3) return; 71869784dac1SMatthias Ringwald 71871cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 7188a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 71891cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 71901cfb383eSMatthias Ringwald 71919784dac1SMatthias Ringwald switch (event_type){ 71929784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 71939784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 71949784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 71959784dac1SMatthias Ringwald break; 71969784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 71979784dac1SMatthias Ringwald if (size != 257) return; 71989784dac1SMatthias Ringwald if (num_responses != 1) return; 71999784dac1SMatthias Ringwald break; 72009784dac1SMatthias Ringwald default: 72019784dac1SMatthias Ringwald return; 72029784dac1SMatthias Ringwald } 72039784dac1SMatthias Ringwald 72041cfb383eSMatthias Ringwald // event[1] is set at the end 72051cfb383eSMatthias Ringwald int i; 72061cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 72071cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 72081cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 7209ac973fd6SMatthias Ringwald uint8_t event_size = 27; // if name is not set by EIR 72101cfb383eSMatthias Ringwald 72116535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 72120e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 72136535961aSMatthias Ringwald (void)memcpy(&event[9], 72146535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 72156535961aSMatthias Ringwald 3); // class of device 72166535961aSMatthias Ringwald (void)memcpy(&event[12], 72176535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 72186535961aSMatthias Ringwald 2); // clock offset 72191cfb383eSMatthias Ringwald 72201cfb383eSMatthias Ringwald switch (event_type){ 72211cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 72221cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 72231cfb383eSMatthias Ringwald break; 72241cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 72251cfb383eSMatthias Ringwald event[14] = 1; 7226a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 72271cfb383eSMatthias Ringwald // 16,17 = 0, size 18 72281cfb383eSMatthias Ringwald break; 72291cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 72301cfb383eSMatthias Ringwald event[14] = 1; 7231a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 723279186815SMatthias Ringwald // EIR packets only contain a single inquiry response 72331cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 72341cfb383eSMatthias Ringwald name = NULL; 7235a8c4e5adSMatthias Ringwald // Iterate over EIR data 7236a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 72371cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 72381cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 72391cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 7240ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 72411cfb383eSMatthias Ringwald switch (data_type){ 72421cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 72431cfb383eSMatthias Ringwald if (name) continue; 7244cf373d3aSMatthias Ringwald /* fall through */ 72451cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 72461cfb383eSMatthias Ringwald name = data; 72471cfb383eSMatthias Ringwald name_len = data_size; 72481cfb383eSMatthias Ringwald break; 7249ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 7250ac9136ccSMatthias Ringwald if (data_size != 8) break; 7251ac9136ccSMatthias Ringwald event[16] = 1; 72523c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 7253ac9136ccSMatthias Ringwald break; 72541cfb383eSMatthias Ringwald default: 72551cfb383eSMatthias Ringwald break; 72561cfb383eSMatthias Ringwald } 72571cfb383eSMatthias Ringwald } 72581cfb383eSMatthias Ringwald if (name){ 7259ac9136ccSMatthias Ringwald event[25] = 1; 72601cfb383eSMatthias Ringwald // truncate name if needed 72611cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 7262ac9136ccSMatthias Ringwald event[26] = len; 7263ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 72641cfb383eSMatthias Ringwald event_size += len; 72651cfb383eSMatthias Ringwald } 72661cfb383eSMatthias Ringwald break; 72677bbeb3adSMilanka Ringwald default: 72687bbeb3adSMilanka Ringwald return; 72691cfb383eSMatthias Ringwald } 72701cfb383eSMatthias Ringwald event[1] = event_size - 2; 72711cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 72721cfb383eSMatthias Ringwald } 72731cfb383eSMatthias Ringwald } 727435454696SMatthias Ringwald #endif 72753bc639ceSMatthias Ringwald 727671de195eSMatthias Ringwald void hci_emit_state(void){ 72773a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 7278425d1371Smatthias.ringwald uint8_t event[3]; 727980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 72804ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 72813a9fb326S[email protected] event[2] = hci_stack->state; 7282d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7283c8e4258aSmatthias.ringwald } 7284c8e4258aSmatthias.ringwald 728535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 72862deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 7287425d1371Smatthias.ringwald uint8_t event[13]; 7288c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 7289425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 729017f1ba2aSmatthias.ringwald event[2] = status; 72912deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 72922deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 7293c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 7294c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 7295d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7296c8e4258aSmatthias.ringwald } 729752db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 729852db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 729952db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 730052db98b2SMatthias Ringwald uint8_t event[4]; 730152db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 730252db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 730352db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 730452db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 730552db98b2SMatthias Ringwald } 730635454696SMatthias Ringwald #endif 7307c8e4258aSmatthias.ringwald 730835454696SMatthias Ringwald #ifdef ENABLE_BLE 7309d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7310667ba9d1SMatthias 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){ 73114f3229d8S[email protected] uint8_t event[21]; 73124f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 73134ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 73144f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 73154f3229d8S[email protected] event[3] = status; 7316fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 73174f3229d8S[email protected] event[6] = 0; // TODO: role 73186e2e9a6bS[email protected] event[7] = address_type; 7319724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 7320f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 7321f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 7322f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 73234f3229d8S[email protected] event[20] = 0; // master clock accuracy 7324d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 73254f3229d8S[email protected] } 732635454696SMatthias Ringwald #endif 7327d70217a2SMatthias Ringwald #endif 73284f3229d8S[email protected] 7329fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 7330fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 7331fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 7332fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 7333fd43c0e0SMatthias Ringwald } 7334fd43c0e0SMatthias Ringwald 7335fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 7336425d1371Smatthias.ringwald uint8_t event[6]; 73373c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 73384ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 73393c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 7340fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 73413c4d4b90Smatthias.ringwald event[5] = reason; 7342d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 73433c4d4b90Smatthias.ringwald } 73443c4d4b90Smatthias.ringwald 7345b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 7346e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 7347425d1371Smatthias.ringwald uint8_t event[3]; 734880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 73494ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 735043bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 7351d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 735243bfb1bdSmatthias.ringwald } 7353038bc64cSmatthias.ringwald 7354b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 7355e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 7356425d1371Smatthias.ringwald uint8_t event[2]; 735780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 73584ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 7359d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7360038bc64cSmatthias.ringwald } 73611b0e3922Smatthias.ringwald 736235454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 736335454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 736435454696SMatthias Ringwald uint8_t event[9]; 736535454696SMatthias Ringwald int pos = 0; 736635454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 73674ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 736835454696SMatthias Ringwald event[pos++] = status; 736935454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 7370d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7371381fbed8Smatthias.ringwald } 7372458bf4e8S[email protected] 737335454696SMatthias Ringwald 737435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 737535454696SMatthias Ringwald 7376b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 7377df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 7378a00031e2S[email protected] uint8_t event[5]; 7379e00caf9cS[email protected] int pos = 0; 73805611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 7381e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 7382f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 7383e00caf9cS[email protected] pos += 2; 7384e00caf9cS[email protected] event[pos++] = level; 7385d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7386e00caf9cS[email protected] } 7387e00caf9cS[email protected] 738835454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 738935454696SMatthias Ringwald if (!connection) return LEVEL_0; 73908daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 7391fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 73928daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 7393170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 73944051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 7395170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 73960e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 7397170fafaeSMatthias Ringwald security_level = LEVEL_3; 7398170fafaeSMatthias Ringwald } 7399170fafaeSMatthias Ringwald return security_level; 740035454696SMatthias Ringwald } 740135454696SMatthias Ringwald 740271fd255dSMatthias Ringwald static void hci_emit_scan_mode_changed(uint8_t discoverable, uint8_t connectable){ 740371fd255dSMatthias Ringwald uint8_t event[4]; 740471fd255dSMatthias Ringwald event[0] = BTSTACK_EVENT_SCAN_MODE_CHANGED; 740535454696SMatthias Ringwald event[1] = sizeof(event) - 2; 740671fd255dSMatthias Ringwald event[2] = discoverable; 740771fd255dSMatthias Ringwald event[3] = connectable; 7408d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7409ad83dc6aS[email protected] } 7410ad83dc6aS[email protected] 741198a2fd1cSMatthias Ringwald // query if remote side supports eSCO 741220dcdd22SMatthias Ringwald bool hci_remote_esco_supported(hci_con_handle_t con_handle){ 741398a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 741420dcdd22SMatthias Ringwald if (!connection) return false; 741576ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 741698a2fd1cSMatthias Ringwald } 741798a2fd1cSMatthias Ringwald 741867aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 741967aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 742067aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 742167aae551SMatthias Ringwald } 742267aae551SMatthias Ringwald 74232bd8b7e7S[email protected] // query if remote side supports SSP 742420dcdd22SMatthias Ringwald bool hci_remote_ssp_supported(hci_con_handle_t con_handle){ 74252bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 742620dcdd22SMatthias Ringwald if (!connection) return false; 742767aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 74282bd8b7e7S[email protected] } 74292bd8b7e7S[email protected] 743020dcdd22SMatthias Ringwald bool gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 7431df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 7432df3354fcS[email protected] } 7433df3354fcS[email protected] 74346ffc78ceSMatthias Ringwald /** 74356ffc78ceSMatthias Ringwald * Check if remote supported features query has completed 74366ffc78ceSMatthias Ringwald */ 74376ffc78ceSMatthias Ringwald bool hci_remote_features_available(hci_con_handle_t handle){ 74386ffc78ceSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 74396ffc78ceSMatthias Ringwald if (!connection) return false; 74406ffc78ceSMatthias Ringwald return (connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0; 74416ffc78ceSMatthias Ringwald } 74426ffc78ceSMatthias Ringwald 7443d6596031SMatthias Ringwald /** 7444d6596031SMatthias Ringwald * Trigger remote supported features query 7445d6596031SMatthias Ringwald */ 7446c9742cd1SMatthias Ringwald 7447c9742cd1SMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection){ 7448c9742cd1SMatthias Ringwald if ((connection->bonding_flags & (BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_RECEIVED_REMOTE_FEATURES)) == 0){ 7449c9742cd1SMatthias Ringwald connection->bonding_flags |= BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 7450c9742cd1SMatthias Ringwald } 7451c9742cd1SMatthias Ringwald } 7452c9742cd1SMatthias Ringwald 7453d6596031SMatthias Ringwald void hci_remote_features_query(hci_con_handle_t con_handle){ 7454d6596031SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 7455d6596031SMatthias Ringwald if (!connection) return; 7456c9742cd1SMatthias Ringwald hci_trigger_remote_features_for_connection(connection); 7457d6596031SMatthias Ringwald hci_run(); 7458d6596031SMatthias Ringwald } 7459d6596031SMatthias Ringwald 7460458bf4e8S[email protected] // GAP API 7461458bf4e8S[email protected] /** 7462458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 7463458bf4e8S[email protected] * @praram enabled 7464458bf4e8S[email protected] */ 74654c57c146S[email protected] void gap_set_bondable_mode(int enable){ 74663a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 7467458bf4e8S[email protected] } 74684ef6443cSMatthias Ringwald /** 74694ef6443cSMatthias Ringwald * @brief Get bondable mode. 74704ef6443cSMatthias Ringwald * @return 1 if bondable 74714ef6443cSMatthias Ringwald */ 74724ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 74734ef6443cSMatthias Ringwald return hci_stack->bondable; 74744ef6443cSMatthias Ringwald } 7475cb230b9dS[email protected] 7476cb230b9dS[email protected] /** 747734d2123cS[email protected] * @brief map link keys to security levels 7478cb230b9dS[email protected] */ 747934d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 748034d2123cS[email protected] switch (link_key_type){ 74813c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 74823c68dfa9S[email protected] return LEVEL_4; 74833c68dfa9S[email protected] case COMBINATION_KEY: 74843c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 74853c68dfa9S[email protected] return LEVEL_3; 74863c68dfa9S[email protected] default: 74873c68dfa9S[email protected] return LEVEL_2; 74883c68dfa9S[email protected] } 7489cb230b9dS[email protected] } 7490cb230b9dS[email protected] 74918b35e16aSMatthias Ringwald /** 74928b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 74938b35e16aSMatthias Ringwald */ 749436c3546bSMatthias Ringwald bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 74958b35e16aSMatthias Ringwald switch (link_key_type){ 74968b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 74978b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 749836c3546bSMatthias Ringwald return true; 74998b35e16aSMatthias Ringwald default: 750036c3546bSMatthias Ringwald return false; 75018b35e16aSMatthias Ringwald } 75028b35e16aSMatthias Ringwald } 75038b35e16aSMatthias Ringwald 75048b35e16aSMatthias Ringwald /** 75058b35e16aSMatthias Ringwald * @brief map link keys to authenticated 75068b35e16aSMatthias Ringwald */ 75077040ba26SMatthias Ringwald bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 75088b35e16aSMatthias Ringwald switch (link_key_type){ 75098b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 75108b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 75117040ba26SMatthias Ringwald return true; 75128b35e16aSMatthias Ringwald default: 75137040ba26SMatthias Ringwald return false; 75148b35e16aSMatthias Ringwald } 75158b35e16aSMatthias Ringwald } 75168b35e16aSMatthias Ringwald 7517552b9260SMatthias Ringwald bool gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 75185127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 7519106d6d11S[email protected] return level > LEVEL_2; 7520106d6d11S[email protected] } 7521106d6d11S[email protected] 752234d2123cS[email protected] /** 752334d2123cS[email protected] * @brief get current security level 752434d2123cS[email protected] */ 752534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 752634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 752734d2123cS[email protected] if (!connection) return LEVEL_0; 752834d2123cS[email protected] return gap_security_level_for_connection(connection); 752934d2123cS[email protected] } 753034d2123cS[email protected] 7531cb230b9dS[email protected] /** 7532cb230b9dS[email protected] * @brief request connection to device to 7533cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 7534cb230b9dS[email protected] */ 753534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 753634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 753734d2123cS[email protected] if (!connection){ 7538a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 753934d2123cS[email protected] return; 754034d2123cS[email protected] } 7541defbf200SMatthias Ringwald 7542defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 7543defbf200SMatthias Ringwald 7544bc00e12cSMatthias 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) 7545bc00e12cSMatthias Ringwald // available on the BR/EDR physical transport require Security Mode 4, Level 4 " 7546bc00e12cSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode && (requested_level != LEVEL_0)){ 7547bc00e12cSMatthias Ringwald requested_level = LEVEL_4; 7548bc00e12cSMatthias Ringwald } 7549bc00e12cSMatthias Ringwald 755034d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 755183d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 755283d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 755383d08d7cSMatthias Ringwald 7554dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 7555dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 7556dbd5dcc3SMatthias Ringwald if (authentication_active){ 75571cf0a6c8SMatthias Ringwald // authentication already active 755883d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 755983d08d7cSMatthias Ringwald // increase requested level as new level is higher 756083d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 756183d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 756283d08d7cSMatthias Ringwald } 75631cf0a6c8SMatthias Ringwald } else { 756483d08d7cSMatthias Ringwald // no request active, notify if security sufficient 756583d08d7cSMatthias Ringwald if (requested_level <= current_level){ 7566a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 756734d2123cS[email protected] return; 756834d2123cS[email protected] } 7569a00031e2S[email protected] 7570e060c07dSMatthias Ringwald // store request 757134d2123cS[email protected] connection->requested_security_level = requested_level; 7572a00031e2S[email protected] 75731cf0a6c8SMatthias Ringwald // start to authenticate connection 75741eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 75757fdbaf79SMatthias Ringwald 75767fdbaf79SMatthias Ringwald // request remote features if not already active, also trigger hci_run 75777fdbaf79SMatthias Ringwald hci_remote_features_query(con_handle); 7578e00caf9cS[email protected] } 75791cf0a6c8SMatthias Ringwald } 7580ad83dc6aS[email protected] 7581ad83dc6aS[email protected] /** 7582ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 7583ad83dc6aS[email protected] * @param device 7584ad83dc6aS[email protected] * @param request MITM protection 7585ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 7586ad83dc6aS[email protected] */ 7587ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 7588ad83dc6aS[email protected] 7589ad83dc6aS[email protected] // create connection state machine 7590f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 7591ad83dc6aS[email protected] 7592ad83dc6aS[email protected] if (!connection){ 7593ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 7594ad83dc6aS[email protected] } 7595ad83dc6aS[email protected] 7596ad83dc6aS[email protected] // delete linkn key 759715a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 7598ad83dc6aS[email protected] 7599ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 7600ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 7601ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 7602f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 7603ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 7604ad83dc6aS[email protected] 7605ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 7606ad83dc6aS[email protected] 7607ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 7608ad83dc6aS[email protected] // handle: authentication failure 7609ad83dc6aS[email protected] // handle: disconnect on done 7610ad83dc6aS[email protected] 7611ad83dc6aS[email protected] hci_run(); 7612ad83dc6aS[email protected] 7613ad83dc6aS[email protected] return 0; 7614ad83dc6aS[email protected] } 76158e618f72S[email protected] 76168e618f72S[email protected] void gap_set_local_name(const char * local_name){ 76178e618f72S[email protected] hci_stack->local_name = local_name; 7618baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_LOCAL_NAME; 761959d59ecfSMatthias Ringwald // also update EIR if not set by user 762059d59ecfSMatthias Ringwald if (hci_stack->eir_data == NULL){ 7621baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 762259d59ecfSMatthias Ringwald } 762359d59ecfSMatthias Ringwald hci_run(); 76248e618f72S[email protected] } 76251aeec2ebSMatthias Ringwald #endif 76268e618f72S[email protected] 762735454696SMatthias Ringwald 762835454696SMatthias Ringwald #ifdef ENABLE_BLE 762935454696SMatthias Ringwald 7630d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7631d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 7632fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 76337bdc6798S[email protected] hci_run(); 76347bdc6798S[email protected] } 76358e618f72S[email protected] 7636d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 7637fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 76387bdc6798S[email protected] hci_run(); 76397bdc6798S[email protected] } 76404f3229d8S[email protected] 7641a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 7642ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 7643a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 7644ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 7645ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 76468b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 7647ef11999fSmatthias.ringwald hci_run(); 7648ef11999fSmatthias.ringwald } 76494f3229d8S[email protected] 7650a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 7651a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 7652a7a719e9SMatthias Ringwald } 7653a7a719e9SMatthias Ringwald 7654667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 76554f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 76564f3229d8S[email protected] if (!conn){ 7657d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 7658d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 7659d32b3f05SMatthias Ringwald log_error("le connection already active"); 7660d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 7661d32b3f05SMatthias Ringwald } 7662d32b3f05SMatthias Ringwald 7663d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 76642e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 76654f3229d8S[email protected] if (!conn){ 76664f3229d8S[email protected] // notify client that alloc failed 76676e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 7668d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 7669472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 76704f3229d8S[email protected] } 7671d5b1a89eSMatthias Ringwald 7672d5b1a89eSMatthias Ringwald // set le connecting state 7673d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 7674d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 7675d5b1a89eSMatthias Ringwald } 7676d5b1a89eSMatthias Ringwald 76774f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 7678d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 7679564fca32S[email protected] hci_run(); 7680b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 76814f3229d8S[email protected] } 76820bf6344aS[email protected] 76830bf6344aS[email protected] if (!hci_is_le_connection(conn) || 7684a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 76850e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 76862e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 7687d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 7688616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 76890bf6344aS[email protected] } 76900bf6344aS[email protected] 7691b0136355SMatthias Ringwald // check if connection was just disconnected 7692b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 7693b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 7694b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 7695b0136355SMatthias Ringwald hci_run(); 7696b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 7697b0136355SMatthias Ringwald } 7698b0136355SMatthias Ringwald 7699d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 7700d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 77014f3229d8S[email protected] hci_run(); 7702b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 77034f3229d8S[email protected] } 77044f3229d8S[email protected] 77057851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 7706d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 7707665d90f2SMatthias Ringwald btstack_linked_item_t *it; 7708a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 77090bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 77100bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 77110bf6344aS[email protected] switch (conn->state){ 7712a6725849S[email protected] case SEND_CREATE_CONNECTION: 77137851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 77147851196eSmatthias.ringwald return conn; 77157851196eSmatthias.ringwald default: 77167851196eSmatthias.ringwald break; 77177851196eSmatthias.ringwald }; 77187851196eSmatthias.ringwald } 77197851196eSmatthias.ringwald return NULL; 77207851196eSmatthias.ringwald } 77217851196eSmatthias.ringwald 7722d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 772314f2d8f0SMatthias Ringwald hci_connection_t * conn; 772414f2d8f0SMatthias Ringwald switch (hci_stack->le_connecting_request){ 772514f2d8f0SMatthias Ringwald case LE_CONNECTING_IDLE: 772614f2d8f0SMatthias Ringwald break; 772714f2d8f0SMatthias Ringwald case LE_CONNECTING_WHITELIST: 772814f2d8f0SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 772914f2d8f0SMatthias Ringwald hci_run(); 773014f2d8f0SMatthias Ringwald break; 773114f2d8f0SMatthias Ringwald case LE_CONNECTING_DIRECT: 773214f2d8f0SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 773314f2d8f0SMatthias Ringwald conn = gap_get_outgoing_connection(); 773414f2d8f0SMatthias Ringwald if (conn == NULL){ 773514f2d8f0SMatthias Ringwald hci_run(); 773614f2d8f0SMatthias Ringwald } else { 77377851196eSmatthias.ringwald switch (conn->state){ 77387851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 77397851196eSmatthias.ringwald // skip sending create connection and emit event instead 77402e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 7741665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 77427851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 77430bf6344aS[email protected] break; 7744a6725849S[email protected] case SENT_CREATE_CONNECTION: 7745ea04fbd8SMatthias Ringwald // let hci_run_general_gap_le cancel outgoing connection 77460bf6344aS[email protected] hci_run(); 77470bf6344aS[email protected] break; 77480bf6344aS[email protected] default: 77490bf6344aS[email protected] break; 77500bf6344aS[email protected] } 775114f2d8f0SMatthias Ringwald } 775214f2d8f0SMatthias Ringwald break; 775343dfd98aSMatthias Ringwald default: 775414f2d8f0SMatthias Ringwald btstack_unreachable(); 775514f2d8f0SMatthias Ringwald break; 775614f2d8f0SMatthias Ringwald } 775714f2d8f0SMatthias Ringwald return ERROR_CODE_SUCCESS; 7758e31f89a7S[email protected] } 77594f3229d8S[email protected] 7760c37a3166S[email protected] /** 77616012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 7762cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 7763cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 77646012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 77656012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 77666012052bSMatthias Ringwald * @param conn_latency, default: 4 77676012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 77686012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 77696012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 77706012052bSMatthias Ringwald */ 77716012052bSMatthias Ringwald 7772cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 7773cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 7774cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 7775cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 7776cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 77776012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 77786012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 77796012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 77806012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 77816012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 77826012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 77836012052bSMatthias Ringwald } 778413e645d2SMatthias Ringwald #endif 77856012052bSMatthias Ringwald 77866012052bSMatthias Ringwald /** 7787c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 7788c37a3166S[email protected] * @param handle 7789c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 7790c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 7791c37a3166S[email protected] * @param conn_latency 7792c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 77936b65794dSMilanka Ringwald * @return 0 if ok 7794c37a3166S[email protected] */ 7795c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 7796c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 7797c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 7798c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7799c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 7800c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 7801c37a3166S[email protected] connection->le_conn_latency = conn_latency; 7802c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 780384cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 7804cfc59f1bSMatthias Ringwald hci_run(); 7805c37a3166S[email protected] return 0; 7806c37a3166S[email protected] } 7807c37a3166S[email protected] 780845c102fdSMatthias Ringwald /** 7809b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 7810b68d7bc3SMatthias Ringwald * @param handle 7811b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 7812b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 7813b68d7bc3SMatthias Ringwald * @param conn_latency 7814b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 78156b65794dSMilanka Ringwald * @return 0 if ok 7816b68d7bc3SMatthias Ringwald */ 7817b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 7818b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 7819b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 7820b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7821b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 7822b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 7823b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 7824b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 7825b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 782609c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 782709c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 7828b68d7bc3SMatthias Ringwald return 0; 7829b68d7bc3SMatthias Ringwald } 7830b68d7bc3SMatthias Ringwald 7831d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 7832d70217a2SMatthias Ringwald 7833b68d7bc3SMatthias Ringwald /** 783445c102fdSMatthias Ringwald * @brief Set Advertisement Data 783545c102fdSMatthias Ringwald * @param advertising_data_length 783645c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 783745c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 783845c102fdSMatthias Ringwald */ 783945c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 784045c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 784145c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 7842501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 7843bbc366e6SMatthias Ringwald hci_run(); 784445c102fdSMatthias Ringwald } 7845501f56b3SMatthias Ringwald 7846501f56b3SMatthias Ringwald /** 7847501f56b3SMatthias Ringwald * @brief Set Scan Response Data 7848501f56b3SMatthias Ringwald * @param advertising_data_length 7849501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 7850501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 7851501f56b3SMatthias Ringwald */ 7852501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 7853501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 7854501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 7855501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 7856bbc366e6SMatthias Ringwald hci_run(); 785745c102fdSMatthias Ringwald } 785845c102fdSMatthias Ringwald 785945c102fdSMatthias Ringwald /** 786045c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 786145c102fdSMatthias Ringwald * @param adv_int_min 786245c102fdSMatthias Ringwald * @param adv_int_max 786345c102fdSMatthias Ringwald * @param adv_type 786445c102fdSMatthias Ringwald * @param direct_address_type 786545c102fdSMatthias Ringwald * @param direct_address 786645c102fdSMatthias Ringwald * @param channel_map 786745c102fdSMatthias Ringwald * @param filter_policy 786845c102fdSMatthias Ringwald * 786945c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 787045c102fdSMatthias Ringwald */ 787145c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 7872b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 787345c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 787445c102fdSMatthias Ringwald 787545c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 787645c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 787745c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 787845c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 787945c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 788045c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 78816535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 78826535961aSMatthias Ringwald 6); 788345c102fdSMatthias Ringwald 78846bf435a6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 78856bf435a6SMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_PARAMS_SET; 7886bbc366e6SMatthias Ringwald hci_run(); 788745c102fdSMatthias Ringwald } 788845c102fdSMatthias Ringwald 788945c102fdSMatthias Ringwald /** 789045c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 789145c102fdSMatthias Ringwald * @param enabled 789245c102fdSMatthias Ringwald */ 789345c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 7894a408e724SMatthias Ringwald if (enabled == 0){ 7895a408e724SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ENABLED; 7896a408e724SMatthias Ringwald } else { 7897a408e724SMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_ENABLED; 7898a408e724SMatthias Ringwald } 7899bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 7900cfc59f1bSMatthias Ringwald hci_run(); 790145c102fdSMatthias Ringwald } 790245c102fdSMatthias Ringwald 7903a5770ecbSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 7904a5770ecbSMatthias Ringwald static le_advertising_set_t * hci_advertising_set_for_handle(uint8_t advertising_handle){ 7905a5770ecbSMatthias Ringwald btstack_linked_list_iterator_t it; 7906a5770ecbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 7907a5770ecbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7908a5770ecbSMatthias Ringwald le_advertising_set_t * item = (le_advertising_set_t *) btstack_linked_list_iterator_next(&it); 7909a5770ecbSMatthias Ringwald if ( item->advertising_handle == advertising_handle ) { 7910a5770ecbSMatthias Ringwald return item; 7911a5770ecbSMatthias Ringwald } 7912a5770ecbSMatthias Ringwald } 7913a5770ecbSMatthias Ringwald return NULL; 7914a5770ecbSMatthias Ringwald } 7915a5770ecbSMatthias Ringwald 7916a5770ecbSMatthias 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){ 7917a5770ecbSMatthias Ringwald // find free advertisement handle 7918a5770ecbSMatthias Ringwald uint8_t advertisement_handle; 7919a5770ecbSMatthias Ringwald for (advertisement_handle = 1; advertisement_handle <= LE_EXTENDED_ADVERTISING_MAX_HANDLE; advertisement_handle++){ 7920a5770ecbSMatthias Ringwald if (hci_advertising_set_for_handle(advertisement_handle) == NULL) break; 7921a5770ecbSMatthias Ringwald } 7922a5770ecbSMatthias Ringwald if (advertisement_handle > LE_EXTENDED_ADVERTISING_MAX_HANDLE) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 7923a5770ecbSMatthias Ringwald // clear 7924a5770ecbSMatthias Ringwald memset(storage, 0, sizeof(le_advertising_set_t)); 7925a5770ecbSMatthias Ringwald // copy params 79260cbe4690SMatthias Ringwald storage->advertising_handle = advertisement_handle; 792757a04237SMatthias Ringwald memcpy(&storage->extended_params, advertising_parameters, sizeof(le_extended_advertising_parameters_t)); 7928a5770ecbSMatthias Ringwald // add to list 7929a5770ecbSMatthias Ringwald bool add_ok = btstack_linked_list_add(&hci_stack->le_advertising_sets, (btstack_linked_item_t *) storage); 7930a5770ecbSMatthias Ringwald if (!add_ok) return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 79310cbe4690SMatthias Ringwald *out_advertising_handle = advertisement_handle; 7932a5770ecbSMatthias Ringwald // set tasks and start 7933a5770ecbSMatthias Ringwald storage->tasks = LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7934a5770ecbSMatthias Ringwald hci_run(); 7935a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7936a5770ecbSMatthias Ringwald } 7937a5770ecbSMatthias Ringwald 7938a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_set_params(uint8_t advertising_handle, const le_extended_advertising_parameters_t * advertising_parameters){ 7939a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7940a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 794157a04237SMatthias Ringwald memcpy(&advertising_set->extended_params, advertising_parameters, sizeof(le_extended_advertising_parameters_t)); 7942a5770ecbSMatthias Ringwald // set tasks and start 7943a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7944a5770ecbSMatthias Ringwald hci_run(); 7945a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7946a5770ecbSMatthias Ringwald } 7947a5770ecbSMatthias Ringwald 7948a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_get_params(uint8_t advertising_handle, le_extended_advertising_parameters_t * advertising_parameters){ 7949a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7950a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 795157a04237SMatthias Ringwald memcpy(advertising_parameters, &advertising_set->extended_params, sizeof(le_extended_advertising_parameters_t)); 7952a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7953a5770ecbSMatthias Ringwald } 7954a5770ecbSMatthias Ringwald 7955a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_set_random_address(uint8_t advertising_handle, bd_addr_t random_address){ 7956a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7957a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7958a5770ecbSMatthias Ringwald memcpy(advertising_set->random_address, random_address, 6); 7959a5770ecbSMatthias Ringwald // set tasks and start 7960a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 7961a5770ecbSMatthias Ringwald hci_run(); 7962a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7963a5770ecbSMatthias Ringwald } 7964a5770ecbSMatthias Ringwald 79652e4aaf70SMatthias Ringwald uint8_t gap_extended_advertising_set_adv_data(uint8_t advertising_handle, uint16_t advertising_data_length, const uint8_t * advertising_data){ 7966a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7967a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7968a5770ecbSMatthias Ringwald advertising_set->adv_data = advertising_data; 7969a5770ecbSMatthias Ringwald advertising_set->adv_data_len = advertising_data_length; 7970a5770ecbSMatthias Ringwald // set tasks and start 7971a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 7972a5770ecbSMatthias Ringwald hci_run(); 7973a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7974a5770ecbSMatthias Ringwald } 7975a5770ecbSMatthias Ringwald 79762e4aaf70SMatthias 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){ 7977a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7978a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7979a5770ecbSMatthias Ringwald advertising_set->scan_data = scan_response_data; 7980a5770ecbSMatthias Ringwald advertising_set->scan_data_len = scan_response_data_length; 7981a5770ecbSMatthias Ringwald // set tasks and start 7982a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 7983a5770ecbSMatthias Ringwald hci_run(); 7984a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7985a5770ecbSMatthias Ringwald } 7986a5770ecbSMatthias Ringwald 7987a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_start(uint8_t advertising_handle, uint16_t timeout, uint8_t num_extended_advertising_events){ 7988a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7989a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7990a5770ecbSMatthias Ringwald advertising_set->enable_timeout = timeout; 7991a5770ecbSMatthias Ringwald advertising_set->enable_max_scan_events = num_extended_advertising_events; 7992a5770ecbSMatthias Ringwald // set tasks and start 7993a5770ecbSMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_ENABLED; 7994a5770ecbSMatthias Ringwald hci_run(); 7995a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7996a5770ecbSMatthias Ringwald } 7997a5770ecbSMatthias Ringwald 7998a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_stop(uint8_t advertising_handle){ 7999a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8000a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8001a5770ecbSMatthias Ringwald // set tasks and start 8002a5770ecbSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_ENABLED; 8003a5770ecbSMatthias Ringwald hci_run(); 8004a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 8005a5770ecbSMatthias Ringwald } 8006a5770ecbSMatthias Ringwald 8007c814a904SMatthias Ringwald uint8_t gap_extended_advertising_remove(uint8_t advertising_handle){ 8008c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8009c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8010c814a904SMatthias Ringwald // set tasks and start 8011c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_REMOVE_SET; 8012c814a904SMatthias Ringwald hci_run(); 8013c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8014c814a904SMatthias Ringwald } 8015c814a904SMatthias Ringwald 8016c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 8017c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_set_params(uint8_t advertising_handle, const le_periodic_advertising_parameters_t * advertising_parameters){ 8018c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8019c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8020c814a904SMatthias Ringwald // periodic advertising requires neither connectable, scannable, legacy or anonymous 8021c814a904SMatthias Ringwald if ((advertising_set->extended_params.advertising_event_properties & 0x1f) != 0) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 8022c814a904SMatthias Ringwald memcpy(&advertising_set->periodic_params, advertising_parameters, sizeof(le_periodic_advertising_parameters_t)); 8023c814a904SMatthias Ringwald // set tasks and start 8024c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS; 8025c814a904SMatthias Ringwald hci_run(); 8026c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8027c814a904SMatthias Ringwald } 8028c814a904SMatthias Ringwald 8029c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_get_params(uint8_t advertising_handle, le_periodic_advertising_parameters_t * advertising_parameters){ 8030c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8031c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8032c814a904SMatthias Ringwald memcpy(advertising_parameters, &advertising_set->extended_params, sizeof(le_periodic_advertising_parameters_t)); 8033c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8034c814a904SMatthias Ringwald } 8035c814a904SMatthias Ringwald 8036c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_set_data(uint8_t advertising_handle, uint16_t periodic_data_length, const uint8_t * periodic_data){ 8037c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8038c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8039c814a904SMatthias Ringwald advertising_set->periodic_data = periodic_data; 8040c814a904SMatthias Ringwald advertising_set->periodic_data_len = periodic_data_length; 8041c814a904SMatthias Ringwald // set tasks and start 8042c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA; 8043c814a904SMatthias Ringwald hci_run(); 8044c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8045c814a904SMatthias Ringwald } 8046c814a904SMatthias Ringwald 804753f240c0SMatthias Ringwald uint8_t gap_periodic_advertising_start(uint8_t advertising_handle, bool include_adi){ 804853f240c0SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 804953f240c0SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 805053f240c0SMatthias Ringwald // set tasks and start 805153f240c0SMatthias Ringwald advertising_set->periodic_include_adi = include_adi; 805253f240c0SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED; 805353f240c0SMatthias Ringwald hci_run(); 805453f240c0SMatthias Ringwald return ERROR_CODE_SUCCESS; 805553f240c0SMatthias Ringwald } 805653f240c0SMatthias Ringwald 805753f240c0SMatthias Ringwald uint8_t gap_periodic_advertising_stop(uint8_t advertising_handle){ 805853f240c0SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 805953f240c0SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 806053f240c0SMatthias Ringwald // set tasks and start 806153f240c0SMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED; 806253f240c0SMatthias Ringwald hci_run(); 806353f240c0SMatthias Ringwald return ERROR_CODE_SUCCESS; 806453f240c0SMatthias Ringwald } 8065c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 806653f240c0SMatthias Ringwald 8067a5770ecbSMatthias Ringwald #endif 8068a5770ecbSMatthias Ringwald 806935454696SMatthias Ringwald #endif 807006e5cf96SMatthias Ringwald 807106e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 807206e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 807306e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 807406e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 807506e5cf96SMatthias Ringwald 807664068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 807706e5cf96SMatthias Ringwald // update advertisement parameters, too 807806e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 8079bbc366e6SMatthias Ringwald hci_run(); 808064068776SMatthias Ringwald #endif 808164068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 808206e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 808364068776SMatthias Ringwald #endif 808406e5cf96SMatthias Ringwald } 808506e5cf96SMatthias Ringwald 80869136bde8SMatthias Ringwald void hci_le_random_address_set(const bd_addr_t random_address){ 80879136bde8SMatthias Ringwald memcpy(hci_stack->le_random_address, random_address, 6); 80889136bde8SMatthias Ringwald hci_stack->le_random_address_set = true; 80899136bde8SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 80909136bde8SMatthias Ringwald hci_run(); 80919136bde8SMatthias Ringwald } 80929136bde8SMatthias Ringwald 8093d70217a2SMatthias Ringwald #endif 809445c102fdSMatthias Ringwald 8095616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 80965917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 80975917a5c5S[email protected] if (!conn){ 80987851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 8099616edd56SMatthias Ringwald return 0; 81005917a5c5S[email protected] } 81017fd7aa6fSMatthias Ringwald // ignore if already disconnected 81027fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 81037fd7aa6fSMatthias Ringwald return 0; 81047fd7aa6fSMatthias Ringwald } 81055917a5c5S[email protected] conn->state = SEND_DISCONNECT; 81065917a5c5S[email protected] hci_run(); 8107616edd56SMatthias Ringwald return 0; 81084f3229d8S[email protected] } 810904a6ef8cSmatthias.ringwald 8110228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 8111228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8112228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 811322df2fe2SMatthias Ringwald hci_connection->gap_connection_tasks |= GAP_CONNECTION_TASK_READ_RSSI; 8114228e430cSMatthias Ringwald hci_run(); 8115228e430cSMatthias Ringwald return 1; 8116228e430cSMatthias Ringwald } 8117228e430cSMatthias Ringwald 8118a1bf5ae7SMatthias Ringwald /** 8119a1bf5ae7SMatthias Ringwald * @brief Get connection type 8120a1bf5ae7SMatthias Ringwald * @param con_handle 8121a1bf5ae7SMatthias Ringwald * @result connection_type 8122a1bf5ae7SMatthias Ringwald */ 8123a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 8124a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 8125a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 8126a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 8127a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 8128a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8129a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 8130a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 8131a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 8132f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8133a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 8134a1bf5ae7SMatthias Ringwald default: 8135a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 8136a1bf5ae7SMatthias Ringwald } 8137a1bf5ae7SMatthias Ringwald } 8138a1bf5ae7SMatthias Ringwald 81392dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 81402dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 81412dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 81422dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 81432dceb1d6SMatthias Ringwald } 81442dceb1d6SMatthias Ringwald 81452dceb1d6SMatthias Ringwald 814644f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 8147667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 814888a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 814988a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 815088a03c8dSMatthias Ringwald conn->request_role = role; 815188a03c8dSMatthias Ringwald hci_run(); 8152d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 815388a03c8dSMatthias Ringwald } 815444f858f3SMatthias Ringwald #endif 815588a03c8dSMatthias Ringwald 8156a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 81574f551432SMatthias Ringwald 8158b45b7749SMilanka 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){ 8159b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 8160b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8161b90f6e0aSMatthias Ringwald 8162b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 8163b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 8164b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 8165b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 8166b90f6e0aSMatthias Ringwald 8167b90f6e0aSMatthias Ringwald hci_run(); 8168b90f6e0aSMatthias Ringwald 8169b90f6e0aSMatthias Ringwald return 0; 8170b90f6e0aSMatthias Ringwald } 8171b90f6e0aSMatthias Ringwald 8172667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 81737a92a9dbSMatthias Ringwald // check if already in list 81747a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 81757a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 81767a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 81777a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 81787a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 81797a92a9dbSMatthias Ringwald continue; 81807a92a9dbSMatthias Ringwald } 81817a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 81827a92a9dbSMatthias Ringwald continue; 81837a92a9dbSMatthias Ringwald } 8184287379ccSMatthias Ringwald // disallow if already scheduled to add 8185287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 81867a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 81877a92a9dbSMatthias Ringwald } 8188287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 8189287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 8190287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 8191287379ccSMatthias Ringwald } 81927a92a9dbSMatthias Ringwald // alloc and add to list 8193e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 8194e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 8195e83201bcSMatthias Ringwald entry->address_type = address_type; 81966535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 8197e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 8198665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 8199226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 8200ac9c45e0SMatthias Ringwald } 8201ac9c45e0SMatthias Ringwald 8202667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 8203665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8204665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 8205665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8206665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 82077a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 82087a92a9dbSMatthias Ringwald continue; 82097a92a9dbSMatthias Ringwald } 82107a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 82117a92a9dbSMatthias Ringwald continue; 82127a92a9dbSMatthias Ringwald } 8213e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 8214e83201bcSMatthias Ringwald // remove from controller if already present 8215e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 8216a3b69fdeSMatthias Ringwald } else { 8217226db5efSMatthias Ringwald // directly remove entry from whitelist 8218665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8219e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 8220e83201bcSMatthias Ringwald } 8221a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8222a3b69fdeSMatthias Ringwald } 8223a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 822442ff5ba1SMatthias Ringwald } 822542ff5ba1SMatthias Ringwald 8226226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 8227665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8228665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 8229665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8230665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 8231e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 8232e83201bcSMatthias Ringwald // remove from controller if already present 8233e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 8234e83201bcSMatthias Ringwald continue; 8235e83201bcSMatthias Ringwald } 823691915b0bSMatthias Ringwald // directly remove entry from whitelist 8237665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8238e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 8239e83201bcSMatthias Ringwald } 8240226db5efSMatthias Ringwald } 8241226db5efSMatthias Ringwald 824283dc1a98SMatthias Ringwald // free all entries unconditionally 824383dc1a98SMatthias Ringwald static void hci_whitelist_free(void){ 824483dc1a98SMatthias Ringwald btstack_linked_list_iterator_t lit; 824583dc1a98SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 824683dc1a98SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 824783dc1a98SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 824883dc1a98SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 824983dc1a98SMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 825083dc1a98SMatthias Ringwald } 825183dc1a98SMatthias Ringwald } 825283dc1a98SMatthias Ringwald 8253a3b69fdeSMatthias Ringwald /** 8254a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 82556b65794dSMilanka Ringwald * @return 0 if ok 8256a3b69fdeSMatthias Ringwald */ 8257a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 8258a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 8259a3b69fdeSMatthias Ringwald hci_run(); 8260a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8261a3b69fdeSMatthias Ringwald } 8262a3b69fdeSMatthias Ringwald 8263a3b69fdeSMatthias Ringwald /** 8264a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 8265a3b69fdeSMatthias Ringwald * @param address_typ 8266a3b69fdeSMatthias Ringwald * @param address 82676b65794dSMilanka Ringwald * @return 0 if ok 8268a3b69fdeSMatthias Ringwald */ 8269667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 8270a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 8271a3b69fdeSMatthias Ringwald if (status){ 8272a3b69fdeSMatthias Ringwald return status; 8273a3b69fdeSMatthias Ringwald } 8274a3b69fdeSMatthias Ringwald hci_run(); 8275a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8276a3b69fdeSMatthias Ringwald } 8277a3b69fdeSMatthias Ringwald 8278a3b69fdeSMatthias Ringwald /** 8279a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 8280a3b69fdeSMatthias Ringwald * @param address_typ 8281a3b69fdeSMatthias Ringwald * @param address 82826b65794dSMilanka Ringwald * @return 0 if ok 8283a3b69fdeSMatthias Ringwald */ 8284667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 8285a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 8286a3b69fdeSMatthias Ringwald if (status){ 8287a3b69fdeSMatthias Ringwald return status; 8288a3b69fdeSMatthias Ringwald } 8289a3b69fdeSMatthias Ringwald hci_run(); 8290a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8291a3b69fdeSMatthias Ringwald } 8292a3b69fdeSMatthias Ringwald 8293226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 8294226db5efSMatthias Ringwald /** 829595e257d9SMatthias Ringwald * @brief Connect with Whitelist 829695e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 82976b65794dSMilanka Ringwald * @return - if ok 829895e257d9SMatthias Ringwald */ 829995e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 830095e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 830195e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 830295e257d9SMatthias Ringwald } 830395e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 830495e257d9SMatthias Ringwald hci_run(); 830595e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 830695e257d9SMatthias Ringwald } 830795e257d9SMatthias Ringwald 830895e257d9SMatthias Ringwald /** 8309226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 8310226db5efSMatthias Ringwald * @param address_typ 8311226db5efSMatthias Ringwald * @param address 83126b65794dSMilanka Ringwald * @return 0 if ok 8313226db5efSMatthias Ringwald */ 8314667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 831595e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 8316226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 8317226db5efSMatthias Ringwald } 8318226db5efSMatthias Ringwald 8319226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 832063f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 8321226db5efSMatthias Ringwald return status; 8322226db5efSMatthias Ringwald } 8323226db5efSMatthias Ringwald 8324226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 8325226db5efSMatthias Ringwald 8326226db5efSMatthias Ringwald hci_run(); 832795e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8328226db5efSMatthias Ringwald } 8329226db5efSMatthias Ringwald 8330226db5efSMatthias Ringwald /** 8331226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 8332226db5efSMatthias Ringwald * @param address_typ 8333226db5efSMatthias Ringwald * @param address 83346b65794dSMilanka Ringwald * @return 0 if ok 8335226db5efSMatthias Ringwald */ 8336667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 833795e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 833895e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 833995e257d9SMatthias Ringwald } 834095e257d9SMatthias Ringwald 8341226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 834295e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 834395e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 834495e257d9SMatthias Ringwald } 8345226db5efSMatthias Ringwald hci_run(); 8346226db5efSMatthias Ringwald return 0; 8347226db5efSMatthias Ringwald } 8348226db5efSMatthias Ringwald 8349226db5efSMatthias Ringwald /** 8350226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 8351226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 8352226db5efSMatthias Ringwald */ 835395e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 835495e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 835595e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 835695e257d9SMatthias Ringwald } 8357226db5efSMatthias Ringwald hci_whitelist_clear(); 8358226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 8359e83201bcSMatthias Ringwald hci_run(); 836095e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8361ac9c45e0SMatthias Ringwald } 8362c9db5c21SMilanka Ringwald 8363b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 8364b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 8365c9db5c21SMilanka Ringwald if (!conn) return 0; 8366c9db5c21SMilanka Ringwald return conn->le_connection_interval; 8367c9db5c21SMilanka Ringwald } 8368d70217a2SMatthias Ringwald #endif 83694f551432SMatthias Ringwald #endif 83704f551432SMatthias Ringwald 837135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 8372ac9c45e0SMatthias Ringwald /** 8373ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 8374a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 8375ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 8376ff00ed1cSMatthias Ringwald */ 8377ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 8378ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 8379baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 838059d59ecfSMatthias Ringwald hci_run(); 8381ff00ed1cSMatthias Ringwald } 8382ff00ed1cSMatthias Ringwald 8383ff00ed1cSMatthias Ringwald /** 8384f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 8385f5875de5SMatthias Ringwald * @param duration in 1.28s units 8386f5875de5SMatthias Ringwald * @return 0 if ok 8387f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 8388f5875de5SMatthias Ringwald */ 8389f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 839099449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 8391f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8392a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 8393f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 8394f5875de5SMatthias Ringwald } 8395f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 839630199e24SMatthias Ringwald hci_stack->inquiry_max_period_length = 0; 839730199e24SMatthias Ringwald hci_stack->inquiry_min_period_length = 0; 839830199e24SMatthias Ringwald hci_run(); 839930199e24SMatthias Ringwald return 0; 840030199e24SMatthias Ringwald } 840130199e24SMatthias Ringwald 840230199e24SMatthias Ringwald uint8_t gap_inquiry_periodic_start(uint8_t duration, uint16_t max_period_length, uint16_t min_period_length){ 840330199e24SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 840430199e24SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 840530199e24SMatthias Ringwald if (duration < GAP_INQUIRY_DURATION_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 840630199e24SMatthias Ringwald if (duration > GAP_INQUIRY_DURATION_MAX) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 840730199e24SMatthias Ringwald if (max_period_length < GAP_INQUIRY_MAX_PERIODIC_LEN_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;; 840830199e24SMatthias Ringwald if (min_period_length < GAP_INQUIRY_MIN_PERIODIC_LEN_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;; 840930199e24SMatthias Ringwald 841030199e24SMatthias Ringwald hci_stack->inquiry_state = duration; 841130199e24SMatthias Ringwald hci_stack->inquiry_max_period_length = max_period_length; 841230199e24SMatthias Ringwald hci_stack->inquiry_min_period_length = min_period_length; 8413f5875de5SMatthias Ringwald hci_run(); 8414f5875de5SMatthias Ringwald return 0; 8415f5875de5SMatthias Ringwald } 8416f5875de5SMatthias Ringwald 8417f5875de5SMatthias Ringwald /** 8418f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 84196b65794dSMilanka Ringwald * @return 0 if ok 8420f5875de5SMatthias Ringwald */ 8421f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 8422a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 8423f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 8424f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 8425f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 8426f5875de5SMatthias Ringwald return 0; 8427f5875de5SMatthias Ringwald } 842830199e24SMatthias Ringwald switch (hci_stack->inquiry_state){ 842930199e24SMatthias Ringwald case GAP_INQUIRY_STATE_ACTIVE: 8430f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 8431f5875de5SMatthias Ringwald hci_run(); 843230199e24SMatthias Ringwald return ERROR_CODE_SUCCESS; 843330199e24SMatthias Ringwald case GAP_INQUIRY_STATE_PERIODIC: 843430199e24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_EXIT_PERIODIC; 843530199e24SMatthias Ringwald hci_run(); 843630199e24SMatthias Ringwald return ERROR_CODE_SUCCESS; 843730199e24SMatthias Ringwald default: 843830199e24SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 843930199e24SMatthias Ringwald } 8440f5875de5SMatthias Ringwald } 8441f5875de5SMatthias Ringwald 8442496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 8443496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 8444496bb884SMatthias Ringwald } 8445496bb884SMatthias Ringwald 84461dc973e7SMatthias Ringwald void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window){ 8447c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_interval = inquiry_scan_interval; 8448c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_window = inquiry_scan_window; 8449baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 8450c0c8a829SMatthias Ringwald hci_run(); 8451c0c8a829SMatthias Ringwald } 8452c0c8a829SMatthias Ringwald 8453b7f1ee76SMatthias Ringwald 8454b7f1ee76SMatthias Ringwald /** 8455b7f1ee76SMatthias Ringwald * @brief Remote Name Request 8456b7f1ee76SMatthias Ringwald * @param addr 8457b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 8458b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 8459b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 8460b7f1ee76SMatthias Ringwald */ 8461667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 8462b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 84636535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 8464b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 8465b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 8466b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 8467b7f1ee76SMatthias Ringwald hci_run(); 8468b7f1ee76SMatthias Ringwald return 0; 8469b7f1ee76SMatthias Ringwald } 8470b7f1ee76SMatthias Ringwald 8471667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 84720a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 84736535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 84740a51f88bSMatthias Ringwald hci_run(); 84750a51f88bSMatthias Ringwald return 0; 84760a51f88bSMatthias Ringwald } 84770a51f88bSMatthias Ringwald 84780a51f88bSMatthias Ringwald /** 8479aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 8480aad97216SMatthias Ringwald * @param addr 8481aad97216SMatthias Ringwald * @param pin_data 8482aad97216SMatthias Ringwald * @param pin_len 8483aad97216SMatthias Ringwald * @return 0 if ok 8484aad97216SMatthias Ringwald */ 8485667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 8486aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8487aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 8488aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 8489aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 8490aad97216SMatthias Ringwald } 8491aad97216SMatthias Ringwald 8492aad97216SMatthias Ringwald /** 84930a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 84940a51f88bSMatthias Ringwald * @param addr 84950a51f88bSMatthias Ringwald * @param pin 84960a51f88bSMatthias Ringwald * @return 0 if ok 84970a51f88bSMatthias Ringwald */ 8498667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 84999d78a7ecSMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, (uint8_t) strlen(pin)); 85000a51f88bSMatthias Ringwald } 85010a51f88bSMatthias Ringwald 85020a51f88bSMatthias Ringwald /** 85030a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 85040a51f88bSMatthias Ringwald * @param addr 85050a51f88bSMatthias Ringwald * @param pin 85060a51f88bSMatthias Ringwald * @return 0 if ok 85070a51f88bSMatthias Ringwald */ 85080a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 8509cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 85100a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 85110a51f88bSMatthias Ringwald } 85120a51f88bSMatthias Ringwald 85130a51f88bSMatthias Ringwald /** 85140a51f88bSMatthias Ringwald * @brief SSP Passkey Response 85150a51f88bSMatthias Ringwald * @param addr 85160a51f88bSMatthias Ringwald * @param passkey 85170a51f88bSMatthias Ringwald * @return 0 if ok 85180a51f88bSMatthias Ringwald */ 8519667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 8520cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8521d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 85220a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 85230a51f88bSMatthias Ringwald } 85240a51f88bSMatthias Ringwald 85250a51f88bSMatthias Ringwald /** 85260a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 85270a51f88bSMatthias Ringwald * @param addr 85280a51f88bSMatthias Ringwald * @param pin 85290a51f88bSMatthias Ringwald * @return 0 if ok 85300a51f88bSMatthias Ringwald */ 8531667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 8532cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 85330a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 85340a51f88bSMatthias Ringwald } 85350a51f88bSMatthias Ringwald 85360a51f88bSMatthias Ringwald /** 85370a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 85380a51f88bSMatthias Ringwald * @param addr 85390a51f88bSMatthias Ringwald * @param passkey 85400a51f88bSMatthias Ringwald * @return 0 if ok 85410a51f88bSMatthias Ringwald */ 8542667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(const bd_addr_t addr){ 8543cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 85440a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION); 85450a51f88bSMatthias Ringwald } 85460a51f88bSMatthias Ringwald 85470a51f88bSMatthias Ringwald /** 85480a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 85490a51f88bSMatthias Ringwald * @param addr 85500a51f88bSMatthias Ringwald * @param pin 85510a51f88bSMatthias Ringwald * @return 0 if ok 85520a51f88bSMatthias Ringwald */ 8553667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(const bd_addr_t addr){ 8554cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 85550a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE); 85560a51f88bSMatthias Ringwald } 85570a51f88bSMatthias Ringwald 8558308eeaffSMatthias Ringwald #if defined(ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY) || defined(ENABLE_EXPLICIT_LINK_KEY_REPLY) 855944565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 856044565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 856144565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 856244565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 856344565b0cSMatthias Ringwald hci_run(); 856444565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 856544565b0cSMatthias Ringwald } 8566308eeaffSMatthias Ringwald #endif 856744565b0cSMatthias Ringwald 8568308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 856944565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 85707ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 857144565b0cSMatthias Ringwald } 857244565b0cSMatthias Ringwald 857344565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 85747ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 857544565b0cSMatthias Ringwald } 857644565b0cSMatthias Ringwald #endif 857744565b0cSMatthias Ringwald 85781849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 85791849becdSMatthias Ringwald /** 85801849becdSMatthias Ringwald * @brief Report Remote OOB Data 85811849becdSMatthias Ringwald * @param bd_addr 85821849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 85831849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 85841849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 85851849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 85861849becdSMatthias Ringwald */ 85871849becdSMatthias 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){ 85881849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 85891849becdSMatthias Ringwald if (connection == NULL) { 85901849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 85911849becdSMatthias Ringwald } 85921849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 85931849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 8594204e8f1dSMatthias Ringwald 8595204e8f1dSMatthias Ringwald // ignore P-256 if not supported by us 8596204e8f1dSMatthias Ringwald if (hci_stack->secure_connections_active){ 85971849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 85981849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 8599204e8f1dSMatthias Ringwald } 8600204e8f1dSMatthias Ringwald 86011849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 86021849becdSMatthias Ringwald } 8603cf01e888SMatthias Ringwald /** 8604cf01e888SMatthias Ringwald * @brief Generate new OOB data 8605cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 8606cf01e888SMatthias Ringwald */ 8607cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 8608cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 8609cf01e888SMatthias Ringwald hci_run(); 8610cf01e888SMatthias Ringwald } 8611cf01e888SMatthias Ringwald 86121849becdSMatthias Ringwald #endif 86131849becdSMatthias Ringwald 8614308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_LINK_KEY_REPLY 8615308eeaffSMatthias Ringwald uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 8616308eeaffSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 8617308eeaffSMatthias Ringwald if (connection == NULL) { 8618308eeaffSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8619308eeaffSMatthias Ringwald } 8620308eeaffSMatthias Ringwald 8621308eeaffSMatthias Ringwald memcpy(connection->link_key, link_key, sizeof(link_key_t)); 8622308eeaffSMatthias Ringwald connection->link_key_type = type; 8623308eeaffSMatthias Ringwald 8624308eeaffSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 8625308eeaffSMatthias Ringwald } 8626308eeaffSMatthias Ringwald 8627308eeaffSMatthias Ringwald #endif // ENABLE_EXPLICIT_LINK_KEY_REPLY 8628f5875de5SMatthias Ringwald /** 8629f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 8630f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 8631f6858d14SMatthias Ringwald */ 8632b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 8633b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 8634f6858d14SMatthias Ringwald } 8635f6858d14SMatthias Ringwald 8636f6858d14SMatthias Ringwald /** 8637d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 8638d950d659SMatthias Ringwald */ 8639d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 8640d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 8641d950d659SMatthias Ringwald } 8642d950d659SMatthias Ringwald 8643d950d659SMatthias Ringwald /** 8644d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 8645d950d659SMatthias Ringwald * @return current voice setting 8646d950d659SMatthias Ringwald */ 86470cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 8648d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 8649d950d659SMatthias Ringwald } 8650d950d659SMatthias Ringwald 8651400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 8652400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 8653400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 8654400ff5abSMatthias Ringwald if (!transport_name) return 0; 8655400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 8656400ff5abSMatthias Ringwald } 8657400ff5abSMatthias Ringwald 8658b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 8659b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 8660b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 8661b3aad8daSMatthias Ringwald */ 866220dcdd22SMatthias Ringwald uint16_t hci_get_sco_packet_length(void){ 866320dcdd22SMatthias Ringwald uint16_t sco_packet_length = 0; 8664cf119f3bSMatthias Ringwald 86651e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 86666f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 86671431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 8668cf119f3bSMatthias Ringwald 8669400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 8670400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 8671cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 867252e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 867352e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 8674400ff5abSMatthias Ringwald } else { 8675400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 8676400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 8677d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 8678ad915196SMatthias Ringwald if (sco_packet_length > (hci_stack->sco_data_packet_length + 3)){ 8679d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 8680d966a453SMatthias Ringwald } 8681400ff5abSMatthias Ringwald } 8682a3069afeSMatthias Ringwald #endif 86835b7087c7SMatthias Ringwald 86845b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 86855b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 86861e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 86875b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 86885b7087c7SMatthias Ringwald #endif 8689a3069afeSMatthias Ringwald return sco_packet_length; 8690b3aad8daSMatthias Ringwald } 8691b3aad8daSMatthias Ringwald 8692c4c88f1bSJakob Krantz /** 8693c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 8694c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 8695c4c88f1bSJakob Krantz */ 8696c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 8697c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 8698c4c88f1bSJakob Krantz } 8699c4c88f1bSJakob Krantz 8700c4c88f1bSJakob Krantz #endif 8701ec111c8bSMatthias Ringwald 8702ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 8703ec111c8bSMatthias Ringwald return hci_stack->state; 8704ec111c8bSMatthias Ringwald } 8705ec111c8bSMatthias Ringwald 870607e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 87075e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 870807e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 870907e010b6SMilanka Ringwald } 871007e010b6SMilanka Ringwald #endif 8711ec111c8bSMatthias Ringwald 8712d950d659SMatthias Ringwald /** 8713d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 8714d23838ecSMatthias Ringwald */ 8715c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 8716d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 8717d23838ecSMatthias Ringwald } 8718d23838ecSMatthias Ringwald 871971de195eSMatthias Ringwald void hci_disconnect_all(void){ 8720665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8721665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 8722665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8723665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 872404a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 872504a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 872604a6ef8cSmatthias.ringwald } 8727d31fba26S[email protected] hci_run(); 872804a6ef8cSmatthias.ringwald } 872933373e40SMatthias Ringwald 873033373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 873133373e40SMatthias Ringwald return hci_stack->manufacturer; 873233373e40SMatthias Ringwald } 87339c6e867eSMatthias Ringwald 87343e329ddfSandryblack #ifdef ENABLE_BLE 87359c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 87369c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 87379c6e867eSMatthias Ringwald if (!hci_con) return NULL; 87389c6e867eSMatthias Ringwald return &hci_con->sm_connection; 87399c6e867eSMatthias Ringwald } 87409c6e867eSMatthias Ringwald 87419c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 87429c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 87436a79f6baSMatthias Ringwald #endif 87449c6e867eSMatthias Ringwald 87456d105c71SMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 8746c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8747c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 8748c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 87496a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 8750c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 8751c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 87529c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 87539c6e867eSMatthias Ringwald } 87546a79f6baSMatthias Ringwald #endif 87556a79f6baSMatthias Ringwald } else { 8756c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 87578daf94bcSMatthias Ringwald if ((hci_connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED)){ 8758c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 8759c5fb5ca4SMatthias Ringwald } 8760c5fb5ca4SMatthias Ringwald #endif 87616a79f6baSMatthias Ringwald } 8762c5fb5ca4SMatthias Ringwald return 0; 8763c5fb5ca4SMatthias Ringwald } 87649c6e867eSMatthias Ringwald 8765ff3f1026SMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 8766c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8767ff3f1026SMatthias Ringwald if (hci_connection == NULL) return false; 87685f3981bfSMatthias Ringwald 8769c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 87706a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 87715f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 87725f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8773c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 8774ff3f1026SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated != 0; 87756a79f6baSMatthias Ringwald #endif 877659a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 87775f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 8778f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8779ff3f1026SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 878059a1a47aSMatthias Ringwald #endif 87815f3981bfSMatthias Ringwald default: 8782ff3f1026SMatthias Ringwald return false; 87835f3981bfSMatthias Ringwald } 87849c6e867eSMatthias Ringwald } 87859c6e867eSMatthias Ringwald 8786f461b3dfSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 8787c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8788c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 87898b35e16aSMatthias Ringwald 8790c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 87916a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 87928b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 87938b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8794f461b3dfSMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return false; // unencrypted connection cannot be authenticated 8795f461b3dfSMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc != 0; 87966a79f6baSMatthias Ringwald #endif 879759a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 87988b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 8799f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8800f461b3dfSMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 880159a1a47aSMatthias Ringwald #endif 88028b35e16aSMatthias Ringwald default: 8803f461b3dfSMatthias Ringwald return false; 88048b35e16aSMatthias Ringwald } 8805f1dfbe18SMatthias Ringwald } 8806f1dfbe18SMatthias Ringwald 88071e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 88081e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 88091e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 88101e122704SMatthias Ringwald 881148f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 88121e122704SMatthias Ringwald link_key_t link_key; 88131e122704SMatthias Ringwald link_key_type_t link_key_type; 881448f33f37SMatthias Ringwald #endif 88151e122704SMatthias Ringwald switch (hci_connection->address_type){ 88166a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 88171e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 88181e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 88191e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 88206a79f6baSMatthias Ringwald #endif 88211e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 88221e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 88231e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 88241e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 88251e122704SMatthias Ringwald #endif 88261e122704SMatthias Ringwald default: 88271e122704SMatthias Ringwald return false; 88281e122704SMatthias Ringwald } 88291e122704SMatthias Ringwald } 88301e122704SMatthias Ringwald 88316a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 88329c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 88339c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 88349c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 88359c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 88369c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 88379c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 88389c6e867eSMatthias Ringwald } 88399c6e867eSMatthias Ringwald #endif 8840f8ee3071SMatthias Ringwald 8841f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 8842f8ee3071SMatthias 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){ 8843f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88447f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8845f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 8846f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 8847f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 8848f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 8849f8ee3071SMatthias Ringwald hci_run(); 8850f8ee3071SMatthias Ringwald return 0; 8851f8ee3071SMatthias Ringwald } 8852f8ee3071SMatthias Ringwald 8853f8ee3071SMatthias Ringwald /** 8854f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 8855f8ee3071SMatthias Ringwald * @param con_handle 8856f8ee3071SMatthias Ringwald @ @return 0 if ok 8857f8ee3071SMatthias Ringwald */ 8858f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 8859f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88607f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8861f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 8862f8ee3071SMatthias Ringwald hci_run(); 8863f8ee3071SMatthias Ringwald return 0; 8864f8ee3071SMatthias Ringwald } 8865bea424a5SMatthias Ringwald 8866140c0557SMatthias 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){ 8867140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88687f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8869140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 8870140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 8871140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 8872140c0557SMatthias Ringwald hci_run(); 8873dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 8874140c0557SMatthias Ringwald } 8875140c0557SMatthias Ringwald 8876278ff8a9SMatthias 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){ 8877278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88787f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8879278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 8880278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 8881278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 8882278ff8a9SMatthias Ringwald conn->qos_latency = latency; 8883278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 8884278ff8a9SMatthias Ringwald hci_run(); 8885278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8886278ff8a9SMatthias Ringwald } 8887278ff8a9SMatthias Ringwald 8888bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 8889bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 8890bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 8891baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 8892bea424a5SMatthias Ringwald hci_run(); 8893bea424a5SMatthias Ringwald } 8894bea424a5SMatthias Ringwald 8895bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 8896bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 8897baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_TYPE; 8898bea424a5SMatthias Ringwald hci_run(); 8899bea424a5SMatthias Ringwald } 8900bea424a5SMatthias Ringwald 890132a12730SMatthias Ringwald void gap_set_page_timeout(uint16_t page_timeout){ 890232a12730SMatthias Ringwald hci_stack->page_timeout = page_timeout; 8903baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_TIMEOUT; 890432a12730SMatthias Ringwald hci_run(); 890532a12730SMatthias Ringwald } 890632a12730SMatthias Ringwald 8907f8ee3071SMatthias Ringwald #endif 8908beceeddeSMatthias Ringwald 890921debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 891021debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 891121debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 891221debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 891321debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 891421debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 891502b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 891621debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 891702b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 891802b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 891902b02cffSMatthias Ringwald } 892002b02cffSMatthias Ringwald } 892102b02cffSMatthias Ringwald 892202b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 892302b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 892402b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 892502b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 892602b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 892702b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 892802b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 892902b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 893021debf25SMatthias Ringwald } 893121debf25SMatthias Ringwald } 8932cf38a091SMatthias Ringwald 8933cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 89341ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE) == false){ 8935cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 8936cf38a091SMatthias Ringwald } 8937cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 8938cf38a091SMatthias Ringwald // restart le resolving list update 8939cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 8940cf38a091SMatthias Ringwald } 8941cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 8942cf38a091SMatthias Ringwald } 894321debf25SMatthias Ringwald #endif 894421debf25SMatthias Ringwald 8945f40c73b4SMatthias Ringwald #ifdef ENABLE_BLE 8946f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 8947f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 8948f40c73b4SMatthias Ringwald 8949f40c73b4SMatthias Ringwald static uint8_t hci_periodic_advertiser_list_add(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8950f40c73b4SMatthias Ringwald // check if already in list 8951f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8952f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8953f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 8954f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t *entry = (periodic_advertiser_list_entry_t *) btstack_linked_list_iterator_next(&it); 8955f40c73b4SMatthias Ringwald if (entry->sid != advertising_sid) { 8956f40c73b4SMatthias Ringwald continue; 8957f40c73b4SMatthias Ringwald } 8958f40c73b4SMatthias Ringwald if (entry->address_type != address_type) { 8959f40c73b4SMatthias Ringwald continue; 8960f40c73b4SMatthias Ringwald } 8961f40c73b4SMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 8962f40c73b4SMatthias Ringwald continue; 8963f40c73b4SMatthias Ringwald } 8964f40c73b4SMatthias Ringwald // disallow if already scheduled to add 8965f40c73b4SMatthias Ringwald if ((entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER) != 0){ 8966f40c73b4SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 8967f40c73b4SMatthias Ringwald } 8968f40c73b4SMatthias Ringwald // still on controller, but scheduled to remove -> re-add 8969f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 8970f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8971f40c73b4SMatthias Ringwald } 8972f40c73b4SMatthias Ringwald // alloc and add to list 8973f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = btstack_memory_periodic_advertiser_list_entry_get(); 8974f40c73b4SMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 8975f40c73b4SMatthias Ringwald entry->sid = advertising_sid; 8976f40c73b4SMatthias Ringwald entry->address_type = address_type; 8977f40c73b4SMatthias Ringwald (void)memcpy(entry->address, address, 6); 8978f40c73b4SMatthias Ringwald entry->state = LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 8979f40c73b4SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t*) entry); 8980f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8981f40c73b4SMatthias Ringwald } 8982f40c73b4SMatthias Ringwald 8983f40c73b4SMatthias Ringwald static uint8_t hci_periodic_advertiser_list_remove(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8984f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8985f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8986f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8987f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&it); 8988f40c73b4SMatthias Ringwald if (entry->sid != advertising_sid) { 8989f40c73b4SMatthias Ringwald continue; 8990f40c73b4SMatthias Ringwald } 8991f40c73b4SMatthias Ringwald if (entry->address_type != address_type) { 8992f40c73b4SMatthias Ringwald continue; 8993f40c73b4SMatthias Ringwald } 8994f40c73b4SMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 8995f40c73b4SMatthias Ringwald continue; 8996f40c73b4SMatthias Ringwald } 8997f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER){ 8998f40c73b4SMatthias Ringwald // remove from controller if already present 8999f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 9000f40c73b4SMatthias Ringwald } else { 9001f40c73b4SMatthias Ringwald // directly remove entry from whitelist 9002f40c73b4SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9003f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 9004f40c73b4SMatthias Ringwald } 9005f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9006f40c73b4SMatthias Ringwald } 9007f40c73b4SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9008f40c73b4SMatthias Ringwald } 9009f40c73b4SMatthias Ringwald 9010f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_clear(void){ 9011f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 9012f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 9013f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9014f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&it); 9015f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER){ 9016f40c73b4SMatthias Ringwald // remove from controller if already present 9017f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 9018f40c73b4SMatthias Ringwald continue; 9019f40c73b4SMatthias Ringwald } 9020f40c73b4SMatthias Ringwald // directly remove entry from whitelist 9021f40c73b4SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9022f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 9023f40c73b4SMatthias Ringwald } 9024f40c73b4SMatthias Ringwald } 9025f40c73b4SMatthias Ringwald 9026f40c73b4SMatthias Ringwald // free all entries unconditionally 9027f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_free(void){ 9028f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t lit; 9029f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 9030f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 9031f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 9032f40c73b4SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t *) entry); 9033f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 9034f40c73b4SMatthias Ringwald } 9035f40c73b4SMatthias Ringwald } 9036f40c73b4SMatthias Ringwald 9037f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_clear(void){ 9038f40c73b4SMatthias Ringwald hci_periodic_advertiser_list_clear(); 9039f40c73b4SMatthias Ringwald hci_run(); 9040f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9041f40c73b4SMatthias Ringwald } 9042f40c73b4SMatthias Ringwald 9043f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_add(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 9044f40c73b4SMatthias Ringwald uint8_t status = hci_periodic_advertiser_list_add(address_type, address, advertising_sid); 9045f40c73b4SMatthias Ringwald if (status){ 9046f40c73b4SMatthias Ringwald return status; 9047f40c73b4SMatthias Ringwald } 9048f40c73b4SMatthias Ringwald hci_run(); 9049f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9050f40c73b4SMatthias Ringwald } 9051f40c73b4SMatthias Ringwald 9052f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_remove(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 9053f40c73b4SMatthias Ringwald uint8_t status = hci_periodic_advertiser_list_remove(address_type, address, advertising_sid); 9054f40c73b4SMatthias Ringwald if (status){ 9055f40c73b4SMatthias Ringwald return status; 9056f40c73b4SMatthias Ringwald } 9057f40c73b4SMatthias Ringwald hci_run(); 9058f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9059f40c73b4SMatthias Ringwald } 90604bafedd5SMatthias Ringwald 90614bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_create_sync(uint8_t options, uint8_t advertising_sid, bd_addr_type_t advertiser_address_type, 90624bafedd5SMatthias Ringwald bd_addr_t advertiser_address, uint16_t skip, uint16_t sync_timeout, uint8_t sync_cte_type){ 90634bafedd5SMatthias Ringwald // abort if already active 90644bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_request != LE_CONNECTING_IDLE) { 90654bafedd5SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 90664bafedd5SMatthias Ringwald } 90674bafedd5SMatthias Ringwald // store request 90684bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_request = ((options & 0) != 0) ? LE_CONNECTING_WHITELIST : LE_CONNECTING_DIRECT; 90694bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_options = options; 90704bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertising_sid = advertising_sid; 90714bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address_type = advertiser_address_type; 90724bafedd5SMatthias Ringwald memcpy(hci_stack->le_periodic_sync_advertiser_address, advertiser_address, 6); 90734bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_skip = skip; 90744bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_timeout = sync_timeout; 90754bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_cte_type = sync_cte_type; 90764bafedd5SMatthias Ringwald 90774bafedd5SMatthias Ringwald hci_run(); 90784bafedd5SMatthias Ringwald return ERROR_CODE_SUCCESS; 90794bafedd5SMatthias Ringwald } 90804bafedd5SMatthias Ringwald 90814bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_create_sync_cancel(void){ 90824bafedd5SMatthias Ringwald // abort if not requested 90834bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_request == LE_CONNECTING_IDLE) { 90844bafedd5SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 90854bafedd5SMatthias Ringwald } 90864bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_request = LE_CONNECTING_IDLE; 90874bafedd5SMatthias Ringwald hci_run(); 90884bafedd5SMatthias Ringwald return ERROR_CODE_SUCCESS; 90894bafedd5SMatthias Ringwald } 90904bafedd5SMatthias Ringwald 90914bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_terminate_sync(uint16_t sync_handle){ 909249a6c69cSMatthias Ringwald if (hci_stack->le_periodic_terminate_sync_handle != HCI_CON_HANDLE_INVALID){ 909349a6c69cSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 909449a6c69cSMatthias Ringwald } 909549a6c69cSMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = sync_handle; 909649a6c69cSMatthias Ringwald hci_run(); 909749a6c69cSMatthias Ringwald return ERROR_CODE_SUCCESS; 90984bafedd5SMatthias Ringwald } 90994bafedd5SMatthias Ringwald 9100f40c73b4SMatthias Ringwald #endif 9101f40c73b4SMatthias Ringwald #endif 91024ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 91038d72db10SMatthias Ringwald static uint8_t hci_iso_stream_create(hci_iso_type_t iso_type, hci_con_handle_t con_handle, uint8_t group_id) { 91044ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = btstack_memory_hci_iso_stream_get(); 91054ae8623eSMatthias Ringwald if (iso_stream == NULL){ 91064ae8623eSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 91074ae8623eSMatthias Ringwald } else { 91088d72db10SMatthias Ringwald iso_stream->iso_type = iso_type; 91094ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_REQUESTED; 911015a15be3SMatthias Ringwald iso_stream->con_handle = con_handle; 91118d72db10SMatthias Ringwald iso_stream->group_id = group_id; 91124ae8623eSMatthias Ringwald btstack_linked_list_add(&hci_stack->iso_streams, (btstack_linked_item_t*) iso_stream); 91134ae8623eSMatthias Ringwald return ERROR_CODE_SUCCESS; 91144ae8623eSMatthias Ringwald } 91154ae8623eSMatthias Ringwald } 91164ae8623eSMatthias Ringwald 911715a15be3SMatthias Ringwald static hci_iso_stream_t * hci_iso_stream_for_con_handle(hci_con_handle_t con_handle){ 91184ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 91194ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 91204ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91214ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 912215a15be3SMatthias Ringwald if (iso_stream->con_handle == con_handle ) { 91234ae8623eSMatthias Ringwald return iso_stream; 91244ae8623eSMatthias Ringwald } 91254ae8623eSMatthias Ringwald } 91264ae8623eSMatthias Ringwald return NULL; 91274ae8623eSMatthias Ringwald } 91284ae8623eSMatthias Ringwald 91294ae8623eSMatthias Ringwald static void hci_iso_stream_finalize(hci_iso_stream_t * iso_stream){ 91308d72db10SMatthias Ringwald log_info("hci_iso_stream_finalize con_handle 0x%04x, group_id 0x%02x", iso_stream->con_handle, iso_stream->group_id); 91314ae8623eSMatthias Ringwald btstack_linked_list_remove(&hci_stack->iso_streams, (btstack_linked_item_t*) iso_stream); 91324ae8623eSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 91334ae8623eSMatthias Ringwald } 91344ae8623eSMatthias Ringwald 9135778463d0SMatthias Ringwald static void hci_iso_stream_finalize_by_type_and_group_id(hci_iso_type_t iso_type, uint8_t group_id) { 9136778463d0SMatthias Ringwald btstack_linked_list_iterator_t it; 9137778463d0SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 9138778463d0SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9139778463d0SMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 9140778463d0SMatthias Ringwald if ((iso_stream->group_id == group_id) && 9141778463d0SMatthias Ringwald (iso_stream->iso_type == iso_type)){ 9142778463d0SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9143778463d0SMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 9144778463d0SMatthias Ringwald } 9145778463d0SMatthias Ringwald } 9146778463d0SMatthias Ringwald } 9147778463d0SMatthias Ringwald 91488d72db10SMatthias Ringwald static void hci_iso_stream_requested_finalize(uint8_t group_id) { 91494ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 91504ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 91514ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91524ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 9153a90016deSMatthias Ringwald if ((iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) && 91548d72db10SMatthias Ringwald (iso_stream->group_id == group_id)){ 91554ae8623eSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 91564ae8623eSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 91574ae8623eSMatthias Ringwald } 91584ae8623eSMatthias Ringwald } 91594ae8623eSMatthias Ringwald } 9160a90016deSMatthias Ringwald static void hci_iso_stream_requested_confirm(uint8_t big_handle){ 91614ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 91624ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 91634ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91644ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 91654ae8623eSMatthias Ringwald if ( iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) { 91664ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_W4_ESTABLISHED; 91674ae8623eSMatthias Ringwald } 91684ae8623eSMatthias Ringwald } 91694ae8623eSMatthias Ringwald } 9170d39e4f95SMatthias Ringwald 9171d39e4f95SMatthias Ringwald static bool hci_iso_sdu_complete(uint8_t * packet, uint16_t size){ 9172d39e4f95SMatthias Ringwald uint8_t sdu_ts_flag = (packet[1] >> 6) & 1; 9173d39e4f95SMatthias Ringwald uint16_t sdu_len_offset = 6 + (sdu_ts_flag * 4); 9174d39e4f95SMatthias Ringwald uint16_t sdu_len = little_endian_read_16(packet, sdu_len_offset) & 0x0fff; 9175d39e4f95SMatthias Ringwald return (sdu_len_offset + 2 + sdu_len) == size; 9176d39e4f95SMatthias Ringwald } 9177d39e4f95SMatthias Ringwald 9178d39e4f95SMatthias Ringwald static void hci_iso_packet_handler(uint8_t * packet, uint16_t size){ 9179d39e4f95SMatthias Ringwald if (hci_stack->iso_packet_handler == NULL) { 9180d39e4f95SMatthias Ringwald return; 9181d39e4f95SMatthias Ringwald } 9182d39e4f95SMatthias Ringwald if (size < 4) { 9183d39e4f95SMatthias Ringwald return; 9184d39e4f95SMatthias Ringwald } 9185d39e4f95SMatthias Ringwald 9186d39e4f95SMatthias Ringwald // parse header 9187d39e4f95SMatthias Ringwald uint16_t conn_handle_and_flags = little_endian_read_16(packet, 0); 9188d39e4f95SMatthias Ringwald uint16_t iso_data_len = little_endian_read_16(packet, 2); 9189d39e4f95SMatthias Ringwald hci_con_handle_t cis_handle = (hci_con_handle_t) (conn_handle_and_flags & 0xfff); 919015a15be3SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(cis_handle); 9191d39e4f95SMatthias Ringwald uint8_t pb_flag = (conn_handle_and_flags >> 12) & 3; 9192d39e4f95SMatthias Ringwald 9193d39e4f95SMatthias Ringwald // assert packet is complete 9194d39e4f95SMatthias Ringwald if ((iso_data_len + 4u) != size){ 9195d39e4f95SMatthias Ringwald return; 9196d39e4f95SMatthias Ringwald } 9197d39e4f95SMatthias Ringwald 9198d39e4f95SMatthias Ringwald if ((pb_flag & 0x01) == 0){ 9199d39e4f95SMatthias Ringwald if (pb_flag == 0x02){ 9200d39e4f95SMatthias Ringwald // The ISO_Data_Load field contains a header and a complete SDU. 9201d39e4f95SMatthias Ringwald if (hci_iso_sdu_complete(packet, size)) { 9202d39e4f95SMatthias Ringwald (hci_stack->iso_packet_handler)(HCI_ISO_DATA_PACKET, 0, packet, size); 9203d39e4f95SMatthias Ringwald } 9204d39e4f95SMatthias Ringwald } else { 9205d39e4f95SMatthias Ringwald // The ISO_Data_Load field contains a header and the first fragment of a fragmented SDU. 9206d39e4f95SMatthias Ringwald if (iso_stream == NULL){ 9207d39e4f95SMatthias Ringwald return; 9208d39e4f95SMatthias Ringwald } 9209d39e4f95SMatthias Ringwald if (size > HCI_ISO_PAYLOAD_SIZE){ 9210d39e4f95SMatthias Ringwald return; 9211d39e4f95SMatthias Ringwald } 9212d39e4f95SMatthias Ringwald memcpy(iso_stream->reassembly_buffer, packet, size); 9213d39e4f95SMatthias Ringwald // fix pb_flag 9214d39e4f95SMatthias Ringwald iso_stream->reassembly_buffer[1] = (iso_stream->reassembly_buffer[1] & 0xcf) | 0x20; 9215d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = size; 9216d39e4f95SMatthias Ringwald } 9217d39e4f95SMatthias Ringwald } else { 9218d39e4f95SMatthias Ringwald // iso_data_load contains continuation or last fragment of an SDU 9219d39e4f95SMatthias Ringwald uint8_t ts_flag = (conn_handle_and_flags >> 14) & 1; 9220d39e4f95SMatthias Ringwald if (ts_flag != 0){ 9221d39e4f95SMatthias Ringwald return; 9222d39e4f95SMatthias Ringwald } 9223d39e4f95SMatthias Ringwald // append fragment 9224d39e4f95SMatthias Ringwald if (iso_stream == NULL){ 9225d39e4f95SMatthias Ringwald return; 9226d39e4f95SMatthias Ringwald } 9227d39e4f95SMatthias Ringwald if (iso_stream->reassembly_pos == 0){ 9228d39e4f95SMatthias Ringwald return; 9229d39e4f95SMatthias Ringwald } 9230d39e4f95SMatthias Ringwald if ((iso_stream->reassembly_pos + iso_data_len) > size){ 9231d39e4f95SMatthias Ringwald // reset reassembly buffer 9232d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = 0; 9233d39e4f95SMatthias Ringwald return; 9234d39e4f95SMatthias Ringwald } 9235d39e4f95SMatthias Ringwald memcpy(&iso_stream->reassembly_buffer[iso_stream->reassembly_pos], &packet[4], iso_data_len); 9236d39e4f95SMatthias Ringwald iso_stream->reassembly_pos += iso_data_len; 9237d39e4f95SMatthias Ringwald 9238d39e4f95SMatthias Ringwald // deliver if last fragment and SDU complete 9239d39e4f95SMatthias Ringwald if (pb_flag == 0x03){ 9240d39e4f95SMatthias Ringwald if (hci_iso_sdu_complete(iso_stream->reassembly_buffer, iso_stream->reassembly_pos)){ 9241d39e4f95SMatthias Ringwald (hci_stack->iso_packet_handler)(HCI_ISO_DATA_PACKET, 0, iso_stream->reassembly_buffer, iso_stream->reassembly_pos); 9242d39e4f95SMatthias Ringwald } 9243d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = 0; 9244d39e4f95SMatthias Ringwald } 9245d39e4f95SMatthias Ringwald } 9246d39e4f95SMatthias Ringwald } 9247d39e4f95SMatthias Ringwald 92487aa35f6aSMatthias Ringwald static void hci_emit_big_created(const le_audio_big_t * big, uint8_t status){ 92497aa35f6aSMatthias Ringwald uint8_t event [6 + (MAX_NR_BIS * 2)]; 92507aa35f6aSMatthias Ringwald uint16_t pos = 0; 92517aa35f6aSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 92527aa35f6aSMatthias Ringwald event[pos++] = 4 + (2 * big->num_bis); 92537aa35f6aSMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_CREATED; 92547aa35f6aSMatthias Ringwald event[pos++] = status; 92557aa35f6aSMatthias Ringwald event[pos++] = big->big_handle; 92567aa35f6aSMatthias Ringwald event[pos++] = big->num_bis; 92577aa35f6aSMatthias Ringwald uint8_t i; 92587aa35f6aSMatthias Ringwald for (i=0;i<big->num_bis;i++){ 92597aa35f6aSMatthias Ringwald little_endian_store_16(event, pos, big->bis_con_handles[i]); 92607aa35f6aSMatthias Ringwald pos += 2; 92617aa35f6aSMatthias Ringwald } 92627aa35f6aSMatthias Ringwald hci_emit_event(event, pos, 0); 92637aa35f6aSMatthias Ringwald } 92647aa35f6aSMatthias Ringwald 9265d3a89d91SMatthias Ringwald static void hci_emit_cig_created(const le_audio_cig_t * cig, uint8_t status){ 9266d3a89d91SMatthias Ringwald uint8_t event [6 + (MAX_NR_CIS * 2)]; 9267d3a89d91SMatthias Ringwald uint16_t pos = 0; 9268d3a89d91SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 9269d3a89d91SMatthias Ringwald event[pos++] = 4 + (2 * cig->num_cis); 9270d3a89d91SMatthias Ringwald event[pos++] = GAP_SUBEVENT_CIG_CREATED; 9271d3a89d91SMatthias Ringwald event[pos++] = status; 9272d3a89d91SMatthias Ringwald event[pos++] = cig->cig_id; 9273d3a89d91SMatthias Ringwald event[pos++] = cig->num_cis; 9274d3a89d91SMatthias Ringwald uint8_t i; 9275d3a89d91SMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 9276d3a89d91SMatthias Ringwald little_endian_store_16(event, pos, cig->cis_con_handles[i]); 9277d3a89d91SMatthias Ringwald pos += 2; 9278d3a89d91SMatthias Ringwald } 9279d3a89d91SMatthias Ringwald hci_emit_event(event, pos, 0); 9280d3a89d91SMatthias Ringwald } 9281f06bb90fSMatthias Ringwald 92823abcd16cSMatthias Ringwald static void 92833abcd16cSMatthias Ringwald hci_emit_cis_created(uint8_t cig_id, hci_con_handle_t cis_con_handle, uint8_t status) { 9284f06bb90fSMatthias Ringwald uint8_t event [7]; 9285f06bb90fSMatthias Ringwald uint16_t pos = 0; 9286f06bb90fSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 9287f06bb90fSMatthias Ringwald event[pos++] = 5; 9288f06bb90fSMatthias Ringwald event[pos++] = GAP_SUBEVENT_CIS_CREATED; 9289f06bb90fSMatthias Ringwald event[pos++] = status; 92903abcd16cSMatthias Ringwald event[pos++] = cig_id; 92913abcd16cSMatthias Ringwald little_endian_store_16(event, pos, cis_con_handle); 9292f06bb90fSMatthias Ringwald pos += 2; 9293f06bb90fSMatthias Ringwald hci_emit_event(event, pos, 0); 9294f06bb90fSMatthias Ringwald } 9295f06bb90fSMatthias Ringwald 92967aa35f6aSMatthias Ringwald static void hci_emit_big_terminated(const le_audio_big_t * big){ 92977aa35f6aSMatthias Ringwald uint8_t event [4]; 92987aa35f6aSMatthias Ringwald uint16_t pos = 0; 92997aa35f6aSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 93007aa35f6aSMatthias Ringwald event[pos++] = 2; 93017aa35f6aSMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_TERMINATED; 93027aa35f6aSMatthias Ringwald event[pos++] = big->big_handle; 93037aa35f6aSMatthias Ringwald hci_emit_event(event, pos, 0); 93047aa35f6aSMatthias Ringwald } 93057aa35f6aSMatthias Ringwald 93062a6c0f82SMatthias Ringwald static void hci_emit_big_sync_created(const le_audio_big_sync_t * big_sync, uint8_t status){ 93072a6c0f82SMatthias Ringwald uint8_t event [6 + (MAX_NR_BIS * 2)]; 93082a6c0f82SMatthias Ringwald uint16_t pos = 0; 93092a6c0f82SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 93102a6c0f82SMatthias Ringwald event[pos++] = 4; 93112a6c0f82SMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_SYNC_CREATED; 93122a6c0f82SMatthias Ringwald event[pos++] = status; 93132a6c0f82SMatthias Ringwald event[pos++] = big_sync->big_handle; 93142a6c0f82SMatthias Ringwald event[pos++] = big_sync->num_bis; 93152a6c0f82SMatthias Ringwald uint8_t i; 93162a6c0f82SMatthias Ringwald for (i=0;i<big_sync->num_bis;i++){ 93172a6c0f82SMatthias Ringwald little_endian_store_16(event, pos, big_sync->bis_con_handles[i]); 93182a6c0f82SMatthias Ringwald pos += 2; 93192a6c0f82SMatthias Ringwald } 93202a6c0f82SMatthias Ringwald hci_emit_event(event, pos, 0); 93212a6c0f82SMatthias Ringwald } 93222a6c0f82SMatthias Ringwald 9323d04a9afcSMatthias Ringwald static void hci_emit_big_sync_stopped(uint8_t big_handle){ 93242a6c0f82SMatthias Ringwald uint8_t event [4]; 93252a6c0f82SMatthias Ringwald uint16_t pos = 0; 93262a6c0f82SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 93272a6c0f82SMatthias Ringwald event[pos++] = 2; 93282a6c0f82SMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_SYNC_STOPPED; 9329d04a9afcSMatthias Ringwald event[pos++] = big_handle; 93302a6c0f82SMatthias Ringwald hci_emit_event(event, pos, 0); 93312a6c0f82SMatthias Ringwald } 93322a6c0f82SMatthias Ringwald 93335ade6657SMatthias Ringwald static void hci_emit_bis_can_send_now(const le_audio_big_t *big, uint8_t bis_index) { 93345ade6657SMatthias Ringwald uint8_t event[6]; 93355ade6657SMatthias Ringwald uint16_t pos = 0; 93365ade6657SMatthias Ringwald event[pos++] = HCI_EVENT_BIS_CAN_SEND_NOW; 93375ade6657SMatthias Ringwald event[pos++] = sizeof(event) - 2; 93385ade6657SMatthias Ringwald event[pos++] = big->big_handle; 93395ade6657SMatthias Ringwald event[pos++] = bis_index; 93405ade6657SMatthias Ringwald little_endian_store_16(event, pos, big->bis_con_handles[bis_index]); 93415ade6657SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 93425ade6657SMatthias Ringwald } 93435ade6657SMatthias Ringwald 93447aa35f6aSMatthias Ringwald static le_audio_big_t * hci_big_for_handle(uint8_t big_handle){ 93457aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 93467aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 93477aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 93487aa35f6aSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 93497aa35f6aSMatthias Ringwald if ( big->big_handle == big_handle ) { 93507aa35f6aSMatthias Ringwald return big; 93517aa35f6aSMatthias Ringwald } 93527aa35f6aSMatthias Ringwald } 93537aa35f6aSMatthias Ringwald return NULL; 93547aa35f6aSMatthias Ringwald } 93557aa35f6aSMatthias Ringwald 93562a6c0f82SMatthias Ringwald static le_audio_big_sync_t * hci_big_sync_for_handle(uint8_t big_handle){ 93572a6c0f82SMatthias Ringwald btstack_linked_list_iterator_t it; 93582a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 93592a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 93602a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 93612a6c0f82SMatthias Ringwald if ( big_sync->big_handle == big_handle ) { 93622a6c0f82SMatthias Ringwald return big_sync; 93632a6c0f82SMatthias Ringwald } 93642a6c0f82SMatthias Ringwald } 93652a6c0f82SMatthias Ringwald return NULL; 93662a6c0f82SMatthias Ringwald } 93672a6c0f82SMatthias Ringwald 936801d4f05fSMatthias Ringwald void hci_set_num_iso_packets_to_queue(uint8_t num_packets){ 936901d4f05fSMatthias Ringwald hci_stack->iso_packets_to_queue = num_packets; 937001d4f05fSMatthias Ringwald } 937101d4f05fSMatthias Ringwald 9372d3a89d91SMatthias Ringwald static le_audio_cig_t * hci_cig_for_id(uint8_t cig_id){ 9373d3a89d91SMatthias Ringwald btstack_linked_list_iterator_t it; 9374d3a89d91SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_cigs); 9375d3a89d91SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9376d3a89d91SMatthias Ringwald le_audio_cig_t * cig = (le_audio_cig_t *) btstack_linked_list_iterator_next(&it); 9377d3a89d91SMatthias Ringwald if ( cig->cig_id == cig_id ) { 9378d3a89d91SMatthias Ringwald return cig; 9379d3a89d91SMatthias Ringwald } 9380d3a89d91SMatthias Ringwald } 9381d3a89d91SMatthias Ringwald return NULL; 9382d3a89d91SMatthias Ringwald } 9383d3a89d91SMatthias Ringwald 93845ade6657SMatthias Ringwald static void hci_iso_notify_can_send_now(void){ 93855ade6657SMatthias Ringwald btstack_linked_list_iterator_t it; 93865ade6657SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 93875ade6657SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 93885ade6657SMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 9389aacb1081SMatthias Ringwald // track number completed packet timestamps 9390aacb1081SMatthias Ringwald if (big->num_completed_timestamp_current_valid){ 9391aacb1081SMatthias Ringwald big->num_completed_timestamp_current_valid = false; 9392aacb1081SMatthias Ringwald if (big->num_completed_timestamp_previous_valid){ 9393aacb1081SMatthias Ringwald // detect delayed sending of all BIS: tolerate up to 50% delayed event handling 9394aacb1081SMatthias Ringwald uint32_t iso_interval_missed_threshold_ms = big->params->sdu_interval_us * 3 / 2000; 9395aacb1081SMatthias Ringwald int32_t num_completed_timestamp_delta_ms = btstack_time_delta(big->num_completed_timestamp_current_ms, 9396aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_ms); 9397aacb1081SMatthias Ringwald if (num_completed_timestamp_delta_ms > iso_interval_missed_threshold_ms){ 9398aacb1081SMatthias Ringwald // to catch up, skip packet on all BIS 9399aacb1081SMatthias Ringwald uint8_t i; 9400aacb1081SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 9401aacb1081SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 9402aacb1081SMatthias Ringwald if (iso_stream){ 9403aacb1081SMatthias Ringwald iso_stream->num_packets_to_skip++; 9404aacb1081SMatthias Ringwald } 9405aacb1081SMatthias Ringwald } 9406aacb1081SMatthias Ringwald } 9407aacb1081SMatthias Ringwald } 9408aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_valid = true; 9409aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_ms = big->num_completed_timestamp_current_ms; 9410aacb1081SMatthias Ringwald } 9411aacb1081SMatthias Ringwald 94125ade6657SMatthias Ringwald if (big->can_send_now_requested){ 9413d4e5d4faSMatthias Ringwald // check if no outgoing iso packets pending and no can send now have to be emitted 94145ade6657SMatthias Ringwald uint8_t i; 94155ade6657SMatthias Ringwald bool can_send = true; 9416aacb1081SMatthias Ringwald uint8_t num_iso_queued_minimum = 0; 94175ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 94185ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 9419aacb1081SMatthias Ringwald if (iso_stream == NULL) continue; 9420aacb1081SMatthias Ringwald // handle case where individual ISO packet was sent too late: 9421aacb1081SMatthias Ringwald // for each additionally queued packet, a new one needs to get skipped 9422aacb1081SMatthias Ringwald if (i==0){ 9423aacb1081SMatthias Ringwald num_iso_queued_minimum = iso_stream->num_packets_sent; 9424aacb1081SMatthias Ringwald } else if (iso_stream->num_packets_sent > num_iso_queued_minimum){ 9425aacb1081SMatthias Ringwald uint8_t num_packets_to_skip = iso_stream->num_packets_sent - num_iso_queued_minimum; 9426aacb1081SMatthias Ringwald iso_stream->num_packets_to_skip += num_packets_to_skip; 9427aacb1081SMatthias Ringwald iso_stream->num_packets_sent -= num_packets_to_skip; 9428aacb1081SMatthias Ringwald } 9429aacb1081SMatthias Ringwald // check if we can send now 9430aacb1081SMatthias Ringwald if ((iso_stream->num_packets_sent >= hci_stack->iso_packets_to_queue) || (iso_stream->emit_ready_to_send)){ 94315ade6657SMatthias Ringwald can_send = false; 94325ade6657SMatthias Ringwald break; 94335ade6657SMatthias Ringwald } 94345ade6657SMatthias Ringwald } 94355ade6657SMatthias Ringwald if (can_send){ 94365ade6657SMatthias Ringwald // propagate can send now to individual streams 94375ade6657SMatthias Ringwald big->can_send_now_requested = false; 94385ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 94395ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 94405ade6657SMatthias Ringwald iso_stream->emit_ready_to_send = true; 94415ade6657SMatthias Ringwald } 94425ade6657SMatthias Ringwald } 94435ade6657SMatthias Ringwald } 94445ade6657SMatthias Ringwald } 94455ade6657SMatthias Ringwald 94465ade6657SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return; 94475ade6657SMatthias Ringwald 94485ade6657SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 94495ade6657SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 94505ade6657SMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 94515ade6657SMatthias Ringwald // report bis ready 94525ade6657SMatthias Ringwald uint8_t i; 94535ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 94545ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 94555ade6657SMatthias Ringwald if ((iso_stream != NULL) && iso_stream->emit_ready_to_send){ 94565ade6657SMatthias Ringwald iso_stream->emit_ready_to_send = false; 94575ade6657SMatthias Ringwald hci_emit_bis_can_send_now(big, i); 94585ade6657SMatthias Ringwald break; 94595ade6657SMatthias Ringwald } 94605ade6657SMatthias Ringwald } 94615ade6657SMatthias Ringwald } 94625ade6657SMatthias Ringwald } 94635ade6657SMatthias Ringwald 94647aa35f6aSMatthias Ringwald uint8_t gap_big_create(le_audio_big_t * storage, le_audio_big_params_t * big_params){ 94657aa35f6aSMatthias Ringwald if (hci_big_for_handle(big_params->big_handle) != NULL){ 94667aa35f6aSMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 94677aa35f6aSMatthias Ringwald } 94687aa35f6aSMatthias Ringwald if (big_params->num_bis == 0){ 94697aa35f6aSMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 94707aa35f6aSMatthias Ringwald } 94717aa35f6aSMatthias Ringwald if (big_params->num_bis > MAX_NR_BIS){ 94727aa35f6aSMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 94737aa35f6aSMatthias Ringwald } 94747aa35f6aSMatthias Ringwald 9475fb16346aSMatthias Ringwald // reserve ISO Streams 9476fb16346aSMatthias Ringwald uint8_t i; 9477fb16346aSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 9478fb16346aSMatthias Ringwald for (i=0;i<big_params->num_bis;i++){ 94798d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, HCI_CON_HANDLE_INVALID, big_params->big_handle); 9480fb16346aSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 9481fb16346aSMatthias Ringwald break; 9482fb16346aSMatthias Ringwald } 9483fb16346aSMatthias Ringwald } 9484fb16346aSMatthias Ringwald 9485fb16346aSMatthias Ringwald // free structs on error 9486fb16346aSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 9487778463d0SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_BIS, big_params->big_handle); 9488fb16346aSMatthias Ringwald return status; 9489fb16346aSMatthias Ringwald } 9490fb16346aSMatthias Ringwald 94917aa35f6aSMatthias Ringwald le_audio_big_t * big = storage; 94927aa35f6aSMatthias Ringwald big->big_handle = big_params->big_handle; 94937aa35f6aSMatthias Ringwald big->params = big_params; 94947aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_CREATE; 94957aa35f6aSMatthias Ringwald big->num_bis = big_params->num_bis; 94967aa35f6aSMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 94977aa35f6aSMatthias Ringwald 94987aa35f6aSMatthias Ringwald hci_run(); 94997aa35f6aSMatthias Ringwald 95007aa35f6aSMatthias Ringwald return ERROR_CODE_SUCCESS; 95017aa35f6aSMatthias Ringwald } 95027aa35f6aSMatthias Ringwald 95032a6c0f82SMatthias Ringwald uint8_t gap_big_sync_create(le_audio_big_sync_t * storage, le_audio_big_sync_params_t * big_sync_params){ 95042a6c0f82SMatthias Ringwald if (hci_big_sync_for_handle(big_sync_params->big_handle) != NULL){ 95052a6c0f82SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 95062a6c0f82SMatthias Ringwald } 95072a6c0f82SMatthias Ringwald if (big_sync_params->num_bis == 0){ 95082a6c0f82SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 95092a6c0f82SMatthias Ringwald } 95102a6c0f82SMatthias Ringwald if (big_sync_params->num_bis > MAX_NR_BIS){ 95112a6c0f82SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 95122a6c0f82SMatthias Ringwald } 95132a6c0f82SMatthias Ringwald 95142a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = storage; 95152a6c0f82SMatthias Ringwald big_sync->big_handle = big_sync_params->big_handle; 95162a6c0f82SMatthias Ringwald big_sync->params = big_sync_params; 95172a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_CREATE; 95182a6c0f82SMatthias Ringwald big_sync->num_bis = big_sync_params->num_bis; 95192a6c0f82SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 95202a6c0f82SMatthias Ringwald 95212a6c0f82SMatthias Ringwald hci_run(); 95222a6c0f82SMatthias Ringwald 95232a6c0f82SMatthias Ringwald return ERROR_CODE_SUCCESS; 95242a6c0f82SMatthias Ringwald } 95252a6c0f82SMatthias Ringwald 95267aa35f6aSMatthias Ringwald uint8_t gap_big_terminate(uint8_t big_handle){ 95277aa35f6aSMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(big_handle); 95287aa35f6aSMatthias Ringwald if (big == NULL){ 95297aa35f6aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 95307aa35f6aSMatthias Ringwald } 95317aa35f6aSMatthias Ringwald switch (big->state){ 95327aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 95337aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 95342a6c0f82SMatthias Ringwald hci_emit_big_terminated(big); 95352a6c0f82SMatthias Ringwald break; 95362a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH: 95372a6c0f82SMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH_THEN_TERMINATE; 95387aa35f6aSMatthias Ringwald break; 95397aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_ESTABLISHED: 95407aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 95417aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_ACTIVE: 95427aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_TERMINATE; 95437aa35f6aSMatthias Ringwald hci_run(); 95447aa35f6aSMatthias Ringwald break; 95452a6c0f82SMatthias Ringwald default: 95462a6c0f82SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 95472a6c0f82SMatthias Ringwald } 95482a6c0f82SMatthias Ringwald return ERROR_CODE_SUCCESS; 95492a6c0f82SMatthias Ringwald } 95502a6c0f82SMatthias Ringwald 95512a6c0f82SMatthias Ringwald uint8_t gap_big_sync_terminate(uint8_t big_handle){ 95522a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = hci_big_sync_for_handle(big_handle); 95532a6c0f82SMatthias Ringwald if (big_sync == NULL){ 95542a6c0f82SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 95552a6c0f82SMatthias Ringwald } 95562a6c0f82SMatthias Ringwald switch (big_sync->state){ 95572a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 95582a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 9559d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 95602a6c0f82SMatthias Ringwald break; 95617aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH: 95622a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH_THEN_TERMINATE; 95632a6c0f82SMatthias Ringwald break; 95642a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_ESTABLISHED: 95652a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 95662a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_ACTIVE: 95672a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_TERMINATE; 95682a6c0f82SMatthias Ringwald hci_run(); 95697aa35f6aSMatthias Ringwald break; 95707aa35f6aSMatthias Ringwald default: 95717aa35f6aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 95727aa35f6aSMatthias Ringwald } 95737aa35f6aSMatthias Ringwald return ERROR_CODE_SUCCESS; 95747aa35f6aSMatthias Ringwald } 9575d39e4f95SMatthias Ringwald 95765ade6657SMatthias Ringwald uint8_t hci_request_bis_can_send_now_events(uint8_t big_handle){ 95775ade6657SMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(big_handle); 95785ade6657SMatthias Ringwald if (big == NULL){ 95795ade6657SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 95805ade6657SMatthias Ringwald } 95815ade6657SMatthias Ringwald if (big->state != LE_AUDIO_BIG_STATE_ACTIVE){ 95825ade6657SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 95835ade6657SMatthias Ringwald } 95845ade6657SMatthias Ringwald big->can_send_now_requested = true; 95855ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 95865ade6657SMatthias Ringwald return ERROR_CODE_SUCCESS; 95875ade6657SMatthias Ringwald } 9588d3a89d91SMatthias Ringwald 9589d3a89d91SMatthias Ringwald uint8_t gap_cig_create(le_audio_cig_t * storage, le_audio_cig_params_t * cig_params){ 9590d3a89d91SMatthias Ringwald if (hci_cig_for_id(cig_params->cig_id) != NULL){ 9591d3a89d91SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 9592d3a89d91SMatthias Ringwald } 9593d3a89d91SMatthias Ringwald if (cig_params->num_cis == 0){ 9594d3a89d91SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 9595d3a89d91SMatthias Ringwald } 9596d3a89d91SMatthias Ringwald if (cig_params->num_cis > MAX_NR_BIS){ 9597d3a89d91SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 9598d3a89d91SMatthias Ringwald } 9599d3a89d91SMatthias Ringwald 9600d3a89d91SMatthias Ringwald // reserve ISO Streams 9601d3a89d91SMatthias Ringwald uint8_t i; 9602d3a89d91SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 9603d3a89d91SMatthias Ringwald for (i=0;i<cig_params->num_cis;i++){ 9604d3a89d91SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_CIS, HCI_CON_HANDLE_INVALID, cig_params->cig_id); 9605d3a89d91SMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 9606d3a89d91SMatthias Ringwald break; 9607d3a89d91SMatthias Ringwald } 9608d3a89d91SMatthias Ringwald } 9609d3a89d91SMatthias Ringwald 9610d3a89d91SMatthias Ringwald // free structs on error 9611d3a89d91SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 9612d3a89d91SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_CIS, cig_params->cig_id); 9613d3a89d91SMatthias Ringwald return status; 9614d3a89d91SMatthias Ringwald } 9615d3a89d91SMatthias Ringwald 9616d3a89d91SMatthias Ringwald le_audio_cig_t * cig = storage; 9617d3a89d91SMatthias Ringwald cig->cig_id = cig_params->cig_id; 9618d3a89d91SMatthias Ringwald cig->num_cis = cig_params->num_cis; 9619d3a89d91SMatthias Ringwald cig->params = cig_params; 9620d3a89d91SMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_CREATE; 9621444e371eSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 9622444e371eSMatthias Ringwald cig->cis_con_handles[i] = HCI_CON_HANDLE_INVALID; 9623444e371eSMatthias Ringwald cig->acl_con_handles[i] = HCI_CON_HANDLE_INVALID; 9624444e371eSMatthias Ringwald } 9625d3a89d91SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_cigs, (btstack_linked_item_t *) cig); 9626d3a89d91SMatthias Ringwald 9627d3a89d91SMatthias Ringwald hci_run(); 9628d3a89d91SMatthias Ringwald 9629d3a89d91SMatthias Ringwald return ERROR_CODE_SUCCESS; 9630d3a89d91SMatthias Ringwald } 9631d3a89d91SMatthias Ringwald 9632444e371eSMatthias Ringwald uint8_t gap_cis_create(uint8_t cig_handle, hci_con_handle_t cis_con_handles [], hci_con_handle_t acl_con_handles []){ 9633444e371eSMatthias Ringwald le_audio_cig_t * cig = hci_cig_for_id(cig_handle); 9634444e371eSMatthias Ringwald if (cig == NULL){ 9635444e371eSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9636444e371eSMatthias Ringwald } 9637444e371eSMatthias Ringwald 9638444e371eSMatthias Ringwald if (cig->state != LE_AUDIO_CIG_STATE_W4_CIS_REQUEST){ 9639444e371eSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 9640444e371eSMatthias Ringwald } 9641444e371eSMatthias Ringwald 9642444e371eSMatthias Ringwald // store ACL Connection Handles 9643444e371eSMatthias Ringwald uint8_t i; 9644444e371eSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 9645444e371eSMatthias Ringwald // check that all con handles exit 9646444e371eSMatthias Ringwald hci_con_handle_t cis_handle = cis_con_handles[i]; 9647444e371eSMatthias Ringwald uint8_t j; 9648444e371eSMatthias Ringwald bool found = false; 9649444e371eSMatthias Ringwald for (j=0;j<cig->num_cis;j++){ 9650444e371eSMatthias Ringwald if (cig->cis_con_handles[j] == cis_handle){ 9651444e371eSMatthias Ringwald cig->acl_con_handles[j] = acl_con_handles[j]; 9652444e371eSMatthias Ringwald found = true; 9653444e371eSMatthias Ringwald break; 9654444e371eSMatthias Ringwald } 9655444e371eSMatthias Ringwald } 9656444e371eSMatthias Ringwald if (!found){ 9657444e371eSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9658444e371eSMatthias Ringwald } 9659444e371eSMatthias Ringwald } 9660444e371eSMatthias Ringwald 9661444e371eSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_CREATE_CIS; 9662444e371eSMatthias Ringwald hci_run(); 9663444e371eSMatthias Ringwald 9664444e371eSMatthias Ringwald return ERROR_CODE_SUCCESS; 9665444e371eSMatthias Ringwald } 9666444e371eSMatthias Ringwald 9667f40c73b4SMatthias Ringwald #endif 96684ae8623eSMatthias Ringwald #endif /* ENABLE_BLE */ 9669f40c73b4SMatthias Ringwald 9670eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 967118976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 967218976c74SMatthias Ringwald hci_connection_t * conn; 967318976c74SMatthias Ringwald 967418976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 967518976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 967618976c74SMatthias Ringwald 96777d33cb26SMilanka Ringwald // setup Controller info 96787d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 96797d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 96807d33cb26SMilanka Ringwald 968118976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 968218976c74SMatthias Ringwald addr[5] = 0x01; 968318976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 968418976c74SMatthias Ringwald conn->con_handle = addr[5]; 968518976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 968618976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 96877d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 968818976c74SMatthias Ringwald 968918976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 969018976c74SMatthias Ringwald addr[5] = 0x02; 969118976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 969218976c74SMatthias Ringwald conn->con_handle = addr[5]; 969318976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 969418976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 96957d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 969618976c74SMatthias Ringwald 969718976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 969818976c74SMatthias Ringwald addr[5] = 0x03; 969918976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 970018976c74SMatthias Ringwald conn->con_handle = addr[5]; 970118976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 970218976c74SMatthias Ringwald conn->state = OPEN; 97037d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 970418976c74SMatthias Ringwald 970518976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 970618976c74SMatthias Ringwald addr[5] = 0x04; 970718976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 970818976c74SMatthias Ringwald conn->con_handle = addr[5]; 970918976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 971018976c74SMatthias Ringwald conn->state = OPEN; 97117d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 971218976c74SMatthias Ringwald 971318976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 971418976c74SMatthias Ringwald addr[5] = 0x05; 971518976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 971618976c74SMatthias Ringwald conn->con_handle = addr[5]; 971718976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 971818976c74SMatthias Ringwald conn->state = OPEN; 97197d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 97208046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 972118976c74SMatthias Ringwald } 972218976c74SMatthias Ringwald 9723eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 9724eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 9725eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 9726eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9727eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 9728eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9729eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 9730eddac615SMatthias Ringwald } 9731eddac615SMatthias Ringwald } 97321470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 9733b6ffb67fSMatthias Ringwald hci_stack->le_scanning_param_update = false; 97341470db0cSMatthias Ringwald hci_init_done(); 97351470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 97361470db0cSMatthias Ringwald } 9737eddac615SMatthias Ringwald #endif 9738