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 } 2858*d75a6bbfSMatthias Ringwald hci_stack->iso_active_operation_group_id = HCI_ISO_GROUP_ID_INVALID; 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){ 2863*d75a6bbfSMatthias Ringwald hci_iso_stream_requested_finalize(HCI_ISO_GROUP_ID_INVALID); 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){ 2869*d75a6bbfSMatthias Ringwald hci_iso_stream_requested_finalize(HCI_ISO_GROUP_ID_INVALID); 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 } 2915f9306a1aSMatthias Ringwald // Lookup CIS via active group operation 2916f9306a1aSMatthias Ringwald if (hci_stack->iso_active_operation_type == HCI_ISO_TYPE_CIS){ 2917f9306a1aSMatthias Ringwald cig = hci_cig_for_id(hci_stack->iso_active_operation_group_id); 2918*d75a6bbfSMatthias Ringwald hci_stack->iso_active_operation_group_id = HCI_ISO_GROUP_ID_INVALID; 2919f9306a1aSMatthias Ringwald if (cig != NULL) { 2920f9306a1aSMatthias Ringwald // emit cis created if all ISO Paths have been created 2921f9306a1aSMatthias Ringwald // assume we are central 2922f9306a1aSMatthias Ringwald uint8_t cis_index = cig->state_vars.next_cis >> 1; 2923f9306a1aSMatthias Ringwald uint8_t cis_direction = cig->state_vars.next_cis & 1; 2924f9306a1aSMatthias Ringwald bool outgoing_needed = cig->params->cis_params[cis_index].max_sdu_p_to_c > 0; 2925f9306a1aSMatthias Ringwald // if outgoing has been setup, or incoming was setup but outgoing not required 2926f9306a1aSMatthias Ringwald if ((cis_direction == 1) || (outgoing_needed == false)){ 2927f9306a1aSMatthias Ringwald hci_emit_cis_created(cig->cig_id, cig->cis_con_handles[cis_index], status); 2928f9306a1aSMatthias Ringwald } 2929f9306a1aSMatthias Ringwald // next state 2930f9306a1aSMatthias Ringwald cig->state_vars.next_cis++; 2931f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_SETUP_ISO_PATH; 2932f9306a1aSMatthias Ringwald } 2933f9306a1aSMatthias Ringwald } 29342a6c0f82SMatthias Ringwald break; 29352a6c0f82SMatthias Ringwald } 29362a6c0f82SMatthias Ringwald case HCI_OPCODE_HCI_LE_BIG_TERMINATE_SYNC: { 29372a6c0f82SMatthias Ringwald // lookup BIG by state 29382a6c0f82SMatthias Ringwald btstack_linked_list_iterator_t it; 29392a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 29402a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 29412a6c0f82SMatthias Ringwald le_audio_big_sync_t *big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 2942d04a9afcSMatthias Ringwald uint8_t big_handle = big_sync->big_handle; 29432a6c0f82SMatthias Ringwald switch (big_sync->state){ 29442a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED: 2945d04a9afcSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 29462a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, big_sync->state_vars.status); 2947d04a9afcSMatthias Ringwald return; 29482a6c0f82SMatthias Ringwald default: 2949d04a9afcSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 2950d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 29517aa35f6aSMatthias Ringwald return; 29527aa35f6aSMatthias Ringwald } 29537aa35f6aSMatthias Ringwald } 29547aa35f6aSMatthias Ringwald break; 29557aa35f6aSMatthias Ringwald } 29564ae8623eSMatthias Ringwald #endif 29574ae8623eSMatthias Ringwald #endif 29586f35bb46SMatthias Ringwald default: 29596f35bb46SMatthias Ringwald break; 29604f781026SMatthias Ringwald } 2961645b7c25SMatthias Ringwald } 29624f781026SMatthias Ringwald 2963afbf1cd7SMatthias Ringwald static void handle_command_status_event(uint8_t * packet, uint16_t size) { 2964afbf1cd7SMatthias Ringwald UNUSED(size); 2965afbf1cd7SMatthias Ringwald 2966afbf1cd7SMatthias Ringwald // get num cmd packets - limit to 1 to reduce complexity 2967afbf1cd7SMatthias Ringwald hci_stack->num_cmd_packets = packet[3] ? 1 : 0; 2968afbf1cd7SMatthias Ringwald 2969c70df5f8SMatthias Ringwald // get opcode and command status 2970c70df5f8SMatthias Ringwald uint16_t opcode = hci_event_command_status_get_command_opcode(packet); 2971c70df5f8SMatthias Ringwald 2972c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) || defined(ENABLE_LE_ISOCHRONOUS_STREAMS) 2973c70df5f8SMatthias Ringwald uint8_t status = hci_event_command_status_get_status(packet); 2974c70df5f8SMatthias Ringwald #endif 2975c70df5f8SMatthias Ringwald 2976c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) 2977c70df5f8SMatthias Ringwald bd_addr_type_t addr_type; 2978c70df5f8SMatthias Ringwald #endif 2979c70df5f8SMatthias Ringwald 2980c70df5f8SMatthias Ringwald switch (opcode){ 2981afbf1cd7SMatthias Ringwald #ifdef ENABLE_CLASSIC 2982c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 2983c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 2984afbf1cd7SMatthias Ringwald #endif 2985afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2986c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 2987afbf1cd7SMatthias Ringwald #endif 2988c70df5f8SMatthias Ringwald #if defined(ENABLE_CLASSIC) || defined(ENABLE_LE_CENTRAL) 2989afbf1cd7SMatthias Ringwald addr_type = hci_stack->outgoing_addr_type; 2990afbf1cd7SMatthias Ringwald 2991afbf1cd7SMatthias Ringwald // reset outgoing address info 2992afbf1cd7SMatthias Ringwald memset(hci_stack->outgoing_addr, 0, 6); 2993afbf1cd7SMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_UNKNOWN; 2994afbf1cd7SMatthias Ringwald 2995afbf1cd7SMatthias Ringwald // on error 2996afbf1cd7SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 2997afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 2998afbf1cd7SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 2999afbf1cd7SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 3000afbf1cd7SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 3001afbf1cd7SMatthias Ringwald } 3002afbf1cd7SMatthias Ringwald #endif 3003afbf1cd7SMatthias Ringwald // error => outgoing connection failed 3004c70df5f8SMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(hci_stack->outgoing_addr, addr_type); 3005afbf1cd7SMatthias Ringwald if (conn != NULL){ 3006afbf1cd7SMatthias Ringwald hci_handle_connection_failed(conn, status); 3007afbf1cd7SMatthias Ringwald } 3008afbf1cd7SMatthias Ringwald } 3009c70df5f8SMatthias Ringwald break; 3010c70df5f8SMatthias Ringwald #endif 3011afbf1cd7SMatthias Ringwald #ifdef ENABLE_CLASSIC 3012c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_INQUIRY: 3013afbf1cd7SMatthias Ringwald if (status == ERROR_CODE_SUCCESS) { 3014afbf1cd7SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_ACTIVE; 3015afbf1cd7SMatthias Ringwald } else { 3016afbf1cd7SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 3017afbf1cd7SMatthias Ringwald } 3018c70df5f8SMatthias Ringwald break; 3019c70df5f8SMatthias Ringwald #endif 3020afbf1cd7SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3021c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CIS: 3022c70df5f8SMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 3023afbf1cd7SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 3024*d75a6bbfSMatthias Ringwald hci_iso_stream_requested_confirm(HCI_ISO_GROUP_ID_INVALID); 3025afbf1cd7SMatthias Ringwald } else { 3026*d75a6bbfSMatthias Ringwald hci_iso_stream_requested_finalize(HCI_ISO_GROUP_ID_INVALID); 3027afbf1cd7SMatthias Ringwald } 3028c70df5f8SMatthias Ringwald break; 3029afbf1cd7SMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 3030c70df5f8SMatthias Ringwald default: 3031c70df5f8SMatthias Ringwald break; 3032c70df5f8SMatthias Ringwald } 3033afbf1cd7SMatthias Ringwald } 3034afbf1cd7SMatthias Ringwald 30350ce3f217SMatthias Ringwald #ifdef ENABLE_BLE 30360ce3f217SMatthias Ringwald static void event_handle_le_connection_complete(const uint8_t * packet){ 30370ce3f217SMatthias Ringwald bd_addr_t addr; 30380ce3f217SMatthias Ringwald bd_addr_type_t addr_type; 30390ce3f217SMatthias Ringwald hci_connection_t * conn; 30400ce3f217SMatthias Ringwald 30410ce3f217SMatthias Ringwald // Connection management 30420ce3f217SMatthias Ringwald reverse_bd_addr(&packet[8], addr); 30430ce3f217SMatthias Ringwald addr_type = (bd_addr_type_t)packet[7]; 30440ce3f217SMatthias Ringwald log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 30450ce3f217SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 30460ce3f217SMatthias Ringwald 30470ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30480ce3f217SMatthias Ringwald // handle error: error is reported only to the initiator -> outgoing connection 30490ce3f217SMatthias Ringwald if (packet[3]){ 30500ce3f217SMatthias Ringwald 30510ce3f217SMatthias Ringwald // handle cancelled outgoing connection 30520ce3f217SMatthias Ringwald // "If the cancellation was successful then, after the Command Complete event for the LE_Create_Connection_Cancel command, 30530ce3f217SMatthias Ringwald // either an LE Connection Complete or an LE Enhanced Connection Complete event shall be generated. 30540ce3f217SMatthias Ringwald // In either case, the event shall be sent with the error code Unknown Connection Identifier (0x02)." 30550ce3f217SMatthias Ringwald if (packet[3] == ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER){ 30561f468175SMatthias Ringwald // reset state 30570ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 30581f468175SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 30590ce3f217SMatthias Ringwald // get outgoing connection conn struct for direct connect 30600ce3f217SMatthias Ringwald conn = gap_get_outgoing_connection(); 30610ce3f217SMatthias Ringwald } 30620ce3f217SMatthias Ringwald 30630ce3f217SMatthias Ringwald // outgoing le connection establishment is done 30640ce3f217SMatthias Ringwald if (conn){ 30650ce3f217SMatthias Ringwald // remove entry 30660ce3f217SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 30670ce3f217SMatthias Ringwald btstack_memory_hci_connection_free( conn ); 30680ce3f217SMatthias Ringwald } 30690ce3f217SMatthias Ringwald return; 30700ce3f217SMatthias Ringwald } 30710ce3f217SMatthias Ringwald #endif 30720ce3f217SMatthias Ringwald 30730ce3f217SMatthias Ringwald // on success, both hosts receive connection complete event 30740ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_MASTER){ 30750ce3f217SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 30760ce3f217SMatthias Ringwald // if we're master on an le connection, it was an outgoing connection and we're done with it 30770ce3f217SMatthias Ringwald // note: no hci_connection_t object exists yet for connect with whitelist 30780ce3f217SMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 30790ce3f217SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 30800ce3f217SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 30810ce3f217SMatthias Ringwald } 30820ce3f217SMatthias Ringwald #endif 30830ce3f217SMatthias Ringwald } else { 30840ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 30850ce3f217SMatthias Ringwald // if we're slave, it was an incoming connection, advertisements have stopped 3086935aa76eSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 30870ce3f217SMatthias Ringwald #endif 30880ce3f217SMatthias Ringwald } 30890ce3f217SMatthias Ringwald 30900ce3f217SMatthias Ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 30910ce3f217SMatthias Ringwald if (!conn){ 30920ce3f217SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, addr_type); 30930ce3f217SMatthias Ringwald } 30940ce3f217SMatthias Ringwald 30950ce3f217SMatthias Ringwald // no memory, sorry. 30960ce3f217SMatthias Ringwald if (!conn){ 30970ce3f217SMatthias Ringwald return; 30980ce3f217SMatthias Ringwald } 30990ce3f217SMatthias Ringwald 31000ce3f217SMatthias Ringwald conn->state = OPEN; 31010ce3f217SMatthias Ringwald conn->role = packet[6]; 31020ce3f217SMatthias Ringwald conn->con_handle = hci_subevent_le_connection_complete_get_connection_handle(packet); 31030ce3f217SMatthias Ringwald conn->le_connection_interval = hci_subevent_le_connection_complete_get_conn_interval(packet); 31040ce3f217SMatthias Ringwald 31050ce3f217SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 31060ce3f217SMatthias Ringwald if (packet[6] == HCI_ROLE_SLAVE){ 31070ce3f217SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 31080ce3f217SMatthias Ringwald } 31090ce3f217SMatthias Ringwald #endif 31100ce3f217SMatthias Ringwald 3111dde9ff1eSMatthias Ringwald // init unenhanced att bearer mtu 3112dde9ff1eSMatthias Ringwald conn->att_connection.mtu = ATT_DEFAULT_MTU; 3113dde9ff1eSMatthias Ringwald conn->att_connection.mtu_exchanged = false; 3114dde9ff1eSMatthias Ringwald 31150ce3f217SMatthias Ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 31160ce3f217SMatthias Ringwald 31170ce3f217SMatthias Ringwald // restart timer 31180ce3f217SMatthias Ringwald // btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 31190ce3f217SMatthias Ringwald // btstack_run_loop_add_timer(&conn->timeout); 31200ce3f217SMatthias Ringwald 31210ce3f217SMatthias Ringwald log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 31220ce3f217SMatthias Ringwald 31230ce3f217SMatthias Ringwald hci_emit_nr_connections_changed(); 31240ce3f217SMatthias Ringwald } 31250ce3f217SMatthias Ringwald #endif 31260ce3f217SMatthias Ringwald 312717c6fe5cSMatthias Ringwald #ifdef ENABLE_CLASSIC 312817c6fe5cSMatthias 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){ 312917c6fe5cSMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_UNKNOWN) return false; 313017c6fe5cSMatthias Ringwald // LEVEL_4 is tested by l2cap 31319a8f78c1SMatthias Ringwald // LEVEL 3 requires MITM protection -> check io capabilities if Authenticated is possible 31329a8f78c1SMatthias Ringwald // @see: Core Spec v5.3, Vol 3, Part C, Table 5.7 313317c6fe5cSMatthias Ringwald if (level >= LEVEL_3){ 31349a8f78c1SMatthias Ringwald // MITM not possible without keyboard or display 313517c6fe5cSMatthias Ringwald if (io_cap_remote >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 313617c6fe5cSMatthias Ringwald if (io_cap_local >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT) return false; 31379a8f78c1SMatthias Ringwald 31389a8f78c1SMatthias Ringwald // MITM possible if one side has keyboard and the other has keyboard or display 31399a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 31409a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_KEYBOARD_ONLY) return true; 31419a8f78c1SMatthias Ringwald 31429a8f78c1SMatthias Ringwald // MITM not possible if one side has only display and other side has no keyboard 31439a8f78c1SMatthias Ringwald if (io_cap_remote == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 31449a8f78c1SMatthias Ringwald if (io_cap_local == SSP_IO_CAPABILITY_DISPLAY_ONLY) return false; 314517c6fe5cSMatthias Ringwald } 314617c6fe5cSMatthias Ringwald // LEVEL 2 requires SSP, which is a given 314717c6fe5cSMatthias Ringwald return true; 314817c6fe5cSMatthias Ringwald } 31493817f9dfSMatthias Ringwald 31503817f9dfSMatthias Ringwald static bool btstack_is_null(uint8_t * data, uint16_t size){ 31513817f9dfSMatthias Ringwald uint16_t i; 31523817f9dfSMatthias Ringwald for (i=0; i < size ; i++){ 31533817f9dfSMatthias Ringwald if (data[i] != 0) { 31543817f9dfSMatthias Ringwald return false; 31553817f9dfSMatthias Ringwald } 31563817f9dfSMatthias Ringwald } 31573817f9dfSMatthias Ringwald return true; 31583817f9dfSMatthias Ringwald } 31593817f9dfSMatthias Ringwald 3160b3c4163bSMatthias Ringwald static void hci_ssp_assess_security_on_io_cap_request(hci_connection_t * conn){ 31612dd8985bSMatthias Ringwald // get requested security level 31622dd8985bSMatthias Ringwald gap_security_level_t requested_security_level = conn->requested_security_level; 31632dd8985bSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 31642dd8985bSMatthias Ringwald requested_security_level = LEVEL_4; 31652dd8985bSMatthias Ringwald } 31662dd8985bSMatthias Ringwald 3167b3c4163bSMatthias Ringwald // assess security: LEVEL 4 requires SC 31682dd8985bSMatthias Ringwald // skip this preliminary test if remote features are not available yet to work around potential issue in ESP32 controller 31692dd8985bSMatthias Ringwald if ((requested_security_level == LEVEL_4) && 31702dd8985bSMatthias Ringwald ((conn->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0) && 31712dd8985bSMatthias Ringwald !hci_remote_sc_enabled(conn)){ 3172b3c4163bSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 3173b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3174b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3175b3c4163bSMatthias Ringwald return; 3176b3c4163bSMatthias Ringwald } 3177b3c4163bSMatthias Ringwald 3178b3c4163bSMatthias Ringwald // assess security based on io capabilities 3179b3c4163bSMatthias Ringwald if (conn->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 3180b3c4163bSMatthias Ringwald // responder: fully validate io caps of both sides as well as OOB data 3181b3c4163bSMatthias Ringwald bool security_possible = false; 3182b3c4163bSMatthias Ringwald security_possible = hci_ssp_security_level_possible_for_io_cap(requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io); 3183b3c4163bSMatthias Ringwald 3184b3c4163bSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3185b3c4163bSMatthias Ringwald // We assume that both Controller can reach LEVEL 4, if one side has received P-192 and the other has received P-256, 3186b3c4163bSMatthias Ringwald // so we merge the OOB data availability 3187b3c4163bSMatthias Ringwald uint8_t have_oob_data = conn->io_cap_response_oob_data; 3188b3c4163bSMatthias Ringwald if (conn->classic_oob_c_192 != NULL){ 3189b3c4163bSMatthias Ringwald have_oob_data |= 1; 3190b3c4163bSMatthias Ringwald } 3191b3c4163bSMatthias Ringwald if (conn->classic_oob_c_256 != NULL){ 3192b3c4163bSMatthias Ringwald have_oob_data |= 2; 3193b3c4163bSMatthias Ringwald } 3194b3c4163bSMatthias Ringwald // for up to Level 3, either P-192 as well as P-256 will do 3195b3c4163bSMatthias 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 3196b3c4163bSMatthias Ringwald // if remote does not SC, we should not receive P-256 data either 3197b3c4163bSMatthias Ringwald if ((requested_security_level <= LEVEL_3) && (have_oob_data != 0)){ 3198b3c4163bSMatthias Ringwald security_possible = true; 3199b3c4163bSMatthias Ringwald } 3200b3c4163bSMatthias Ringwald // for Level 4, P-256 is needed 3201b3c4163bSMatthias Ringwald if ((requested_security_level == LEVEL_4 && ((have_oob_data & 2) != 0))){ 3202b3c4163bSMatthias Ringwald security_possible = true; 3203b3c4163bSMatthias Ringwald } 3204b3c4163bSMatthias Ringwald #endif 3205b3c4163bSMatthias Ringwald 3206b3c4163bSMatthias Ringwald if (security_possible == false){ 32072dd8985bSMatthias Ringwald log_info("IOCap/OOB insufficient for level %u -> abort", requested_security_level); 3208b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3209b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3210b3c4163bSMatthias Ringwald return; 3211b3c4163bSMatthias Ringwald } 3212b3c4163bSMatthias Ringwald } else { 3213b3c4163bSMatthias Ringwald // initiator: remote io cap not yet, only check if we have ability for MITM protection if requested and OOB is not supported 32141fe968f5SMatthias Ringwald #ifndef ENABLE_CLASSIC_PAIRING_OOB 32151fe968f5SMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 3216b3c4163bSMatthias Ringwald if ((conn->requested_security_level >= LEVEL_3) && (hci_stack->ssp_io_capability >= SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT)){ 3217b3c4163bSMatthias Ringwald log_info("Level 3+ required, but no input/output -> abort"); 3218b3c4163bSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3219b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3220b3c4163bSMatthias Ringwald return; 3221b3c4163bSMatthias Ringwald } 3222b3c4163bSMatthias Ringwald #endif 32231fe968f5SMatthias Ringwald #endif 3224b3c4163bSMatthias Ringwald } 3225b3c4163bSMatthias Ringwald 3226b3c4163bSMatthias Ringwald #ifndef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 3227b3c4163bSMatthias Ringwald if (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN){ 3228b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 3229b3c4163bSMatthias Ringwald } else { 3230b3c4163bSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 3231b3c4163bSMatthias Ringwald } 3232b3c4163bSMatthias Ringwald #endif 3233b3c4163bSMatthias Ringwald } 3234b3c4163bSMatthias Ringwald 323517c6fe5cSMatthias Ringwald #endif 323617c6fe5cSMatthias Ringwald 3237c3b46f5aSMatthias Ringwald static void event_handler(uint8_t *packet, uint16_t size){ 32384f781026SMatthias Ringwald 32394f781026SMatthias Ringwald uint16_t event_length = packet[1]; 32404f781026SMatthias Ringwald 32414f781026SMatthias Ringwald // assert packet is complete 32424ea43905SMatthias Ringwald if (size != (event_length + 2u)){ 32434f781026SMatthias Ringwald log_error("event_handler called with packet of wrong size %d, expected %u => dropping packet", size, event_length + 2); 32444f781026SMatthias Ringwald return; 32454f781026SMatthias Ringwald } 32464f781026SMatthias Ringwald 32474f781026SMatthias Ringwald hci_con_handle_t handle; 32484f781026SMatthias Ringwald hci_connection_t * conn; 32494f781026SMatthias Ringwald int i; 32504f781026SMatthias Ringwald 32514f781026SMatthias Ringwald #ifdef ENABLE_CLASSIC 32525e91d96cSMatthias Ringwald hci_link_type_t link_type; 325348f33f37SMatthias Ringwald bd_addr_t addr; 3254cef94710SMatthias Ringwald bd_addr_type_t addr_type; 32554f781026SMatthias Ringwald #endif 32564ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 32574ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream; 32587aa35f6aSMatthias Ringwald le_audio_big_t * big; 32592a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync; 32604ae8623eSMatthias Ringwald #endif 32614f781026SMatthias Ringwald 32624f781026SMatthias Ringwald // log_info("HCI:EVENT:%02x", hci_event_packet_get_type(packet)); 32634f781026SMatthias Ringwald 32644f781026SMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 32654f781026SMatthias Ringwald 32664f781026SMatthias Ringwald case HCI_EVENT_COMMAND_COMPLETE: 32674f781026SMatthias Ringwald handle_command_complete_event(packet, size); 326856cf178bSmatthias.ringwald break; 326956cf178bSmatthias.ringwald 32707ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 3271afbf1cd7SMatthias Ringwald handle_command_status_event(packet, size); 32727ec5eeaaSmatthias.ringwald break; 32737ec5eeaaSmatthias.ringwald 32742e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 32759784dac1SMatthias Ringwald if (size < 3) return; 32769784dac1SMatthias Ringwald uint16_t num_handles = packet[2]; 32774ea43905SMatthias Ringwald if (size != (3u + num_handles * 4u)) return; 3278d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3279d9a1d8edSMatthias Ringwald bool notify_sco = false; 3280d9a1d8edSMatthias Ringwald #endif 3281d9a1d8edSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3282d9a1d8edSMatthias Ringwald bool notify_iso = false; 3283d9a1d8edSMatthias Ringwald #endif 32849784dac1SMatthias Ringwald uint16_t offset = 3; 32859784dac1SMatthias Ringwald for (i=0; i<num_handles;i++){ 32864ea43905SMatthias Ringwald handle = little_endian_read_16(packet, offset) & 0x0fffu; 32874ea43905SMatthias Ringwald offset += 2u; 3288f8fbdce0SMatthias Ringwald uint16_t num_packets = little_endian_read_16(packet, offset); 32894ea43905SMatthias Ringwald offset += 2u; 32902e440c8aS[email protected] 32915061f3afS[email protected] conn = hci_connection_for_handle(handle); 3292e5413aa0SMatthias Ringwald if (conn != NULL) { 329323bed257S[email protected] 3294ce41473eSMatthias Ringwald if (conn->num_packets_sent >= num_packets) { 3295ce41473eSMatthias Ringwald conn->num_packets_sent -= num_packets; 3296e35edcc1S[email protected] } else { 3297ce41473eSMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 3298ce41473eSMatthias Ringwald conn->num_packets_sent = 0; 3299e35edcc1S[email protected] } 3300ce41473eSMatthias Ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_packets_sent); 3301d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3302d9a1d8edSMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO){ 3303d9a1d8edSMatthias Ringwald notify_sco = true; 3304d9a1d8edSMatthias Ringwald } 3305d9a1d8edSMatthias Ringwald #endif 3306e5413aa0SMatthias Ringwald } 3307e5413aa0SMatthias Ringwald 33088ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 33098ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 33108ad9cf99SMatthias Ringwald #endif 33118ad9cf99SMatthias Ringwald 3312e5413aa0SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3313e5413aa0SMatthias Ringwald if (conn == NULL){ 3314e5413aa0SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(handle); 3315e5413aa0SMatthias Ringwald if (iso_stream != NULL){ 3316e5413aa0SMatthias Ringwald if (iso_stream->num_packets_sent >= num_packets) { 3317e5413aa0SMatthias Ringwald iso_stream->num_packets_sent -= num_packets; 3318e5413aa0SMatthias Ringwald } else { 3319e5413aa0SMatthias Ringwald log_error("hci_number_completed_packets, more packet slots freed then sent."); 3320e5413aa0SMatthias Ringwald iso_stream->num_packets_sent = 0; 3321e5413aa0SMatthias Ringwald } 33228d72db10SMatthias Ringwald if (iso_stream->iso_type == HCI_ISO_TYPE_BIS){ 33238d72db10SMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(iso_stream->group_id); 3324fe977b37SMatthias Ringwald if (big != NULL){ 3325fe977b37SMatthias Ringwald big->num_completed_timestamp_current_valid = true; 3326fe977b37SMatthias Ringwald big->num_completed_timestamp_current_ms = btstack_run_loop_get_time_ms(); 3327fe977b37SMatthias Ringwald } 3328fe977b37SMatthias Ringwald } 3329e5413aa0SMatthias Ringwald log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", 3330e5413aa0SMatthias Ringwald num_packets, handle, iso_stream->num_packets_sent); 3331d9a1d8edSMatthias Ringwald notify_iso = true; 3332e5413aa0SMatthias Ringwald } 3333d9a1d8edSMatthias Ringwald } 3334d9a1d8edSMatthias Ringwald #endif 3335d9a1d8edSMatthias Ringwald } 3336d9a1d8edSMatthias Ringwald 3337d9a1d8edSMatthias Ringwald #ifdef ENABLE_CLASSIC 3338d9a1d8edSMatthias Ringwald if (notify_sco){ 3339d9a1d8edSMatthias Ringwald hci_notify_if_sco_can_send_now(); 3340d9a1d8edSMatthias Ringwald } 3341d9a1d8edSMatthias Ringwald #endif 3342d9a1d8edSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3343d9a1d8edSMatthias Ringwald if (notify_iso){ 33445ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 3345e5413aa0SMatthias Ringwald } 3346e5413aa0SMatthias Ringwald #endif 33476772a24cSmatthias.ringwald break; 33482e440c8aS[email protected] } 334935454696SMatthias Ringwald 335035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 33519afaa4d4SMatthias Ringwald case HCI_EVENT_FLUSH_OCCURRED: 33529afaa4d4SMatthias Ringwald // flush occurs only if automatic flush has been enabled by gap_enable_link_watchdog() 33539afaa4d4SMatthias Ringwald handle = hci_event_flush_occurred_get_handle(packet); 33549afaa4d4SMatthias Ringwald conn = hci_connection_for_handle(handle); 33559afaa4d4SMatthias Ringwald if (conn) { 33569afaa4d4SMatthias Ringwald log_info("Flush occurred, disconnect 0x%04x", handle); 33579afaa4d4SMatthias Ringwald conn->state = SEND_DISCONNECT; 33589afaa4d4SMatthias Ringwald } 33599afaa4d4SMatthias Ringwald break; 33609afaa4d4SMatthias Ringwald 3361f5875de5SMatthias Ringwald case HCI_EVENT_INQUIRY_COMPLETE: 3362f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_ACTIVE){ 3363f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_IDLE; 3364f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 3365f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 3366f5875de5SMatthias Ringwald } 3367f5875de5SMatthias Ringwald break; 3368b7f1ee76SMatthias Ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 3369b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 3370b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_IDLE; 3371b7f1ee76SMatthias Ringwald } 3372b7f1ee76SMatthias Ringwald break; 33731f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 3374724d70a2SMatthias Ringwald reverse_bd_addr(&packet[2], addr); 33755e91d96cSMatthias Ringwald link_type = (hci_link_type_t) packet[11]; 337672cf8859SMatthias Ringwald 337772cf8859SMatthias Ringwald // CVE-2020-26555: reject incoming connection from device with same BD ADDR 337872cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0){ 337972cf8859SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 338072cf8859SMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 338172cf8859SMatthias Ringwald break; 338272cf8859SMatthias Ringwald } 338372cf8859SMatthias Ringwald 338407e010b6SMilanka Ringwald if (hci_stack->gap_classic_accept_callback != NULL){ 33855e91d96cSMatthias Ringwald if ((*hci_stack->gap_classic_accept_callback)(addr, link_type) == 0){ 3386d152b6c6SMatthias Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS; 338707e010b6SMilanka Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 338807e010b6SMilanka Ringwald break; 338907e010b6SMilanka Ringwald } 339007e010b6SMilanka Ringwald } 339107e010b6SMilanka Ringwald 339237eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 33935e91d96cSMatthias Ringwald log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), (unsigned int) link_type); 33945e91d96cSMatthias Ringwald addr_type = (link_type == HCI_LINK_TYPE_ACL) ? BD_ADDR_TYPE_ACL : BD_ADDR_TYPE_SCO; 33952e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 33961f7b95a1Smatthias.ringwald if (!conn) { 33975293c072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 33981f7b95a1Smatthias.ringwald } 3399ce4c8fabSmatthias.ringwald if (!conn) { 3400ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 34014536712cSMilanka Ringwald hci_stack->decline_reason = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES; 3402058e3d6bSMatthias Ringwald bd_addr_copy(hci_stack->decline_addr, addr); 3403f23819bcSMatthias Ringwald hci_run(); 3404f23819bcSMatthias Ringwald // avoid event to higher layer 3405f23819bcSMatthias Ringwald return; 3406ce4c8fabSmatthias.ringwald } 34075cf766e8SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 340832ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 3409f3a16b9aSMatthias Ringwald // store info about eSCO 34105e91d96cSMatthias Ringwald if (link_type == HCI_LINK_TYPE_ESCO){ 341176ccfb2aSMatthias Ringwald conn->remote_supported_features[0] |= 1; 3412f3a16b9aSMatthias Ringwald } 341332ab9390Smatthias.ringwald hci_run(); 34141f7b95a1Smatthias.ringwald break; 34151f7b95a1Smatthias.ringwald 34166772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 3417fe1ed1b8Smatthias.ringwald // Connection management 3418724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 34199da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 3420f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 34212e77e513S[email protected] conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3422fe1ed1b8Smatthias.ringwald if (conn) { 3423b448a0e7Smatthias.ringwald if (!packet[2]){ 3424c8e4258aSmatthias.ringwald conn->state = OPEN; 3425f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 34266909f064SMatthias Ringwald 342722df2fe2SMatthias Ringwald // trigger write supervision timeout if we're master 3428d821984bSMatthias Ringwald if ((hci_stack->link_supervision_timeout != HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT) && (conn->role == HCI_ROLE_MASTER)){ 342922df2fe2SMatthias Ringwald conn->gap_connection_tasks |= GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT; 34306909f064SMatthias Ringwald } 34316909f064SMatthias Ringwald 343222df2fe2SMatthias Ringwald // trigger write automatic flush timeout 34339afaa4d4SMatthias Ringwald if (hci_stack->automatic_flush_timeout != 0){ 34349afaa4d4SMatthias Ringwald conn->gap_connection_tasks |= GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT; 34359afaa4d4SMatthias Ringwald } 34369afaa4d4SMatthias Ringwald 3437c785ef68Smatthias.ringwald // restart timer 3438528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 3439528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&conn->timeout); 3440c785ef68Smatthias.ringwald 344118c4a0a3SMatthias Ringwald // trigger remote features for dedicated bonding 344218c4a0a3SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 344318c4a0a3SMatthias Ringwald hci_trigger_remote_features_for_connection(conn); 344418c4a0a3SMatthias Ringwald } 344518c4a0a3SMatthias Ringwald 34469da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 344743bfb1bdSmatthias.ringwald 344843bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 3449b448a0e7Smatthias.ringwald } else { 34500bbba85bSMatthias Ringwald // connection failed 34510bbba85bSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 3452fe1ed1b8Smatthias.ringwald } 3453fe1ed1b8Smatthias.ringwald } 34546772a24cSmatthias.ringwald break; 3455fe1ed1b8Smatthias.ringwald 345644d0e3d5S[email protected] case HCI_EVENT_SYNCHRONOUS_CONNECTION_COMPLETE: 3457724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], addr); 34581f34df2cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 34591f34df2cSMatthias Ringwald log_info("Synchronous Connection Complete for %p (status=%u) %s", conn, packet[2], bd_addr_to_str(addr)); 34601a06f663S[email protected] if (packet[2]){ 346144d0e3d5S[email protected] // connection failed 34621f34df2cSMatthias Ringwald if (conn){ 34631f34df2cSMatthias Ringwald hci_handle_connection_failed(conn, packet[2]); 34641f34df2cSMatthias Ringwald } 346544d0e3d5S[email protected] break; 346644d0e3d5S[email protected] } 3467e35edcc1S[email protected] if (!conn) { 3468e35edcc1S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 3469e35edcc1S[email protected] } 3470e35edcc1S[email protected] if (!conn) { 3471e35edcc1S[email protected] break; 3472e35edcc1S[email protected] } 34731a06f663S[email protected] conn->state = OPEN; 3474f8fbdce0SMatthias Ringwald conn->con_handle = little_endian_read_16(packet, 3); 3475ee752bb8SMatthias Ringwald 3476ee752bb8SMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 3477ee752bb8SMatthias Ringwald // update SCO 3478ee752bb8SMatthias Ringwald if (conn->address_type == BD_ADDR_TYPE_SCO && hci_stack->hci_transport && hci_stack->hci_transport->set_sco_config){ 3479ee752bb8SMatthias Ringwald hci_stack->hci_transport->set_sco_config(hci_stack->sco_voice_setting_active, hci_number_sco_connections()); 3480ee752bb8SMatthias Ringwald } 3481f234b250SMatthias Ringwald // trigger can send now 3482f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 34831972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 3484f234b250SMatthias Ringwald } 3485ee752bb8SMatthias Ringwald #endif 3486cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 3487cb70c5abSMatthias Ringwald // configure sco transport 3488cb70c5abSMatthias Ringwald if (hci_stack->sco_transport != NULL){ 3489cb70c5abSMatthias Ringwald sco_format_t sco_format = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? SCO_FORMAT_8_BIT : SCO_FORMAT_16_BIT; 3490cb70c5abSMatthias Ringwald hci_stack->sco_transport->open(conn->con_handle, sco_format); 3491cb70c5abSMatthias Ringwald } 3492cb70c5abSMatthias Ringwald #endif 349344d0e3d5S[email protected] break; 349444d0e3d5S[email protected] 3495afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 3496f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 3497afd4e962S[email protected] conn = hci_connection_for_handle(handle); 3498afd4e962S[email protected] if (!conn) break; 3499afd4e962S[email protected] if (!packet[2]){ 35002f5c44baSMatthias Ringwald const uint8_t * features = &packet[5]; 35012f5c44baSMatthias Ringwald hci_handle_remote_features_page_0(conn, features); 35022f5c44baSMatthias Ringwald 35035ccef624SMatthias Ringwald // read extended features if possible 35041ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_REMOTE_EXTENDED_FEATURES) 35051ffa425cSMatthias Ringwald && ((conn->remote_supported_features[0] & 2) != 0)) { 35065ccef624SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 35075ccef624SMatthias Ringwald break; 35085ccef624SMatthias Ringwald } 35095ccef624SMatthias Ringwald } 35105ccef624SMatthias Ringwald hci_handle_remote_features_received(conn); 35115ccef624SMatthias Ringwald break; 35125ccef624SMatthias Ringwald 35135ccef624SMatthias Ringwald case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE: 35145ccef624SMatthias Ringwald handle = little_endian_read_16(packet, 3); 35155ccef624SMatthias Ringwald conn = hci_connection_for_handle(handle); 35165ccef624SMatthias Ringwald if (!conn) break; 35175ccef624SMatthias Ringwald // status = ok, page = 1 351850c51a77SMatthias Ringwald if (!packet[2]) { 351950c51a77SMatthias Ringwald uint8_t page_number = packet[5]; 352050c51a77SMatthias Ringwald uint8_t maximum_page_number = packet[6]; 35215ccef624SMatthias Ringwald const uint8_t * features = &packet[7]; 352250c51a77SMatthias Ringwald bool done = false; 352350c51a77SMatthias Ringwald switch (page_number){ 352450c51a77SMatthias Ringwald case 1: 35252f5c44baSMatthias Ringwald hci_handle_remote_features_page_1(conn, features); 352650c51a77SMatthias Ringwald if (maximum_page_number >= 2){ 352750c51a77SMatthias Ringwald // get Secure Connections (Controller) from Page 2 if available 352850c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 352950c51a77SMatthias Ringwald } else { 353050c51a77SMatthias Ringwald // otherwise, assume SC (Controller) == SC (Host) 353150c51a77SMatthias Ringwald if ((conn->bonding_flags & BONDING_REMOTE_SUPPORTS_SC_HOST) != 0){ 353250c51a77SMatthias Ringwald conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SC_CONTROLLER; 353350c51a77SMatthias Ringwald } 353450c51a77SMatthias Ringwald done = true; 353550c51a77SMatthias Ringwald } 353650c51a77SMatthias Ringwald break; 353750c51a77SMatthias Ringwald case 2: 353850c51a77SMatthias Ringwald hci_handle_remote_features_page_2(conn, features); 353950c51a77SMatthias Ringwald done = true; 354050c51a77SMatthias Ringwald break; 354150c51a77SMatthias Ringwald default: 354250c51a77SMatthias Ringwald break; 354350c51a77SMatthias Ringwald } 354450c51a77SMatthias Ringwald if (!done) break; 3545afd4e962S[email protected] } 3546de0df013SMatthias Ringwald hci_handle_remote_features_received(conn); 3547afd4e962S[email protected] break; 3548afd4e962S[email protected] 35497fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 3550308eeaffSMatthias Ringwald #ifndef ENABLE_EXPLICIT_LINK_KEY_REPLY 355163c0b2fdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 355263c0b2fdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 355363c0b2fdSMatthias Ringwald if (!conn) break; 355463c0b2fdSMatthias Ringwald 355563c0b2fdSMatthias Ringwald // lookup link key in db if not cached 355663c0b2fdSMatthias Ringwald if ((conn->link_key_type == INVALID_LINK_KEY) && (hci_stack->link_key_db != NULL)){ 355763c0b2fdSMatthias Ringwald hci_stack->link_key_db->get_link_key(conn->address, conn->link_key, &conn->link_key_type); 355863c0b2fdSMatthias Ringwald } 355963c0b2fdSMatthias Ringwald 356063c0b2fdSMatthias Ringwald // response sent by hci_run() 356163c0b2fdSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_HANDLE_LINK_KEY_REQUEST; 356263c0b2fdSMatthias Ringwald #endif 3563608f51bbSMatthias Ringwald break; 35647fde4af9Smatthias.ringwald 35659ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 35661714cbbdSMatthias Ringwald hci_event_link_key_request_get_bd_addr(packet, addr); 3567f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 35689ab95c90S[email protected] if (!conn) break; 35691714cbbdSMatthias Ringwald 35701714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_SUCCESS); 35711714cbbdSMatthias Ringwald 35723817f9dfSMatthias Ringwald // CVE-2020-26555: ignore NULL link key 35733817f9dfSMatthias Ringwald // default link_key_type = INVALID_LINK_KEY asserts that NULL key won't be used for encryption 35743817f9dfSMatthias Ringwald if (btstack_is_null(&packet[8], 16)) break; 35753817f9dfSMatthias Ringwald 35767bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 35779ab95c90S[email protected] // Change Connection Encryption keeps link key type 35789ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 35799ab95c90S[email protected] conn->link_key_type = link_key_type; 35809ab95c90S[email protected] } 35813817f9dfSMatthias Ringwald 3582e9f98c4aSMatthias Ringwald // cache link key. link keys stored in little-endian format for legacy reasons 3583e9f98c4aSMatthias Ringwald memcpy(&conn->link_key, &packet[8], 16); 3584e9f98c4aSMatthias Ringwald 3585bec5f683SMatthias Ringwald // only store link key: 3586bec5f683SMatthias Ringwald // - if bondable enabled 3587bec5f683SMatthias Ringwald if (hci_stack->bondable == false) break; 35886edaed7fSMatthias Ringwald // - if security level sufficient 35896edaed7fSMatthias Ringwald if (gap_security_level_for_link_key_type(link_key_type) < conn->requested_security_level) break; 3590bec5f683SMatthias Ringwald // - for SSP, also check if remote side requested bonding as well 3591bec5f683SMatthias Ringwald if (conn->link_key_type != COMBINATION_KEY){ 3592532454f9SMatthias Ringwald bool remote_bonding = conn->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 3593532454f9SMatthias Ringwald if (!remote_bonding){ 3594bec5f683SMatthias Ringwald break; 3595bec5f683SMatthias Ringwald } 3596bec5f683SMatthias Ringwald } 359755597469SMatthias Ringwald gap_store_link_key_for_bd_addr(addr, &packet[8], conn->link_key_type); 35987fde4af9Smatthias.ringwald break; 35999ab95c90S[email protected] } 36007fde4af9Smatthias.ringwald 36017fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 36021714cbbdSMatthias Ringwald hci_event_pin_code_request_get_bd_addr(packet, addr); 36031714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 36041714cbbdSMatthias Ringwald if (!conn) break; 36051714cbbdSMatthias Ringwald 36061714cbbdSMatthias Ringwald hci_pairing_started(conn, false); 3607a800d95eSMatthias Ringwald // abort pairing if: non-bondable mode (pin code request is not forwarded to app) 36083a9fb326S[email protected] if (!hci_stack->bondable ){ 36098daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 36101714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_PAIRING_NOT_ALLOWED); 3611f8fb5f6eS[email protected] hci_run(); 3612f8fb5f6eS[email protected] return; 36134c57c146S[email protected] } 3614a800d95eSMatthias Ringwald // abort pairing if: LEVEL_4 required (pin code request is not forwarded to app) 3615a800d95eSMatthias Ringwald if ((hci_stack->gap_secure_connections_only_mode) || (conn->requested_security_level == LEVEL_4)){ 3616a800d95eSMatthias Ringwald log_info("Level 4 required, but SC not supported -> abort"); 361750dcc63cSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_DENY_PIN_CODE_REQUEST; 361850dcc63cSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 361950dcc63cSMatthias Ringwald hci_run(); 362050dcc63cSMatthias Ringwald return; 362150dcc63cSMatthias Ringwald } 36227fde4af9Smatthias.ringwald break; 36237fde4af9Smatthias.ringwald 362450d7398cSMatthias Ringwald case HCI_EVENT_IO_CAPABILITY_RESPONSE: 362550d7398cSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 362650d7398cSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 362750d7398cSMatthias Ringwald if (!conn) break; 36281714cbbdSMatthias Ringwald 36298daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE); 36301714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 363150d7398cSMatthias Ringwald conn->io_cap_response_auth_req = hci_event_io_capability_response_get_authentication_requirements(packet); 3632e276e383SMatthias Ringwald conn->io_cap_response_io = hci_event_io_capability_response_get_io_capability(packet); 3633d22b82d6SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 3634d22b82d6SMatthias Ringwald conn->io_cap_response_oob_data = hci_event_io_capability_response_get_oob_data_present(packet); 3635d22b82d6SMatthias Ringwald #endif 363650d7398cSMatthias Ringwald break; 363750d7398cSMatthias Ringwald 36381d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 36391714cbbdSMatthias Ringwald hci_event_io_capability_response_get_bd_addr(packet, addr); 36401714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 36411714cbbdSMatthias Ringwald if (!conn) break; 36421714cbbdSMatthias Ringwald 3643c950c316SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 36449671e615SMatthias Ringwald hci_connection_timestamp(conn); 36451714cbbdSMatthias Ringwald hci_pairing_started(conn, true); 3646dbe1a790S[email protected] break; 3647dbe1a790S[email protected] 36481849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 36491849becdSMatthias Ringwald case HCI_EVENT_REMOTE_OOB_DATA_REQUEST: 36507ca4a7edSMatthias Ringwald hci_event_remote_oob_data_request_get_bd_addr(packet, addr); 36517ca4a7edSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 36527ca4a7edSMatthias Ringwald if (!conn) break; 36537ca4a7edSMatthias Ringwald 36547ca4a7edSMatthias Ringwald hci_connection_timestamp(conn); 36557ca4a7edSMatthias Ringwald 36567ca4a7edSMatthias Ringwald hci_pairing_started(conn, true); 36577ca4a7edSMatthias Ringwald 36587ca4a7edSMatthias Ringwald connectionSetAuthenticationFlags(conn, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 36591849becdSMatthias Ringwald break; 36601849becdSMatthias Ringwald #endif 36611849becdSMatthias Ringwald 3662dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 366317c6fe5cSMatthias Ringwald hci_event_user_confirmation_request_get_bd_addr(packet, addr); 3664367aedc0SMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 3665367aedc0SMatthias Ringwald if (!conn) break; 3666367aedc0SMatthias Ringwald if (hci_ssp_security_level_possible_for_io_cap(conn->requested_security_level, hci_stack->ssp_io_capability, conn->io_cap_response_io)) { 3667367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 36688daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 3669367aedc0SMatthias Ringwald }; 3670367aedc0SMatthias Ringwald } else { 3671367aedc0SMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_INSUFFICIENT_SECURITY); 3672367aedc0SMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 3673367aedc0SMatthias Ringwald // don't forward event to app 3674367aedc0SMatthias Ringwald hci_run(); 3675367aedc0SMatthias Ringwald return; 3676367aedc0SMatthias Ringwald } 3677dbe1a790S[email protected] break; 3678dbe1a790S[email protected] 3679dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 3680367aedc0SMatthias Ringwald // Pairing using Passkey results in MITM protection. If Level 4 is required, support for SC is validated on IO Cap Request 3681367aedc0SMatthias Ringwald if (hci_stack->ssp_auto_accept){ 36828daf94bcSMatthias Ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 3683367aedc0SMatthias Ringwald }; 36841d6b20aeS[email protected] break; 36851849becdSMatthias Ringwald 36863dce6128SMatthias Ringwald case HCI_EVENT_MODE_CHANGE: 36873dce6128SMatthias Ringwald handle = hci_event_mode_change_get_handle(packet); 36883dce6128SMatthias Ringwald conn = hci_connection_for_handle(handle); 36893dce6128SMatthias Ringwald if (!conn) break; 36903dce6128SMatthias Ringwald conn->connection_mode = hci_event_mode_change_get_mode(packet); 36913dce6128SMatthias Ringwald log_info("HCI_EVENT_MODE_CHANGE, handle 0x%04x, mode %u", handle, conn->connection_mode); 36923dce6128SMatthias Ringwald break; 369335454696SMatthias Ringwald #endif 36941d6b20aeS[email protected] 3695f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 3696a355334eSMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 3697254b78eeSMatthias Ringwald handle = hci_event_encryption_change_get_connection_handle(packet); 3698f0944df2S[email protected] conn = hci_connection_for_handle(handle); 3699f0944df2S[email protected] if (!conn) break; 37004ea43905SMatthias Ringwald if (hci_event_encryption_change_get_status(packet) == 0u) { 3701254b78eeSMatthias Ringwald uint8_t encryption_enabled = hci_event_encryption_change_get_encryption_enabled(packet); 3702254b78eeSMatthias Ringwald if (encryption_enabled){ 3703573897a0SMatthias Ringwald if (hci_is_le_connection(conn)){ 3704573897a0SMatthias Ringwald // For LE, we accept connection as encrypted 37058daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_ENCRYPTED; 3706573897a0SMatthias Ringwald } 3707573897a0SMatthias Ringwald #ifdef ENABLE_CLASSIC 3708573897a0SMatthias Ringwald else { 3709fcaf38b9SMatthias Ringwald 3710254b78eeSMatthias Ringwald // Detect Secure Connection -> Legacy Connection Downgrade Attack (BIAS) 371136c3546bSMatthias Ringwald bool sc_used_during_pairing = gap_secure_connection_for_link_key_type(conn->link_key_type); 3712254b78eeSMatthias Ringwald bool connected_uses_aes_ccm = encryption_enabled == 2; 3713edc1ac20SMatthias Ringwald if (hci_stack->secure_connections_active && sc_used_during_pairing && !connected_uses_aes_ccm){ 3714254b78eeSMatthias Ringwald log_info("SC during pairing, but only E0 now -> abort"); 37159ece71c2SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 3716254b78eeSMatthias Ringwald break; 3717254b78eeSMatthias Ringwald } 3718254b78eeSMatthias Ringwald 3719aa2fe986SMatthias Ringwald // if AES-CCM is used, authentication used SC -> authentication was mutual and we can skip explicit authentication 3720aa2fe986SMatthias Ringwald if (connected_uses_aes_ccm){ 3721aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3722aa2fe986SMatthias Ringwald } 3723aa2fe986SMatthias Ringwald 3724aa2fe986SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 3725aa2fe986SMatthias Ringwald // work around for issue with PTS dongle 3726aa2fe986SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3727aa2fe986SMatthias Ringwald #endif 3728a355334eSMatthias Ringwald // validate encryption key size 3729a355334eSMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE_V2) { 3730a355334eSMatthias Ringwald uint8_t encryption_key_size = hci_event_encryption_change_v2_get_encryption_key_size(packet); 3731a355334eSMatthias Ringwald // already got encryption key size 3732a355334eSMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, encryption_key_size); 3733a355334eSMatthias Ringwald } else { 37341ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_ENCRYPTION_KEY_SIZE)) { 37356e058d3fSMatthias Ringwald // For Classic, we need to validate encryption key size first, if possible (== supported by Controller) 3736573897a0SMatthias Ringwald conn->bonding_flags |= BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 37376e058d3fSMatthias Ringwald } else { 37386e058d3fSMatthias Ringwald // if not, pretend everything is perfect 37399866fdc7SMatthias Ringwald hci_handle_read_encryption_key_size_complete(conn, 16); 37406e058d3fSMatthias Ringwald } 3741573897a0SMatthias Ringwald } 3742a355334eSMatthias Ringwald } 3743573897a0SMatthias Ringwald #endif 3744f0944df2S[email protected] } else { 37458daf94bcSMatthias Ringwald conn->authentication_flags &= ~AUTH_FLAG_CONNECTION_ENCRYPTED; 3746f0944df2S[email protected] } 37475999ee11SMatthias Ringwald } else { 37485999ee11SMatthias Ringwald uint8_t status = hci_event_encryption_change_get_status(packet); 37495999ee11SMatthias Ringwald if ((conn->bonding_flags & BONDING_DEDICATED) != 0){ 37505999ee11SMatthias Ringwald conn->bonding_flags &= ~BONDING_DEDICATED; 37515999ee11SMatthias Ringwald conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 37525999ee11SMatthias Ringwald conn->bonding_status = status; 37535999ee11SMatthias Ringwald } 3754ad83dc6aS[email protected] } 3755573897a0SMatthias Ringwald 3756f0944df2S[email protected] break; 3757f0944df2S[email protected] 375835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 37591eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 3760abdad579SMatthias Ringwald handle = hci_event_authentication_complete_get_connection_handle(packet); 37611eb2563eS[email protected] conn = hci_connection_for_handle(handle); 37621eb2563eS[email protected] if (!conn) break; 3763ad83dc6aS[email protected] 3764dbd5dcc3SMatthias Ringwald // clear authentication active flag 3765dbd5dcc3SMatthias Ringwald conn->bonding_flags &= ~BONDING_SENT_AUTHENTICATE_REQUEST; 37661714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_authentication_complete_get_status(packet)); 3767dbd5dcc3SMatthias Ringwald 3768abdad579SMatthias Ringwald // authenticated only if auth status == 0 3769abdad579SMatthias Ringwald if (hci_event_authentication_complete_get_status(packet) == 0){ 3770abdad579SMatthias Ringwald // authenticated 37718daf94bcSMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3772abdad579SMatthias Ringwald 3773131ef17aSMatthias Ringwald // If not already encrypted, start encryption 37748daf94bcSMatthias Ringwald if ((conn->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0){ 37751eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 3776ad83dc6aS[email protected] break; 3777ad83dc6aS[email protected] } 3778abdad579SMatthias Ringwald } 3779abdad579SMatthias Ringwald 3780abdad579SMatthias Ringwald // emit updated security level 37811eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 37821eb2563eS[email protected] break; 37831714cbbdSMatthias Ringwald 37841714cbbdSMatthias Ringwald case HCI_EVENT_SIMPLE_PAIRING_COMPLETE: 37851714cbbdSMatthias Ringwald hci_event_simple_pairing_complete_get_bd_addr(packet, addr); 37861714cbbdSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 37871714cbbdSMatthias Ringwald if (!conn) break; 37881714cbbdSMatthias Ringwald 3789aa2fe986SMatthias Ringwald // treat successfully paired connection as authenticated 3790aa2fe986SMatthias Ringwald if (hci_event_simple_pairing_complete_get_status(packet) == ERROR_CODE_SUCCESS){ 3791f3aafff1SMatthias Ringwald conn->authentication_flags |= AUTH_FLAG_CONNECTION_AUTHENTICATED; 3792aa2fe986SMatthias Ringwald } 3793aa2fe986SMatthias Ringwald 37941714cbbdSMatthias Ringwald hci_pairing_complete(conn, hci_event_simple_pairing_complete_get_status(packet)); 37951714cbbdSMatthias Ringwald break; 379635454696SMatthias Ringwald #endif 379734d2123cS[email protected] 379886805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 3799ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 3800ccda6e14S[email protected] // see end of function, too. 3801a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 3802a4f30ec0S[email protected] if (packet[2]) break; // status != 0 3803f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 380481d2bdb2SMatthias Ringwald // drop outgoing ACL fragments if it is for closed connection and release buffer if tx not active 38054ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 3806c6a37cfdSMatthias Ringwald if (handle == READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 38074ea43905SMatthias Ringwald int release_buffer = hci_stack->acl_fragmentation_tx_active == 0u; 380881d2bdb2SMatthias Ringwald log_info("drop fragmented ACL data for closed connection, release buffer %u", release_buffer); 3809c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_total_size = 0; 3810c6a37cfdSMatthias Ringwald hci_stack->acl_fragmentation_pos = 0; 381181d2bdb2SMatthias Ringwald if (release_buffer){ 381281d2bdb2SMatthias Ringwald hci_release_packet_buffer(); 381381d2bdb2SMatthias Ringwald } 3814c6a37cfdSMatthias Ringwald } 3815c6a37cfdSMatthias Ringwald } 381635454696SMatthias Ringwald 38174ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3818098c2716SMatthias Ringwald // drop outgoing ISO fragments if it is for closed connection and release buffer if tx not active 3819098c2716SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size > 0u) { 3820098c2716SMatthias Ringwald if (handle == READ_ISO_CONNECTION_HANDLE(hci_stack->hci_packet_buffer)){ 3821098c2716SMatthias Ringwald int release_buffer = hci_stack->iso_fragmentation_tx_active == 0u; 3822098c2716SMatthias Ringwald log_info("drop fragmented ISO data for closed connection, release buffer %u", release_buffer); 3823098c2716SMatthias Ringwald hci_stack->iso_fragmentation_total_size = 0; 3824098c2716SMatthias Ringwald hci_stack->iso_fragmentation_pos = 0; 3825098c2716SMatthias Ringwald if (release_buffer){ 3826098c2716SMatthias Ringwald hci_release_packet_buffer(); 3827098c2716SMatthias Ringwald } 3828098c2716SMatthias Ringwald } 3829098c2716SMatthias Ringwald } 3830098c2716SMatthias Ringwald 38314ae8623eSMatthias Ringwald // finalize iso stream if handle matches 383215a15be3SMatthias Ringwald iso_stream = hci_iso_stream_for_con_handle(handle); 38334ae8623eSMatthias Ringwald if (iso_stream != NULL){ 38344ae8623eSMatthias Ringwald hci_iso_stream_finalize(iso_stream); 38354ae8623eSMatthias Ringwald break; 38364ae8623eSMatthias Ringwald } 38374ae8623eSMatthias Ringwald #endif 38384ae8623eSMatthias Ringwald 3839c6a37cfdSMatthias Ringwald conn = hci_connection_for_handle(handle); 3840c6a37cfdSMatthias Ringwald if (!conn) break; 38411714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 38421714cbbdSMatthias Ringwald // pairing failed if it was ongoing 38431714cbbdSMatthias Ringwald hci_pairing_complete(conn, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 38441714cbbdSMatthias Ringwald #endif 3845046ec007SMatthias Ringwald 3846046ec007SMatthias Ringwald // emit dedicatd bonding event 3847046ec007SMatthias Ringwald if (conn->bonding_flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 3848046ec007SMatthias Ringwald hci_emit_dedicated_bonding_result(conn->address, conn->bonding_status); 3849046ec007SMatthias Ringwald } 3850046ec007SMatthias Ringwald 3851459d27b9SMatthias Ringwald // mark connection for shutdown, stop timers, reset state 3852459d27b9SMatthias Ringwald conn->state = RECEIVED_DISCONNECTION_COMPLETE; 3853459d27b9SMatthias Ringwald hci_connection_stop_timer(conn); 3854459d27b9SMatthias Ringwald hci_connection_init(conn); 3855459d27b9SMatthias Ringwald 385635454696SMatthias Ringwald #ifdef ENABLE_BLE 3857d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 3858046ec007SMatthias Ringwald // re-enable advertisements for le connections if active 38592b6ab3e6SMatthias Ringwald if (hci_is_le_connection(conn)){ 3860bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 38619a2e4658SMatthias Ringwald } 386235454696SMatthias Ringwald #endif 3863d70217a2SMatthias Ringwald #endif 3864ccda6e14S[email protected] break; 38656772a24cSmatthias.ringwald 3866c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 3867313e5f9cSMatthias Ringwald log_error("Hardware Error: 0x%02x", packet[2]); 3868d23838ecSMatthias Ringwald if (hci_stack->hardware_error_callback){ 3869c2e1fa60SMatthias Ringwald (*hci_stack->hardware_error_callback)(packet[2]); 38707586ee35S[email protected] } else { 38717586ee35S[email protected] // if no special requests, just reboot stack 38727586ee35S[email protected] hci_power_control_off(); 38737586ee35S[email protected] hci_power_control_on(); 3874c68bdf90Smatthias.ringwald } 3875c68bdf90Smatthias.ringwald break; 3876c68bdf90Smatthias.ringwald 38770e6f3837SMatthias Ringwald #ifdef ENABLE_CLASSIC 38785cf766e8SMatthias Ringwald case HCI_EVENT_ROLE_CHANGE: 38795cf766e8SMatthias Ringwald if (packet[2]) break; // status != 0 3880c4c88f1bSJakob Krantz reverse_bd_addr(&packet[3], addr); 3881f16129ceSMatthias Ringwald addr_type = BD_ADDR_TYPE_ACL; 3882c4c88f1bSJakob Krantz conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 3883c4c88f1bSJakob Krantz if (!conn) break; 38845cf766e8SMatthias Ringwald conn->role = packet[9]; 38855cf766e8SMatthias Ringwald break; 38860e6f3837SMatthias Ringwald #endif 38875cf766e8SMatthias Ringwald 388863fa3374SMatthias Ringwald case HCI_EVENT_TRANSPORT_PACKET_SENT: 3889098c2716SMatthias Ringwald // release packet buffer only for asynchronous transport and if there are not further fragments 38904fa24b5fS[email protected] if (hci_transport_synchronous()) { 389163fa3374SMatthias Ringwald log_error("Synchronous HCI Transport shouldn't send HCI_EVENT_TRANSPORT_PACKET_SENT"); 38924fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 38934fa24b5fS[email protected] } 389481d2bdb2SMatthias Ringwald hci_stack->acl_fragmentation_tx_active = 0; 3895098c2716SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 3896098c2716SMatthias Ringwald hci_stack->iso_fragmentation_tx_active = 0; 3897098c2716SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size) break; 3898098c2716SMatthias Ringwald #endif 3899d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 3900d051460cS[email protected] hci_release_packet_buffer(); 3901701e3307SMatthias Ringwald 39025ade6657SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 39035ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 39045ade6657SMatthias Ringwald #endif 390563fa3374SMatthias Ringwald // L2CAP receives this event via the hci_emit_event below 390663fa3374SMatthias Ringwald 390735454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 390863fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 3909701e3307SMatthias Ringwald hci_notify_if_sco_can_send_now(); 391035454696SMatthias Ringwald #endif 39116b4af23dS[email protected] break; 39126b4af23dS[email protected] 391335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 391463fa3374SMatthias Ringwald case HCI_EVENT_SCO_CAN_SEND_NOW: 391563fa3374SMatthias Ringwald // For SCO, we do the can_send_now_check here 39161972f31fSMatthias Ringwald hci_stack->sco_can_send_now = true; 391763fa3374SMatthias Ringwald hci_notify_if_sco_can_send_now(); 391863fa3374SMatthias Ringwald return; 39191cfb383eSMatthias Ringwald 39201cfb383eSMatthias Ringwald // explode inquriy results for easier consumption 39211cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 39221cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 39231cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 39249784dac1SMatthias Ringwald gap_inquiry_explode(packet, size); 39251cfb383eSMatthias Ringwald break; 392635454696SMatthias Ringwald #endif 392763fa3374SMatthias Ringwald 3928a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 39295909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 39305909f7f2Smatthias.ringwald switch (packet[2]){ 3931d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 393257c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 39336fab74dbSMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 393457c9da5bS[email protected] le_handle_advertisement_report(packet, size); 39357bdc6798S[email protected] break; 3936a0698cb4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 3937a0698cb4SMatthias Ringwald case HCI_SUBEVENT_LE_EXTENDED_ADVERTISING_REPORT: 3938a0698cb4SMatthias Ringwald if (!hci_stack->le_scanning_enabled) break; 3939a0698cb4SMatthias Ringwald le_handle_extended_advertisement_report(packet, size); 3940a0698cb4SMatthias Ringwald break; 3941ce56b945SMatthias Ringwald case HCI_SUBEVENT_LE_PERIODIC_ADVERTISING_SYNC_ESTABLISHMENT: 3942ce56b945SMatthias Ringwald hci_stack->le_periodic_sync_request = LE_CONNECTING_IDLE; 3943ce56b945SMatthias Ringwald hci_stack->le_periodic_sync_state = LE_CONNECTING_IDLE; 3944ce56b945SMatthias Ringwald break; 3945a0698cb4SMatthias Ringwald #endif 3946d70217a2SMatthias Ringwald #endif 39475909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 39480ce3f217SMatthias Ringwald event_handle_le_connection_complete(packet); 39495909f7f2Smatthias.ringwald break; 39505909f7f2Smatthias.ringwald 3951f8fbdce0SMatthias Ringwald // log_info("LE buffer size: %u, count %u", little_endian_read_16(packet,6), packet[8]); 3952c9db5c21SMilanka Ringwald case HCI_SUBEVENT_LE_CONNECTION_UPDATE_COMPLETE: 3953c9db5c21SMilanka Ringwald handle = hci_subevent_le_connection_update_complete_get_connection_handle(packet); 3954c9db5c21SMilanka Ringwald conn = hci_connection_for_handle(handle); 3955c9db5c21SMilanka Ringwald if (!conn) break; 3956c9db5c21SMilanka Ringwald conn->le_connection_interval = hci_subevent_le_connection_update_complete_get_conn_interval(packet); 3957c9db5c21SMilanka Ringwald break; 395865a46ef3S[email protected] 395973cd8a2aSMatthias Ringwald case HCI_SUBEVENT_LE_REMOTE_CONNECTION_PARAMETER_REQUEST: 396073cd8a2aSMatthias Ringwald // connection 396173cd8a2aSMatthias Ringwald handle = hci_subevent_le_remote_connection_parameter_request_get_connection_handle(packet); 396273cd8a2aSMatthias Ringwald conn = hci_connection_for_handle(handle); 396373cd8a2aSMatthias Ringwald if (conn) { 396473cd8a2aSMatthias Ringwald // read arguments 396573cd8a2aSMatthias Ringwald uint16_t le_conn_interval_min = hci_subevent_le_remote_connection_parameter_request_get_interval_min(packet); 396673cd8a2aSMatthias Ringwald uint16_t le_conn_interval_max = hci_subevent_le_remote_connection_parameter_request_get_interval_max(packet); 396773cd8a2aSMatthias Ringwald uint16_t le_conn_latency = hci_subevent_le_remote_connection_parameter_request_get_latency(packet); 396873cd8a2aSMatthias Ringwald uint16_t le_supervision_timeout = hci_subevent_le_remote_connection_parameter_request_get_timeout(packet); 396973cd8a2aSMatthias Ringwald 397073cd8a2aSMatthias Ringwald // validate against current connection parameter range 397173cd8a2aSMatthias Ringwald le_connection_parameter_range_t existing_range; 397273cd8a2aSMatthias Ringwald gap_get_connection_parameter_range(&existing_range); 397373cd8a2aSMatthias 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); 397473cd8a2aSMatthias Ringwald if (update_parameter){ 397573cd8a2aSMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_REPLY; 397673cd8a2aSMatthias Ringwald conn->le_conn_interval_min = le_conn_interval_min; 397773cd8a2aSMatthias Ringwald conn->le_conn_interval_max = le_conn_interval_max; 397873cd8a2aSMatthias Ringwald conn->le_conn_latency = le_conn_latency; 397973cd8a2aSMatthias Ringwald conn->le_supervision_timeout = le_supervision_timeout; 398073cd8a2aSMatthias Ringwald } else { 3981c3898ca4SMatthias Ringwald conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NEGATIVE_REPLY; 398273cd8a2aSMatthias Ringwald } 398373cd8a2aSMatthias Ringwald } 398473cd8a2aSMatthias Ringwald break; 39850f3b27c5SMatthias Ringwald #ifdef ENABLE_LE_LIMIT_ACL_FRAGMENT_BY_MAX_OCTETS 39860f3b27c5SMatthias Ringwald case HCI_SUBEVENT_LE_DATA_LENGTH_CHANGE: 39870f3b27c5SMatthias Ringwald handle = hci_subevent_le_data_length_change_get_connection_handle(packet); 39880f3b27c5SMatthias Ringwald conn = hci_connection_for_handle(handle); 39890f3b27c5SMatthias Ringwald if (conn) { 39900f3b27c5SMatthias Ringwald conn->le_max_tx_octets = hci_subevent_le_data_length_change_get_max_tx_octets(packet); 39910f3b27c5SMatthias Ringwald } 39920f3b27c5SMatthias Ringwald break; 39930f3b27c5SMatthias Ringwald #endif 39944ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 39954ae8623eSMatthias Ringwald case HCI_SUBEVENT_LE_CIS_ESTABLISHED: 39968e58e4f7SMatthias Ringwald if (hci_stack->iso_active_operation_type == HCI_ISO_TYPE_CIS){ 39978e58e4f7SMatthias Ringwald le_audio_cig_t * cig = hci_cig_for_id(hci_stack->iso_active_operation_group_id); 39988e58e4f7SMatthias Ringwald btstack_assert(cig != NULL); 39994ae8623eSMatthias Ringwald handle = hci_subevent_le_cis_established_get_connection_handle(packet); 40004ae8623eSMatthias Ringwald uint8_t status = hci_subevent_le_cis_established_get_status(packet); 40018e58e4f7SMatthias Ringwald 40028e58e4f7SMatthias Ringwald // update iso stream state 40038e58e4f7SMatthias Ringwald iso_stream = hci_iso_stream_for_con_handle(handle); 40048e58e4f7SMatthias Ringwald btstack_assert(iso_stream != NULL); 40054ae8623eSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 40064ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_ESTABLISHED; 40074ae8623eSMatthias Ringwald } else { 40088e58e4f7SMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_IDLE; 40094ae8623eSMatthias Ringwald } 40108e58e4f7SMatthias Ringwald 40118e58e4f7SMatthias Ringwald // update cig state 4012f06bb90fSMatthias Ringwald uint8_t i; 4013f06bb90fSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 4014f06bb90fSMatthias Ringwald if (cig->cis_con_handles[i] == handle){ 40153abcd16cSMatthias Ringwald cig->cis_setup_active[i] = false; 4016f9306a1aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 40178e58e4f7SMatthias Ringwald cig->cis_setup_active[i] = false; 4018f9306a1aSMatthias Ringwald cig->cis_established[i] = true; 4019f9306a1aSMatthias Ringwald } else { 40208e58e4f7SMatthias Ringwald hci_emit_cis_created(cig->cig_id, handle, status); 4021f06bb90fSMatthias Ringwald } 4022f06bb90fSMatthias Ringwald } 4023f06bb90fSMatthias Ringwald } 40248e58e4f7SMatthias Ringwald 40258e58e4f7SMatthias Ringwald // trigger iso path setup if complete 40268e58e4f7SMatthias Ringwald bool setup_active = false; 4027f9306a1aSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 40288e58e4f7SMatthias Ringwald setup_active |= cig->cis_setup_active[i]; 4029f9306a1aSMatthias Ringwald } 40308e58e4f7SMatthias Ringwald if (setup_active == false){ 4031f9306a1aSMatthias Ringwald cig->state_vars.next_cis = 0; 4032f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_SETUP_ISO_PATH; 4033*d75a6bbfSMatthias Ringwald hci_stack->iso_active_operation_group_id = HCI_ISO_GROUP_ID_INVALID; 4034f06bb90fSMatthias Ringwald } 40354ae8623eSMatthias Ringwald } 40367aa35f6aSMatthias Ringwald break; 40377aa35f6aSMatthias Ringwald case HCI_SUBEVENT_LE_CREATE_BIG_COMPLETE: 4038*d75a6bbfSMatthias Ringwald hci_stack->iso_active_operation_group_id = HCI_ISO_GROUP_ID_INVALID; 40397aa35f6aSMatthias Ringwald big = hci_big_for_handle(packet[4]); 40407aa35f6aSMatthias Ringwald if (big != NULL){ 40417aa35f6aSMatthias Ringwald uint8_t status = packet[3]; 40427aa35f6aSMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 40437aa35f6aSMatthias Ringwald // store bis_con_handles and trigger iso path setup 40447aa35f6aSMatthias Ringwald uint8_t num_bis = btstack_min(MAX_NR_BIS, packet[20]); 40457aa35f6aSMatthias Ringwald uint8_t i; 40467aa35f6aSMatthias Ringwald for (i=0;i<num_bis;i++){ 4047fb16346aSMatthias Ringwald hci_con_handle_t bis_handle = (hci_con_handle_t) little_endian_read_16(packet, 21 + (2 * i)); 4048fb16346aSMatthias Ringwald big->bis_con_handles[i] = bis_handle; 4049fb16346aSMatthias Ringwald // assign bis handle 4050fb16346aSMatthias Ringwald btstack_linked_list_iterator_t it; 4051fb16346aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 4052fb16346aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4053fb16346aSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 4054fb16346aSMatthias Ringwald if ((iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) && 40558d72db10SMatthias Ringwald (iso_stream->group_id == big->big_handle)){ 4056fb16346aSMatthias Ringwald iso_stream->con_handle = bis_handle; 4057fb16346aSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_ESTABLISHED; 4058cd1139aaSMatthias Ringwald break; 4059fb16346aSMatthias Ringwald } 4060fb16346aSMatthias Ringwald } 40617aa35f6aSMatthias Ringwald } 40622a6c0f82SMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 40637aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 40647aa35f6aSMatthias Ringwald big->state_vars.next_bis = 0; 40652a6c0f82SMatthias Ringwald } 40667aa35f6aSMatthias Ringwald } else { 40672a6c0f82SMatthias Ringwald // create BIG failed or has been stopped by us 4068778463d0SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_BIS, big->big_handle); 40697aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 40702a6c0f82SMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED){ 40717aa35f6aSMatthias Ringwald hci_emit_big_created(big, status); 40722a6c0f82SMatthias Ringwald } else { 40732a6c0f82SMatthias Ringwald hci_emit_big_terminated(big); 40742a6c0f82SMatthias Ringwald } 40757aa35f6aSMatthias Ringwald } 40767aa35f6aSMatthias Ringwald } 40777aa35f6aSMatthias Ringwald break; 40787aa35f6aSMatthias Ringwald case HCI_SUBEVENT_LE_TERMINATE_BIG_COMPLETE: 40797aa35f6aSMatthias Ringwald big = hci_big_for_handle(hci_subevent_le_terminate_big_complete_get_big_handle(packet)); 40807aa35f6aSMatthias Ringwald if (big != NULL){ 4081fb16346aSMatthias Ringwald // finalize associated ISO streams 4082fb16346aSMatthias Ringwald btstack_linked_list_iterator_t it; 4083fb16346aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 4084fb16346aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 4085fb16346aSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 40868d72db10SMatthias Ringwald if (iso_stream->group_id == big->big_handle){ 40878d72db10SMatthias Ringwald log_info("BIG Terminated, big_handle 0x%02x, con handle 0x%04x", iso_stream->group_id, iso_stream->con_handle); 4088fb16346aSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 4089fb16346aSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 4090fb16346aSMatthias Ringwald } 4091fb16346aSMatthias Ringwald } 40927aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 40937aa35f6aSMatthias Ringwald switch (big->state){ 40947aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED: 40957aa35f6aSMatthias Ringwald hci_emit_big_created(big, big->state_vars.status); 40967aa35f6aSMatthias Ringwald break; 40977aa35f6aSMatthias Ringwald default: 40987aa35f6aSMatthias Ringwald hci_emit_big_terminated(big); 40997aa35f6aSMatthias Ringwald break; 41007aa35f6aSMatthias Ringwald } 41017aa35f6aSMatthias Ringwald } 41027aa35f6aSMatthias Ringwald break; 41032a6c0f82SMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_ESTABLISHED: 41042a6c0f82SMatthias Ringwald big_sync = hci_big_sync_for_handle(packet[4]); 41052a6c0f82SMatthias Ringwald if (big_sync != NULL){ 41062a6c0f82SMatthias Ringwald uint8_t status = packet[3]; 4107d04a9afcSMatthias Ringwald uint8_t big_handle = packet[4]; 41082a6c0f82SMatthias Ringwald if (status == ERROR_CODE_SUCCESS){ 41092a6c0f82SMatthias Ringwald // store bis_con_handles and trigger iso path setup 41102a6c0f82SMatthias Ringwald uint8_t num_bis = btstack_min(MAX_NR_BIS, packet[16]); 41112a6c0f82SMatthias Ringwald uint8_t i; 41122a6c0f82SMatthias Ringwald for (i=0;i<num_bis;i++){ 41132a6c0f82SMatthias Ringwald big_sync->bis_con_handles[i] = little_endian_read_16(packet, 17 + (2 * i)); 41142a6c0f82SMatthias Ringwald } 41152a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 41162a6c0f82SMatthias Ringwald // trigger iso path setup 41172a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_SETUP_ISO_PATH; 41182a6c0f82SMatthias Ringwald big_sync->state_vars.next_bis = 0; 41192a6c0f82SMatthias Ringwald } 41202a6c0f82SMatthias Ringwald } else { 41212a6c0f82SMatthias Ringwald // create BIG Sync failed or has been stopped by us 41222a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 41232a6c0f82SMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_ESTABLISHED) { 41242a6c0f82SMatthias Ringwald hci_emit_big_sync_created(big_sync, status); 41252a6c0f82SMatthias Ringwald } else { 4126d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 41272a6c0f82SMatthias Ringwald } 41282a6c0f82SMatthias Ringwald } 41292a6c0f82SMatthias Ringwald } 41302a6c0f82SMatthias Ringwald break; 41312a6c0f82SMatthias Ringwald case HCI_SUBEVENT_LE_BIG_SYNC_LOST: 41322a6c0f82SMatthias Ringwald big_sync = hci_big_sync_for_handle(packet[4]); 41332a6c0f82SMatthias Ringwald if (big_sync != NULL){ 4134d04a9afcSMatthias Ringwald uint8_t big_handle = packet[4]; 41352a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 4136d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 41372a6c0f82SMatthias Ringwald } 41382a6c0f82SMatthias Ringwald break; 41394ae8623eSMatthias Ringwald #endif 41405909f7f2Smatthias.ringwald default: 41415909f7f2Smatthias.ringwald break; 41425909f7f2Smatthias.ringwald } 41435909f7f2Smatthias.ringwald break; 41445909f7f2Smatthias.ringwald #endif 41450b36101dSMatthias Ringwald case HCI_EVENT_VENDOR_SPECIFIC: 41460b36101dSMatthias Ringwald // Vendor specific commands often create vendor specific event instead of num completed packets 41470b36101dSMatthias Ringwald // To avoid getting stuck as num_cmds_packets is zero, reset it to 1 for controllers with this behaviour 41480b36101dSMatthias Ringwald switch (hci_stack->manufacturer){ 41490b36101dSMatthias Ringwald case BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO: 41500b36101dSMatthias Ringwald hci_stack->num_cmd_packets = 1; 41510b36101dSMatthias Ringwald break; 41520b36101dSMatthias Ringwald default: 41530b36101dSMatthias Ringwald break; 41540b36101dSMatthias Ringwald } 41550b36101dSMatthias Ringwald break; 41566772a24cSmatthias.ringwald default: 41576772a24cSmatthias.ringwald break; 4158fe1ed1b8Smatthias.ringwald } 4159fe1ed1b8Smatthias.ringwald 416067c6c9dcSMatthias Ringwald handle_event_for_current_stack_state(packet, size); 416189db417bSmatthias.ringwald 416286805605S[email protected] // notify upper stack 4163d6b06661SMatthias Ringwald hci_emit_event(packet, size, 0); // don't dump, already happened in packet handler 416494ab26f8Smatthias.ringwald 416586805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 4166797b2a3fSMatthias Ringwald if ((hci_event_packet_get_type(packet) == HCI_EVENT_DISCONNECTION_COMPLETE) && (packet[2] == 0)){ 4167f8fbdce0SMatthias Ringwald handle = little_endian_read_16(packet, 3); 416805ae8de3SMatthias Ringwald hci_connection_t * aConn = hci_connection_for_handle(handle); 4169046ec007SMatthias Ringwald // discard connection if app did not trigger a reconnect in the event handler 4170797b2a3fSMatthias Ringwald if (aConn && aConn->state == RECEIVED_DISCONNECTION_COMPLETE){ 417105ae8de3SMatthias Ringwald hci_shutdown_connection(aConn); 4172b0136355SMatthias Ringwald } 41738ad9cf99SMatthias Ringwald #ifdef ENABLE_CONTROLLER_DUMP_PACKETS 41748ad9cf99SMatthias Ringwald hci_controller_dump_packets(); 41758ad9cf99SMatthias Ringwald #endif 417686805605S[email protected] } 417786805605S[email protected] 417894ab26f8Smatthias.ringwald // execute main loop 417994ab26f8Smatthias.ringwald hci_run(); 418016833f0aSmatthias.ringwald } 418116833f0aSmatthias.ringwald 418235454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 41836f28d2eeSMatthias Ringwald 41846be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 41856f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts); 41866f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn); 41876f28d2eeSMatthias Ringwald 41886f28d2eeSMatthias Ringwald static void sco_tx_timeout_handler(btstack_timer_source_t * ts){ 418959c33575SMatthias Ringwald log_debug("SCO TX Timeout"); 41905a6b2dbdSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts); 41916f28d2eeSMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 41926f28d2eeSMatthias Ringwald if (!conn) return; 41936f28d2eeSMatthias Ringwald 4194afaddd67SMatthias Ringwald // trigger send 4195afaddd67SMatthias Ringwald conn->sco_tx_ready = 1; 4196e4157653SMatthias Ringwald // extra packet if CVSD but SCO buffer is too short 4197a1df452eSMatthias Ringwald if (((hci_stack->sco_voice_setting_active & 0x03) != 0x03) && (hci_stack->sco_data_packet_length < 123)){ 4198e4157653SMatthias Ringwald conn->sco_tx_ready++; 4199e4157653SMatthias Ringwald } 4200afaddd67SMatthias Ringwald hci_notify_if_sco_can_send_now(); 42016f28d2eeSMatthias Ringwald } 42026f28d2eeSMatthias Ringwald 420359c33575SMatthias Ringwald 420459c33575SMatthias Ringwald #define SCO_TX_AFTER_RX_MS (6) 420559c33575SMatthias Ringwald 42066f28d2eeSMatthias Ringwald static void sco_schedule_tx(hci_connection_t * conn){ 420759c33575SMatthias Ringwald 42086f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 420959c33575SMatthias Ringwald uint32_t sco_tx_ms = conn->sco_rx_ms + SCO_TX_AFTER_RX_MS; 421059c33575SMatthias Ringwald int time_delta_ms = sco_tx_ms - now; 421159c33575SMatthias Ringwald 421259c33575SMatthias Ringwald btstack_timer_source_t * timer = (conn->sco_rx_count & 1) ? &conn->timeout : &conn->timeout_sco; 421359c33575SMatthias Ringwald 421459c33575SMatthias Ringwald // log_error("SCO TX at %u in %u", (int) sco_tx_ms, time_delta_ms); 4215ddbec4ceSMatthias Ringwald btstack_run_loop_remove_timer(timer); 421659c33575SMatthias Ringwald btstack_run_loop_set_timer(timer, time_delta_ms); 421759c33575SMatthias Ringwald btstack_run_loop_set_timer_context(timer, (void *) (uintptr_t) conn->con_handle); 421859c33575SMatthias Ringwald btstack_run_loop_set_timer_handler(timer, &sco_tx_timeout_handler); 421959c33575SMatthias Ringwald btstack_run_loop_add_timer(timer); 42206f28d2eeSMatthias Ringwald } 42216be3cf7fSMatthias Ringwald #endif 42226f28d2eeSMatthias Ringwald 4223c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 42240b3f95dfSMatthias Ringwald // lookup connection struct 42252b838201SMatthias Ringwald hci_con_handle_t con_handle = READ_SCO_CONNECTION_HANDLE(packet); 42262b838201SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 42270b3f95dfSMatthias Ringwald if (!conn) return; 42280b3f95dfSMatthias Ringwald 42296be3cf7fSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 4230760b20efSMatthias Ringwald // CSR 8811 prefixes 60 byte SCO packet in transparent mode with 20 zero bytes -> skip first 20 payload bytes 4231760b20efSMatthias Ringwald if (hci_stack->manufacturer == BLUETOOTH_COMPANY_ID_CAMBRIDGE_SILICON_RADIO){ 4232a1df452eSMatthias Ringwald if ((size == 83) && ((hci_stack->sco_voice_setting_active & 0x03) == 0x03)){ 4233760b20efSMatthias Ringwald packet[2] = 0x3c; 4234760b20efSMatthias Ringwald memmove(&packet[3], &packet[23], 63); 4235760b20efSMatthias Ringwald size = 63; 42360b3f95dfSMatthias Ringwald } 42370b3f95dfSMatthias Ringwald } 4238760b20efSMatthias Ringwald 4239f234b250SMatthias Ringwald if (hci_have_usb_transport()){ 4240f234b250SMatthias Ringwald // Nothing to do 4241f234b250SMatthias Ringwald } else { 424249205f5dSMatthias 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); 4243760b20efSMatthias Ringwald if (hci_stack->synchronous_flow_control_enabled == 0){ 42446f28d2eeSMatthias Ringwald uint32_t now = btstack_run_loop_get_time_ms(); 424559c33575SMatthias Ringwald 424659c33575SMatthias Ringwald if (!conn->sco_rx_valid){ 424759c33575SMatthias Ringwald // ignore first 10 packets 42486f28d2eeSMatthias Ringwald conn->sco_rx_count++; 424959c33575SMatthias Ringwald // log_debug("sco rx count %u", conn->sco_rx_count); 425059c33575SMatthias Ringwald if (conn->sco_rx_count == 10) { 425159c33575SMatthias Ringwald // use first timestamp as is and pretent it just started 42526f28d2eeSMatthias Ringwald conn->sco_rx_ms = now; 42536f28d2eeSMatthias Ringwald conn->sco_rx_valid = 1; 425459c33575SMatthias Ringwald conn->sco_rx_count = 0; 425559c33575SMatthias Ringwald sco_schedule_tx(conn); 425659c33575SMatthias Ringwald } 425759c33575SMatthias Ringwald } else { 425859c33575SMatthias Ringwald // track expected arrival timme 425959c33575SMatthias Ringwald conn->sco_rx_count++; 426059c33575SMatthias Ringwald conn->sco_rx_ms += 7; 426159c33575SMatthias Ringwald int delta = (int32_t) (now - conn->sco_rx_ms); 426259c33575SMatthias Ringwald if (delta > 0){ 426359c33575SMatthias Ringwald conn->sco_rx_ms++; 426459c33575SMatthias Ringwald } 426559c33575SMatthias Ringwald // log_debug("sco rx %u", conn->sco_rx_ms); 42666f28d2eeSMatthias Ringwald sco_schedule_tx(conn); 42676f28d2eeSMatthias Ringwald } 4268760b20efSMatthias Ringwald } 4269f234b250SMatthias Ringwald } 42706be3cf7fSMatthias Ringwald #endif 42716be3cf7fSMatthias Ringwald 42720b3f95dfSMatthias Ringwald // deliver to app 42730b3f95dfSMatthias Ringwald if (hci_stack->sco_packet_handler) { 42740b3f95dfSMatthias Ringwald hci_stack->sco_packet_handler(HCI_SCO_DATA_PACKET, 0, packet, size); 42750b3f95dfSMatthias Ringwald } 42760b3f95dfSMatthias Ringwald 4277ed325439SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 4278ed325439SMatthias Ringwald // We can send one packet for each received packet 4279ed325439SMatthias Ringwald conn->sco_tx_ready++; 4280ed325439SMatthias Ringwald hci_notify_if_sco_can_send_now(); 4281ed325439SMatthias Ringwald #endif 4282ed325439SMatthias Ringwald 42830b3f95dfSMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 42842b838201SMatthias Ringwald conn->num_packets_completed++; 42852b838201SMatthias Ringwald hci_stack->host_completed_packets = 1; 42862b838201SMatthias Ringwald hci_run(); 42872b838201SMatthias Ringwald #endif 4288c91d150bS[email protected] } 428935454696SMatthias Ringwald #endif 4290c91d150bS[email protected] 42910a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 42925bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 429310e830c9Smatthias.ringwald switch (packet_type) { 429410e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 429510e830c9Smatthias.ringwald event_handler(packet, size); 429610e830c9Smatthias.ringwald break; 429710e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 429810e830c9Smatthias.ringwald acl_handler(packet, size); 429910e830c9Smatthias.ringwald break; 430035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4301c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 4302c91d150bS[email protected] sco_handler(packet, size); 4303202c8a4cSMatthias Ringwald break; 430435454696SMatthias Ringwald #endif 43050a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 43065027c498SMatthias Ringwald case HCI_ISO_DATA_PACKET: 4307d39e4f95SMatthias Ringwald hci_iso_packet_handler(packet, size); 43085027c498SMatthias Ringwald break; 43095027c498SMatthias Ringwald #endif 431010e830c9Smatthias.ringwald default: 431110e830c9Smatthias.ringwald break; 431210e830c9Smatthias.ringwald } 431310e830c9Smatthias.ringwald } 431410e830c9Smatthias.ringwald 4315d6b06661SMatthias Ringwald /** 4316d6b06661SMatthias Ringwald * @brief Add event packet handler. 4317d6b06661SMatthias Ringwald */ 4318d6b06661SMatthias Ringwald void hci_add_event_handler(btstack_packet_callback_registration_t * callback_handler){ 4319d6b06661SMatthias Ringwald btstack_linked_list_add_tail(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 4320d6b06661SMatthias Ringwald } 4321d6b06661SMatthias Ringwald 432267f708e0SMatthias Ringwald /** 432367f708e0SMatthias Ringwald * @brief Remove event packet handler. 432467f708e0SMatthias Ringwald */ 432567f708e0SMatthias Ringwald void hci_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){ 432667f708e0SMatthias Ringwald btstack_linked_list_remove(&hci_stack->event_handlers, (btstack_linked_item_t*) callback_handler); 432767f708e0SMatthias Ringwald } 4328d6b06661SMatthias Ringwald 4329fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 43303d50b4baSMatthias Ringwald void hci_register_acl_packet_handler(btstack_packet_handler_t handler){ 4331fb37a842SMatthias Ringwald hci_stack->acl_packet_handler = handler; 433216833f0aSmatthias.ringwald } 433316833f0aSmatthias.ringwald 433435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 43358abbe8b5SMatthias Ringwald /** 43368abbe8b5SMatthias Ringwald * @brief Registers a packet handler for SCO data. Used for HSP and HFP profiles. 43378abbe8b5SMatthias Ringwald */ 43383d50b4baSMatthias Ringwald void hci_register_sco_packet_handler(btstack_packet_handler_t handler){ 43398abbe8b5SMatthias Ringwald hci_stack->sco_packet_handler = handler; 43408abbe8b5SMatthias Ringwald } 434135454696SMatthias Ringwald #endif 43428abbe8b5SMatthias Ringwald 43430a8cccd5SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 43445027c498SMatthias Ringwald void hci_register_iso_packet_handler(btstack_packet_handler_t handler){ 43455027c498SMatthias Ringwald hci_stack->iso_packet_handler = handler; 43465027c498SMatthias Ringwald } 43475027c498SMatthias Ringwald #endif 43485027c498SMatthias Ringwald 434971de195eSMatthias Ringwald static void hci_state_reset(void){ 4350595bdbfbS[email protected] // no connections yet 4351595bdbfbS[email protected] hci_stack->connections = NULL; 435274308b23Smatthias.ringwald 435374308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 435474308b23Smatthias.ringwald // hci_stack->discoverable = 0; 435574308b23Smatthias.ringwald // hci_stack->connectable = 0; 435674308b23Smatthias.ringwald // hci_stack->bondable = 1; 4357b95a5a35SMatthias Ringwald // hci_stack->own_addr_type = 0; 4358595bdbfbS[email protected] 435944935e40S[email protected] // buffer is free 436002c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 436144935e40S[email protected] 4362595bdbfbS[email protected] // no pending cmds 4363595bdbfbS[email protected] hci_stack->decline_reason = 0; 4364595bdbfbS[email protected] 4365c214d65bSMatthias Ringwald hci_stack->secure_connections_active = false; 4366c214d65bSMatthias Ringwald 4367bea424a5SMatthias Ringwald #ifdef ENABLE_CLASSIC 4368496bb884SMatthias Ringwald hci_stack->inquiry_lap = GAP_IAC_GENERAL_INQUIRY; 436932a12730SMatthias Ringwald hci_stack->page_timeout = 0x6000; // ca. 15 sec 437032a12730SMatthias Ringwald 4371baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic = 437259d59ecfSMatthias Ringwald GAP_TASK_SET_DEFAULT_LINK_POLICY | 437359d59ecfSMatthias Ringwald GAP_TASK_SET_CLASS_OF_DEVICE | 437459d59ecfSMatthias Ringwald GAP_TASK_SET_LOCAL_NAME | 4375bc2dcc03SMatthias Ringwald GAP_TASK_SET_EIR_DATA | 437632a12730SMatthias Ringwald GAP_TASK_WRITE_SCAN_ENABLE | 437732a12730SMatthias Ringwald GAP_TASK_WRITE_PAGE_TIMEOUT; 4378bea424a5SMatthias Ringwald #endif 4379bea424a5SMatthias Ringwald 4380cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 43818d5f0979SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 43821ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = HCI_CON_HANDLE_INVALID; 4383cf01e888SMatthias Ringwald #endif 4384cf01e888SMatthias Ringwald 4385595bdbfbS[email protected] // LE 4386b95a5a35SMatthias Ringwald #ifdef ENABLE_BLE 4387b95a5a35SMatthias Ringwald memset(hci_stack->le_random_address, 0, 6); 4388b95a5a35SMatthias Ringwald hci_stack->le_random_address_set = 0; 4389d70217a2SMatthias Ringwald #endif 4390d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4391a61834b6SMatthias Ringwald hci_stack->le_scanning_active = false; 439273799937SMatthias Ringwald hci_stack->le_scanning_param_update = true; 4393b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_IDLE; 4394d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 4395e83201bcSMatthias Ringwald hci_stack->le_whitelist_capacity = 0; 43964bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 43974bafedd5SMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = HCI_CON_HANDLE_INVALID; 43984bafedd5SMatthias Ringwald #endif 4399d70217a2SMatthias Ringwald #endif 4400a61834b6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 4401935aa76eSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 44026bf435a6SMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_PARAMS_SET) != 0){ 4403a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 4404a61834b6SMatthias Ringwald } 4405a61834b6SMatthias Ringwald if (hci_stack->le_advertisements_data != NULL){ 4406a61834b6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 4407a61834b6SMatthias Ringwald } 4408a61834b6SMatthias Ringwald #endif 440940f86dfdSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 4410*d75a6bbfSMatthias Ringwald hci_stack->iso_active_operation_group_id = HCI_ISO_GROUP_ID_INVALID; 441140f86dfdSMatthias Ringwald #endif 4412595bdbfbS[email protected] } 4413595bdbfbS[email protected] 441435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 44152d5e09d6SMatthias Ringwald /** 44162d5e09d6SMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called before power on. 44172d5e09d6SMatthias Ringwald */ 44182d5e09d6SMatthias Ringwald void hci_set_link_key_db(btstack_link_key_db_t const * link_key_db){ 44192d5e09d6SMatthias Ringwald // store and open remote device db 44202d5e09d6SMatthias Ringwald hci_stack->link_key_db = link_key_db; 44212d5e09d6SMatthias Ringwald if (hci_stack->link_key_db) { 44222d5e09d6SMatthias Ringwald hci_stack->link_key_db->open(); 44232d5e09d6SMatthias Ringwald } 44242d5e09d6SMatthias Ringwald } 442535454696SMatthias Ringwald #endif 44262d5e09d6SMatthias Ringwald 44272d5e09d6SMatthias Ringwald void hci_init(const hci_transport_t *transport, const void *config){ 4428475c8125Smatthias.ringwald 44293a9fb326S[email protected] #ifdef HAVE_MALLOC 44303a9fb326S[email protected] if (!hci_stack) { 44313a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 44323a9fb326S[email protected] } 44333a9fb326S[email protected] #else 44343a9fb326S[email protected] hci_stack = &hci_stack_static; 44353a9fb326S[email protected] #endif 443666fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 44373a9fb326S[email protected] 4438475c8125Smatthias.ringwald // reference to use transport layer implementation 44393a9fb326S[email protected] hci_stack->hci_transport = transport; 4440475c8125Smatthias.ringwald 444111e23e5fSmatthias.ringwald // reference to used config 44423a9fb326S[email protected] hci_stack->config = config; 444311e23e5fSmatthias.ringwald 444426a9b6daSMatthias Ringwald // setup pointer for outgoing packet buffer 444526a9b6daSMatthias Ringwald hci_stack->hci_packet_buffer = &hci_stack->hci_packet_buffer_data[HCI_OUTGOING_PRE_BUFFER_SIZE]; 444626a9b6daSMatthias Ringwald 44478fcba05dSmatthias.ringwald // max acl payload size defined in config.h 44483a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 44498fcba05dSmatthias.ringwald 445016833f0aSmatthias.ringwald // register packet handlers with transport 445110e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 4452f5454fc6Smatthias.ringwald 44533a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 4454e2386ba1S[email protected] 4455e2386ba1S[email protected] // class of device 44563a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 4457a45d6b9fS[email protected] 4458f20168b8Smatthias.ringwald // bondable by default 4459f20168b8Smatthias.ringwald hci_stack->bondable = 1; 4460f20168b8Smatthias.ringwald 4461e9f343c8SMatthias Ringwald #ifdef ENABLE_CLASSIC 446263168530SMatthias Ringwald // classic name 446363168530SMatthias Ringwald hci_stack->local_name = default_classic_name; 4464c4c88f1bSJakob Krantz 4465c4c88f1bSJakob Krantz // Master slave policy 4466c4c88f1bSJakob Krantz hci_stack->master_slave_policy = 1; 4467170fafaeSMatthias Ringwald 4468b4eb4420SMatthias Ringwald // Allow Role Switch 4469b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = 1; 4470b4eb4420SMatthias Ringwald 447178315a58SMatthias Ringwald // Default / minimum security level = 2 447278315a58SMatthias Ringwald hci_stack->gap_security_level = LEVEL_2; 447378315a58SMatthias Ringwald 44745dbec6b3SMatthias Ringwald // Default Security Mode 4 44755dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = GAP_SECURITY_MODE_4; 44765dbec6b3SMatthias Ringwald 4477cd345294SMatthias Ringwald // Errata-11838 mandates 7 bytes for GAP Security Level 1-3 4478cd345294SMatthias Ringwald hci_stack->gap_required_encyrption_key_size = 7; 4479d821984bSMatthias Ringwald 4480d821984bSMatthias Ringwald // Link Supervision Timeout 4481d821984bSMatthias Ringwald hci_stack->link_supervision_timeout = HCI_LINK_SUPERVISION_TIMEOUT_DEFAULT; 4482d821984bSMatthias Ringwald 4483e9f343c8SMatthias Ringwald #endif 448463168530SMatthias Ringwald 448563048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 44863a9fb326S[email protected] hci_stack->ssp_enable = 1; 44873a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 44883a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 44893a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 449069a97523S[email protected] 44915d23aae8SMatthias Ringwald // Secure Connections: enable (requires support from Controller) 44925d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = true; 44935d23aae8SMatthias Ringwald 4494fac2e2feSMatthias Ringwald // voice setting - signed 16 bit pcm data with CVSD over the air 4495fac2e2feSMatthias Ringwald hci_stack->sco_voice_setting = 0x60; 4496d950d659SMatthias Ringwald 4497831711daSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 4498831711daSMatthias Ringwald // connection parameter to use for outgoing connections 4499cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = 0x0060; // 60ms 4500cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = 0x0030; // 30ms 4501831711daSMatthias Ringwald hci_stack->le_connection_interval_min = 0x0008; // 10 ms 4502831711daSMatthias Ringwald hci_stack->le_connection_interval_max = 0x0018; // 30 ms 4503831711daSMatthias Ringwald hci_stack->le_connection_latency = 4; // 4 4504831711daSMatthias Ringwald hci_stack->le_supervision_timeout = 0x0048; // 720 ms 4505831711daSMatthias Ringwald hci_stack->le_minimum_ce_length = 2; // 1.25 ms 4506831711daSMatthias Ringwald hci_stack->le_maximum_ce_length = 0x0030; // 30 ms 45077261e5d8SMatthias Ringwald 45087261e5d8SMatthias Ringwald // default LE Scanning 45098b69e4c7SMatthias Ringwald hci_stack->le_scan_type = 0x1; // active 45108b69e4c7SMatthias Ringwald hci_stack->le_scan_interval = 0x1e0; // 300 ms 45118b69e4c7SMatthias Ringwald hci_stack->le_scan_window = 0x30; // 30 ms 4512831711daSMatthias Ringwald #endif 4513831711daSMatthias Ringwald 45142b6ab3e6SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 45152b6ab3e6SMatthias Ringwald hci_stack->le_max_number_peripheral_connections = 1; // only single connection as peripheral 45162b6ab3e6SMatthias Ringwald #endif 45172b6ab3e6SMatthias Ringwald 4518831711daSMatthias Ringwald // connection parameter range used to answer connection parameter update requests in l2cap 4519831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_min = 6; 4520831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_interval_max = 3200; 4521831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_min = 0; 4522831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_conn_latency_max = 500; 4523831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 10; 4524831711daSMatthias Ringwald hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 3200; 4525831711daSMatthias Ringwald 452601d4f05fSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 452701d4f05fSMatthias Ringwald hci_stack->iso_packets_to_queue = 1; 452801d4f05fSMatthias Ringwald #endif 452901d4f05fSMatthias Ringwald 4530595bdbfbS[email protected] hci_state_reset(); 4531475c8125Smatthias.ringwald } 4532475c8125Smatthias.ringwald 45334688f216SMatthias Ringwald void hci_deinit(void){ 4534b02cfc15SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 45354688f216SMatthias Ringwald #ifdef HAVE_MALLOC 45364688f216SMatthias Ringwald if (hci_stack) { 45374688f216SMatthias Ringwald free(hci_stack); 45384688f216SMatthias Ringwald } 45394688f216SMatthias Ringwald #endif 45404688f216SMatthias Ringwald hci_stack = NULL; 4541b5bbcbf4SMatthias Ringwald 4542b5bbcbf4SMatthias Ringwald #ifdef ENABLE_CLASSIC 45434688f216SMatthias Ringwald disable_l2cap_timeouts = 0; 4544b5bbcbf4SMatthias Ringwald #endif 45454688f216SMatthias Ringwald } 45464688f216SMatthias Ringwald 45473fb36a29SMatthias Ringwald /** 45483fb36a29SMatthias Ringwald * @brief Configure Bluetooth chipset driver. Has to be called before power on, or right after receiving the local version information 45493fb36a29SMatthias Ringwald */ 45503fb36a29SMatthias Ringwald void hci_set_chipset(const btstack_chipset_t *chipset_driver){ 45513fb36a29SMatthias Ringwald hci_stack->chipset = chipset_driver; 45523fb36a29SMatthias Ringwald 45533fb36a29SMatthias Ringwald // reset chipset driver - init is also called on power_up 45543fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 45553fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 45563fb36a29SMatthias Ringwald } 45573fb36a29SMatthias Ringwald } 45583fb36a29SMatthias Ringwald 4559fb55bd0aSMatthias Ringwald /** 4560d0b87befSMatthias Ringwald * @brief Configure Bluetooth hardware control. Has to be called after hci_init() but before power on. 4561fb55bd0aSMatthias Ringwald */ 4562fb55bd0aSMatthias Ringwald void hci_set_control(const btstack_control_t *hardware_control){ 4563fb55bd0aSMatthias Ringwald // references to used control implementation 4564fb55bd0aSMatthias Ringwald hci_stack->control = hardware_control; 4565d0b87befSMatthias Ringwald // init with transport config 4566d0b87befSMatthias Ringwald hardware_control->init(hci_stack->config); 4567fb55bd0aSMatthias Ringwald } 4568fb55bd0aSMatthias Ringwald 45691f9eb2ccSMatthias Ringwald static void hci_discard_connections(void){ 4570346f8c2cSMatthias Ringwald btstack_linked_list_iterator_t it; 4571346f8c2cSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 4572346f8c2cSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 45731f9eb2ccSMatthias Ringwald // cancel all l2cap connections by emitting dicsconnection complete before shutdown (free) connection 4574346f8c2cSMatthias Ringwald hci_connection_t * connection = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 45751f9eb2ccSMatthias Ringwald hci_emit_disconnection_complete(connection->con_handle, 0x16); // terminated by local host 45761f9eb2ccSMatthias Ringwald hci_shutdown_connection(connection); 45771f9eb2ccSMatthias Ringwald } 4578346f8c2cSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 4579346f8c2cSMatthias Ringwald while (hci_stack->iso_streams != NULL){ 4580346f8c2cSMatthias Ringwald hci_iso_stream_finalize((hci_iso_stream_t *) hci_stack->iso_streams); 4581346f8c2cSMatthias Ringwald } 4582346f8c2cSMatthias Ringwald #endif 45831f9eb2ccSMatthias Ringwald } 45841f9eb2ccSMatthias Ringwald 458571de195eSMatthias Ringwald void hci_close(void){ 4586e6d6524dSMatthias Ringwald 4587e6d6524dSMatthias Ringwald #ifdef ENABLE_CLASSIC 4588404843c1Smatthias.ringwald // close remote device db 4589a98592bcSMatthias Ringwald if (hci_stack->link_key_db) { 4590a98592bcSMatthias Ringwald hci_stack->link_key_db->close(); 4591404843c1Smatthias.ringwald } 4592e6d6524dSMatthias Ringwald #endif 45937224be7eSMatthias Ringwald 45941f9eb2ccSMatthias Ringwald hci_discard_connections(); 45957224be7eSMatthias Ringwald 4596f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 45973a9fb326S[email protected] 45983a9fb326S[email protected] #ifdef HAVE_MALLOC 45993a9fb326S[email protected] free(hci_stack); 46003a9fb326S[email protected] #endif 46013a9fb326S[email protected] hci_stack = NULL; 4602404843c1Smatthias.ringwald } 4603404843c1Smatthias.ringwald 4604cb70c5abSMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 4605cb70c5abSMatthias Ringwald void hci_set_sco_transport(const btstack_sco_transport_t *sco_transport){ 4606cb70c5abSMatthias Ringwald hci_stack->sco_transport = sco_transport; 4607cb70c5abSMatthias Ringwald sco_transport->register_packet_handler(&packet_handler); 4608cb70c5abSMatthias Ringwald } 4609cb70c5abSMatthias Ringwald #endif 4610cb70c5abSMatthias Ringwald 461135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 4612170fafaeSMatthias Ringwald void gap_set_required_encryption_key_size(uint8_t encryption_key_size){ 4613170fafaeSMatthias Ringwald // validate ranage and set 4614170fafaeSMatthias Ringwald if (encryption_key_size < 7) return; 4615170fafaeSMatthias Ringwald if (encryption_key_size > 16) return; 4616170fafaeSMatthias Ringwald hci_stack->gap_required_encyrption_key_size = encryption_key_size; 4617170fafaeSMatthias Ringwald } 461878315a58SMatthias Ringwald 4619137715ebSMatthias Ringwald uint8_t gap_set_security_mode(gap_security_mode_t security_mode){ 4620137715ebSMatthias Ringwald if ((security_mode == GAP_SECURITY_MODE_4) || (security_mode == GAP_SECURITY_MODE_2)){ 46215dbec6b3SMatthias Ringwald hci_stack->gap_security_mode = security_mode; 4622137715ebSMatthias Ringwald return ERROR_CODE_SUCCESS; 4623137715ebSMatthias Ringwald } else { 4624137715ebSMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 4625137715ebSMatthias Ringwald } 46265dbec6b3SMatthias Ringwald } 46275dbec6b3SMatthias Ringwald 46285dbec6b3SMatthias Ringwald gap_security_mode_t gap_get_security_mode(void){ 46295dbec6b3SMatthias Ringwald return hci_stack->gap_security_mode; 46305dbec6b3SMatthias Ringwald } 46315dbec6b3SMatthias Ringwald 463278315a58SMatthias Ringwald void gap_set_security_level(gap_security_level_t security_level){ 463378315a58SMatthias Ringwald hci_stack->gap_security_level = security_level; 463478315a58SMatthias Ringwald } 463578315a58SMatthias Ringwald 463678315a58SMatthias Ringwald gap_security_level_t gap_get_security_level(void){ 4637d7387af3SMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode){ 4638d7387af3SMatthias Ringwald return LEVEL_4; 4639d7387af3SMatthias Ringwald } 464078315a58SMatthias Ringwald return hci_stack->gap_security_level; 464178315a58SMatthias Ringwald } 464230cdf3c6SMatthias Ringwald 46438ad4dfffSMatthias Ringwald void gap_set_minimal_service_security_level(gap_security_level_t security_level){ 46448ad4dfffSMatthias Ringwald hci_stack->gap_minimal_service_security_level = security_level; 46458ad4dfffSMatthias Ringwald } 46468ad4dfffSMatthias Ringwald 464730cdf3c6SMatthias Ringwald void gap_set_secure_connections_only_mode(bool enable){ 464830cdf3c6SMatthias Ringwald hci_stack->gap_secure_connections_only_mode = enable; 464930cdf3c6SMatthias Ringwald } 465030cdf3c6SMatthias Ringwald 465130cdf3c6SMatthias Ringwald bool gap_get_secure_connections_only_mode(void){ 465230cdf3c6SMatthias Ringwald return hci_stack->gap_secure_connections_only_mode; 465330cdf3c6SMatthias Ringwald } 4654170fafaeSMatthias Ringwald #endif 4655170fafaeSMatthias Ringwald 4656170fafaeSMatthias Ringwald #ifdef ENABLE_CLASSIC 465760b9e82fSMatthias Ringwald void gap_set_class_of_device(uint32_t class_of_device){ 46589e61646fS[email protected] hci_stack->class_of_device = class_of_device; 4659baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_CLASS_OF_DEVICE; 466059d59ecfSMatthias Ringwald hci_run(); 46619e61646fS[email protected] } 466276f27cffSMatthias Ringwald 4663c33e56d3SMatthias Ringwald void gap_set_default_link_policy_settings(uint16_t default_link_policy_settings){ 4664c33e56d3SMatthias Ringwald hci_stack->default_link_policy_settings = default_link_policy_settings; 4665baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_DEFAULT_LINK_POLICY; 466659d59ecfSMatthias Ringwald hci_run(); 4667c33e56d3SMatthias Ringwald } 4668c33e56d3SMatthias Ringwald 4669b4eb4420SMatthias Ringwald void gap_set_allow_role_switch(bool allow_role_switch){ 4670b4eb4420SMatthias Ringwald hci_stack->allow_role_switch = allow_role_switch ? 1 : 0; 4671b4eb4420SMatthias Ringwald } 4672b4eb4420SMatthias Ringwald 4673b4eb4420SMatthias Ringwald uint8_t hci_get_allow_role_switch(void){ 4674b4eb4420SMatthias Ringwald return hci_stack->allow_role_switch; 4675b4eb4420SMatthias Ringwald } 4676b4eb4420SMatthias Ringwald 46770c3eb48dSMatthias Ringwald void gap_set_link_supervision_timeout(uint16_t link_supervision_timeout){ 46780c3eb48dSMatthias Ringwald hci_stack->link_supervision_timeout = link_supervision_timeout; 46790c3eb48dSMatthias Ringwald } 46800c3eb48dSMatthias Ringwald 46819afaa4d4SMatthias Ringwald void gap_enable_link_watchdog(uint16_t timeout_ms){ 46829afaa4d4SMatthias Ringwald hci_stack->automatic_flush_timeout = btstack_min(timeout_ms, 1280) * 8 / 5; // divide by 0.625 46839afaa4d4SMatthias Ringwald } 46849afaa4d4SMatthias Ringwald 46859afaa4d4SMatthias Ringwald uint16_t hci_automatic_flush_timeout(void){ 46869afaa4d4SMatthias Ringwald return hci_stack->automatic_flush_timeout; 46879afaa4d4SMatthias Ringwald } 46889afaa4d4SMatthias Ringwald 468976f27cffSMatthias Ringwald void hci_disable_l2cap_timeout_check(void){ 469076f27cffSMatthias Ringwald disable_l2cap_timeouts = 1; 469176f27cffSMatthias Ringwald } 469235454696SMatthias Ringwald #endif 46939e61646fS[email protected] 46946fad2c37SMatthias Ringwald #ifndef HAVE_HOST_CONTROLLER_API 4695f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 4696f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 46976535961aSMatthias Ringwald (void)memcpy(hci_stack->custom_bd_addr, addr, 6); 4698f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 4699f456b2d0S[email protected] } 470076f27cffSMatthias Ringwald #endif 4701f456b2d0S[email protected] 47028d213e1aSmatthias.ringwald // State-Module-Driver overview 47038d213e1aSmatthias.ringwald // state module low-level 47048d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 47058d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 47068d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 47078d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 4708d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 4709d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 4710c7e0c5f6Smatthias.ringwald 471140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 47127301ad89Smatthias.ringwald 4713038bc64cSmatthias.ringwald // power on 4714f9a30166Smatthias.ringwald int err = 0; 47153a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 4716d0b87befSMatthias Ringwald err = (*hci_stack->control->on)(); 4717f9a30166Smatthias.ringwald } 4718038bc64cSmatthias.ringwald if (err){ 47199da54300S[email protected] log_error( "POWER_ON failed"); 4720038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 4721038bc64cSmatthias.ringwald return err; 4722038bc64cSmatthias.ringwald } 4723038bc64cSmatthias.ringwald 472424b3c629SMatthias Ringwald // int chipset driver 47253fb36a29SMatthias Ringwald if (hci_stack->chipset && hci_stack->chipset->init){ 47263fb36a29SMatthias Ringwald hci_stack->chipset->init(hci_stack->config); 47273fb36a29SMatthias Ringwald } 47283fb36a29SMatthias Ringwald 472924b3c629SMatthias Ringwald // init transport 473024b3c629SMatthias Ringwald if (hci_stack->hci_transport->init){ 473124b3c629SMatthias Ringwald hci_stack->hci_transport->init(hci_stack->config); 473224b3c629SMatthias Ringwald } 473324b3c629SMatthias Ringwald 473424b3c629SMatthias Ringwald // open transport 473524b3c629SMatthias Ringwald err = hci_stack->hci_transport->open(); 4736038bc64cSmatthias.ringwald if (err){ 47379da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 47383a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4739d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4740f9a30166Smatthias.ringwald } 4741038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 4742038bc64cSmatthias.ringwald return err; 4743038bc64cSmatthias.ringwald } 47448d213e1aSmatthias.ringwald return 0; 47458d213e1aSmatthias.ringwald } 4746038bc64cSmatthias.ringwald 474740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 47488d213e1aSmatthias.ringwald 47499da54300S[email protected] log_info("hci_power_control_off"); 47509418f9c9Smatthias.ringwald 47518d213e1aSmatthias.ringwald // close low-level device 475224b3c629SMatthias Ringwald hci_stack->hci_transport->close(); 47538d213e1aSmatthias.ringwald 47549da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 47559418f9c9Smatthias.ringwald 47568d213e1aSmatthias.ringwald // power off 47573a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4758d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 47598d213e1aSmatthias.ringwald } 47609418f9c9Smatthias.ringwald 47619da54300S[email protected] log_info("hci_power_control_off - control closed"); 47629418f9c9Smatthias.ringwald 47633a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 476472ea5239Smatthias.ringwald } 476572ea5239Smatthias.ringwald 476640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 476772ea5239Smatthias.ringwald 47689da54300S[email protected] log_info("hci_power_control_sleep"); 47693144bce4Smatthias.ringwald 4770b429b9b7Smatthias.ringwald #if 0 4771b429b9b7Smatthias.ringwald // don't close serial port during sleep 4772b429b9b7Smatthias.ringwald 477372ea5239Smatthias.ringwald // close low-level device 47743a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 4775b429b9b7Smatthias.ringwald #endif 477672ea5239Smatthias.ringwald 477772ea5239Smatthias.ringwald // sleep mode 47783a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 4779d0b87befSMatthias Ringwald (*hci_stack->control->sleep)(); 478072ea5239Smatthias.ringwald } 4781b429b9b7Smatthias.ringwald 47823a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 47838d213e1aSmatthias.ringwald } 47848d213e1aSmatthias.ringwald 478540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 4786b429b9b7Smatthias.ringwald 47879da54300S[email protected] log_info("hci_power_control_wake"); 4788b429b9b7Smatthias.ringwald 4789b429b9b7Smatthias.ringwald // wake on 47903a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 4791d0b87befSMatthias Ringwald (*hci_stack->control->wake)(); 4792b429b9b7Smatthias.ringwald } 4793b429b9b7Smatthias.ringwald 4794b429b9b7Smatthias.ringwald #if 0 4795b429b9b7Smatthias.ringwald // open low-level device 47963a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 4797b429b9b7Smatthias.ringwald if (err){ 47989da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 47993a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 4800d0b87befSMatthias Ringwald (*hci_stack->control->off)(); 4801b429b9b7Smatthias.ringwald } 4802b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 4803b429b9b7Smatthias.ringwald return err; 4804b429b9b7Smatthias.ringwald } 4805b429b9b7Smatthias.ringwald #endif 4806b429b9b7Smatthias.ringwald 4807b429b9b7Smatthias.ringwald return 0; 4808b429b9b7Smatthias.ringwald } 4809b429b9b7Smatthias.ringwald 48100c88d5efSMatthias Ringwald static void hci_power_enter_initializing_state(void){ 481144935e40S[email protected] // set up state machine 481244935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 481302c7fc01SMatthias Ringwald hci_stack->hci_packet_buffer_reserved = false; 481444935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 48155c363727SMatthias Ringwald hci_stack->substate = HCI_INIT_SEND_RESET; 481644935e40S[email protected] } 4817b429b9b7Smatthias.ringwald 48180c88d5efSMatthias Ringwald static void hci_power_enter_halting_state(void){ 48190c88d5efSMatthias Ringwald #ifdef ENABLE_BLE 48200c88d5efSMatthias Ringwald hci_whitelist_free(); 4821a2c9d908SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 4822a2c9d908SMatthias Ringwald hci_periodic_advertiser_list_free(); 4823a2c9d908SMatthias Ringwald #endif 48240c88d5efSMatthias Ringwald #endif 4825184dbe2fSMatthias Ringwald // see hci_run 4826184dbe2fSMatthias Ringwald hci_stack->state = HCI_STATE_HALTING; 48270c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_CLASSIC_STOP; 4828184dbe2fSMatthias Ringwald // setup watchdog timer for disconnect - only triggers if Controller does not respond anymore 4829184dbe2fSMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 1000); 4830184dbe2fSMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 4831184dbe2fSMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 4832184dbe2fSMatthias Ringwald } 4833184dbe2fSMatthias Ringwald 483442bd3d77SMatthias Ringwald // returns error 483542bd3d77SMatthias Ringwald static int hci_power_control_state_off(HCI_POWER_MODE power_mode){ 483642bd3d77SMatthias Ringwald int err; 48378d213e1aSmatthias.ringwald switch (power_mode){ 48388d213e1aSmatthias.ringwald case HCI_POWER_ON: 48398d213e1aSmatthias.ringwald err = hci_power_control_on(); 484042bd3d77SMatthias Ringwald if (err != 0) { 4841f04a0c31SMatthias Ringwald log_error("hci_power_control_on() error %d", err); 484297b61c7bS[email protected] return err; 484397b61c7bS[email protected] } 48440c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 48458d213e1aSmatthias.ringwald break; 48468d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48478d213e1aSmatthias.ringwald // do nothing 48488d213e1aSmatthias.ringwald break; 48498d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4850b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 48518d213e1aSmatthias.ringwald break; 48527bbeb3adSMilanka Ringwald default: 48537bbeb3adSMilanka Ringwald btstack_assert(false); 48547bbeb3adSMilanka Ringwald break; 48558d213e1aSmatthias.ringwald } 485642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 485742bd3d77SMatthias Ringwald } 48587301ad89Smatthias.ringwald 485942bd3d77SMatthias Ringwald static int hci_power_control_state_initializing(HCI_POWER_MODE power_mode){ 48608d213e1aSmatthias.ringwald switch (power_mode){ 48618d213e1aSmatthias.ringwald case HCI_POWER_ON: 48628d213e1aSmatthias.ringwald // do nothing 48638d213e1aSmatthias.ringwald break; 48648d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48658d213e1aSmatthias.ringwald // no connections yet, just turn it off 48668d213e1aSmatthias.ringwald hci_power_control_off(); 48678d213e1aSmatthias.ringwald break; 48688d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4869b546ac54Smatthias.ringwald // no connections yet, just turn it off 487072ea5239Smatthias.ringwald hci_power_control_sleep(); 48718d213e1aSmatthias.ringwald break; 48727bbeb3adSMilanka Ringwald default: 48737bbeb3adSMilanka Ringwald btstack_assert(false); 48747bbeb3adSMilanka Ringwald break; 48758d213e1aSmatthias.ringwald } 487642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 487742bd3d77SMatthias Ringwald } 48787301ad89Smatthias.ringwald 487942bd3d77SMatthias Ringwald static int hci_power_control_state_working(HCI_POWER_MODE power_mode) { 48808d213e1aSmatthias.ringwald switch (power_mode){ 48818d213e1aSmatthias.ringwald case HCI_POWER_ON: 48828d213e1aSmatthias.ringwald // do nothing 48838d213e1aSmatthias.ringwald break; 48848d213e1aSmatthias.ringwald case HCI_POWER_OFF: 48850c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 48868d213e1aSmatthias.ringwald break; 48878d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4888b546ac54Smatthias.ringwald // see hci_run 48893a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 489074b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 48918d213e1aSmatthias.ringwald break; 48927bbeb3adSMilanka Ringwald default: 48937bbeb3adSMilanka Ringwald btstack_assert(false); 48947bbeb3adSMilanka Ringwald break; 48958d213e1aSmatthias.ringwald } 489642bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 489742bd3d77SMatthias Ringwald } 48987301ad89Smatthias.ringwald 489942bd3d77SMatthias Ringwald static int hci_power_control_state_halting(HCI_POWER_MODE power_mode) { 49008d213e1aSmatthias.ringwald switch (power_mode){ 49018d213e1aSmatthias.ringwald case HCI_POWER_ON: 49020c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 49038d213e1aSmatthias.ringwald break; 49048d213e1aSmatthias.ringwald case HCI_POWER_OFF: 49058d213e1aSmatthias.ringwald // do nothing 49068d213e1aSmatthias.ringwald break; 49078d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4908b546ac54Smatthias.ringwald // see hci_run 49093a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 491074b323a9SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_DISCONNECT; 49118d213e1aSmatthias.ringwald break; 49127bbeb3adSMilanka Ringwald default: 49137bbeb3adSMilanka Ringwald btstack_assert(false); 49147bbeb3adSMilanka Ringwald break; 49158d213e1aSmatthias.ringwald } 491600278272SMatthias Ringwald return ERROR_CODE_SUCCESS; 491742bd3d77SMatthias Ringwald } 49188d213e1aSmatthias.ringwald 491942bd3d77SMatthias Ringwald static int hci_power_control_state_falling_asleep(HCI_POWER_MODE power_mode) { 49208d213e1aSmatthias.ringwald switch (power_mode){ 49218d213e1aSmatthias.ringwald case HCI_POWER_ON: 49220c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 49238d213e1aSmatthias.ringwald break; 49248d213e1aSmatthias.ringwald case HCI_POWER_OFF: 49250c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 49268d213e1aSmatthias.ringwald break; 49278d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4928b546ac54Smatthias.ringwald // do nothing 49298d213e1aSmatthias.ringwald break; 49307bbeb3adSMilanka Ringwald default: 49317bbeb3adSMilanka Ringwald btstack_assert(false); 49327bbeb3adSMilanka Ringwald break; 49338d213e1aSmatthias.ringwald } 493442bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 493542bd3d77SMatthias Ringwald } 49368d213e1aSmatthias.ringwald 493742bd3d77SMatthias Ringwald static int hci_power_control_state_sleeping(HCI_POWER_MODE power_mode) { 493842bd3d77SMatthias Ringwald int err; 49398d213e1aSmatthias.ringwald switch (power_mode){ 49408d213e1aSmatthias.ringwald case HCI_POWER_ON: 49413144bce4Smatthias.ringwald err = hci_power_control_wake(); 49423144bce4Smatthias.ringwald if (err) return err; 49430c88d5efSMatthias Ringwald hci_power_enter_initializing_state(); 49448d213e1aSmatthias.ringwald break; 49458d213e1aSmatthias.ringwald case HCI_POWER_OFF: 49460c88d5efSMatthias Ringwald hci_power_enter_halting_state(); 49478d213e1aSmatthias.ringwald break; 49488d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 4949b546ac54Smatthias.ringwald // do nothing 49508d213e1aSmatthias.ringwald break; 49517bbeb3adSMilanka Ringwald default: 49527bbeb3adSMilanka Ringwald btstack_assert(false); 49537bbeb3adSMilanka Ringwald break; 49548d213e1aSmatthias.ringwald } 495542bd3d77SMatthias Ringwald return ERROR_CODE_SUCCESS; 495642bd3d77SMatthias Ringwald } 49577bbeb3adSMilanka Ringwald 495842bd3d77SMatthias Ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 495942bd3d77SMatthias Ringwald log_info("hci_power_control: %d, current mode %u", power_mode, hci_stack->state); 496024f87726SMatthias Ringwald int err = 0; 496142bd3d77SMatthias Ringwald switch (hci_stack->state){ 496242bd3d77SMatthias Ringwald case HCI_STATE_OFF: 496342bd3d77SMatthias Ringwald err = hci_power_control_state_off(power_mode); 496442bd3d77SMatthias Ringwald break; 496542bd3d77SMatthias Ringwald case HCI_STATE_INITIALIZING: 496642bd3d77SMatthias Ringwald err = hci_power_control_state_initializing(power_mode); 496742bd3d77SMatthias Ringwald break; 496842bd3d77SMatthias Ringwald case HCI_STATE_WORKING: 496942bd3d77SMatthias Ringwald err = hci_power_control_state_working(power_mode); 497042bd3d77SMatthias Ringwald break; 497142bd3d77SMatthias Ringwald case HCI_STATE_HALTING: 497242bd3d77SMatthias Ringwald err = hci_power_control_state_halting(power_mode); 497342bd3d77SMatthias Ringwald break; 497442bd3d77SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 497542bd3d77SMatthias Ringwald err = hci_power_control_state_falling_asleep(power_mode); 497642bd3d77SMatthias Ringwald break; 497742bd3d77SMatthias Ringwald case HCI_STATE_SLEEPING: 497842bd3d77SMatthias Ringwald err = hci_power_control_state_sleeping(power_mode); 497942bd3d77SMatthias Ringwald break; 49807bbeb3adSMilanka Ringwald default: 49817bbeb3adSMilanka Ringwald btstack_assert(false); 49827bbeb3adSMilanka Ringwald break; 498311e23e5fSmatthias.ringwald } 498424f87726SMatthias Ringwald if (err != 0){ 498542bd3d77SMatthias Ringwald return err; 498642bd3d77SMatthias Ringwald } 498768d92d03Smatthias.ringwald 4988038bc64cSmatthias.ringwald // create internal event 4989ee8bf225Smatthias.ringwald hci_emit_state(); 4990ee8bf225Smatthias.ringwald 499168d92d03Smatthias.ringwald // trigger next/first action 499268d92d03Smatthias.ringwald hci_run(); 499368d92d03Smatthias.ringwald 4994475c8125Smatthias.ringwald return 0; 4995475c8125Smatthias.ringwald } 4996475c8125Smatthias.ringwald 499735454696SMatthias Ringwald 4998091e35e3SMatthias Ringwald static void hci_halting_run(void) { 4999091e35e3SMatthias Ringwald 5000091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, substate %x\n", hci_stack->substate); 5001091e35e3SMatthias Ringwald 5002091e35e3SMatthias Ringwald hci_connection_t *connection; 5003d5f71a7eSMatthias Ringwald #ifdef ENABLE_BLE 5004d5f71a7eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5005911801caSMatthias Ringwald bool stop_advertismenets; 5006d5f71a7eSMatthias Ringwald #endif 5007d5f71a7eSMatthias Ringwald #endif 5008091e35e3SMatthias Ringwald 5009091e35e3SMatthias Ringwald switch (hci_stack->substate) { 50100c88d5efSMatthias Ringwald case HCI_HALTING_CLASSIC_STOP: 50110c88d5efSMatthias Ringwald #ifdef ENABLE_CLASSIC 5012911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5013911801caSMatthias Ringwald 50140c88d5efSMatthias Ringwald if (hci_stack->connectable || hci_stack->discoverable){ 50150c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_ADV_STOP; 50160c88d5efSMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, 0); 50170c88d5efSMatthias Ringwald return; 50180c88d5efSMatthias Ringwald } 50190c88d5efSMatthias Ringwald #endif 50200c88d5efSMatthias Ringwald /* fall through */ 50210c88d5efSMatthias Ringwald 50220c88d5efSMatthias Ringwald case HCI_HALTING_LE_ADV_STOP: 50230c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_ADV_STOP; 50240c88d5efSMatthias Ringwald 50250c88d5efSMatthias Ringwald #ifdef ENABLE_BLE 50260c88d5efSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5027911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5028911801caSMatthias Ringwald 5029911801caSMatthias Ringwald stop_advertismenets = (hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0; 5030911801caSMatthias Ringwald 5031911801caSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5032911801caSMatthias Ringwald if (hci_extended_advertising_supported()){ 5033c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 5034911801caSMatthias Ringwald btstack_linked_list_iterator_t it; 5035911801caSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5036911801caSMatthias Ringwald // stop all periodic advertisements and check if an extended set is active 5037911801caSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5038911801caSMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 5039911801caSMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) != 0) { 5040911801caSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 5041911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, 0, advertising_set->advertising_handle); 50420c88d5efSMatthias Ringwald return; 50430c88d5efSMatthias Ringwald } 5044911801caSMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0) { 5045911801caSMatthias Ringwald stop_advertismenets = true; 5046911801caSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5047911801caSMatthias Ringwald } 5048911801caSMatthias Ringwald } 5049c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5050911801caSMatthias Ringwald if (stop_advertismenets){ 5051911801caSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5052911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 0, NULL, NULL, NULL); 5053911801caSMatthias Ringwald return; 5054911801caSMatthias Ringwald } 5055911801caSMatthias Ringwald } 5056911801caSMatthias Ringwald else 5057a2c9d908SMatthias Ringwald #else /* ENABLE_LE_PERIPHERAL */ 5058911801caSMatthias Ringwald { 5059911801caSMatthias Ringwald if (stop_advertismenets) { 5060911801caSMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5061911801caSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 5062911801caSMatthias Ringwald return; 5063911801caSMatthias Ringwald } 5064911801caSMatthias Ringwald } 5065911801caSMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING*/ 5066911801caSMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 5067911801caSMatthias Ringwald #endif /* ENABLE_BLE */ 5068911801caSMatthias Ringwald 50690c88d5efSMatthias Ringwald /* fall through */ 50700c88d5efSMatthias Ringwald 50710c88d5efSMatthias Ringwald case HCI_HALTING_LE_SCAN_STOP: 50720c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_LE_SCAN_STOP; 5073911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5074091e35e3SMatthias Ringwald 5075091e35e3SMatthias Ringwald #ifdef ENABLE_BLE 5076091e35e3SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 50770c88d5efSMatthias Ringwald if (hci_stack->le_scanning_active){ 5078c42da3bcSMatthias Ringwald hci_le_scan_stop(); 50790c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL; 50800c88d5efSMatthias Ringwald return; 50810c88d5efSMatthias Ringwald } 5082091e35e3SMatthias Ringwald #endif 5083091e35e3SMatthias Ringwald #endif 50840c88d5efSMatthias Ringwald 50850c88d5efSMatthias Ringwald /* fall through */ 50860c88d5efSMatthias Ringwald 50870c88d5efSMatthias Ringwald case HCI_HALTING_DISCONNECT_ALL: 50880c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_DISCONNECT_ALL; 5089911801caSMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 50900c88d5efSMatthias Ringwald 5091091e35e3SMatthias Ringwald // close all open connections 5092091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 5093091e35e3SMatthias Ringwald if (connection) { 5094091e35e3SMatthias Ringwald hci_con_handle_t con_handle = (uint16_t) connection->con_handle; 5095091e35e3SMatthias Ringwald 5096091e35e3SMatthias Ringwald // check state 5097091e35e3SMatthias Ringwald if (connection->state == SENT_DISCONNECT) return; 5098091e35e3SMatthias Ringwald connection->state = SENT_DISCONNECT; 5099091e35e3SMatthias Ringwald 5100091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, con_handle); 5101091e35e3SMatthias Ringwald 5102091e35e3SMatthias Ringwald // finally, send the disconnect command 5103091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5104091e35e3SMatthias Ringwald return; 5105091e35e3SMatthias Ringwald } 5106091e35e3SMatthias Ringwald 5107346f8c2cSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 5108346f8c2cSMatthias Ringwald // stop BIGs and BIG Syncs 5109346f8c2cSMatthias Ringwald if (hci_stack->le_audio_bigs != NULL){ 5110346f8c2cSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t*) hci_stack->le_audio_bigs; 5111346f8c2cSMatthias Ringwald if (big->state == LE_AUDIO_BIG_STATE_W4_TERMINATED) return; 5112346f8c2cSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 5113346f8c2cSMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle); 5114346f8c2cSMatthias Ringwald return; 5115346f8c2cSMatthias Ringwald } 5116346f8c2cSMatthias Ringwald if (hci_stack->le_audio_big_syncs != NULL){ 5117346f8c2cSMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t*) hci_stack->le_audio_big_syncs; 5118346f8c2cSMatthias Ringwald if (big_sync->state == LE_AUDIO_BIG_STATE_W4_TERMINATED) return; 5119346f8c2cSMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 5120346f8c2cSMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 5121346f8c2cSMatthias Ringwald return; 5122346f8c2cSMatthias Ringwald } 5123346f8c2cSMatthias Ringwald #endif 5124346f8c2cSMatthias Ringwald 5125091e35e3SMatthias Ringwald btstack_run_loop_remove_timer(&hci_stack->timeout); 5126091e35e3SMatthias Ringwald 5127091e35e3SMatthias Ringwald // no connections left, wait a bit to assert that btstack_cyrpto isn't waiting for an HCI event 5128091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING: wait 50 ms"); 51290c88d5efSMatthias Ringwald hci_stack->substate = HCI_HALTING_W4_CLOSE_TIMER; 5130091e35e3SMatthias Ringwald btstack_run_loop_set_timer(&hci_stack->timeout, 50); 5131091e35e3SMatthias Ringwald btstack_run_loop_set_timer_handler(&hci_stack->timeout, hci_halting_timeout_handler); 5132091e35e3SMatthias Ringwald btstack_run_loop_add_timer(&hci_stack->timeout); 5133091e35e3SMatthias Ringwald break; 5134091e35e3SMatthias Ringwald 5135091e35e3SMatthias Ringwald case HCI_HALTING_CLOSE: 5136091e35e3SMatthias Ringwald // close left over connections (that had not been properly closed before) 5137091e35e3SMatthias Ringwald hci_discard_connections(); 5138091e35e3SMatthias Ringwald 5139091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling off"); 5140091e35e3SMatthias Ringwald 5141091e35e3SMatthias Ringwald // switch mode 5142091e35e3SMatthias Ringwald hci_power_control_off(); 5143091e35e3SMatthias Ringwald 5144091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, emitting state"); 5145091e35e3SMatthias Ringwald hci_emit_state(); 5146091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, done"); 5147091e35e3SMatthias Ringwald break; 5148091e35e3SMatthias Ringwald 51490c88d5efSMatthias Ringwald case HCI_HALTING_W4_CLOSE_TIMER: 5150091e35e3SMatthias Ringwald // keep waiting 5151091e35e3SMatthias Ringwald 5152091e35e3SMatthias Ringwald break; 5153091e35e3SMatthias Ringwald default: 5154091e35e3SMatthias Ringwald break; 5155091e35e3SMatthias Ringwald } 5156091e35e3SMatthias Ringwald }; 5157091e35e3SMatthias Ringwald 5158091e35e3SMatthias Ringwald static void hci_falling_asleep_run(void){ 5159091e35e3SMatthias Ringwald hci_connection_t * connection; 5160091e35e3SMatthias Ringwald switch(hci_stack->substate) { 5161091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_DISCONNECT: 5162091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP"); 5163091e35e3SMatthias Ringwald // close all open connections 5164091e35e3SMatthias Ringwald connection = (hci_connection_t *) hci_stack->connections; 5165091e35e3SMatthias Ringwald if (connection){ 5166091e35e3SMatthias Ringwald 5167091e35e3SMatthias Ringwald // send disconnect 5168091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5169091e35e3SMatthias Ringwald 5170091e35e3SMatthias Ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 5171091e35e3SMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 5172091e35e3SMatthias Ringwald 5173091e35e3SMatthias Ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 5174091e35e3SMatthias Ringwald hci_shutdown_connection(connection); 5175091e35e3SMatthias Ringwald return; 5176091e35e3SMatthias Ringwald } 5177091e35e3SMatthias Ringwald 5178091e35e3SMatthias Ringwald if (hci_classic_supported()){ 5179091e35e3SMatthias Ringwald // disable page and inquiry scan 5180091e35e3SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 5181091e35e3SMatthias Ringwald 5182091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, disabling inq scans"); 5183091e35e3SMatthias Ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 5184091e35e3SMatthias Ringwald 5185091e35e3SMatthias Ringwald // continue in next sub state 5186091e35e3SMatthias Ringwald hci_stack->substate = HCI_FALLING_ASLEEP_W4_WRITE_SCAN_ENABLE; 5187091e35e3SMatthias Ringwald break; 5188091e35e3SMatthias Ringwald } 5189091e35e3SMatthias Ringwald 5190091e35e3SMatthias Ringwald /* fall through */ 5191091e35e3SMatthias Ringwald 5192091e35e3SMatthias Ringwald case HCI_FALLING_ASLEEP_COMPLETE: 5193091e35e3SMatthias Ringwald log_info("HCI_STATE_HALTING, calling sleep"); 5194091e35e3SMatthias Ringwald // switch mode 5195091e35e3SMatthias Ringwald hci_power_control_sleep(); // changes hci_stack->state to SLEEP 5196091e35e3SMatthias Ringwald hci_emit_state(); 5197091e35e3SMatthias Ringwald break; 5198091e35e3SMatthias Ringwald 5199091e35e3SMatthias Ringwald default: 5200091e35e3SMatthias Ringwald break; 5201091e35e3SMatthias Ringwald } 5202091e35e3SMatthias Ringwald } 5203091e35e3SMatthias Ringwald 520435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 520535454696SMatthias Ringwald 5206758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 5207758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 5208a1df452eSMatthias Ringwald hci_stack->new_scan_enable_value = (hci_stack->connectable << 1) | hci_stack->discoverable; 5209baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_SCAN_ENABLE; 5210758b46ceSmatthias.ringwald hci_run(); 5211758b46ceSmatthias.ringwald } 5212758b46ceSmatthias.ringwald 521315a95bd5SMatthias Ringwald void gap_discoverable_control(uint8_t enable){ 5214381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 5215381fbed8Smatthias.ringwald 52163a9fb326S[email protected] if (hci_stack->discoverable == enable){ 521771fd255dSMatthias Ringwald hci_emit_scan_mode_changed(hci_stack->discoverable, hci_stack->connectable); 5218381fbed8Smatthias.ringwald return; 5219381fbed8Smatthias.ringwald } 5220381fbed8Smatthias.ringwald 52213a9fb326S[email protected] hci_stack->discoverable = enable; 5222758b46ceSmatthias.ringwald hci_update_scan_enable(); 5223758b46ceSmatthias.ringwald } 5224b031bebbSmatthias.ringwald 522515a95bd5SMatthias Ringwald void gap_connectable_control(uint8_t enable){ 5226758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 5227758b46ceSmatthias.ringwald 5228758b46ceSmatthias.ringwald // don't emit event 52293a9fb326S[email protected] if (hci_stack->connectable == enable) return; 5230758b46ceSmatthias.ringwald 52313a9fb326S[email protected] hci_stack->connectable = enable; 5232758b46ceSmatthias.ringwald hci_update_scan_enable(); 5233381fbed8Smatthias.ringwald } 523435454696SMatthias Ringwald #endif 5235381fbed8Smatthias.ringwald 523615a95bd5SMatthias Ringwald void gap_local_bd_addr(bd_addr_t address_buffer){ 52376535961aSMatthias Ringwald (void)memcpy(address_buffer, hci_stack->local_bd_addr, 6); 52385061f3afS[email protected] } 52395061f3afS[email protected] 52402b838201SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 52412b838201SMatthias Ringwald static void hci_host_num_completed_packets(void){ 52422b838201SMatthias Ringwald 52432b838201SMatthias Ringwald // create packet manually as arrays are not supported and num_commands should not get reduced 52442b838201SMatthias Ringwald hci_reserve_packet_buffer(); 52452b838201SMatthias Ringwald uint8_t * packet = hci_get_outgoing_packet_buffer(); 52462b838201SMatthias Ringwald 52472b838201SMatthias Ringwald uint16_t size = 0; 52482b838201SMatthias Ringwald uint16_t num_handles = 0; 52492b838201SMatthias Ringwald packet[size++] = 0x35; 52502b838201SMatthias Ringwald packet[size++] = 0x0c; 52512b838201SMatthias Ringwald size++; // skip param len 52522b838201SMatthias Ringwald size++; // skip num handles 52532b838201SMatthias Ringwald 52542b838201SMatthias Ringwald // add { handle, packets } entries 52552b838201SMatthias Ringwald btstack_linked_item_t * it; 52562b838201SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it ; it = it->next){ 52572b838201SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 52582b838201SMatthias Ringwald if (connection->num_packets_completed){ 52592b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->con_handle); 52602b838201SMatthias Ringwald size += 2; 52612b838201SMatthias Ringwald little_endian_store_16(packet, size, connection->num_packets_completed); 52622b838201SMatthias Ringwald size += 2; 52632b838201SMatthias Ringwald // 52642b838201SMatthias Ringwald num_handles++; 52652b838201SMatthias Ringwald connection->num_packets_completed = 0; 52662b838201SMatthias Ringwald } 52672b838201SMatthias Ringwald } 52682b838201SMatthias Ringwald 52692b838201SMatthias Ringwald packet[2] = size - 3; 52702b838201SMatthias Ringwald packet[3] = num_handles; 52712b838201SMatthias Ringwald 52722b838201SMatthias Ringwald hci_stack->host_completed_packets = 0; 52732b838201SMatthias Ringwald 52742b838201SMatthias Ringwald hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 52752b838201SMatthias Ringwald hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 52762b838201SMatthias Ringwald 52772b838201SMatthias Ringwald // release packet buffer for synchronous transport implementations 52782b838201SMatthias Ringwald if (hci_transport_synchronous()){ 5279e2d22487SMatthias Ringwald hci_release_packet_buffer(); 5280068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 52812b838201SMatthias Ringwald } 52822b838201SMatthias Ringwald } 52832b838201SMatthias Ringwald #endif 52842b838201SMatthias Ringwald 528526fe9592SMatthias Ringwald static void hci_halting_timeout_handler(btstack_timer_source_t * ds){ 528626fe9592SMatthias Ringwald UNUSED(ds); 528726fe9592SMatthias Ringwald hci_stack->substate = HCI_HALTING_CLOSE; 5288beceeddeSMatthias Ringwald // allow packet handlers to defer final shutdown 5289beceeddeSMatthias Ringwald hci_emit_state(); 529026fe9592SMatthias Ringwald hci_run(); 529126fe9592SMatthias Ringwald } 529226fe9592SMatthias Ringwald 5293f30077b7SMatthias Ringwald static bool hci_run_acl_fragments(void){ 52944ea43905SMatthias Ringwald if (hci_stack->acl_fragmentation_total_size > 0u) { 5295b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 5296b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 5297b5d8b22bS[email protected] if (connection) { 529828a0332dSMatthias Ringwald if (hci_can_send_prepared_acl_packet_now(con_handle)){ 5299b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 5300f30077b7SMatthias Ringwald return true; 5301b5d8b22bS[email protected] } 530228a0332dSMatthias Ringwald } else { 5303b5d8b22bS[email protected] // connection gone -> discard further fragments 530428a0332dSMatthias Ringwald log_info("hci_run: fragmented ACL packet no connection -> discard fragment"); 5305b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 5306b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 5307b5d8b22bS[email protected] } 5308b5d8b22bS[email protected] } 5309f30077b7SMatthias Ringwald return false; 53102b838201SMatthias Ringwald } 5311b031bebbSmatthias.ringwald 531268ab6207SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 531368ab6207SMatthias Ringwald static bool hci_run_iso_fragments(void){ 531468ab6207SMatthias Ringwald if (hci_stack->iso_fragmentation_total_size > 0u) { 531568ab6207SMatthias Ringwald // TODO: flow control 531668ab6207SMatthias Ringwald if (hci_transport_can_send_prepared_packet_now(HCI_ISO_DATA_PACKET)){ 531768ab6207SMatthias Ringwald hci_send_iso_packet_fragments(); 531868ab6207SMatthias Ringwald return true; 531968ab6207SMatthias Ringwald } 532068ab6207SMatthias Ringwald } 532168ab6207SMatthias Ringwald return false; 532268ab6207SMatthias Ringwald } 532368ab6207SMatthias Ringwald #endif 532468ab6207SMatthias Ringwald 532535454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 53266c0d5426SMatthias Ringwald 53276c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 53286c0d5426SMatthias Ringwald static bool hci_classic_operation_active(void) { 53296c0d5426SMatthias Ringwald if (hci_stack->inquiry_state >= GAP_INQUIRY_STATE_W4_ACTIVE){ 53306c0d5426SMatthias Ringwald return true; 53316c0d5426SMatthias Ringwald } 53326c0d5426SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W4_COMPLETE){ 53336c0d5426SMatthias Ringwald return true; 53346c0d5426SMatthias Ringwald } 53356c0d5426SMatthias Ringwald btstack_linked_item_t * it; 53366c0d5426SMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next) { 53376c0d5426SMatthias Ringwald hci_connection_t *connection = (hci_connection_t *) it; 53386c0d5426SMatthias Ringwald switch (connection->state) { 53396c0d5426SMatthias Ringwald case SENT_CREATE_CONNECTION: 53406c0d5426SMatthias Ringwald case SENT_CANCEL_CONNECTION: 53416c0d5426SMatthias Ringwald case SENT_DISCONNECT: 53426c0d5426SMatthias Ringwald return true; 53436c0d5426SMatthias Ringwald default: 53446c0d5426SMatthias Ringwald break; 53456c0d5426SMatthias Ringwald } 53466c0d5426SMatthias Ringwald } 53476c0d5426SMatthias Ringwald return false; 53486c0d5426SMatthias Ringwald } 53496c0d5426SMatthias Ringwald #endif 53506c0d5426SMatthias Ringwald 5351f30077b7SMatthias Ringwald static bool hci_run_general_gap_classic(void){ 5352f30077b7SMatthias Ringwald 535359d59ecfSMatthias Ringwald // assert stack is working and classic is active 535459d59ecfSMatthias Ringwald if (hci_classic_supported() == false) return false; 535559d59ecfSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 535659d59ecfSMatthias Ringwald 5357b031bebbSmatthias.ringwald // decline incoming connections 53583a9fb326S[email protected] if (hci_stack->decline_reason){ 53593a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 53603a9fb326S[email protected] hci_stack->decline_reason = 0; 53613a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 5362f30077b7SMatthias Ringwald return true; 5363ce4c8fabSmatthias.ringwald } 536459d59ecfSMatthias Ringwald 5365baa4881eSMatthias Ringwald if (hci_stack->gap_tasks_classic != 0){ 5366ab4831a3SMatthias Ringwald hci_run_gap_tasks_classic(); 5367f30077b7SMatthias Ringwald return true; 5368b031bebbSmatthias.ringwald } 536927741fe7SMatthias Ringwald 5370f5875de5SMatthias Ringwald // start/stop inquiry 5371a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)){ 53726c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 53736c0d5426SMatthias Ringwald if (hci_classic_operation_active() == false) 53746c0d5426SMatthias Ringwald #endif 53756c0d5426SMatthias Ringwald { 5376f5875de5SMatthias Ringwald uint8_t duration = hci_stack->inquiry_state; 5377beb3c81dSMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_ACTIVE; 537830199e24SMatthias Ringwald if (hci_stack->inquiry_max_period_length != 0){ 537930199e24SMatthias 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); 538030199e24SMatthias Ringwald } else { 5381496bb884SMatthias Ringwald hci_send_cmd(&hci_inquiry, hci_stack->inquiry_lap, duration, 0); 538230199e24SMatthias Ringwald } 5383f30077b7SMatthias Ringwald return true; 5384f5875de5SMatthias Ringwald } 53856c0d5426SMatthias Ringwald } 5386f5875de5SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_CANCEL){ 5387f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 5388f5875de5SMatthias Ringwald hci_send_cmd(&hci_inquiry_cancel); 5389f30077b7SMatthias Ringwald return true; 5390f5875de5SMatthias Ringwald } 539130199e24SMatthias Ringwald 539230199e24SMatthias Ringwald if (hci_stack->inquiry_state == GAP_INQUIRY_STATE_W2_EXIT_PERIODIC){ 5393bc06f564SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W4_CANCELLED; 539430199e24SMatthias Ringwald hci_send_cmd(&hci_exit_periodic_inquiry_mode); 539530199e24SMatthias Ringwald return true; 539630199e24SMatthias Ringwald } 539730199e24SMatthias Ringwald 5398b7f1ee76SMatthias Ringwald // remote name request 5399b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state == GAP_REMOTE_NAME_STATE_W2_SEND){ 54006c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 54016c0d5426SMatthias Ringwald if (hci_classic_operation_active() == false) 54026c0d5426SMatthias Ringwald #endif 54036c0d5426SMatthias Ringwald { 5404b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W4_COMPLETE; 5405b7f1ee76SMatthias Ringwald hci_send_cmd(&hci_remote_name_request, hci_stack->remote_name_addr, 5406ee8a36c8SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode, 0, hci_stack->remote_name_clock_offset); 5407f30077b7SMatthias Ringwald return true; 5408b7f1ee76SMatthias Ringwald } 54096c0d5426SMatthias Ringwald } 5410cf01e888SMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 5411cf01e888SMatthias Ringwald // Local OOB data 541259d59ecfSMatthias Ringwald if (hci_stack->classic_read_local_oob_data){ 5413cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = false; 54141e83575aSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_READ_LOCAL_OOB_EXTENDED_DATA_COMMAND)){ 5415cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_extended_oob_data); 5416cf01e888SMatthias Ringwald } else { 5417cf01e888SMatthias Ringwald hci_send_cmd(&hci_read_local_oob_data); 5418cf01e888SMatthias Ringwald } 5419cf01e888SMatthias Ringwald } 5420cf01e888SMatthias Ringwald #endif 54210a51f88bSMatthias Ringwald // pairing 54220a51f88bSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE){ 54230a51f88bSMatthias Ringwald uint8_t state = hci_stack->gap_pairing_state; 54243f659ee4SMilanka Ringwald uint8_t pin_code[16]; 54250a51f88bSMatthias Ringwald switch (state){ 54260a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN: 5427cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 54283f659ee4SMilanka Ringwald memset(pin_code, 0, 16); 54293f659ee4SMilanka Ringwald memcpy(pin_code, hci_stack->gap_pairing_input.gap_pairing_pin, hci_stack->gap_pairing_pin_len); 54303f659ee4SMilanka Ringwald hci_send_cmd(&hci_pin_code_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_pin_len, pin_code); 54310a51f88bSMatthias Ringwald break; 54320a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PIN_NEGATIVE: 5433cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 54340a51f88bSMatthias Ringwald hci_send_cmd(&hci_pin_code_request_negative_reply, hci_stack->gap_pairing_addr); 54350a51f88bSMatthias Ringwald break; 54360a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY: 5437cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 5438d504181aSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_reply, hci_stack->gap_pairing_addr, hci_stack->gap_pairing_input.gap_pairing_passkey); 54390a51f88bSMatthias Ringwald break; 54400a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE: 5441cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 54420a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_passkey_request_negative_reply, hci_stack->gap_pairing_addr); 54430a51f88bSMatthias Ringwald break; 54440a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION: 5445cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_IDLE; 54460a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_reply, hci_stack->gap_pairing_addr); 54470a51f88bSMatthias Ringwald break; 54480a51f88bSMatthias Ringwald case GAP_PAIRING_STATE_SEND_CONFIRMATION_NEGATIVE: 5449cc15bb2cSMatthias Ringwald hci_stack->gap_pairing_state = GAP_PAIRING_STATE_WAIT_FOR_COMMAND_COMPLETE; 54500a51f88bSMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, hci_stack->gap_pairing_addr); 54510a51f88bSMatthias Ringwald break; 54520a51f88bSMatthias Ringwald default: 54530a51f88bSMatthias Ringwald break; 54540a51f88bSMatthias Ringwald } 5455f30077b7SMatthias Ringwald return true; 5456f30077b7SMatthias Ringwald } 5457f30077b7SMatthias Ringwald return false; 54580a51f88bSMatthias Ringwald } 545935454696SMatthias Ringwald #endif 5460b031bebbSmatthias.ringwald 5461a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 5462be7ef9d8SMatthias Ringwald 5463c42da3bcSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5464c42da3bcSMatthias Ringwald static void hci_le_scan_stop(void){ 5465c42da3bcSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5466c42da3bcSMatthias Ringwald if (hci_extended_advertising_supported()) { 5467c42da3bcSMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 0, 0, 0, 0); 5468c42da3bcSMatthias Ringwald } else 5469c42da3bcSMatthias Ringwald #endif 5470c42da3bcSMatthias Ringwald { 5471c42da3bcSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 5472c42da3bcSMatthias Ringwald } 5473c42da3bcSMatthias Ringwald } 5474c42da3bcSMatthias Ringwald #endif 5475c42da3bcSMatthias Ringwald 5476be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5477be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5478be7ef9d8SMatthias Ringwald uint8_t hci_le_extended_advertising_operation_for_chunk(uint16_t pos, uint16_t len){ 5479be7ef9d8SMatthias Ringwald uint8_t operation = 0; 5480be7ef9d8SMatthias Ringwald if (pos == 0){ 5481be7ef9d8SMatthias Ringwald // first fragment or complete data 5482be7ef9d8SMatthias Ringwald operation |= 1; 5483be7ef9d8SMatthias Ringwald } 5484be7ef9d8SMatthias Ringwald if (pos + LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN >= len){ 5485be7ef9d8SMatthias Ringwald // last fragment or complete data 5486be7ef9d8SMatthias Ringwald operation |= 2; 5487be7ef9d8SMatthias Ringwald } 5488be7ef9d8SMatthias Ringwald return operation; 5489be7ef9d8SMatthias Ringwald } 5490be7ef9d8SMatthias Ringwald #endif 5491be7ef9d8SMatthias Ringwald #endif 5492be7ef9d8SMatthias Ringwald 5493f30077b7SMatthias Ringwald static bool hci_run_general_gap_le(void){ 5494f30077b7SMatthias Ringwald 5495f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t lit; 5496f40c73b4SMatthias Ringwald 5497a41310b7SMatthias Ringwald // Phase 1: collect what to stop 5498a41310b7SMatthias Ringwald 549949a6c69cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5500a41310b7SMatthias Ringwald bool scanning_stop = false; 5501a41310b7SMatthias Ringwald bool connecting_stop = false; 5502be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5503c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 55044bafedd5SMatthias Ringwald bool periodic_sync_stop = false; 55054bafedd5SMatthias Ringwald #endif 5506be7ef9d8SMatthias Ringwald #endif 5507c814a904SMatthias Ringwald #endif 5508be7ef9d8SMatthias Ringwald 550949a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 551049a6c69cSMatthias Ringwald bool advertising_stop = false; 551149a6c69cSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 551249a6c69cSMatthias Ringwald le_advertising_set_t * advertising_stop_set = NULL; 551349a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 551449a6c69cSMatthias Ringwald bool periodic_advertising_stop = false; 5515a41310b7SMatthias Ringwald #endif 551649a6c69cSMatthias Ringwald #endif 5517a41310b7SMatthias Ringwald #endif 5518a41310b7SMatthias Ringwald 55199136bde8SMatthias Ringwald // check if own address changes 55209136bde8SMatthias Ringwald bool random_address_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 55219136bde8SMatthias Ringwald 552229c24bebSMatthias Ringwald // check if whitelist needs modification 552329c24bebSMatthias Ringwald bool whitelist_modification_pending = false; 552429c24bebSMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 552529c24bebSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 552629c24bebSMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 552729c24bebSMatthias Ringwald if (entry->state & (LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER)){ 552829c24bebSMatthias Ringwald whitelist_modification_pending = true; 552929c24bebSMatthias Ringwald break; 553029c24bebSMatthias Ringwald } 553129c24bebSMatthias Ringwald } 5532f40c73b4SMatthias Ringwald 553321debf25SMatthias Ringwald // check if resolving list needs modification 553421debf25SMatthias Ringwald bool resolving_list_modification_pending = false; 553521debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 55361ffa425cSMatthias Ringwald bool resolving_list_supported = hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE); 5537ea151974SMatthias Ringwald if (resolving_list_supported && hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_DONE){ 553821debf25SMatthias Ringwald resolving_list_modification_pending = true; 553921debf25SMatthias Ringwald } 554021debf25SMatthias Ringwald #endif 554129c24bebSMatthias Ringwald 5542f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 554349a6c69cSMatthias Ringwald 5544f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 554549a6c69cSMatthias Ringwald // check if periodic advertiser list needs modification 5546f40c73b4SMatthias Ringwald bool periodic_list_modification_pending = false; 5547f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 5548f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 5549f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 5550f40c73b4SMatthias Ringwald if (entry->state & (LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER | LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER)){ 5551f40c73b4SMatthias Ringwald periodic_list_modification_pending = true; 5552f40c73b4SMatthias Ringwald break; 5553f40c73b4SMatthias Ringwald } 5554f40c73b4SMatthias Ringwald } 5555f40c73b4SMatthias Ringwald #endif 5556f40c73b4SMatthias Ringwald 5557fde725feSMatthias Ringwald // scanning control 55583251a108SMatthias Ringwald if (hci_stack->le_scanning_active) { 555929c24bebSMatthias Ringwald // stop if: 556029c24bebSMatthias Ringwald // - parameter change required 556129c24bebSMatthias Ringwald // - it's disabled 556229c24bebSMatthias Ringwald // - whitelist change required but used for scanning 556321debf25SMatthias Ringwald // - resolving list modified 556451e51a58SMatthias Ringwald // - own address changes 556529c24bebSMatthias Ringwald bool scanning_uses_whitelist = (hci_stack->le_scan_filter_policy & 1) == 1; 556621debf25SMatthias Ringwald if ((hci_stack->le_scanning_param_update) || 556721debf25SMatthias Ringwald !hci_stack->le_scanning_enabled || 5568d54b2c94SMatthias Ringwald (scanning_uses_whitelist && whitelist_modification_pending) || 556951e51a58SMatthias Ringwald resolving_list_modification_pending || 557051e51a58SMatthias Ringwald random_address_change){ 557121debf25SMatthias Ringwald 55722d5c2a27SMatthias Ringwald scanning_stop = true; 5573fde725feSMatthias Ringwald } 5574fde725feSMatthias Ringwald } 5575fde725feSMatthias Ringwald 557629c24bebSMatthias Ringwald // connecting control 5577f496d06eSMatthias Ringwald bool connecting_with_whitelist; 5578f496d06eSMatthias Ringwald switch (hci_stack->le_connecting_state){ 5579f496d06eSMatthias Ringwald case LE_CONNECTING_DIRECT: 5580f496d06eSMatthias Ringwald case LE_CONNECTING_WHITELIST: 5581af64f147SMatthias Ringwald // stop connecting if: 5582af64f147SMatthias Ringwald // - connecting uses white and whitelist modification pending 5583af64f147SMatthias Ringwald // - if it got disabled 558421debf25SMatthias Ringwald // - resolving list modified 558551e51a58SMatthias Ringwald // - own address changes 5586f496d06eSMatthias Ringwald connecting_with_whitelist = hci_stack->le_connecting_state == LE_CONNECTING_WHITELIST; 5587f496d06eSMatthias Ringwald if ((connecting_with_whitelist && whitelist_modification_pending) || 558821debf25SMatthias Ringwald (hci_stack->le_connecting_request == LE_CONNECTING_IDLE) || 558951e51a58SMatthias Ringwald resolving_list_modification_pending || 559051e51a58SMatthias Ringwald random_address_change) { 559121debf25SMatthias Ringwald 559229c24bebSMatthias Ringwald connecting_stop = true; 559329c24bebSMatthias Ringwald } 5594f496d06eSMatthias Ringwald break; 5595f496d06eSMatthias Ringwald default: 5596f496d06eSMatthias Ringwald break; 559729c24bebSMatthias Ringwald } 559849a6c69cSMatthias Ringwald 55994bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 560049a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 56014bafedd5SMatthias Ringwald // periodic sync control 56024bafedd5SMatthias Ringwald bool sync_with_advertiser_list; 56034bafedd5SMatthias Ringwald switch(hci_stack->le_periodic_sync_state){ 56044bafedd5SMatthias Ringwald case LE_CONNECTING_DIRECT: 56054bafedd5SMatthias Ringwald case LE_CONNECTING_WHITELIST: 560649a6c69cSMatthias Ringwald // stop sync if: 56074bafedd5SMatthias Ringwald // - sync with advertiser list and advertiser list modification pending 560849a6c69cSMatthias Ringwald // - if it got disabled 56094bafedd5SMatthias Ringwald sync_with_advertiser_list = hci_stack->le_periodic_sync_state == LE_CONNECTING_WHITELIST; 56104bafedd5SMatthias Ringwald if ((sync_with_advertiser_list && periodic_list_modification_pending) || 56114bafedd5SMatthias Ringwald (hci_stack->le_periodic_sync_request == LE_CONNECTING_IDLE)){ 56124bafedd5SMatthias Ringwald periodic_sync_stop = true; 56134bafedd5SMatthias Ringwald } 56144bafedd5SMatthias Ringwald break; 56154bafedd5SMatthias Ringwald default: 56164bafedd5SMatthias Ringwald break; 56174bafedd5SMatthias Ringwald } 56184bafedd5SMatthias Ringwald #endif 5619d70217a2SMatthias Ringwald #endif 562029c24bebSMatthias Ringwald 562149a6c69cSMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 562249a6c69cSMatthias Ringwald 5623d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 562445c102fdSMatthias Ringwald // le advertisement control 5625935aa76eSMatthias Ringwald if ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0){ 562629c24bebSMatthias Ringwald // stop if: 562729c24bebSMatthias Ringwald // - parameter change required 56289136bde8SMatthias Ringwald // - random address used in advertising and changes 562929c24bebSMatthias Ringwald // - it's disabled 5630ba44ad41SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 563121debf25SMatthias Ringwald // - resolving list modified 563251e51a58SMatthias Ringwald // - own address changes 5633a61834b6SMatthias Ringwald bool advertising_uses_whitelist = hci_stack->le_advertisements_filter_policy != 0; 56349136bde8SMatthias Ringwald bool advertising_uses_random_address = hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC; 56354e5d21eaSMatthias Ringwald bool advertising_change = (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 5636a61834b6SMatthias Ringwald if (advertising_change || 56379136bde8SMatthias Ringwald (advertising_uses_random_address && random_address_change) || 5638a61834b6SMatthias Ringwald (hci_stack->le_advertisements_enabled_for_current_roles == 0) || 56399136bde8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 564051e51a58SMatthias Ringwald resolving_list_modification_pending || 564151e51a58SMatthias Ringwald random_address_change) { 564221debf25SMatthias Ringwald 5643fde725feSMatthias Ringwald advertising_stop = true; 5644fde725feSMatthias Ringwald } 5645fde725feSMatthias Ringwald } 5646be7ef9d8SMatthias Ringwald 5647be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5648be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported() && (advertising_stop == false)){ 5649be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 5650be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5651be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5652be7ef9d8SMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 565353f240c0SMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) != 0) { 5654be7ef9d8SMatthias Ringwald // stop if: 5655be7ef9d8SMatthias Ringwald // - parameter change required 5656be7ef9d8SMatthias Ringwald // - random address used in connectable advertising and changes 5657be7ef9d8SMatthias Ringwald // - it's disabled 5658be7ef9d8SMatthias Ringwald // - whitelist change required but used for advertisement filter policy 5659be7ef9d8SMatthias Ringwald // - resolving list modified 5660be7ef9d8SMatthias Ringwald // - own address changes 5661be7ef9d8SMatthias Ringwald // - advertisement set will be removed 566257a04237SMatthias Ringwald bool advertising_uses_whitelist = advertising_set->extended_params.advertising_filter_policy != 0; 566357a04237SMatthias Ringwald bool advertising_connectable = (advertising_set->extended_params.advertising_event_properties & 1) != 0; 566453f240c0SMatthias Ringwald bool advertising_uses_random_address = 566553f240c0SMatthias Ringwald (advertising_set->extended_params.own_address_type != BD_ADDR_TYPE_LE_PUBLIC) && 566653f240c0SMatthias Ringwald advertising_connectable; 5667be7ef9d8SMatthias Ringwald bool advertising_parameter_change = (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0; 5668be7ef9d8SMatthias Ringwald bool advertising_enabled = (advertising_set->state & LE_ADVERTISEMENT_STATE_ENABLED) != 0; 566953f240c0SMatthias Ringwald bool advertising_set_random_address_change = 567053f240c0SMatthias Ringwald (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0; 567153f240c0SMatthias Ringwald bool advertising_set_will_be_removed = 567253f240c0SMatthias Ringwald (advertising_set->state & LE_ADVERTISEMENT_TASKS_REMOVE_SET) != 0; 5673be7ef9d8SMatthias Ringwald if (advertising_parameter_change || 5674be7ef9d8SMatthias Ringwald (advertising_uses_random_address && advertising_set_random_address_change) || 5675be7ef9d8SMatthias Ringwald (advertising_enabled == false) || 5676be7ef9d8SMatthias Ringwald (advertising_uses_whitelist && whitelist_modification_pending) || 5677be7ef9d8SMatthias Ringwald resolving_list_modification_pending || 5678be7ef9d8SMatthias Ringwald advertising_set_will_be_removed) { 5679be7ef9d8SMatthias Ringwald 5680be7ef9d8SMatthias Ringwald advertising_stop = true; 5681be7ef9d8SMatthias Ringwald advertising_stop_set = advertising_set; 5682be7ef9d8SMatthias Ringwald break; 5683be7ef9d8SMatthias Ringwald } 5684be7ef9d8SMatthias Ringwald } 5685c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 568653f240c0SMatthias Ringwald if ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) != 0) { 568753f240c0SMatthias Ringwald // stop if: 568853f240c0SMatthias Ringwald // - it's disabled 568953f240c0SMatthias Ringwald // - parameter change required 569053f240c0SMatthias Ringwald bool periodic_enabled = (advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED) != 0; 569153f240c0SMatthias Ringwald bool periodic_parameter_change = (advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS) != 0; 569253f240c0SMatthias Ringwald if ((periodic_enabled == false) || periodic_parameter_change){ 569349a6c69cSMatthias Ringwald periodic_advertising_stop = true; 569453f240c0SMatthias Ringwald advertising_stop_set = advertising_set; 569553f240c0SMatthias Ringwald } 569653f240c0SMatthias Ringwald } 569749a6c69cSMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 569853f240c0SMatthias Ringwald } 5699be7ef9d8SMatthias Ringwald } 5700be7ef9d8SMatthias Ringwald #endif 5701be7ef9d8SMatthias Ringwald 5702a41310b7SMatthias Ringwald #endif 5703fde725feSMatthias Ringwald 5704a41310b7SMatthias Ringwald 5705a41310b7SMatthias Ringwald // Phase 2: stop everything that should be off during modifications 5706a41310b7SMatthias Ringwald 5707131d4778SMatthias Ringwald 5708131d4778SMatthias Ringwald // 2.1 Outgoing connection 5709131d4778SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5710131d4778SMatthias Ringwald if (connecting_stop){ 5711131d4778SMatthias Ringwald hci_send_cmd(&hci_le_create_connection_cancel); 5712131d4778SMatthias Ringwald return true; 5713131d4778SMatthias Ringwald } 5714131d4778SMatthias Ringwald #endif 5715131d4778SMatthias Ringwald 5716131d4778SMatthias Ringwald // 2.2 Scanning 5717a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5718a41310b7SMatthias Ringwald if (scanning_stop){ 57195226d7f2SMatthias Ringwald hci_stack->le_scanning_active = false; 5720c42da3bcSMatthias Ringwald hci_le_scan_stop(); 5721a41310b7SMatthias Ringwald return true; 5722a41310b7SMatthias Ringwald } 5723a41310b7SMatthias Ringwald 5724131d4778SMatthias Ringwald // 2.3 Periodic Sync 57254bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 572649a6c69cSMatthias Ringwald if (hci_stack->le_periodic_terminate_sync_handle != HCI_CON_HANDLE_INVALID){ 572749a6c69cSMatthias Ringwald uint16_t sync_handle = hci_stack->le_periodic_terminate_sync_handle; 572849a6c69cSMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = HCI_CON_HANDLE_INVALID; 572949a6c69cSMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_terminate_sync, sync_handle); 573049a6c69cSMatthias Ringwald return true; 573149a6c69cSMatthias Ringwald } 573249a6c69cSMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 57334bafedd5SMatthias Ringwald if (periodic_sync_stop){ 57344bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_state = LE_CONNECTING_CANCEL; 57354bafedd5SMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_create_sync_cancel); 57364bafedd5SMatthias Ringwald return true; 57374bafedd5SMatthias Ringwald } 573849a6c69cSMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5739a8016f96SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 5740a8016f96SMatthias Ringwald #endif /* ENABLE_LE_CENTRAL */ 5741a41310b7SMatthias Ringwald 5742131d4778SMatthias Ringwald // 2.4 Advertising: legacy, extended, periodic 5743a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 5744fde725feSMatthias Ringwald if (advertising_stop){ 5745e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5746e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 5747be7ef9d8SMatthias Ringwald uint8_t advertising_stop_handle; 5748be7ef9d8SMatthias Ringwald if (advertising_stop_set != NULL){ 5749be7ef9d8SMatthias Ringwald advertising_stop_handle = advertising_stop_set->advertising_handle; 5750be7ef9d8SMatthias Ringwald advertising_stop_set->state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5751be7ef9d8SMatthias Ringwald } else { 5752be7ef9d8SMatthias Ringwald advertising_stop_handle = 0; 5753be7ef9d8SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 5754be7ef9d8SMatthias Ringwald } 5755be7ef9d8SMatthias Ringwald const uint8_t advertising_handles[] = { advertising_stop_handle }; 5756e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 5757e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 5758e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 0, 1, advertising_handles, durations, max_events); 5759e464cd48SMatthias Ringwald } else 5760e464cd48SMatthias Ringwald #endif 5761e464cd48SMatthias Ringwald { 5762be7ef9d8SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ACTIVE; 576345c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 0); 5764e464cd48SMatthias Ringwald } 5765f30077b7SMatthias Ringwald return true; 576645c102fdSMatthias Ringwald } 576753f240c0SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5768c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 576949a6c69cSMatthias Ringwald if (periodic_advertising_stop){ 577053f240c0SMatthias Ringwald advertising_stop_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 577153f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, 0, advertising_stop_set->advertising_handle); 577253f240c0SMatthias Ringwald return true; 577353f240c0SMatthias Ringwald } 5774c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5775c814a904SMatthias Ringwald #endif /* ENABLE_LE_EXTENDED_ADVERTISING */ 5776a8016f96SMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 5777fde725feSMatthias Ringwald 5778131d4778SMatthias Ringwald 5779a41310b7SMatthias Ringwald // Phase 3: modify 5780a41310b7SMatthias Ringwald 57819136bde8SMatthias Ringwald if (random_address_change){ 57829136bde8SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 5783e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5784e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()) { 5785e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, 0, hci_stack->le_random_address); 5786e464cd48SMatthias Ringwald } 5787e464cd48SMatthias Ringwald #endif 5788e464cd48SMatthias Ringwald { 57899136bde8SMatthias Ringwald hci_send_cmd(&hci_le_set_random_address, hci_stack->le_random_address); 5790e464cd48SMatthias Ringwald } 57919136bde8SMatthias Ringwald return true; 57929136bde8SMatthias Ringwald } 57939136bde8SMatthias Ringwald 5794a41310b7SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 5795a41310b7SMatthias Ringwald if (hci_stack->le_scanning_param_update){ 5796a41310b7SMatthias Ringwald hci_stack->le_scanning_param_update = false; 579763671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 579863671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 579963671e69SMatthias Ringwald // prepare arrays for all PHYs 580063671e69SMatthias Ringwald uint8_t scan_types[1] = { hci_stack->le_scan_type }; 580163671e69SMatthias Ringwald uint16_t scan_intervals[1] = { hci_stack->le_scan_interval }; 580263671e69SMatthias Ringwald uint16_t scan_windows[1] = { hci_stack->le_scan_window }; 580363671e69SMatthias Ringwald uint8_t scanning_phys = 1; // LE 1M PHY 580463671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_parameters, hci_stack->le_own_addr_type, 580563671e69SMatthias Ringwald hci_stack->le_scan_filter_policy, scanning_phys, scan_types, scan_intervals, scan_windows); 580663671e69SMatthias Ringwald } else 580763671e69SMatthias Ringwald #endif 580863671e69SMatthias Ringwald { 5809a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_parameters, hci_stack->le_scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, 5810a41310b7SMatthias Ringwald hci_stack->le_own_addr_type, hci_stack->le_scan_filter_policy); 581163671e69SMatthias Ringwald } 5812a41310b7SMatthias Ringwald return true; 5813a41310b7SMatthias Ringwald } 5814a41310b7SMatthias Ringwald #endif 5815a41310b7SMatthias Ringwald 5816a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 581745c102fdSMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_PARAMS){ 581845c102fdSMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 58196bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type = hci_stack->le_own_addr_type; 5820e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5821e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5822e464cd48SMatthias Ringwald // map advertisment type to advertising event properties 5823e464cd48SMatthias Ringwald uint16_t adv_event_properties = 0; 5824e464cd48SMatthias Ringwald const uint16_t mapping[] = { 0b00010011, 0b00010101, 0b00011101, 0b00010010, 0b00010000}; 5825e464cd48SMatthias Ringwald if (hci_stack->le_advertisements_type < (sizeof(mapping)/sizeof(uint16_t))){ 5826e464cd48SMatthias Ringwald adv_event_properties = mapping[hci_stack->le_advertisements_type]; 5827e464cd48SMatthias Ringwald } 5828174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5829e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 5830e464cd48SMatthias Ringwald 0, 5831e464cd48SMatthias Ringwald adv_event_properties, 5832e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_min, 5833e464cd48SMatthias Ringwald hci_stack->le_advertisements_interval_max, 5834e464cd48SMatthias Ringwald hci_stack->le_advertisements_channel_map, 5835e464cd48SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 5836e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 5837e464cd48SMatthias Ringwald hci_stack->le_advertisements_direct_address, 5838e464cd48SMatthias Ringwald hci_stack->le_advertisements_filter_policy, 5839e464cd48SMatthias Ringwald 0x7f, // tx power: no preference 5840e464cd48SMatthias Ringwald 0x01, // primary adv phy: LE 1M 5841e464cd48SMatthias Ringwald 0, // secondary adv max skip 5842e464cd48SMatthias Ringwald 0, // secondary adv phy 5843e464cd48SMatthias Ringwald 0, // adv sid 5844e464cd48SMatthias Ringwald 0 // scan request notification 5845e464cd48SMatthias Ringwald ); 5846e464cd48SMatthias Ringwald } 5847e464cd48SMatthias Ringwald #endif 5848e464cd48SMatthias Ringwald { 584945c102fdSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_parameters, 585045c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min, 585145c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max, 585245c102fdSMatthias Ringwald hci_stack->le_advertisements_type, 58536bcfa632SMatthias Ringwald hci_stack->le_advertisements_own_addr_type, 585445c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type, 585545c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address, 585645c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map, 585745c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy); 5858e464cd48SMatthias Ringwald } 5859f30077b7SMatthias Ringwald return true; 586045c102fdSMatthias Ringwald } 5861be7ef9d8SMatthias Ringwald 5862501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA){ 5863501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 58647a85e4f5SMatthias Ringwald uint8_t adv_data_clean[31]; 58657a85e4f5SMatthias Ringwald memset(adv_data_clean, 0, sizeof(adv_data_clean)); 58666535961aSMatthias Ringwald (void)memcpy(adv_data_clean, hci_stack->le_advertisements_data, 58676535961aSMatthias Ringwald hci_stack->le_advertisements_data_len); 58683c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(adv_data_clean, hci_stack->le_advertisements_data_len, hci_stack->local_bd_addr); 5869e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5870e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5871174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5872e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, 0, 0x03, 0x01, hci_stack->le_advertisements_data_len, adv_data_clean); 5873e464cd48SMatthias Ringwald } else 5874e464cd48SMatthias Ringwald #endif 5875e464cd48SMatthias Ringwald { 58767a85e4f5SMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_data, hci_stack->le_advertisements_data_len, adv_data_clean); 5877e464cd48SMatthias Ringwald } 5878f30077b7SMatthias Ringwald return true; 587945c102fdSMatthias Ringwald } 5880be7ef9d8SMatthias Ringwald 5881501f56b3SMatthias Ringwald if (hci_stack->le_advertisements_todo & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA){ 5882501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5883f868b059SMatthias Ringwald uint8_t scan_data_clean[31]; 5884f868b059SMatthias Ringwald memset(scan_data_clean, 0, sizeof(scan_data_clean)); 58856535961aSMatthias Ringwald (void)memcpy(scan_data_clean, hci_stack->le_scan_response_data, 58866535961aSMatthias Ringwald hci_stack->le_scan_response_data_len); 58873c9da642SMatthias Ringwald btstack_replace_bd_addr_placeholder(scan_data_clean, hci_stack->le_scan_response_data_len, hci_stack->local_bd_addr); 5888e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5889e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 5890174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = 0; 5891e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, 0, 0x03, 0x01, hci_stack->le_scan_response_data_len, scan_data_clean); 5892e464cd48SMatthias Ringwald } else 5893e464cd48SMatthias Ringwald #endif 5894e464cd48SMatthias Ringwald { 5895214bfd60SMatthias Ringwald hci_send_cmd(&hci_le_set_scan_response_data, hci_stack->le_scan_response_data_len, scan_data_clean); 5896e464cd48SMatthias Ringwald } 5897f30077b7SMatthias Ringwald return true; 5898501f56b3SMatthias Ringwald } 5899be7ef9d8SMatthias Ringwald 5900be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 5901be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported()) { 5902be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 5903be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 5904be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5905be7ef9d8SMatthias Ringwald le_advertising_set_t * advertising_set = (le_advertising_set_t*) btstack_linked_list_iterator_next(&it); 5906be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_REMOVE_SET) != 0) { 5907be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_REMOVE_SET; 5908174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5909be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_remove_advertising_set, advertising_set->advertising_handle); 5910be7ef9d8SMatthias Ringwald return true; 5911be7ef9d8SMatthias Ringwald } 5912f66adbdeSMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADDRESS) != 0){ 5913f66adbdeSMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 5914f66adbdeSMatthias Ringwald hci_send_cmd(&hci_le_set_advertising_set_random_address, advertising_set->advertising_handle, advertising_set->random_address); 5915f66adbdeSMatthias Ringwald return true; 5916f66adbdeSMatthias Ringwald } 5917be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PARAMS) != 0){ 5918be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PARAMS; 5919174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5920be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_parameters, 5921be7ef9d8SMatthias Ringwald advertising_set->advertising_handle, 592257a04237SMatthias Ringwald advertising_set->extended_params.advertising_event_properties, 592357a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_interval_min, 592457a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_interval_max, 592557a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_channel_map, 592657a04237SMatthias Ringwald advertising_set->extended_params.own_address_type, 592757a04237SMatthias Ringwald advertising_set->extended_params.peer_address_type, 592857a04237SMatthias Ringwald advertising_set->extended_params.peer_address, 592957a04237SMatthias Ringwald advertising_set->extended_params.advertising_filter_policy, 593057a04237SMatthias Ringwald advertising_set->extended_params.advertising_tx_power, 593157a04237SMatthias Ringwald advertising_set->extended_params.primary_advertising_phy, 593257a04237SMatthias Ringwald advertising_set->extended_params.secondary_advertising_max_skip, 593357a04237SMatthias Ringwald advertising_set->extended_params.secondary_advertising_phy, 593457a04237SMatthias Ringwald advertising_set->extended_params.advertising_sid, 593557a04237SMatthias Ringwald advertising_set->extended_params.scan_request_notification_enable 5936be7ef9d8SMatthias Ringwald ); 5937be7ef9d8SMatthias Ringwald return true; 5938be7ef9d8SMatthias Ringwald } 5939be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_ADV_DATA) != 0) { 5940be7ef9d8SMatthias Ringwald uint16_t pos = advertising_set->adv_data_pos; 5941be7ef9d8SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->adv_data_len); 5942be7ef9d8SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->adv_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 5943be7ef9d8SMatthias Ringwald if ((operation & 0x02) != 0){ 5944be7ef9d8SMatthias Ringwald // last fragment or complete data 5945be7ef9d8SMatthias Ringwald operation |= 2; 5946be7ef9d8SMatthias Ringwald advertising_set->adv_data_pos = 0; 5947be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 5948be7ef9d8SMatthias Ringwald } else { 5949be7ef9d8SMatthias Ringwald advertising_set->adv_data_pos += data_to_upload; 5950be7ef9d8SMatthias Ringwald } 5951174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 59520cbe4690SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_data, advertising_set->advertising_handle, operation, 0x01, data_to_upload, &advertising_set->adv_data[pos]); 5953be7ef9d8SMatthias Ringwald return true; 5954be7ef9d8SMatthias Ringwald } 5955be7ef9d8SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA) != 0) { 5956be7ef9d8SMatthias Ringwald uint16_t pos = advertising_set->scan_data_pos; 5957be7ef9d8SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->scan_data_len); 5958be7ef9d8SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->scan_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 5959be7ef9d8SMatthias Ringwald if ((operation & 0x02) != 0){ 5960be7ef9d8SMatthias Ringwald advertising_set->scan_data_pos = 0; 5961be7ef9d8SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 5962be7ef9d8SMatthias Ringwald } else { 5963be7ef9d8SMatthias Ringwald advertising_set->scan_data_pos += data_to_upload; 5964be7ef9d8SMatthias Ringwald } 5965174b4d21SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 5966be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_response_data, operation, 0x03, 0x01, data_to_upload, &advertising_set->scan_data[pos]); 5967be7ef9d8SMatthias Ringwald return true; 5968be7ef9d8SMatthias Ringwald } 5969c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 597053f240c0SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS) != 0){ 597153f240c0SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS; 597253f240c0SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 597353f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_parameters, 597453f240c0SMatthias Ringwald advertising_set->advertising_handle, 597553f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_interval_min, 597653f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_interval_max, 597753f240c0SMatthias Ringwald advertising_set->periodic_params.periodic_advertising_properties); 597853f240c0SMatthias Ringwald return true; 597953f240c0SMatthias Ringwald } 598053f240c0SMatthias Ringwald if ((advertising_set->tasks & LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA) != 0) { 59810cbe4690SMatthias Ringwald uint16_t pos = advertising_set->periodic_data_pos; 598253f240c0SMatthias Ringwald uint8_t operation = hci_le_extended_advertising_operation_for_chunk(pos, advertising_set->periodic_data_len); 598353f240c0SMatthias Ringwald uint16_t data_to_upload = btstack_min(advertising_set->periodic_data_len - pos, LE_EXTENDED_ADVERTISING_MAX_CHUNK_LEN); 598453f240c0SMatthias Ringwald if ((operation & 0x02) != 0){ 598553f240c0SMatthias Ringwald // last fragment or complete data 598653f240c0SMatthias Ringwald operation |= 2; 59870cbe4690SMatthias Ringwald advertising_set->periodic_data_pos = 0; 598853f240c0SMatthias Ringwald advertising_set->tasks &= ~LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA; 598953f240c0SMatthias Ringwald } else { 59900cbe4690SMatthias Ringwald advertising_set->periodic_data_pos += data_to_upload; 599153f240c0SMatthias Ringwald } 599253f240c0SMatthias Ringwald hci_stack->le_advertising_set_in_current_command = advertising_set->advertising_handle; 59930cbe4690SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_data, advertising_set->advertising_handle, operation, data_to_upload, &advertising_set->periodic_data[pos]); 599453f240c0SMatthias Ringwald return true; 599553f240c0SMatthias Ringwald } 5996c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 5997be7ef9d8SMatthias Ringwald } 5998be7ef9d8SMatthias Ringwald } 5999be7ef9d8SMatthias Ringwald #endif 6000be7ef9d8SMatthias Ringwald 6001d70217a2SMatthias Ringwald #endif 60029956955bSMatthias Ringwald 6003057ab60cSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6004a41310b7SMatthias Ringwald // if connect with whitelist was active and is not cancelled yet, wait until next time 6005a41310b7SMatthias Ringwald if (hci_stack->le_connecting_state == LE_CONNECTING_CANCEL) return false; 60064bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 60074bafedd5SMatthias Ringwald // if periodic sync with advertiser list was active and is not cancelled yet, wait until next time 60084bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_state == LE_CONNECTING_CANCEL) return false; 60094bafedd5SMatthias Ringwald #endif 6010057ab60cSMatthias Ringwald #endif 6011057ab60cSMatthias Ringwald 6012a41310b7SMatthias Ringwald // LE Whitelist Management 6013a41310b7SMatthias Ringwald if (whitelist_modification_pending){ 60149956955bSMatthias Ringwald // add/remove entries 6015665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 6016665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 6017665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 6018453459ddSMatthias Ringwald if (entry->state & LE_WHITELIST_REMOVE_FROM_CONTROLLER){ 6019453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_REMOVE_FROM_CONTROLLER; 6020453459ddSMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_white_list, entry->address_type, entry->address); 6021453459ddSMatthias Ringwald return true; 6022453459ddSMatthias Ringwald } 60239956955bSMatthias Ringwald if (entry->state & LE_WHITELIST_ADD_TO_CONTROLLER){ 6024453459ddSMatthias Ringwald entry->state &= ~LE_WHITELIST_ADD_TO_CONTROLLER; 6025453459ddSMatthias Ringwald entry->state |= LE_WHITELIST_ON_CONTROLLER; 60269956955bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_white_list, entry->address_type, entry->address); 6027f30077b7SMatthias Ringwald return true; 60289956955bSMatthias Ringwald } 6029453459ddSMatthias Ringwald if ((entry->state & LE_WHITELIST_ON_CONTROLLER) == 0){ 6030665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 60319956955bSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 60329956955bSMatthias Ringwald } 60339956955bSMatthias Ringwald } 603491915b0bSMatthias Ringwald } 60359956955bSMatthias Ringwald 603621debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 603721debf25SMatthias Ringwald // LE Resolving List Management 6038ea151974SMatthias Ringwald if (resolving_list_supported) { 603921debf25SMatthias Ringwald uint16_t i; 604021debf25SMatthias Ringwald switch (hci_stack->le_resolving_list_state) { 604121debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION: 604221debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 604321debf25SMatthias Ringwald hci_send_cmd(&hci_le_set_address_resolution_enabled, 1); 604421debf25SMatthias Ringwald return true; 604521debf25SMatthias Ringwald case LE_RESOLVING_LIST_READ_SIZE: 604621debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_SEND_CLEAR; 604721debf25SMatthias Ringwald hci_send_cmd(&hci_le_read_resolving_list_size); 604821debf25SMatthias Ringwald return true; 604921debf25SMatthias Ringwald case LE_RESOLVING_LIST_SEND_CLEAR: 605002b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 6051ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_add_entries, 0xff, 6052ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_add_entries)); 6053ea151974SMatthias Ringwald (void) memset(hci_stack->le_resolving_list_remove_entries, 0, 6054ea151974SMatthias Ringwald sizeof(hci_stack->le_resolving_list_remove_entries)); 605521debf25SMatthias Ringwald hci_send_cmd(&hci_le_clear_resolving_list); 605621debf25SMatthias Ringwald return true; 605702b02cffSMatthias Ringwald case LE_RESOLVING_LIST_REMOVE_ENTRIES: 605802b02cffSMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 605902b02cffSMatthias Ringwald uint8_t offset = i >> 3; 606002b02cffSMatthias Ringwald uint8_t mask = 1 << (i & 7); 606102b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_remove_entries[offset] & mask) == 0) continue; 606202b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] &= ~mask; 606302b02cffSMatthias Ringwald bd_addr_t peer_identity_addreses; 606402b02cffSMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 606502b02cffSMatthias Ringwald sm_key_t peer_irk; 606602b02cffSMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 606702b02cffSMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 6068f5228c62SMatthias Ringwald 6069f5228c62SMatthias Ringwald #ifdef ENABLE_LE_WHITELIST_TOUCH_AFTER_RESOLVING_LIST_UPDATE 6070f5228c62SMatthias Ringwald // trigger whitelist entry 'update' (work around for controller bug) 6071f5228c62SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 6072f5228c62SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)) { 6073f5228c62SMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&lit); 6074f5228c62SMatthias Ringwald if (entry->address_type != peer_identity_addr_type) continue; 6075f5228c62SMatthias Ringwald if (memcmp(entry->address, peer_identity_addreses, 6) != 0) continue; 6076f5228c62SMatthias Ringwald log_info("trigger whitelist update %s", bd_addr_to_str(peer_identity_addreses)); 6077f5228c62SMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER | LE_WHITELIST_ADD_TO_CONTROLLER; 6078f5228c62SMatthias Ringwald } 6079f5228c62SMatthias Ringwald #endif 6080f5228c62SMatthias Ringwald 6081ea151974SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_resolving_list, peer_identity_addr_type, 6082ea151974SMatthias Ringwald peer_identity_addreses); 608302b02cffSMatthias Ringwald return true; 608402b02cffSMatthias Ringwald } 608502b02cffSMatthias Ringwald 608602b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_ADD_ENTRIES; 608702b02cffSMatthias Ringwald 608802b02cffSMatthias Ringwald /* fall through */ 608902b02cffSMatthias Ringwald 609021debf25SMatthias Ringwald case LE_RESOLVING_LIST_ADD_ENTRIES: 609121debf25SMatthias Ringwald for (i = 0; i < MAX_NUM_RESOLVING_LIST_ENTRIES && i < le_device_db_max_count(); i++) { 609221debf25SMatthias Ringwald uint8_t offset = i >> 3; 609321debf25SMatthias Ringwald uint8_t mask = 1 << (i & 7); 609402b02cffSMatthias Ringwald if ((hci_stack->le_resolving_list_add_entries[offset] & mask) == 0) continue; 609502b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] &= ~mask; 609621debf25SMatthias Ringwald bd_addr_t peer_identity_addreses; 609721debf25SMatthias Ringwald int peer_identity_addr_type = (int) BD_ADDR_TYPE_UNKNOWN; 609821debf25SMatthias Ringwald sm_key_t peer_irk; 609921debf25SMatthias Ringwald le_device_db_info(i, &peer_identity_addr_type, peer_identity_addreses, peer_irk); 610021debf25SMatthias Ringwald if (peer_identity_addr_type == BD_ADDR_TYPE_UNKNOWN) continue; 6101e938c062SMatthias Ringwald if (btstack_is_null(peer_irk, 16)) continue; 610221debf25SMatthias Ringwald const uint8_t *local_irk = gap_get_persistent_irk(); 610321debf25SMatthias Ringwald // command uses format specifier 'P' that stores 16-byte value without flip 610421debf25SMatthias Ringwald uint8_t local_irk_flipped[16]; 610521debf25SMatthias Ringwald uint8_t peer_irk_flipped[16]; 610621debf25SMatthias Ringwald reverse_128(local_irk, local_irk_flipped); 610721debf25SMatthias Ringwald reverse_128(peer_irk, peer_irk_flipped); 6108ea151974SMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_resolving_list, peer_identity_addr_type, peer_identity_addreses, 6109ea151974SMatthias Ringwald peer_irk_flipped, local_irk_flipped); 611021debf25SMatthias Ringwald return true; 611121debf25SMatthias Ringwald } 611202b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 611321debf25SMatthias Ringwald break; 611402b02cffSMatthias Ringwald 611521debf25SMatthias Ringwald default: 611621debf25SMatthias Ringwald break; 611721debf25SMatthias Ringwald } 6118ea151974SMatthias Ringwald } 611921debf25SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_DONE; 612021debf25SMatthias Ringwald #endif 6121a41310b7SMatthias Ringwald 6122f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6123f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6124f40c73b4SMatthias Ringwald // LE Whitelist Management 6125f40c73b4SMatthias Ringwald if (periodic_list_modification_pending){ 6126f40c73b4SMatthias Ringwald // add/remove entries 6127f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 6128f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 6129f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 6130f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER){ 6131f40c73b4SMatthias Ringwald entry->state &= ~LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 6132f40c73b4SMatthias Ringwald hci_send_cmd(&hci_le_remove_device_from_periodic_advertiser_list, entry->address_type, entry->address); 6133f40c73b4SMatthias Ringwald return true; 6134f40c73b4SMatthias Ringwald } 6135f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER){ 6136f40c73b4SMatthias Ringwald entry->state &= ~LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 6137f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER; 61389e88556bSMatthias Ringwald hci_send_cmd(&hci_le_add_device_to_periodic_advertiser_list, entry->address_type, entry->address, entry->sid); 6139f40c73b4SMatthias Ringwald return true; 6140f40c73b4SMatthias Ringwald } 6141f40c73b4SMatthias Ringwald if ((entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER) == 0){ 6142f40c73b4SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t *) entry); 6143f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 6144f40c73b4SMatthias Ringwald } 6145f40c73b4SMatthias Ringwald } 6146f40c73b4SMatthias Ringwald } 6147f40c73b4SMatthias Ringwald #endif 6148f40c73b4SMatthias Ringwald #endif 6149f40c73b4SMatthias Ringwald 615073799937SMatthias Ringwald // post-pone all actions until stack is fully working 615173799937SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return false; 615273799937SMatthias Ringwald 615373799937SMatthias Ringwald // advertisements, active scanning, and creating connections requires random address to be set if using private address 615473799937SMatthias Ringwald if ( (hci_stack->le_own_addr_type != BD_ADDR_TYPE_LE_PUBLIC) && (hci_stack->le_random_address_set == 0u) ) return false; 615573799937SMatthias Ringwald 6156a41310b7SMatthias Ringwald // Phase 4: restore state 615729c24bebSMatthias Ringwald 615829c24bebSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6159af64f147SMatthias Ringwald // re-start scanning 616029c24bebSMatthias Ringwald if ((hci_stack->le_scanning_enabled && !hci_stack->le_scanning_active)){ 616129c24bebSMatthias Ringwald hci_stack->le_scanning_active = true; 616263671e69SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 616363671e69SMatthias Ringwald if (hci_extended_advertising_supported()){ 616463671e69SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_scan_enable, 1, 0, 0, 0); 616563671e69SMatthias Ringwald } else 616663671e69SMatthias Ringwald #endif 616763671e69SMatthias Ringwald { 616829c24bebSMatthias Ringwald hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 616963671e69SMatthias Ringwald } 617029c24bebSMatthias Ringwald return true; 617129c24bebSMatthias Ringwald } 617229c24bebSMatthias Ringwald #endif 617329c24bebSMatthias Ringwald 617413eb2a2eSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 6175af64f147SMatthias Ringwald // re-start connecting 6176af64f147SMatthias Ringwald if ( (hci_stack->le_connecting_state == LE_CONNECTING_IDLE) && (hci_stack->le_connecting_request == LE_CONNECTING_WHITELIST)){ 61779956955bSMatthias Ringwald bd_addr_t null_addr; 61789956955bSMatthias Ringwald memset(null_addr, 0, 6); 61796bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 61806bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 61819956955bSMatthias Ringwald hci_send_cmd(&hci_le_create_connection, 6182cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // scan interval: 60 ms 6183cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // scan interval: 30 ms 61849956955bSMatthias Ringwald 1, // use whitelist 61859956955bSMatthias Ringwald 0, // peer address type 61869956955bSMatthias Ringwald null_addr, // peer bd addr 61876bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 618873044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 618973044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 619073044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 619173044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 619273044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 619373044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 61949956955bSMatthias Ringwald ); 6195f30077b7SMatthias Ringwald return true; 61969956955bSMatthias Ringwald } 61974bafedd5SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 61984bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_state == LE_CONNECTING_IDLE){ 61994bafedd5SMatthias Ringwald switch(hci_stack->le_periodic_sync_request){ 62004bafedd5SMatthias Ringwald case LE_CONNECTING_DIRECT: 62014bafedd5SMatthias Ringwald case LE_CONNECTING_WHITELIST: 62024bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_state = ((hci_stack->le_periodic_sync_options & 1) != 0) ? LE_CONNECTING_WHITELIST : LE_CONNECTING_DIRECT; 62034bafedd5SMatthias Ringwald hci_send_cmd(&hci_le_periodic_advertising_create_sync, 62044bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_options, 62054bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertising_sid, 62064bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address_type, 62074bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address, 62084bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_skip, 62094bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_timeout, 62104bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_cte_type); 62114bafedd5SMatthias Ringwald return true; 62124bafedd5SMatthias Ringwald default: 62134bafedd5SMatthias Ringwald break; 62144bafedd5SMatthias Ringwald } 62154bafedd5SMatthias Ringwald } 62164bafedd5SMatthias Ringwald #endif 6217d70217a2SMatthias Ringwald #endif 6218a41310b7SMatthias Ringwald 6219a41310b7SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 6220a41310b7SMatthias Ringwald // re-start advertising 6221935aa76eSMatthias Ringwald if (hci_stack->le_advertisements_enabled_for_current_roles && ((hci_stack->le_advertisements_state & LE_ADVERTISEMENT_STATE_ACTIVE) == 0)){ 6222a41310b7SMatthias Ringwald // check if advertisements should be enabled given 6223935aa76eSMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_ACTIVE; 6224b892db1cSMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_advertisements_own_addr_type, hci_stack->le_advertisements_own_address); 6225e464cd48SMatthias Ringwald 6226e464cd48SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6227e464cd48SMatthias Ringwald if (hci_extended_advertising_supported()){ 6228e464cd48SMatthias Ringwald const uint8_t advertising_handles[] = { 0 }; 6229e464cd48SMatthias Ringwald const uint16_t durations[] = { 0 }; 6230e464cd48SMatthias Ringwald const uint16_t max_events[] = { 0 }; 6231e464cd48SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 6232e464cd48SMatthias Ringwald } else 6233e464cd48SMatthias Ringwald #endif 6234e464cd48SMatthias Ringwald { 6235a41310b7SMatthias Ringwald hci_send_cmd(&hci_le_set_advertise_enable, 1); 6236e464cd48SMatthias Ringwald } 6237a41310b7SMatthias Ringwald return true; 6238a41310b7SMatthias Ringwald } 6239be7ef9d8SMatthias Ringwald 6240be7ef9d8SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6241be7ef9d8SMatthias Ringwald if (hci_extended_advertising_supported()) { 6242be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_t it; 6243be7ef9d8SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 6244be7ef9d8SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 6245be7ef9d8SMatthias Ringwald le_advertising_set_t *advertising_set = (le_advertising_set_t *) btstack_linked_list_iterator_next(&it); 6246be7ef9d8SMatthias Ringwald if (((advertising_set->state & LE_ADVERTISEMENT_STATE_ENABLED) != 0) && ((advertising_set->state & LE_ADVERTISEMENT_STATE_ACTIVE) == 0)){ 62478caa405cSMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_ACTIVE; 6248be7ef9d8SMatthias Ringwald const uint8_t advertising_handles[] = { advertising_set->advertising_handle }; 6249be7ef9d8SMatthias Ringwald const uint16_t durations[] = { advertising_set->enable_timeout }; 6250be7ef9d8SMatthias Ringwald const uint16_t max_events[] = { advertising_set->enable_max_scan_events }; 6251be7ef9d8SMatthias Ringwald hci_send_cmd(&hci_le_set_extended_advertising_enable, 1, 1, advertising_handles, durations, max_events); 6252be7ef9d8SMatthias Ringwald return true; 6253be7ef9d8SMatthias Ringwald } 6254c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 625553f240c0SMatthias Ringwald if (((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED) != 0) && ((advertising_set->state & LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE) == 0)){ 625653f240c0SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PERIODIC_ACTIVE; 625753f240c0SMatthias Ringwald uint8_t enable = 1; 625853f240c0SMatthias Ringwald if (advertising_set->periodic_include_adi){ 625953f240c0SMatthias Ringwald enable |= 2; 626053f240c0SMatthias Ringwald } 626153f240c0SMatthias Ringwald hci_send_cmd(&hci_le_set_periodic_advertising_enable, enable, advertising_set->advertising_handle); 626253f240c0SMatthias Ringwald return true; 626353f240c0SMatthias Ringwald } 6264c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 6265be7ef9d8SMatthias Ringwald } 6266be7ef9d8SMatthias Ringwald } 6267be7ef9d8SMatthias Ringwald #endif 6268a41310b7SMatthias Ringwald #endif 6269a41310b7SMatthias Ringwald 6270f30077b7SMatthias Ringwald return false; 62717bdc6798S[email protected] } 62727aa35f6aSMatthias Ringwald 62737aa35f6aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 62747aa35f6aSMatthias Ringwald static bool hci_run_iso_tasks(void){ 62757aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 62762a6c0f82SMatthias Ringwald 6277*d75a6bbfSMatthias Ringwald if (hci_stack->iso_active_operation_group_id != HCI_ISO_GROUP_ID_INVALID) { 627840f86dfdSMatthias Ringwald return false; 627940f86dfdSMatthias Ringwald } 628040f86dfdSMatthias Ringwald 62812a6c0f82SMatthias Ringwald // BIG 62827aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 62837aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 62847aa35f6aSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 62857aa35f6aSMatthias Ringwald switch (big->state){ 62867aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 628740f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = big->params->big_handle; 628840f86dfdSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_BIS; 62897aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_ESTABLISHED; 62907aa35f6aSMatthias Ringwald hci_send_cmd(&hci_le_create_big, 62917aa35f6aSMatthias Ringwald big->params->big_handle, 62927aa35f6aSMatthias Ringwald big->params->advertising_handle, 62937aa35f6aSMatthias Ringwald big->params->num_bis, 62947aa35f6aSMatthias Ringwald big->params->sdu_interval_us, 62957aa35f6aSMatthias Ringwald big->params->max_sdu, 62967aa35f6aSMatthias Ringwald big->params->max_transport_latency_ms, 62977aa35f6aSMatthias Ringwald big->params->rtn, 62987aa35f6aSMatthias Ringwald big->params->phy, 62997aa35f6aSMatthias Ringwald big->params->packing, 63007aa35f6aSMatthias Ringwald big->params->framing, 63017aa35f6aSMatthias Ringwald big->params->encryption, 63027aa35f6aSMatthias Ringwald big->params->broadcast_code); 63037aa35f6aSMatthias Ringwald return true; 63047aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 63057aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH; 63067aa35f6aSMatthias 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); 63077aa35f6aSMatthias Ringwald return true; 63087aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED: 63097aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED; 63102a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle, big->state_vars.status); 6311c1f83111SMatthias Ringwald return true; 63127aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_TERMINATE: 63137aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 63142a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_terminate_big, big->big_handle, ERROR_CODE_SUCCESS); 63157aa35f6aSMatthias Ringwald return true; 63167aa35f6aSMatthias Ringwald default: 63177aa35f6aSMatthias Ringwald break; 63187aa35f6aSMatthias Ringwald } 63197aa35f6aSMatthias Ringwald } 63202a6c0f82SMatthias Ringwald 63212a6c0f82SMatthias Ringwald // BIG Sync 63222a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 63232a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 63242a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 63252a6c0f82SMatthias Ringwald switch (big_sync->state){ 63262a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 632740f86dfdSMatthias Ringwald hci_stack->iso_active_operation_group_id = big_sync->params->big_handle; 632840f86dfdSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_BIS; 63292a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_ESTABLISHED; 63302a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_create_sync, 63312a6c0f82SMatthias Ringwald big_sync->params->big_handle, 63322a6c0f82SMatthias Ringwald big_sync->params->sync_handle, 63332a6c0f82SMatthias Ringwald big_sync->params->encryption, 63342a6c0f82SMatthias Ringwald big_sync->params->broadcast_code, 63352a6c0f82SMatthias Ringwald big_sync->params->mse, 63362a6c0f82SMatthias Ringwald big_sync->params->big_sync_timeout_10ms, 63372a6c0f82SMatthias Ringwald big_sync->params->num_bis, 63382a6c0f82SMatthias Ringwald big_sync->params->bis_indices); 63392a6c0f82SMatthias Ringwald return true; 63402a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 63412a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH; 63422a6c0f82SMatthias 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); 63432a6c0f82SMatthias Ringwald return true; 63442a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATHS_FAILED: 63452a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED_AFTER_SETUP_FAILED; 63462a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 6347c1f83111SMatthias Ringwald return true; 63482a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_TERMINATE: 63492a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_TERMINATED; 63502a6c0f82SMatthias Ringwald hci_send_cmd(&hci_le_big_terminate_sync, big_sync->big_handle); 63512a6c0f82SMatthias Ringwald return true; 63522a6c0f82SMatthias Ringwald default: 63532a6c0f82SMatthias Ringwald break; 63542a6c0f82SMatthias Ringwald } 63552a6c0f82SMatthias Ringwald } 63562a6c0f82SMatthias Ringwald 6357d3a89d91SMatthias Ringwald // CIG 6358d3a89d91SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_cigs); 6359d3a89d91SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 6360d3a89d91SMatthias Ringwald le_audio_cig_t *cig = (le_audio_cig_t *) btstack_linked_list_iterator_next(&it); 6361d3a89d91SMatthias Ringwald uint8_t i; 6362444e371eSMatthias Ringwald // Set CIG Parameters 6363d3a89d91SMatthias Ringwald uint8_t cis_id[MAX_NR_CIS]; 6364d3a89d91SMatthias Ringwald uint16_t max_sdu_c_to_p[MAX_NR_CIS]; 6365d3a89d91SMatthias Ringwald uint16_t max_sdu_p_to_c[MAX_NR_CIS]; 6366d3a89d91SMatthias Ringwald uint8_t phy_c_to_p[MAX_NR_CIS]; 6367d3a89d91SMatthias Ringwald uint8_t phy_p_to_c[MAX_NR_CIS]; 6368d3a89d91SMatthias Ringwald uint8_t rtn_c_to_p[MAX_NR_CIS]; 6369d3a89d91SMatthias Ringwald uint8_t rtn_p_to_c[MAX_NR_CIS]; 6370d3a89d91SMatthias Ringwald switch (cig->state) { 6371d3a89d91SMatthias Ringwald case LE_AUDIO_CIG_STATE_CREATE: 6372d3a89d91SMatthias Ringwald hci_stack->iso_active_operation_group_id = cig->params->cig_id; 6373d3a89d91SMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_CIS; 6374d3a89d91SMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_ESTABLISHED; 6375d3a89d91SMatthias Ringwald le_audio_cig_params_t * params = cig->params; 6376d3a89d91SMatthias Ringwald for (i = 0; i < params->num_cis; i++) { 6377d3a89d91SMatthias Ringwald le_audio_cis_params_t * cis_params = &cig->params->cis_params[i]; 6378d3a89d91SMatthias Ringwald cis_id[i] = cis_params->cis_id; 6379d3a89d91SMatthias Ringwald max_sdu_c_to_p[i] = cis_params->max_sdu_c_to_p; 6380d3a89d91SMatthias Ringwald max_sdu_p_to_c[i] = cis_params->max_sdu_p_to_c; 6381d3a89d91SMatthias Ringwald phy_c_to_p[i] = cis_params->phy_c_to_p; 6382d3a89d91SMatthias Ringwald phy_p_to_c[i] = cis_params->phy_p_to_c; 6383d3a89d91SMatthias Ringwald rtn_c_to_p[i] = cis_params->rtn_c_to_p; 6384d3a89d91SMatthias Ringwald rtn_p_to_c[i] = cis_params->rtn_p_to_c; 6385d3a89d91SMatthias Ringwald } 6386d3a89d91SMatthias Ringwald hci_send_cmd(&hci_le_set_cig_parameters, 6387d3a89d91SMatthias Ringwald cig->cig_id, 6388d3a89d91SMatthias Ringwald params->sdu_interval_c_to_p, 6389d3a89d91SMatthias Ringwald params->sdu_interval_p_to_c, 6390d3a89d91SMatthias Ringwald params->worst_case_sca, 6391d3a89d91SMatthias Ringwald params->packing, 6392d3a89d91SMatthias Ringwald params->framing, 6393d3a89d91SMatthias Ringwald params->max_transport_latency_c_to_p, 6394d3a89d91SMatthias Ringwald params->max_transport_latency_p_to_c, 6395d3a89d91SMatthias Ringwald params->num_cis, 6396d3a89d91SMatthias Ringwald cis_id, 6397d3a89d91SMatthias Ringwald max_sdu_c_to_p, 6398d3a89d91SMatthias Ringwald max_sdu_p_to_c, 6399d3a89d91SMatthias Ringwald phy_c_to_p, 6400d3a89d91SMatthias Ringwald phy_p_to_c, 6401d3a89d91SMatthias Ringwald rtn_c_to_p, 6402d3a89d91SMatthias Ringwald rtn_p_to_c 6403d3a89d91SMatthias Ringwald ); 6404bb42b5c0SMatthias Ringwald return true; 6405444e371eSMatthias Ringwald case LE_AUDIO_CIG_STATE_CREATE_CIS: 6406bb42b5c0SMatthias Ringwald hci_stack->iso_active_operation_group_id = cig->params->cig_id; 6407bb42b5c0SMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_CIS; 6408444e371eSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_CREATE_CIS; 64093abcd16cSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 64103abcd16cSMatthias Ringwald cig->cis_setup_active[i] = true; 64113abcd16cSMatthias Ringwald } 6412444e371eSMatthias Ringwald hci_send_cmd(&hci_le_create_cis, cig->num_cis, cig->cis_con_handles, cig->acl_con_handles); 6413bb42b5c0SMatthias Ringwald return true; 6414f9306a1aSMatthias Ringwald case LE_AUDIO_CIG_STATE_SETUP_ISO_PATH: 6415f9306a1aSMatthias Ringwald while (cig->state_vars.next_cis < (cig->num_cis * 2)){ 6416f9306a1aSMatthias Ringwald // find next path to setup 6417f9306a1aSMatthias Ringwald uint8_t cis_index = cig->state_vars.next_cis >> 1; 6418f9306a1aSMatthias Ringwald if (cig->cis_established[cis_index] == false) { 6419f9306a1aSMatthias Ringwald continue; 6420f9306a1aSMatthias Ringwald } 6421f9306a1aSMatthias Ringwald uint8_t cis_direction = cig->state_vars.next_cis & 1; 6422f9306a1aSMatthias Ringwald bool setup = true; 64238e58e4f7SMatthias Ringwald if (cis_direction == 0){ 6424f9306a1aSMatthias Ringwald // 0 - input - host to controller 6425f9306a1aSMatthias Ringwald // we are central => central to peripheral 6426f9306a1aSMatthias Ringwald setup &= cig->params->cis_params[cis_index].max_sdu_c_to_p > 0; 6427f9306a1aSMatthias Ringwald } else { 6428f9306a1aSMatthias Ringwald // 1 - output - controller to host 6429f9306a1aSMatthias Ringwald // we are central => peripheral to central 6430f9306a1aSMatthias Ringwald setup &= cig->params->cis_params[cis_index].max_sdu_p_to_c > 0; 6431f9306a1aSMatthias Ringwald } 6432f9306a1aSMatthias Ringwald if (setup){ 6433f9306a1aSMatthias Ringwald hci_stack->iso_active_operation_group_id = cig->params->cig_id; 6434f9306a1aSMatthias Ringwald hci_stack->iso_active_operation_type = HCI_ISO_TYPE_CIS; 6435f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_W4_SETUP_ISO_PATH; 64368e58e4f7SMatthias Ringwald hci_send_cmd(&hci_le_setup_iso_data_path, cig->cis_con_handles[cis_index], cis_direction, 0, 0, 0, 0, 0, 0, NULL); 6437f9306a1aSMatthias Ringwald return true; 6438f9306a1aSMatthias Ringwald } 64398e58e4f7SMatthias Ringwald cig->state_vars.next_cis++; 6440f9306a1aSMatthias Ringwald } 6441f9306a1aSMatthias Ringwald // emit done 6442f9306a1aSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_ACTIVE; 6443d3a89d91SMatthias Ringwald default: 6444d3a89d91SMatthias Ringwald break; 6445d3a89d91SMatthias Ringwald } 6446d3a89d91SMatthias Ringwald } 6447d3a89d91SMatthias Ringwald 64487aa35f6aSMatthias Ringwald return false; 64497aa35f6aSMatthias Ringwald } 64507aa35f6aSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 6451b2f949feS[email protected] #endif 64527bdc6798S[email protected] 645388a03c8dSMatthias Ringwald static bool hci_run_general_pending_commands(void){ 6454f30077b7SMatthias Ringwald btstack_linked_item_t * it; 6455a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 645605ae8de3SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) it; 645732ab9390Smatthias.ringwald 64580bf6344aS[email protected] switch(connection->state){ 64590bf6344aS[email protected] case SEND_CREATE_CONNECTION: 64604f3229d8S[email protected] switch(connection->address_type){ 646135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6462f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 64639da54300S[email protected] log_info("sending hci_create_connection"); 6464b4eb4420SMatthias Ringwald hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_stack->allow_role_switch); 64654f3229d8S[email protected] break; 646635454696SMatthias Ringwald #endif 64674f3229d8S[email protected] default: 6468a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 6469d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 64709da54300S[email protected] log_info("sending hci_le_create_connection"); 64716bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type = hci_stack->le_own_addr_type; 64726bcfa632SMatthias Ringwald hci_get_own_address_for_addr_type(hci_stack->le_connection_own_addr_type, hci_stack->le_connection_own_address); 6473e90f848dSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 6474e90f848dSMatthias Ringwald if (hci_extended_advertising_supported()) { 6475e90f848dSMatthias Ringwald uint16_t le_connection_scan_interval[1] = { hci_stack->le_connection_scan_interval }; 6476e90f848dSMatthias Ringwald uint16_t le_connection_scan_window[1] = { hci_stack->le_connection_scan_window }; 6477e90f848dSMatthias Ringwald uint16_t le_connection_interval_min[1] = { hci_stack->le_connection_interval_min }; 6478e90f848dSMatthias Ringwald uint16_t le_connection_interval_max[1] = { hci_stack->le_connection_interval_max }; 6479e90f848dSMatthias Ringwald uint16_t le_connection_latency[1] = { hci_stack->le_connection_latency }; 6480e90f848dSMatthias Ringwald uint16_t le_supervision_timeout[1] = { hci_stack->le_supervision_timeout }; 6481e90f848dSMatthias Ringwald uint16_t le_minimum_ce_length[1] = { hci_stack->le_minimum_ce_length }; 6482e90f848dSMatthias Ringwald uint16_t le_maximum_ce_length[1] = { hci_stack->le_maximum_ce_length }; 6483e90f848dSMatthias Ringwald hci_send_cmd(&hci_le_extended_create_connection, 6484e90f848dSMatthias Ringwald 0, // don't use whitelist 6485e90f848dSMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 6486e90f848dSMatthias Ringwald connection->address_type, // peer address type 6487e90f848dSMatthias Ringwald connection->address, // peer bd addr 6488e90f848dSMatthias Ringwald 1, // initiating PHY - 1M 6489e90f848dSMatthias Ringwald le_connection_scan_interval, // conn scan interval 6490e90f848dSMatthias Ringwald le_connection_scan_window, // conn scan windows 6491e90f848dSMatthias Ringwald le_connection_interval_min, // conn interval min 6492e90f848dSMatthias Ringwald le_connection_interval_max, // conn interval max 6493e90f848dSMatthias Ringwald le_connection_latency, // conn latency 6494e90f848dSMatthias Ringwald le_supervision_timeout, // conn latency 6495e90f848dSMatthias Ringwald le_minimum_ce_length, // min ce length 6496e90f848dSMatthias Ringwald le_maximum_ce_length // max ce length 6497e90f848dSMatthias Ringwald ); } 6498e90f848dSMatthias Ringwald else 6499e90f848dSMatthias Ringwald #endif 6500e90f848dSMatthias Ringwald { 65014f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 6502cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval, // conn scan interval 6503cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window, // conn scan windows 65044f3229d8S[email protected] 0, // don't use whitelist 65054f3229d8S[email protected] connection->address_type, // peer address type 65064f3229d8S[email protected] connection->address, // peer bd addr 65076bcfa632SMatthias Ringwald hci_stack->le_connection_own_addr_type, // our addr type: 650873044eb2SMatthias Ringwald hci_stack->le_connection_interval_min, // conn interval min 650973044eb2SMatthias Ringwald hci_stack->le_connection_interval_max, // conn interval max 651073044eb2SMatthias Ringwald hci_stack->le_connection_latency, // conn latency 651173044eb2SMatthias Ringwald hci_stack->le_supervision_timeout, // conn latency 651273044eb2SMatthias Ringwald hci_stack->le_minimum_ce_length, // min ce length 651373044eb2SMatthias Ringwald hci_stack->le_maximum_ce_length // max ce length 65144f3229d8S[email protected] ); 6515e90f848dSMatthias Ringwald } 65164f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 6517b2f949feS[email protected] #endif 6518d70217a2SMatthias Ringwald #endif 65194f3229d8S[email protected] break; 65204f3229d8S[email protected] } 6521f30077b7SMatthias Ringwald return true; 6522ad83dc6aS[email protected] 652335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 65240bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 65255cf766e8SMatthias Ringwald connection->role = HCI_ROLE_SLAVE; 6526f16129ceSMatthias Ringwald if (connection->address_type == BD_ADDR_TYPE_ACL){ 652776ccfb2aSMatthias Ringwald log_info("sending hci_accept_connection_request"); 6528895f6685SMilanka Ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 6529c4c88f1bSJakob Krantz hci_send_cmd(&hci_accept_connection_request, connection->address, hci_stack->master_slave_policy); 6530f30077b7SMatthias Ringwald return true; 6531ee025741SMatthias Ringwald } 6532ee025741SMatthias Ringwald break; 653335454696SMatthias Ringwald #endif 65340bf6344aS[email protected] case SEND_DISCONNECT: 65350bf6344aS[email protected] connection->state = SENT_DISCONNECT; 65366ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 6537f30077b7SMatthias Ringwald return true; 65380bf6344aS[email protected] 65390bf6344aS[email protected] default: 65400bf6344aS[email protected] break; 6541c7e0c5f6Smatthias.ringwald } 6542c7e0c5f6Smatthias.ringwald 6543cabf004eSMatthias Ringwald // no further commands if connection is about to get shut down 6544cabf004eSMatthias Ringwald if (connection->state == SENT_DISCONNECT) continue; 6545cabf004eSMatthias Ringwald 654694418890SMatthias Ringwald #ifdef ENABLE_CLASSIC 654794418890SMatthias Ringwald 6548ad20f0c8SMatthias Ringwald // Handling link key request requires remote supported features 654950f49832SMatthias Ringwald if (((connection->authentication_flags & AUTH_FLAG_HANDLE_LINK_KEY_REQUEST) != 0)){ 6550608f51bbSMatthias Ringwald log_info("responding to link key request, have link key db: %u", hci_stack->link_key_db != NULL); 65518daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 655230e72d78SMatthias Ringwald 6553e9f98c4aSMatthias Ringwald bool have_link_key = connection->link_key_type != INVALID_LINK_KEY; 6554e9f98c4aSMatthias Ringwald bool security_level_sufficient = have_link_key && (gap_security_level_for_link_key_type(connection->link_key_type) >= connection->requested_security_level); 655530e72d78SMatthias Ringwald if (have_link_key && security_level_sufficient){ 6556e9f98c4aSMatthias Ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &connection->link_key); 655732ab9390Smatthias.ringwald } else { 655832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 655932ab9390Smatthias.ringwald } 6560f30077b7SMatthias Ringwald return true; 656132ab9390Smatthias.ringwald } 65621d6b20aeS[email protected] 65638daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_DENY_PIN_CODE_REQUEST){ 65649da54300S[email protected] log_info("denying to pin request"); 65658daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_DENY_PIN_CODE_REQUEST); 656634d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 6567f30077b7SMatthias Ringwald return true; 65684c57c146S[email protected] } 65694c57c146S[email protected] 6570c950c316SMatthias Ringwald // security assessment requires remote features 65712dd8985bSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST) != 0){ 6572c950c316SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_RECV_IO_CAPABILITIES_REQUEST); 6573c950c316SMatthias Ringwald hci_ssp_assess_security_on_io_cap_request(connection); 6574c950c316SMatthias 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 6575c950c316SMatthias Ringwald } 6576c950c316SMatthias Ringwald 65778daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY){ 65788daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 6579a8d20135SMatthias Ringwald // set authentication requirements: 6580a8d20135SMatthias Ringwald // - MITM = ssp_authentication_requirement (USER) | requested_security_level (dynamic) 6581532454f9SMatthias Ringwald // - BONDING MODE: dedicated if requested, bondable otherwise. Drop bondable if not set for remote 6582a8d20135SMatthias Ringwald uint8_t authreq = hci_stack->ssp_authentication_requirement & 1; 65839faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 65849faad3abS[email protected] authreq |= 1; 6585106d6d11S[email protected] } 6586532454f9SMatthias Ringwald bool bonding = hci_stack->bondable; 65878daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_RECV_IO_CAPABILITIES_RESPONSE){ 6588532454f9SMatthias Ringwald // if we have received IO Cap Response, we're in responder role 6589532454f9SMatthias Ringwald bool remote_bonding = connection->io_cap_response_auth_req >= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 6590532454f9SMatthias Ringwald if (bonding && !remote_bonding){ 6591532454f9SMatthias Ringwald log_info("Remote not bonding, dropping local flag"); 6592532454f9SMatthias Ringwald bonding = false; 6593532454f9SMatthias Ringwald } 6594532454f9SMatthias Ringwald } 6595532454f9SMatthias Ringwald if (bonding){ 6596a8d20135SMatthias Ringwald if (connection->bonding_flags & BONDING_DEDICATED){ 6597a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 6598532454f9SMatthias Ringwald } else { 6599a8d20135SMatthias Ringwald authreq |= SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 6600a8d20135SMatthias Ringwald } 6601532454f9SMatthias Ringwald } 66021849becdSMatthias Ringwald uint8_t have_oob_data = 0; 66031849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 66041849becdSMatthias Ringwald if (connection->classic_oob_c_192 != NULL){ 66051849becdSMatthias Ringwald have_oob_data |= 1; 66061849becdSMatthias Ringwald } 66071849becdSMatthias Ringwald if (connection->classic_oob_c_256 != NULL){ 66081849becdSMatthias Ringwald have_oob_data |= 2; 66091849becdSMatthias Ringwald } 66101849becdSMatthias Ringwald #endif 66111849becdSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, have_oob_data, authreq); 661211b03efaSMatthias Ringwald return true; 6613f8fb5f6eS[email protected] } 661411b03efaSMatthias Ringwald 66158daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY) { 66168daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 661711b03efaSMatthias Ringwald hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 6618f30077b7SMatthias Ringwald return true; 661932ab9390Smatthias.ringwald } 662032ab9390Smatthias.ringwald 66211849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 66227ca4a7edSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY){ 66237ca4a7edSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_REMOTE_OOB_DATA_REPLY); 66241849becdSMatthias Ringwald const uint8_t zero[16] = { 0 }; 66251849becdSMatthias Ringwald const uint8_t * r_192 = zero; 66261849becdSMatthias Ringwald const uint8_t * c_192 = zero; 66271849becdSMatthias Ringwald const uint8_t * r_256 = zero; 66281849becdSMatthias Ringwald const uint8_t * c_256 = zero; 66291849becdSMatthias Ringwald // verify P-256 OOB 66301e83575aSMatthias Ringwald if ((connection->classic_oob_c_256 != NULL) && hci_command_supported(SUPPORTED_HCI_COMMAND_REMOTE_OOB_EXTENDED_DATA_REQUEST_REPLY)) { 66311849becdSMatthias Ringwald c_256 = connection->classic_oob_c_256; 66321849becdSMatthias Ringwald if (connection->classic_oob_r_256 != NULL) { 66331849becdSMatthias Ringwald r_256 = connection->classic_oob_r_256; 66341849becdSMatthias Ringwald } 66351849becdSMatthias Ringwald } 66361849becdSMatthias Ringwald // verify P-192 OOB 66371849becdSMatthias Ringwald if ((connection->classic_oob_c_192 != NULL)) { 66381849becdSMatthias Ringwald c_192 = connection->classic_oob_c_192; 66391849becdSMatthias Ringwald if (connection->classic_oob_r_192 != NULL) { 66401849becdSMatthias Ringwald r_192 = connection->classic_oob_r_192; 66411849becdSMatthias Ringwald } 66421849becdSMatthias Ringwald } 66437ca4a7edSMatthias Ringwald 66447ca4a7edSMatthias Ringwald // assess security 66457ca4a7edSMatthias Ringwald bool need_level_4 = hci_stack->gap_secure_connections_only_mode || (connection->requested_security_level == LEVEL_4); 66467ca4a7edSMatthias Ringwald bool can_reach_level_4 = hci_remote_sc_enabled(connection) && (c_256 != NULL); 66477ca4a7edSMatthias Ringwald if (need_level_4 && !can_reach_level_4){ 66487ca4a7edSMatthias Ringwald log_info("Level 4 required, but not possible -> abort"); 66497ca4a7edSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_INSUFFICIENT_SECURITY); 66507ca4a7edSMatthias Ringwald // send oob negative reply 66517ca4a7edSMatthias Ringwald c_256 = NULL; 66527ca4a7edSMatthias Ringwald c_192 = NULL; 66537ca4a7edSMatthias Ringwald } 66547ca4a7edSMatthias Ringwald 66551849becdSMatthias Ringwald // Reply 66561849becdSMatthias Ringwald if (c_256 != zero) { 66571849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_extended_data_request_reply, &connection->address, c_192, r_192, c_256, r_256); 66581849becdSMatthias Ringwald } else if (c_192 != zero){ 66591849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_reply, &connection->address, c_192, r_192); 66601849becdSMatthias Ringwald } else { 66611ae74bf3SMatthias Ringwald hci_stack->classic_oob_con_handle = connection->con_handle; 66621849becdSMatthias Ringwald hci_send_cmd(&hci_remote_oob_data_request_negative_reply, &connection->address); 66631849becdSMatthias Ringwald } 66641849becdSMatthias Ringwald return true; 66651849becdSMatthias Ringwald } 66661849becdSMatthias Ringwald #endif 66671849becdSMatthias Ringwald 66688daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_REPLY){ 66698daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_REPLY); 667034d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 6671f30077b7SMatthias Ringwald return true; 6672dbe1a790S[email protected] } 6673dbe1a790S[email protected] 6674367aedc0SMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY){ 6675367aedc0SMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_CONFIRM_NEGATIVE_REPLY); 6676367aedc0SMatthias Ringwald hci_send_cmd(&hci_user_confirmation_request_negative_reply, &connection->address); 6677367aedc0SMatthias Ringwald return true; 6678367aedc0SMatthias Ringwald } 6679367aedc0SMatthias Ringwald 66808daf94bcSMatthias Ringwald if (connection->authentication_flags & AUTH_FLAG_SEND_USER_PASSKEY_REPLY){ 66818daf94bcSMatthias Ringwald connectionClearAuthenticationFlags(connection, AUTH_FLAG_SEND_USER_PASSKEY_REPLY); 668234d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 6683f30077b7SMatthias Ringwald return true; 6684dbe1a790S[email protected] } 6685afd4e962S[email protected] 6686ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 6687ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 66881bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 66896ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 66906ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_REMOTE_USER_TERMINATED_CONNECTION); 6691f30077b7SMatthias Ringwald return true; 6692ad83dc6aS[email protected] } 669376f27cffSMatthias Ringwald 66942a75353aSMatthias Ringwald if ((connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST) && ((connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0)){ 669534d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 6696abdad579SMatthias Ringwald connection->bonding_flags |= BONDING_SENT_AUTHENTICATE_REQUEST; 669734d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 6698f30077b7SMatthias Ringwald return true; 6699afd4e962S[email protected] } 670076f27cffSMatthias Ringwald 6701dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 6702dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 6703dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 6704f30077b7SMatthias Ringwald return true; 6705dce78009S[email protected] } 6706447016ffSMatthias Ringwald 6707573897a0SMatthias Ringwald if (connection->bonding_flags & BONDING_SEND_READ_ENCRYPTION_KEY_SIZE){ 6708573897a0SMatthias Ringwald connection->bonding_flags &= ~BONDING_SEND_READ_ENCRYPTION_KEY_SIZE; 6709573897a0SMatthias Ringwald hci_send_cmd(&hci_read_encryption_key_size, connection->con_handle, 1); 6710f30077b7SMatthias Ringwald return true; 6711573897a0SMatthias Ringwald } 6712447016ffSMatthias Ringwald 6713447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_0){ 6714447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 6715447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 6716447016ffSMatthias Ringwald return true; 6717447016ffSMatthias Ringwald } 6718447016ffSMatthias Ringwald 6719447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_1){ 6720447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_1; 6721447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 1); 6722447016ffSMatthias Ringwald return true; 6723447016ffSMatthias Ringwald } 6724447016ffSMatthias Ringwald 6725447016ffSMatthias Ringwald if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES_PAGE_2){ 6726447016ffSMatthias Ringwald connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES_PAGE_2; 6727447016ffSMatthias Ringwald hci_send_cmd(&hci_read_remote_extended_features_command, connection->con_handle, 2); 6728447016ffSMatthias Ringwald return true; 6729447016ffSMatthias Ringwald } 673076f27cffSMatthias Ringwald #endif 673176f27cffSMatthias Ringwald 673276f27cffSMatthias Ringwald if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 673376f27cffSMatthias Ringwald connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 67341714cbbdSMatthias Ringwald #ifdef ENABLE_CLASSIC 67351714cbbdSMatthias Ringwald hci_pairing_complete(connection, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_SECURITY_REASONS); 67361714cbbdSMatthias Ringwald #endif 67376ef3696aSMatthias Ringwald if (connection->state != SENT_DISCONNECT){ 67386ef3696aSMatthias Ringwald connection->state = SENT_DISCONNECT; 67396ef3696aSMatthias Ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, ERROR_CODE_AUTHENTICATION_FAILURE); 6740f30077b7SMatthias Ringwald return true; 674176f27cffSMatthias Ringwald } 67426ef3696aSMatthias Ringwald } 6743da886c03S[email protected] 67446cdc2862SMatthias Ringwald #ifdef ENABLE_CLASSIC 6745f8ee3071SMatthias Ringwald uint16_t sniff_min_interval; 6746f8ee3071SMatthias Ringwald switch (connection->sniff_min_interval){ 6747f8ee3071SMatthias Ringwald case 0: 6748f8ee3071SMatthias Ringwald break; 6749f8ee3071SMatthias Ringwald case 0xffff: 6750f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 6751f8ee3071SMatthias Ringwald hci_send_cmd(&hci_exit_sniff_mode, connection->con_handle); 6752f30077b7SMatthias Ringwald return true; 6753f8ee3071SMatthias Ringwald default: 6754f8ee3071SMatthias Ringwald sniff_min_interval = connection->sniff_min_interval; 6755f8ee3071SMatthias Ringwald connection->sniff_min_interval = 0; 6756f8ee3071SMatthias Ringwald hci_send_cmd(&hci_sniff_mode, connection->con_handle, connection->sniff_max_interval, sniff_min_interval, connection->sniff_attempt, connection->sniff_timeout); 6757f30077b7SMatthias Ringwald return true; 6758f8ee3071SMatthias Ringwald } 675988a03c8dSMatthias Ringwald 6760140c0557SMatthias Ringwald if (connection->sniff_subrating_max_latency != 0xffff){ 6761140c0557SMatthias Ringwald uint16_t max_latency = connection->sniff_subrating_max_latency; 6762140c0557SMatthias Ringwald connection->sniff_subrating_max_latency = 0; 6763140c0557SMatthias Ringwald hci_send_cmd(&hci_sniff_subrating, connection->con_handle, max_latency, connection->sniff_subrating_min_remote_timeout, connection->sniff_subrating_min_local_timeout); 6764140c0557SMatthias Ringwald return true; 6765140c0557SMatthias Ringwald } 6766140c0557SMatthias Ringwald 6767965a4ccfSMatthias Ringwald if (connection->qos_service_type != HCI_SERVICE_TYPE_INVALID){ 6768278ff8a9SMatthias Ringwald uint8_t service_type = (uint8_t) connection->qos_service_type; 6769965a4ccfSMatthias Ringwald connection->qos_service_type = HCI_SERVICE_TYPE_INVALID; 6770278ff8a9SMatthias 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); 6771278ff8a9SMatthias Ringwald return true; 6772278ff8a9SMatthias Ringwald } 6773278ff8a9SMatthias Ringwald 677488a03c8dSMatthias Ringwald if (connection->request_role != HCI_ROLE_INVALID){ 677588a03c8dSMatthias Ringwald hci_role_t role = connection->request_role; 677688a03c8dSMatthias Ringwald connection->request_role = HCI_ROLE_INVALID; 677788a03c8dSMatthias Ringwald hci_send_cmd(&hci_switch_role_command, connection->address, role); 677888a03c8dSMatthias Ringwald return true; 677988a03c8dSMatthias Ringwald } 678022df2fe2SMatthias Ringwald #endif 67819afaa4d4SMatthias Ringwald 67829afaa4d4SMatthias Ringwald if (connection->gap_connection_tasks != 0){ 678322df2fe2SMatthias Ringwald #ifdef ENABLE_CLASSIC 67849afaa4d4SMatthias Ringwald if ((connection->gap_connection_tasks & GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT) != 0){ 67859afaa4d4SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_WRITE_AUTOMATIC_FLUSH_TIMEOUT; 67869afaa4d4SMatthias Ringwald hci_send_cmd(&hci_write_automatic_flush_timeout, connection->con_handle, hci_stack->automatic_flush_timeout); 67879afaa4d4SMatthias Ringwald return true; 67889afaa4d4SMatthias Ringwald } 678922df2fe2SMatthias Ringwald if (connection->gap_connection_tasks & GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT){ 679022df2fe2SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_WRITE_SUPERVISION_TIMEOUT; 679122df2fe2SMatthias Ringwald hci_send_cmd(&hci_write_link_supervision_timeout, connection->con_handle, hci_stack->link_supervision_timeout); 679222df2fe2SMatthias Ringwald return true; 67939afaa4d4SMatthias Ringwald } 67946cdc2862SMatthias Ringwald #endif 679522df2fe2SMatthias Ringwald if (connection->gap_connection_tasks & GAP_CONNECTION_TASK_READ_RSSI){ 679622df2fe2SMatthias Ringwald connection->gap_connection_tasks &= ~GAP_CONNECTION_TASK_READ_RSSI; 679722df2fe2SMatthias Ringwald hci_send_cmd(&hci_read_rssi, connection->con_handle); 679822df2fe2SMatthias Ringwald return true; 679922df2fe2SMatthias Ringwald } 680022df2fe2SMatthias Ringwald } 6801f8ee3071SMatthias Ringwald 6802a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 680373cd8a2aSMatthias Ringwald switch (connection->le_con_parameter_update_state){ 680473cd8a2aSMatthias Ringwald // response to L2CAP CON PARAMETER UPDATE REQUEST 680573cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS: 6806da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 680773cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection->le_conn_interval_min, 6808c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 6809c37a3166S[email protected] 0x0000, 0xffff); 6810f30077b7SMatthias Ringwald return true; 681173cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_REPLY: 681273cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 681373cd8a2aSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_reply, connection->con_handle, connection->le_conn_interval_min, 681473cd8a2aSMatthias Ringwald connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 681573cd8a2aSMatthias Ringwald 0x0000, 0xffff); 6816f30077b7SMatthias Ringwald return true; 681773cd8a2aSMatthias Ringwald case CON_PARAMETER_UPDATE_NEGATIVE_REPLY: 681873cd8a2aSMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 68191e0338ebSMatthias Ringwald hci_send_cmd(&hci_le_remote_connection_parameter_request_negative_reply, connection->con_handle, 68201e0338ebSMatthias Ringwald ERROR_CODE_UNACCEPTABLE_CONNECTION_PARAMETERS); 6821f30077b7SMatthias Ringwald return true; 682273cd8a2aSMatthias Ringwald default: 682373cd8a2aSMatthias Ringwald break; 6824c37a3166S[email protected] } 68254ea43905SMatthias Ringwald if (connection->le_phy_update_all_phys != 0xffu){ 6826b90f6e0aSMatthias Ringwald uint8_t all_phys = connection->le_phy_update_all_phys; 6827b90f6e0aSMatthias Ringwald connection->le_phy_update_all_phys = 0xff; 6828b90f6e0aSMatthias 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); 6829f30077b7SMatthias Ringwald return true; 6830b90f6e0aSMatthias Ringwald } 6831c37a3166S[email protected] #endif 6832dbe1a790S[email protected] } 6833f30077b7SMatthias Ringwald return false; 6834f30077b7SMatthias Ringwald } 6835c7e0c5f6Smatthias.ringwald 6836f30077b7SMatthias Ringwald static void hci_run(void){ 6837f30077b7SMatthias Ringwald 6838df9e2780SMatthias Ringwald // stack state sub statemachines 6839df9e2780SMatthias Ringwald switch (hci_stack->state) { 6840df9e2780SMatthias Ringwald case HCI_STATE_INITIALIZING: 6841df9e2780SMatthias Ringwald hci_initializing_run(); 6842d0dea5fbSMatthias Ringwald break; 6843df9e2780SMatthias Ringwald case HCI_STATE_HALTING: 6844df9e2780SMatthias Ringwald hci_halting_run(); 6845d0dea5fbSMatthias Ringwald break; 6846df9e2780SMatthias Ringwald case HCI_STATE_FALLING_ASLEEP: 6847df9e2780SMatthias Ringwald hci_falling_asleep_run(); 6848d0dea5fbSMatthias Ringwald break; 6849df9e2780SMatthias Ringwald default: 6850df9e2780SMatthias Ringwald break; 6851df9e2780SMatthias Ringwald } 6852df9e2780SMatthias Ringwald 6853d0dea5fbSMatthias Ringwald // allow to run after initialization to working transition 6854d0dea5fbSMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING){ 6855d0dea5fbSMatthias Ringwald return; 6856d0dea5fbSMatthias Ringwald } 6857d0dea5fbSMatthias Ringwald 6858f30077b7SMatthias Ringwald bool done; 6859f30077b7SMatthias Ringwald 6860f30077b7SMatthias Ringwald // send continuation fragments first, as they block the prepared packet buffer 6861f30077b7SMatthias Ringwald done = hci_run_acl_fragments(); 6862f30077b7SMatthias Ringwald if (done) return; 6863f30077b7SMatthias Ringwald 686468ab6207SMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 686568ab6207SMatthias Ringwald done = hci_run_iso_fragments(); 686668ab6207SMatthias Ringwald if (done) return; 686768ab6207SMatthias Ringwald #endif 686868ab6207SMatthias Ringwald 6869f30077b7SMatthias Ringwald #ifdef ENABLE_HCI_CONTROLLER_TO_HOST_FLOW_CONTROL 6870f30077b7SMatthias Ringwald // send host num completed packets next as they don't require num_cmd_packets > 0 6871f30077b7SMatthias Ringwald if (!hci_can_send_comand_packet_transport()) return; 6872f30077b7SMatthias Ringwald if (hci_stack->host_completed_packets){ 6873f30077b7SMatthias Ringwald hci_host_num_completed_packets(); 6874f30077b7SMatthias Ringwald return; 6875f30077b7SMatthias Ringwald } 6876f30077b7SMatthias Ringwald #endif 6877f30077b7SMatthias Ringwald 6878f30077b7SMatthias Ringwald if (!hci_can_send_command_packet_now()) return; 6879f30077b7SMatthias Ringwald 6880f30077b7SMatthias Ringwald // global/non-connection oriented commands 6881f30077b7SMatthias Ringwald 6882f30077b7SMatthias Ringwald 6883f30077b7SMatthias Ringwald #ifdef ENABLE_CLASSIC 6884f30077b7SMatthias Ringwald // general gap classic 6885f30077b7SMatthias Ringwald done = hci_run_general_gap_classic(); 6886f30077b7SMatthias Ringwald if (done) return; 6887f30077b7SMatthias Ringwald #endif 6888f30077b7SMatthias Ringwald 6889f30077b7SMatthias Ringwald #ifdef ENABLE_BLE 6890f30077b7SMatthias Ringwald // general gap le 6891f30077b7SMatthias Ringwald done = hci_run_general_gap_le(); 6892f30077b7SMatthias Ringwald if (done) return; 68937aa35f6aSMatthias Ringwald 68947aa35f6aSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 68957aa35f6aSMatthias Ringwald // ISO related tasks, e.g. BIG create/terminate/sync 68967aa35f6aSMatthias Ringwald done = hci_run_iso_tasks(); 68977aa35f6aSMatthias Ringwald if (done) return; 68987aa35f6aSMatthias Ringwald #endif 6899f30077b7SMatthias Ringwald #endif 6900f30077b7SMatthias Ringwald 6901f30077b7SMatthias Ringwald // send pending HCI commands 6902df9e2780SMatthias Ringwald hci_run_general_pending_commands(); 69033429f56bSmatthias.ringwald } 690416833f0aSmatthias.ringwald 69053e2050f7SMatthias Ringwald uint8_t hci_send_cmd_packet(uint8_t *packet, int size){ 690635454696SMatthias Ringwald // house-keeping 690735454696SMatthias Ringwald 690835454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 6909c8e4258aSmatthias.ringwald bd_addr_t addr; 6910c8e4258aSmatthias.ringwald hci_connection_t * conn; 6911c123d999SMatthias Ringwald #endif 6912c123d999SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 69136f35bb46SMatthias Ringwald uint8_t initiator_filter_policy; 69149cbd2215SMatthias Ringwald #endif 69154ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 69164ae8623eSMatthias Ringwald uint8_t i; 69174ae8623eSMatthias Ringwald uint8_t num_cis; 69184ae8623eSMatthias Ringwald hci_con_handle_t cis_handle; 69194ae8623eSMatthias Ringwald uint8_t status; 69204ae8623eSMatthias Ringwald #endif 6921c8e4258aSmatthias.ringwald 69226f35bb46SMatthias Ringwald uint16_t opcode = little_endian_read_16(packet, 0); 69236f35bb46SMatthias Ringwald switch (opcode) { 69249cbd2215SMatthias Ringwald case HCI_OPCODE_HCI_WRITE_LOOPBACK_MODE: 69259cbd2215SMatthias Ringwald hci_stack->loopback_mode = packet[3]; 69269cbd2215SMatthias Ringwald break; 69279cbd2215SMatthias Ringwald 69289cbd2215SMatthias Ringwald #ifdef ENABLE_CLASSIC 69296f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_CREATE_CONNECTION: 6930724d70a2SMatthias Ringwald reverse_bd_addr(&packet[3], addr); 69319da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 6932c8e4258aSmatthias.ringwald 693372cf8859SMatthias Ringwald // CVE-2020-26555: reject outgoing connection to device with same BD ADDR 693472cf8859SMatthias Ringwald if (memcmp(hci_stack->local_bd_addr, addr, 6) == 0) { 693579e0fa07SMatthias Ringwald hci_emit_connection_complete(addr, 0, ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR); 69363e2050f7SMatthias Ringwald return ERROR_CODE_CONNECTION_REJECTED_DUE_TO_UNACCEPTABLE_BD_ADDR; 693772cf8859SMatthias Ringwald } 693872cf8859SMatthias Ringwald 6939f16129ceSMatthias Ringwald conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 6940ad83dc6aS[email protected] if (!conn) { 6941f16129ceSMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 694217f1ba2aSmatthias.ringwald if (!conn) { 694317f1ba2aSmatthias.ringwald // notify client that alloc failed 69442deddeceSMatthias Ringwald hci_emit_connection_complete(addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 69453e2050f7SMatthias Ringwald return BTSTACK_MEMORY_ALLOC_FAILED; // packet not sent to controller 694617f1ba2aSmatthias.ringwald } 6947ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 6948f3e2cd2aSMatthias Ringwald conn->role = HCI_ROLE_MASTER; 6949ad83dc6aS[email protected] } 6950aee5d5c1SMatthias Ringwald 6951ad83dc6aS[email protected] log_info("conn state %u", conn->state); 69523e2050f7SMatthias Ringwald // TODO: L2CAP should not send create connection command, instead a (new) gap function should be used 6953ad83dc6aS[email protected] switch (conn->state) { 6954ad83dc6aS[email protected] // if connection active exists 6955ad83dc6aS[email protected] case OPEN: 6956f5e5741dSMatthias Ringwald // and OPEN, emit connection complete command 695772cf8859SMatthias Ringwald hci_emit_connection_complete(addr, conn->con_handle, ERROR_CODE_SUCCESS); 69583e2050f7SMatthias Ringwald // packet not sent to controller 69593e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 6960672e36abSMatthias Ringwald case RECEIVED_DISCONNECTION_COMPLETE: 6961672e36abSMatthias Ringwald // create connection triggered in disconnect complete event, let's do it now 6962672e36abSMatthias Ringwald break; 6963ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 69646c0d5426SMatthias Ringwald #ifdef ENABLE_HCI_SERIALIZED_CONTROLLER_OPERATIONS 69656c0d5426SMatthias Ringwald if (hci_classic_operation_active()){ 69666c0d5426SMatthias Ringwald return ERROR_CODE_SUCCESS; 69676c0d5426SMatthias Ringwald } 69686c0d5426SMatthias Ringwald #endif 6969532f91a5SMatthias Ringwald // connection created by hci, e.g. dedicated bonding, but not executed yet, let's do it now 6970532f91a5SMatthias Ringwald break; 6971ad83dc6aS[email protected] default: 6972ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 69733e2050f7SMatthias Ringwald // packet not sent to controller 69743e2050f7SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 6975ad83dc6aS[email protected] } 6976c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 6977229331c6SMatthias Ringwald 6978229331c6SMatthias Ringwald // track outgoing connection 6979f16129ceSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_ACL; 69806535961aSMatthias Ringwald (void) memcpy(hci_stack->outgoing_addr, addr, 6); 69816f35bb46SMatthias Ringwald break; 6982ee752bb8SMatthias Ringwald 69835b7087c7SMatthias Ringwald #if defined (ENABLE_SCO_OVER_HCI) || defined (HAVE_SCO_TRANSPORT) 69846f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_SETUP_SYNCHRONOUS_CONNECTION: 6985ee752bb8SMatthias Ringwald // setup_synchronous_connection? Voice setting at offset 22 6986ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 6987ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 15); 69886f35bb46SMatthias Ringwald break; 69896f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_ACCEPT_SYNCHRONOUS_CONNECTION: 6990c91e6e4fSMatthias Ringwald // accept_synchronous_connection? Voice setting at offset 18 6991ee752bb8SMatthias Ringwald // TODO: compare to current setting if sco connection already active 6992ee752bb8SMatthias Ringwald hci_stack->sco_voice_setting_active = little_endian_read_16(packet, 19); 69931f34df2cSMatthias Ringwald // track outgoing connection 69941f34df2cSMatthias Ringwald hci_stack->outgoing_addr_type = BD_ADDR_TYPE_SCO; 69951f34df2cSMatthias Ringwald reverse_bd_addr(&packet[3], hci_stack->outgoing_addr); 69966f35bb46SMatthias Ringwald break; 6997ee752bb8SMatthias Ringwald #endif 699835454696SMatthias Ringwald #endif 69994b3e1e19SMatthias Ringwald 7000a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 7001d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 70026f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION: 7003b04dfa37SMatthias Ringwald // white list used? 70046f35bb46SMatthias Ringwald initiator_filter_policy = packet[7]; 7005b04dfa37SMatthias Ringwald switch (initiator_filter_policy) { 7006b04dfa37SMatthias Ringwald case 0: 7007b04dfa37SMatthias Ringwald // whitelist not used 7008b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_DIRECT; 7009b04dfa37SMatthias Ringwald break; 7010b04dfa37SMatthias Ringwald case 1: 7011b04dfa37SMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_WHITELIST; 7012b04dfa37SMatthias Ringwald break; 7013b04dfa37SMatthias Ringwald default: 7014b04dfa37SMatthias Ringwald log_error("Invalid initiator_filter_policy in LE Create Connection %u", initiator_filter_policy); 7015b04dfa37SMatthias Ringwald break; 7016b04dfa37SMatthias Ringwald } 7017c163146eSMatthias Ringwald // track outgoing connection 701805002aecSMatthias Ringwald hci_stack->outgoing_addr_type = (bd_addr_type_t) packet[8]; // peer addres type 7019c163146eSMatthias Ringwald reverse_bd_addr( &packet[9], hci_stack->outgoing_addr); // peer address 70206f35bb46SMatthias Ringwald break; 70216f35bb46SMatthias Ringwald case HCI_OPCODE_HCI_LE_CREATE_CONNECTION_CANCEL: 70226ea9315cSMatthias Ringwald hci_stack->le_connecting_state = LE_CONNECTING_CANCEL; 70236f35bb46SMatthias Ringwald break; 70249cbd2215SMatthias Ringwald #endif 70254ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 70264ae8623eSMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 70274ae8623eSMatthias Ringwald case HCI_OPCODE_HCI_LE_ACCEPT_CIS_REQUEST: 70284ae8623eSMatthias Ringwald cis_handle = (hci_con_handle_t) little_endian_read_16(packet, 3); 7029*d75a6bbfSMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, cis_handle, HCI_ISO_GROUP_ID_INVALID); 70304ae8623eSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 70314ae8623eSMatthias Ringwald return status; 70324ae8623eSMatthias Ringwald } 70334ae8623eSMatthias Ringwald break; 70344ae8623eSMatthias Ringwald #endif /* ENABLE_LE_PERIPHERAL */ 70354ae8623eSMatthias Ringwald #endif /* ENABLE_LE_ISOCHRONOUS_STREAMS */ 70364ae8623eSMatthias Ringwald #endif /* ENABLE_BLE */ 70376f35bb46SMatthias Ringwald default: 70386f35bb46SMatthias Ringwald break; 7039b04dfa37SMatthias Ringwald } 704069a97523S[email protected] 70413a9fb326S[email protected] hci_stack->num_cmd_packets--; 70425bb5bc3eS[email protected] 70435bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 70443e2050f7SMatthias Ringwald int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 70453e2050f7SMatthias Ringwald if (err != 0){ 70463e2050f7SMatthias Ringwald return ERROR_CODE_HARDWARE_FAILURE; 70473e2050f7SMatthias Ringwald } 70483e2050f7SMatthias Ringwald return ERROR_CODE_SUCCESS; 704931452debSmatthias.ringwald } 70508adf0ddaSmatthias.ringwald 70512bd8b7e7S[email protected] // disconnect because of security block 70522bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 70532bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 70542bd8b7e7S[email protected] if (!connection) return; 70552bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 70562bd8b7e7S[email protected] } 70572bd8b7e7S[email protected] 70582bd8b7e7S[email protected] 7059dbe1a790S[email protected] // Configure Secure Simple Pairing 7060dbe1a790S[email protected] 706135454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 706235454696SMatthias Ringwald 7063dbe1a790S[email protected] // enable will enable SSP during init 706415a95bd5SMatthias Ringwald void gap_ssp_set_enable(int enable){ 70653a9fb326S[email protected] hci_stack->ssp_enable = enable; 7066dbe1a790S[email protected] } 7067dbe1a790S[email protected] 706895d71764SMatthias Ringwald static int hci_local_ssp_activated(void){ 706915a95bd5SMatthias Ringwald return gap_ssp_supported() && hci_stack->ssp_enable; 70702bd8b7e7S[email protected] } 70712bd8b7e7S[email protected] 7072dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 707315a95bd5SMatthias Ringwald void gap_ssp_set_io_capability(int io_capability){ 70743a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 7075dbe1a790S[email protected] } 707615a95bd5SMatthias Ringwald void gap_ssp_set_authentication_requirement(int authentication_requirement){ 70773a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 7078dbe1a790S[email protected] } 7079dbe1a790S[email protected] 7080dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 708115a95bd5SMatthias Ringwald void gap_ssp_set_auto_accept(int auto_accept){ 70823a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 7083dbe1a790S[email protected] } 70845d23aae8SMatthias Ringwald 70855d23aae8SMatthias Ringwald void gap_secure_connections_enable(bool enable){ 70865d23aae8SMatthias Ringwald hci_stack->secure_connections_enable = enable; 70875d23aae8SMatthias Ringwald } 708822d445d8SMatthias Ringwald bool gap_secure_connections_active(void){ 7089eaf85bc8SMatthias Ringwald return hci_stack->secure_connections_active; 709022d445d8SMatthias Ringwald } 70915d23aae8SMatthias Ringwald 709235454696SMatthias Ringwald #endif 7093dbe1a790S[email protected] 709494be1a66SMatthias Ringwald // va_list part of hci_send_cmd 70953e2050f7SMatthias Ringwald uint8_t hci_send_cmd_va_arg(const hci_cmd_t * cmd, va_list argptr){ 7096d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 70979d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 70983e2050f7SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 70999d14b626S[email protected] } 71009d14b626S[email protected] 71015127cc62S[email protected] // for HCI INITIALIZATION 71029da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 71035127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 71045127cc62S[email protected] 71059d14b626S[email protected] hci_reserve_packet_buffer(); 71069d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 710794be1a66SMatthias Ringwald uint16_t size = hci_cmd_create_from_template(packet, cmd, argptr); 71083e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_packet(packet, size); 7109bfea0222SMatthias Ringwald 7110593702caSMatthias Ringwald // release packet buffer on error or for synchronous transport implementations 71113e2050f7SMatthias Ringwald if ((status != ERROR_CODE_SUCCESS) || hci_transport_synchronous()){ 7112e2d22487SMatthias Ringwald hci_release_packet_buffer(); 7113068b8592SMatthias Ringwald hci_emit_transport_packet_sent(); 7114bfea0222SMatthias Ringwald } 7115bfea0222SMatthias Ringwald 71163e2050f7SMatthias Ringwald return status; 711794be1a66SMatthias Ringwald } 71189d14b626S[email protected] 711994be1a66SMatthias Ringwald /** 712094be1a66SMatthias Ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 712194be1a66SMatthias Ringwald */ 71223e2050f7SMatthias Ringwald uint8_t hci_send_cmd(const hci_cmd_t * cmd, ...){ 71231cd208adSmatthias.ringwald va_list argptr; 71241cd208adSmatthias.ringwald va_start(argptr, cmd); 71253e2050f7SMatthias Ringwald uint8_t status = hci_send_cmd_va_arg(cmd, argptr); 71261cd208adSmatthias.ringwald va_end(argptr); 71273e2050f7SMatthias Ringwald return status; 712893b8dc03Smatthias.ringwald } 7129c8e4258aSmatthias.ringwald 7130ee091cf1Smatthias.ringwald // Create various non-HCI events. 7131ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 7132ee091cf1Smatthias.ringwald 7133d6b06661SMatthias Ringwald static void hci_emit_event(uint8_t * event, uint16_t size, int dump){ 7134fb37a842SMatthias Ringwald // dump packet 7135d6b06661SMatthias Ringwald if (dump) { 713692da54c4SMatthias Ringwald hci_dump_packet( HCI_EVENT_PACKET, 1, event, size); 7137d6b06661SMatthias Ringwald } 71381ef6bb52SMatthias Ringwald 7139fb37a842SMatthias Ringwald // dispatch to all event handlers 71401ef6bb52SMatthias Ringwald btstack_linked_list_iterator_t it; 71411ef6bb52SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->event_handlers); 71421ef6bb52SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 71431ef6bb52SMatthias Ringwald btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it); 7144d9a7306aSMatthias Ringwald entry->callback(HCI_EVENT_PACKET, 0, event, size); 71451ef6bb52SMatthias Ringwald } 7146d6b06661SMatthias Ringwald } 7147d6b06661SMatthias Ringwald 7148d6b06661SMatthias Ringwald static void hci_emit_acl_packet(uint8_t * packet, uint16_t size){ 7149fb37a842SMatthias Ringwald if (!hci_stack->acl_packet_handler) return; 71503d50b4baSMatthias Ringwald hci_stack->acl_packet_handler(HCI_ACL_DATA_PACKET, 0, packet, size); 7151d6b06661SMatthias Ringwald } 7152d6b06661SMatthias Ringwald 715335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 7154701e3307SMatthias Ringwald static void hci_notify_if_sco_can_send_now(void){ 71553bc639ceSMatthias Ringwald // notify SCO sender if waiting 7156701e3307SMatthias Ringwald if (!hci_stack->sco_waiting_for_can_send_now) return; 7157701e3307SMatthias Ringwald if (hci_can_send_sco_packet_now()){ 71583bc639ceSMatthias Ringwald hci_stack->sco_waiting_for_can_send_now = 0; 7159701e3307SMatthias Ringwald uint8_t event[2] = { HCI_EVENT_SCO_CAN_SEND_NOW, 0 }; 7160701e3307SMatthias Ringwald hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event)); 71613d50b4baSMatthias Ringwald hci_stack->sco_packet_handler(HCI_EVENT_PACKET, 0, event, sizeof(event)); 71623bc639ceSMatthias Ringwald } 71633bc639ceSMatthias Ringwald } 71641cfb383eSMatthias Ringwald 71651cfb383eSMatthias Ringwald // parsing end emitting has been merged to reduce code size 71669784dac1SMatthias Ringwald static void gap_inquiry_explode(uint8_t *packet, uint16_t size) { 7167ac9136ccSMatthias Ringwald uint8_t event[28+GAP_INQUIRY_MAX_NAME_LEN]; 71681cfb383eSMatthias Ringwald 71691cfb383eSMatthias Ringwald uint8_t * eir_data; 71701cfb383eSMatthias Ringwald ad_context_t context; 71711cfb383eSMatthias Ringwald const uint8_t * name; 71721cfb383eSMatthias Ringwald uint8_t name_len; 71731cfb383eSMatthias Ringwald 71749784dac1SMatthias Ringwald if (size < 3) return; 71759784dac1SMatthias Ringwald 71761cfb383eSMatthias Ringwald int event_type = hci_event_packet_get_type(packet); 7177a1df452eSMatthias Ringwald int num_reserved_fields = (event_type == HCI_EVENT_INQUIRY_RESULT) ? 2 : 1; // 2 for old event, 1 otherwise 71781cfb383eSMatthias Ringwald int num_responses = hci_event_inquiry_result_get_num_responses(packet); 71791cfb383eSMatthias Ringwald 71809784dac1SMatthias Ringwald switch (event_type){ 71819784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 71829784dac1SMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 71839784dac1SMatthias Ringwald if (size != (3 + (num_responses * 14))) return; 71849784dac1SMatthias Ringwald break; 71859784dac1SMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 71869784dac1SMatthias Ringwald if (size != 257) return; 71879784dac1SMatthias Ringwald if (num_responses != 1) return; 71889784dac1SMatthias Ringwald break; 71899784dac1SMatthias Ringwald default: 71909784dac1SMatthias Ringwald return; 71919784dac1SMatthias Ringwald } 71929784dac1SMatthias Ringwald 71931cfb383eSMatthias Ringwald // event[1] is set at the end 71941cfb383eSMatthias Ringwald int i; 71951cfb383eSMatthias Ringwald for (i=0; i<num_responses;i++){ 71961cfb383eSMatthias Ringwald memset(event, 0, sizeof(event)); 71971cfb383eSMatthias Ringwald event[0] = GAP_EVENT_INQUIRY_RESULT; 7198ac973fd6SMatthias Ringwald uint8_t event_size = 27; // if name is not set by EIR 71991cfb383eSMatthias Ringwald 72006535961aSMatthias Ringwald (void)memcpy(&event[2], &packet[3 + (i * 6)], 6); // bd_addr 72010e588213SMatthias Ringwald event[8] = packet[3 + (num_responses*(6)) + (i*1)]; // page_scan_repetition_mode 72026535961aSMatthias Ringwald (void)memcpy(&event[9], 72036535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields)) + (i * 3)], 72046535961aSMatthias Ringwald 3); // class of device 72056535961aSMatthias Ringwald (void)memcpy(&event[12], 72066535961aSMatthias Ringwald &packet[3 + (num_responses * (6 + 1 + num_reserved_fields + 3)) + (i * 2)], 72076535961aSMatthias Ringwald 2); // clock offset 72081cfb383eSMatthias Ringwald 72091cfb383eSMatthias Ringwald switch (event_type){ 72101cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT: 72111cfb383eSMatthias Ringwald // 14,15,16,17 = 0, size 18 72121cfb383eSMatthias Ringwald break; 72131cfb383eSMatthias Ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 72141cfb383eSMatthias Ringwald event[14] = 1; 7215a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 72161cfb383eSMatthias Ringwald // 16,17 = 0, size 18 72171cfb383eSMatthias Ringwald break; 72181cfb383eSMatthias Ringwald case HCI_EVENT_EXTENDED_INQUIRY_RESPONSE: 72191cfb383eSMatthias Ringwald event[14] = 1; 7220a1df452eSMatthias Ringwald event[15] = packet [3 + (num_responses*(6+1+num_reserved_fields+3+2)) + (i*1)]; // rssi 722179186815SMatthias Ringwald // EIR packets only contain a single inquiry response 72221cfb383eSMatthias Ringwald eir_data = &packet[3 + (6+1+num_reserved_fields+3+2+1)]; 72231cfb383eSMatthias Ringwald name = NULL; 7224a8c4e5adSMatthias Ringwald // Iterate over EIR data 7225a8c4e5adSMatthias Ringwald for (ad_iterator_init(&context, EXTENDED_INQUIRY_RESPONSE_DATA_LEN, eir_data) ; ad_iterator_has_more(&context) ; ad_iterator_next(&context)){ 72261cfb383eSMatthias Ringwald uint8_t data_type = ad_iterator_get_data_type(&context); 72271cfb383eSMatthias Ringwald uint8_t data_size = ad_iterator_get_data_len(&context); 72281cfb383eSMatthias Ringwald const uint8_t * data = ad_iterator_get_data(&context); 7229ac9136ccSMatthias Ringwald // Prefer Complete Local Name over Shortened Local Name 72301cfb383eSMatthias Ringwald switch (data_type){ 72311cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_SHORTENED_LOCAL_NAME: 72321cfb383eSMatthias Ringwald if (name) continue; 7233cf373d3aSMatthias Ringwald /* fall through */ 72341cfb383eSMatthias Ringwald case BLUETOOTH_DATA_TYPE_COMPLETE_LOCAL_NAME: 72351cfb383eSMatthias Ringwald name = data; 72361cfb383eSMatthias Ringwald name_len = data_size; 72371cfb383eSMatthias Ringwald break; 7238ac9136ccSMatthias Ringwald case BLUETOOTH_DATA_TYPE_DEVICE_ID: 7239ac9136ccSMatthias Ringwald if (data_size != 8) break; 7240ac9136ccSMatthias Ringwald event[16] = 1; 72413c0c7fefSMatthias Ringwald memcpy(&event[17], data, 8); 7242ac9136ccSMatthias Ringwald break; 72431cfb383eSMatthias Ringwald default: 72441cfb383eSMatthias Ringwald break; 72451cfb383eSMatthias Ringwald } 72461cfb383eSMatthias Ringwald } 72471cfb383eSMatthias Ringwald if (name){ 7248ac9136ccSMatthias Ringwald event[25] = 1; 72491cfb383eSMatthias Ringwald // truncate name if needed 72501cfb383eSMatthias Ringwald int len = btstack_min(name_len, GAP_INQUIRY_MAX_NAME_LEN); 7251ac9136ccSMatthias Ringwald event[26] = len; 7252ac9136ccSMatthias Ringwald (void)memcpy(&event[27], name, len); 72531cfb383eSMatthias Ringwald event_size += len; 72541cfb383eSMatthias Ringwald } 72551cfb383eSMatthias Ringwald break; 72567bbeb3adSMilanka Ringwald default: 72577bbeb3adSMilanka Ringwald return; 72581cfb383eSMatthias Ringwald } 72591cfb383eSMatthias Ringwald event[1] = event_size - 2; 72601cfb383eSMatthias Ringwald hci_emit_event(event, event_size, 1); 72611cfb383eSMatthias Ringwald } 72621cfb383eSMatthias Ringwald } 726335454696SMatthias Ringwald #endif 72643bc639ceSMatthias Ringwald 726571de195eSMatthias Ringwald void hci_emit_state(void){ 72663a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 7267425d1371Smatthias.ringwald uint8_t event[3]; 726880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 72694ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 72703a9fb326S[email protected] event[2] = hci_stack->state; 7271d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7272c8e4258aSmatthias.ringwald } 7273c8e4258aSmatthias.ringwald 727435454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 72752deddeceSMatthias Ringwald static void hci_emit_connection_complete(bd_addr_t address, hci_con_handle_t con_handle, uint8_t status){ 7276425d1371Smatthias.ringwald uint8_t event[13]; 7277c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 7278425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 727917f1ba2aSmatthias.ringwald event[2] = status; 72802deddeceSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 72812deddeceSMatthias Ringwald reverse_bd_addr(address, &event[5]); 7282c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 7283c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 7284d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7285c8e4258aSmatthias.ringwald } 728652db98b2SMatthias Ringwald static void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 728752db98b2SMatthias Ringwald if (disable_l2cap_timeouts) return; 728852db98b2SMatthias Ringwald log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 728952db98b2SMatthias Ringwald uint8_t event[4]; 729052db98b2SMatthias Ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 729152db98b2SMatthias Ringwald event[1] = sizeof(event) - 2; 729252db98b2SMatthias Ringwald little_endian_store_16(event, 2, conn->con_handle); 729352db98b2SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 729452db98b2SMatthias Ringwald } 729535454696SMatthias Ringwald #endif 7296c8e4258aSmatthias.ringwald 729735454696SMatthias Ringwald #ifdef ENABLE_BLE 7298d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7299667ba9d1SMatthias 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){ 73004f3229d8S[email protected] uint8_t event[21]; 73014f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 73024ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 73034f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 73044f3229d8S[email protected] event[3] = status; 7305fc64f94aSMatthias Ringwald little_endian_store_16(event, 4, con_handle); 73064f3229d8S[email protected] event[6] = 0; // TODO: role 73076e2e9a6bS[email protected] event[7] = address_type; 7308724d70a2SMatthias Ringwald reverse_bd_addr(address, &event[8]); 7309f8fbdce0SMatthias Ringwald little_endian_store_16(event, 14, 0); // interval 7310f8fbdce0SMatthias Ringwald little_endian_store_16(event, 16, 0); // latency 7311f8fbdce0SMatthias Ringwald little_endian_store_16(event, 18, 0); // supervision timeout 73124f3229d8S[email protected] event[20] = 0; // master clock accuracy 7313d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 73144f3229d8S[email protected] } 731535454696SMatthias Ringwald #endif 7316d70217a2SMatthias Ringwald #endif 73174f3229d8S[email protected] 7318fd43c0e0SMatthias Ringwald static void hci_emit_transport_packet_sent(void){ 7319fd43c0e0SMatthias Ringwald // notify upper stack that it might be possible to send again 7320fd43c0e0SMatthias Ringwald uint8_t event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; 7321fd43c0e0SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 7322fd43c0e0SMatthias Ringwald } 7323fd43c0e0SMatthias Ringwald 7324fc64f94aSMatthias Ringwald static void hci_emit_disconnection_complete(hci_con_handle_t con_handle, uint8_t reason){ 7325425d1371Smatthias.ringwald uint8_t event[6]; 73263c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 73274ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 73283c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 7329fc64f94aSMatthias Ringwald little_endian_store_16(event, 3, con_handle); 73303c4d4b90Smatthias.ringwald event[5] = reason; 7331d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 73323c4d4b90Smatthias.ringwald } 73333c4d4b90Smatthias.ringwald 7334b83d5eabSMatthias Ringwald static void hci_emit_nr_connections_changed(void){ 7335e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 7336425d1371Smatthias.ringwald uint8_t event[3]; 733780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 73384ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 733943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 7340d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 734143bfb1bdSmatthias.ringwald } 7342038bc64cSmatthias.ringwald 7343b83d5eabSMatthias Ringwald static void hci_emit_hci_open_failed(void){ 7344e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 7345425d1371Smatthias.ringwald uint8_t event[2]; 734680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 73474ea43905SMatthias Ringwald event[1] = sizeof(event) - 2u; 7348d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7349038bc64cSmatthias.ringwald } 73501b0e3922Smatthias.ringwald 735135454696SMatthias Ringwald static void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 735235454696SMatthias Ringwald log_info("hci_emit_dedicated_bonding_result %u ", status); 735335454696SMatthias Ringwald uint8_t event[9]; 735435454696SMatthias Ringwald int pos = 0; 735535454696SMatthias Ringwald event[pos++] = GAP_EVENT_DEDICATED_BONDING_COMPLETED; 73564ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 735735454696SMatthias Ringwald event[pos++] = status; 735835454696SMatthias Ringwald reverse_bd_addr(address, &event[pos]); 7359d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7360381fbed8Smatthias.ringwald } 7361458bf4e8S[email protected] 736235454696SMatthias Ringwald 736335454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 736435454696SMatthias Ringwald 7365b83d5eabSMatthias Ringwald static void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 7366df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 7367a00031e2S[email protected] uint8_t event[5]; 7368e00caf9cS[email protected] int pos = 0; 73695611a760SMatthias Ringwald event[pos++] = GAP_EVENT_SECURITY_LEVEL; 7370e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 7371f8fbdce0SMatthias Ringwald little_endian_store_16(event, 2, con_handle); 7372e00caf9cS[email protected] pos += 2; 7373e00caf9cS[email protected] event[pos++] = level; 7374d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7375e00caf9cS[email protected] } 7376e00caf9cS[email protected] 737735454696SMatthias Ringwald static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 737835454696SMatthias Ringwald if (!connection) return LEVEL_0; 73798daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 7380fcaf38b9SMatthias Ringwald // BIAS: we only consider Authenticated if the connection is already encrypted, which requires that both sides have link key 73818daf94bcSMatthias Ringwald if ((connection->authentication_flags & AUTH_FLAG_CONNECTION_AUTHENTICATED) == 0) return LEVEL_0; 7382170fafaeSMatthias Ringwald if (connection->encryption_key_size < hci_stack->gap_required_encyrption_key_size) return LEVEL_0; 73834051b7ffSMatthias Ringwald gap_security_level_t security_level = gap_security_level_for_link_key_type(connection->link_key_type); 7384170fafaeSMatthias Ringwald // LEVEL 4 always requires 128 bit encrytion key size 73850e588213SMatthias Ringwald if ((security_level == LEVEL_4) && (connection->encryption_key_size < 16)){ 7386170fafaeSMatthias Ringwald security_level = LEVEL_3; 7387170fafaeSMatthias Ringwald } 7388170fafaeSMatthias Ringwald return security_level; 738935454696SMatthias Ringwald } 739035454696SMatthias Ringwald 739171fd255dSMatthias Ringwald static void hci_emit_scan_mode_changed(uint8_t discoverable, uint8_t connectable){ 739271fd255dSMatthias Ringwald uint8_t event[4]; 739371fd255dSMatthias Ringwald event[0] = BTSTACK_EVENT_SCAN_MODE_CHANGED; 739435454696SMatthias Ringwald event[1] = sizeof(event) - 2; 739571fd255dSMatthias Ringwald event[2] = discoverable; 739671fd255dSMatthias Ringwald event[3] = connectable; 7397d6b06661SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 7398ad83dc6aS[email protected] } 7399ad83dc6aS[email protected] 740098a2fd1cSMatthias Ringwald // query if remote side supports eSCO 740120dcdd22SMatthias Ringwald bool hci_remote_esco_supported(hci_con_handle_t con_handle){ 740298a2fd1cSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 740320dcdd22SMatthias Ringwald if (!connection) return false; 740476ccfb2aSMatthias Ringwald return (connection->remote_supported_features[0] & 1) != 0; 740598a2fd1cSMatthias Ringwald } 740698a2fd1cSMatthias Ringwald 740767aae551SMatthias Ringwald static bool hci_ssp_supported(hci_connection_t * connection){ 740867aae551SMatthias Ringwald const uint8_t mask = BONDING_REMOTE_SUPPORTS_SSP_CONTROLLER | BONDING_REMOTE_SUPPORTS_SSP_HOST; 740967aae551SMatthias Ringwald return (connection->bonding_flags & mask) == mask; 741067aae551SMatthias Ringwald } 741167aae551SMatthias Ringwald 74122bd8b7e7S[email protected] // query if remote side supports SSP 741320dcdd22SMatthias Ringwald bool hci_remote_ssp_supported(hci_con_handle_t con_handle){ 74142bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 741520dcdd22SMatthias Ringwald if (!connection) return false; 741667aae551SMatthias Ringwald return hci_ssp_supported(connection) ? 1 : 0; 74172bd8b7e7S[email protected] } 74182bd8b7e7S[email protected] 741920dcdd22SMatthias Ringwald bool gap_ssp_supported_on_both_sides(hci_con_handle_t handle){ 7420df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 7421df3354fcS[email protected] } 7422df3354fcS[email protected] 74236ffc78ceSMatthias Ringwald /** 74246ffc78ceSMatthias Ringwald * Check if remote supported features query has completed 74256ffc78ceSMatthias Ringwald */ 74266ffc78ceSMatthias Ringwald bool hci_remote_features_available(hci_con_handle_t handle){ 74276ffc78ceSMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(handle); 74286ffc78ceSMatthias Ringwald if (!connection) return false; 74296ffc78ceSMatthias Ringwald return (connection->bonding_flags & BONDING_RECEIVED_REMOTE_FEATURES) != 0; 74306ffc78ceSMatthias Ringwald } 74316ffc78ceSMatthias Ringwald 7432d6596031SMatthias Ringwald /** 7433d6596031SMatthias Ringwald * Trigger remote supported features query 7434d6596031SMatthias Ringwald */ 7435c9742cd1SMatthias Ringwald 7436c9742cd1SMatthias Ringwald static void hci_trigger_remote_features_for_connection(hci_connection_t * connection){ 7437c9742cd1SMatthias Ringwald if ((connection->bonding_flags & (BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_RECEIVED_REMOTE_FEATURES)) == 0){ 7438c9742cd1SMatthias Ringwald connection->bonding_flags |= BONDING_REMOTE_FEATURES_QUERY_ACTIVE | BONDING_REQUEST_REMOTE_FEATURES_PAGE_0; 7439c9742cd1SMatthias Ringwald } 7440c9742cd1SMatthias Ringwald } 7441c9742cd1SMatthias Ringwald 7442d6596031SMatthias Ringwald void hci_remote_features_query(hci_con_handle_t con_handle){ 7443d6596031SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 7444d6596031SMatthias Ringwald if (!connection) return; 7445c9742cd1SMatthias Ringwald hci_trigger_remote_features_for_connection(connection); 7446d6596031SMatthias Ringwald hci_run(); 7447d6596031SMatthias Ringwald } 7448d6596031SMatthias Ringwald 7449458bf4e8S[email protected] // GAP API 7450458bf4e8S[email protected] /** 7451458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 7452458bf4e8S[email protected] * @praram enabled 7453458bf4e8S[email protected] */ 74544c57c146S[email protected] void gap_set_bondable_mode(int enable){ 74553a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 7456458bf4e8S[email protected] } 74574ef6443cSMatthias Ringwald /** 74584ef6443cSMatthias Ringwald * @brief Get bondable mode. 74594ef6443cSMatthias Ringwald * @return 1 if bondable 74604ef6443cSMatthias Ringwald */ 74614ef6443cSMatthias Ringwald int gap_get_bondable_mode(void){ 74624ef6443cSMatthias Ringwald return hci_stack->bondable; 74634ef6443cSMatthias Ringwald } 7464cb230b9dS[email protected] 7465cb230b9dS[email protected] /** 746634d2123cS[email protected] * @brief map link keys to security levels 7467cb230b9dS[email protected] */ 746834d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 746934d2123cS[email protected] switch (link_key_type){ 74703c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 74713c68dfa9S[email protected] return LEVEL_4; 74723c68dfa9S[email protected] case COMBINATION_KEY: 74733c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 74743c68dfa9S[email protected] return LEVEL_3; 74753c68dfa9S[email protected] default: 74763c68dfa9S[email protected] return LEVEL_2; 74773c68dfa9S[email protected] } 7478cb230b9dS[email protected] } 7479cb230b9dS[email protected] 74808b35e16aSMatthias Ringwald /** 74818b35e16aSMatthias Ringwald * @brief map link keys to secure connection yes/no 74828b35e16aSMatthias Ringwald */ 748336c3546bSMatthias Ringwald bool gap_secure_connection_for_link_key_type(link_key_type_t link_key_type){ 74848b35e16aSMatthias Ringwald switch (link_key_type){ 74858b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 74868b35e16aSMatthias Ringwald case UNAUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 748736c3546bSMatthias Ringwald return true; 74888b35e16aSMatthias Ringwald default: 748936c3546bSMatthias Ringwald return false; 74908b35e16aSMatthias Ringwald } 74918b35e16aSMatthias Ringwald } 74928b35e16aSMatthias Ringwald 74938b35e16aSMatthias Ringwald /** 74948b35e16aSMatthias Ringwald * @brief map link keys to authenticated 74958b35e16aSMatthias Ringwald */ 74967040ba26SMatthias Ringwald bool gap_authenticated_for_link_key_type(link_key_type_t link_key_type){ 74978b35e16aSMatthias Ringwald switch (link_key_type){ 74988b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 74998b35e16aSMatthias Ringwald case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 75007040ba26SMatthias Ringwald return true; 75018b35e16aSMatthias Ringwald default: 75027040ba26SMatthias Ringwald return false; 75038b35e16aSMatthias Ringwald } 75048b35e16aSMatthias Ringwald } 75058b35e16aSMatthias Ringwald 7506552b9260SMatthias Ringwald bool gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 75075127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 7508106d6d11S[email protected] return level > LEVEL_2; 7509106d6d11S[email protected] } 7510106d6d11S[email protected] 751134d2123cS[email protected] /** 751234d2123cS[email protected] * @brief get current security level 751334d2123cS[email protected] */ 751434d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 751534d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 751634d2123cS[email protected] if (!connection) return LEVEL_0; 751734d2123cS[email protected] return gap_security_level_for_connection(connection); 751834d2123cS[email protected] } 751934d2123cS[email protected] 7520cb230b9dS[email protected] /** 7521cb230b9dS[email protected] * @brief request connection to device to 7522cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 7523cb230b9dS[email protected] */ 752434d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 752534d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 752634d2123cS[email protected] if (!connection){ 7527a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 752834d2123cS[email protected] return; 752934d2123cS[email protected] } 7530defbf200SMatthias Ringwald 7531defbf200SMatthias Ringwald btstack_assert(hci_is_le_connection(connection) == false); 7532defbf200SMatthias Ringwald 7533bc00e12cSMatthias 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) 7534bc00e12cSMatthias Ringwald // available on the BR/EDR physical transport require Security Mode 4, Level 4 " 7535bc00e12cSMatthias Ringwald if (hci_stack->gap_secure_connections_only_mode && (requested_level != LEVEL_0)){ 7536bc00e12cSMatthias Ringwald requested_level = LEVEL_4; 7537bc00e12cSMatthias Ringwald } 7538bc00e12cSMatthias Ringwald 753934d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 754083d08d7cSMatthias Ringwald log_info("gap_request_security_level requested level %u, planned level %u, current level %u", 754183d08d7cSMatthias Ringwald requested_level, connection->requested_security_level, current_level); 754283d08d7cSMatthias Ringwald 7543dbd5dcc3SMatthias Ringwald // authentication active if authentication request was sent or planned level > 0 7544dbd5dcc3SMatthias Ringwald bool authentication_active = ((connection->bonding_flags & BONDING_SENT_AUTHENTICATE_REQUEST) != 0) || (connection->requested_security_level > LEVEL_0); 7545dbd5dcc3SMatthias Ringwald if (authentication_active){ 75461cf0a6c8SMatthias Ringwald // authentication already active 754783d08d7cSMatthias Ringwald if (connection->requested_security_level < requested_level){ 754883d08d7cSMatthias Ringwald // increase requested level as new level is higher 754983d08d7cSMatthias Ringwald // TODO: handle re-authentication when done 755083d08d7cSMatthias Ringwald connection->requested_security_level = requested_level; 755183d08d7cSMatthias Ringwald } 75521cf0a6c8SMatthias Ringwald } else { 755383d08d7cSMatthias Ringwald // no request active, notify if security sufficient 755483d08d7cSMatthias Ringwald if (requested_level <= current_level){ 7555a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 755634d2123cS[email protected] return; 755734d2123cS[email protected] } 7558a00031e2S[email protected] 7559e060c07dSMatthias Ringwald // store request 756034d2123cS[email protected] connection->requested_security_level = requested_level; 7561a00031e2S[email protected] 75621cf0a6c8SMatthias Ringwald // start to authenticate connection 75631eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 75647fdbaf79SMatthias Ringwald 75657fdbaf79SMatthias Ringwald // request remote features if not already active, also trigger hci_run 75667fdbaf79SMatthias Ringwald hci_remote_features_query(con_handle); 7567e00caf9cS[email protected] } 75681cf0a6c8SMatthias Ringwald } 7569ad83dc6aS[email protected] 7570ad83dc6aS[email protected] /** 7571ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 7572ad83dc6aS[email protected] * @param device 7573ad83dc6aS[email protected] * @param request MITM protection 7574ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 7575ad83dc6aS[email protected] */ 7576ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 7577ad83dc6aS[email protected] 7578ad83dc6aS[email protected] // create connection state machine 7579f16129ceSMatthias Ringwald hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_ACL); 7580ad83dc6aS[email protected] 7581ad83dc6aS[email protected] if (!connection){ 7582ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 7583ad83dc6aS[email protected] } 7584ad83dc6aS[email protected] 7585ad83dc6aS[email protected] // delete linkn key 758615a95bd5SMatthias Ringwald gap_drop_link_key_for_bd_addr(device); 7587ad83dc6aS[email protected] 7588ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 7589ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 7590ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 7591f04a0c31SMatthias Ringwald log_info("gap_dedicated_bonding, mitm %d -> level %u", mitm_protection_required, connection->requested_security_level); 7592ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 7593ad83dc6aS[email protected] 7594ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 7595ad83dc6aS[email protected] 7596ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 7597ad83dc6aS[email protected] // handle: authentication failure 7598ad83dc6aS[email protected] // handle: disconnect on done 7599ad83dc6aS[email protected] 7600ad83dc6aS[email protected] hci_run(); 7601ad83dc6aS[email protected] 7602ad83dc6aS[email protected] return 0; 7603ad83dc6aS[email protected] } 76048e618f72S[email protected] 76058e618f72S[email protected] void gap_set_local_name(const char * local_name){ 76068e618f72S[email protected] hci_stack->local_name = local_name; 7607baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_LOCAL_NAME; 760859d59ecfSMatthias Ringwald // also update EIR if not set by user 760959d59ecfSMatthias Ringwald if (hci_stack->eir_data == NULL){ 7610baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 761159d59ecfSMatthias Ringwald } 761259d59ecfSMatthias Ringwald hci_run(); 76138e618f72S[email protected] } 76141aeec2ebSMatthias Ringwald #endif 76158e618f72S[email protected] 761635454696SMatthias Ringwald 761735454696SMatthias Ringwald #ifdef ENABLE_BLE 761835454696SMatthias Ringwald 7619d70217a2SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 7620d8e8f12aSMatthias Ringwald void gap_start_scan(void){ 7621fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = true; 76227bdc6798S[email protected] hci_run(); 76237bdc6798S[email protected] } 76248e618f72S[email protected] 7625d8e8f12aSMatthias Ringwald void gap_stop_scan(void){ 7626fde725feSMatthias Ringwald hci_stack->le_scanning_enabled = false; 76277bdc6798S[email protected] hci_run(); 76287bdc6798S[email protected] } 76294f3229d8S[email protected] 7630a7a719e9SMatthias Ringwald void gap_set_scan_params(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window, uint8_t scanning_filter_policy){ 7631ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 7632a7a719e9SMatthias Ringwald hci_stack->le_scan_filter_policy = scanning_filter_policy; 7633ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 7634ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 76358b69e4c7SMatthias Ringwald hci_stack->le_scanning_param_update = true; 7636ef11999fSmatthias.ringwald hci_run(); 7637ef11999fSmatthias.ringwald } 76384f3229d8S[email protected] 7639a7a719e9SMatthias Ringwald void gap_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 7640a7a719e9SMatthias Ringwald gap_set_scan_params(scan_type, scan_interval, scan_window, 0); 7641a7a719e9SMatthias Ringwald } 7642a7a719e9SMatthias Ringwald 7643667ba9d1SMatthias Ringwald uint8_t gap_connect(const bd_addr_t addr, bd_addr_type_t addr_type){ 76444f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 76454f3229d8S[email protected] if (!conn){ 7646d32b3f05SMatthias Ringwald // disallow if le connection is already outgoing 7647d32b3f05SMatthias Ringwald if (hci_is_le_connection_type(addr_type) && hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 7648d32b3f05SMatthias Ringwald log_error("le connection already active"); 7649d32b3f05SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 7650d32b3f05SMatthias Ringwald } 7651d32b3f05SMatthias Ringwald 7652d8e8f12aSMatthias Ringwald log_info("gap_connect: no connection exists yet, creating context"); 76532e77e513S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 76544f3229d8S[email protected] if (!conn){ 76554f3229d8S[email protected] // notify client that alloc failed 76566e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 7657d8e8f12aSMatthias Ringwald log_info("gap_connect: failed to alloc hci_connection_t"); 7658472a5742SMatthias Ringwald return GATT_CLIENT_NOT_CONNECTED; // don't sent packet to controller 76594f3229d8S[email protected] } 7660d5b1a89eSMatthias Ringwald 7661d5b1a89eSMatthias Ringwald // set le connecting state 7662d5b1a89eSMatthias Ringwald if (hci_is_le_connection_type(addr_type)){ 7663d5b1a89eSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_DIRECT; 7664d5b1a89eSMatthias Ringwald } 7665d5b1a89eSMatthias Ringwald 76664f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 7667d8e8f12aSMatthias Ringwald log_info("gap_connect: send create connection next"); 7668564fca32S[email protected] hci_run(); 7669b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 76704f3229d8S[email protected] } 76710bf6344aS[email protected] 76720bf6344aS[email protected] if (!hci_is_le_connection(conn) || 7673a1df452eSMatthias Ringwald (conn->state == SEND_CREATE_CONNECTION) || 76740e588213SMatthias Ringwald (conn->state == SENT_CREATE_CONNECTION)) { 76752e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 7676d8e8f12aSMatthias Ringwald log_error("gap_connect: classic connection or connect is already being created"); 7677616edd56SMatthias Ringwald return GATT_CLIENT_IN_WRONG_STATE; 76780bf6344aS[email protected] } 76790bf6344aS[email protected] 7680b0136355SMatthias Ringwald // check if connection was just disconnected 7681b0136355SMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 7682b0136355SMatthias Ringwald log_info("gap_connect: send create connection (again)"); 7683b0136355SMatthias Ringwald conn->state = SEND_CREATE_CONNECTION; 7684b0136355SMatthias Ringwald hci_run(); 7685b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 7686b0136355SMatthias Ringwald } 7687b0136355SMatthias Ringwald 7688d8e8f12aSMatthias Ringwald log_info("gap_connect: context exists with state %u", conn->state); 7689d5b1a89eSMatthias Ringwald hci_emit_le_connection_complete(conn->address_type, conn->address, conn->con_handle, ERROR_CODE_SUCCESS); 76904f3229d8S[email protected] hci_run(); 7691b0136355SMatthias Ringwald return ERROR_CODE_SUCCESS; 76924f3229d8S[email protected] } 76934f3229d8S[email protected] 76947851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 7695d8e8f12aSMatthias Ringwald static hci_connection_t * gap_get_outgoing_connection(void){ 7696665d90f2SMatthias Ringwald btstack_linked_item_t *it; 7697a0da043fSMatthias Ringwald for (it = (btstack_linked_item_t *) hci_stack->connections; it != NULL; it = it->next){ 76980bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 76990bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 77000bf6344aS[email protected] switch (conn->state){ 7701a6725849S[email protected] case SEND_CREATE_CONNECTION: 77027851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 77037851196eSmatthias.ringwald return conn; 77047851196eSmatthias.ringwald default: 77057851196eSmatthias.ringwald break; 77067851196eSmatthias.ringwald }; 77077851196eSmatthias.ringwald } 77087851196eSmatthias.ringwald return NULL; 77097851196eSmatthias.ringwald } 77107851196eSmatthias.ringwald 7711d8e8f12aSMatthias Ringwald uint8_t gap_connect_cancel(void){ 771214f2d8f0SMatthias Ringwald hci_connection_t * conn; 771314f2d8f0SMatthias Ringwald switch (hci_stack->le_connecting_request){ 771414f2d8f0SMatthias Ringwald case LE_CONNECTING_IDLE: 771514f2d8f0SMatthias Ringwald break; 771614f2d8f0SMatthias Ringwald case LE_CONNECTING_WHITELIST: 771714f2d8f0SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 771814f2d8f0SMatthias Ringwald hci_run(); 771914f2d8f0SMatthias Ringwald break; 772014f2d8f0SMatthias Ringwald case LE_CONNECTING_DIRECT: 772114f2d8f0SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 772214f2d8f0SMatthias Ringwald conn = gap_get_outgoing_connection(); 772314f2d8f0SMatthias Ringwald if (conn == NULL){ 772414f2d8f0SMatthias Ringwald hci_run(); 772514f2d8f0SMatthias Ringwald } else { 77267851196eSmatthias.ringwald switch (conn->state){ 77277851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 77287851196eSmatthias.ringwald // skip sending create connection and emit event instead 77292e77e513S[email protected] hci_emit_le_connection_complete(conn->address_type, conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 7730665d90f2SMatthias Ringwald btstack_linked_list_remove(&hci_stack->connections, (btstack_linked_item_t *) conn); 77317851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 77320bf6344aS[email protected] break; 7733a6725849S[email protected] case SENT_CREATE_CONNECTION: 7734ea04fbd8SMatthias Ringwald // let hci_run_general_gap_le cancel outgoing connection 77350bf6344aS[email protected] hci_run(); 77360bf6344aS[email protected] break; 77370bf6344aS[email protected] default: 77380bf6344aS[email protected] break; 77390bf6344aS[email protected] } 774014f2d8f0SMatthias Ringwald } 774114f2d8f0SMatthias Ringwald break; 774243dfd98aSMatthias Ringwald default: 774314f2d8f0SMatthias Ringwald btstack_unreachable(); 774414f2d8f0SMatthias Ringwald break; 774514f2d8f0SMatthias Ringwald } 774614f2d8f0SMatthias Ringwald return ERROR_CODE_SUCCESS; 7747e31f89a7S[email protected] } 77484f3229d8S[email protected] 7749c37a3166S[email protected] /** 77506012052bSMatthias Ringwald * @brief Set connection parameters for outgoing connections 7751cbe54ab2SJakob Krantz * @param conn_scan_interval (unit: 0.625 msec), default: 60 ms 7752cbe54ab2SJakob Krantz * @param conn_scan_window (unit: 0.625 msec), default: 30 ms 77536012052bSMatthias Ringwald * @param conn_interval_min (unit: 1.25ms), default: 10 ms 77546012052bSMatthias Ringwald * @param conn_interval_max (unit: 1.25ms), default: 30 ms 77556012052bSMatthias Ringwald * @param conn_latency, default: 4 77566012052bSMatthias Ringwald * @param supervision_timeout (unit: 10ms), default: 720 ms 77576012052bSMatthias Ringwald * @param min_ce_length (unit: 0.625ms), default: 10 ms 77586012052bSMatthias Ringwald * @param max_ce_length (unit: 0.625ms), default: 30 ms 77596012052bSMatthias Ringwald */ 77606012052bSMatthias Ringwald 7761cbe54ab2SJakob Krantz void gap_set_connection_parameters(uint16_t conn_scan_interval, uint16_t conn_scan_window, 7762cbe54ab2SJakob Krantz uint16_t conn_interval_min, uint16_t conn_interval_max, uint16_t conn_latency, 7763cbe54ab2SJakob Krantz uint16_t supervision_timeout, uint16_t min_ce_length, uint16_t max_ce_length){ 7764cbe54ab2SJakob Krantz hci_stack->le_connection_scan_interval = conn_scan_interval; 7765cbe54ab2SJakob Krantz hci_stack->le_connection_scan_window = conn_scan_window; 77666012052bSMatthias Ringwald hci_stack->le_connection_interval_min = conn_interval_min; 77676012052bSMatthias Ringwald hci_stack->le_connection_interval_max = conn_interval_max; 77686012052bSMatthias Ringwald hci_stack->le_connection_latency = conn_latency; 77696012052bSMatthias Ringwald hci_stack->le_supervision_timeout = supervision_timeout; 77706012052bSMatthias Ringwald hci_stack->le_minimum_ce_length = min_ce_length; 77716012052bSMatthias Ringwald hci_stack->le_maximum_ce_length = max_ce_length; 77726012052bSMatthias Ringwald } 777313e645d2SMatthias Ringwald #endif 77746012052bSMatthias Ringwald 77756012052bSMatthias Ringwald /** 7776c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 7777c37a3166S[email protected] * @param handle 7778c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 7779c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 7780c37a3166S[email protected] * @param conn_latency 7781c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 77826b65794dSMilanka Ringwald * @return 0 if ok 7783c37a3166S[email protected] */ 7784c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 7785c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 7786c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 7787c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7788c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 7789c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 7790c37a3166S[email protected] connection->le_conn_latency = conn_latency; 7791c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 779284cf6d83SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS; 7793cfc59f1bSMatthias Ringwald hci_run(); 7794c37a3166S[email protected] return 0; 7795c37a3166S[email protected] } 7796c37a3166S[email protected] 779745c102fdSMatthias Ringwald /** 7798b68d7bc3SMatthias Ringwald * @brief Request an update of the connection parameter for a given LE connection 7799b68d7bc3SMatthias Ringwald * @param handle 7800b68d7bc3SMatthias Ringwald * @param conn_interval_min (unit: 1.25ms) 7801b68d7bc3SMatthias Ringwald * @param conn_interval_max (unit: 1.25ms) 7802b68d7bc3SMatthias Ringwald * @param conn_latency 7803b68d7bc3SMatthias Ringwald * @param supervision_timeout (unit: 10ms) 78046b65794dSMilanka Ringwald * @return 0 if ok 7805b68d7bc3SMatthias Ringwald */ 7806b68d7bc3SMatthias Ringwald int gap_request_connection_parameter_update(hci_con_handle_t con_handle, uint16_t conn_interval_min, 7807b68d7bc3SMatthias Ringwald uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 7808b68d7bc3SMatthias Ringwald hci_connection_t * connection = hci_connection_for_handle(con_handle); 7809b68d7bc3SMatthias Ringwald if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7810b68d7bc3SMatthias Ringwald connection->le_conn_interval_min = conn_interval_min; 7811b68d7bc3SMatthias Ringwald connection->le_conn_interval_max = conn_interval_max; 7812b68d7bc3SMatthias Ringwald connection->le_conn_latency = conn_latency; 7813b68d7bc3SMatthias Ringwald connection->le_supervision_timeout = supervision_timeout; 7814b68d7bc3SMatthias Ringwald connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_REQUEST; 781509c9c963SMatthias Ringwald uint8_t l2cap_trigger_run_event[2] = { L2CAP_EVENT_TRIGGER_RUN, 0}; 781609c9c963SMatthias Ringwald hci_emit_event(l2cap_trigger_run_event, sizeof(l2cap_trigger_run_event), 0); 7817b68d7bc3SMatthias Ringwald return 0; 7818b68d7bc3SMatthias Ringwald } 7819b68d7bc3SMatthias Ringwald 7820d70217a2SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 7821d70217a2SMatthias Ringwald 7822b68d7bc3SMatthias Ringwald /** 782345c102fdSMatthias Ringwald * @brief Set Advertisement Data 782445c102fdSMatthias Ringwald * @param advertising_data_length 782545c102fdSMatthias Ringwald * @param advertising_data (max 31 octets) 782645c102fdSMatthias Ringwald * @note data is not copied, pointer has to stay valid 782745c102fdSMatthias Ringwald */ 782845c102fdSMatthias Ringwald void gap_advertisements_set_data(uint8_t advertising_data_length, uint8_t * advertising_data){ 782945c102fdSMatthias Ringwald hci_stack->le_advertisements_data_len = advertising_data_length; 783045c102fdSMatthias Ringwald hci_stack->le_advertisements_data = advertising_data; 7831501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 7832bbc366e6SMatthias Ringwald hci_run(); 783345c102fdSMatthias Ringwald } 7834501f56b3SMatthias Ringwald 7835501f56b3SMatthias Ringwald /** 7836501f56b3SMatthias Ringwald * @brief Set Scan Response Data 7837501f56b3SMatthias Ringwald * @param advertising_data_length 7838501f56b3SMatthias Ringwald * @param advertising_data (max 31 octets) 7839501f56b3SMatthias Ringwald * @note data is not copied, pointer has to stay valid 7840501f56b3SMatthias Ringwald */ 7841501f56b3SMatthias Ringwald void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * scan_response_data){ 7842501f56b3SMatthias Ringwald hci_stack->le_scan_response_data_len = scan_response_data_length; 7843501f56b3SMatthias Ringwald hci_stack->le_scan_response_data = scan_response_data; 7844501f56b3SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 7845bbc366e6SMatthias Ringwald hci_run(); 784645c102fdSMatthias Ringwald } 784745c102fdSMatthias Ringwald 784845c102fdSMatthias Ringwald /** 784945c102fdSMatthias Ringwald * @brief Set Advertisement Parameters 785045c102fdSMatthias Ringwald * @param adv_int_min 785145c102fdSMatthias Ringwald * @param adv_int_max 785245c102fdSMatthias Ringwald * @param adv_type 785345c102fdSMatthias Ringwald * @param direct_address_type 785445c102fdSMatthias Ringwald * @param direct_address 785545c102fdSMatthias Ringwald * @param channel_map 785645c102fdSMatthias Ringwald * @param filter_policy 785745c102fdSMatthias Ringwald * 785845c102fdSMatthias Ringwald * @note internal use. use gap_advertisements_set_params from gap_le.h instead. 785945c102fdSMatthias Ringwald */ 786045c102fdSMatthias Ringwald void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, 7861b95a5a35SMatthias Ringwald uint8_t direct_address_typ, bd_addr_t direct_address, 786245c102fdSMatthias Ringwald uint8_t channel_map, uint8_t filter_policy) { 786345c102fdSMatthias Ringwald 786445c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_min = adv_int_min; 786545c102fdSMatthias Ringwald hci_stack->le_advertisements_interval_max = adv_int_max; 786645c102fdSMatthias Ringwald hci_stack->le_advertisements_type = adv_type; 786745c102fdSMatthias Ringwald hci_stack->le_advertisements_direct_address_type = direct_address_typ; 786845c102fdSMatthias Ringwald hci_stack->le_advertisements_channel_map = channel_map; 786945c102fdSMatthias Ringwald hci_stack->le_advertisements_filter_policy = filter_policy; 78706535961aSMatthias Ringwald (void)memcpy(hci_stack->le_advertisements_direct_address, direct_address, 78716535961aSMatthias Ringwald 6); 787245c102fdSMatthias Ringwald 78736bf435a6SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 78746bf435a6SMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_PARAMS_SET; 7875bbc366e6SMatthias Ringwald hci_run(); 787645c102fdSMatthias Ringwald } 787745c102fdSMatthias Ringwald 787845c102fdSMatthias Ringwald /** 787945c102fdSMatthias Ringwald * @brief Enable/Disable Advertisements 788045c102fdSMatthias Ringwald * @param enabled 788145c102fdSMatthias Ringwald */ 788245c102fdSMatthias Ringwald void gap_advertisements_enable(int enabled){ 7883a408e724SMatthias Ringwald if (enabled == 0){ 7884a408e724SMatthias Ringwald hci_stack->le_advertisements_state &= ~LE_ADVERTISEMENT_STATE_ENABLED; 7885a408e724SMatthias Ringwald } else { 7886a408e724SMatthias Ringwald hci_stack->le_advertisements_state |= LE_ADVERTISEMENT_STATE_ENABLED; 7887a408e724SMatthias Ringwald } 7888bbc366e6SMatthias Ringwald hci_update_advertisements_enabled_for_current_roles(); 7889cfc59f1bSMatthias Ringwald hci_run(); 789045c102fdSMatthias Ringwald } 789145c102fdSMatthias Ringwald 7892a5770ecbSMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 7893a5770ecbSMatthias Ringwald static le_advertising_set_t * hci_advertising_set_for_handle(uint8_t advertising_handle){ 7894a5770ecbSMatthias Ringwald btstack_linked_list_iterator_t it; 7895a5770ecbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_advertising_sets); 7896a5770ecbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7897a5770ecbSMatthias Ringwald le_advertising_set_t * item = (le_advertising_set_t *) btstack_linked_list_iterator_next(&it); 7898a5770ecbSMatthias Ringwald if ( item->advertising_handle == advertising_handle ) { 7899a5770ecbSMatthias Ringwald return item; 7900a5770ecbSMatthias Ringwald } 7901a5770ecbSMatthias Ringwald } 7902a5770ecbSMatthias Ringwald return NULL; 7903a5770ecbSMatthias Ringwald } 7904a5770ecbSMatthias Ringwald 7905a5770ecbSMatthias 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){ 7906a5770ecbSMatthias Ringwald // find free advertisement handle 7907a5770ecbSMatthias Ringwald uint8_t advertisement_handle; 7908a5770ecbSMatthias Ringwald for (advertisement_handle = 1; advertisement_handle <= LE_EXTENDED_ADVERTISING_MAX_HANDLE; advertisement_handle++){ 7909a5770ecbSMatthias Ringwald if (hci_advertising_set_for_handle(advertisement_handle) == NULL) break; 7910a5770ecbSMatthias Ringwald } 7911a5770ecbSMatthias Ringwald if (advertisement_handle > LE_EXTENDED_ADVERTISING_MAX_HANDLE) return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 7912a5770ecbSMatthias Ringwald // clear 7913a5770ecbSMatthias Ringwald memset(storage, 0, sizeof(le_advertising_set_t)); 7914a5770ecbSMatthias Ringwald // copy params 79150cbe4690SMatthias Ringwald storage->advertising_handle = advertisement_handle; 791657a04237SMatthias Ringwald memcpy(&storage->extended_params, advertising_parameters, sizeof(le_extended_advertising_parameters_t)); 7917a5770ecbSMatthias Ringwald // add to list 7918a5770ecbSMatthias Ringwald bool add_ok = btstack_linked_list_add(&hci_stack->le_advertising_sets, (btstack_linked_item_t *) storage); 7919a5770ecbSMatthias Ringwald if (!add_ok) return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 79200cbe4690SMatthias Ringwald *out_advertising_handle = advertisement_handle; 7921a5770ecbSMatthias Ringwald // set tasks and start 7922a5770ecbSMatthias Ringwald storage->tasks = LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7923a5770ecbSMatthias Ringwald hci_run(); 7924a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7925a5770ecbSMatthias Ringwald } 7926a5770ecbSMatthias Ringwald 7927a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_set_params(uint8_t advertising_handle, const le_extended_advertising_parameters_t * advertising_parameters){ 7928a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7929a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 793057a04237SMatthias Ringwald memcpy(&advertising_set->extended_params, advertising_parameters, sizeof(le_extended_advertising_parameters_t)); 7931a5770ecbSMatthias Ringwald // set tasks and start 7932a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 7933a5770ecbSMatthias Ringwald hci_run(); 7934a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7935a5770ecbSMatthias Ringwald } 7936a5770ecbSMatthias Ringwald 7937a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_get_params(uint8_t advertising_handle, le_extended_advertising_parameters_t * advertising_parameters){ 7938a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7939a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 794057a04237SMatthias Ringwald memcpy(advertising_parameters, &advertising_set->extended_params, sizeof(le_extended_advertising_parameters_t)); 7941a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7942a5770ecbSMatthias Ringwald } 7943a5770ecbSMatthias Ringwald 7944a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_set_random_address(uint8_t advertising_handle, bd_addr_t random_address){ 7945a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7946a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7947a5770ecbSMatthias Ringwald memcpy(advertising_set->random_address, random_address, 6); 7948a5770ecbSMatthias Ringwald // set tasks and start 7949a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 7950a5770ecbSMatthias Ringwald hci_run(); 7951a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7952a5770ecbSMatthias Ringwald } 7953a5770ecbSMatthias Ringwald 79542e4aaf70SMatthias Ringwald uint8_t gap_extended_advertising_set_adv_data(uint8_t advertising_handle, uint16_t advertising_data_length, const uint8_t * advertising_data){ 7955a5770ecbSMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7956a5770ecbSMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7957a5770ecbSMatthias Ringwald advertising_set->adv_data = advertising_data; 7958a5770ecbSMatthias Ringwald advertising_set->adv_data_len = advertising_data_length; 7959a5770ecbSMatthias Ringwald // set tasks and start 7960a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_ADV_DATA; 7961a5770ecbSMatthias Ringwald hci_run(); 7962a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7963a5770ecbSMatthias Ringwald } 7964a5770ecbSMatthias Ringwald 79652e4aaf70SMatthias 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){ 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->scan_data = scan_response_data; 7969a5770ecbSMatthias Ringwald advertising_set->scan_data_len = scan_response_data_length; 7970a5770ecbSMatthias Ringwald // set tasks and start 7971a5770ecbSMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_SCAN_DATA; 7972a5770ecbSMatthias Ringwald hci_run(); 7973a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7974a5770ecbSMatthias Ringwald } 7975a5770ecbSMatthias Ringwald 7976a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_start(uint8_t advertising_handle, uint16_t timeout, uint8_t num_extended_advertising_events){ 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->enable_timeout = timeout; 7980a5770ecbSMatthias Ringwald advertising_set->enable_max_scan_events = num_extended_advertising_events; 7981a5770ecbSMatthias Ringwald // set tasks and start 7982a5770ecbSMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_ENABLED; 7983a5770ecbSMatthias Ringwald hci_run(); 7984a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7985a5770ecbSMatthias Ringwald } 7986a5770ecbSMatthias Ringwald 7987a5770ecbSMatthias Ringwald uint8_t gap_extended_advertising_stop(uint8_t advertising_handle){ 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 // set tasks and start 7991a5770ecbSMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_ENABLED; 7992a5770ecbSMatthias Ringwald hci_run(); 7993a5770ecbSMatthias Ringwald return ERROR_CODE_SUCCESS; 7994a5770ecbSMatthias Ringwald } 7995a5770ecbSMatthias Ringwald 7996c814a904SMatthias Ringwald uint8_t gap_extended_advertising_remove(uint8_t advertising_handle){ 7997c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 7998c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 7999c814a904SMatthias Ringwald // set tasks and start 8000c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_REMOVE_SET; 8001c814a904SMatthias Ringwald hci_run(); 8002c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8003c814a904SMatthias Ringwald } 8004c814a904SMatthias Ringwald 8005c814a904SMatthias Ringwald #ifdef ENABLE_LE_PERIODIC_ADVERTISING 8006c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_set_params(uint8_t advertising_handle, const le_periodic_advertising_parameters_t * advertising_parameters){ 8007c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8008c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8009c814a904SMatthias Ringwald // periodic advertising requires neither connectable, scannable, legacy or anonymous 8010c814a904SMatthias Ringwald if ((advertising_set->extended_params.advertising_event_properties & 0x1f) != 0) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 8011c814a904SMatthias Ringwald memcpy(&advertising_set->periodic_params, advertising_parameters, sizeof(le_periodic_advertising_parameters_t)); 8012c814a904SMatthias Ringwald // set tasks and start 8013c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PERIODIC_PARAMS; 8014c814a904SMatthias Ringwald hci_run(); 8015c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8016c814a904SMatthias Ringwald } 8017c814a904SMatthias Ringwald 8018c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_get_params(uint8_t advertising_handle, le_periodic_advertising_parameters_t * advertising_parameters){ 8019c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8020c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8021c814a904SMatthias Ringwald memcpy(advertising_parameters, &advertising_set->extended_params, sizeof(le_periodic_advertising_parameters_t)); 8022c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8023c814a904SMatthias Ringwald } 8024c814a904SMatthias Ringwald 8025c814a904SMatthias Ringwald uint8_t gap_periodic_advertising_set_data(uint8_t advertising_handle, uint16_t periodic_data_length, const uint8_t * periodic_data){ 8026c814a904SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 8027c814a904SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8028c814a904SMatthias Ringwald advertising_set->periodic_data = periodic_data; 8029c814a904SMatthias Ringwald advertising_set->periodic_data_len = periodic_data_length; 8030c814a904SMatthias Ringwald // set tasks and start 8031c814a904SMatthias Ringwald advertising_set->tasks |= LE_ADVERTISEMENT_TASKS_SET_PERIODIC_DATA; 8032c814a904SMatthias Ringwald hci_run(); 8033c814a904SMatthias Ringwald return ERROR_CODE_SUCCESS; 8034c814a904SMatthias Ringwald } 8035c814a904SMatthias Ringwald 803653f240c0SMatthias Ringwald uint8_t gap_periodic_advertising_start(uint8_t advertising_handle, bool include_adi){ 803753f240c0SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 803853f240c0SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 803953f240c0SMatthias Ringwald // set tasks and start 804053f240c0SMatthias Ringwald advertising_set->periodic_include_adi = include_adi; 804153f240c0SMatthias Ringwald advertising_set->state |= LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED; 804253f240c0SMatthias Ringwald hci_run(); 804353f240c0SMatthias Ringwald return ERROR_CODE_SUCCESS; 804453f240c0SMatthias Ringwald } 804553f240c0SMatthias Ringwald 804653f240c0SMatthias Ringwald uint8_t gap_periodic_advertising_stop(uint8_t advertising_handle){ 804753f240c0SMatthias Ringwald le_advertising_set_t * advertising_set = hci_advertising_set_for_handle(advertising_handle); 804853f240c0SMatthias Ringwald if (advertising_set == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 804953f240c0SMatthias Ringwald // set tasks and start 805053f240c0SMatthias Ringwald advertising_set->state &= ~LE_ADVERTISEMENT_STATE_PERIODIC_ENABLED; 805153f240c0SMatthias Ringwald hci_run(); 805253f240c0SMatthias Ringwald return ERROR_CODE_SUCCESS; 805353f240c0SMatthias Ringwald } 8054c814a904SMatthias Ringwald #endif /* ENABLE_LE_PERIODIC_ADVERTISING */ 805553f240c0SMatthias Ringwald 8056a5770ecbSMatthias Ringwald #endif 8057a5770ecbSMatthias Ringwald 805835454696SMatthias Ringwald #endif 805906e5cf96SMatthias Ringwald 806006e5cf96SMatthias Ringwald void hci_le_set_own_address_type(uint8_t own_address_type){ 806106e5cf96SMatthias Ringwald log_info("hci_le_set_own_address_type: old %u, new %u", hci_stack->le_own_addr_type, own_address_type); 806206e5cf96SMatthias Ringwald if (own_address_type == hci_stack->le_own_addr_type) return; 806306e5cf96SMatthias Ringwald hci_stack->le_own_addr_type = own_address_type; 806406e5cf96SMatthias Ringwald 806564068776SMatthias Ringwald #ifdef ENABLE_LE_PERIPHERAL 806606e5cf96SMatthias Ringwald // update advertisement parameters, too 806706e5cf96SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; 8068bbc366e6SMatthias Ringwald hci_run(); 806964068776SMatthias Ringwald #endif 807064068776SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 807106e5cf96SMatthias Ringwald // note: we don't update scan parameters or modify ongoing connection attempts 807264068776SMatthias Ringwald #endif 807306e5cf96SMatthias Ringwald } 807406e5cf96SMatthias Ringwald 80759136bde8SMatthias Ringwald void hci_le_random_address_set(const bd_addr_t random_address){ 80769136bde8SMatthias Ringwald memcpy(hci_stack->le_random_address, random_address, 6); 80779136bde8SMatthias Ringwald hci_stack->le_random_address_set = true; 80789136bde8SMatthias Ringwald hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_ADDRESS; 80799136bde8SMatthias Ringwald hci_run(); 80809136bde8SMatthias Ringwald } 80819136bde8SMatthias Ringwald 8082d70217a2SMatthias Ringwald #endif 808345c102fdSMatthias Ringwald 8084616edd56SMatthias Ringwald uint8_t gap_disconnect(hci_con_handle_t handle){ 80855917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 80865917a5c5S[email protected] if (!conn){ 80877851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 8088616edd56SMatthias Ringwald return 0; 80895917a5c5S[email protected] } 80907fd7aa6fSMatthias Ringwald // ignore if already disconnected 80917fd7aa6fSMatthias Ringwald if (conn->state == RECEIVED_DISCONNECTION_COMPLETE){ 80927fd7aa6fSMatthias Ringwald return 0; 80937fd7aa6fSMatthias Ringwald } 80945917a5c5S[email protected] conn->state = SEND_DISCONNECT; 80955917a5c5S[email protected] hci_run(); 8096616edd56SMatthias Ringwald return 0; 80974f3229d8S[email protected] } 809804a6ef8cSmatthias.ringwald 8099228e430cSMatthias Ringwald int gap_read_rssi(hci_con_handle_t con_handle){ 8100228e430cSMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8101228e430cSMatthias Ringwald if (hci_connection == NULL) return 0; 810222df2fe2SMatthias Ringwald hci_connection->gap_connection_tasks |= GAP_CONNECTION_TASK_READ_RSSI; 8103228e430cSMatthias Ringwald hci_run(); 8104228e430cSMatthias Ringwald return 1; 8105228e430cSMatthias Ringwald } 8106228e430cSMatthias Ringwald 8107a1bf5ae7SMatthias Ringwald /** 8108a1bf5ae7SMatthias Ringwald * @brief Get connection type 8109a1bf5ae7SMatthias Ringwald * @param con_handle 8110a1bf5ae7SMatthias Ringwald * @result connection_type 8111a1bf5ae7SMatthias Ringwald */ 8112a1bf5ae7SMatthias Ringwald gap_connection_type_t gap_get_connection_type(hci_con_handle_t connection_handle){ 8113a1bf5ae7SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 8114a1bf5ae7SMatthias Ringwald if (!conn) return GAP_CONNECTION_INVALID; 8115a1bf5ae7SMatthias Ringwald switch (conn->address_type){ 8116a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 8117a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8118a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_LE; 8119a1bf5ae7SMatthias Ringwald case BD_ADDR_TYPE_SCO: 8120a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_SCO; 8121f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8122a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_ACL; 8123a1bf5ae7SMatthias Ringwald default: 8124a1bf5ae7SMatthias Ringwald return GAP_CONNECTION_INVALID; 8125a1bf5ae7SMatthias Ringwald } 8126a1bf5ae7SMatthias Ringwald } 8127a1bf5ae7SMatthias Ringwald 81282dceb1d6SMatthias Ringwald hci_role_t gap_get_role(hci_con_handle_t connection_handle){ 81292dceb1d6SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(connection_handle); 81302dceb1d6SMatthias Ringwald if (!conn) return HCI_ROLE_INVALID; 81312dceb1d6SMatthias Ringwald return (hci_role_t) conn->role; 81322dceb1d6SMatthias Ringwald } 81332dceb1d6SMatthias Ringwald 81342dceb1d6SMatthias Ringwald 813544f858f3SMatthias Ringwald #ifdef ENABLE_CLASSIC 8136667ba9d1SMatthias Ringwald uint8_t gap_request_role(const bd_addr_t addr, hci_role_t role){ 813788a03c8dSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 813888a03c8dSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 813988a03c8dSMatthias Ringwald conn->request_role = role; 814088a03c8dSMatthias Ringwald hci_run(); 8141d04a455eSMatthias Ringwald return ERROR_CODE_SUCCESS; 814288a03c8dSMatthias Ringwald } 814344f858f3SMatthias Ringwald #endif 814488a03c8dSMatthias Ringwald 8145a9a4c409SMatthias Ringwald #ifdef ENABLE_BLE 81464f551432SMatthias Ringwald 8147b45b7749SMilanka 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){ 8148b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 8149b90f6e0aSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8150b90f6e0aSMatthias Ringwald 8151b90f6e0aSMatthias Ringwald conn->le_phy_update_all_phys = all_phys; 8152b90f6e0aSMatthias Ringwald conn->le_phy_update_tx_phys = tx_phys; 8153b90f6e0aSMatthias Ringwald conn->le_phy_update_rx_phys = rx_phys; 8154b90f6e0aSMatthias Ringwald conn->le_phy_update_phy_options = phy_options; 8155b90f6e0aSMatthias Ringwald 8156b90f6e0aSMatthias Ringwald hci_run(); 8157b90f6e0aSMatthias Ringwald 8158b90f6e0aSMatthias Ringwald return 0; 8159b90f6e0aSMatthias Ringwald } 8160b90f6e0aSMatthias Ringwald 8161667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 81627a92a9dbSMatthias Ringwald // check if already in list 81637a92a9dbSMatthias Ringwald btstack_linked_list_iterator_t it; 81647a92a9dbSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 81657a92a9dbSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 81667a92a9dbSMatthias Ringwald whitelist_entry_t *entry = (whitelist_entry_t *) btstack_linked_list_iterator_next(&it); 81677a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 81687a92a9dbSMatthias Ringwald continue; 81697a92a9dbSMatthias Ringwald } 81707a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 81717a92a9dbSMatthias Ringwald continue; 81727a92a9dbSMatthias Ringwald } 8173287379ccSMatthias Ringwald // disallow if already scheduled to add 8174287379ccSMatthias Ringwald if ((entry->state & LE_WHITELIST_ADD_TO_CONTROLLER) != 0){ 81757a92a9dbSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 81767a92a9dbSMatthias Ringwald } 8177287379ccSMatthias Ringwald // still on controller, but scheduled to remove -> re-add 8178287379ccSMatthias Ringwald entry->state |= LE_WHITELIST_ADD_TO_CONTROLLER; 8179287379ccSMatthias Ringwald return ERROR_CODE_SUCCESS; 8180287379ccSMatthias Ringwald } 81817a92a9dbSMatthias Ringwald // alloc and add to list 8182e83201bcSMatthias Ringwald whitelist_entry_t * entry = btstack_memory_whitelist_entry_get(); 8183e83201bcSMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 8184e83201bcSMatthias Ringwald entry->address_type = address_type; 81856535961aSMatthias Ringwald (void)memcpy(entry->address, address, 6); 8186e83201bcSMatthias Ringwald entry->state = LE_WHITELIST_ADD_TO_CONTROLLER; 8187665d90f2SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_whitelist, (btstack_linked_item_t*) entry); 8188226db5efSMatthias Ringwald return ERROR_CODE_SUCCESS; 8189ac9c45e0SMatthias Ringwald } 8190ac9c45e0SMatthias Ringwald 8191667ba9d1SMatthias Ringwald static uint8_t hci_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 8192665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8193665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 8194665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8195665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 81967a92a9dbSMatthias Ringwald if (entry->address_type != address_type) { 81977a92a9dbSMatthias Ringwald continue; 81987a92a9dbSMatthias Ringwald } 81997a92a9dbSMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 82007a92a9dbSMatthias Ringwald continue; 82017a92a9dbSMatthias Ringwald } 8202e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 8203e83201bcSMatthias Ringwald // remove from controller if already present 8204e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 8205a3b69fdeSMatthias Ringwald } else { 8206226db5efSMatthias Ringwald // directly remove entry from whitelist 8207665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8208e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 8209e83201bcSMatthias Ringwald } 8210a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8211a3b69fdeSMatthias Ringwald } 8212a3b69fdeSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 821342ff5ba1SMatthias Ringwald } 821442ff5ba1SMatthias Ringwald 8215226db5efSMatthias Ringwald static void hci_whitelist_clear(void){ 8216665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8217665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_whitelist); 8218665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8219665d90f2SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&it); 8220e83201bcSMatthias Ringwald if (entry->state & LE_WHITELIST_ON_CONTROLLER){ 8221e83201bcSMatthias Ringwald // remove from controller if already present 8222e83201bcSMatthias Ringwald entry->state |= LE_WHITELIST_REMOVE_FROM_CONTROLLER; 8223e83201bcSMatthias Ringwald continue; 8224e83201bcSMatthias Ringwald } 822591915b0bSMatthias Ringwald // directly remove entry from whitelist 8226665d90f2SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8227e83201bcSMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 8228e83201bcSMatthias Ringwald } 8229226db5efSMatthias Ringwald } 8230226db5efSMatthias Ringwald 823183dc1a98SMatthias Ringwald // free all entries unconditionally 823283dc1a98SMatthias Ringwald static void hci_whitelist_free(void){ 823383dc1a98SMatthias Ringwald btstack_linked_list_iterator_t lit; 823483dc1a98SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_whitelist); 823583dc1a98SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 823683dc1a98SMatthias Ringwald whitelist_entry_t * entry = (whitelist_entry_t*) btstack_linked_list_iterator_next(&lit); 823783dc1a98SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_whitelist, (btstack_linked_item_t *) entry); 823883dc1a98SMatthias Ringwald btstack_memory_whitelist_entry_free(entry); 823983dc1a98SMatthias Ringwald } 824083dc1a98SMatthias Ringwald } 824183dc1a98SMatthias Ringwald 8242a3b69fdeSMatthias Ringwald /** 8243a3b69fdeSMatthias Ringwald * @brief Clear Whitelist 82446b65794dSMilanka Ringwald * @return 0 if ok 8245a3b69fdeSMatthias Ringwald */ 8246a3b69fdeSMatthias Ringwald uint8_t gap_whitelist_clear(void){ 8247a3b69fdeSMatthias Ringwald hci_whitelist_clear(); 8248a3b69fdeSMatthias Ringwald hci_run(); 8249a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8250a3b69fdeSMatthias Ringwald } 8251a3b69fdeSMatthias Ringwald 8252a3b69fdeSMatthias Ringwald /** 8253a3b69fdeSMatthias Ringwald * @brief Add Device to Whitelist 8254a3b69fdeSMatthias Ringwald * @param address_typ 8255a3b69fdeSMatthias Ringwald * @param address 82566b65794dSMilanka Ringwald * @return 0 if ok 8257a3b69fdeSMatthias Ringwald */ 8258667ba9d1SMatthias Ringwald uint8_t gap_whitelist_add(bd_addr_type_t address_type, const bd_addr_t address){ 8259a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 8260a3b69fdeSMatthias Ringwald if (status){ 8261a3b69fdeSMatthias Ringwald return status; 8262a3b69fdeSMatthias Ringwald } 8263a3b69fdeSMatthias Ringwald hci_run(); 8264a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8265a3b69fdeSMatthias Ringwald } 8266a3b69fdeSMatthias Ringwald 8267a3b69fdeSMatthias Ringwald /** 8268a3b69fdeSMatthias Ringwald * @brief Remove Device from Whitelist 8269a3b69fdeSMatthias Ringwald * @param address_typ 8270a3b69fdeSMatthias Ringwald * @param address 82716b65794dSMilanka Ringwald * @return 0 if ok 8272a3b69fdeSMatthias Ringwald */ 8273667ba9d1SMatthias Ringwald uint8_t gap_whitelist_remove(bd_addr_type_t address_type, const bd_addr_t address){ 8274a3b69fdeSMatthias Ringwald uint8_t status = hci_whitelist_remove(address_type, address); 8275a3b69fdeSMatthias Ringwald if (status){ 8276a3b69fdeSMatthias Ringwald return status; 8277a3b69fdeSMatthias Ringwald } 8278a3b69fdeSMatthias Ringwald hci_run(); 8279a3b69fdeSMatthias Ringwald return ERROR_CODE_SUCCESS; 8280a3b69fdeSMatthias Ringwald } 8281a3b69fdeSMatthias Ringwald 8282226db5efSMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 8283226db5efSMatthias Ringwald /** 828495e257d9SMatthias Ringwald * @brief Connect with Whitelist 828595e257d9SMatthias Ringwald * @note Explicit whitelist management and this connect with whitelist replace deprecated gap_auto_connection_* functions 82866b65794dSMilanka Ringwald * @return - if ok 828795e257d9SMatthias Ringwald */ 828895e257d9SMatthias Ringwald uint8_t gap_connect_with_whitelist(void){ 828995e257d9SMatthias Ringwald if (hci_stack->le_connecting_request != LE_CONNECTING_IDLE){ 829095e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 829195e257d9SMatthias Ringwald } 829295e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 829395e257d9SMatthias Ringwald hci_run(); 829495e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 829595e257d9SMatthias Ringwald } 829695e257d9SMatthias Ringwald 829795e257d9SMatthias Ringwald /** 8298226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Start Connecting to device 8299226db5efSMatthias Ringwald * @param address_typ 8300226db5efSMatthias Ringwald * @param address 83016b65794dSMilanka Ringwald * @return 0 if ok 8302226db5efSMatthias Ringwald */ 8303667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_start(bd_addr_type_t address_type, const bd_addr_t address){ 830495e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 8305226db5efSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 8306226db5efSMatthias Ringwald } 8307226db5efSMatthias Ringwald 8308226db5efSMatthias Ringwald uint8_t status = hci_whitelist_add(address_type, address); 830963f2efc9SMatthias Ringwald if (status == BTSTACK_MEMORY_ALLOC_FAILED) { 8310226db5efSMatthias Ringwald return status; 8311226db5efSMatthias Ringwald } 8312226db5efSMatthias Ringwald 8313226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_WHITELIST; 8314226db5efSMatthias Ringwald 8315226db5efSMatthias Ringwald hci_run(); 831695e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8317226db5efSMatthias Ringwald } 8318226db5efSMatthias Ringwald 8319226db5efSMatthias Ringwald /** 8320226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop Connecting to device 8321226db5efSMatthias Ringwald * @param address_typ 8322226db5efSMatthias Ringwald * @param address 83236b65794dSMilanka Ringwald * @return 0 if ok 8324226db5efSMatthias Ringwald */ 8325667ba9d1SMatthias Ringwald uint8_t gap_auto_connection_stop(bd_addr_type_t address_type, const bd_addr_t address){ 832695e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT){ 832795e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 832895e257d9SMatthias Ringwald } 832995e257d9SMatthias Ringwald 8330226db5efSMatthias Ringwald hci_whitelist_remove(address_type, address); 833195e257d9SMatthias Ringwald if (btstack_linked_list_empty(&hci_stack->le_whitelist)){ 833295e257d9SMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 833395e257d9SMatthias Ringwald } 8334226db5efSMatthias Ringwald hci_run(); 8335226db5efSMatthias Ringwald return 0; 8336226db5efSMatthias Ringwald } 8337226db5efSMatthias Ringwald 8338226db5efSMatthias Ringwald /** 8339226db5efSMatthias Ringwald * @brief Auto Connection Establishment - Stop everything 8340226db5efSMatthias Ringwald * @note Convenience function to stop all active auto connection attempts 8341226db5efSMatthias Ringwald */ 834295e257d9SMatthias Ringwald uint8_t gap_auto_connection_stop_all(void){ 834395e257d9SMatthias Ringwald if (hci_stack->le_connecting_request == LE_CONNECTING_DIRECT) { 834495e257d9SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 834595e257d9SMatthias Ringwald } 8346226db5efSMatthias Ringwald hci_whitelist_clear(); 8347226db5efSMatthias Ringwald hci_stack->le_connecting_request = LE_CONNECTING_IDLE; 8348e83201bcSMatthias Ringwald hci_run(); 834995e257d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8350ac9c45e0SMatthias Ringwald } 8351c9db5c21SMilanka Ringwald 8352b45b7749SMilanka Ringwald uint16_t gap_le_connection_interval(hci_con_handle_t con_handle){ 8353b45b7749SMilanka Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 8354c9db5c21SMilanka Ringwald if (!conn) return 0; 8355c9db5c21SMilanka Ringwald return conn->le_connection_interval; 8356c9db5c21SMilanka Ringwald } 8357d70217a2SMatthias Ringwald #endif 83584f551432SMatthias Ringwald #endif 83594f551432SMatthias Ringwald 836035454696SMatthias Ringwald #ifdef ENABLE_CLASSIC 8361ac9c45e0SMatthias Ringwald /** 8362ff00ed1cSMatthias Ringwald * @brief Set Extended Inquiry Response data 8363a8c4e5adSMatthias Ringwald * @param eir_data size HCI_EXTENDED_INQUIRY_RESPONSE_DATA_LEN (240) bytes, is not copied make sure memory is accessible during stack startup 8364ff00ed1cSMatthias Ringwald * @note has to be done before stack starts up 8365ff00ed1cSMatthias Ringwald */ 8366ff00ed1cSMatthias Ringwald void gap_set_extended_inquiry_response(const uint8_t * data){ 8367ff00ed1cSMatthias Ringwald hci_stack->eir_data = data; 8368baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_SET_EIR_DATA; 836959d59ecfSMatthias Ringwald hci_run(); 8370ff00ed1cSMatthias Ringwald } 8371ff00ed1cSMatthias Ringwald 8372ff00ed1cSMatthias Ringwald /** 8373f5875de5SMatthias Ringwald * @brief Start GAP Classic Inquiry 8374f5875de5SMatthias Ringwald * @param duration in 1.28s units 8375f5875de5SMatthias Ringwald * @return 0 if ok 8376f5875de5SMatthias Ringwald * @events: GAP_EVENT_INQUIRY_RESULT, GAP_EVENT_INQUIRY_COMPLETE 8377f5875de5SMatthias Ringwald */ 8378f5875de5SMatthias Ringwald int gap_inquiry_start(uint8_t duration_in_1280ms_units){ 837999449554SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 8380f5875de5SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8381a1df452eSMatthias Ringwald if ((duration_in_1280ms_units < GAP_INQUIRY_DURATION_MIN) || (duration_in_1280ms_units > GAP_INQUIRY_DURATION_MAX)){ 8382f5875de5SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 8383f5875de5SMatthias Ringwald } 8384f5875de5SMatthias Ringwald hci_stack->inquiry_state = duration_in_1280ms_units; 838530199e24SMatthias Ringwald hci_stack->inquiry_max_period_length = 0; 838630199e24SMatthias Ringwald hci_stack->inquiry_min_period_length = 0; 838730199e24SMatthias Ringwald hci_run(); 838830199e24SMatthias Ringwald return 0; 838930199e24SMatthias Ringwald } 839030199e24SMatthias Ringwald 839130199e24SMatthias Ringwald uint8_t gap_inquiry_periodic_start(uint8_t duration, uint16_t max_period_length, uint16_t min_period_length){ 839230199e24SMatthias Ringwald if (hci_stack->state != HCI_STATE_WORKING) return ERROR_CODE_COMMAND_DISALLOWED; 839330199e24SMatthias Ringwald if (hci_stack->inquiry_state != GAP_INQUIRY_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 839430199e24SMatthias Ringwald if (duration < GAP_INQUIRY_DURATION_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 839530199e24SMatthias Ringwald if (duration > GAP_INQUIRY_DURATION_MAX) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 839630199e24SMatthias Ringwald if (max_period_length < GAP_INQUIRY_MAX_PERIODIC_LEN_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;; 839730199e24SMatthias Ringwald if (min_period_length < GAP_INQUIRY_MIN_PERIODIC_LEN_MIN) return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;; 839830199e24SMatthias Ringwald 839930199e24SMatthias Ringwald hci_stack->inquiry_state = duration; 840030199e24SMatthias Ringwald hci_stack->inquiry_max_period_length = max_period_length; 840130199e24SMatthias Ringwald hci_stack->inquiry_min_period_length = min_period_length; 8402f5875de5SMatthias Ringwald hci_run(); 8403f5875de5SMatthias Ringwald return 0; 8404f5875de5SMatthias Ringwald } 8405f5875de5SMatthias Ringwald 8406f5875de5SMatthias Ringwald /** 8407f5875de5SMatthias Ringwald * @brief Stop GAP Classic Inquiry 84086b65794dSMilanka Ringwald * @return 0 if ok 8409f5875de5SMatthias Ringwald */ 8410f5875de5SMatthias Ringwald int gap_inquiry_stop(void){ 8411a1df452eSMatthias Ringwald if ((hci_stack->inquiry_state >= GAP_INQUIRY_DURATION_MIN) && (hci_stack->inquiry_state <= GAP_INQUIRY_DURATION_MAX)) { 8412f5875de5SMatthias Ringwald // emit inquiry complete event, before it even started 8413f5875de5SMatthias Ringwald uint8_t event[] = { GAP_EVENT_INQUIRY_COMPLETE, 1, 0}; 8414f5875de5SMatthias Ringwald hci_emit_event(event, sizeof(event), 1); 8415f5875de5SMatthias Ringwald return 0; 8416f5875de5SMatthias Ringwald } 841730199e24SMatthias Ringwald switch (hci_stack->inquiry_state){ 841830199e24SMatthias Ringwald case GAP_INQUIRY_STATE_ACTIVE: 8419f5875de5SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_CANCEL; 8420f5875de5SMatthias Ringwald hci_run(); 842130199e24SMatthias Ringwald return ERROR_CODE_SUCCESS; 842230199e24SMatthias Ringwald case GAP_INQUIRY_STATE_PERIODIC: 842330199e24SMatthias Ringwald hci_stack->inquiry_state = GAP_INQUIRY_STATE_W2_EXIT_PERIODIC; 842430199e24SMatthias Ringwald hci_run(); 842530199e24SMatthias Ringwald return ERROR_CODE_SUCCESS; 842630199e24SMatthias Ringwald default: 842730199e24SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 842830199e24SMatthias Ringwald } 8429f5875de5SMatthias Ringwald } 8430f5875de5SMatthias Ringwald 8431496bb884SMatthias Ringwald void gap_inquiry_set_lap(uint32_t lap){ 8432496bb884SMatthias Ringwald hci_stack->inquiry_lap = lap; 8433496bb884SMatthias Ringwald } 8434496bb884SMatthias Ringwald 84351dc973e7SMatthias Ringwald void gap_inquiry_set_scan_activity(uint16_t inquiry_scan_interval, uint16_t inquiry_scan_window){ 8436c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_interval = inquiry_scan_interval; 8437c0c8a829SMatthias Ringwald hci_stack->inquiry_scan_window = inquiry_scan_window; 8438baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_INQUIRY_SCAN_ACTIVITY; 8439c0c8a829SMatthias Ringwald hci_run(); 8440c0c8a829SMatthias Ringwald } 8441c0c8a829SMatthias Ringwald 8442b7f1ee76SMatthias Ringwald 8443b7f1ee76SMatthias Ringwald /** 8444b7f1ee76SMatthias Ringwald * @brief Remote Name Request 8445b7f1ee76SMatthias Ringwald * @param addr 8446b7f1ee76SMatthias Ringwald * @param page_scan_repetition_mode 8447b7f1ee76SMatthias Ringwald * @param clock_offset only used when bit 15 is set 8448b7f1ee76SMatthias Ringwald * @events: HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 8449b7f1ee76SMatthias Ringwald */ 8450667ba9d1SMatthias Ringwald int gap_remote_name_request(const bd_addr_t addr, uint8_t page_scan_repetition_mode, uint16_t clock_offset){ 8451b7f1ee76SMatthias Ringwald if (hci_stack->remote_name_state != GAP_REMOTE_NAME_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 84526535961aSMatthias Ringwald (void)memcpy(hci_stack->remote_name_addr, addr, 6); 8453b7f1ee76SMatthias Ringwald hci_stack->remote_name_page_scan_repetition_mode = page_scan_repetition_mode; 8454b7f1ee76SMatthias Ringwald hci_stack->remote_name_clock_offset = clock_offset; 8455b7f1ee76SMatthias Ringwald hci_stack->remote_name_state = GAP_REMOTE_NAME_STATE_W2_SEND; 8456b7f1ee76SMatthias Ringwald hci_run(); 8457b7f1ee76SMatthias Ringwald return 0; 8458b7f1ee76SMatthias Ringwald } 8459b7f1ee76SMatthias Ringwald 8460667ba9d1SMatthias Ringwald static int gap_pairing_set_state_and_run(const bd_addr_t addr, uint8_t state){ 84610a51f88bSMatthias Ringwald hci_stack->gap_pairing_state = state; 84626535961aSMatthias Ringwald (void)memcpy(hci_stack->gap_pairing_addr, addr, 6); 84630a51f88bSMatthias Ringwald hci_run(); 84640a51f88bSMatthias Ringwald return 0; 84650a51f88bSMatthias Ringwald } 84660a51f88bSMatthias Ringwald 84670a51f88bSMatthias Ringwald /** 8468aad97216SMatthias Ringwald * @brief Legacy Pairing Pin Code Response for binary data / non-strings 8469aad97216SMatthias Ringwald * @param addr 8470aad97216SMatthias Ringwald * @param pin_data 8471aad97216SMatthias Ringwald * @param pin_len 8472aad97216SMatthias Ringwald * @return 0 if ok 8473aad97216SMatthias Ringwald */ 8474667ba9d1SMatthias Ringwald int gap_pin_code_response_binary(const bd_addr_t addr, const uint8_t * pin_data, uint8_t pin_len){ 8475aad97216SMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8476aad97216SMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_pin = pin_data; 8477aad97216SMatthias Ringwald hci_stack->gap_pairing_pin_len = pin_len; 8478aad97216SMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN); 8479aad97216SMatthias Ringwald } 8480aad97216SMatthias Ringwald 8481aad97216SMatthias Ringwald /** 84820a51f88bSMatthias Ringwald * @brief Legacy Pairing Pin Code Response 84830a51f88bSMatthias Ringwald * @param addr 84840a51f88bSMatthias Ringwald * @param pin 84850a51f88bSMatthias Ringwald * @return 0 if ok 84860a51f88bSMatthias Ringwald */ 8487667ba9d1SMatthias Ringwald int gap_pin_code_response(const bd_addr_t addr, const char * pin){ 84889d78a7ecSMatthias Ringwald return gap_pin_code_response_binary(addr, (const uint8_t*) pin, (uint8_t) strlen(pin)); 84890a51f88bSMatthias Ringwald } 84900a51f88bSMatthias Ringwald 84910a51f88bSMatthias Ringwald /** 84920a51f88bSMatthias Ringwald * @brief Abort Legacy Pairing 84930a51f88bSMatthias Ringwald * @param addr 84940a51f88bSMatthias Ringwald * @param pin 84950a51f88bSMatthias Ringwald * @return 0 if ok 84960a51f88bSMatthias Ringwald */ 84970a51f88bSMatthias Ringwald int gap_pin_code_negative(bd_addr_t addr){ 8498cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 84990a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PIN_NEGATIVE); 85000a51f88bSMatthias Ringwald } 85010a51f88bSMatthias Ringwald 85020a51f88bSMatthias Ringwald /** 85030a51f88bSMatthias Ringwald * @brief SSP Passkey Response 85040a51f88bSMatthias Ringwald * @param addr 85050a51f88bSMatthias Ringwald * @param passkey 85060a51f88bSMatthias Ringwald * @return 0 if ok 85070a51f88bSMatthias Ringwald */ 8508667ba9d1SMatthias Ringwald int gap_ssp_passkey_response(const bd_addr_t addr, uint32_t passkey){ 8509cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 8510d504181aSMatthias Ringwald hci_stack->gap_pairing_input.gap_pairing_passkey = passkey; 85110a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY); 85120a51f88bSMatthias Ringwald } 85130a51f88bSMatthias Ringwald 85140a51f88bSMatthias Ringwald /** 85150a51f88bSMatthias Ringwald * @brief Abort SSP Passkey Entry/Pairing 85160a51f88bSMatthias Ringwald * @param addr 85170a51f88bSMatthias Ringwald * @param pin 85180a51f88bSMatthias Ringwald * @return 0 if ok 85190a51f88bSMatthias Ringwald */ 8520667ba9d1SMatthias Ringwald int gap_ssp_passkey_negative(const bd_addr_t addr){ 8521cd74063aSMatthias Ringwald if (hci_stack->gap_pairing_state != GAP_PAIRING_STATE_IDLE) return ERROR_CODE_COMMAND_DISALLOWED; 85220a51f88bSMatthias Ringwald return gap_pairing_set_state_and_run(addr, GAP_PAIRING_STATE_SEND_PASSKEY_NEGATIVE); 85230a51f88bSMatthias Ringwald } 85240a51f88bSMatthias Ringwald 85250a51f88bSMatthias Ringwald /** 85260a51f88bSMatthias Ringwald * @brief Accept SSP Numeric Comparison 85270a51f88bSMatthias Ringwald * @param addr 85280a51f88bSMatthias Ringwald * @param passkey 85290a51f88bSMatthias Ringwald * @return 0 if ok 85300a51f88bSMatthias Ringwald */ 8531667ba9d1SMatthias Ringwald int gap_ssp_confirmation_response(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_CONFIRMATION); 85340a51f88bSMatthias Ringwald } 85350a51f88bSMatthias Ringwald 85360a51f88bSMatthias Ringwald /** 85370a51f88bSMatthias Ringwald * @brief Abort SSP Numeric Comparison/Pairing 85380a51f88bSMatthias Ringwald * @param addr 85390a51f88bSMatthias Ringwald * @param pin 85400a51f88bSMatthias Ringwald * @return 0 if ok 85410a51f88bSMatthias Ringwald */ 8542667ba9d1SMatthias Ringwald int gap_ssp_confirmation_negative(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_NEGATIVE); 85450a51f88bSMatthias Ringwald } 85460a51f88bSMatthias Ringwald 8547308eeaffSMatthias Ringwald #if defined(ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY) || defined(ENABLE_EXPLICIT_LINK_KEY_REPLY) 854844565b0cSMatthias Ringwald static uint8_t gap_set_auth_flag_and_run(const bd_addr_t addr, hci_authentication_flags_t flag){ 854944565b0cSMatthias Ringwald hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 855044565b0cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 855144565b0cSMatthias Ringwald connectionSetAuthenticationFlags(conn, flag); 855244565b0cSMatthias Ringwald hci_run(); 855344565b0cSMatthias Ringwald return ERROR_CODE_SUCCESS; 855444565b0cSMatthias Ringwald } 8555308eeaffSMatthias Ringwald #endif 855644565b0cSMatthias Ringwald 8557308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_IO_CAPABILITIES_REPLY 855844565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_response(const bd_addr_t addr){ 85597ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_REPLY); 856044565b0cSMatthias Ringwald } 856144565b0cSMatthias Ringwald 856244565b0cSMatthias Ringwald uint8_t gap_ssp_io_capabilities_negative(const bd_addr_t addr){ 85637ca4a7edSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_SEND_IO_CAPABILITIES_NEGATIVE_REPLY); 856444565b0cSMatthias Ringwald } 856544565b0cSMatthias Ringwald #endif 856644565b0cSMatthias Ringwald 85671849becdSMatthias Ringwald #ifdef ENABLE_CLASSIC_PAIRING_OOB 85681849becdSMatthias Ringwald /** 85691849becdSMatthias Ringwald * @brief Report Remote OOB Data 85701849becdSMatthias Ringwald * @param bd_addr 85711849becdSMatthias Ringwald * @param c_192 Simple Pairing Hash C derived from P-192 public key 85721849becdSMatthias Ringwald * @param r_192 Simple Pairing Randomizer derived from P-192 public key 85731849becdSMatthias Ringwald * @param c_256 Simple Pairing Hash C derived from P-256 public key 85741849becdSMatthias Ringwald * @param r_256 Simple Pairing Randomizer derived from P-256 public key 85751849becdSMatthias Ringwald */ 85761849becdSMatthias 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){ 85771849becdSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 85781849becdSMatthias Ringwald if (connection == NULL) { 85791849becdSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 85801849becdSMatthias Ringwald } 85811849becdSMatthias Ringwald connection->classic_oob_c_192 = c_192; 85821849becdSMatthias Ringwald connection->classic_oob_r_192 = r_192; 8583204e8f1dSMatthias Ringwald 8584204e8f1dSMatthias Ringwald // ignore P-256 if not supported by us 8585204e8f1dSMatthias Ringwald if (hci_stack->secure_connections_active){ 85861849becdSMatthias Ringwald connection->classic_oob_c_256 = c_256; 85871849becdSMatthias Ringwald connection->classic_oob_r_256 = r_256; 8588204e8f1dSMatthias Ringwald } 8589204e8f1dSMatthias Ringwald 85901849becdSMatthias Ringwald return ERROR_CODE_SUCCESS; 85911849becdSMatthias Ringwald } 8592cf01e888SMatthias Ringwald /** 8593cf01e888SMatthias Ringwald * @brief Generate new OOB data 8594cf01e888SMatthias Ringwald * @note OOB data will be provided in GAP_EVENT_LOCAL_OOB_DATA and be used in future pairing procedures 8595cf01e888SMatthias Ringwald */ 8596cf01e888SMatthias Ringwald void gap_ssp_generate_oob_data(void){ 8597cf01e888SMatthias Ringwald hci_stack->classic_read_local_oob_data = true; 8598cf01e888SMatthias Ringwald hci_run(); 8599cf01e888SMatthias Ringwald } 8600cf01e888SMatthias Ringwald 86011849becdSMatthias Ringwald #endif 86021849becdSMatthias Ringwald 8603308eeaffSMatthias Ringwald #ifdef ENABLE_EXPLICIT_LINK_KEY_REPLY 8604308eeaffSMatthias Ringwald uint8_t gap_send_link_key_response(const bd_addr_t addr, link_key_t link_key, link_key_type_t type){ 8605308eeaffSMatthias Ringwald hci_connection_t * connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 8606308eeaffSMatthias Ringwald if (connection == NULL) { 8607308eeaffSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8608308eeaffSMatthias Ringwald } 8609308eeaffSMatthias Ringwald 8610308eeaffSMatthias Ringwald memcpy(connection->link_key, link_key, sizeof(link_key_t)); 8611308eeaffSMatthias Ringwald connection->link_key_type = type; 8612308eeaffSMatthias Ringwald 8613308eeaffSMatthias Ringwald return gap_set_auth_flag_and_run(addr, AUTH_FLAG_HANDLE_LINK_KEY_REQUEST); 8614308eeaffSMatthias Ringwald } 8615308eeaffSMatthias Ringwald 8616308eeaffSMatthias Ringwald #endif // ENABLE_EXPLICIT_LINK_KEY_REPLY 8617f5875de5SMatthias Ringwald /** 8618f6858d14SMatthias Ringwald * @brief Set inquiry mode: standard, with RSSI, with RSSI + Extended Inquiry Results. Has to be called before power on. 8619f5875de5SMatthias Ringwald * @param inquiry_mode see bluetooth_defines.h 8620f6858d14SMatthias Ringwald */ 8621b45b7749SMilanka Ringwald void hci_set_inquiry_mode(inquiry_mode_t inquiry_mode){ 8622b45b7749SMilanka Ringwald hci_stack->inquiry_mode = inquiry_mode; 8623f6858d14SMatthias Ringwald } 8624f6858d14SMatthias Ringwald 8625f6858d14SMatthias Ringwald /** 8626d950d659SMatthias Ringwald * @brief Configure Voice Setting for use with SCO data in HSP/HFP 8627d950d659SMatthias Ringwald */ 8628d950d659SMatthias Ringwald void hci_set_sco_voice_setting(uint16_t voice_setting){ 8629d950d659SMatthias Ringwald hci_stack->sco_voice_setting = voice_setting; 8630d950d659SMatthias Ringwald } 8631d950d659SMatthias Ringwald 8632d950d659SMatthias Ringwald /** 8633d950d659SMatthias Ringwald * @brief Get SCO Voice Setting 8634d950d659SMatthias Ringwald * @return current voice setting 8635d950d659SMatthias Ringwald */ 86360cb5b971SMatthias Ringwald uint16_t hci_get_sco_voice_setting(void){ 8637d950d659SMatthias Ringwald return hci_stack->sco_voice_setting; 8638d950d659SMatthias Ringwald } 8639d950d659SMatthias Ringwald 8640400ff5abSMatthias Ringwald static int hci_have_usb_transport(void){ 8641400ff5abSMatthias Ringwald if (!hci_stack->hci_transport) return 0; 8642400ff5abSMatthias Ringwald const char * transport_name = hci_stack->hci_transport->name; 8643400ff5abSMatthias Ringwald if (!transport_name) return 0; 8644400ff5abSMatthias Ringwald return (transport_name[0] == 'H') && (transport_name[1] == '2'); 8645400ff5abSMatthias Ringwald } 8646400ff5abSMatthias Ringwald 8647b3aad8daSMatthias Ringwald /** @brief Get SCO packet length for current SCO Voice setting 8648b3aad8daSMatthias Ringwald * @note Using SCO packets of the exact length is required for USB transfer 8649b3aad8daSMatthias Ringwald * @return Length of SCO packets in bytes (not audio frames) 8650b3aad8daSMatthias Ringwald */ 865120dcdd22SMatthias Ringwald uint16_t hci_get_sco_packet_length(void){ 865220dcdd22SMatthias Ringwald uint16_t sco_packet_length = 0; 8653cf119f3bSMatthias Ringwald 86541e20a53eSMatthias Ringwald #ifdef ENABLE_SCO_OVER_HCI 86556f28d2eeSMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 86561431ce27SMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 8657cf119f3bSMatthias Ringwald 8658400ff5abSMatthias Ringwald if (hci_have_usb_transport()){ 8659400ff5abSMatthias Ringwald // see Core Spec for H2 USB Transfer. 8660cf119f3bSMatthias Ringwald // 3 byte SCO header + 24 bytes per connection 866152e46257SMatthias Ringwald int num_sco_connections = btstack_max(1, hci_number_sco_connections()); 866252e46257SMatthias Ringwald sco_packet_length = 3 + 24 * num_sco_connections * multiplier; 8663400ff5abSMatthias Ringwald } else { 8664400ff5abSMatthias Ringwald // 3 byte SCO header + SCO packet size over the air (60 bytes) 8665400ff5abSMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 8666d966a453SMatthias Ringwald // assert that it still fits inside an SCO buffer 8667ad915196SMatthias Ringwald if (sco_packet_length > (hci_stack->sco_data_packet_length + 3)){ 8668d966a453SMatthias Ringwald sco_packet_length = 3 + 60; 8669d966a453SMatthias Ringwald } 8670400ff5abSMatthias Ringwald } 8671a3069afeSMatthias Ringwald #endif 86725b7087c7SMatthias Ringwald 86735b7087c7SMatthias Ringwald #ifdef HAVE_SCO_TRANSPORT 86745b7087c7SMatthias Ringwald // Transparent = mSBC => 1, CVSD with 16-bit samples requires twice as much bytes 86751e20a53eSMatthias Ringwald int multiplier = ((hci_stack->sco_voice_setting_active & 0x03) == 0x03) ? 1 : 2; 86765b7087c7SMatthias Ringwald sco_packet_length = 3 + 60 * multiplier; 86775b7087c7SMatthias Ringwald #endif 8678a3069afeSMatthias Ringwald return sco_packet_length; 8679b3aad8daSMatthias Ringwald } 8680b3aad8daSMatthias Ringwald 8681c4c88f1bSJakob Krantz /** 8682c4c88f1bSJakob Krantz * @brief Sets the master/slave policy 8683c4c88f1bSJakob Krantz * @param policy (0: attempt to become master, 1: let connecting device decide) 8684c4c88f1bSJakob Krantz */ 8685c4c88f1bSJakob Krantz void hci_set_master_slave_policy(uint8_t policy){ 8686c4c88f1bSJakob Krantz hci_stack->master_slave_policy = policy; 8687c4c88f1bSJakob Krantz } 8688c4c88f1bSJakob Krantz 8689c4c88f1bSJakob Krantz #endif 8690ec111c8bSMatthias Ringwald 8691ec111c8bSMatthias Ringwald HCI_STATE hci_get_state(void){ 8692ec111c8bSMatthias Ringwald return hci_stack->state; 8693ec111c8bSMatthias Ringwald } 8694ec111c8bSMatthias Ringwald 869507e010b6SMilanka Ringwald #ifdef ENABLE_CLASSIC 86965e91d96cSMatthias Ringwald void gap_register_classic_connection_filter(int (*accept_callback)(bd_addr_t addr, hci_link_type_t link_type)){ 869707e010b6SMilanka Ringwald hci_stack->gap_classic_accept_callback = accept_callback; 869807e010b6SMilanka Ringwald } 869907e010b6SMilanka Ringwald #endif 8700ec111c8bSMatthias Ringwald 8701d950d659SMatthias Ringwald /** 8702d23838ecSMatthias Ringwald * @brief Set callback for Bluetooth Hardware Error 8703d23838ecSMatthias Ringwald */ 8704c2e1fa60SMatthias Ringwald void hci_set_hardware_error_callback(void (*fn)(uint8_t error)){ 8705d23838ecSMatthias Ringwald hci_stack->hardware_error_callback = fn; 8706d23838ecSMatthias Ringwald } 8707d23838ecSMatthias Ringwald 870871de195eSMatthias Ringwald void hci_disconnect_all(void){ 8709665d90f2SMatthias Ringwald btstack_linked_list_iterator_t it; 8710665d90f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 8711665d90f2SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8712665d90f2SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 871304a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 871404a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 871504a6ef8cSmatthias.ringwald } 8716d31fba26S[email protected] hci_run(); 871704a6ef8cSmatthias.ringwald } 871833373e40SMatthias Ringwald 871933373e40SMatthias Ringwald uint16_t hci_get_manufacturer(void){ 872033373e40SMatthias Ringwald return hci_stack->manufacturer; 872133373e40SMatthias Ringwald } 87229c6e867eSMatthias Ringwald 87233e329ddfSandryblack #ifdef ENABLE_BLE 87249c6e867eSMatthias Ringwald static sm_connection_t * sm_get_connection_for_handle(hci_con_handle_t con_handle){ 87259c6e867eSMatthias Ringwald hci_connection_t * hci_con = hci_connection_for_handle(con_handle); 87269c6e867eSMatthias Ringwald if (!hci_con) return NULL; 87279c6e867eSMatthias Ringwald return &hci_con->sm_connection; 87289c6e867eSMatthias Ringwald } 87299c6e867eSMatthias Ringwald 87309c6e867eSMatthias Ringwald // extracted from sm.c to allow enabling of l2cap le data channels without adding sm.c to the build 87319c6e867eSMatthias Ringwald // without sm.c default values from create_connection_for_bd_addr_and_type() resulg in non-encrypted, not-authenticated 87326a79f6baSMatthias Ringwald #endif 87339c6e867eSMatthias Ringwald 87346d105c71SMatthias Ringwald uint8_t gap_encryption_key_size(hci_con_handle_t con_handle){ 8735c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8736c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 8737c5fb5ca4SMatthias Ringwald if (hci_is_le_connection(hci_connection)){ 87386a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 8739c5fb5ca4SMatthias Ringwald sm_connection_t * sm_conn = &hci_connection->sm_connection; 8740c5fb5ca4SMatthias Ringwald if (sm_conn->sm_connection_encrypted) { 87419c6e867eSMatthias Ringwald return sm_conn->sm_actual_encryption_key_size; 87429c6e867eSMatthias Ringwald } 87436a79f6baSMatthias Ringwald #endif 87446a79f6baSMatthias Ringwald } else { 8745c5fb5ca4SMatthias Ringwald #ifdef ENABLE_CLASSIC 87468daf94bcSMatthias Ringwald if ((hci_connection->authentication_flags & AUTH_FLAG_CONNECTION_ENCRYPTED)){ 8747c5fb5ca4SMatthias Ringwald return hci_connection->encryption_key_size; 8748c5fb5ca4SMatthias Ringwald } 8749c5fb5ca4SMatthias Ringwald #endif 87506a79f6baSMatthias Ringwald } 8751c5fb5ca4SMatthias Ringwald return 0; 8752c5fb5ca4SMatthias Ringwald } 87539c6e867eSMatthias Ringwald 8754ff3f1026SMatthias Ringwald bool gap_authenticated(hci_con_handle_t con_handle){ 8755c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8756ff3f1026SMatthias Ringwald if (hci_connection == NULL) return false; 87575f3981bfSMatthias Ringwald 8758c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 87596a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 87605f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 87615f3981bfSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8762c5fb5ca4SMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return 0; // unencrypted connection cannot be authenticated 8763ff3f1026SMatthias Ringwald return hci_connection->sm_connection.sm_connection_authenticated != 0; 87646a79f6baSMatthias Ringwald #endif 876559a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 87665f3981bfSMatthias Ringwald case BD_ADDR_TYPE_SCO: 8767f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8768ff3f1026SMatthias Ringwald return gap_authenticated_for_link_key_type(hci_connection->link_key_type); 876959a1a47aSMatthias Ringwald #endif 87705f3981bfSMatthias Ringwald default: 8771ff3f1026SMatthias Ringwald return false; 87725f3981bfSMatthias Ringwald } 87739c6e867eSMatthias Ringwald } 87749c6e867eSMatthias Ringwald 8775f461b3dfSMatthias Ringwald bool gap_secure_connection(hci_con_handle_t con_handle){ 8776c5fb5ca4SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8777c5fb5ca4SMatthias Ringwald if (hci_connection == NULL) return 0; 87788b35e16aSMatthias Ringwald 8779c5fb5ca4SMatthias Ringwald switch (hci_connection->address_type){ 87806a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 87818b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 87828b35e16aSMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 8783f461b3dfSMatthias Ringwald if (hci_connection->sm_connection.sm_connection_encrypted == 0) return false; // unencrypted connection cannot be authenticated 8784f461b3dfSMatthias Ringwald return hci_connection->sm_connection.sm_connection_sc != 0; 87856a79f6baSMatthias Ringwald #endif 878659a1a47aSMatthias Ringwald #ifdef ENABLE_CLASSIC 87878b35e16aSMatthias Ringwald case BD_ADDR_TYPE_SCO: 8788f16129ceSMatthias Ringwald case BD_ADDR_TYPE_ACL: 8789f461b3dfSMatthias Ringwald return gap_secure_connection_for_link_key_type(hci_connection->link_key_type); 879059a1a47aSMatthias Ringwald #endif 87918b35e16aSMatthias Ringwald default: 8792f461b3dfSMatthias Ringwald return false; 87938b35e16aSMatthias Ringwald } 8794f1dfbe18SMatthias Ringwald } 8795f1dfbe18SMatthias Ringwald 87961e122704SMatthias Ringwald bool gap_bonded(hci_con_handle_t con_handle){ 87971e122704SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 87981e122704SMatthias Ringwald if (hci_connection == NULL) return 0; 87991e122704SMatthias Ringwald 880048f33f37SMatthias Ringwald #ifdef ENABLE_CLASSIC 88011e122704SMatthias Ringwald link_key_t link_key; 88021e122704SMatthias Ringwald link_key_type_t link_key_type; 880348f33f37SMatthias Ringwald #endif 88041e122704SMatthias Ringwald switch (hci_connection->address_type){ 88056a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 88061e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_PUBLIC: 88071e122704SMatthias Ringwald case BD_ADDR_TYPE_LE_RANDOM: 88081e122704SMatthias Ringwald return hci_connection->sm_connection.sm_le_db_index >= 0; 88096a79f6baSMatthias Ringwald #endif 88101e122704SMatthias Ringwald #ifdef ENABLE_CLASSIC 88111e122704SMatthias Ringwald case BD_ADDR_TYPE_SCO: 88121e122704SMatthias Ringwald case BD_ADDR_TYPE_ACL: 88131e122704SMatthias Ringwald return hci_stack->link_key_db && hci_stack->link_key_db->get_link_key(hci_connection->address, link_key, &link_key_type); 88141e122704SMatthias Ringwald #endif 88151e122704SMatthias Ringwald default: 88161e122704SMatthias Ringwald return false; 88171e122704SMatthias Ringwald } 88181e122704SMatthias Ringwald } 88191e122704SMatthias Ringwald 88206a79f6baSMatthias Ringwald #ifdef ENABLE_BLE 88219c6e867eSMatthias Ringwald authorization_state_t gap_authorization_state(hci_con_handle_t con_handle){ 88229c6e867eSMatthias Ringwald sm_connection_t * sm_conn = sm_get_connection_for_handle(con_handle); 88239c6e867eSMatthias Ringwald if (!sm_conn) return AUTHORIZATION_UNKNOWN; // wrong connection 88249c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_encrypted) return AUTHORIZATION_UNKNOWN; // unencrypted connection cannot be authorized 88259c6e867eSMatthias Ringwald if (!sm_conn->sm_connection_authenticated) return AUTHORIZATION_UNKNOWN; // unauthenticatd connection cannot be authorized 88269c6e867eSMatthias Ringwald return sm_conn->sm_connection_authorization_state; 88279c6e867eSMatthias Ringwald } 88289c6e867eSMatthias Ringwald #endif 8829f8ee3071SMatthias Ringwald 8830f8ee3071SMatthias Ringwald #ifdef ENABLE_CLASSIC 8831f8ee3071SMatthias 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){ 8832f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88337f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8834f8ee3071SMatthias Ringwald conn->sniff_min_interval = sniff_min_interval; 8835f8ee3071SMatthias Ringwald conn->sniff_max_interval = sniff_max_interval; 8836f8ee3071SMatthias Ringwald conn->sniff_attempt = sniff_attempt; 8837f8ee3071SMatthias Ringwald conn->sniff_timeout = sniff_timeout; 8838f8ee3071SMatthias Ringwald hci_run(); 8839f8ee3071SMatthias Ringwald return 0; 8840f8ee3071SMatthias Ringwald } 8841f8ee3071SMatthias Ringwald 8842f8ee3071SMatthias Ringwald /** 8843f8ee3071SMatthias Ringwald * @brief Exit Sniff mode 8844f8ee3071SMatthias Ringwald * @param con_handle 8845f8ee3071SMatthias Ringwald @ @return 0 if ok 8846f8ee3071SMatthias Ringwald */ 8847f8ee3071SMatthias Ringwald uint8_t gap_sniff_mode_exit(hci_con_handle_t con_handle){ 8848f8ee3071SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88497f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8850f8ee3071SMatthias Ringwald conn->sniff_min_interval = 0xffff; 8851f8ee3071SMatthias Ringwald hci_run(); 8852f8ee3071SMatthias Ringwald return 0; 8853f8ee3071SMatthias Ringwald } 8854bea424a5SMatthias Ringwald 8855140c0557SMatthias 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){ 8856140c0557SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88577f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8858140c0557SMatthias Ringwald conn->sniff_subrating_max_latency = max_latency; 8859140c0557SMatthias Ringwald conn->sniff_subrating_min_remote_timeout = min_remote_timeout; 8860140c0557SMatthias Ringwald conn->sniff_subrating_min_local_timeout = min_local_timeout; 8861140c0557SMatthias Ringwald hci_run(); 8862dc8d5bd3SMatthias Ringwald return ERROR_CODE_SUCCESS; 8863140c0557SMatthias Ringwald } 8864140c0557SMatthias Ringwald 8865278ff8a9SMatthias 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){ 8866278ff8a9SMatthias Ringwald hci_connection_t * conn = hci_connection_for_handle(con_handle); 88677f839e2cSMatthias Ringwald if (!conn) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8868278ff8a9SMatthias Ringwald conn->qos_service_type = service_type; 8869278ff8a9SMatthias Ringwald conn->qos_token_rate = token_rate; 8870278ff8a9SMatthias Ringwald conn->qos_peak_bandwidth = peak_bandwidth; 8871278ff8a9SMatthias Ringwald conn->qos_latency = latency; 8872278ff8a9SMatthias Ringwald conn->qos_delay_variation = delay_variation; 8873278ff8a9SMatthias Ringwald hci_run(); 8874278ff8a9SMatthias Ringwald return ERROR_CODE_SUCCESS; 8875278ff8a9SMatthias Ringwald } 8876278ff8a9SMatthias Ringwald 8877bea424a5SMatthias Ringwald void gap_set_page_scan_activity(uint16_t page_scan_interval, uint16_t page_scan_window){ 8878bea424a5SMatthias Ringwald hci_stack->new_page_scan_interval = page_scan_interval; 8879bea424a5SMatthias Ringwald hci_stack->new_page_scan_window = page_scan_window; 8880baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_ACTIVITY; 8881bea424a5SMatthias Ringwald hci_run(); 8882bea424a5SMatthias Ringwald } 8883bea424a5SMatthias Ringwald 8884bea424a5SMatthias Ringwald void gap_set_page_scan_type(page_scan_type_t page_scan_type){ 8885bea424a5SMatthias Ringwald hci_stack->new_page_scan_type = (uint8_t) page_scan_type; 8886baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_SCAN_TYPE; 8887bea424a5SMatthias Ringwald hci_run(); 8888bea424a5SMatthias Ringwald } 8889bea424a5SMatthias Ringwald 889032a12730SMatthias Ringwald void gap_set_page_timeout(uint16_t page_timeout){ 889132a12730SMatthias Ringwald hci_stack->page_timeout = page_timeout; 8892baa4881eSMatthias Ringwald hci_stack->gap_tasks_classic |= GAP_TASK_WRITE_PAGE_TIMEOUT; 889332a12730SMatthias Ringwald hci_run(); 889432a12730SMatthias Ringwald } 889532a12730SMatthias Ringwald 8896f8ee3071SMatthias Ringwald #endif 8897beceeddeSMatthias Ringwald 889821debf25SMatthias Ringwald #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION 889921debf25SMatthias Ringwald void hci_load_le_device_db_entry_into_resolving_list(uint16_t le_device_db_index){ 890021debf25SMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 890121debf25SMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 890221debf25SMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 890321debf25SMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 890402b02cffSMatthias Ringwald hci_stack->le_resolving_list_add_entries[offset] |= mask; 890521debf25SMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 890602b02cffSMatthias Ringwald // note: go back to remove entries, otherwise, a remove + add will skip the add 890702b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 890802b02cffSMatthias Ringwald } 890902b02cffSMatthias Ringwald } 891002b02cffSMatthias Ringwald 891102b02cffSMatthias Ringwald void hci_remove_le_device_db_entry_from_resolving_list(uint16_t le_device_db_index){ 891202b02cffSMatthias Ringwald if (le_device_db_index >= MAX_NUM_RESOLVING_LIST_ENTRIES) return; 891302b02cffSMatthias Ringwald if (le_device_db_index >= le_device_db_max_count()) return; 891402b02cffSMatthias Ringwald uint8_t offset = le_device_db_index >> 3; 891502b02cffSMatthias Ringwald uint8_t mask = 1 << (le_device_db_index & 7); 891602b02cffSMatthias Ringwald hci_stack->le_resolving_list_remove_entries[offset] |= mask; 891702b02cffSMatthias Ringwald if (hci_stack->le_resolving_list_state == LE_RESOLVING_LIST_DONE){ 891802b02cffSMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_REMOVE_ENTRIES; 891921debf25SMatthias Ringwald } 892021debf25SMatthias Ringwald } 8921cf38a091SMatthias Ringwald 8922cf38a091SMatthias Ringwald uint8_t gap_load_resolving_list_from_le_device_db(void){ 89231ffa425cSMatthias Ringwald if (hci_command_supported(SUPPORTED_HCI_COMMAND_LE_SET_ADDRESS_RESOLUTION_ENABLE) == false){ 8924cf38a091SMatthias Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 8925cf38a091SMatthias Ringwald } 8926cf38a091SMatthias Ringwald if (hci_stack->le_resolving_list_state != LE_RESOLVING_LIST_SEND_ENABLE_ADDRESS_RESOLUTION){ 8927cf38a091SMatthias Ringwald // restart le resolving list update 8928cf38a091SMatthias Ringwald hci_stack->le_resolving_list_state = LE_RESOLVING_LIST_READ_SIZE; 8929cf38a091SMatthias Ringwald } 8930cf38a091SMatthias Ringwald return ERROR_CODE_SUCCESS; 8931cf38a091SMatthias Ringwald } 893221debf25SMatthias Ringwald #endif 893321debf25SMatthias Ringwald 8934f40c73b4SMatthias Ringwald #ifdef ENABLE_BLE 8935f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_CENTRAL 8936f40c73b4SMatthias Ringwald #ifdef ENABLE_LE_EXTENDED_ADVERTISING 8937f40c73b4SMatthias Ringwald 8938f40c73b4SMatthias Ringwald static uint8_t hci_periodic_advertiser_list_add(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8939f40c73b4SMatthias Ringwald // check if already in list 8940f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8941f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8942f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 8943f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t *entry = (periodic_advertiser_list_entry_t *) btstack_linked_list_iterator_next(&it); 8944f40c73b4SMatthias Ringwald if (entry->sid != advertising_sid) { 8945f40c73b4SMatthias Ringwald continue; 8946f40c73b4SMatthias Ringwald } 8947f40c73b4SMatthias Ringwald if (entry->address_type != address_type) { 8948f40c73b4SMatthias Ringwald continue; 8949f40c73b4SMatthias Ringwald } 8950f40c73b4SMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 8951f40c73b4SMatthias Ringwald continue; 8952f40c73b4SMatthias Ringwald } 8953f40c73b4SMatthias Ringwald // disallow if already scheduled to add 8954f40c73b4SMatthias Ringwald if ((entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER) != 0){ 8955f40c73b4SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 8956f40c73b4SMatthias Ringwald } 8957f40c73b4SMatthias Ringwald // still on controller, but scheduled to remove -> re-add 8958f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 8959f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8960f40c73b4SMatthias Ringwald } 8961f40c73b4SMatthias Ringwald // alloc and add to list 8962f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = btstack_memory_periodic_advertiser_list_entry_get(); 8963f40c73b4SMatthias Ringwald if (!entry) return BTSTACK_MEMORY_ALLOC_FAILED; 8964f40c73b4SMatthias Ringwald entry->sid = advertising_sid; 8965f40c73b4SMatthias Ringwald entry->address_type = address_type; 8966f40c73b4SMatthias Ringwald (void)memcpy(entry->address, address, 6); 8967f40c73b4SMatthias Ringwald entry->state = LE_PERIODIC_ADVERTISER_LIST_ENTRY_ADD_TO_CONTROLLER; 8968f40c73b4SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t*) entry); 8969f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8970f40c73b4SMatthias Ringwald } 8971f40c73b4SMatthias Ringwald 8972f40c73b4SMatthias Ringwald static uint8_t hci_periodic_advertiser_list_remove(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 8973f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 8974f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 8975f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 8976f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&it); 8977f40c73b4SMatthias Ringwald if (entry->sid != advertising_sid) { 8978f40c73b4SMatthias Ringwald continue; 8979f40c73b4SMatthias Ringwald } 8980f40c73b4SMatthias Ringwald if (entry->address_type != address_type) { 8981f40c73b4SMatthias Ringwald continue; 8982f40c73b4SMatthias Ringwald } 8983f40c73b4SMatthias Ringwald if (memcmp(entry->address, address, 6) != 0) { 8984f40c73b4SMatthias Ringwald continue; 8985f40c73b4SMatthias Ringwald } 8986f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER){ 8987f40c73b4SMatthias Ringwald // remove from controller if already present 8988f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 8989f40c73b4SMatthias Ringwald } else { 8990f40c73b4SMatthias Ringwald // directly remove entry from whitelist 8991f40c73b4SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 8992f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 8993f40c73b4SMatthias Ringwald } 8994f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 8995f40c73b4SMatthias Ringwald } 8996f40c73b4SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 8997f40c73b4SMatthias Ringwald } 8998f40c73b4SMatthias Ringwald 8999f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_clear(void){ 9000f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t it; 9001f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_periodic_advertiser_list); 9002f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9003f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&it); 9004f40c73b4SMatthias Ringwald if (entry->state & LE_PERIODIC_ADVERTISER_LIST_ENTRY_ON_CONTROLLER){ 9005f40c73b4SMatthias Ringwald // remove from controller if already present 9006f40c73b4SMatthias Ringwald entry->state |= LE_PERIODIC_ADVERTISER_LIST_ENTRY_REMOVE_FROM_CONTROLLER; 9007f40c73b4SMatthias Ringwald continue; 9008f40c73b4SMatthias Ringwald } 9009f40c73b4SMatthias Ringwald // directly remove entry from whitelist 9010f40c73b4SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9011f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 9012f40c73b4SMatthias Ringwald } 9013f40c73b4SMatthias Ringwald } 9014f40c73b4SMatthias Ringwald 9015f40c73b4SMatthias Ringwald // free all entries unconditionally 9016f40c73b4SMatthias Ringwald static void hci_periodic_advertiser_list_free(void){ 9017f40c73b4SMatthias Ringwald btstack_linked_list_iterator_t lit; 9018f40c73b4SMatthias Ringwald btstack_linked_list_iterator_init(&lit, &hci_stack->le_periodic_advertiser_list); 9019f40c73b4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&lit)){ 9020f40c73b4SMatthias Ringwald periodic_advertiser_list_entry_t * entry = (periodic_advertiser_list_entry_t*) btstack_linked_list_iterator_next(&lit); 9021f40c73b4SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_periodic_advertiser_list, (btstack_linked_item_t *) entry); 9022f40c73b4SMatthias Ringwald btstack_memory_periodic_advertiser_list_entry_free(entry); 9023f40c73b4SMatthias Ringwald } 9024f40c73b4SMatthias Ringwald } 9025f40c73b4SMatthias Ringwald 9026f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_clear(void){ 9027f40c73b4SMatthias Ringwald hci_periodic_advertiser_list_clear(); 9028f40c73b4SMatthias Ringwald hci_run(); 9029f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9030f40c73b4SMatthias Ringwald } 9031f40c73b4SMatthias Ringwald 9032f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_add(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 9033f40c73b4SMatthias Ringwald uint8_t status = hci_periodic_advertiser_list_add(address_type, address, advertising_sid); 9034f40c73b4SMatthias Ringwald if (status){ 9035f40c73b4SMatthias Ringwald return status; 9036f40c73b4SMatthias Ringwald } 9037f40c73b4SMatthias Ringwald hci_run(); 9038f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9039f40c73b4SMatthias Ringwald } 9040f40c73b4SMatthias Ringwald 9041f40c73b4SMatthias Ringwald uint8_t gap_periodic_advertiser_list_remove(bd_addr_type_t address_type, const bd_addr_t address, uint8_t advertising_sid){ 9042f40c73b4SMatthias Ringwald uint8_t status = hci_periodic_advertiser_list_remove(address_type, address, advertising_sid); 9043f40c73b4SMatthias Ringwald if (status){ 9044f40c73b4SMatthias Ringwald return status; 9045f40c73b4SMatthias Ringwald } 9046f40c73b4SMatthias Ringwald hci_run(); 9047f40c73b4SMatthias Ringwald return ERROR_CODE_SUCCESS; 9048f40c73b4SMatthias Ringwald } 90494bafedd5SMatthias Ringwald 90504bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_create_sync(uint8_t options, uint8_t advertising_sid, bd_addr_type_t advertiser_address_type, 90514bafedd5SMatthias Ringwald bd_addr_t advertiser_address, uint16_t skip, uint16_t sync_timeout, uint8_t sync_cte_type){ 90524bafedd5SMatthias Ringwald // abort if already active 90534bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_request != LE_CONNECTING_IDLE) { 90544bafedd5SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 90554bafedd5SMatthias Ringwald } 90564bafedd5SMatthias Ringwald // store request 90574bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_request = ((options & 0) != 0) ? LE_CONNECTING_WHITELIST : LE_CONNECTING_DIRECT; 90584bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_options = options; 90594bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertising_sid = advertising_sid; 90604bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_advertiser_address_type = advertiser_address_type; 90614bafedd5SMatthias Ringwald memcpy(hci_stack->le_periodic_sync_advertiser_address, advertiser_address, 6); 90624bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_skip = skip; 90634bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_timeout = sync_timeout; 90644bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_cte_type = sync_cte_type; 90654bafedd5SMatthias Ringwald 90664bafedd5SMatthias Ringwald hci_run(); 90674bafedd5SMatthias Ringwald return ERROR_CODE_SUCCESS; 90684bafedd5SMatthias Ringwald } 90694bafedd5SMatthias Ringwald 90704bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_create_sync_cancel(void){ 90714bafedd5SMatthias Ringwald // abort if not requested 90724bafedd5SMatthias Ringwald if (hci_stack->le_periodic_sync_request == LE_CONNECTING_IDLE) { 90734bafedd5SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 90744bafedd5SMatthias Ringwald } 90754bafedd5SMatthias Ringwald hci_stack->le_periodic_sync_request = LE_CONNECTING_IDLE; 90764bafedd5SMatthias Ringwald hci_run(); 90774bafedd5SMatthias Ringwald return ERROR_CODE_SUCCESS; 90784bafedd5SMatthias Ringwald } 90794bafedd5SMatthias Ringwald 90804bafedd5SMatthias Ringwald uint8_t gap_periodic_advertising_terminate_sync(uint16_t sync_handle){ 908149a6c69cSMatthias Ringwald if (hci_stack->le_periodic_terminate_sync_handle != HCI_CON_HANDLE_INVALID){ 908249a6c69cSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 908349a6c69cSMatthias Ringwald } 908449a6c69cSMatthias Ringwald hci_stack->le_periodic_terminate_sync_handle = sync_handle; 908549a6c69cSMatthias Ringwald hci_run(); 908649a6c69cSMatthias Ringwald return ERROR_CODE_SUCCESS; 90874bafedd5SMatthias Ringwald } 90884bafedd5SMatthias Ringwald 9089f40c73b4SMatthias Ringwald #endif 9090f40c73b4SMatthias Ringwald #endif 90914ae8623eSMatthias Ringwald #ifdef ENABLE_LE_ISOCHRONOUS_STREAMS 90928d72db10SMatthias Ringwald static uint8_t hci_iso_stream_create(hci_iso_type_t iso_type, hci_con_handle_t con_handle, uint8_t group_id) { 90934ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = btstack_memory_hci_iso_stream_get(); 90944ae8623eSMatthias Ringwald if (iso_stream == NULL){ 90954ae8623eSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 90964ae8623eSMatthias Ringwald } else { 90978d72db10SMatthias Ringwald iso_stream->iso_type = iso_type; 90984ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_REQUESTED; 909915a15be3SMatthias Ringwald iso_stream->con_handle = con_handle; 91008d72db10SMatthias Ringwald iso_stream->group_id = group_id; 91014ae8623eSMatthias Ringwald btstack_linked_list_add(&hci_stack->iso_streams, (btstack_linked_item_t*) iso_stream); 91024ae8623eSMatthias Ringwald return ERROR_CODE_SUCCESS; 91034ae8623eSMatthias Ringwald } 91044ae8623eSMatthias Ringwald } 91054ae8623eSMatthias Ringwald 910615a15be3SMatthias Ringwald static hci_iso_stream_t * hci_iso_stream_for_con_handle(hci_con_handle_t con_handle){ 91074ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 91084ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 91094ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91104ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 911115a15be3SMatthias Ringwald if (iso_stream->con_handle == con_handle ) { 91124ae8623eSMatthias Ringwald return iso_stream; 91134ae8623eSMatthias Ringwald } 91144ae8623eSMatthias Ringwald } 91154ae8623eSMatthias Ringwald return NULL; 91164ae8623eSMatthias Ringwald } 91174ae8623eSMatthias Ringwald 91184ae8623eSMatthias Ringwald static void hci_iso_stream_finalize(hci_iso_stream_t * iso_stream){ 91198d72db10SMatthias Ringwald log_info("hci_iso_stream_finalize con_handle 0x%04x, group_id 0x%02x", iso_stream->con_handle, iso_stream->group_id); 91204ae8623eSMatthias Ringwald btstack_linked_list_remove(&hci_stack->iso_streams, (btstack_linked_item_t*) iso_stream); 91214ae8623eSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 91224ae8623eSMatthias Ringwald } 91234ae8623eSMatthias Ringwald 9124778463d0SMatthias Ringwald static void hci_iso_stream_finalize_by_type_and_group_id(hci_iso_type_t iso_type, uint8_t group_id) { 9125778463d0SMatthias Ringwald btstack_linked_list_iterator_t it; 9126778463d0SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 9127778463d0SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9128778463d0SMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 9129778463d0SMatthias Ringwald if ((iso_stream->group_id == group_id) && 9130778463d0SMatthias Ringwald (iso_stream->iso_type == iso_type)){ 9131778463d0SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9132778463d0SMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 9133778463d0SMatthias Ringwald } 9134778463d0SMatthias Ringwald } 9135778463d0SMatthias Ringwald } 9136778463d0SMatthias Ringwald 91378d72db10SMatthias Ringwald static void hci_iso_stream_requested_finalize(uint8_t group_id) { 91384ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 91394ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 91404ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91414ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 9142a90016deSMatthias Ringwald if ((iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) && 91438d72db10SMatthias Ringwald (iso_stream->group_id == group_id)){ 91444ae8623eSMatthias Ringwald btstack_linked_list_iterator_remove(&it); 91454ae8623eSMatthias Ringwald btstack_memory_hci_iso_stream_free(iso_stream); 91464ae8623eSMatthias Ringwald } 91474ae8623eSMatthias Ringwald } 91484ae8623eSMatthias Ringwald } 9149a90016deSMatthias Ringwald static void hci_iso_stream_requested_confirm(uint8_t big_handle){ 91504ae8623eSMatthias Ringwald btstack_linked_list_iterator_t it; 91514ae8623eSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->iso_streams); 91524ae8623eSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 91534ae8623eSMatthias Ringwald hci_iso_stream_t * iso_stream = (hci_iso_stream_t *) btstack_linked_list_iterator_next(&it); 91544ae8623eSMatthias Ringwald if ( iso_stream->state == HCI_ISO_STREAM_STATE_REQUESTED ) { 91554ae8623eSMatthias Ringwald iso_stream->state = HCI_ISO_STREAM_STATE_W4_ESTABLISHED; 91564ae8623eSMatthias Ringwald } 91574ae8623eSMatthias Ringwald } 91584ae8623eSMatthias Ringwald } 9159d39e4f95SMatthias Ringwald 9160d39e4f95SMatthias Ringwald static bool hci_iso_sdu_complete(uint8_t * packet, uint16_t size){ 9161d39e4f95SMatthias Ringwald uint8_t sdu_ts_flag = (packet[1] >> 6) & 1; 9162d39e4f95SMatthias Ringwald uint16_t sdu_len_offset = 6 + (sdu_ts_flag * 4); 9163d39e4f95SMatthias Ringwald uint16_t sdu_len = little_endian_read_16(packet, sdu_len_offset) & 0x0fff; 9164d39e4f95SMatthias Ringwald return (sdu_len_offset + 2 + sdu_len) == size; 9165d39e4f95SMatthias Ringwald } 9166d39e4f95SMatthias Ringwald 9167d39e4f95SMatthias Ringwald static void hci_iso_packet_handler(uint8_t * packet, uint16_t size){ 9168d39e4f95SMatthias Ringwald if (hci_stack->iso_packet_handler == NULL) { 9169d39e4f95SMatthias Ringwald return; 9170d39e4f95SMatthias Ringwald } 9171d39e4f95SMatthias Ringwald if (size < 4) { 9172d39e4f95SMatthias Ringwald return; 9173d39e4f95SMatthias Ringwald } 9174d39e4f95SMatthias Ringwald 9175d39e4f95SMatthias Ringwald // parse header 9176d39e4f95SMatthias Ringwald uint16_t conn_handle_and_flags = little_endian_read_16(packet, 0); 9177d39e4f95SMatthias Ringwald uint16_t iso_data_len = little_endian_read_16(packet, 2); 9178d39e4f95SMatthias Ringwald hci_con_handle_t cis_handle = (hci_con_handle_t) (conn_handle_and_flags & 0xfff); 917915a15be3SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(cis_handle); 9180d39e4f95SMatthias Ringwald uint8_t pb_flag = (conn_handle_and_flags >> 12) & 3; 9181d39e4f95SMatthias Ringwald 9182d39e4f95SMatthias Ringwald // assert packet is complete 9183d39e4f95SMatthias Ringwald if ((iso_data_len + 4u) != size){ 9184d39e4f95SMatthias Ringwald return; 9185d39e4f95SMatthias Ringwald } 9186d39e4f95SMatthias Ringwald 9187d39e4f95SMatthias Ringwald if ((pb_flag & 0x01) == 0){ 9188d39e4f95SMatthias Ringwald if (pb_flag == 0x02){ 9189d39e4f95SMatthias Ringwald // The ISO_Data_Load field contains a header and a complete SDU. 9190d39e4f95SMatthias Ringwald if (hci_iso_sdu_complete(packet, size)) { 9191d39e4f95SMatthias Ringwald (hci_stack->iso_packet_handler)(HCI_ISO_DATA_PACKET, 0, packet, size); 9192d39e4f95SMatthias Ringwald } 9193d39e4f95SMatthias Ringwald } else { 9194d39e4f95SMatthias Ringwald // The ISO_Data_Load field contains a header and the first fragment of a fragmented SDU. 9195d39e4f95SMatthias Ringwald if (iso_stream == NULL){ 9196d39e4f95SMatthias Ringwald return; 9197d39e4f95SMatthias Ringwald } 9198d39e4f95SMatthias Ringwald if (size > HCI_ISO_PAYLOAD_SIZE){ 9199d39e4f95SMatthias Ringwald return; 9200d39e4f95SMatthias Ringwald } 9201d39e4f95SMatthias Ringwald memcpy(iso_stream->reassembly_buffer, packet, size); 9202d39e4f95SMatthias Ringwald // fix pb_flag 9203d39e4f95SMatthias Ringwald iso_stream->reassembly_buffer[1] = (iso_stream->reassembly_buffer[1] & 0xcf) | 0x20; 9204d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = size; 9205d39e4f95SMatthias Ringwald } 9206d39e4f95SMatthias Ringwald } else { 9207d39e4f95SMatthias Ringwald // iso_data_load contains continuation or last fragment of an SDU 9208d39e4f95SMatthias Ringwald uint8_t ts_flag = (conn_handle_and_flags >> 14) & 1; 9209d39e4f95SMatthias Ringwald if (ts_flag != 0){ 9210d39e4f95SMatthias Ringwald return; 9211d39e4f95SMatthias Ringwald } 9212d39e4f95SMatthias Ringwald // append fragment 9213d39e4f95SMatthias Ringwald if (iso_stream == NULL){ 9214d39e4f95SMatthias Ringwald return; 9215d39e4f95SMatthias Ringwald } 9216d39e4f95SMatthias Ringwald if (iso_stream->reassembly_pos == 0){ 9217d39e4f95SMatthias Ringwald return; 9218d39e4f95SMatthias Ringwald } 9219d39e4f95SMatthias Ringwald if ((iso_stream->reassembly_pos + iso_data_len) > size){ 9220d39e4f95SMatthias Ringwald // reset reassembly buffer 9221d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = 0; 9222d39e4f95SMatthias Ringwald return; 9223d39e4f95SMatthias Ringwald } 9224d39e4f95SMatthias Ringwald memcpy(&iso_stream->reassembly_buffer[iso_stream->reassembly_pos], &packet[4], iso_data_len); 9225d39e4f95SMatthias Ringwald iso_stream->reassembly_pos += iso_data_len; 9226d39e4f95SMatthias Ringwald 9227d39e4f95SMatthias Ringwald // deliver if last fragment and SDU complete 9228d39e4f95SMatthias Ringwald if (pb_flag == 0x03){ 9229d39e4f95SMatthias Ringwald if (hci_iso_sdu_complete(iso_stream->reassembly_buffer, iso_stream->reassembly_pos)){ 9230d39e4f95SMatthias Ringwald (hci_stack->iso_packet_handler)(HCI_ISO_DATA_PACKET, 0, iso_stream->reassembly_buffer, iso_stream->reassembly_pos); 9231d39e4f95SMatthias Ringwald } 9232d39e4f95SMatthias Ringwald iso_stream->reassembly_pos = 0; 9233d39e4f95SMatthias Ringwald } 9234d39e4f95SMatthias Ringwald } 9235d39e4f95SMatthias Ringwald } 9236d39e4f95SMatthias Ringwald 92377aa35f6aSMatthias Ringwald static void hci_emit_big_created(const le_audio_big_t * big, uint8_t status){ 92387aa35f6aSMatthias Ringwald uint8_t event [6 + (MAX_NR_BIS * 2)]; 92397aa35f6aSMatthias Ringwald uint16_t pos = 0; 92407aa35f6aSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 92417aa35f6aSMatthias Ringwald event[pos++] = 4 + (2 * big->num_bis); 92427aa35f6aSMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_CREATED; 92437aa35f6aSMatthias Ringwald event[pos++] = status; 92447aa35f6aSMatthias Ringwald event[pos++] = big->big_handle; 92457aa35f6aSMatthias Ringwald event[pos++] = big->num_bis; 92467aa35f6aSMatthias Ringwald uint8_t i; 92477aa35f6aSMatthias Ringwald for (i=0;i<big->num_bis;i++){ 92487aa35f6aSMatthias Ringwald little_endian_store_16(event, pos, big->bis_con_handles[i]); 92497aa35f6aSMatthias Ringwald pos += 2; 92507aa35f6aSMatthias Ringwald } 92517aa35f6aSMatthias Ringwald hci_emit_event(event, pos, 0); 92527aa35f6aSMatthias Ringwald } 92537aa35f6aSMatthias Ringwald 9254d3a89d91SMatthias Ringwald static void hci_emit_cig_created(const le_audio_cig_t * cig, uint8_t status){ 9255d3a89d91SMatthias Ringwald uint8_t event [6 + (MAX_NR_CIS * 2)]; 9256d3a89d91SMatthias Ringwald uint16_t pos = 0; 9257d3a89d91SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 9258d3a89d91SMatthias Ringwald event[pos++] = 4 + (2 * cig->num_cis); 9259d3a89d91SMatthias Ringwald event[pos++] = GAP_SUBEVENT_CIG_CREATED; 9260d3a89d91SMatthias Ringwald event[pos++] = status; 9261d3a89d91SMatthias Ringwald event[pos++] = cig->cig_id; 9262d3a89d91SMatthias Ringwald event[pos++] = cig->num_cis; 9263d3a89d91SMatthias Ringwald uint8_t i; 9264d3a89d91SMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 9265d3a89d91SMatthias Ringwald little_endian_store_16(event, pos, cig->cis_con_handles[i]); 9266d3a89d91SMatthias Ringwald pos += 2; 9267d3a89d91SMatthias Ringwald } 9268d3a89d91SMatthias Ringwald hci_emit_event(event, pos, 0); 9269d3a89d91SMatthias Ringwald } 9270f06bb90fSMatthias Ringwald 92713abcd16cSMatthias Ringwald static void 92723abcd16cSMatthias Ringwald hci_emit_cis_created(uint8_t cig_id, hci_con_handle_t cis_con_handle, uint8_t status) { 9273f06bb90fSMatthias Ringwald uint8_t event [7]; 9274f06bb90fSMatthias Ringwald uint16_t pos = 0; 9275f06bb90fSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 9276f06bb90fSMatthias Ringwald event[pos++] = 5; 9277f06bb90fSMatthias Ringwald event[pos++] = GAP_SUBEVENT_CIS_CREATED; 9278f06bb90fSMatthias Ringwald event[pos++] = status; 92793abcd16cSMatthias Ringwald event[pos++] = cig_id; 92803abcd16cSMatthias Ringwald little_endian_store_16(event, pos, cis_con_handle); 9281f06bb90fSMatthias Ringwald pos += 2; 9282f06bb90fSMatthias Ringwald hci_emit_event(event, pos, 0); 9283f06bb90fSMatthias Ringwald } 9284f06bb90fSMatthias Ringwald 92857aa35f6aSMatthias Ringwald static void hci_emit_big_terminated(const le_audio_big_t * big){ 92867aa35f6aSMatthias Ringwald uint8_t event [4]; 92877aa35f6aSMatthias Ringwald uint16_t pos = 0; 92887aa35f6aSMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 92897aa35f6aSMatthias Ringwald event[pos++] = 2; 92907aa35f6aSMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_TERMINATED; 92917aa35f6aSMatthias Ringwald event[pos++] = big->big_handle; 92927aa35f6aSMatthias Ringwald hci_emit_event(event, pos, 0); 92937aa35f6aSMatthias Ringwald } 92947aa35f6aSMatthias Ringwald 92952a6c0f82SMatthias Ringwald static void hci_emit_big_sync_created(const le_audio_big_sync_t * big_sync, uint8_t status){ 92962a6c0f82SMatthias Ringwald uint8_t event [6 + (MAX_NR_BIS * 2)]; 92972a6c0f82SMatthias Ringwald uint16_t pos = 0; 92982a6c0f82SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 92992a6c0f82SMatthias Ringwald event[pos++] = 4; 93002a6c0f82SMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_SYNC_CREATED; 93012a6c0f82SMatthias Ringwald event[pos++] = status; 93022a6c0f82SMatthias Ringwald event[pos++] = big_sync->big_handle; 93032a6c0f82SMatthias Ringwald event[pos++] = big_sync->num_bis; 93042a6c0f82SMatthias Ringwald uint8_t i; 93052a6c0f82SMatthias Ringwald for (i=0;i<big_sync->num_bis;i++){ 93062a6c0f82SMatthias Ringwald little_endian_store_16(event, pos, big_sync->bis_con_handles[i]); 93072a6c0f82SMatthias Ringwald pos += 2; 93082a6c0f82SMatthias Ringwald } 93092a6c0f82SMatthias Ringwald hci_emit_event(event, pos, 0); 93102a6c0f82SMatthias Ringwald } 93112a6c0f82SMatthias Ringwald 9312d04a9afcSMatthias Ringwald static void hci_emit_big_sync_stopped(uint8_t big_handle){ 93132a6c0f82SMatthias Ringwald uint8_t event [4]; 93142a6c0f82SMatthias Ringwald uint16_t pos = 0; 93152a6c0f82SMatthias Ringwald event[pos++] = HCI_EVENT_META_GAP; 93162a6c0f82SMatthias Ringwald event[pos++] = 2; 93172a6c0f82SMatthias Ringwald event[pos++] = GAP_SUBEVENT_BIG_SYNC_STOPPED; 9318d04a9afcSMatthias Ringwald event[pos++] = big_handle; 93192a6c0f82SMatthias Ringwald hci_emit_event(event, pos, 0); 93202a6c0f82SMatthias Ringwald } 93212a6c0f82SMatthias Ringwald 93225ade6657SMatthias Ringwald static void hci_emit_bis_can_send_now(const le_audio_big_t *big, uint8_t bis_index) { 93235ade6657SMatthias Ringwald uint8_t event[6]; 93245ade6657SMatthias Ringwald uint16_t pos = 0; 93255ade6657SMatthias Ringwald event[pos++] = HCI_EVENT_BIS_CAN_SEND_NOW; 93265ade6657SMatthias Ringwald event[pos++] = sizeof(event) - 2; 93275ade6657SMatthias Ringwald event[pos++] = big->big_handle; 93285ade6657SMatthias Ringwald event[pos++] = bis_index; 93295ade6657SMatthias Ringwald little_endian_store_16(event, pos, big->bis_con_handles[bis_index]); 93305ade6657SMatthias Ringwald hci_emit_event(&event[0], sizeof(event), 0); // don't dump 93315ade6657SMatthias Ringwald } 93325ade6657SMatthias Ringwald 93337aa35f6aSMatthias Ringwald static le_audio_big_t * hci_big_for_handle(uint8_t big_handle){ 93347aa35f6aSMatthias Ringwald btstack_linked_list_iterator_t it; 93357aa35f6aSMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 93367aa35f6aSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 93377aa35f6aSMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 93387aa35f6aSMatthias Ringwald if ( big->big_handle == big_handle ) { 93397aa35f6aSMatthias Ringwald return big; 93407aa35f6aSMatthias Ringwald } 93417aa35f6aSMatthias Ringwald } 93427aa35f6aSMatthias Ringwald return NULL; 93437aa35f6aSMatthias Ringwald } 93447aa35f6aSMatthias Ringwald 93452a6c0f82SMatthias Ringwald static le_audio_big_sync_t * hci_big_sync_for_handle(uint8_t big_handle){ 93462a6c0f82SMatthias Ringwald btstack_linked_list_iterator_t it; 93472a6c0f82SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_big_syncs); 93482a6c0f82SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 93492a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = (le_audio_big_sync_t *) btstack_linked_list_iterator_next(&it); 93502a6c0f82SMatthias Ringwald if ( big_sync->big_handle == big_handle ) { 93512a6c0f82SMatthias Ringwald return big_sync; 93522a6c0f82SMatthias Ringwald } 93532a6c0f82SMatthias Ringwald } 93542a6c0f82SMatthias Ringwald return NULL; 93552a6c0f82SMatthias Ringwald } 93562a6c0f82SMatthias Ringwald 935701d4f05fSMatthias Ringwald void hci_set_num_iso_packets_to_queue(uint8_t num_packets){ 935801d4f05fSMatthias Ringwald hci_stack->iso_packets_to_queue = num_packets; 935901d4f05fSMatthias Ringwald } 936001d4f05fSMatthias Ringwald 9361d3a89d91SMatthias Ringwald static le_audio_cig_t * hci_cig_for_id(uint8_t cig_id){ 9362d3a89d91SMatthias Ringwald btstack_linked_list_iterator_t it; 9363d3a89d91SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_cigs); 9364d3a89d91SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9365d3a89d91SMatthias Ringwald le_audio_cig_t * cig = (le_audio_cig_t *) btstack_linked_list_iterator_next(&it); 9366d3a89d91SMatthias Ringwald if ( cig->cig_id == cig_id ) { 9367d3a89d91SMatthias Ringwald return cig; 9368d3a89d91SMatthias Ringwald } 9369d3a89d91SMatthias Ringwald } 9370d3a89d91SMatthias Ringwald return NULL; 9371d3a89d91SMatthias Ringwald } 9372d3a89d91SMatthias Ringwald 93735ade6657SMatthias Ringwald static void hci_iso_notify_can_send_now(void){ 93745ade6657SMatthias Ringwald btstack_linked_list_iterator_t it; 93755ade6657SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 93765ade6657SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 93775ade6657SMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 9378aacb1081SMatthias Ringwald // track number completed packet timestamps 9379aacb1081SMatthias Ringwald if (big->num_completed_timestamp_current_valid){ 9380aacb1081SMatthias Ringwald big->num_completed_timestamp_current_valid = false; 9381aacb1081SMatthias Ringwald if (big->num_completed_timestamp_previous_valid){ 9382aacb1081SMatthias Ringwald // detect delayed sending of all BIS: tolerate up to 50% delayed event handling 9383aacb1081SMatthias Ringwald uint32_t iso_interval_missed_threshold_ms = big->params->sdu_interval_us * 3 / 2000; 9384aacb1081SMatthias Ringwald int32_t num_completed_timestamp_delta_ms = btstack_time_delta(big->num_completed_timestamp_current_ms, 9385aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_ms); 9386aacb1081SMatthias Ringwald if (num_completed_timestamp_delta_ms > iso_interval_missed_threshold_ms){ 9387aacb1081SMatthias Ringwald // to catch up, skip packet on all BIS 9388aacb1081SMatthias Ringwald uint8_t i; 9389aacb1081SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 9390aacb1081SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 9391aacb1081SMatthias Ringwald if (iso_stream){ 9392aacb1081SMatthias Ringwald iso_stream->num_packets_to_skip++; 9393aacb1081SMatthias Ringwald } 9394aacb1081SMatthias Ringwald } 9395aacb1081SMatthias Ringwald } 9396aacb1081SMatthias Ringwald } 9397aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_valid = true; 9398aacb1081SMatthias Ringwald big->num_completed_timestamp_previous_ms = big->num_completed_timestamp_current_ms; 9399aacb1081SMatthias Ringwald } 9400aacb1081SMatthias Ringwald 94015ade6657SMatthias Ringwald if (big->can_send_now_requested){ 9402d4e5d4faSMatthias Ringwald // check if no outgoing iso packets pending and no can send now have to be emitted 94035ade6657SMatthias Ringwald uint8_t i; 94045ade6657SMatthias Ringwald bool can_send = true; 9405aacb1081SMatthias Ringwald uint8_t num_iso_queued_minimum = 0; 94065ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 94075ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 9408aacb1081SMatthias Ringwald if (iso_stream == NULL) continue; 9409aacb1081SMatthias Ringwald // handle case where individual ISO packet was sent too late: 9410aacb1081SMatthias Ringwald // for each additionally queued packet, a new one needs to get skipped 9411aacb1081SMatthias Ringwald if (i==0){ 9412aacb1081SMatthias Ringwald num_iso_queued_minimum = iso_stream->num_packets_sent; 9413aacb1081SMatthias Ringwald } else if (iso_stream->num_packets_sent > num_iso_queued_minimum){ 9414aacb1081SMatthias Ringwald uint8_t num_packets_to_skip = iso_stream->num_packets_sent - num_iso_queued_minimum; 9415aacb1081SMatthias Ringwald iso_stream->num_packets_to_skip += num_packets_to_skip; 9416aacb1081SMatthias Ringwald iso_stream->num_packets_sent -= num_packets_to_skip; 9417aacb1081SMatthias Ringwald } 9418aacb1081SMatthias Ringwald // check if we can send now 9419aacb1081SMatthias Ringwald if ((iso_stream->num_packets_sent >= hci_stack->iso_packets_to_queue) || (iso_stream->emit_ready_to_send)){ 94205ade6657SMatthias Ringwald can_send = false; 94215ade6657SMatthias Ringwald break; 94225ade6657SMatthias Ringwald } 94235ade6657SMatthias Ringwald } 94245ade6657SMatthias Ringwald if (can_send){ 94255ade6657SMatthias Ringwald // propagate can send now to individual streams 94265ade6657SMatthias Ringwald big->can_send_now_requested = false; 94275ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 94285ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 94295ade6657SMatthias Ringwald iso_stream->emit_ready_to_send = true; 94305ade6657SMatthias Ringwald } 94315ade6657SMatthias Ringwald } 94325ade6657SMatthias Ringwald } 94335ade6657SMatthias Ringwald } 94345ade6657SMatthias Ringwald 94355ade6657SMatthias Ringwald if (hci_stack->hci_packet_buffer_reserved) return; 94365ade6657SMatthias Ringwald 94375ade6657SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->le_audio_bigs); 94385ade6657SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 94395ade6657SMatthias Ringwald le_audio_big_t * big = (le_audio_big_t *) btstack_linked_list_iterator_next(&it); 94405ade6657SMatthias Ringwald // report bis ready 94415ade6657SMatthias Ringwald uint8_t i; 94425ade6657SMatthias Ringwald for (i=0;i<big->num_bis;i++){ 94435ade6657SMatthias Ringwald hci_iso_stream_t * iso_stream = hci_iso_stream_for_con_handle(big->bis_con_handles[i]); 94445ade6657SMatthias Ringwald if ((iso_stream != NULL) && iso_stream->emit_ready_to_send){ 94455ade6657SMatthias Ringwald iso_stream->emit_ready_to_send = false; 94465ade6657SMatthias Ringwald hci_emit_bis_can_send_now(big, i); 94475ade6657SMatthias Ringwald break; 94485ade6657SMatthias Ringwald } 94495ade6657SMatthias Ringwald } 94505ade6657SMatthias Ringwald } 94515ade6657SMatthias Ringwald } 94525ade6657SMatthias Ringwald 94537aa35f6aSMatthias Ringwald uint8_t gap_big_create(le_audio_big_t * storage, le_audio_big_params_t * big_params){ 94547aa35f6aSMatthias Ringwald if (hci_big_for_handle(big_params->big_handle) != NULL){ 94557aa35f6aSMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 94567aa35f6aSMatthias Ringwald } 94577aa35f6aSMatthias Ringwald if (big_params->num_bis == 0){ 94587aa35f6aSMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 94597aa35f6aSMatthias Ringwald } 94607aa35f6aSMatthias Ringwald if (big_params->num_bis > MAX_NR_BIS){ 94617aa35f6aSMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 94627aa35f6aSMatthias Ringwald } 94637aa35f6aSMatthias Ringwald 9464fb16346aSMatthias Ringwald // reserve ISO Streams 9465fb16346aSMatthias Ringwald uint8_t i; 9466fb16346aSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 9467fb16346aSMatthias Ringwald for (i=0;i<big_params->num_bis;i++){ 94688d72db10SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_BIS, HCI_CON_HANDLE_INVALID, big_params->big_handle); 9469fb16346aSMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 9470fb16346aSMatthias Ringwald break; 9471fb16346aSMatthias Ringwald } 9472fb16346aSMatthias Ringwald } 9473fb16346aSMatthias Ringwald 9474fb16346aSMatthias Ringwald // free structs on error 9475fb16346aSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 9476778463d0SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_BIS, big_params->big_handle); 9477fb16346aSMatthias Ringwald return status; 9478fb16346aSMatthias Ringwald } 9479fb16346aSMatthias Ringwald 94807aa35f6aSMatthias Ringwald le_audio_big_t * big = storage; 94817aa35f6aSMatthias Ringwald big->big_handle = big_params->big_handle; 94827aa35f6aSMatthias Ringwald big->params = big_params; 94837aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_CREATE; 94847aa35f6aSMatthias Ringwald big->num_bis = big_params->num_bis; 94857aa35f6aSMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 94867aa35f6aSMatthias Ringwald 94877aa35f6aSMatthias Ringwald hci_run(); 94887aa35f6aSMatthias Ringwald 94897aa35f6aSMatthias Ringwald return ERROR_CODE_SUCCESS; 94907aa35f6aSMatthias Ringwald } 94917aa35f6aSMatthias Ringwald 94922a6c0f82SMatthias Ringwald uint8_t gap_big_sync_create(le_audio_big_sync_t * storage, le_audio_big_sync_params_t * big_sync_params){ 94932a6c0f82SMatthias Ringwald if (hci_big_sync_for_handle(big_sync_params->big_handle) != NULL){ 94942a6c0f82SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 94952a6c0f82SMatthias Ringwald } 94962a6c0f82SMatthias Ringwald if (big_sync_params->num_bis == 0){ 94972a6c0f82SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 94982a6c0f82SMatthias Ringwald } 94992a6c0f82SMatthias Ringwald if (big_sync_params->num_bis > MAX_NR_BIS){ 95002a6c0f82SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 95012a6c0f82SMatthias Ringwald } 95022a6c0f82SMatthias Ringwald 95032a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = storage; 95042a6c0f82SMatthias Ringwald big_sync->big_handle = big_sync_params->big_handle; 95052a6c0f82SMatthias Ringwald big_sync->params = big_sync_params; 95062a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_CREATE; 95072a6c0f82SMatthias Ringwald big_sync->num_bis = big_sync_params->num_bis; 95082a6c0f82SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 95092a6c0f82SMatthias Ringwald 95102a6c0f82SMatthias Ringwald hci_run(); 95112a6c0f82SMatthias Ringwald 95122a6c0f82SMatthias Ringwald return ERROR_CODE_SUCCESS; 95132a6c0f82SMatthias Ringwald } 95142a6c0f82SMatthias Ringwald 95157aa35f6aSMatthias Ringwald uint8_t gap_big_terminate(uint8_t big_handle){ 95167aa35f6aSMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(big_handle); 95177aa35f6aSMatthias Ringwald if (big == NULL){ 95187aa35f6aSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 95197aa35f6aSMatthias Ringwald } 95207aa35f6aSMatthias Ringwald switch (big->state){ 95217aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 95227aa35f6aSMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_bigs, (btstack_linked_item_t *) big); 95232a6c0f82SMatthias Ringwald hci_emit_big_terminated(big); 95242a6c0f82SMatthias Ringwald break; 95252a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH: 95262a6c0f82SMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH_THEN_TERMINATE; 95277aa35f6aSMatthias Ringwald break; 95287aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_ESTABLISHED: 95297aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 95307aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_ACTIVE: 95317aa35f6aSMatthias Ringwald big->state = LE_AUDIO_BIG_STATE_TERMINATE; 95327aa35f6aSMatthias Ringwald hci_run(); 95337aa35f6aSMatthias Ringwald break; 95342a6c0f82SMatthias Ringwald default: 95352a6c0f82SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 95362a6c0f82SMatthias Ringwald } 95372a6c0f82SMatthias Ringwald return ERROR_CODE_SUCCESS; 95382a6c0f82SMatthias Ringwald } 95392a6c0f82SMatthias Ringwald 95402a6c0f82SMatthias Ringwald uint8_t gap_big_sync_terminate(uint8_t big_handle){ 95412a6c0f82SMatthias Ringwald le_audio_big_sync_t * big_sync = hci_big_sync_for_handle(big_handle); 95422a6c0f82SMatthias Ringwald if (big_sync == NULL){ 95432a6c0f82SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 95442a6c0f82SMatthias Ringwald } 95452a6c0f82SMatthias Ringwald switch (big_sync->state){ 95462a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_CREATE: 95472a6c0f82SMatthias Ringwald btstack_linked_list_remove(&hci_stack->le_audio_big_syncs, (btstack_linked_item_t *) big_sync); 9548d04a9afcSMatthias Ringwald hci_emit_big_sync_stopped(big_handle); 95492a6c0f82SMatthias Ringwald break; 95507aa35f6aSMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH: 95512a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_W4_SETUP_ISO_PATH_THEN_TERMINATE; 95522a6c0f82SMatthias Ringwald break; 95532a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_W4_ESTABLISHED: 95542a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_SETUP_ISO_PATH: 95552a6c0f82SMatthias Ringwald case LE_AUDIO_BIG_STATE_ACTIVE: 95562a6c0f82SMatthias Ringwald big_sync->state = LE_AUDIO_BIG_STATE_TERMINATE; 95572a6c0f82SMatthias Ringwald hci_run(); 95587aa35f6aSMatthias Ringwald break; 95597aa35f6aSMatthias Ringwald default: 95607aa35f6aSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 95617aa35f6aSMatthias Ringwald } 95627aa35f6aSMatthias Ringwald return ERROR_CODE_SUCCESS; 95637aa35f6aSMatthias Ringwald } 9564d39e4f95SMatthias Ringwald 95655ade6657SMatthias Ringwald uint8_t hci_request_bis_can_send_now_events(uint8_t big_handle){ 95665ade6657SMatthias Ringwald le_audio_big_t * big = hci_big_for_handle(big_handle); 95675ade6657SMatthias Ringwald if (big == NULL){ 95685ade6657SMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 95695ade6657SMatthias Ringwald } 95705ade6657SMatthias Ringwald if (big->state != LE_AUDIO_BIG_STATE_ACTIVE){ 95715ade6657SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 95725ade6657SMatthias Ringwald } 95735ade6657SMatthias Ringwald big->can_send_now_requested = true; 95745ade6657SMatthias Ringwald hci_iso_notify_can_send_now(); 95755ade6657SMatthias Ringwald return ERROR_CODE_SUCCESS; 95765ade6657SMatthias Ringwald } 9577d3a89d91SMatthias Ringwald 9578d3a89d91SMatthias Ringwald uint8_t gap_cig_create(le_audio_cig_t * storage, le_audio_cig_params_t * cig_params){ 9579d3a89d91SMatthias Ringwald if (hci_cig_for_id(cig_params->cig_id) != NULL){ 9580d3a89d91SMatthias Ringwald return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS; 9581d3a89d91SMatthias Ringwald } 9582d3a89d91SMatthias Ringwald if (cig_params->num_cis == 0){ 9583d3a89d91SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 9584d3a89d91SMatthias Ringwald } 9585d3a89d91SMatthias Ringwald if (cig_params->num_cis > MAX_NR_BIS){ 9586d3a89d91SMatthias Ringwald return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS; 9587d3a89d91SMatthias Ringwald } 9588d3a89d91SMatthias Ringwald 9589d3a89d91SMatthias Ringwald // reserve ISO Streams 9590d3a89d91SMatthias Ringwald uint8_t i; 9591d3a89d91SMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 9592d3a89d91SMatthias Ringwald for (i=0;i<cig_params->num_cis;i++){ 9593d3a89d91SMatthias Ringwald status = hci_iso_stream_create(HCI_ISO_TYPE_CIS, HCI_CON_HANDLE_INVALID, cig_params->cig_id); 9594d3a89d91SMatthias Ringwald if (status != ERROR_CODE_SUCCESS) { 9595d3a89d91SMatthias Ringwald break; 9596d3a89d91SMatthias Ringwald } 9597d3a89d91SMatthias Ringwald } 9598d3a89d91SMatthias Ringwald 9599d3a89d91SMatthias Ringwald // free structs on error 9600d3a89d91SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 9601d3a89d91SMatthias Ringwald hci_iso_stream_finalize_by_type_and_group_id(HCI_ISO_TYPE_CIS, cig_params->cig_id); 9602d3a89d91SMatthias Ringwald return status; 9603d3a89d91SMatthias Ringwald } 9604d3a89d91SMatthias Ringwald 9605d3a89d91SMatthias Ringwald le_audio_cig_t * cig = storage; 9606d3a89d91SMatthias Ringwald cig->cig_id = cig_params->cig_id; 9607d3a89d91SMatthias Ringwald cig->num_cis = cig_params->num_cis; 9608d3a89d91SMatthias Ringwald cig->params = cig_params; 9609d3a89d91SMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_CREATE; 9610444e371eSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 9611444e371eSMatthias Ringwald cig->cis_con_handles[i] = HCI_CON_HANDLE_INVALID; 9612444e371eSMatthias Ringwald cig->acl_con_handles[i] = HCI_CON_HANDLE_INVALID; 9613444e371eSMatthias Ringwald } 9614d3a89d91SMatthias Ringwald btstack_linked_list_add(&hci_stack->le_audio_cigs, (btstack_linked_item_t *) cig); 9615d3a89d91SMatthias Ringwald 9616d3a89d91SMatthias Ringwald hci_run(); 9617d3a89d91SMatthias Ringwald 9618d3a89d91SMatthias Ringwald return ERROR_CODE_SUCCESS; 9619d3a89d91SMatthias Ringwald } 9620d3a89d91SMatthias Ringwald 9621444e371eSMatthias Ringwald uint8_t gap_cis_create(uint8_t cig_handle, hci_con_handle_t cis_con_handles [], hci_con_handle_t acl_con_handles []){ 9622444e371eSMatthias Ringwald le_audio_cig_t * cig = hci_cig_for_id(cig_handle); 9623444e371eSMatthias Ringwald if (cig == NULL){ 9624444e371eSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9625444e371eSMatthias Ringwald } 9626444e371eSMatthias Ringwald 9627444e371eSMatthias Ringwald if (cig->state != LE_AUDIO_CIG_STATE_W4_CIS_REQUEST){ 9628444e371eSMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 9629444e371eSMatthias Ringwald } 9630444e371eSMatthias Ringwald 9631444e371eSMatthias Ringwald // store ACL Connection Handles 9632444e371eSMatthias Ringwald uint8_t i; 9633444e371eSMatthias Ringwald for (i=0;i<cig->num_cis;i++){ 9634444e371eSMatthias Ringwald // check that all con handles exit 9635444e371eSMatthias Ringwald hci_con_handle_t cis_handle = cis_con_handles[i]; 9636444e371eSMatthias Ringwald uint8_t j; 9637444e371eSMatthias Ringwald bool found = false; 9638444e371eSMatthias Ringwald for (j=0;j<cig->num_cis;j++){ 9639444e371eSMatthias Ringwald if (cig->cis_con_handles[j] == cis_handle){ 9640444e371eSMatthias Ringwald cig->acl_con_handles[j] = acl_con_handles[j]; 9641444e371eSMatthias Ringwald found = true; 9642444e371eSMatthias Ringwald break; 9643444e371eSMatthias Ringwald } 9644444e371eSMatthias Ringwald } 9645444e371eSMatthias Ringwald if (!found){ 9646444e371eSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 9647444e371eSMatthias Ringwald } 9648444e371eSMatthias Ringwald } 9649444e371eSMatthias Ringwald 9650444e371eSMatthias Ringwald cig->state = LE_AUDIO_CIG_STATE_CREATE_CIS; 9651444e371eSMatthias Ringwald hci_run(); 9652444e371eSMatthias Ringwald 9653444e371eSMatthias Ringwald return ERROR_CODE_SUCCESS; 9654444e371eSMatthias Ringwald } 9655444e371eSMatthias Ringwald 9656f40c73b4SMatthias Ringwald #endif 96574ae8623eSMatthias Ringwald #endif /* ENABLE_BLE */ 9658f40c73b4SMatthias Ringwald 9659eddac615SMatthias Ringwald #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION 966018976c74SMatthias Ringwald void hci_setup_test_connections_fuzz(void){ 966118976c74SMatthias Ringwald hci_connection_t * conn; 966218976c74SMatthias Ringwald 966318976c74SMatthias Ringwald // default address: 66:55:44:33:00:01 966418976c74SMatthias Ringwald bd_addr_t addr = { 0x66, 0x55, 0x44, 0x33, 0x00, 0x00}; 966518976c74SMatthias Ringwald 96667d33cb26SMilanka Ringwald // setup Controller info 96677d33cb26SMilanka Ringwald hci_stack->num_cmd_packets = 255; 96687d33cb26SMilanka Ringwald hci_stack->acl_packets_total_num = 255; 96697d33cb26SMilanka Ringwald 967018976c74SMatthias Ringwald // setup incoming Classic ACL connection with con handle 0x0001, 66:55:44:33:22:01 967118976c74SMatthias Ringwald addr[5] = 0x01; 967218976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 967318976c74SMatthias Ringwald conn->con_handle = addr[5]; 967418976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 967518976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 96767d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 967718976c74SMatthias Ringwald 967818976c74SMatthias Ringwald // setup incoming Classic SCO connection with con handle 0x0002 967918976c74SMatthias Ringwald addr[5] = 0x02; 968018976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 968118976c74SMatthias Ringwald conn->con_handle = addr[5]; 968218976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 968318976c74SMatthias Ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 96847d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 968518976c74SMatthias Ringwald 968618976c74SMatthias Ringwald // setup ready Classic ACL connection with con handle 0x0003 968718976c74SMatthias Ringwald addr[5] = 0x03; 968818976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL); 968918976c74SMatthias Ringwald conn->con_handle = addr[5]; 969018976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 969118976c74SMatthias Ringwald conn->state = OPEN; 96927d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 969318976c74SMatthias Ringwald 969418976c74SMatthias Ringwald // setup ready Classic SCO connection with con handle 0x0004 969518976c74SMatthias Ringwald addr[5] = 0x04; 969618976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_SCO); 969718976c74SMatthias Ringwald conn->con_handle = addr[5]; 969818976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 969918976c74SMatthias Ringwald conn->state = OPEN; 97007d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 970118976c74SMatthias Ringwald 970218976c74SMatthias Ringwald // setup ready LE ACL connection with con handle 0x005 and public address 970318976c74SMatthias Ringwald addr[5] = 0x05; 970418976c74SMatthias Ringwald conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_LE_PUBLIC); 970518976c74SMatthias Ringwald conn->con_handle = addr[5]; 970618976c74SMatthias Ringwald conn->role = HCI_ROLE_SLAVE; 970718976c74SMatthias Ringwald conn->state = OPEN; 97087d33cb26SMilanka Ringwald conn->sm_connection.sm_role = HCI_ROLE_SLAVE; 97098046a24aSMatthias Ringwald conn->sm_connection.sm_connection_encrypted = 1; 971018976c74SMatthias Ringwald } 971118976c74SMatthias Ringwald 9712eddac615SMatthias Ringwald void hci_free_connections_fuzz(void){ 9713eddac615SMatthias Ringwald btstack_linked_list_iterator_t it; 9714eddac615SMatthias Ringwald btstack_linked_list_iterator_init(&it, &hci_stack->connections); 9715eddac615SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9716eddac615SMatthias Ringwald hci_connection_t * con = (hci_connection_t*) btstack_linked_list_iterator_next(&it); 9717eddac615SMatthias Ringwald btstack_linked_list_iterator_remove(&it); 9718eddac615SMatthias Ringwald btstack_memory_hci_connection_free(con); 9719eddac615SMatthias Ringwald } 9720eddac615SMatthias Ringwald } 97211470db0cSMatthias Ringwald void hci_simulate_working_fuzz(void){ 9722b6ffb67fSMatthias Ringwald hci_stack->le_scanning_param_update = false; 97231470db0cSMatthias Ringwald hci_init_done(); 97241470db0cSMatthias Ringwald hci_stack->num_cmd_packets = 255; 97251470db0cSMatthias Ringwald } 9726eddac615SMatthias Ringwald #endif 9727