11f504dbdSmatthias.ringwald /* 26b64433eSmatthias.ringwald * Copyright (C) 2009-2012 by Matthias Ringwald 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 * 201713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD 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 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD 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 * 336b64433eSmatthias.ringwald * Please inquire about commercial licensing options at [email protected] 346b64433eSmatthias.ringwald * 351713bceaSmatthias.ringwald */ 361713bceaSmatthias.ringwald 371713bceaSmatthias.ringwald /* 381f504dbdSmatthias.ringwald * hci.c 391f504dbdSmatthias.ringwald * 401f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 411f504dbdSmatthias.ringwald * 421f504dbdSmatthias.ringwald */ 431f504dbdSmatthias.ringwald 44bde315ceS[email protected] #include "btstack-config.h" 4528171530Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "hci.h" 474c57c146S[email protected] #include "gap.h" 487f2435e6Smatthias.ringwald 4993b8dc03Smatthias.ringwald #include <stdarg.h> 5093b8dc03Smatthias.ringwald #include <string.h> 5156fe0872Smatthias.ringwald #include <stdio.h> 527f2435e6Smatthias.ringwald 53549e6ebeSmatthias.ringwald #ifndef EMBEDDED 54fe475138S[email protected] #ifdef _WIN32 55fe475138S[email protected] #include "Winsock2.h" 56fe475138S[email protected] #else 57549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 58fe475138S[email protected] #endif 5909ba8edeSmatthias.ringwald #include <btstack/version.h> 60549e6ebeSmatthias.ringwald #endif 61549e6ebeSmatthias.ringwald 62a3b02b71Smatthias.ringwald #include "btstack_memory.h" 637f2435e6Smatthias.ringwald #include "debug.h" 64d8905019Smatthias.ringwald #include "hci_dump.h" 6593b8dc03Smatthias.ringwald 66da886c03S[email protected] #include <btstack/linked_list.h> 67ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 681b0e3922Smatthias.ringwald 69169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 70ee091cf1Smatthias.ringwald 71a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 72da5275c5S[email protected] 7328171530Smatthias.ringwald #ifdef USE_BLUETOOL 743d7a3f67S[email protected] #include "../platforms/ios/src/bt_control_iphone.h" 7528171530Smatthias.ringwald #endif 7628171530Smatthias.ringwald 77758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 78a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 7996a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer); 8096a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 817586ee35S[email protected] static int hci_power_control_on(void); 827586ee35S[email protected] static void hci_power_control_off(void); 836155b3d3S[email protected] static void hci_state_reset(); 84758b46ceSmatthias.ringwald 8506b35ec0Smatthias.ringwald // the STACK is here 863a9fb326S[email protected] #ifndef HAVE_MALLOC 873a9fb326S[email protected] static hci_stack_t hci_stack_static; 883a9fb326S[email protected] #endif 893a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 9016833f0aSmatthias.ringwald 9166fb9560S[email protected] // test helper 9266fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 9366fb9560S[email protected] 9496a45072S[email protected] /** 9596a45072S[email protected] * create connection for given address 9696a45072S[email protected] * 9796a45072S[email protected] * @return connection OR NULL, if no memory left 9896a45072S[email protected] */ 9996a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1005127cc62S[email protected] log_info("create_connection_for_addr %s", bd_addr_to_str(addr)); 101bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 10296a45072S[email protected] if (!conn) return NULL; 10396a45072S[email protected] BD_ADDR_COPY(conn->address, addr); 10496a45072S[email protected] conn->address_type = addr_type; 10596a45072S[email protected] conn->con_handle = 0xffff; 10696a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 10796a45072S[email protected] conn->bonding_flags = 0; 10896a45072S[email protected] conn->requested_security_level = LEVEL_0; 10996a45072S[email protected] linked_item_set_user(&conn->timeout.item, conn); 11096a45072S[email protected] conn->timeout.process = hci_connection_timeout_handler; 11196a45072S[email protected] hci_connection_timestamp(conn); 11296a45072S[email protected] conn->acl_recombination_length = 0; 11396a45072S[email protected] conn->acl_recombination_pos = 0; 11496a45072S[email protected] conn->num_acl_packets_sent = 0; 115da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 11696a45072S[email protected] linked_list_add(&hci_stack->connections, (linked_item_t *) conn); 11796a45072S[email protected] return conn; 11896a45072S[email protected] } 11966fb9560S[email protected] 120da886c03S[email protected] 121da886c03S[email protected] /** 122da886c03S[email protected] * get le connection parameter range 123da886c03S[email protected] * 124da886c03S[email protected] * @return le connection parameter range struct 125da886c03S[email protected] */ 126da886c03S[email protected] le_connection_parameter_range_t gap_le_get_connection_parameter_range(){ 127da886c03S[email protected] return hci_stack->le_connection_parameter_range; 128da886c03S[email protected] } 129da886c03S[email protected] 130da886c03S[email protected] /** 131da886c03S[email protected] * set le connection parameter range 132da886c03S[email protected] * 133da886c03S[email protected] */ 134da886c03S[email protected] 135da886c03S[email protected] void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range){ 136da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_interval_min; 137da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_interval_max; 138da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_latency_min; 139da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_latency_max; 140da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_min = range.le_supervision_timeout_min; 141da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_max = range.le_supervision_timeout_max; 142da886c03S[email protected] } 143da886c03S[email protected] 144da886c03S[email protected] /** 145da886c03S[email protected] * get hci connections iterator 146da886c03S[email protected] * 147da886c03S[email protected] * @return hci connections iterator 148da886c03S[email protected] */ 149da886c03S[email protected] 150da886c03S[email protected] void hci_connections_get_iterator(linked_list_iterator_t *it){ 151da886c03S[email protected] linked_list_iterator_init(it, &hci_stack->connections); 152da886c03S[email protected] } 153da886c03S[email protected] 15497addcc5Smatthias.ringwald /** 155ee091cf1Smatthias.ringwald * get connection for a given handle 156ee091cf1Smatthias.ringwald * 157ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 158ee091cf1Smatthias.ringwald */ 1595061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 160da886c03S[email protected] linked_list_iterator_t it; 161da886c03S[email protected] linked_list_iterator_init(&it, &hci_stack->connections); 162da886c03S[email protected] while (linked_list_iterator_has_next(&it)){ 163da886c03S[email protected] hci_connection_t * item = (hci_connection_t *) linked_list_iterator_next(&it); 1643ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 165da886c03S[email protected] return item; 166ee091cf1Smatthias.ringwald } 167ee091cf1Smatthias.ringwald } 168ee091cf1Smatthias.ringwald return NULL; 169ee091cf1Smatthias.ringwald } 170ee091cf1Smatthias.ringwald 17196a45072S[email protected] /** 17296a45072S[email protected] * get connection for given address 17396a45072S[email protected] * 17496a45072S[email protected] * @return connection OR NULL, if not found 17596a45072S[email protected] */ 17696a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){ 177da886c03S[email protected] linked_list_iterator_t it; 178da886c03S[email protected] linked_list_iterator_init(&it, &hci_stack->connections); 179da886c03S[email protected] while (linked_list_iterator_has_next(&it)){ 180da886c03S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it); 18196a45072S[email protected] if (connection->address_type != addr_type) continue; 18296a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 18362bda3fbS[email protected] return connection; 18462bda3fbS[email protected] } 18562bda3fbS[email protected] return NULL; 18662bda3fbS[email protected] } 18762bda3fbS[email protected] 1882b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 18928ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 190c785ef68Smatthias.ringwald #ifdef HAVE_TIME 191ee091cf1Smatthias.ringwald struct timeval tv; 192ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 193c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 194ee091cf1Smatthias.ringwald // connections might be timed out 195ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 196ee091cf1Smatthias.ringwald } 1972b12a0b9Smatthias.ringwald #endif 198e5780900Smatthias.ringwald #ifdef HAVE_TICK 199c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 200c785ef68Smatthias.ringwald // connections might be timed out 201c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 202c785ef68Smatthias.ringwald } 203c785ef68Smatthias.ringwald #endif 204c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 205c785ef68Smatthias.ringwald run_loop_add_timer(timer); 206c785ef68Smatthias.ringwald } 207ee091cf1Smatthias.ringwald 208ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 209c7492964Smatthias.ringwald #ifdef HAVE_TIME 210ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 211c7492964Smatthias.ringwald #endif 212e5780900Smatthias.ringwald #ifdef HAVE_TICK 213c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 214c785ef68Smatthias.ringwald #endif 215ee091cf1Smatthias.ringwald } 216ee091cf1Smatthias.ringwald 21706b35ec0Smatthias.ringwald 21828ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 21928ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 22028ca2b46S[email protected] } 22128ca2b46S[email protected] 22228ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 22328ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 22428ca2b46S[email protected] } 22528ca2b46S[email protected] 22628ca2b46S[email protected] 22743bfb1bdSmatthias.ringwald /** 22880ca58a0Smatthias.ringwald * add authentication flags and reset timer 22996a45072S[email protected] * @note: assumes classic connection 2307fde4af9Smatthias.ringwald */ 2317fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 2327fde4af9Smatthias.ringwald bd_addr_t addr; 2337fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 23496a45072S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 2357fde4af9Smatthias.ringwald if (conn) { 23628ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 23780ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 2387fde4af9Smatthias.ringwald } 2397fde4af9Smatthias.ringwald } 2407fde4af9Smatthias.ringwald 24180ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 2425061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 24380ca58a0Smatthias.ringwald if (!conn) return 0; 2446724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 2456724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 2466724cd9eS[email protected] return 0; 24780ca58a0Smatthias.ringwald } 24880ca58a0Smatthias.ringwald 249c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 2503a9fb326S[email protected] if (hci_stack->remote_device_db) { 2513a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(addr); 252c12e46e7Smatthias.ringwald } 253c12e46e7Smatthias.ringwald } 254c12e46e7Smatthias.ringwald 2550bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){ 2560bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 2570bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 2580bf6344aS[email protected] } 2590bf6344aS[email protected] 2607fde4af9Smatthias.ringwald 2617fde4af9Smatthias.ringwald /** 26243bfb1bdSmatthias.ringwald * count connections 26343bfb1bdSmatthias.ringwald */ 26440d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 26556c253c9Smatthias.ringwald int count = 0; 26643bfb1bdSmatthias.ringwald linked_item_t *it; 2673a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 26843bfb1bdSmatthias.ringwald return count; 26943bfb1bdSmatthias.ringwald } 270c8e4258aSmatthias.ringwald 27197addcc5Smatthias.ringwald /** 272ba681a6cSmatthias.ringwald * Dummy handler called by HCI 27316833f0aSmatthias.ringwald */ 2742718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 27516833f0aSmatthias.ringwald } 27616833f0aSmatthias.ringwald 277998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2785061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 279998906cdSmatthias.ringwald if (!connection) { 2809da54300S[email protected] log_error("hci_number_outgoing_packets: connection for handle %u does not exist!", handle); 281998906cdSmatthias.ringwald return 0; 282998906cdSmatthias.ringwald } 283998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 284998906cdSmatthias.ringwald } 285998906cdSmatthias.ringwald 286e79abdd6S[email protected] uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 287ee303eddS[email protected] 288ee303eddS[email protected] int num_packets_sent_classic = 0; 289ee303eddS[email protected] int num_packets_sent_le = 0; 290ee303eddS[email protected] 291ee303eddS[email protected] bd_addr_type_t address_type = BD_ADDR_TYPE_UNKNOWN; 292ee303eddS[email protected] 293998906cdSmatthias.ringwald linked_item_t *it; 2943a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 295998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 296ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 297ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 298ee303eddS[email protected] } else { 299ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 300ee303eddS[email protected] } 301*ccda6e14S[email protected] // ignore connections that are not open, e.g., in state RECEIVED_DISCONNECTION_COMPLETE 302*ccda6e14S[email protected] if (connection->con_handle == con_handle && connection->state == OPEN){ 303ee303eddS[email protected] address_type = connection->address_type; 304ee303eddS[email protected] } 305ee303eddS[email protected] } 306ee303eddS[email protected] 307ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 308ee303eddS[email protected] int free_slots_le = 0; 309ee303eddS[email protected] 310ee303eddS[email protected] if (free_slots_classic < 0){ 3119da54300S[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); 312998906cdSmatthias.ringwald return 0; 313998906cdSmatthias.ringwald } 314ee303eddS[email protected] 315ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 316ee303eddS[email protected] // if we have LE slots, they are used 317ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 318ee303eddS[email protected] if (free_slots_le < 0){ 3199da54300S[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); 320ee303eddS[email protected] return 0; 321998906cdSmatthias.ringwald } 322ee303eddS[email protected] } else { 323ee303eddS[email protected] // otherwise, classic slots are used for LE, too 324ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 325ee303eddS[email protected] if (free_slots_classic < 0){ 3269da54300S[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); 327ee303eddS[email protected] return 0; 328ee303eddS[email protected] } 329ee303eddS[email protected] } 330ee303eddS[email protected] 331ee303eddS[email protected] switch (address_type){ 332ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3339da54300S[email protected] log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 334ee303eddS[email protected] return 0; 335ee303eddS[email protected] 336ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 337ee303eddS[email protected] return free_slots_classic; 338ee303eddS[email protected] 339ee303eddS[email protected] default: 340cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 341ee303eddS[email protected] return free_slots_le; 342ee303eddS[email protected] } 343cb00d3aaS[email protected] return free_slots_classic; 344cb00d3aaS[email protected] } 345998906cdSmatthias.ringwald } 346998906cdSmatthias.ringwald 347ac928cc2S[email protected] 348ac928cc2S[email protected] // @deprecated 349c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 350c24735b1Smatthias.ringwald switch (packet_type) { 351c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 352ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(0); 353c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 354ac928cc2S[email protected] return hci_can_send_command_packet_now(); 355c24735b1Smatthias.ringwald default: 356c24735b1Smatthias.ringwald return 0; 357c24735b1Smatthias.ringwald } 358c24735b1Smatthias.ringwald } 359c24735b1Smatthias.ringwald 360ac928cc2S[email protected] // @deprecated 3616b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use 3626b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 363ac928cc2S[email protected] 3646b4af23dS[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 365ac928cc2S[email protected] 366ac928cc2S[email protected] switch (packet_type) { 367ac928cc2S[email protected] case HCI_ACL_DATA_PACKET: 368ac928cc2S[email protected] return hci_can_send_acl_packet_now(0); 369ac928cc2S[email protected] case HCI_COMMAND_DATA_PACKET: 370ac928cc2S[email protected] return hci_can_send_command_packet_now(); 371ac928cc2S[email protected] default: 372ac928cc2S[email protected] return 0; 373ac928cc2S[email protected] } 374ac928cc2S[email protected] } 375ac928cc2S[email protected] 376ac928cc2S[email protected] 377ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 378ac928cc2S[email protected] int hci_can_send_command_packet_now(void){ 379ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 380ac928cc2S[email protected] 381ac928cc2S[email protected] // check for async hci transport implementations 382ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 383ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 384ac928cc2S[email protected] return 0; 385ac928cc2S[email protected] } 386ac928cc2S[email protected] } 387ac928cc2S[email protected] 388ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 389ac928cc2S[email protected] } 390ac928cc2S[email protected] 391ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 392ac928cc2S[email protected] // check for async hci transport implementations 393ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 394ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){ 395ac928cc2S[email protected] return 0; 396ac928cc2S[email protected] } 397ac928cc2S[email protected] } 398e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 399ac928cc2S[email protected] } 400ac928cc2S[email protected] 401ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 402ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 403ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 4046b4af23dS[email protected] } 4056b4af23dS[email protected] 406c8b9416aS[email protected] // used for internal checks in l2cap[-le].c 407c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 408c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 409c8b9416aS[email protected] } 410c8b9416aS[email protected] 4116b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 4126b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 4139d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 4149d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 4159d14b626S[email protected] return 0; 4169d14b626S[email protected] } 4176b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 4186b4af23dS[email protected] return 1; 4196b4af23dS[email protected] } 4206b4af23dS[email protected] 42168a0fcf7S[email protected] void hci_release_packet_buffer(void){ 42268a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 42368a0fcf7S[email protected] } 42468a0fcf7S[email protected] 4256b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 4266b4af23dS[email protected] int hci_transport_synchronous(void){ 4276b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 4286b4af23dS[email protected] } 4296b4af23dS[email protected] 4306c26b087S[email protected] uint16_t hci_max_acl_le_data_packet_length(void){ 4316c26b087S[email protected] return hci_stack->le_data_packets_length > 0 ? hci_stack->le_data_packets_length : hci_stack->acl_data_packet_length; 4326c26b087S[email protected] } 4336c26b087S[email protected] 434452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 435452cf3bbS[email protected] 436452cf3bbS[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); 437452cf3bbS[email protected] 438452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 439452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 440452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 441452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 442452cf3bbS[email protected] } 443452cf3bbS[email protected] 444452cf3bbS[email protected] // testing: reduce buffer to minimum 445452cf3bbS[email protected] // max_acl_data_packet_length = 52; 446452cf3bbS[email protected] 447452cf3bbS[email protected] int err; 448452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 449452cf3bbS[email protected] while (1){ 450452cf3bbS[email protected] 451452cf3bbS[email protected] // get current data 452452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 453452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 454452cf3bbS[email protected] int more_fragments = 0; 455452cf3bbS[email protected] 456452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 457452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 458452cf3bbS[email protected] more_fragments = 1; 459452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 460452cf3bbS[email protected] } 461452cf3bbS[email protected] 462452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 463452cf3bbS[email protected] if (acl_header_pos > 0){ 464452cf3bbS[email protected] uint16_t handle_and_flags = READ_BT_16(hci_stack->hci_packet_buffer, 0); 465452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 466452cf3bbS[email protected] bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 467452cf3bbS[email protected] } 468452cf3bbS[email protected] 469452cf3bbS[email protected] // update header len 470452cf3bbS[email protected] bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 471452cf3bbS[email protected] 472452cf3bbS[email protected] // count packet 473452cf3bbS[email protected] connection->num_acl_packets_sent++; 474452cf3bbS[email protected] 475452cf3bbS[email protected] // send packet 476452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 477452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 4785bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 479452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 480452cf3bbS[email protected] 481452cf3bbS[email protected] // done yet? 482452cf3bbS[email protected] if (!more_fragments) break; 483452cf3bbS[email protected] 484452cf3bbS[email protected] // update start of next fragment to send 485452cf3bbS[email protected] hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 486452cf3bbS[email protected] 487452cf3bbS[email protected] // can send more? 488452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 489452cf3bbS[email protected] } 490452cf3bbS[email protected] 491452cf3bbS[email protected] // done 492452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 0; 493452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = 0; 494452cf3bbS[email protected] 495d051460cS[email protected] // release buffer now for synchronous transport 496203bace6S[email protected] if (hci_transport_synchronous()){ 497452cf3bbS[email protected] hci_release_packet_buffer(); 498452cf3bbS[email protected] } 499452cf3bbS[email protected] 500452cf3bbS[email protected] return err; 501452cf3bbS[email protected] } 502452cf3bbS[email protected] 503826f7347S[email protected] // pre: caller has reserved the packet buffer 504826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 5057856c818Smatthias.ringwald 506452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 507452cf3bbS[email protected] 508826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 509826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 510826f7347S[email protected] return 0; 511826f7347S[email protected] } 512826f7347S[email protected] 513d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 514d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 515d713a683S[email protected] 516826f7347S[email protected] // check for free places on Bluetooth module 517d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 518826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 51997b61c7bS[email protected] hci_release_packet_buffer(); 52097b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 52197b61c7bS[email protected] } 5226218e6f1Smatthias.ringwald 5235061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 52497b61c7bS[email protected] if (!connection) { 5255fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 52697b61c7bS[email protected] hci_release_packet_buffer(); 52797b61c7bS[email protected] return 0; 52897b61c7bS[email protected] } 52956cf178bSmatthias.ringwald hci_connection_timestamp(connection); 53056cf178bSmatthias.ringwald 531452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 5327856c818Smatthias.ringwald 533452cf3bbS[email protected] // setup data 534452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 535452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 5366218e6f1Smatthias.ringwald 537452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 538ee091cf1Smatthias.ringwald } 539ee091cf1Smatthias.ringwald 54016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 5417856c818Smatthias.ringwald 542e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 543e76a89eeS[email protected] 5447856c818Smatthias.ringwald // get info 5457856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 5465061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 5477856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 5487856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 5497856c818Smatthias.ringwald 5507856c818Smatthias.ringwald // ignore non-registered handle 5517856c818Smatthias.ringwald if (!conn){ 5529da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 5537856c818Smatthias.ringwald return; 5547856c818Smatthias.ringwald } 5557856c818Smatthias.ringwald 556e76a89eeS[email protected] // assert packet is complete 5579ecc3e17S[email protected] if (acl_length + 4 != size){ 558e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 559e76a89eeS[email protected] return; 560e76a89eeS[email protected] } 561e76a89eeS[email protected] 5627856c818Smatthias.ringwald // update idle timestamp 5637856c818Smatthias.ringwald hci_connection_timestamp(conn); 5647856c818Smatthias.ringwald 5657856c818Smatthias.ringwald // handle different packet types 5667856c818Smatthias.ringwald switch (acl_flags & 0x03) { 5677856c818Smatthias.ringwald 5687856c818Smatthias.ringwald case 0x01: // continuation fragment 5697856c818Smatthias.ringwald 5700ca847afS[email protected] // sanity checks 5717856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 5729da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 5737856c818Smatthias.ringwald return; 5747856c818Smatthias.ringwald } 5750ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 5760ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 5770ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 5780ca847afS[email protected] conn->acl_recombination_pos = 0; 5790ca847afS[email protected] return; 5800ca847afS[email protected] } 5817856c818Smatthias.ringwald 5827856c818Smatthias.ringwald // append fragment payload (header already stored) 583ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 5847856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 5857856c818Smatthias.ringwald 5869da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 587decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 5887856c818Smatthias.ringwald 5897856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 5907856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 5917856c818Smatthias.ringwald 592ec6321eeS[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, &conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 5937856c818Smatthias.ringwald // reset recombination buffer 5947856c818Smatthias.ringwald conn->acl_recombination_length = 0; 5957856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 5967856c818Smatthias.ringwald } 5977856c818Smatthias.ringwald break; 5987856c818Smatthias.ringwald 5997856c818Smatthias.ringwald case 0x02: { // first fragment 6007856c818Smatthias.ringwald 60123a77e1aS[email protected] // sanity check 60223a77e1aS[email protected] if (conn->acl_recombination_pos) { 60323a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 60423a77e1aS[email protected] conn->acl_recombination_pos = 0; 60523a77e1aS[email protected] } 60623a77e1aS[email protected] 6077856c818Smatthias.ringwald // peek into L2CAP packet! 6087856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 6097856c818Smatthias.ringwald 6109da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 611decc01a8Smatthias.ringwald 6127856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 6137856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 6147856c818Smatthias.ringwald 6157856c818Smatthias.ringwald // forward fragment as L2CAP packet 6163a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 6177856c818Smatthias.ringwald 6187856c818Smatthias.ringwald } else { 6190ca847afS[email protected] 6200ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 6210ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 6220ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 6230ca847afS[email protected] return; 6240ca847afS[email protected] } 6250ca847afS[email protected] 6267856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 627ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 6287856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 6297856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 630ec6321eeS[email protected] bt_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 6317856c818Smatthias.ringwald } 6327856c818Smatthias.ringwald break; 6337856c818Smatthias.ringwald 6347856c818Smatthias.ringwald } 6357856c818Smatthias.ringwald default: 6369da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 6377856c818Smatthias.ringwald return; 6387856c818Smatthias.ringwald } 63994ab26f8Smatthias.ringwald 64094ab26f8Smatthias.ringwald // execute main loop 64194ab26f8Smatthias.ringwald hci_run(); 64216833f0aSmatthias.ringwald } 64322909952Smatthias.ringwald 64467a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 6459da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 6463c4d4b90Smatthias.ringwald 647c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 648c785ef68Smatthias.ringwald 6493a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 650a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 6513c4d4b90Smatthias.ringwald 6523c4d4b90Smatthias.ringwald // now it's gone 653c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 654c7e0c5f6Smatthias.ringwald } 655c7e0c5f6Smatthias.ringwald 6560c042179S[email protected] static const uint16_t packet_type_sizes[] = { 6578f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 6588f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 6598f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 6608f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 6618f8108aaSmatthias.ringwald }; 66265389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 66365389bfcS[email protected] 0, // 3 slot packets 66465389bfcS[email protected] 1, // 5 slot packets 66565389bfcS[email protected] 25, // EDR 2 mpbs 66665389bfcS[email protected] 26, // EDR 3 mbps 66765389bfcS[email protected] 39, // 3 slot EDR packts 66865389bfcS[email protected] 40, // 5 slot EDR packet 66965389bfcS[email protected] }; 67065389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 67165389bfcS[email protected] 0x0f00, // 3 slot packets 67265389bfcS[email protected] 0xf000, // 5 slot packets 67365389bfcS[email protected] 0x1102, // EDR 2 mpbs 67465389bfcS[email protected] 0x2204, // EDR 3 mbps 67565389bfcS[email protected] 0x0300, // 3 slot EDR packts 67665389bfcS[email protected] 0x3000, // 5 slot EDR packet 67765389bfcS[email protected] }; 6788f8108aaSmatthias.ringwald 67965389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 68065389bfcS[email protected] // enable packet types based on size 6818f8108aaSmatthias.ringwald uint16_t packet_types = 0; 682f16a69bbS[email protected] unsigned int i; 6838f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 6848f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 6858f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 6868f8108aaSmatthias.ringwald packet_types |= 1 << i; 6878f8108aaSmatthias.ringwald } 6888f8108aaSmatthias.ringwald } 68965389bfcS[email protected] // disable packet types due to missing local supported features 69065389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 69165389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 69265389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 69365389bfcS[email protected] if (feature_set) continue; 69465389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 69565389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 69665389bfcS[email protected] } 6978f8108aaSmatthias.ringwald // flip bits for "may not be used" 6988f8108aaSmatthias.ringwald packet_types ^= 0x3306; 6998f8108aaSmatthias.ringwald return packet_types; 7008f8108aaSmatthias.ringwald } 7018f8108aaSmatthias.ringwald 7028f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 7033a9fb326S[email protected] return hci_stack->packet_types; 7048f8108aaSmatthias.ringwald } 7058f8108aaSmatthias.ringwald 706facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 7077dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 7083a9fb326S[email protected] return hci_stack->hci_packet_buffer; 7097dc17943Smatthias.ringwald } 7107dc17943Smatthias.ringwald 711f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 7123a9fb326S[email protected] return hci_stack->acl_data_packet_length; 7137dc17943Smatthias.ringwald } 7147dc17943Smatthias.ringwald 7156ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 7166ac9a97eS[email protected] // No. 54, byte 6, bit 6 7176ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 7186ac9a97eS[email protected] } 7196ac9a97eS[email protected] 720f5d8d141S[email protected] int hci_ssp_supported(void){ 7216ac9a97eS[email protected] // No. 51, byte 6, bit 3 7223a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 723f5d8d141S[email protected] } 724f5d8d141S[email protected] 725f5d8d141S[email protected] int hci_classic_supported(void){ 7266ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 7273a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 728f5d8d141S[email protected] } 729f5d8d141S[email protected] 730f5d8d141S[email protected] int hci_le_supported(void){ 731f5d8d141S[email protected] #ifdef HAVE_BLE 7326ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 7333a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 734f5d8d141S[email protected] #else 735f5d8d141S[email protected] return 0; 736f5d8d141S[email protected] #endif 737f5d8d141S[email protected] } 738f5d8d141S[email protected] 73969a97523S[email protected] // get addr type and address used in advertisement packets 74018904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 7413a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 7423a9fb326S[email protected] if (hci_stack->adv_addr_type){ 7433a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 74469a97523S[email protected] } else { 7453a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 74669a97523S[email protected] } 74769a97523S[email protected] } 74869a97523S[email protected] 749b2f949feS[email protected] #ifdef HAVE_BLE 750e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){ 751d1dc057bS[email protected] int offset = 3; 752d1dc057bS[email protected] int num_reports = packet[offset]; 753d1dc057bS[email protected] offset += 1; 754d1dc057bS[email protected] 75557c9da5bS[email protected] int i; 7564f4b43f3S[email protected] log_info("HCI: handle adv report with num reports: %d", num_reports); 757507d06b8S[email protected] uint8_t event[2 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 75857c9da5bS[email protected] for (i=0; i<num_reports;i++){ 759210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 7602b552b23S[email protected] uint8_t event_size = 10 + data_length; 761d1dc057bS[email protected] int pos = 0; 7622b552b23S[email protected] event[pos++] = GAP_LE_ADVERTISING_REPORT; 76357c9da5bS[email protected] event[pos++] = event_size; 764210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 765d1dc057bS[email protected] offset += 8; 766d1dc057bS[email protected] pos += 8; 767d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 768d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 769d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 77057c9da5bS[email protected] pos += data_length; 771d1dc057bS[email protected] offset += data_length + 1; // rssi 7724f4b43f3S[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 77357c9da5bS[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 77457c9da5bS[email protected] } 77557c9da5bS[email protected] } 776b2f949feS[email protected] #endif 77757c9da5bS[email protected] 7786155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 779a2481739S[email protected] uint8_t command_completed = 0; 780a2481739S[email protected] if ((hci_stack->substate % 2) == 0) return; 7816155b3d3S[email protected] // odd: waiting for event 7826155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 7836155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,3); 7846155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 785a2481739S[email protected] command_completed = 1; 7866155b3d3S[email protected] log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate); 7876155b3d3S[email protected] } else { 7886155b3d3S[email protected] log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 7896155b3d3S[email protected] } 7906155b3d3S[email protected] } 7916155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_STATUS){ 7926155b3d3S[email protected] uint8_t status = packet[2]; 7936155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,4); 7946155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 7956155b3d3S[email protected] if (status){ 796a2481739S[email protected] command_completed = 1; 7976155b3d3S[email protected] log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate); 7986155b3d3S[email protected] } else { 7996155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 8006155b3d3S[email protected] } 8016155b3d3S[email protected] } else { 8026155b3d3S[email protected] log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 8036155b3d3S[email protected] } 8046155b3d3S[email protected] } 805a2481739S[email protected] 806a2481739S[email protected] if (!command_completed) return; 807a2481739S[email protected] 808a2481739S[email protected] switch(hci_stack->substate >> 1){ 809a2481739S[email protected] default: 810a2481739S[email protected] hci_stack->substate++; 811a2481739S[email protected] break; 8126155b3d3S[email protected] } 8136155b3d3S[email protected] } 8146155b3d3S[email protected] 8156155b3d3S[email protected] static void hci_initializing_state_machine(){ 8169da54300S[email protected] // log_info("hci_init: substate %u", hci_stack->substate); 8176155b3d3S[email protected] if (hci_stack->substate % 2) { 8186155b3d3S[email protected] // odd: waiting for command completion 8196155b3d3S[email protected] return; 8206155b3d3S[email protected] } 8216155b3d3S[email protected] switch (hci_stack->substate >> 1){ 8226155b3d3S[email protected] case 0: // RESET 8236155b3d3S[email protected] hci_state_reset(); 8246155b3d3S[email protected] 8256155b3d3S[email protected] hci_send_cmd(&hci_reset); 8266155b3d3S[email protected] if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 8276155b3d3S[email protected] // skip baud change 8286155b3d3S[email protected] hci_stack->substate = 4; // >> 1 = 2 8296155b3d3S[email protected] } 8306155b3d3S[email protected] break; 8316155b3d3S[email protected] case 1: // SEND BAUD CHANGE 8326155b3d3S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 83388789c61Smatthias.ringwald hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 8345bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 8356155b3d3S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 8366155b3d3S[email protected] break; 8376155b3d3S[email protected] case 2: // LOCAL BAUD CHANGE 8386155b3d3S[email protected] log_info("Local baud rate change"); 8396155b3d3S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 8406155b3d3S[email protected] hci_stack->substate += 2; 8416155b3d3S[email protected] // break missing here for fall through 8426155b3d3S[email protected] 8436155b3d3S[email protected] case 3: 8446155b3d3S[email protected] log_info("Custom init"); 8456155b3d3S[email protected] // Custom initialization 8466155b3d3S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 8476155b3d3S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 8486155b3d3S[email protected] if (valid_cmd){ 8496155b3d3S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 8506155b3d3S[email protected] hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 8515bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 8526155b3d3S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 8536155b3d3S[email protected] hci_stack->substate = 4; // more init commands 8546155b3d3S[email protected] break; 8556155b3d3S[email protected] } 8569da54300S[email protected] log_info("hci_run: init script done"); 8576155b3d3S[email protected] } 8586155b3d3S[email protected] // otherwise continue 8596155b3d3S[email protected] hci_send_cmd(&hci_read_bd_addr); 8606155b3d3S[email protected] break; 8616155b3d3S[email protected] case 4: 8626155b3d3S[email protected] hci_send_cmd(&hci_read_buffer_size); 8636155b3d3S[email protected] break; 8646155b3d3S[email protected] case 5: 8656155b3d3S[email protected] hci_send_cmd(&hci_read_local_supported_features); 8666155b3d3S[email protected] break; 8676155b3d3S[email protected] case 6: 8686155b3d3S[email protected] if (hci_le_supported()){ 8696155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 8706155b3d3S[email protected] } else { 8716155b3d3S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 8726155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 8736155b3d3S[email protected] } 8746155b3d3S[email protected] 8756155b3d3S[email protected] // skip Classic init commands for LE only chipsets 8766155b3d3S[email protected] if (!hci_classic_supported()){ 8776155b3d3S[email protected] if (hci_le_supported()){ 8786155b3d3S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 8796155b3d3S[email protected] } else { 8806155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 8816155b3d3S[email protected] hci_stack->substate = 14 << 1; // skip all 8826155b3d3S[email protected] } 8836155b3d3S[email protected] } 8846155b3d3S[email protected] break; 8856155b3d3S[email protected] case 7: 8866155b3d3S[email protected] if (hci_ssp_supported()){ 8876155b3d3S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 8886155b3d3S[email protected] break; 8896155b3d3S[email protected] } 8906155b3d3S[email protected] hci_stack->substate += 2; 8916155b3d3S[email protected] // break missing here for fall through 8926155b3d3S[email protected] 8936155b3d3S[email protected] case 8: 8946155b3d3S[email protected] // ca. 15 sec 8956155b3d3S[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 8966155b3d3S[email protected] break; 8976155b3d3S[email protected] case 9: 8986155b3d3S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 8996155b3d3S[email protected] break; 9006155b3d3S[email protected] case 10: 9016155b3d3S[email protected] if (hci_stack->local_name){ 9026155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 9036155b3d3S[email protected] } else { 9046155b3d3S[email protected] char hostname[30]; 9056155b3d3S[email protected] #ifdef EMBEDDED 9066155b3d3S[email protected] // BTstack-11:22:33:44:55:66 9076155b3d3S[email protected] strcpy(hostname, "BTstack "); 9086155b3d3S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 9096155b3d3S[email protected] log_info("---> Name %s", hostname); 9106155b3d3S[email protected] #else 9116155b3d3S[email protected] // hostname for POSIX systems 9126155b3d3S[email protected] gethostname(hostname, 30); 9136155b3d3S[email protected] hostname[29] = '\0'; 9146155b3d3S[email protected] #endif 9156155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hostname); 9166155b3d3S[email protected] } 9176155b3d3S[email protected] break; 9186155b3d3S[email protected] case 11: 9196155b3d3S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 9206155b3d3S[email protected] if (!hci_le_supported()){ 9216155b3d3S[email protected] // SKIP LE init for Classic only configuration 9226155b3d3S[email protected] hci_stack->substate = 14 << 1; 9236155b3d3S[email protected] } 9246155b3d3S[email protected] break; 9256155b3d3S[email protected] 9266155b3d3S[email protected] #ifdef HAVE_BLE 9276155b3d3S[email protected] // LE INIT 9286155b3d3S[email protected] case 12: 9296155b3d3S[email protected] hci_send_cmd(&hci_le_read_buffer_size); 9306155b3d3S[email protected] break; 9316155b3d3S[email protected] case 13: 9326155b3d3S[email protected] // LE Supported Host = 1, Simultaneous Host = 0 9336155b3d3S[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 9346155b3d3S[email protected] break; 9356155b3d3S[email protected] case 14: 9366155b3d3S[email protected] // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs 9376155b3d3S[email protected] hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0); 9386155b3d3S[email protected] break; 9396155b3d3S[email protected] #endif 9406155b3d3S[email protected] 9416155b3d3S[email protected] // DONE 9426155b3d3S[email protected] case 15: 9436155b3d3S[email protected] // done. 9446155b3d3S[email protected] hci_stack->state = HCI_STATE_WORKING; 9456155b3d3S[email protected] hci_emit_state(); 9466155b3d3S[email protected] break; 9476155b3d3S[email protected] default: 9486155b3d3S[email protected] break; 9496155b3d3S[email protected] } 9506155b3d3S[email protected] hci_stack->substate++; 9516155b3d3S[email protected] } 9526155b3d3S[email protected] 95374ec757aSmatthias.ringwald // avoid huge local variables 954223aafc1Smatthias.ringwald #ifndef EMBEDDED 95574ec757aSmatthias.ringwald static device_name_t device_name; 956223aafc1Smatthias.ringwald #endif 95716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 958e76a89eeS[email protected] 959e76a89eeS[email protected] uint16_t event_length = packet[1]; 960e76a89eeS[email protected] 961e76a89eeS[email protected] // assert packet is complete 962e76a89eeS[email protected] if (size != event_length + 2){ 963e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 964e76a89eeS[email protected] return; 965e76a89eeS[email protected] } 966e76a89eeS[email protected] 9671281a47eSmatthias.ringwald bd_addr_t addr; 96896a45072S[email protected] bd_addr_type_t addr_type; 9692d00edd4Smatthias.ringwald uint8_t link_type; 970fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 9711f7b95a1Smatthias.ringwald hci_connection_t * conn; 97256cf178bSmatthias.ringwald int i; 97322909952Smatthias.ringwald 9749da54300S[email protected] // log_info("HCI:EVENT:%02x", packet[0]); 9755909f7f2Smatthias.ringwald 9766772a24cSmatthias.ringwald switch (packet[0]) { 97722909952Smatthias.ringwald 9786772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 9797ec5eeaaSmatthias.ringwald // get num cmd packets 9809da54300S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]); 9813a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 9827ec5eeaaSmatthias.ringwald 983e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 984e2edc0c3Smatthias.ringwald // from offset 5 985e2edc0c3Smatthias.ringwald // status 9861d279b20Smatthias.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" 9873a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 98856cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 989ee303eddS[email protected] hci_stack->acl_packets_total_num = packet[9]; 99056cf178bSmatthias.ringwald // ignore: total num SCO packets 9913a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 992a7a04bd9Smatthias.ringwald // determine usable ACL payload size 9933a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 9943a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 9958f8108aaSmatthias.ringwald } 9969da54300S[email protected] log_info("hci_read_buffer_size: used size %u, count %u", 997ee303eddS[email protected] hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num); 998e2edc0c3Smatthias.ringwald } 99956cf178bSmatthias.ringwald } 100065a46ef3S[email protected] #ifdef HAVE_BLE 100165a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 1002219eea5fS[email protected] hci_stack->le_data_packets_length = READ_BT_16(packet, 6); 1003ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 10046c26b087S[email protected] // determine usable ACL payload size 10056c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 10066c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 10076c26b087S[email protected] } 10089da54300S[email protected] log_info("hci_le_read_buffer_size: size %u, count %u", hci_stack->le_data_packets_length, hci_stack->le_acl_packets_total_num); 100965a46ef3S[email protected] } 101065a46ef3S[email protected] #endif 1011188981d2Smatthias.ringwald // Dump local address 1012188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 10133a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 10149da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 10153a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 1016188981d2Smatthias.ringwald } 1017381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 10183a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1019381fbed8Smatthias.ringwald } 102065389bfcS[email protected] // Note: HCI init checks 1021559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 10223a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 1023559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 10243a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 10253a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 10263a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 10273a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 102865389bfcS[email protected] 1029a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1030a5a23fc2S[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]); 10313a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 1032f5d8d141S[email protected] 1033f5d8d141S[email protected] // Classic/LE 1034f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1035559e517eS[email protected] } 103656cf178bSmatthias.ringwald break; 103756cf178bSmatthias.ringwald 10387ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 10397ec5eeaaSmatthias.ringwald // get num cmd packets 10409da54300S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]); 10413a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 10427ec5eeaaSmatthias.ringwald break; 10437ec5eeaaSmatthias.ringwald 10442e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 10452e440c8aS[email protected] int offset = 3; 104656cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 10472e440c8aS[email protected] handle = READ_BT_16(packet, offset); 10482e440c8aS[email protected] offset += 2; 10492e440c8aS[email protected] uint16_t num_packets = READ_BT_16(packet, offset); 10502e440c8aS[email protected] offset += 2; 10512e440c8aS[email protected] 10525061f3afS[email protected] conn = hci_connection_for_handle(handle); 105356cf178bSmatthias.ringwald if (!conn){ 10549da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 105556cf178bSmatthias.ringwald continue; 105656cf178bSmatthias.ringwald } 105723bed257S[email protected] 105823bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 105956cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 106023bed257S[email protected] } else { 106123bed257S[email protected] log_error("hci_number_completed_packets, more slots freed then sent."); 106223bed257S[email protected] conn->num_acl_packets_sent = 0; 106323bed257S[email protected] } 10649da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 106556cf178bSmatthias.ringwald } 10666772a24cSmatthias.ringwald break; 10672e440c8aS[email protected] } 10681f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 106937eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 107037eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 10712d00edd4Smatthias.ringwald link_type = packet[11]; 10729da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 107337eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 107496a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 10751f7b95a1Smatthias.ringwald if (!conn) { 107696a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 10771f7b95a1Smatthias.ringwald } 1078ce4c8fabSmatthias.ringwald if (!conn) { 1079ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 10803a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 10813a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 1082ce4c8fabSmatthias.ringwald break; 1083ce4c8fabSmatthias.ringwald } 108432ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 108532ab9390Smatthias.ringwald hci_run(); 108637eaa4cfSmatthias.ringwald } else { 1087ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 10883a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 10893a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 109037eaa4cfSmatthias.ringwald } 10911f7b95a1Smatthias.ringwald break; 10921f7b95a1Smatthias.ringwald 10936772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1094fe1ed1b8Smatthias.ringwald // Connection management 1095fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 10969da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 109796a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 109896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 1099fe1ed1b8Smatthias.ringwald if (conn) { 1100b448a0e7Smatthias.ringwald if (!packet[2]){ 1101c8e4258aSmatthias.ringwald conn->state = OPEN; 1102fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 1103ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1104ee091cf1Smatthias.ringwald 1105c785ef68Smatthias.ringwald // restart timer 1106c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1107ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 1108c785ef68Smatthias.ringwald 11099da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 111043bfb1bdSmatthias.ringwald 111143bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1112b448a0e7Smatthias.ringwald } else { 11131bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 11141bd5283dS[email protected] uint8_t status = packet[2]; 11151bd5283dS[email protected] bd_addr_t bd_address; 11161bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1117ad83dc6aS[email protected] 1118b448a0e7Smatthias.ringwald // connection failed, remove entry 11193a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 1120a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1121c12e46e7Smatthias.ringwald 11221bd5283dS[email protected] // notify client if dedicated bonding 11231bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 11241bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 11251bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 11261bd5283dS[email protected] } 11271bd5283dS[email protected] 1128c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1129c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 1130c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 1131c12e46e7Smatthias.ringwald } 1132fe1ed1b8Smatthias.ringwald } 1133fe1ed1b8Smatthias.ringwald } 11346772a24cSmatthias.ringwald break; 1135fe1ed1b8Smatthias.ringwald 1136afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1137afd4e962S[email protected] handle = READ_BT_16(packet, 3); 1138afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1139afd4e962S[email protected] if (!conn) break; 1140afd4e962S[email protected] if (!packet[2]){ 1141afd4e962S[email protected] uint8_t * features = &packet[5]; 1142afd4e962S[email protected] if (features[6] & (1 << 3)){ 1143afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1144afd4e962S[email protected] } 1145afd4e962S[email protected] } 1146afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 1147ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 1148ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1149ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1150ad83dc6aS[email protected] } 1151afd4e962S[email protected] break; 1152afd4e962S[email protected] 11537fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 11549da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 11557fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 115674d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 11573a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 115832ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 115964472d52Smatthias.ringwald hci_run(); 1160d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 116129d53098Smatthias.ringwald return; 11627fde4af9Smatthias.ringwald 11639ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 116429d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 116596a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 11669ab95c90S[email protected] if (!conn) break; 11679ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 11687bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 11699ab95c90S[email protected] // Change Connection Encryption keeps link key type 11709ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 11719ab95c90S[email protected] conn->link_key_type = link_key_type; 11729ab95c90S[email protected] } 11733a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 11743a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 117529d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 11767fde4af9Smatthias.ringwald break; 11779ab95c90S[email protected] } 11787fde4af9Smatthias.ringwald 11797fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 11806724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 11814c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 11823a9fb326S[email protected] if (!hci_stack->bondable){ 1183899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1184f8fb5f6eS[email protected] hci_run(); 1185f8fb5f6eS[email protected] return; 11864c57c146S[email protected] } 1187d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 11883a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 1189d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 11903a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 11917fde4af9Smatthias.ringwald break; 11927fde4af9Smatthias.ringwald 11931d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 11941d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1195dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1196dbe1a790S[email protected] break; 1197dbe1a790S[email protected] 1198dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 11996724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 12003a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1201dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1202dbe1a790S[email protected] break; 1203dbe1a790S[email protected] 1204dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 12056724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 12063a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1207dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 12081d6b20aeS[email protected] break; 12091d6b20aeS[email protected] 1210f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1211f0944df2S[email protected] handle = READ_BT_16(packet, 3); 1212f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1213f0944df2S[email protected] if (!conn) break; 1214ad83dc6aS[email protected] if (packet[2] == 0) { 1215f0944df2S[email protected] if (packet[5]){ 1216f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1217f0944df2S[email protected] } else { 1218536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1219f0944df2S[email protected] } 1220ad83dc6aS[email protected] } 1221a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 1222f0944df2S[email protected] break; 1223f0944df2S[email protected] 12241eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 12251eb2563eS[email protected] handle = READ_BT_16(packet, 3); 12261eb2563eS[email protected] conn = hci_connection_for_handle(handle); 12271eb2563eS[email protected] if (!conn) break; 1228ad83dc6aS[email protected] 1229ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1230ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1231ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1232ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 12331bd5283dS[email protected] conn->bonding_status = packet[2]; 1234ad83dc6aS[email protected] break; 1235ad83dc6aS[email protected] } 1236ad83dc6aS[email protected] 1237b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 12381eb2563eS[email protected] // link key sufficient for requested security 12391eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1240ad83dc6aS[email protected] break; 1241ad83dc6aS[email protected] } 12421eb2563eS[email protected] // not enough 12431eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 12441eb2563eS[email protected] break; 124534d2123cS[email protected] 1246223aafc1Smatthias.ringwald #ifndef EMBEDDED 124774ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 12483a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 124974ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 125074ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 12515a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 12525a06394aSmatthias.ringwald for (i=0; i<248;i++){ 12535a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 12545a06394aSmatthias.ringwald packet[9+i] = 0; 12555a06394aSmatthias.ringwald break; 1256cdc9101dSmatthias.ringwald } 12575a06394aSmatthias.ringwald } 1258d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 125974ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 12603a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 126174ec757aSmatthias.ringwald break; 126274ec757aSmatthias.ringwald 126374ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 1264206a9031S[email protected] case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ 12653a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 126674ec757aSmatthias.ringwald // first send inq result packet 12673a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 126874ec757aSmatthias.ringwald // then send cached remote names 1269206a9031S[email protected] int offset = 3; 127074ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 1271206a9031S[email protected] bt_flip_addr(addr, &packet[offset]); 1272206a9031S[email protected] offset += 14; // 6 + 1 + 1 + 1 + 3 + 2; 12733a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 127474ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 127574ec757aSmatthias.ringwald } 127674ec757aSmatthias.ringwald } 127774ec757aSmatthias.ringwald return; 1278206a9031S[email protected] } 1279223aafc1Smatthias.ringwald #endif 128074ec757aSmatthias.ringwald 128186805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1282*ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1283*ccda6e14S[email protected] // see end of function, too. 1284*ccda6e14S[email protected] if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){ 1285*ccda6e14S[email protected] if (!packet[2]){ 1286*ccda6e14S[email protected] handle = READ_BT_16(packet, 3); 1287*ccda6e14S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 1288*ccda6e14S[email protected] if (conn) { 1289*ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 1290*ccda6e14S[email protected] } 1291*ccda6e14S[email protected] } 1292*ccda6e14S[email protected] break; 1293*ccda6e14S[email protected] } 12946772a24cSmatthias.ringwald 1295c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 12963a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 12973a9fb326S[email protected] (*hci_stack->control->hw_error)(); 12987586ee35S[email protected] } else { 12997586ee35S[email protected] // if no special requests, just reboot stack 13007586ee35S[email protected] hci_power_control_off(); 13017586ee35S[email protected] hci_power_control_on(); 1302c68bdf90Smatthias.ringwald } 1303c68bdf90Smatthias.ringwald break; 1304c68bdf90Smatthias.ringwald 13056b4af23dS[email protected] case DAEMON_EVENT_HCI_PACKET_SENT: 1306d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 13074fa24b5fS[email protected] if (hci_transport_synchronous()) { 13084fa24b5fS[email protected] log_error("Synchronous HCI Transport shouldn't send DAEMON_EVENT_HCI_PACKET_SENT"); 13094fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 13104fa24b5fS[email protected] } 1311d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 1312d051460cS[email protected] hci_release_packet_buffer(); 13136b4af23dS[email protected] break; 13146b4af23dS[email protected] 13155909f7f2Smatthias.ringwald #ifdef HAVE_BLE 13165909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 13175909f7f2Smatthias.ringwald switch (packet[2]){ 131857c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 13199da54300S[email protected] log_info("advertising report received"); 13207bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 132157c9da5bS[email protected] le_handle_advertisement_report(packet, size); 13227bdc6798S[email protected] break; 13235909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 13245909f7f2Smatthias.ringwald // Connection management 13255909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 13267bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 13279da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 13285909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 132996a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 13305909f7f2Smatthias.ringwald if (packet[3]){ 13315909f7f2Smatthias.ringwald if (conn){ 13325909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 13333a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 13345909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 13355909f7f2Smatthias.ringwald } 13365909f7f2Smatthias.ringwald // if authentication error, also delete link key 13375909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 13385909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 13395909f7f2Smatthias.ringwald } 13405909f7f2Smatthias.ringwald break; 13415909f7f2Smatthias.ringwald } 13425909f7f2Smatthias.ringwald if (!conn){ 134396a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 13445909f7f2Smatthias.ringwald } 13455909f7f2Smatthias.ringwald if (!conn){ 13465909f7f2Smatthias.ringwald // no memory 13475909f7f2Smatthias.ringwald break; 13485909f7f2Smatthias.ringwald } 13495909f7f2Smatthias.ringwald 13505909f7f2Smatthias.ringwald conn->state = OPEN; 13515909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 13525909f7f2Smatthias.ringwald 13535909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 13545909f7f2Smatthias.ringwald 13555909f7f2Smatthias.ringwald // restart timer 13565909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 13575909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 13585909f7f2Smatthias.ringwald 13599da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 13605909f7f2Smatthias.ringwald 13615909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 13625909f7f2Smatthias.ringwald break; 13635909f7f2Smatthias.ringwald 13649da54300S[email protected] // log_info("LE buffer size: %u, count %u", READ_BT_16(packet,6), packet[8]); 136565a46ef3S[email protected] 13665909f7f2Smatthias.ringwald default: 13675909f7f2Smatthias.ringwald break; 13685909f7f2Smatthias.ringwald } 13695909f7f2Smatthias.ringwald break; 13705909f7f2Smatthias.ringwald #endif 13716772a24cSmatthias.ringwald default: 13726772a24cSmatthias.ringwald break; 1373fe1ed1b8Smatthias.ringwald } 1374fe1ed1b8Smatthias.ringwald 13753429f56bSmatthias.ringwald // handle BT initialization 13763a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 13776155b3d3S[email protected] hci_initializing_event_handler(packet, size); 1378c9af4d3fS[email protected] } 137922909952Smatthias.ringwald 138089db417bSmatthias.ringwald // help with BT sleep 13813a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 13823a9fb326S[email protected] && hci_stack->substate == 1 138389db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 13843a9fb326S[email protected] hci_stack->substate++; 138589db417bSmatthias.ringwald } 138689db417bSmatthias.ringwald 138786805605S[email protected] // notify upper stack 13883a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 138994ab26f8Smatthias.ringwald 139086805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 139186805605S[email protected] if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){ 139286805605S[email protected] if (!packet[2]){ 139386805605S[email protected] handle = READ_BT_16(packet, 3); 139486805605S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 139586805605S[email protected] if (conn) { 139686805605S[email protected] uint8_t status = conn->bonding_status; 1397bb820044S[email protected] uint16_t flags = conn->bonding_flags; 139886805605S[email protected] bd_addr_t bd_address; 139986805605S[email protected] memcpy(&bd_address, conn->address, 6); 140086805605S[email protected] hci_shutdown_connection(conn); 1401bb820044S[email protected] // connection struct is gone, don't access anymore 1402bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 140386805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 140486805605S[email protected] } 140586805605S[email protected] } 140686805605S[email protected] } 140786805605S[email protected] } 140886805605S[email protected] 140994ab26f8Smatthias.ringwald // execute main loop 141094ab26f8Smatthias.ringwald hci_run(); 141116833f0aSmatthias.ringwald } 141216833f0aSmatthias.ringwald 14130a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14145bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 141510e830c9Smatthias.ringwald switch (packet_type) { 141610e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 141710e830c9Smatthias.ringwald event_handler(packet, size); 141810e830c9Smatthias.ringwald break; 141910e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 142010e830c9Smatthias.ringwald acl_handler(packet, size); 142110e830c9Smatthias.ringwald break; 142210e830c9Smatthias.ringwald default: 142310e830c9Smatthias.ringwald break; 142410e830c9Smatthias.ringwald } 142510e830c9Smatthias.ringwald } 142610e830c9Smatthias.ringwald 1427fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 14282718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 14293a9fb326S[email protected] hci_stack->packet_handler = handler; 143016833f0aSmatthias.ringwald } 143116833f0aSmatthias.ringwald 14326155b3d3S[email protected] static void hci_state_reset(){ 1433595bdbfbS[email protected] // no connections yet 1434595bdbfbS[email protected] hci_stack->connections = NULL; 143574308b23Smatthias.ringwald 143674308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 143774308b23Smatthias.ringwald // hci_stack->discoverable = 0; 143874308b23Smatthias.ringwald // hci_stack->connectable = 0; 143974308b23Smatthias.ringwald // hci_stack->bondable = 1; 1440595bdbfbS[email protected] 144144935e40S[email protected] // buffer is free 144244935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 144344935e40S[email protected] 1444595bdbfbS[email protected] // no pending cmds 1445595bdbfbS[email protected] hci_stack->decline_reason = 0; 1446595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 1447595bdbfbS[email protected] 1448595bdbfbS[email protected] // LE 1449595bdbfbS[email protected] hci_stack->adv_addr_type = 0; 1450595bdbfbS[email protected] memset(hci_stack->adv_address, 0, 6); 1451595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1452e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1453da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_min = 0x0006; 1454da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_max = 0x0C80; 1455da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_latency_min = 0x0000; 1456da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_latency_max = 0x03E8; 1457da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 0x000A; 1458da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 0x0C80; 1459595bdbfbS[email protected] } 1460595bdbfbS[email protected] 14614f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 1462475c8125Smatthias.ringwald 14633a9fb326S[email protected] #ifdef HAVE_MALLOC 14643a9fb326S[email protected] if (!hci_stack) { 14653a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 14663a9fb326S[email protected] } 14673a9fb326S[email protected] #else 14683a9fb326S[email protected] hci_stack = &hci_stack_static; 14693a9fb326S[email protected] #endif 147066fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 14713a9fb326S[email protected] 1472475c8125Smatthias.ringwald // reference to use transport layer implementation 14733a9fb326S[email protected] hci_stack->hci_transport = transport; 1474475c8125Smatthias.ringwald 147511e23e5fSmatthias.ringwald // references to used control implementation 14763a9fb326S[email protected] hci_stack->control = control; 147711e23e5fSmatthias.ringwald 147811e23e5fSmatthias.ringwald // reference to used config 14793a9fb326S[email protected] hci_stack->config = config; 148011e23e5fSmatthias.ringwald 148116833f0aSmatthias.ringwald // higher level handler 14823a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 148316833f0aSmatthias.ringwald 1484404843c1Smatthias.ringwald // store and open remote device db 14853a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 14863a9fb326S[email protected] if (hci_stack->remote_device_db) { 14873a9fb326S[email protected] hci_stack->remote_device_db->open(); 1488404843c1Smatthias.ringwald } 148929d53098Smatthias.ringwald 14908fcba05dSmatthias.ringwald // max acl payload size defined in config.h 14913a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 14928fcba05dSmatthias.ringwald 149316833f0aSmatthias.ringwald // register packet handlers with transport 149410e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 1495f5454fc6Smatthias.ringwald 14963a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 1497e2386ba1S[email protected] 1498e2386ba1S[email protected] // class of device 14993a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 1500a45d6b9fS[email protected] 1501f20168b8Smatthias.ringwald // bondable by default 1502f20168b8Smatthias.ringwald hci_stack->bondable = 1; 1503f20168b8Smatthias.ringwald 150463048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 15053a9fb326S[email protected] hci_stack->ssp_enable = 1; 15063a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 15073a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 15083a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 150969a97523S[email protected] 1510595bdbfbS[email protected] hci_state_reset(); 1511475c8125Smatthias.ringwald } 1512475c8125Smatthias.ringwald 1513404843c1Smatthias.ringwald void hci_close(){ 1514404843c1Smatthias.ringwald // close remote device db 15153a9fb326S[email protected] if (hci_stack->remote_device_db) { 15163a9fb326S[email protected] hci_stack->remote_device_db->close(); 1517404843c1Smatthias.ringwald } 15183a9fb326S[email protected] while (hci_stack->connections) { 1519bc68afe2S[email protected] // cancel all l2cap connections 1520bc68afe2S[email protected] hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host 15213a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 1522f5454fc6Smatthias.ringwald } 1523f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 15243a9fb326S[email protected] 15253a9fb326S[email protected] #ifdef HAVE_MALLOC 15263a9fb326S[email protected] free(hci_stack); 15273a9fb326S[email protected] #endif 15283a9fb326S[email protected] hci_stack = NULL; 1529404843c1Smatthias.ringwald } 1530404843c1Smatthias.ringwald 15319e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 15329e61646fS[email protected] hci_stack->class_of_device = class_of_device; 15339e61646fS[email protected] } 15349e61646fS[email protected] 153566fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 153666fb9560S[email protected] disable_l2cap_timeouts = 1; 153766fb9560S[email protected] } 15388d213e1aSmatthias.ringwald // State-Module-Driver overview 15398d213e1aSmatthias.ringwald // state module low-level 15408d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 15418d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 15428d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 15438d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 1544d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 1545d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 1546c7e0c5f6Smatthias.ringwald 154740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 15487301ad89Smatthias.ringwald 1549038bc64cSmatthias.ringwald // power on 1550f9a30166Smatthias.ringwald int err = 0; 15513a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 15523a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1553f9a30166Smatthias.ringwald } 1554038bc64cSmatthias.ringwald if (err){ 15559da54300S[email protected] log_error( "POWER_ON failed"); 1556038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1557038bc64cSmatthias.ringwald return err; 1558038bc64cSmatthias.ringwald } 1559038bc64cSmatthias.ringwald 1560038bc64cSmatthias.ringwald // open low-level device 15613a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1562038bc64cSmatthias.ringwald if (err){ 15639da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 15643a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 15653a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1566f9a30166Smatthias.ringwald } 1567038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1568038bc64cSmatthias.ringwald return err; 1569038bc64cSmatthias.ringwald } 15708d213e1aSmatthias.ringwald return 0; 15718d213e1aSmatthias.ringwald } 1572038bc64cSmatthias.ringwald 157340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 15748d213e1aSmatthias.ringwald 15759da54300S[email protected] log_info("hci_power_control_off"); 15769418f9c9Smatthias.ringwald 15778d213e1aSmatthias.ringwald // close low-level device 15783a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 15798d213e1aSmatthias.ringwald 15809da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 15819418f9c9Smatthias.ringwald 15828d213e1aSmatthias.ringwald // power off 15833a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 15843a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 15858d213e1aSmatthias.ringwald } 15869418f9c9Smatthias.ringwald 15879da54300S[email protected] log_info("hci_power_control_off - control closed"); 15889418f9c9Smatthias.ringwald 15893a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 159072ea5239Smatthias.ringwald } 159172ea5239Smatthias.ringwald 159240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 159372ea5239Smatthias.ringwald 15949da54300S[email protected] log_info("hci_power_control_sleep"); 15953144bce4Smatthias.ringwald 1596b429b9b7Smatthias.ringwald #if 0 1597b429b9b7Smatthias.ringwald // don't close serial port during sleep 1598b429b9b7Smatthias.ringwald 159972ea5239Smatthias.ringwald // close low-level device 16003a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1601b429b9b7Smatthias.ringwald #endif 160272ea5239Smatthias.ringwald 160372ea5239Smatthias.ringwald // sleep mode 16043a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 16053a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 160672ea5239Smatthias.ringwald } 1607b429b9b7Smatthias.ringwald 16083a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 16098d213e1aSmatthias.ringwald } 16108d213e1aSmatthias.ringwald 161140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1612b429b9b7Smatthias.ringwald 16139da54300S[email protected] log_info("hci_power_control_wake"); 1614b429b9b7Smatthias.ringwald 1615b429b9b7Smatthias.ringwald // wake on 16163a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 16173a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1618b429b9b7Smatthias.ringwald } 1619b429b9b7Smatthias.ringwald 1620b429b9b7Smatthias.ringwald #if 0 1621b429b9b7Smatthias.ringwald // open low-level device 16223a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1623b429b9b7Smatthias.ringwald if (err){ 16249da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 16253a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 16263a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1627b429b9b7Smatthias.ringwald } 1628b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1629b429b9b7Smatthias.ringwald return err; 1630b429b9b7Smatthias.ringwald } 1631b429b9b7Smatthias.ringwald #endif 1632b429b9b7Smatthias.ringwald 1633b429b9b7Smatthias.ringwald return 0; 1634b429b9b7Smatthias.ringwald } 1635b429b9b7Smatthias.ringwald 163644935e40S[email protected] static void hci_power_transition_to_initializing(void){ 163744935e40S[email protected] // set up state machine 163844935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 163944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 164044935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 164144935e40S[email protected] hci_stack->substate = 0; 164244935e40S[email protected] } 1643b429b9b7Smatthias.ringwald 16448d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1645d661ed19Smatthias.ringwald 16469da54300S[email protected] log_info("hci_power_control: %u, current mode %u", power_mode, hci_stack->state); 1647d661ed19Smatthias.ringwald 16488d213e1aSmatthias.ringwald int err = 0; 16493a9fb326S[email protected] switch (hci_stack->state){ 16508d213e1aSmatthias.ringwald 16518d213e1aSmatthias.ringwald case HCI_STATE_OFF: 16528d213e1aSmatthias.ringwald switch (power_mode){ 16538d213e1aSmatthias.ringwald case HCI_POWER_ON: 16548d213e1aSmatthias.ringwald err = hci_power_control_on(); 165597b61c7bS[email protected] if (err) { 165697b61c7bS[email protected] log_error("hci_power_control_on() error %u", err); 165797b61c7bS[email protected] return err; 165897b61c7bS[email protected] } 165944935e40S[email protected] hci_power_transition_to_initializing(); 16608d213e1aSmatthias.ringwald break; 16618d213e1aSmatthias.ringwald case HCI_POWER_OFF: 16628d213e1aSmatthias.ringwald // do nothing 16638d213e1aSmatthias.ringwald break; 16648d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1665b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 16668d213e1aSmatthias.ringwald break; 16678d213e1aSmatthias.ringwald } 16688d213e1aSmatthias.ringwald break; 16697301ad89Smatthias.ringwald 16708d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 16718d213e1aSmatthias.ringwald switch (power_mode){ 16728d213e1aSmatthias.ringwald case HCI_POWER_ON: 16738d213e1aSmatthias.ringwald // do nothing 16748d213e1aSmatthias.ringwald break; 16758d213e1aSmatthias.ringwald case HCI_POWER_OFF: 16768d213e1aSmatthias.ringwald // no connections yet, just turn it off 16778d213e1aSmatthias.ringwald hci_power_control_off(); 16788d213e1aSmatthias.ringwald break; 16798d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1680b546ac54Smatthias.ringwald // no connections yet, just turn it off 168172ea5239Smatthias.ringwald hci_power_control_sleep(); 16828d213e1aSmatthias.ringwald break; 16838d213e1aSmatthias.ringwald } 16848d213e1aSmatthias.ringwald break; 16857301ad89Smatthias.ringwald 16868d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 16878d213e1aSmatthias.ringwald switch (power_mode){ 16888d213e1aSmatthias.ringwald case HCI_POWER_ON: 16898d213e1aSmatthias.ringwald // do nothing 16908d213e1aSmatthias.ringwald break; 16918d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1692c7e0c5f6Smatthias.ringwald // see hci_run 16933a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 16948d213e1aSmatthias.ringwald break; 16958d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1696b546ac54Smatthias.ringwald // see hci_run 16973a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 16983a9fb326S[email protected] hci_stack->substate = 0; 16998d213e1aSmatthias.ringwald break; 17008d213e1aSmatthias.ringwald } 17018d213e1aSmatthias.ringwald break; 17027301ad89Smatthias.ringwald 17038d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 17048d213e1aSmatthias.ringwald switch (power_mode){ 17058d213e1aSmatthias.ringwald case HCI_POWER_ON: 170644935e40S[email protected] hci_power_transition_to_initializing(); 17078d213e1aSmatthias.ringwald break; 17088d213e1aSmatthias.ringwald case HCI_POWER_OFF: 17098d213e1aSmatthias.ringwald // do nothing 17108d213e1aSmatthias.ringwald break; 17118d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1712b546ac54Smatthias.ringwald // see hci_run 17133a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 17143a9fb326S[email protected] hci_stack->substate = 0; 17158d213e1aSmatthias.ringwald break; 17168d213e1aSmatthias.ringwald } 17178d213e1aSmatthias.ringwald break; 17188d213e1aSmatthias.ringwald 17198d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 17208d213e1aSmatthias.ringwald switch (power_mode){ 17218d213e1aSmatthias.ringwald case HCI_POWER_ON: 172228171530Smatthias.ringwald 172328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 172428171530Smatthias.ringwald // nothing to do, if H4 supports power management 172528171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 17263a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 17273a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 172828171530Smatthias.ringwald break; 172928171530Smatthias.ringwald } 173028171530Smatthias.ringwald #endif 173144935e40S[email protected] hci_power_transition_to_initializing(); 17328d213e1aSmatthias.ringwald break; 17338d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1734b546ac54Smatthias.ringwald // see hci_run 17353a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 17368d213e1aSmatthias.ringwald break; 17378d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1738b546ac54Smatthias.ringwald // do nothing 17398d213e1aSmatthias.ringwald break; 17408d213e1aSmatthias.ringwald } 17418d213e1aSmatthias.ringwald break; 17428d213e1aSmatthias.ringwald 17438d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 17448d213e1aSmatthias.ringwald switch (power_mode){ 17458d213e1aSmatthias.ringwald case HCI_POWER_ON: 174628171530Smatthias.ringwald 174728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 174828171530Smatthias.ringwald // nothing to do, if H4 supports power management 174928171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 17503a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 17513a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1752758b46ceSmatthias.ringwald hci_update_scan_enable(); 175328171530Smatthias.ringwald break; 175428171530Smatthias.ringwald } 175528171530Smatthias.ringwald #endif 17563144bce4Smatthias.ringwald err = hci_power_control_wake(); 17573144bce4Smatthias.ringwald if (err) return err; 175844935e40S[email protected] hci_power_transition_to_initializing(); 17598d213e1aSmatthias.ringwald break; 17608d213e1aSmatthias.ringwald case HCI_POWER_OFF: 17613a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 17628d213e1aSmatthias.ringwald break; 17638d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1764b546ac54Smatthias.ringwald // do nothing 17658d213e1aSmatthias.ringwald break; 17668d213e1aSmatthias.ringwald } 17678d213e1aSmatthias.ringwald break; 176811e23e5fSmatthias.ringwald } 176968d92d03Smatthias.ringwald 1770038bc64cSmatthias.ringwald // create internal event 1771ee8bf225Smatthias.ringwald hci_emit_state(); 1772ee8bf225Smatthias.ringwald 177368d92d03Smatthias.ringwald // trigger next/first action 177468d92d03Smatthias.ringwald hci_run(); 177568d92d03Smatthias.ringwald 1776475c8125Smatthias.ringwald return 0; 1777475c8125Smatthias.ringwald } 1778475c8125Smatthias.ringwald 1779758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1780758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 17813a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1782758b46ceSmatthias.ringwald hci_run(); 1783758b46ceSmatthias.ringwald } 1784758b46ceSmatthias.ringwald 1785381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1786381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1787381fbed8Smatthias.ringwald 17883a9fb326S[email protected] if (hci_stack->discoverable == enable){ 17893a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1790381fbed8Smatthias.ringwald return; 1791381fbed8Smatthias.ringwald } 1792381fbed8Smatthias.ringwald 17933a9fb326S[email protected] hci_stack->discoverable = enable; 1794758b46ceSmatthias.ringwald hci_update_scan_enable(); 1795758b46ceSmatthias.ringwald } 1796b031bebbSmatthias.ringwald 1797758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1798758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1799758b46ceSmatthias.ringwald 1800758b46ceSmatthias.ringwald // don't emit event 18013a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1802758b46ceSmatthias.ringwald 18033a9fb326S[email protected] hci_stack->connectable = enable; 1804758b46ceSmatthias.ringwald hci_update_scan_enable(); 1805381fbed8Smatthias.ringwald } 1806381fbed8Smatthias.ringwald 18075061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 18083a9fb326S[email protected] return &hci_stack->local_bd_addr; 18095061f3afS[email protected] } 18105061f3afS[email protected] 181106b35ec0Smatthias.ringwald void hci_run(){ 18128a485f27Smatthias.ringwald 181332ab9390Smatthias.ringwald hci_connection_t * connection; 181432ab9390Smatthias.ringwald linked_item_t * it; 181532ab9390Smatthias.ringwald 1816b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 1817b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 1818b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 1819b5d8b22bS[email protected] if (hci_can_send_prepared_acl_packet_now(con_handle)){ 1820b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 1821b5d8b22bS[email protected] if (connection) { 1822b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 1823b5d8b22bS[email protected] return; 1824b5d8b22bS[email protected] } 1825b5d8b22bS[email protected] // connection gone -> discard further fragments 1826b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 1827b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 1828b5d8b22bS[email protected] } 1829b5d8b22bS[email protected] } 1830b5d8b22bS[email protected] 1831d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 1832ce4c8fabSmatthias.ringwald 1833b031bebbSmatthias.ringwald // global/non-connection oriented commands 1834b031bebbSmatthias.ringwald 1835b031bebbSmatthias.ringwald // decline incoming connections 18363a9fb326S[email protected] if (hci_stack->decline_reason){ 18373a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 18383a9fb326S[email protected] hci_stack->decline_reason = 0; 18393a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1840dbe1a790S[email protected] return; 1841ce4c8fabSmatthias.ringwald } 1842ce4c8fabSmatthias.ringwald 1843b031bebbSmatthias.ringwald // send scan enable 18443a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 18453a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 18463a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1847dbe1a790S[email protected] return; 1848b031bebbSmatthias.ringwald } 1849b031bebbSmatthias.ringwald 1850b2f949feS[email protected] #ifdef HAVE_BLE 18517bdc6798S[email protected] // handle le scan 18527bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 18537bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 18547bdc6798S[email protected] case LE_START_SCAN: 18557bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 18567bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 18577bdc6798S[email protected] return; 18587bdc6798S[email protected] 18597bdc6798S[email protected] case LE_STOP_SCAN: 18607bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 18617bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 18627bdc6798S[email protected] return; 18637bdc6798S[email protected] default: 18647bdc6798S[email protected] break; 18657bdc6798S[email protected] } 1866e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 1867e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 1868e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 1869e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1870e2602ea2Smatthias.ringwald hci_send_cmd(&hci_le_set_scan_parameters, scan_type, hci_stack->le_scan_interval, hci_stack->le_scan_window, hci_stack->adv_addr_type, 0); 1871e2602ea2Smatthias.ringwald return; 1872e2602ea2Smatthias.ringwald } 18737bdc6798S[email protected] } 1874b2f949feS[email protected] #endif 18757bdc6798S[email protected] 187632ab9390Smatthias.ringwald // send pending HCI commands 18773a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 1878b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 187932ab9390Smatthias.ringwald 18800bf6344aS[email protected] switch(connection->state){ 18810bf6344aS[email protected] case SEND_CREATE_CONNECTION: 18824f3229d8S[email protected] switch(connection->address_type){ 18834f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 18849da54300S[email protected] log_info("sending hci_create_connection"); 1885ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 18864f3229d8S[email protected] break; 18874f3229d8S[email protected] default: 1888b2f949feS[email protected] #ifdef HAVE_BLE 18899da54300S[email protected] log_info("sending hci_le_create_connection"); 18904f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 1891b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 1892b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 18934f3229d8S[email protected] 0, // don't use whitelist 18944f3229d8S[email protected] connection->address_type, // peer address type 18954f3229d8S[email protected] connection->address, // peer bd addr 1896b2571179Smatthias.ringwald hci_stack->adv_addr_type, // our addr type: 1897b2571179Smatthias.ringwald 0x0008, // conn interval min 1898b2571179Smatthias.ringwald 0x0018, // conn interval max 18994f3229d8S[email protected] 0, // conn latency 1900b2571179Smatthias.ringwald 0x0048, // supervision timeout 1901b2571179Smatthias.ringwald 0x0001, // min ce length 1902b2571179Smatthias.ringwald 0x0001 // max ce length 19034f3229d8S[email protected] ); 19044f3229d8S[email protected] 19054f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 1906b2f949feS[email protected] #endif 19074f3229d8S[email protected] break; 19084f3229d8S[email protected] } 1909ad83dc6aS[email protected] return; 1910ad83dc6aS[email protected] 19110bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 19129da54300S[email protected] log_info("sending hci_accept_connection_request"); 191332ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 191434d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1915dbe1a790S[email protected] return; 19160bf6344aS[email protected] 1917a6725849S[email protected] #ifdef HAVE_BLE 19180bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 19190bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 19200bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 19210bf6344aS[email protected] return; 1922a6725849S[email protected] #endif 19230bf6344aS[email protected] case SEND_DISCONNECT: 19240bf6344aS[email protected] connection->state = SENT_DISCONNECT; 19257851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 19260bf6344aS[email protected] return; 19270bf6344aS[email protected] 19280bf6344aS[email protected] default: 19290bf6344aS[email protected] break; 1930c7e0c5f6Smatthias.ringwald } 1931c7e0c5f6Smatthias.ringwald 193232ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 19339da54300S[email protected] log_info("responding to link key request"); 193434d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 193532ab9390Smatthias.ringwald link_key_t link_key; 1936c77e8838S[email protected] link_key_type_t link_key_type; 19373a9fb326S[email protected] if ( hci_stack->remote_device_db 19383a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 193934d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 19409ab95c90S[email protected] connection->link_key_type = link_key_type; 194132ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 194232ab9390Smatthias.ringwald } else { 194332ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 194432ab9390Smatthias.ringwald } 1945dbe1a790S[email protected] return; 194632ab9390Smatthias.ringwald } 19471d6b20aeS[email protected] 1948899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 19499da54300S[email protected] log_info("denying to pin request"); 1950899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 195134d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 19524c57c146S[email protected] return; 19534c57c146S[email protected] } 19544c57c146S[email protected] 1955dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 195634d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 195782d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 195882d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 1959106d6d11S[email protected] // tweak authentication requirements 19603a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1961106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 19629faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 19639faad3abS[email protected] } 19649faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 19659faad3abS[email protected] authreq |= 1; 1966106d6d11S[email protected] } 19673a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1968f8fb5f6eS[email protected] } else { 1969f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1970f8fb5f6eS[email protected] } 1971dbe1a790S[email protected] return; 197232ab9390Smatthias.ringwald } 197332ab9390Smatthias.ringwald 1974dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1975dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 197634d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1977dbe1a790S[email protected] return; 1978dbe1a790S[email protected] } 1979dbe1a790S[email protected] 1980dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1981dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 198234d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1983dbe1a790S[email protected] return; 1984dbe1a790S[email protected] } 1985afd4e962S[email protected] 1986afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1987afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 198834d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 19892bd8b7e7S[email protected] return; 19902bd8b7e7S[email protected] } 19912bd8b7e7S[email protected] 19922bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 19932bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 199434d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 199534d2123cS[email protected] return; 199634d2123cS[email protected] } 1997ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1998ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 19991bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 200052d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 2001ad83dc6aS[email protected] return; 2002ad83dc6aS[email protected] } 200334d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 200434d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 200534d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 20062bd8b7e7S[email protected] return; 2007afd4e962S[email protected] } 2008dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 2009dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 2010dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 2011dce78009S[email protected] return; 2012dce78009S[email protected] } 2013da886c03S[email protected] 2014c37a3166S[email protected] #ifdef HAVE_BLE 2015da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 2016da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 2017da886c03S[email protected] 2018c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 2019c37a3166S[email protected] connection->le_conn_interval_min = 0; 2020c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 2021c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 2022c37a3166S[email protected] 0x0000, 0xffff); 2023c37a3166S[email protected] } 2024c37a3166S[email protected] #endif 2025dbe1a790S[email protected] } 2026c7e0c5f6Smatthias.ringwald 20273a9fb326S[email protected] switch (hci_stack->state){ 20283429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 2029bd88fde9S[email protected] hci_initializing_state_machine(); 20303429f56bSmatthias.ringwald break; 2031c7e0c5f6Smatthias.ringwald 2032c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 2033c7e0c5f6Smatthias.ringwald 20349da54300S[email protected] log_info("HCI_STATE_HALTING"); 2035c7e0c5f6Smatthias.ringwald // close all open connections 20363a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 2037c7e0c5f6Smatthias.ringwald if (connection){ 203832ab9390Smatthias.ringwald 2039c7e0c5f6Smatthias.ringwald // send disconnect 2040d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 204132ab9390Smatthias.ringwald 20429da54300S[email protected] log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 20436ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 2044c7e0c5f6Smatthias.ringwald 2045c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 2046c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 2047c7e0c5f6Smatthias.ringwald return; 2048c7e0c5f6Smatthias.ringwald } 20499da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 2050c7e0c5f6Smatthias.ringwald 205172ea5239Smatthias.ringwald // switch mode 2052c7e0c5f6Smatthias.ringwald hci_power_control_off(); 20539418f9c9Smatthias.ringwald 20549da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 205572ea5239Smatthias.ringwald hci_emit_state(); 20569da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 205772ea5239Smatthias.ringwald break; 2058c7e0c5f6Smatthias.ringwald 205972ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 20603a9fb326S[email protected] switch(hci_stack->substate) { 206189db417bSmatthias.ringwald case 0: 20629da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 206372ea5239Smatthias.ringwald // close all open connections 20643a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 206566da7044Smatthias.ringwald 206628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 206766da7044Smatthias.ringwald // don't close connections, if H4 supports power management 206866da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 206966da7044Smatthias.ringwald connection = NULL; 207066da7044Smatthias.ringwald } 207166da7044Smatthias.ringwald #endif 207272ea5239Smatthias.ringwald if (connection){ 207332ab9390Smatthias.ringwald 207472ea5239Smatthias.ringwald // send disconnect 2075d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 207632ab9390Smatthias.ringwald 20779da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 20786ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 207972ea5239Smatthias.ringwald 208072ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 208172ea5239Smatthias.ringwald hci_shutdown_connection(connection); 208272ea5239Smatthias.ringwald return; 208372ea5239Smatthias.ringwald } 208472ea5239Smatthias.ringwald 208592368cd3S[email protected] if (hci_classic_supported()){ 208689db417bSmatthias.ringwald // disable page and inquiry scan 2087d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 208832ab9390Smatthias.ringwald 20899da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 20903a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 209189db417bSmatthias.ringwald 209289db417bSmatthias.ringwald // continue in next sub state 20933a9fb326S[email protected] hci_stack->substate++; 209489db417bSmatthias.ringwald break; 209592368cd3S[email protected] } 209692368cd3S[email protected] // fall through for ble-only chips 209792368cd3S[email protected] 209889db417bSmatthias.ringwald case 2: 20999da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 210028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 210128171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 210228171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 210328171530Smatthias.ringwald // SLEEP MODE reached 21043a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 210528171530Smatthias.ringwald hci_emit_state(); 210628171530Smatthias.ringwald break; 210728171530Smatthias.ringwald } 210828171530Smatthias.ringwald #endif 210972ea5239Smatthias.ringwald // switch mode 21103a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 2111c7e0c5f6Smatthias.ringwald hci_emit_state(); 211228171530Smatthias.ringwald break; 211328171530Smatthias.ringwald 211489db417bSmatthias.ringwald default: 211589db417bSmatthias.ringwald break; 211689db417bSmatthias.ringwald } 2117c7e0c5f6Smatthias.ringwald break; 2118c7e0c5f6Smatthias.ringwald 21193429f56bSmatthias.ringwald default: 21203429f56bSmatthias.ringwald break; 21211f504dbdSmatthias.ringwald } 21223429f56bSmatthias.ringwald } 212316833f0aSmatthias.ringwald 212431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 2125c8e4258aSmatthias.ringwald bd_addr_t addr; 2126c8e4258aSmatthias.ringwald hci_connection_t * conn; 2127c8e4258aSmatthias.ringwald // house-keeping 2128c8e4258aSmatthias.ringwald 2129c8e4258aSmatthias.ringwald // create_connection? 2130c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 2131c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 21329da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 2133c8e4258aSmatthias.ringwald 213496a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 2135ad83dc6aS[email protected] if (!conn){ 213696a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 213717f1ba2aSmatthias.ringwald if (!conn){ 213817f1ba2aSmatthias.ringwald // notify client that alloc failed 213917f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 214017f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 214117f1ba2aSmatthias.ringwald } 2142ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 2143ad83dc6aS[email protected] } 2144ad83dc6aS[email protected] log_info("conn state %u", conn->state); 2145ad83dc6aS[email protected] switch (conn->state){ 2146ad83dc6aS[email protected] // if connection active exists 2147ad83dc6aS[email protected] case OPEN: 214862bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 2149ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 215062bda3fbS[email protected] return 0; 2151ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 2152ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 2153ad83dc6aS[email protected] break; 2154ad83dc6aS[email protected] default: 2155ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 2156ad83dc6aS[email protected] return 0; 2157ad83dc6aS[email protected] } 2158c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 2159c8e4258aSmatthias.ringwald } 2160c8e4258aSmatthias.ringwald 21617fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 21627fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 21637fde4af9Smatthias.ringwald } 21647fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 21657fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 21667fde4af9Smatthias.ringwald } 21677fde4af9Smatthias.ringwald 21688ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 21693a9fb326S[email protected] if (hci_stack->remote_device_db){ 21708ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 21713a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 21728ef73945Smatthias.ringwald } 21738ef73945Smatthias.ringwald } 2174c8e4258aSmatthias.ringwald 21756724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 21766724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 21776724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 217896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 21796724cd9eS[email protected] if (conn){ 21806724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 21816724cd9eS[email protected] } 21826724cd9eS[email protected] } 21836724cd9eS[email protected] 21846724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 21856724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 21866724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 21876724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 21886724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 218996a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 21906724cd9eS[email protected] if (conn){ 21916724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 21926724cd9eS[email protected] } 21936724cd9eS[email protected] } 21946724cd9eS[email protected] 219569a97523S[email protected] #ifdef HAVE_BLE 219669a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 21973a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 219869a97523S[email protected] } 219969a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 22003a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 220169a97523S[email protected] } 220269a97523S[email protected] #endif 220369a97523S[email protected] 22043a9fb326S[email protected] hci_stack->num_cmd_packets--; 22055bb5bc3eS[email protected] 22065bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 22076b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 22086b4af23dS[email protected] 2209d051460cS[email protected] // release packet buffer for synchronous transport implementations 2210c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 22116b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 22126b4af23dS[email protected] } 22136b4af23dS[email protected] 22146b4af23dS[email protected] return err; 221531452debSmatthias.ringwald } 22168adf0ddaSmatthias.ringwald 22172bd8b7e7S[email protected] // disconnect because of security block 22182bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 22192bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 22202bd8b7e7S[email protected] if (!connection) return; 22212bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 22222bd8b7e7S[email protected] } 22232bd8b7e7S[email protected] 22242bd8b7e7S[email protected] 2225dbe1a790S[email protected] // Configure Secure Simple Pairing 2226dbe1a790S[email protected] 2227dbe1a790S[email protected] // enable will enable SSP during init 2228dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 22293a9fb326S[email protected] hci_stack->ssp_enable = enable; 2230dbe1a790S[email protected] } 2231dbe1a790S[email protected] 22322bd8b7e7S[email protected] int hci_local_ssp_activated(){ 22333a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 22342bd8b7e7S[email protected] } 22352bd8b7e7S[email protected] 2236dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 2237dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 22383a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 2239dbe1a790S[email protected] } 2240dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 22413a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 2242dbe1a790S[email protected] } 2243dbe1a790S[email protected] 2244dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 2245dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 22463a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 2247dbe1a790S[email protected] } 2248dbe1a790S[email protected] 22491cd208adSmatthias.ringwald /** 22501cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 22511cd208adSmatthias.ringwald */ 2252fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 22535127cc62S[email protected] 2254d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 22559d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 22569d14b626S[email protected] return 0; 22579d14b626S[email protected] } 22589d14b626S[email protected] 22595127cc62S[email protected] // for HCI INITIALIZATION 22609da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 22615127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 22625127cc62S[email protected] 22639d14b626S[email protected] hci_reserve_packet_buffer(); 22649d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 22659d14b626S[email protected] 22661cd208adSmatthias.ringwald va_list argptr; 22671cd208adSmatthias.ringwald va_start(argptr, cmd); 22689d14b626S[email protected] uint16_t size = hci_create_cmd_internal(packet, cmd, argptr); 22691cd208adSmatthias.ringwald va_end(argptr); 22709d14b626S[email protected] 22719d14b626S[email protected] return hci_send_cmd_packet(packet, size); 227293b8dc03Smatthias.ringwald } 2273c8e4258aSmatthias.ringwald 2274ee091cf1Smatthias.ringwald // Create various non-HCI events. 2275ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 2276ee091cf1Smatthias.ringwald 2277c8e4258aSmatthias.ringwald void hci_emit_state(){ 22783a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 2279425d1371Smatthias.ringwald uint8_t event[3]; 228080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 2281425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 22823a9fb326S[email protected] event[2] = hci_stack->state; 2283425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 22843a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2285c8e4258aSmatthias.ringwald } 2286c8e4258aSmatthias.ringwald 228717f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 2288425d1371Smatthias.ringwald uint8_t event[13]; 2289c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 2290425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 229117f1ba2aSmatthias.ringwald event[2] = status; 2292c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 2293c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 2294c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 2295c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 2296425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 22973a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2298c8e4258aSmatthias.ringwald } 2299c8e4258aSmatthias.ringwald 23006e2e9a6bS[email protected] void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t * address, uint16_t conn_handle, uint8_t status){ 23014f3229d8S[email protected] uint8_t event[21]; 23024f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 23034f3229d8S[email protected] event[1] = sizeof(event) - 2; 23044f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 23054f3229d8S[email protected] event[3] = status; 23066e2e9a6bS[email protected] bt_store_16(event, 4, conn_handle); 23074f3229d8S[email protected] event[6] = 0; // TODO: role 23086e2e9a6bS[email protected] event[7] = address_type; 23096e2e9a6bS[email protected] bt_flip_addr(&event[8], *address); 23104f3229d8S[email protected] bt_store_16(event, 14, 0); // interval 23114f3229d8S[email protected] bt_store_16(event, 16, 0); // latency 23124f3229d8S[email protected] bt_store_16(event, 18, 0); // supervision timeout 23134f3229d8S[email protected] event[20] = 0; // master clock accuracy 23144f3229d8S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 23154f3229d8S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23164f3229d8S[email protected] } 23174f3229d8S[email protected] 23183c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 2319425d1371Smatthias.ringwald uint8_t event[6]; 23203c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 2321e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 23223c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 23233c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 23243c4d4b90Smatthias.ringwald event[5] = reason; 2325425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 23263a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23273c4d4b90Smatthias.ringwald } 23283c4d4b90Smatthias.ringwald 2329ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 233066fb9560S[email protected] if (disable_l2cap_timeouts) return; 2331e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 2332425d1371Smatthias.ringwald uint8_t event[4]; 233380d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 2334425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2335ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 2336425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 23373a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2338ee091cf1Smatthias.ringwald } 233943bfb1bdSmatthias.ringwald 234043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 2341e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 2342425d1371Smatthias.ringwald uint8_t event[3]; 234380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 2344425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 234543bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 2346425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23473a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 234843bfb1bdSmatthias.ringwald } 2349038bc64cSmatthias.ringwald 2350038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 2351e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 2352425d1371Smatthias.ringwald uint8_t event[2]; 235380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 2354425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2355425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23563a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2357038bc64cSmatthias.ringwald } 23581b0e3922Smatthias.ringwald 235909ba8edeSmatthias.ringwald #ifndef EMBEDDED 23601b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 2361e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 2362425d1371Smatthias.ringwald uint8_t event[6]; 23631b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 2364425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2365425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 2366425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 2367425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 2368425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23693a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23701b0e3922Smatthias.ringwald } 237109ba8edeSmatthias.ringwald #endif 23721b0e3922Smatthias.ringwald 23732ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 2374e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 2375425d1371Smatthias.ringwald uint8_t event[3]; 23762ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 2377425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 23782ed6235cSmatthias.ringwald event[2] = enabled; 2379425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23803a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23812ed6235cSmatthias.ringwald } 2382627c2f45Smatthias.ringwald 2383627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 2384e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 2385627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 2386e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 2387f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 23882f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 2389f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 2390e0abb8e7S[email protected] 2391e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 2392e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 2393e0abb8e7S[email protected] 2394e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 23953a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 2396627c2f45Smatthias.ringwald } 2397381fbed8Smatthias.ringwald 2398381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 2399e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 2400425d1371Smatthias.ringwald uint8_t event[3]; 2401381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 2402425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2403381fbed8Smatthias.ringwald event[2] = enabled; 2404425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 24053a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2406381fbed8Smatthias.ringwald } 2407458bf4e8S[email protected] 2408a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 2409df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 2410a00031e2S[email protected] uint8_t event[5]; 2411e00caf9cS[email protected] int pos = 0; 2412a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 2413e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 2414a00031e2S[email protected] bt_store_16(event, 2, con_handle); 2415e00caf9cS[email protected] pos += 2; 2416e00caf9cS[email protected] event[pos++] = level; 2417e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 24183a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2419e00caf9cS[email protected] } 2420e00caf9cS[email protected] 24211bd5283dS[email protected] void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 2422ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 2423ad83dc6aS[email protected] uint8_t event[9]; 2424ad83dc6aS[email protected] int pos = 0; 2425ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 2426ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 2427ad83dc6aS[email protected] event[pos++] = status; 24281bd5283dS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], address); 2429ad83dc6aS[email protected] pos += 6; 2430ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 24313a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2432ad83dc6aS[email protected] } 2433ad83dc6aS[email protected] 24342bd8b7e7S[email protected] // query if remote side supports SSP 24352bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 24362bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 24372bd8b7e7S[email protected] if (!connection) return 0; 24382bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 24392bd8b7e7S[email protected] } 24402bd8b7e7S[email protected] 2441df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 2442df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 2443df3354fcS[email protected] } 2444df3354fcS[email protected] 2445458bf4e8S[email protected] // GAP API 2446458bf4e8S[email protected] /** 2447458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 2448458bf4e8S[email protected] * @praram enabled 2449458bf4e8S[email protected] */ 24504c57c146S[email protected] void gap_set_bondable_mode(int enable){ 24513a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 2452458bf4e8S[email protected] } 2453cb230b9dS[email protected] 2454cb230b9dS[email protected] /** 245534d2123cS[email protected] * @brief map link keys to security levels 2456cb230b9dS[email protected] */ 245734d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 245834d2123cS[email protected] switch (link_key_type){ 24593c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 24603c68dfa9S[email protected] return LEVEL_4; 24613c68dfa9S[email protected] case COMBINATION_KEY: 24623c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 24633c68dfa9S[email protected] return LEVEL_3; 24643c68dfa9S[email protected] default: 24653c68dfa9S[email protected] return LEVEL_2; 24663c68dfa9S[email protected] } 2467cb230b9dS[email protected] } 2468cb230b9dS[email protected] 2469a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 247034d2123cS[email protected] if (!connection) return LEVEL_0; 247134d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 247234d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 247334d2123cS[email protected] } 247434d2123cS[email protected] 247534d2123cS[email protected] 2476106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 24775127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 2478106d6d11S[email protected] return level > LEVEL_2; 2479106d6d11S[email protected] } 2480106d6d11S[email protected] 248134d2123cS[email protected] /** 248234d2123cS[email protected] * @brief get current security level 248334d2123cS[email protected] */ 248434d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 248534d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 248634d2123cS[email protected] if (!connection) return LEVEL_0; 248734d2123cS[email protected] return gap_security_level_for_connection(connection); 248834d2123cS[email protected] } 248934d2123cS[email protected] 2490cb230b9dS[email protected] /** 2491cb230b9dS[email protected] * @brief request connection to device to 2492cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 2493cb230b9dS[email protected] */ 249434d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 249534d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 249634d2123cS[email protected] if (!connection){ 2497a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 249834d2123cS[email protected] return; 249934d2123cS[email protected] } 250034d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 250134d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 250234d2123cS[email protected] if (current_level >= requested_level){ 2503a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 250434d2123cS[email protected] return; 250534d2123cS[email protected] } 2506a00031e2S[email protected] 250734d2123cS[email protected] connection->requested_security_level = requested_level; 2508a00031e2S[email protected] 250925bf5872S[email protected] #if 0 251025bf5872S[email protected] // sending encryption request without a link key results in an error. 251125bf5872S[email protected] // TODO: figure out how to use it properly 251225bf5872S[email protected] 2513fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 25143a9fb326S[email protected] if (hci_stack->remote_device_db){ 2515a00031e2S[email protected] link_key_type_t link_key_type; 2516a00031e2S[email protected] link_key_t link_key; 25173a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 2518a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 2519a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2520a00031e2S[email protected] return; 2521a00031e2S[email protected] } 2522a00031e2S[email protected] } 2523a00031e2S[email protected] } 252425bf5872S[email protected] #endif 2525a00031e2S[email protected] 25261eb2563eS[email protected] // try to authenticate connection 25271eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2528e80b2cf9S[email protected] hci_run(); 2529e00caf9cS[email protected] } 2530ad83dc6aS[email protected] 2531ad83dc6aS[email protected] /** 2532ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 2533ad83dc6aS[email protected] * @param device 2534ad83dc6aS[email protected] * @param request MITM protection 2535ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 2536ad83dc6aS[email protected] */ 2537ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 2538ad83dc6aS[email protected] 2539ad83dc6aS[email protected] // create connection state machine 254096a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 2541ad83dc6aS[email protected] 2542ad83dc6aS[email protected] if (!connection){ 2543ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 2544ad83dc6aS[email protected] } 2545ad83dc6aS[email protected] 2546ad83dc6aS[email protected] // delete linkn key 2547ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 2548ad83dc6aS[email protected] 2549ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 2550ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 2551ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 25525127cc62S[email protected] log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level); 2553ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 2554ad83dc6aS[email protected] 2555ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 2556ad83dc6aS[email protected] 2557ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 2558ad83dc6aS[email protected] // handle: authentication failure 2559ad83dc6aS[email protected] // handle: disconnect on done 2560ad83dc6aS[email protected] 2561ad83dc6aS[email protected] hci_run(); 2562ad83dc6aS[email protected] 2563ad83dc6aS[email protected] return 0; 2564ad83dc6aS[email protected] } 25658e618f72S[email protected] 25668e618f72S[email protected] void gap_set_local_name(const char * local_name){ 25678e618f72S[email protected] hci_stack->local_name = local_name; 25688e618f72S[email protected] } 25698e618f72S[email protected] 25707bdc6798S[email protected] le_command_status_t le_central_start_scan(){ 257136af3e18S[email protected] if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK; 25727bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 25737bdc6798S[email protected] hci_run(); 25747bdc6798S[email protected] return BLE_PERIPHERAL_OK; 25757bdc6798S[email protected] } 25768e618f72S[email protected] 25777bdc6798S[email protected] le_command_status_t le_central_stop_scan(){ 257836af3e18S[email protected] if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK; 25797bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 25807bdc6798S[email protected] hci_run(); 25817bdc6798S[email protected] return BLE_PERIPHERAL_OK; 25827bdc6798S[email protected] } 25834f3229d8S[email protected] 2584ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 2585ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 2586ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 2587ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 2588ef11999fSmatthias.ringwald hci_run(); 2589ef11999fSmatthias.ringwald } 25904f3229d8S[email protected] 25914f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){ 25924f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 25934f3229d8S[email protected] if (!conn){ 25941f479f8cS[email protected] log_info("le_central_connect: no connection exists yet, creating context"); 25954f3229d8S[email protected] conn = create_connection_for_bd_addr_and_type(*addr, addr_type); 25964f3229d8S[email protected] if (!conn){ 25974f3229d8S[email protected] // notify client that alloc failed 25986e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 25996e2e9a6bS[email protected] log_info("le_central_connect: failed to alloc hci_connection_t"); 26004f3229d8S[email protected] return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller 26014f3229d8S[email protected] } 26024f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 26031f479f8cS[email protected] log_info("le_central_connect: send create connection next"); 2604564fca32S[email protected] hci_run(); 26054f3229d8S[email protected] return BLE_PERIPHERAL_OK; 26064f3229d8S[email protected] } 26070bf6344aS[email protected] 26080bf6344aS[email protected] if (!hci_is_le_connection(conn) || 26090bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 26100bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 26116e2e9a6bS[email protected] hci_emit_le_connection_complete(conn->address_type, &conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 26121f479f8cS[email protected] log_error("le_central_connect: classic connection or connect is already being created"); 26130bf6344aS[email protected] return BLE_PERIPHERAL_IN_WRONG_STATE; 26140bf6344aS[email protected] } 26150bf6344aS[email protected] 26161f479f8cS[email protected] log_info("le_central_connect: context exists with state %u", conn->state); 26176e2e9a6bS[email protected] hci_emit_le_connection_complete(conn->address_type, &conn->address, conn->con_handle, 0); 26184f3229d8S[email protected] hci_run(); 26194f3229d8S[email protected] return BLE_PERIPHERAL_OK; 26204f3229d8S[email protected] } 26214f3229d8S[email protected] 26227851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 26237851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){ 26240bf6344aS[email protected] linked_item_t *it; 26250bf6344aS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 26260bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 26270bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 26280bf6344aS[email protected] switch (conn->state){ 2629a6725849S[email protected] case SEND_CREATE_CONNECTION: 26307851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 26317851196eSmatthias.ringwald return conn; 26327851196eSmatthias.ringwald default: 26337851196eSmatthias.ringwald break; 26347851196eSmatthias.ringwald }; 26357851196eSmatthias.ringwald } 26367851196eSmatthias.ringwald return NULL; 26377851196eSmatthias.ringwald } 26387851196eSmatthias.ringwald 26397851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){ 26407851196eSmatthias.ringwald hci_connection_t * conn = le_central_get_outgoing_connection(); 26417851196eSmatthias.ringwald switch (conn->state){ 26427851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 26437851196eSmatthias.ringwald // skip sending create connection and emit event instead 26446e2e9a6bS[email protected] hci_emit_le_connection_complete(conn->address_type, &conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 26457851196eSmatthias.ringwald linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 26467851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 26470bf6344aS[email protected] break; 2648a6725849S[email protected] case SENT_CREATE_CONNECTION: 26497851196eSmatthias.ringwald // request to send cancel connection 26500bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 26510bf6344aS[email protected] hci_run(); 26520bf6344aS[email protected] break; 26530bf6344aS[email protected] default: 26540bf6344aS[email protected] break; 26550bf6344aS[email protected] } 2656e31f89a7S[email protected] return BLE_PERIPHERAL_OK; 2657e31f89a7S[email protected] } 26584f3229d8S[email protected] 2659c37a3166S[email protected] /** 2660c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 2661c37a3166S[email protected] * @param handle 2662c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 2663c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 2664c37a3166S[email protected] * @param conn_latency 2665c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 2666c37a3166S[email protected] * @returns 0 if ok 2667c37a3166S[email protected] */ 2668c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 2669c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 2670c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 2671c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2672c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 2673c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 2674c37a3166S[email protected] connection->le_conn_latency = conn_latency; 2675c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 2676c37a3166S[email protected] return 0; 2677c37a3166S[email protected] } 2678c37a3166S[email protected] 26795917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){ 26805917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 26815917a5c5S[email protected] if (!conn){ 26827851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 26835917a5c5S[email protected] return BLE_PERIPHERAL_OK; 26845917a5c5S[email protected] } 26855917a5c5S[email protected] conn->state = SEND_DISCONNECT; 26865917a5c5S[email protected] hci_run(); 26874f3229d8S[email protected] return BLE_PERIPHERAL_OK; 26884f3229d8S[email protected] } 268904a6ef8cSmatthias.ringwald 269004a6ef8cSmatthias.ringwald void hci_disconnect_all(){ 269104a6ef8cSmatthias.ringwald linked_list_iterator_t it; 269204a6ef8cSmatthias.ringwald linked_list_iterator_init(&it, &hci_stack->connections); 269304a6ef8cSmatthias.ringwald while (linked_list_iterator_has_next(&it)){ 269404a6ef8cSmatthias.ringwald hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it); 269504a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 269604a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 269704a6ef8cSmatthias.ringwald } 2698d31fba26S[email protected] hci_run(); 269904a6ef8cSmatthias.ringwald } 2700