11f504dbdSmatthias.ringwald /* 2a0c35809S[email protected] * Copyright (C) 2014 BlueKitchen GmbH 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 20a0c35809S[email protected] * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 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 * 33a0c35809S[email protected] * Please inquire about commercial licensing options at 34a0c35809S[email protected] * [email protected] 356b64433eSmatthias.ringwald * 361713bceaSmatthias.ringwald */ 371713bceaSmatthias.ringwald 381713bceaSmatthias.ringwald /* 391f504dbdSmatthias.ringwald * hci.c 401f504dbdSmatthias.ringwald * 411f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 421f504dbdSmatthias.ringwald * 431f504dbdSmatthias.ringwald */ 441f504dbdSmatthias.ringwald 45bde315ceS[email protected] #include "btstack-config.h" 4628171530Smatthias.ringwald 477f2435e6Smatthias.ringwald #include "hci.h" 484c57c146S[email protected] #include "gap.h" 497f2435e6Smatthias.ringwald 5093b8dc03Smatthias.ringwald #include <stdarg.h> 5193b8dc03Smatthias.ringwald #include <string.h> 5256fe0872Smatthias.ringwald #include <stdio.h> 537f2435e6Smatthias.ringwald 54549e6ebeSmatthias.ringwald #ifndef EMBEDDED 55fe475138S[email protected] #ifdef _WIN32 56fe475138S[email protected] #include "Winsock2.h" 57fe475138S[email protected] #else 58549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 59fe475138S[email protected] #endif 6009ba8edeSmatthias.ringwald #include <btstack/version.h> 61549e6ebeSmatthias.ringwald #endif 62549e6ebeSmatthias.ringwald 63a3b02b71Smatthias.ringwald #include "btstack_memory.h" 647f2435e6Smatthias.ringwald #include "debug.h" 65d8905019Smatthias.ringwald #include "hci_dump.h" 6693b8dc03Smatthias.ringwald 67da886c03S[email protected] #include <btstack/linked_list.h> 68ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 691b0e3922Smatthias.ringwald 70169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 71ee091cf1Smatthias.ringwald 72a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 73da5275c5S[email protected] 7428171530Smatthias.ringwald #ifdef USE_BLUETOOL 753d7a3f67S[email protected] #include "../platforms/ios/src/bt_control_iphone.h" 7628171530Smatthias.ringwald #endif 7728171530Smatthias.ringwald 78758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 79a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 8096a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer); 8196a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 827586ee35S[email protected] static int hci_power_control_on(void); 837586ee35S[email protected] static void hci_power_control_off(void); 846155b3d3S[email protected] static void hci_state_reset(); 85758b46ceSmatthias.ringwald 8606b35ec0Smatthias.ringwald // the STACK is here 873a9fb326S[email protected] #ifndef HAVE_MALLOC 883a9fb326S[email protected] static hci_stack_t hci_stack_static; 893a9fb326S[email protected] #endif 903a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 9116833f0aSmatthias.ringwald 9266fb9560S[email protected] // test helper 9366fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 9466fb9560S[email protected] 9596a45072S[email protected] /** 9696a45072S[email protected] * create connection for given address 9796a45072S[email protected] * 9896a45072S[email protected] * @return connection OR NULL, if no memory left 9996a45072S[email protected] */ 10096a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 1015127cc62S[email protected] log_info("create_connection_for_addr %s", bd_addr_to_str(addr)); 102bb69aaaeS[email protected] hci_connection_t * conn = btstack_memory_hci_connection_get(); 10396a45072S[email protected] if (!conn) return NULL; 104c91d150bS[email protected] memset(conn, 0, sizeof(hci_connection_t)); 10596a45072S[email protected] BD_ADDR_COPY(conn->address, addr); 10696a45072S[email protected] conn->address_type = addr_type; 10796a45072S[email protected] conn->con_handle = 0xffff; 10896a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 10996a45072S[email protected] conn->bonding_flags = 0; 11096a45072S[email protected] conn->requested_security_level = LEVEL_0; 11196a45072S[email protected] linked_item_set_user(&conn->timeout.item, conn); 11296a45072S[email protected] conn->timeout.process = hci_connection_timeout_handler; 11396a45072S[email protected] hci_connection_timestamp(conn); 11496a45072S[email protected] conn->acl_recombination_length = 0; 11596a45072S[email protected] conn->acl_recombination_pos = 0; 11696a45072S[email protected] conn->num_acl_packets_sent = 0; 117da886c03S[email protected] conn->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 11896a45072S[email protected] linked_list_add(&hci_stack->connections, (linked_item_t *) conn); 11996a45072S[email protected] return conn; 12096a45072S[email protected] } 12166fb9560S[email protected] 122da886c03S[email protected] 123da886c03S[email protected] /** 124da886c03S[email protected] * get le connection parameter range 125da886c03S[email protected] * 126da886c03S[email protected] * @return le connection parameter range struct 127da886c03S[email protected] */ 128da886c03S[email protected] le_connection_parameter_range_t gap_le_get_connection_parameter_range(){ 129da886c03S[email protected] return hci_stack->le_connection_parameter_range; 130da886c03S[email protected] } 131da886c03S[email protected] 132da886c03S[email protected] /** 133da886c03S[email protected] * set le connection parameter range 134da886c03S[email protected] * 135da886c03S[email protected] */ 136da886c03S[email protected] 137da886c03S[email protected] void gap_le_set_connection_parameter_range(le_connection_parameter_range_t range){ 138da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_interval_min; 139da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_interval_max; 140da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_min = range.le_conn_latency_min; 141da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_max = range.le_conn_latency_max; 142da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_min = range.le_supervision_timeout_min; 143da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_max = range.le_supervision_timeout_max; 144da886c03S[email protected] } 145da886c03S[email protected] 146da886c03S[email protected] /** 147da886c03S[email protected] * get hci connections iterator 148da886c03S[email protected] * 149da886c03S[email protected] * @return hci connections iterator 150da886c03S[email protected] */ 151da886c03S[email protected] 152da886c03S[email protected] void hci_connections_get_iterator(linked_list_iterator_t *it){ 153da886c03S[email protected] linked_list_iterator_init(it, &hci_stack->connections); 154da886c03S[email protected] } 155da886c03S[email protected] 15697addcc5Smatthias.ringwald /** 157ee091cf1Smatthias.ringwald * get connection for a given handle 158ee091cf1Smatthias.ringwald * 159ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 160ee091cf1Smatthias.ringwald */ 1615061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 162da886c03S[email protected] linked_list_iterator_t it; 163da886c03S[email protected] linked_list_iterator_init(&it, &hci_stack->connections); 164da886c03S[email protected] while (linked_list_iterator_has_next(&it)){ 165da886c03S[email protected] hci_connection_t * item = (hci_connection_t *) linked_list_iterator_next(&it); 1663ac2fe56S[email protected] if ( item->con_handle == con_handle ) { 167da886c03S[email protected] return item; 168ee091cf1Smatthias.ringwald } 169ee091cf1Smatthias.ringwald } 170ee091cf1Smatthias.ringwald return NULL; 171ee091cf1Smatthias.ringwald } 172ee091cf1Smatthias.ringwald 17396a45072S[email protected] /** 17496a45072S[email protected] * get connection for given address 17596a45072S[email protected] * 17696a45072S[email protected] * @return connection OR NULL, if not found 17796a45072S[email protected] */ 17896a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){ 179da886c03S[email protected] linked_list_iterator_t it; 180da886c03S[email protected] linked_list_iterator_init(&it, &hci_stack->connections); 181da886c03S[email protected] while (linked_list_iterator_has_next(&it)){ 182da886c03S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_list_iterator_next(&it); 18396a45072S[email protected] if (connection->address_type != addr_type) continue; 18496a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 18562bda3fbS[email protected] return connection; 18662bda3fbS[email protected] } 18762bda3fbS[email protected] return NULL; 18862bda3fbS[email protected] } 18962bda3fbS[email protected] 1902b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 19128ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 192c785ef68Smatthias.ringwald #ifdef HAVE_TIME 193ee091cf1Smatthias.ringwald struct timeval tv; 194ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 195c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 196ee091cf1Smatthias.ringwald // connections might be timed out 197ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 198ee091cf1Smatthias.ringwald } 1992b12a0b9Smatthias.ringwald #endif 200e5780900Smatthias.ringwald #ifdef HAVE_TICK 201c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 202c785ef68Smatthias.ringwald // connections might be timed out 203c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 204c785ef68Smatthias.ringwald } 205c785ef68Smatthias.ringwald #endif 206c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 207c785ef68Smatthias.ringwald run_loop_add_timer(timer); 208c785ef68Smatthias.ringwald } 209ee091cf1Smatthias.ringwald 210ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 211c7492964Smatthias.ringwald #ifdef HAVE_TIME 212ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 213c7492964Smatthias.ringwald #endif 214e5780900Smatthias.ringwald #ifdef HAVE_TICK 215c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 216c785ef68Smatthias.ringwald #endif 217ee091cf1Smatthias.ringwald } 218ee091cf1Smatthias.ringwald 21906b35ec0Smatthias.ringwald 22028ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 22128ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 22228ca2b46S[email protected] } 22328ca2b46S[email protected] 22428ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 22528ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 22628ca2b46S[email protected] } 22728ca2b46S[email protected] 22828ca2b46S[email protected] 22943bfb1bdSmatthias.ringwald /** 23080ca58a0Smatthias.ringwald * add authentication flags and reset timer 23196a45072S[email protected] * @note: assumes classic connection 2327fde4af9Smatthias.ringwald */ 2337fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 2347fde4af9Smatthias.ringwald bd_addr_t addr; 2357fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 23696a45072S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 2377fde4af9Smatthias.ringwald if (conn) { 23828ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 23980ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 2407fde4af9Smatthias.ringwald } 2417fde4af9Smatthias.ringwald } 2427fde4af9Smatthias.ringwald 24380ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 2445061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 24580ca58a0Smatthias.ringwald if (!conn) return 0; 2466724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 2476724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 2486724cd9eS[email protected] return 0; 24980ca58a0Smatthias.ringwald } 25080ca58a0Smatthias.ringwald 251c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 2523a9fb326S[email protected] if (hci_stack->remote_device_db) { 2533a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(addr); 254c12e46e7Smatthias.ringwald } 255c12e46e7Smatthias.ringwald } 256c12e46e7Smatthias.ringwald 2570bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){ 2580bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 2590bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 2600bf6344aS[email protected] } 2610bf6344aS[email protected] 2627fde4af9Smatthias.ringwald 2637fde4af9Smatthias.ringwald /** 26443bfb1bdSmatthias.ringwald * count connections 26543bfb1bdSmatthias.ringwald */ 26640d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 26756c253c9Smatthias.ringwald int count = 0; 26843bfb1bdSmatthias.ringwald linked_item_t *it; 2693a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 27043bfb1bdSmatthias.ringwald return count; 27143bfb1bdSmatthias.ringwald } 272c8e4258aSmatthias.ringwald 27397addcc5Smatthias.ringwald /** 274ba681a6cSmatthias.ringwald * Dummy handler called by HCI 27516833f0aSmatthias.ringwald */ 2762718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 27716833f0aSmatthias.ringwald } 27816833f0aSmatthias.ringwald 279998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2805061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 281998906cdSmatthias.ringwald if (!connection) { 2829da54300S[email protected] log_error("hci_number_outgoing_packets: connection for handle %u does not exist!", handle); 283998906cdSmatthias.ringwald return 0; 284998906cdSmatthias.ringwald } 285998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 286998906cdSmatthias.ringwald } 287998906cdSmatthias.ringwald 288e79abdd6S[email protected] uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 289ee303eddS[email protected] 290ee303eddS[email protected] int num_packets_sent_classic = 0; 291ee303eddS[email protected] int num_packets_sent_le = 0; 292ee303eddS[email protected] 293ee303eddS[email protected] bd_addr_type_t address_type = BD_ADDR_TYPE_UNKNOWN; 294ee303eddS[email protected] 295998906cdSmatthias.ringwald linked_item_t *it; 2963a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 297998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 298ee303eddS[email protected] if (connection->address_type == BD_ADDR_TYPE_CLASSIC){ 299ee303eddS[email protected] num_packets_sent_classic += connection->num_acl_packets_sent; 300ee303eddS[email protected] } else { 301ee303eddS[email protected] num_packets_sent_le += connection->num_acl_packets_sent; 302ee303eddS[email protected] } 303ccda6e14S[email protected] // ignore connections that are not open, e.g., in state RECEIVED_DISCONNECTION_COMPLETE 304ccda6e14S[email protected] if (connection->con_handle == con_handle && connection->state == OPEN){ 305ee303eddS[email protected] address_type = connection->address_type; 306ee303eddS[email protected] } 307ee303eddS[email protected] } 308ee303eddS[email protected] 309ee303eddS[email protected] int free_slots_classic = hci_stack->acl_packets_total_num - num_packets_sent_classic; 310ee303eddS[email protected] int free_slots_le = 0; 311ee303eddS[email protected] 312ee303eddS[email protected] if (free_slots_classic < 0){ 3139da54300S[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); 314998906cdSmatthias.ringwald return 0; 315998906cdSmatthias.ringwald } 316ee303eddS[email protected] 317ee303eddS[email protected] if (hci_stack->le_acl_packets_total_num){ 318ee303eddS[email protected] // if we have LE slots, they are used 319ee303eddS[email protected] free_slots_le = hci_stack->le_acl_packets_total_num - num_packets_sent_le; 320ee303eddS[email protected] if (free_slots_le < 0){ 3219da54300S[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); 322ee303eddS[email protected] return 0; 323998906cdSmatthias.ringwald } 324ee303eddS[email protected] } else { 325ee303eddS[email protected] // otherwise, classic slots are used for LE, too 326ee303eddS[email protected] free_slots_classic -= num_packets_sent_le; 327ee303eddS[email protected] if (free_slots_classic < 0){ 3289da54300S[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); 329ee303eddS[email protected] return 0; 330ee303eddS[email protected] } 331ee303eddS[email protected] } 332ee303eddS[email protected] 333ee303eddS[email protected] switch (address_type){ 334ee303eddS[email protected] case BD_ADDR_TYPE_UNKNOWN: 3359da54300S[email protected] log_error("hci_number_free_acl_slots: handle 0x%04x not in connection list", con_handle); 336ee303eddS[email protected] return 0; 337ee303eddS[email protected] 338ee303eddS[email protected] case BD_ADDR_TYPE_CLASSIC: 339ee303eddS[email protected] return free_slots_classic; 340ee303eddS[email protected] 341ee303eddS[email protected] default: 342cb00d3aaS[email protected] if (hci_stack->le_acl_packets_total_num){ 343ee303eddS[email protected] return free_slots_le; 344ee303eddS[email protected] } 345cb00d3aaS[email protected] return free_slots_classic; 346cb00d3aaS[email protected] } 347998906cdSmatthias.ringwald } 348998906cdSmatthias.ringwald 349ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 350ac928cc2S[email protected] int hci_can_send_command_packet_now(void){ 351ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 352ac928cc2S[email protected] 353ac928cc2S[email protected] // check for async hci transport implementations 354ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 355ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 356ac928cc2S[email protected] return 0; 357ac928cc2S[email protected] } 358ac928cc2S[email protected] } 359ac928cc2S[email protected] 360ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 361ac928cc2S[email protected] } 362ac928cc2S[email protected] 363ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 364ac928cc2S[email protected] // check for async hci transport implementations 365ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 366ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){ 367ac928cc2S[email protected] return 0; 368ac928cc2S[email protected] } 369ac928cc2S[email protected] } 370e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 371ac928cc2S[email protected] } 372ac928cc2S[email protected] 373ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 374ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 375ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 3766b4af23dS[email protected] } 3776b4af23dS[email protected] 378c8b9416aS[email protected] // used for internal checks in l2cap[-le].c 379c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 380c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 381c8b9416aS[email protected] } 382c8b9416aS[email protected] 3836b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 3846b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 3859d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 3869d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 3879d14b626S[email protected] return 0; 3889d14b626S[email protected] } 3896b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 3906b4af23dS[email protected] return 1; 3916b4af23dS[email protected] } 3926b4af23dS[email protected] 39368a0fcf7S[email protected] void hci_release_packet_buffer(void){ 39468a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 39568a0fcf7S[email protected] } 39668a0fcf7S[email protected] 3976b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 3986b4af23dS[email protected] int hci_transport_synchronous(void){ 3996b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 4006b4af23dS[email protected] } 4016b4af23dS[email protected] 4026c26b087S[email protected] uint16_t hci_max_acl_le_data_packet_length(void){ 4036c26b087S[email protected] return hci_stack->le_data_packets_length > 0 ? hci_stack->le_data_packets_length : hci_stack->acl_data_packet_length; 4046c26b087S[email protected] } 4056c26b087S[email protected] 406452cf3bbS[email protected] static int hci_send_acl_packet_fragments(hci_connection_t *connection){ 407452cf3bbS[email protected] 408452cf3bbS[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); 409452cf3bbS[email protected] 410452cf3bbS[email protected] // max ACL data packet length depends on connection type (LE vs. Classic) and available buffers 411452cf3bbS[email protected] uint16_t max_acl_data_packet_length = hci_stack->acl_data_packet_length; 412452cf3bbS[email protected] if (hci_is_le_connection(connection) && hci_stack->le_data_packets_length > 0){ 413452cf3bbS[email protected] max_acl_data_packet_length = hci_stack->le_data_packets_length; 414452cf3bbS[email protected] } 415452cf3bbS[email protected] 416452cf3bbS[email protected] // testing: reduce buffer to minimum 417452cf3bbS[email protected] // max_acl_data_packet_length = 52; 418452cf3bbS[email protected] 419452cf3bbS[email protected] int err; 420452cf3bbS[email protected] // multiple packets could be send on a synchronous HCI transport 421452cf3bbS[email protected] while (1){ 422452cf3bbS[email protected] 423452cf3bbS[email protected] // get current data 424452cf3bbS[email protected] const uint16_t acl_header_pos = hci_stack->acl_fragmentation_pos - 4; 425452cf3bbS[email protected] int current_acl_data_packet_length = hci_stack->acl_fragmentation_total_size - hci_stack->acl_fragmentation_pos; 426452cf3bbS[email protected] int more_fragments = 0; 427452cf3bbS[email protected] 428452cf3bbS[email protected] // if ACL packet is larger than Bluetooth packet buffer, only send max_acl_data_packet_length 429452cf3bbS[email protected] if (current_acl_data_packet_length > max_acl_data_packet_length){ 430452cf3bbS[email protected] more_fragments = 1; 431452cf3bbS[email protected] current_acl_data_packet_length = max_acl_data_packet_length; 432452cf3bbS[email protected] } 433452cf3bbS[email protected] 434452cf3bbS[email protected] // copy handle_and_flags if not first fragment and update packet boundary flags to be 01 (continuing fragmnent) 435452cf3bbS[email protected] if (acl_header_pos > 0){ 436452cf3bbS[email protected] uint16_t handle_and_flags = READ_BT_16(hci_stack->hci_packet_buffer, 0); 437452cf3bbS[email protected] handle_and_flags = (handle_and_flags & 0xcfff) | (1 << 12); 438452cf3bbS[email protected] bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos, handle_and_flags); 439452cf3bbS[email protected] } 440452cf3bbS[email protected] 441452cf3bbS[email protected] // update header len 442452cf3bbS[email protected] bt_store_16(hci_stack->hci_packet_buffer, acl_header_pos + 2, current_acl_data_packet_length); 443452cf3bbS[email protected] 444452cf3bbS[email protected] // count packet 445452cf3bbS[email protected] connection->num_acl_packets_sent++; 446452cf3bbS[email protected] 447452cf3bbS[email protected] // send packet 448452cf3bbS[email protected] uint8_t * packet = &hci_stack->hci_packet_buffer[acl_header_pos]; 449452cf3bbS[email protected] const int size = current_acl_data_packet_length + 4; 4505bb5bc3eS[email protected] hci_dump_packet(HCI_ACL_DATA_PACKET, 0, packet, size); 451452cf3bbS[email protected] err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 452452cf3bbS[email protected] 453452cf3bbS[email protected] // done yet? 454452cf3bbS[email protected] if (!more_fragments) break; 455452cf3bbS[email protected] 456452cf3bbS[email protected] // update start of next fragment to send 457452cf3bbS[email protected] hci_stack->acl_fragmentation_pos += current_acl_data_packet_length; 458452cf3bbS[email protected] 459452cf3bbS[email protected] // can send more? 460452cf3bbS[email protected] if (!hci_can_send_prepared_acl_packet_now(connection->con_handle)) return err; 461452cf3bbS[email protected] } 462452cf3bbS[email protected] 463452cf3bbS[email protected] // done 464452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 0; 465452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = 0; 466452cf3bbS[email protected] 467d051460cS[email protected] // release buffer now for synchronous transport 468203bace6S[email protected] if (hci_transport_synchronous()){ 469452cf3bbS[email protected] hci_release_packet_buffer(); 470452cf3bbS[email protected] } 471452cf3bbS[email protected] 472452cf3bbS[email protected] return err; 473452cf3bbS[email protected] } 474452cf3bbS[email protected] 475826f7347S[email protected] // pre: caller has reserved the packet buffer 476826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 4777856c818Smatthias.ringwald 478452cf3bbS[email protected] // log_info("hci_send_acl_packet_buffer size %u", size); 479452cf3bbS[email protected] 480826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 481826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 482826f7347S[email protected] return 0; 483826f7347S[email protected] } 484826f7347S[email protected] 485d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 486d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 487d713a683S[email protected] 488826f7347S[email protected] // check for free places on Bluetooth module 489d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 490826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 49197b61c7bS[email protected] hci_release_packet_buffer(); 49297b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 49397b61c7bS[email protected] } 4946218e6f1Smatthias.ringwald 4955061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 49697b61c7bS[email protected] if (!connection) { 4975fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 49897b61c7bS[email protected] hci_release_packet_buffer(); 49997b61c7bS[email protected] return 0; 50097b61c7bS[email protected] } 50156cf178bSmatthias.ringwald hci_connection_timestamp(connection); 50256cf178bSmatthias.ringwald 503452cf3bbS[email protected] // hci_dump_packet( HCI_ACL_DATA_PACKET, 0, packet, size); 5047856c818Smatthias.ringwald 505452cf3bbS[email protected] // setup data 506452cf3bbS[email protected] hci_stack->acl_fragmentation_total_size = size; 507452cf3bbS[email protected] hci_stack->acl_fragmentation_pos = 4; // start of L2CAP packet 5086218e6f1Smatthias.ringwald 509452cf3bbS[email protected] return hci_send_acl_packet_fragments(connection); 510ee091cf1Smatthias.ringwald } 511ee091cf1Smatthias.ringwald 51216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 5137856c818Smatthias.ringwald 514e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 515e76a89eeS[email protected] 5167856c818Smatthias.ringwald // get info 5177856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 5185061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 5197856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 5207856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 5217856c818Smatthias.ringwald 5227856c818Smatthias.ringwald // ignore non-registered handle 5237856c818Smatthias.ringwald if (!conn){ 5249da54300S[email protected] log_error( "hci.c: acl_handler called with non-registered handle %u!" , con_handle); 5257856c818Smatthias.ringwald return; 5267856c818Smatthias.ringwald } 5277856c818Smatthias.ringwald 528e76a89eeS[email protected] // assert packet is complete 5299ecc3e17S[email protected] if (acl_length + 4 != size){ 530e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 531e76a89eeS[email protected] return; 532e76a89eeS[email protected] } 533e76a89eeS[email protected] 5347856c818Smatthias.ringwald // update idle timestamp 5357856c818Smatthias.ringwald hci_connection_timestamp(conn); 5367856c818Smatthias.ringwald 5377856c818Smatthias.ringwald // handle different packet types 5387856c818Smatthias.ringwald switch (acl_flags & 0x03) { 5397856c818Smatthias.ringwald 5407856c818Smatthias.ringwald case 0x01: // continuation fragment 5417856c818Smatthias.ringwald 5420ca847afS[email protected] // sanity checks 5437856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 5449da54300S[email protected] log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x", con_handle); 5457856c818Smatthias.ringwald return; 5467856c818Smatthias.ringwald } 5470ca847afS[email protected] if (conn->acl_recombination_pos + acl_length > 4 + HCI_ACL_BUFFER_SIZE){ 5480ca847afS[email protected] log_error( "ACL Cont Fragment to large: combined packet %u > buffer size %u for handle 0x%02x", 5490ca847afS[email protected] conn->acl_recombination_pos + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 5500ca847afS[email protected] conn->acl_recombination_pos = 0; 5510ca847afS[email protected] return; 5520ca847afS[email protected] } 5537856c818Smatthias.ringwald 5547856c818Smatthias.ringwald // append fragment payload (header already stored) 555ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE + conn->acl_recombination_pos], &packet[4], acl_length ); 5567856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 5577856c818Smatthias.ringwald 5589da54300S[email protected] // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u", acl_length, 559decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 5607856c818Smatthias.ringwald 5617856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 5627856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 5637856c818Smatthias.ringwald 564ec6321eeS[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, &conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], conn->acl_recombination_pos); 5657856c818Smatthias.ringwald // reset recombination buffer 5667856c818Smatthias.ringwald conn->acl_recombination_length = 0; 5677856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 5687856c818Smatthias.ringwald } 5697856c818Smatthias.ringwald break; 5707856c818Smatthias.ringwald 5717856c818Smatthias.ringwald case 0x02: { // first fragment 5727856c818Smatthias.ringwald 57323a77e1aS[email protected] // sanity check 57423a77e1aS[email protected] if (conn->acl_recombination_pos) { 57523a77e1aS[email protected] log_error( "ACL First Fragment but data in buffer for handle 0x%02x, dropping stale fragments", con_handle); 57623a77e1aS[email protected] conn->acl_recombination_pos = 0; 57723a77e1aS[email protected] } 57823a77e1aS[email protected] 5797856c818Smatthias.ringwald // peek into L2CAP packet! 5807856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 5817856c818Smatthias.ringwald 5829da54300S[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u", acl_length, l2cap_length); 583decc01a8Smatthias.ringwald 5847856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 5857856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 5867856c818Smatthias.ringwald 5877856c818Smatthias.ringwald // forward fragment as L2CAP packet 5883a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 5897856c818Smatthias.ringwald 5907856c818Smatthias.ringwald } else { 5910ca847afS[email protected] 5920ca847afS[email protected] if (acl_length > HCI_ACL_BUFFER_SIZE){ 5930ca847afS[email protected] log_error( "ACL First Fragment to large: fragment %u > buffer size %u for handle 0x%02x", 5940ca847afS[email protected] 4 + acl_length, 4 + HCI_ACL_BUFFER_SIZE, con_handle); 5950ca847afS[email protected] return; 5960ca847afS[email protected] } 5970ca847afS[email protected] 5987856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 599ec6321eeS[email protected] memcpy(&conn->acl_recombination_buffer[HCI_INCOMING_PRE_BUFFER_SIZE], packet, acl_length + 4); 6007856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 6017856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 602ec6321eeS[email protected] bt_store_16(conn->acl_recombination_buffer, HCI_INCOMING_PRE_BUFFER_SIZE + 2, l2cap_length +4); 6037856c818Smatthias.ringwald } 6047856c818Smatthias.ringwald break; 6057856c818Smatthias.ringwald 6067856c818Smatthias.ringwald } 6077856c818Smatthias.ringwald default: 6089da54300S[email protected] log_error( "hci.c: acl_handler called with invalid packet boundary flags %u", acl_flags & 0x03); 6097856c818Smatthias.ringwald return; 6107856c818Smatthias.ringwald } 61194ab26f8Smatthias.ringwald 61294ab26f8Smatthias.ringwald // execute main loop 61394ab26f8Smatthias.ringwald hci_run(); 61416833f0aSmatthias.ringwald } 61522909952Smatthias.ringwald 61667a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 6179da54300S[email protected] log_info("Connection closed: handle 0x%x, %s", conn->con_handle, bd_addr_to_str(conn->address)); 6183c4d4b90Smatthias.ringwald 619c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 620c785ef68Smatthias.ringwald 6213a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 622a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 6233c4d4b90Smatthias.ringwald 6243c4d4b90Smatthias.ringwald // now it's gone 625c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 626c7e0c5f6Smatthias.ringwald } 627c7e0c5f6Smatthias.ringwald 6280c042179S[email protected] static const uint16_t packet_type_sizes[] = { 6298f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 6308f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 6318f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 6328f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 6338f8108aaSmatthias.ringwald }; 63465389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 63565389bfcS[email protected] 0, // 3 slot packets 63665389bfcS[email protected] 1, // 5 slot packets 63765389bfcS[email protected] 25, // EDR 2 mpbs 63865389bfcS[email protected] 26, // EDR 3 mbps 63965389bfcS[email protected] 39, // 3 slot EDR packts 64065389bfcS[email protected] 40, // 5 slot EDR packet 64165389bfcS[email protected] }; 64265389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 64365389bfcS[email protected] 0x0f00, // 3 slot packets 64465389bfcS[email protected] 0xf000, // 5 slot packets 64565389bfcS[email protected] 0x1102, // EDR 2 mpbs 64665389bfcS[email protected] 0x2204, // EDR 3 mbps 64765389bfcS[email protected] 0x0300, // 3 slot EDR packts 64865389bfcS[email protected] 0x3000, // 5 slot EDR packet 64965389bfcS[email protected] }; 6508f8108aaSmatthias.ringwald 65165389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 65265389bfcS[email protected] // enable packet types based on size 6538f8108aaSmatthias.ringwald uint16_t packet_types = 0; 654f16a69bbS[email protected] unsigned int i; 6558f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 6568f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 6578f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 6588f8108aaSmatthias.ringwald packet_types |= 1 << i; 6598f8108aaSmatthias.ringwald } 6608f8108aaSmatthias.ringwald } 66165389bfcS[email protected] // disable packet types due to missing local supported features 66265389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 66365389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 66465389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 66565389bfcS[email protected] if (feature_set) continue; 66665389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 66765389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 66865389bfcS[email protected] } 6698f8108aaSmatthias.ringwald // flip bits for "may not be used" 6708f8108aaSmatthias.ringwald packet_types ^= 0x3306; 6718f8108aaSmatthias.ringwald return packet_types; 6728f8108aaSmatthias.ringwald } 6738f8108aaSmatthias.ringwald 6748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 6753a9fb326S[email protected] return hci_stack->packet_types; 6768f8108aaSmatthias.ringwald } 6778f8108aaSmatthias.ringwald 678facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 6797dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 6803a9fb326S[email protected] return hci_stack->hci_packet_buffer; 6817dc17943Smatthias.ringwald } 6827dc17943Smatthias.ringwald 683f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 6843a9fb326S[email protected] return hci_stack->acl_data_packet_length; 6857dc17943Smatthias.ringwald } 6867dc17943Smatthias.ringwald 6876ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 6886ac9a97eS[email protected] // No. 54, byte 6, bit 6 6896ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 6906ac9a97eS[email protected] } 6916ac9a97eS[email protected] 692f5d8d141S[email protected] int hci_ssp_supported(void){ 6936ac9a97eS[email protected] // No. 51, byte 6, bit 3 6943a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 695f5d8d141S[email protected] } 696f5d8d141S[email protected] 697f5d8d141S[email protected] int hci_classic_supported(void){ 6986ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 6993a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 700f5d8d141S[email protected] } 701f5d8d141S[email protected] 702f5d8d141S[email protected] int hci_le_supported(void){ 703f5d8d141S[email protected] #ifdef HAVE_BLE 7046ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 7053a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 706f5d8d141S[email protected] #else 707f5d8d141S[email protected] return 0; 708f5d8d141S[email protected] #endif 709f5d8d141S[email protected] } 710f5d8d141S[email protected] 71169a97523S[email protected] // get addr type and address used in advertisement packets 71218904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 7133a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 7143a9fb326S[email protected] if (hci_stack->adv_addr_type){ 7153a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 71669a97523S[email protected] } else { 7173a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 71869a97523S[email protected] } 71969a97523S[email protected] } 72069a97523S[email protected] 721b2f949feS[email protected] #ifdef HAVE_BLE 722e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){ 723d1dc057bS[email protected] int offset = 3; 724d1dc057bS[email protected] int num_reports = packet[offset]; 725d1dc057bS[email protected] offset += 1; 726d1dc057bS[email protected] 72757c9da5bS[email protected] int i; 7284f4b43f3S[email protected] log_info("HCI: handle adv report with num reports: %d", num_reports); 72903fbe9c6S[email protected] uint8_t event[12 + LE_ADVERTISING_DATA_SIZE]; // use upper bound to avoid var size automatic var 73057c9da5bS[email protected] for (i=0; i<num_reports;i++){ 731210c6774S[email protected] uint8_t data_length = packet[offset + 8]; 732a0aac9c4S[email protected] uint8_t event_size = 10 + data_length; 733d1dc057bS[email protected] int pos = 0; 7342b552b23S[email protected] event[pos++] = GAP_LE_ADVERTISING_REPORT; 73557c9da5bS[email protected] event[pos++] = event_size; 736210c6774S[email protected] memcpy(&event[pos], &packet[offset], 1+1+6); // event type + address type + address 737d1dc057bS[email protected] offset += 8; 738d1dc057bS[email protected] pos += 8; 739d1dc057bS[email protected] event[pos++] = packet[offset + 1 + data_length]; // rssi 740d1dc057bS[email protected] event[pos++] = packet[offset++]; //data_length; 741d1dc057bS[email protected] memcpy(&event[pos], &packet[offset], data_length); 74257c9da5bS[email protected] pos += data_length; 743d1dc057bS[email protected] offset += data_length + 1; // rssi 74403fbe9c6S[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, pos); 74503fbe9c6S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, pos); 74657c9da5bS[email protected] } 74757c9da5bS[email protected] } 748b2f949feS[email protected] #endif 74957c9da5bS[email protected] 7506155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 751a2481739S[email protected] uint8_t command_completed = 0; 752a2481739S[email protected] if ((hci_stack->substate % 2) == 0) return; 7536155b3d3S[email protected] // odd: waiting for event 7546155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 7556155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,3); 7566155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 757a2481739S[email protected] command_completed = 1; 758f456b2d0S[email protected] log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate >> 1); 7596155b3d3S[email protected] } else { 7606155b3d3S[email protected] log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 7616155b3d3S[email protected] } 7626155b3d3S[email protected] } 7636155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_STATUS){ 7646155b3d3S[email protected] uint8_t status = packet[2]; 7656155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,4); 7666155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 7676155b3d3S[email protected] if (status){ 768a2481739S[email protected] command_completed = 1; 769f456b2d0S[email protected] log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate >> 1); 7706155b3d3S[email protected] } else { 7716155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 7726155b3d3S[email protected] } 7736155b3d3S[email protected] } else { 7746155b3d3S[email protected] log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 7756155b3d3S[email protected] } 7766155b3d3S[email protected] } 777a2481739S[email protected] 778a2481739S[email protected] if (!command_completed) return; 779a2481739S[email protected] 780a2481739S[email protected] switch(hci_stack->substate >> 1){ 781a2481739S[email protected] default: 782a2481739S[email protected] hci_stack->substate++; 783a2481739S[email protected] break; 7846155b3d3S[email protected] } 7856155b3d3S[email protected] } 7866155b3d3S[email protected] 7876155b3d3S[email protected] static void hci_initializing_state_machine(){ 7886155b3d3S[email protected] if (hci_stack->substate % 2) { 7896155b3d3S[email protected] // odd: waiting for command completion 7906155b3d3S[email protected] return; 7916155b3d3S[email protected] } 792f456b2d0S[email protected] // log_info("hci_init: substate %u", hci_stack->substate >> 1); 7936155b3d3S[email protected] switch (hci_stack->substate >> 1){ 7946155b3d3S[email protected] case 0: // RESET 7956155b3d3S[email protected] hci_state_reset(); 7966155b3d3S[email protected] 7976155b3d3S[email protected] hci_send_cmd(&hci_reset); 7986155b3d3S[email protected] if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 7996155b3d3S[email protected] // skip baud change 800f456b2d0S[email protected] hci_stack->substate = 2 << 1; 8016155b3d3S[email protected] } 8026155b3d3S[email protected] break; 8036155b3d3S[email protected] case 1: // SEND BAUD CHANGE 8046155b3d3S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 80588789c61Smatthias.ringwald hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 8066155b3d3S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 8076155b3d3S[email protected] break; 8086155b3d3S[email protected] case 2: // LOCAL BAUD CHANGE 8096155b3d3S[email protected] log_info("Local baud rate change"); 8106155b3d3S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 8116155b3d3S[email protected] hci_stack->substate += 2; 8126155b3d3S[email protected] // break missing here for fall through 8136155b3d3S[email protected] 814f456b2d0S[email protected] case 3: // SET BD ADDR 815f456b2d0S[email protected] if ( hci_stack->custom_bd_addr_set && hci_stack->control && hci_stack->control->set_bd_addr_cmd){ 816f456b2d0S[email protected] log_info("Set Public BD ADDR to %s", bd_addr_to_str(hci_stack->custom_bd_addr)); 817f456b2d0S[email protected] hci_stack->control->set_bd_addr_cmd(hci_stack->config, hci_stack->custom_bd_addr, hci_stack->hci_packet_buffer); 818bc01039dS[email protected] hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 819f456b2d0S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 820f456b2d0S[email protected] break; 821f456b2d0S[email protected] } 822f456b2d0S[email protected] hci_stack->substate += 2; 823f456b2d0S[email protected] // break missing here for fall through 824f456b2d0S[email protected] 825f456b2d0S[email protected] case 4: 8266155b3d3S[email protected] log_info("Custom init"); 8276155b3d3S[email protected] // Custom initialization 8286155b3d3S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 8296155b3d3S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 8306155b3d3S[email protected] if (valid_cmd){ 8316155b3d3S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 8326155b3d3S[email protected] hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 8335bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, hci_stack->hci_packet_buffer, size); 8346155b3d3S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 835f456b2d0S[email protected] hci_stack->substate = 3 << 1; // more init commands 8366155b3d3S[email protected] break; 8376155b3d3S[email protected] } 8389da54300S[email protected] log_info("hci_run: init script done"); 8396155b3d3S[email protected] } 8406155b3d3S[email protected] // otherwise continue 8416155b3d3S[email protected] hci_send_cmd(&hci_read_bd_addr); 8426155b3d3S[email protected] break; 843f456b2d0S[email protected] case 5: 8446155b3d3S[email protected] hci_send_cmd(&hci_read_buffer_size); 8456155b3d3S[email protected] break; 846f456b2d0S[email protected] case 6: 8476155b3d3S[email protected] hci_send_cmd(&hci_read_local_supported_features); 8486155b3d3S[email protected] break; 849f456b2d0S[email protected] case 7: 8506155b3d3S[email protected] if (hci_le_supported()){ 8516155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 8526155b3d3S[email protected] } else { 853f456b2d0S[email protected] // Kensington Bluetooth 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 8546155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 8556155b3d3S[email protected] } 8566155b3d3S[email protected] 8576155b3d3S[email protected] // skip Classic init commands for LE only chipsets 8586155b3d3S[email protected] if (!hci_classic_supported()){ 8596155b3d3S[email protected] if (hci_le_supported()){ 860f456b2d0S[email protected] hci_stack->substate = 12 << 1; // skip all classic command 8616155b3d3S[email protected] } else { 8626155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 863f456b2d0S[email protected] hci_stack->substate = 15 << 1; // skip all 8646155b3d3S[email protected] } 8656155b3d3S[email protected] } 8666155b3d3S[email protected] break; 867f456b2d0S[email protected] case 8: 8686155b3d3S[email protected] if (hci_ssp_supported()){ 8696155b3d3S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 8706155b3d3S[email protected] break; 8716155b3d3S[email protected] } 8726155b3d3S[email protected] hci_stack->substate += 2; 8736155b3d3S[email protected] // break missing here for fall through 8746155b3d3S[email protected] 875f456b2d0S[email protected] case 9: 8766155b3d3S[email protected] // ca. 15 sec 8776155b3d3S[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 8786155b3d3S[email protected] break; 879f456b2d0S[email protected] case 10: 8806155b3d3S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 8816155b3d3S[email protected] break; 882f456b2d0S[email protected] case 11: 8836155b3d3S[email protected] if (hci_stack->local_name){ 8846155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 8856155b3d3S[email protected] } else { 8866155b3d3S[email protected] char hostname[30]; 8876155b3d3S[email protected] #ifdef EMBEDDED 8886155b3d3S[email protected] // BTstack-11:22:33:44:55:66 8896155b3d3S[email protected] strcpy(hostname, "BTstack "); 8906155b3d3S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 8916155b3d3S[email protected] log_info("---> Name %s", hostname); 8926155b3d3S[email protected] #else 8936155b3d3S[email protected] // hostname for POSIX systems 8946155b3d3S[email protected] gethostname(hostname, 30); 8956155b3d3S[email protected] hostname[29] = '\0'; 8966155b3d3S[email protected] #endif 8976155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hostname); 8986155b3d3S[email protected] } 8996155b3d3S[email protected] break; 900f456b2d0S[email protected] case 12: 9016155b3d3S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 9026155b3d3S[email protected] if (!hci_le_supported()){ 9036155b3d3S[email protected] // SKIP LE init for Classic only configuration 904f456b2d0S[email protected] hci_stack->substate = 15 << 1; 9056155b3d3S[email protected] } 9066155b3d3S[email protected] break; 9076155b3d3S[email protected] 9086155b3d3S[email protected] #ifdef HAVE_BLE 9096155b3d3S[email protected] // LE INIT 910f456b2d0S[email protected] case 13: 9116155b3d3S[email protected] hci_send_cmd(&hci_le_read_buffer_size); 9126155b3d3S[email protected] break; 913f456b2d0S[email protected] case 14: 9146155b3d3S[email protected] // LE Supported Host = 1, Simultaneous Host = 0 9156155b3d3S[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 9166155b3d3S[email protected] break; 917f456b2d0S[email protected] case 15: 9186155b3d3S[email protected] // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs 9196155b3d3S[email protected] hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0); 9206155b3d3S[email protected] break; 9216155b3d3S[email protected] #endif 9226155b3d3S[email protected] 9236155b3d3S[email protected] // DONE 924f456b2d0S[email protected] case 16: 9256155b3d3S[email protected] // done. 9266155b3d3S[email protected] hci_stack->state = HCI_STATE_WORKING; 9276155b3d3S[email protected] hci_emit_state(); 9286155b3d3S[email protected] break; 9296155b3d3S[email protected] default: 9306155b3d3S[email protected] break; 9316155b3d3S[email protected] } 9326155b3d3S[email protected] hci_stack->substate++; 9336155b3d3S[email protected] } 9346155b3d3S[email protected] 93574ec757aSmatthias.ringwald // avoid huge local variables 936223aafc1Smatthias.ringwald #ifndef EMBEDDED 93774ec757aSmatthias.ringwald static device_name_t device_name; 938223aafc1Smatthias.ringwald #endif 93916833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 940e76a89eeS[email protected] 941e76a89eeS[email protected] uint16_t event_length = packet[1]; 942e76a89eeS[email protected] 943e76a89eeS[email protected] // assert packet is complete 944e76a89eeS[email protected] if (size != event_length + 2){ 945e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 946e76a89eeS[email protected] return; 947e76a89eeS[email protected] } 948e76a89eeS[email protected] 9491281a47eSmatthias.ringwald bd_addr_t addr; 95096a45072S[email protected] bd_addr_type_t addr_type; 9512d00edd4Smatthias.ringwald uint8_t link_type; 952fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 9531f7b95a1Smatthias.ringwald hci_connection_t * conn; 95456cf178bSmatthias.ringwald int i; 95522909952Smatthias.ringwald 9569da54300S[email protected] // log_info("HCI:EVENT:%02x", packet[0]); 9575909f7f2Smatthias.ringwald 9586772a24cSmatthias.ringwald switch (packet[0]) { 95922909952Smatthias.ringwald 9606772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 9617ec5eeaaSmatthias.ringwald // get num cmd packets 9629da54300S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u", hci_stack->num_cmd_packets, packet[2]); 9633a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 9647ec5eeaaSmatthias.ringwald 965e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 966e2edc0c3Smatthias.ringwald // from offset 5 967e2edc0c3Smatthias.ringwald // status 9681d279b20Smatthias.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" 9693a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 970*a8b12447S[email protected] hci_stack->sco_data_packet_length = packet[8]; 971ee303eddS[email protected] hci_stack->acl_packets_total_num = packet[9]; 972*a8b12447S[email protected] hci_stack->sco_packets_total_num = packet[10]; 973*a8b12447S[email protected] 9743a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 975a7a04bd9Smatthias.ringwald // determine usable ACL payload size 9763a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 9773a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 9788f8108aaSmatthias.ringwald } 9799da54300S[email protected] log_info("hci_read_buffer_size: used size %u, count %u", 980ee303eddS[email protected] hci_stack->acl_data_packet_length, hci_stack->acl_packets_total_num); 981e2edc0c3Smatthias.ringwald } 98256cf178bSmatthias.ringwald } 98365a46ef3S[email protected] #ifdef HAVE_BLE 98465a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 985219eea5fS[email protected] hci_stack->le_data_packets_length = READ_BT_16(packet, 6); 986ee303eddS[email protected] hci_stack->le_acl_packets_total_num = packet[8]; 9876c26b087S[email protected] // determine usable ACL payload size 9886c26b087S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->le_data_packets_length){ 9896c26b087S[email protected] hci_stack->le_data_packets_length = HCI_ACL_PAYLOAD_SIZE; 9906c26b087S[email protected] } 9919da54300S[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); 99265a46ef3S[email protected] } 99365a46ef3S[email protected] #endif 994188981d2Smatthias.ringwald // Dump local address 995188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 9963a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 9979da54300S[email protected] log_info("Local Address, Status: 0x%02x: Addr: %s", 9983a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 999188981d2Smatthias.ringwald } 1000381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 10013a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1002381fbed8Smatthias.ringwald } 100365389bfcS[email protected] // Note: HCI init checks 1004559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 10053a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 1006559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 10073a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 10083a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 10093a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 10103a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 101165389bfcS[email protected] 1012a5a23fc2S[email protected] // determine usable ACL packet types based on host buffer size and supported features 1013a5a23fc2S[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]); 10143a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 1015f5d8d141S[email protected] 1016f5d8d141S[email protected] // Classic/LE 1017f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 1018559e517eS[email protected] } 101956cf178bSmatthias.ringwald break; 102056cf178bSmatthias.ringwald 10217ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 10227ec5eeaaSmatthias.ringwald // get num cmd packets 10239da54300S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u", hci_stack->num_cmd_packets, packet[3]); 10243a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 10257ec5eeaaSmatthias.ringwald break; 10267ec5eeaaSmatthias.ringwald 10272e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 10282e440c8aS[email protected] int offset = 3; 102956cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 10302e440c8aS[email protected] handle = READ_BT_16(packet, offset); 10312e440c8aS[email protected] offset += 2; 10322e440c8aS[email protected] uint16_t num_packets = READ_BT_16(packet, offset); 10332e440c8aS[email protected] offset += 2; 10342e440c8aS[email protected] 10355061f3afS[email protected] conn = hci_connection_for_handle(handle); 103656cf178bSmatthias.ringwald if (!conn){ 10379da54300S[email protected] log_error("hci_number_completed_packet lists unused con handle %u", handle); 103856cf178bSmatthias.ringwald continue; 103956cf178bSmatthias.ringwald } 104023bed257S[email protected] 104123bed257S[email protected] if (conn->num_acl_packets_sent >= num_packets){ 104256cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 104323bed257S[email protected] } else { 104423bed257S[email protected] log_error("hci_number_completed_packets, more slots freed then sent."); 104523bed257S[email protected] conn->num_acl_packets_sent = 0; 104623bed257S[email protected] } 10479da54300S[email protected] // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u", num_packets, handle, conn->num_acl_packets_sent); 104856cf178bSmatthias.ringwald } 10496772a24cSmatthias.ringwald break; 10502e440c8aS[email protected] } 10511f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 105237eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 105337eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 10542d00edd4Smatthias.ringwald link_type = packet[11]; 10559da54300S[email protected] log_info("Connection_incoming: %s, type %u", bd_addr_to_str(addr), link_type); 105637eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 105796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 10581f7b95a1Smatthias.ringwald if (!conn) { 105996a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 10601f7b95a1Smatthias.ringwald } 1061ce4c8fabSmatthias.ringwald if (!conn) { 1062ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 10633a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 10643a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 1065ce4c8fabSmatthias.ringwald break; 1066ce4c8fabSmatthias.ringwald } 106732ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 106832ab9390Smatthias.ringwald hci_run(); 106937eaa4cfSmatthias.ringwald } else { 1070ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 10713a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 10723a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 107337eaa4cfSmatthias.ringwald } 10741f7b95a1Smatthias.ringwald break; 10751f7b95a1Smatthias.ringwald 10766772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 1077fe1ed1b8Smatthias.ringwald // Connection management 1078fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 10799da54300S[email protected] log_info("Connection_complete (status=%u) %s", packet[2], bd_addr_to_str(addr)); 108096a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 108196a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 1082fe1ed1b8Smatthias.ringwald if (conn) { 1083b448a0e7Smatthias.ringwald if (!packet[2]){ 1084c8e4258aSmatthias.ringwald conn->state = OPEN; 1085fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 1086ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 1087ee091cf1Smatthias.ringwald 1088c785ef68Smatthias.ringwald // restart timer 1089c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 1090ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 1091c785ef68Smatthias.ringwald 10929da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 109343bfb1bdSmatthias.ringwald 109443bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 1095b448a0e7Smatthias.ringwald } else { 10961bd5283dS[email protected] int notify_dedicated_bonding_failed = conn->bonding_flags & BONDING_DEDICATED; 10971bd5283dS[email protected] uint8_t status = packet[2]; 10981bd5283dS[email protected] bd_addr_t bd_address; 10991bd5283dS[email protected] memcpy(&bd_address, conn->address, 6); 1100ad83dc6aS[email protected] 1101b448a0e7Smatthias.ringwald // connection failed, remove entry 11023a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 1103a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 1104c12e46e7Smatthias.ringwald 11051bd5283dS[email protected] // notify client if dedicated bonding 11061bd5283dS[email protected] if (notify_dedicated_bonding_failed){ 11071bd5283dS[email protected] log_info("hci notify_dedicated_bonding_failed"); 11081bd5283dS[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 11091bd5283dS[email protected] } 11101bd5283dS[email protected] 1111c12e46e7Smatthias.ringwald // if authentication error, also delete link key 1112c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 1113c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 1114c12e46e7Smatthias.ringwald } 1115fe1ed1b8Smatthias.ringwald } 1116fe1ed1b8Smatthias.ringwald } 11176772a24cSmatthias.ringwald break; 1118fe1ed1b8Smatthias.ringwald 1119afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 1120afd4e962S[email protected] handle = READ_BT_16(packet, 3); 1121afd4e962S[email protected] conn = hci_connection_for_handle(handle); 1122afd4e962S[email protected] if (!conn) break; 1123afd4e962S[email protected] if (!packet[2]){ 1124afd4e962S[email protected] uint8_t * features = &packet[5]; 1125afd4e962S[email protected] if (features[6] & (1 << 3)){ 1126afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 1127afd4e962S[email protected] } 1128afd4e962S[email protected] } 1129afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 1130ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 1131ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1132ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 1133ad83dc6aS[email protected] } 1134afd4e962S[email protected] break; 1135afd4e962S[email protected] 11367fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 11379da54300S[email protected] log_info("HCI_EVENT_LINK_KEY_REQUEST"); 11387fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 113974d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 11403a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 114132ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 114264472d52Smatthias.ringwald hci_run(); 1143d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 114429d53098Smatthias.ringwald return; 11457fde4af9Smatthias.ringwald 11469ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 114729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 114896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 11499ab95c90S[email protected] if (!conn) break; 11509ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 11517bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 11529ab95c90S[email protected] // Change Connection Encryption keeps link key type 11539ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 11549ab95c90S[email protected] conn->link_key_type = link_key_type; 11559ab95c90S[email protected] } 11563a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 11573a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 115829d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 11597fde4af9Smatthias.ringwald break; 11609ab95c90S[email protected] } 11617fde4af9Smatthias.ringwald 11627fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 11636724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 11644c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 11653a9fb326S[email protected] if (!hci_stack->bondable){ 1166899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1167f8fb5f6eS[email protected] hci_run(); 1168f8fb5f6eS[email protected] return; 11694c57c146S[email protected] } 1170d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 11713a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 1172d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 11733a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 11747fde4af9Smatthias.ringwald break; 11757fde4af9Smatthias.ringwald 11761d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 11771d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1178dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1179dbe1a790S[email protected] break; 1180dbe1a790S[email protected] 1181dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 11826724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 11833a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1184dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1185dbe1a790S[email protected] break; 1186dbe1a790S[email protected] 1187dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 11886724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 11893a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1190dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 11911d6b20aeS[email protected] break; 11921d6b20aeS[email protected] 1193f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1194f0944df2S[email protected] handle = READ_BT_16(packet, 3); 1195f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1196f0944df2S[email protected] if (!conn) break; 1197ad83dc6aS[email protected] if (packet[2] == 0) { 1198f0944df2S[email protected] if (packet[5]){ 1199f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1200f0944df2S[email protected] } else { 1201536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1202f0944df2S[email protected] } 1203ad83dc6aS[email protected] } 1204a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 1205f0944df2S[email protected] break; 1206f0944df2S[email protected] 12071eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 12081eb2563eS[email protected] handle = READ_BT_16(packet, 3); 12091eb2563eS[email protected] conn = hci_connection_for_handle(handle); 12101eb2563eS[email protected] if (!conn) break; 1211ad83dc6aS[email protected] 1212ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1213ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1214ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1215ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 12161bd5283dS[email protected] conn->bonding_status = packet[2]; 1217ad83dc6aS[email protected] break; 1218ad83dc6aS[email protected] } 1219ad83dc6aS[email protected] 1220b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 12211eb2563eS[email protected] // link key sufficient for requested security 12221eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1223ad83dc6aS[email protected] break; 1224ad83dc6aS[email protected] } 12251eb2563eS[email protected] // not enough 12261eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 12271eb2563eS[email protected] break; 122834d2123cS[email protected] 1229223aafc1Smatthias.ringwald #ifndef EMBEDDED 123074ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 12313a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 123274ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 123374ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 12345a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 12355a06394aSmatthias.ringwald for (i=0; i<248;i++){ 12365a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 12375a06394aSmatthias.ringwald packet[9+i] = 0; 12385a06394aSmatthias.ringwald break; 1239cdc9101dSmatthias.ringwald } 12405a06394aSmatthias.ringwald } 1241d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 124274ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 12433a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 124474ec757aSmatthias.ringwald break; 124574ec757aSmatthias.ringwald 124674ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 1247206a9031S[email protected] case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ 12483a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 124974ec757aSmatthias.ringwald // first send inq result packet 12503a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 125174ec757aSmatthias.ringwald // then send cached remote names 1252206a9031S[email protected] int offset = 3; 125374ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 1254206a9031S[email protected] bt_flip_addr(addr, &packet[offset]); 1255206a9031S[email protected] offset += 14; // 6 + 1 + 1 + 1 + 3 + 2; 12563a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 125774ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 125874ec757aSmatthias.ringwald } 125974ec757aSmatthias.ringwald } 126074ec757aSmatthias.ringwald return; 1261206a9031S[email protected] } 1262223aafc1Smatthias.ringwald #endif 126374ec757aSmatthias.ringwald 126486805605S[email protected] // HCI_EVENT_DISCONNECTION_COMPLETE 1265ccda6e14S[email protected] // has been split, to first notify stack before shutting connection down 1266ccda6e14S[email protected] // see end of function, too. 1267a4f30ec0S[email protected] case HCI_EVENT_DISCONNECTION_COMPLETE: 1268a4f30ec0S[email protected] if (packet[2]) break; // status != 0 1269ccda6e14S[email protected] handle = READ_BT_16(packet, 3); 1270ccda6e14S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 1271a4f30ec0S[email protected] if (!conn) break; // no conn struct anymore 1272ccda6e14S[email protected] conn->state = RECEIVED_DISCONNECTION_COMPLETE; 1273ccda6e14S[email protected] break; 12746772a24cSmatthias.ringwald 1275c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 12763a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 12773a9fb326S[email protected] (*hci_stack->control->hw_error)(); 12787586ee35S[email protected] } else { 12797586ee35S[email protected] // if no special requests, just reboot stack 12807586ee35S[email protected] hci_power_control_off(); 12817586ee35S[email protected] hci_power_control_on(); 1282c68bdf90Smatthias.ringwald } 1283c68bdf90Smatthias.ringwald break; 1284c68bdf90Smatthias.ringwald 12856b4af23dS[email protected] case DAEMON_EVENT_HCI_PACKET_SENT: 1286d051460cS[email protected] // release packet buffer only for asynchronous transport and if there are not further fragements 12874fa24b5fS[email protected] if (hci_transport_synchronous()) { 12884fa24b5fS[email protected] log_error("Synchronous HCI Transport shouldn't send DAEMON_EVENT_HCI_PACKET_SENT"); 12894fa24b5fS[email protected] return; // instead of break: to avoid re-entering hci_run() 12904fa24b5fS[email protected] } 1291d051460cS[email protected] if (hci_stack->acl_fragmentation_total_size) break; 1292d051460cS[email protected] hci_release_packet_buffer(); 12936b4af23dS[email protected] break; 12946b4af23dS[email protected] 12955909f7f2Smatthias.ringwald #ifdef HAVE_BLE 12965909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 12975909f7f2Smatthias.ringwald switch (packet[2]){ 129857c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 12999da54300S[email protected] log_info("advertising report received"); 13007bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 130157c9da5bS[email protected] le_handle_advertisement_report(packet, size); 13027bdc6798S[email protected] break; 13035909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 13045909f7f2Smatthias.ringwald // Connection management 13055909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 13067bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 13079da54300S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s", packet[3], addr_type, bd_addr_to_str(addr)); 13085909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 130996a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 13105909f7f2Smatthias.ringwald if (packet[3]){ 13115909f7f2Smatthias.ringwald if (conn){ 13125909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 13133a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 13145909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 13155909f7f2Smatthias.ringwald } 13165909f7f2Smatthias.ringwald // if authentication error, also delete link key 13175909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 13185909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 13195909f7f2Smatthias.ringwald } 13205909f7f2Smatthias.ringwald break; 13215909f7f2Smatthias.ringwald } 13225909f7f2Smatthias.ringwald if (!conn){ 132396a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 13245909f7f2Smatthias.ringwald } 13255909f7f2Smatthias.ringwald if (!conn){ 13265909f7f2Smatthias.ringwald // no memory 13275909f7f2Smatthias.ringwald break; 13285909f7f2Smatthias.ringwald } 13295909f7f2Smatthias.ringwald 13305909f7f2Smatthias.ringwald conn->state = OPEN; 13315909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 13325909f7f2Smatthias.ringwald 13335909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 13345909f7f2Smatthias.ringwald 13355909f7f2Smatthias.ringwald // restart timer 13365909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 13375909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 13385909f7f2Smatthias.ringwald 13399da54300S[email protected] log_info("New connection: handle %u, %s", conn->con_handle, bd_addr_to_str(conn->address)); 13405909f7f2Smatthias.ringwald 13415909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 13425909f7f2Smatthias.ringwald break; 13435909f7f2Smatthias.ringwald 13449da54300S[email protected] // log_info("LE buffer size: %u, count %u", READ_BT_16(packet,6), packet[8]); 134565a46ef3S[email protected] 13465909f7f2Smatthias.ringwald default: 13475909f7f2Smatthias.ringwald break; 13485909f7f2Smatthias.ringwald } 13495909f7f2Smatthias.ringwald break; 13505909f7f2Smatthias.ringwald #endif 13516772a24cSmatthias.ringwald default: 13526772a24cSmatthias.ringwald break; 1353fe1ed1b8Smatthias.ringwald } 1354fe1ed1b8Smatthias.ringwald 13553429f56bSmatthias.ringwald // handle BT initialization 13563a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 13576155b3d3S[email protected] hci_initializing_event_handler(packet, size); 1358c9af4d3fS[email protected] } 135922909952Smatthias.ringwald 136089db417bSmatthias.ringwald // help with BT sleep 13613a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 13623a9fb326S[email protected] && hci_stack->substate == 1 136389db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 13643a9fb326S[email protected] hci_stack->substate++; 136589db417bSmatthias.ringwald } 136689db417bSmatthias.ringwald 136786805605S[email protected] // notify upper stack 13683a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 136994ab26f8Smatthias.ringwald 137086805605S[email protected] // moved here to give upper stack a chance to close down everything with hci_connection_t intact 137186805605S[email protected] if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE){ 137286805605S[email protected] if (!packet[2]){ 137386805605S[email protected] handle = READ_BT_16(packet, 3); 137486805605S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 137586805605S[email protected] if (conn) { 137686805605S[email protected] uint8_t status = conn->bonding_status; 1377bb820044S[email protected] uint16_t flags = conn->bonding_flags; 137886805605S[email protected] bd_addr_t bd_address; 137986805605S[email protected] memcpy(&bd_address, conn->address, 6); 138086805605S[email protected] hci_shutdown_connection(conn); 1381bb820044S[email protected] // connection struct is gone, don't access anymore 1382bb820044S[email protected] if (flags & BONDING_EMIT_COMPLETE_ON_DISCONNECT){ 138386805605S[email protected] hci_emit_dedicated_bonding_result(bd_address, status); 138486805605S[email protected] } 138586805605S[email protected] } 138686805605S[email protected] } 138786805605S[email protected] } 138886805605S[email protected] 138994ab26f8Smatthias.ringwald // execute main loop 139094ab26f8Smatthias.ringwald hci_run(); 139116833f0aSmatthias.ringwald } 139216833f0aSmatthias.ringwald 1393c91d150bS[email protected] static void sco_handler(uint8_t * packet, uint16_t size){ 1394c91d150bS[email protected] // not handled yet 1395c91d150bS[email protected] } 1396c91d150bS[email protected] 13970a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 13985bb5bc3eS[email protected] hci_dump_packet(packet_type, 1, packet, size); 139910e830c9Smatthias.ringwald switch (packet_type) { 140010e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 140110e830c9Smatthias.ringwald event_handler(packet, size); 140210e830c9Smatthias.ringwald break; 140310e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 140410e830c9Smatthias.ringwald acl_handler(packet, size); 140510e830c9Smatthias.ringwald break; 1406c91d150bS[email protected] case HCI_SCO_DATA_PACKET: 1407c91d150bS[email protected] sco_handler(packet, size); 140810e830c9Smatthias.ringwald default: 140910e830c9Smatthias.ringwald break; 141010e830c9Smatthias.ringwald } 141110e830c9Smatthias.ringwald } 141210e830c9Smatthias.ringwald 1413fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 14142718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 14153a9fb326S[email protected] hci_stack->packet_handler = handler; 141616833f0aSmatthias.ringwald } 141716833f0aSmatthias.ringwald 14186155b3d3S[email protected] static void hci_state_reset(){ 1419595bdbfbS[email protected] // no connections yet 1420595bdbfbS[email protected] hci_stack->connections = NULL; 142174308b23Smatthias.ringwald 142274308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 142374308b23Smatthias.ringwald // hci_stack->discoverable = 0; 142474308b23Smatthias.ringwald // hci_stack->connectable = 0; 142574308b23Smatthias.ringwald // hci_stack->bondable = 1; 1426595bdbfbS[email protected] 142744935e40S[email protected] // buffer is free 142844935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 142944935e40S[email protected] 1430595bdbfbS[email protected] // no pending cmds 1431595bdbfbS[email protected] hci_stack->decline_reason = 0; 1432595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 1433595bdbfbS[email protected] 1434595bdbfbS[email protected] // LE 1435595bdbfbS[email protected] hci_stack->adv_addr_type = 0; 1436595bdbfbS[email protected] memset(hci_stack->adv_address, 0, 6); 1437595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1438e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1439da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_min = 0x0006; 1440da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_interval_max = 0x0C80; 1441da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_latency_min = 0x0000; 1442da886c03S[email protected] hci_stack->le_connection_parameter_range.le_conn_latency_max = 0x03E8; 1443da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_min = 0x000A; 1444da886c03S[email protected] hci_stack->le_connection_parameter_range.le_supervision_timeout_max = 0x0C80; 1445595bdbfbS[email protected] } 1446595bdbfbS[email protected] 14474f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 1448475c8125Smatthias.ringwald 14493a9fb326S[email protected] #ifdef HAVE_MALLOC 14503a9fb326S[email protected] if (!hci_stack) { 14513a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 14523a9fb326S[email protected] } 14533a9fb326S[email protected] #else 14543a9fb326S[email protected] hci_stack = &hci_stack_static; 14553a9fb326S[email protected] #endif 145666fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 14573a9fb326S[email protected] 1458475c8125Smatthias.ringwald // reference to use transport layer implementation 14593a9fb326S[email protected] hci_stack->hci_transport = transport; 1460475c8125Smatthias.ringwald 146111e23e5fSmatthias.ringwald // references to used control implementation 14623a9fb326S[email protected] hci_stack->control = control; 146311e23e5fSmatthias.ringwald 146411e23e5fSmatthias.ringwald // reference to used config 14653a9fb326S[email protected] hci_stack->config = config; 146611e23e5fSmatthias.ringwald 146716833f0aSmatthias.ringwald // higher level handler 14683a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 146916833f0aSmatthias.ringwald 1470404843c1Smatthias.ringwald // store and open remote device db 14713a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 14723a9fb326S[email protected] if (hci_stack->remote_device_db) { 14733a9fb326S[email protected] hci_stack->remote_device_db->open(); 1474404843c1Smatthias.ringwald } 147529d53098Smatthias.ringwald 14768fcba05dSmatthias.ringwald // max acl payload size defined in config.h 14773a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 14788fcba05dSmatthias.ringwald 147916833f0aSmatthias.ringwald // register packet handlers with transport 148010e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 1481f5454fc6Smatthias.ringwald 14823a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 1483e2386ba1S[email protected] 1484e2386ba1S[email protected] // class of device 14853a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 1486a45d6b9fS[email protected] 1487f20168b8Smatthias.ringwald // bondable by default 1488f20168b8Smatthias.ringwald hci_stack->bondable = 1; 1489f20168b8Smatthias.ringwald 149063048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 14913a9fb326S[email protected] hci_stack->ssp_enable = 1; 14923a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 14933a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 14943a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 149569a97523S[email protected] 1496595bdbfbS[email protected] hci_state_reset(); 1497475c8125Smatthias.ringwald } 1498475c8125Smatthias.ringwald 1499404843c1Smatthias.ringwald void hci_close(){ 1500404843c1Smatthias.ringwald // close remote device db 15013a9fb326S[email protected] if (hci_stack->remote_device_db) { 15023a9fb326S[email protected] hci_stack->remote_device_db->close(); 1503404843c1Smatthias.ringwald } 15043a9fb326S[email protected] while (hci_stack->connections) { 1505bc68afe2S[email protected] // cancel all l2cap connections 1506bc68afe2S[email protected] hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host 15073a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 1508f5454fc6Smatthias.ringwald } 1509f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 15103a9fb326S[email protected] 15113a9fb326S[email protected] #ifdef HAVE_MALLOC 15123a9fb326S[email protected] free(hci_stack); 15133a9fb326S[email protected] #endif 15143a9fb326S[email protected] hci_stack = NULL; 1515404843c1Smatthias.ringwald } 1516404843c1Smatthias.ringwald 15179e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 15189e61646fS[email protected] hci_stack->class_of_device = class_of_device; 15199e61646fS[email protected] } 15209e61646fS[email protected] 1521f456b2d0S[email protected] // Set Public BD ADDR - passed on to Bluetooth chipset if supported in bt_control_h 1522f456b2d0S[email protected] void hci_set_bd_addr(bd_addr_t addr){ 1523f456b2d0S[email protected] memcpy(hci_stack->custom_bd_addr, addr, 6); 1524f456b2d0S[email protected] hci_stack->custom_bd_addr_set = 1; 1525f456b2d0S[email protected] } 1526f456b2d0S[email protected] 152766fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 152866fb9560S[email protected] disable_l2cap_timeouts = 1; 152966fb9560S[email protected] } 15308d213e1aSmatthias.ringwald // State-Module-Driver overview 15318d213e1aSmatthias.ringwald // state module low-level 15328d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 15338d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 15348d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 15358d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 1536d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 1537d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 1538c7e0c5f6Smatthias.ringwald 153940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 15407301ad89Smatthias.ringwald 1541038bc64cSmatthias.ringwald // power on 1542f9a30166Smatthias.ringwald int err = 0; 15433a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 15443a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1545f9a30166Smatthias.ringwald } 1546038bc64cSmatthias.ringwald if (err){ 15479da54300S[email protected] log_error( "POWER_ON failed"); 1548038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1549038bc64cSmatthias.ringwald return err; 1550038bc64cSmatthias.ringwald } 1551038bc64cSmatthias.ringwald 1552038bc64cSmatthias.ringwald // open low-level device 15533a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1554038bc64cSmatthias.ringwald if (err){ 15559da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 15563a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 15573a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1558f9a30166Smatthias.ringwald } 1559038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1560038bc64cSmatthias.ringwald return err; 1561038bc64cSmatthias.ringwald } 15628d213e1aSmatthias.ringwald return 0; 15638d213e1aSmatthias.ringwald } 1564038bc64cSmatthias.ringwald 156540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 15668d213e1aSmatthias.ringwald 15679da54300S[email protected] log_info("hci_power_control_off"); 15689418f9c9Smatthias.ringwald 15698d213e1aSmatthias.ringwald // close low-level device 15703a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 15718d213e1aSmatthias.ringwald 15729da54300S[email protected] log_info("hci_power_control_off - hci_transport closed"); 15739418f9c9Smatthias.ringwald 15748d213e1aSmatthias.ringwald // power off 15753a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 15763a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 15778d213e1aSmatthias.ringwald } 15789418f9c9Smatthias.ringwald 15799da54300S[email protected] log_info("hci_power_control_off - control closed"); 15809418f9c9Smatthias.ringwald 15813a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 158272ea5239Smatthias.ringwald } 158372ea5239Smatthias.ringwald 158440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 158572ea5239Smatthias.ringwald 15869da54300S[email protected] log_info("hci_power_control_sleep"); 15873144bce4Smatthias.ringwald 1588b429b9b7Smatthias.ringwald #if 0 1589b429b9b7Smatthias.ringwald // don't close serial port during sleep 1590b429b9b7Smatthias.ringwald 159172ea5239Smatthias.ringwald // close low-level device 15923a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1593b429b9b7Smatthias.ringwald #endif 159472ea5239Smatthias.ringwald 159572ea5239Smatthias.ringwald // sleep mode 15963a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 15973a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 159872ea5239Smatthias.ringwald } 1599b429b9b7Smatthias.ringwald 16003a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 16018d213e1aSmatthias.ringwald } 16028d213e1aSmatthias.ringwald 160340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1604b429b9b7Smatthias.ringwald 16059da54300S[email protected] log_info("hci_power_control_wake"); 1606b429b9b7Smatthias.ringwald 1607b429b9b7Smatthias.ringwald // wake on 16083a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 16093a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1610b429b9b7Smatthias.ringwald } 1611b429b9b7Smatthias.ringwald 1612b429b9b7Smatthias.ringwald #if 0 1613b429b9b7Smatthias.ringwald // open low-level device 16143a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1615b429b9b7Smatthias.ringwald if (err){ 16169da54300S[email protected] log_error( "HCI_INIT failed, turning Bluetooth off again"); 16173a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 16183a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1619b429b9b7Smatthias.ringwald } 1620b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1621b429b9b7Smatthias.ringwald return err; 1622b429b9b7Smatthias.ringwald } 1623b429b9b7Smatthias.ringwald #endif 1624b429b9b7Smatthias.ringwald 1625b429b9b7Smatthias.ringwald return 0; 1626b429b9b7Smatthias.ringwald } 1627b429b9b7Smatthias.ringwald 162844935e40S[email protected] static void hci_power_transition_to_initializing(void){ 162944935e40S[email protected] // set up state machine 163044935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 163144935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 163244935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 163344935e40S[email protected] hci_stack->substate = 0; 163444935e40S[email protected] } 1635b429b9b7Smatthias.ringwald 16368d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1637d661ed19Smatthias.ringwald 16389da54300S[email protected] log_info("hci_power_control: %u, current mode %u", power_mode, hci_stack->state); 1639d661ed19Smatthias.ringwald 16408d213e1aSmatthias.ringwald int err = 0; 16413a9fb326S[email protected] switch (hci_stack->state){ 16428d213e1aSmatthias.ringwald 16438d213e1aSmatthias.ringwald case HCI_STATE_OFF: 16448d213e1aSmatthias.ringwald switch (power_mode){ 16458d213e1aSmatthias.ringwald case HCI_POWER_ON: 16468d213e1aSmatthias.ringwald err = hci_power_control_on(); 164797b61c7bS[email protected] if (err) { 164897b61c7bS[email protected] log_error("hci_power_control_on() error %u", err); 164997b61c7bS[email protected] return err; 165097b61c7bS[email protected] } 165144935e40S[email protected] hci_power_transition_to_initializing(); 16528d213e1aSmatthias.ringwald break; 16538d213e1aSmatthias.ringwald case HCI_POWER_OFF: 16548d213e1aSmatthias.ringwald // do nothing 16558d213e1aSmatthias.ringwald break; 16568d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1657b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 16588d213e1aSmatthias.ringwald break; 16598d213e1aSmatthias.ringwald } 16608d213e1aSmatthias.ringwald break; 16617301ad89Smatthias.ringwald 16628d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 16638d213e1aSmatthias.ringwald switch (power_mode){ 16648d213e1aSmatthias.ringwald case HCI_POWER_ON: 16658d213e1aSmatthias.ringwald // do nothing 16668d213e1aSmatthias.ringwald break; 16678d213e1aSmatthias.ringwald case HCI_POWER_OFF: 16688d213e1aSmatthias.ringwald // no connections yet, just turn it off 16698d213e1aSmatthias.ringwald hci_power_control_off(); 16708d213e1aSmatthias.ringwald break; 16718d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1672b546ac54Smatthias.ringwald // no connections yet, just turn it off 167372ea5239Smatthias.ringwald hci_power_control_sleep(); 16748d213e1aSmatthias.ringwald break; 16758d213e1aSmatthias.ringwald } 16768d213e1aSmatthias.ringwald break; 16777301ad89Smatthias.ringwald 16788d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 16798d213e1aSmatthias.ringwald switch (power_mode){ 16808d213e1aSmatthias.ringwald case HCI_POWER_ON: 16818d213e1aSmatthias.ringwald // do nothing 16828d213e1aSmatthias.ringwald break; 16838d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1684c7e0c5f6Smatthias.ringwald // see hci_run 16853a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 16868d213e1aSmatthias.ringwald break; 16878d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1688b546ac54Smatthias.ringwald // see hci_run 16893a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 16903a9fb326S[email protected] hci_stack->substate = 0; 16918d213e1aSmatthias.ringwald break; 16928d213e1aSmatthias.ringwald } 16938d213e1aSmatthias.ringwald break; 16947301ad89Smatthias.ringwald 16958d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 16968d213e1aSmatthias.ringwald switch (power_mode){ 16978d213e1aSmatthias.ringwald case HCI_POWER_ON: 169844935e40S[email protected] hci_power_transition_to_initializing(); 16998d213e1aSmatthias.ringwald break; 17008d213e1aSmatthias.ringwald case HCI_POWER_OFF: 17018d213e1aSmatthias.ringwald // do nothing 17028d213e1aSmatthias.ringwald break; 17038d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1704b546ac54Smatthias.ringwald // see hci_run 17053a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 17063a9fb326S[email protected] hci_stack->substate = 0; 17078d213e1aSmatthias.ringwald break; 17088d213e1aSmatthias.ringwald } 17098d213e1aSmatthias.ringwald break; 17108d213e1aSmatthias.ringwald 17118d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 17128d213e1aSmatthias.ringwald switch (power_mode){ 17138d213e1aSmatthias.ringwald case HCI_POWER_ON: 171428171530Smatthias.ringwald 171528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 171628171530Smatthias.ringwald // nothing to do, if H4 supports power management 171728171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 17183a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 17193a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 172028171530Smatthias.ringwald break; 172128171530Smatthias.ringwald } 172228171530Smatthias.ringwald #endif 172344935e40S[email protected] hci_power_transition_to_initializing(); 17248d213e1aSmatthias.ringwald break; 17258d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1726b546ac54Smatthias.ringwald // see hci_run 17273a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 17288d213e1aSmatthias.ringwald break; 17298d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1730b546ac54Smatthias.ringwald // do nothing 17318d213e1aSmatthias.ringwald break; 17328d213e1aSmatthias.ringwald } 17338d213e1aSmatthias.ringwald break; 17348d213e1aSmatthias.ringwald 17358d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 17368d213e1aSmatthias.ringwald switch (power_mode){ 17378d213e1aSmatthias.ringwald case HCI_POWER_ON: 173828171530Smatthias.ringwald 173928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 174028171530Smatthias.ringwald // nothing to do, if H4 supports power management 174128171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 17423a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 17433a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1744758b46ceSmatthias.ringwald hci_update_scan_enable(); 174528171530Smatthias.ringwald break; 174628171530Smatthias.ringwald } 174728171530Smatthias.ringwald #endif 17483144bce4Smatthias.ringwald err = hci_power_control_wake(); 17493144bce4Smatthias.ringwald if (err) return err; 175044935e40S[email protected] hci_power_transition_to_initializing(); 17518d213e1aSmatthias.ringwald break; 17528d213e1aSmatthias.ringwald case HCI_POWER_OFF: 17533a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 17548d213e1aSmatthias.ringwald break; 17558d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1756b546ac54Smatthias.ringwald // do nothing 17578d213e1aSmatthias.ringwald break; 17588d213e1aSmatthias.ringwald } 17598d213e1aSmatthias.ringwald break; 176011e23e5fSmatthias.ringwald } 176168d92d03Smatthias.ringwald 1762038bc64cSmatthias.ringwald // create internal event 1763ee8bf225Smatthias.ringwald hci_emit_state(); 1764ee8bf225Smatthias.ringwald 176568d92d03Smatthias.ringwald // trigger next/first action 176668d92d03Smatthias.ringwald hci_run(); 176768d92d03Smatthias.ringwald 1768475c8125Smatthias.ringwald return 0; 1769475c8125Smatthias.ringwald } 1770475c8125Smatthias.ringwald 1771758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1772758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 17733a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1774758b46ceSmatthias.ringwald hci_run(); 1775758b46ceSmatthias.ringwald } 1776758b46ceSmatthias.ringwald 1777381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1778381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1779381fbed8Smatthias.ringwald 17803a9fb326S[email protected] if (hci_stack->discoverable == enable){ 17813a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1782381fbed8Smatthias.ringwald return; 1783381fbed8Smatthias.ringwald } 1784381fbed8Smatthias.ringwald 17853a9fb326S[email protected] hci_stack->discoverable = enable; 1786758b46ceSmatthias.ringwald hci_update_scan_enable(); 1787758b46ceSmatthias.ringwald } 1788b031bebbSmatthias.ringwald 1789758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1790758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1791758b46ceSmatthias.ringwald 1792758b46ceSmatthias.ringwald // don't emit event 17933a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1794758b46ceSmatthias.ringwald 17953a9fb326S[email protected] hci_stack->connectable = enable; 1796758b46ceSmatthias.ringwald hci_update_scan_enable(); 1797381fbed8Smatthias.ringwald } 1798381fbed8Smatthias.ringwald 17995061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 18003a9fb326S[email protected] return &hci_stack->local_bd_addr; 18015061f3afS[email protected] } 18025061f3afS[email protected] 180306b35ec0Smatthias.ringwald void hci_run(){ 18048a485f27Smatthias.ringwald 180532ab9390Smatthias.ringwald hci_connection_t * connection; 180632ab9390Smatthias.ringwald linked_item_t * it; 180732ab9390Smatthias.ringwald 1808b5d8b22bS[email protected] // send continuation fragments first, as they block the prepared packet buffer 1809b5d8b22bS[email protected] if (hci_stack->acl_fragmentation_total_size > 0) { 1810b5d8b22bS[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(hci_stack->hci_packet_buffer); 1811b5d8b22bS[email protected] if (hci_can_send_prepared_acl_packet_now(con_handle)){ 1812b5d8b22bS[email protected] hci_connection_t *connection = hci_connection_for_handle(con_handle); 1813b5d8b22bS[email protected] if (connection) { 1814b5d8b22bS[email protected] hci_send_acl_packet_fragments(connection); 1815b5d8b22bS[email protected] return; 1816b5d8b22bS[email protected] } 1817b5d8b22bS[email protected] // connection gone -> discard further fragments 1818b5d8b22bS[email protected] hci_stack->acl_fragmentation_total_size = 0; 1819b5d8b22bS[email protected] hci_stack->acl_fragmentation_pos = 0; 1820b5d8b22bS[email protected] } 1821b5d8b22bS[email protected] } 1822b5d8b22bS[email protected] 1823d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 1824ce4c8fabSmatthias.ringwald 1825b031bebbSmatthias.ringwald // global/non-connection oriented commands 1826b031bebbSmatthias.ringwald 1827b031bebbSmatthias.ringwald // decline incoming connections 18283a9fb326S[email protected] if (hci_stack->decline_reason){ 18293a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 18303a9fb326S[email protected] hci_stack->decline_reason = 0; 18313a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1832dbe1a790S[email protected] return; 1833ce4c8fabSmatthias.ringwald } 1834ce4c8fabSmatthias.ringwald 1835b031bebbSmatthias.ringwald // send scan enable 18363a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 18373a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 18383a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1839dbe1a790S[email protected] return; 1840b031bebbSmatthias.ringwald } 1841b031bebbSmatthias.ringwald 1842b2f949feS[email protected] #ifdef HAVE_BLE 18437bdc6798S[email protected] // handle le scan 18447bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 18457bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 18467bdc6798S[email protected] case LE_START_SCAN: 18477bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 18487bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 18497bdc6798S[email protected] return; 18507bdc6798S[email protected] 18517bdc6798S[email protected] case LE_STOP_SCAN: 18527bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 18537bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 18547bdc6798S[email protected] return; 18557bdc6798S[email protected] default: 18567bdc6798S[email protected] break; 18577bdc6798S[email protected] } 1858e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 1859e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 1860e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 1861e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1862e2602ea2Smatthias.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); 1863e2602ea2Smatthias.ringwald return; 1864e2602ea2Smatthias.ringwald } 18657bdc6798S[email protected] } 1866b2f949feS[email protected] #endif 18677bdc6798S[email protected] 186832ab9390Smatthias.ringwald // send pending HCI commands 18693a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 1870b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 187132ab9390Smatthias.ringwald 18720bf6344aS[email protected] switch(connection->state){ 18730bf6344aS[email protected] case SEND_CREATE_CONNECTION: 18744f3229d8S[email protected] switch(connection->address_type){ 18754f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 18769da54300S[email protected] log_info("sending hci_create_connection"); 1877ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 18784f3229d8S[email protected] break; 18794f3229d8S[email protected] default: 1880b2f949feS[email protected] #ifdef HAVE_BLE 18819da54300S[email protected] log_info("sending hci_le_create_connection"); 18824f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 1883b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 1884b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 18854f3229d8S[email protected] 0, // don't use whitelist 18864f3229d8S[email protected] connection->address_type, // peer address type 18874f3229d8S[email protected] connection->address, // peer bd addr 1888b2571179Smatthias.ringwald hci_stack->adv_addr_type, // our addr type: 1889b2571179Smatthias.ringwald 0x0008, // conn interval min 1890b2571179Smatthias.ringwald 0x0018, // conn interval max 18914f3229d8S[email protected] 0, // conn latency 1892b2571179Smatthias.ringwald 0x0048, // supervision timeout 1893b2571179Smatthias.ringwald 0x0001, // min ce length 1894b2571179Smatthias.ringwald 0x0001 // max ce length 18954f3229d8S[email protected] ); 18964f3229d8S[email protected] 18974f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 1898b2f949feS[email protected] #endif 18994f3229d8S[email protected] break; 19004f3229d8S[email protected] } 1901ad83dc6aS[email protected] return; 1902ad83dc6aS[email protected] 19030bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 19049da54300S[email protected] log_info("sending hci_accept_connection_request"); 190532ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 190634d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1907dbe1a790S[email protected] return; 19080bf6344aS[email protected] 1909a6725849S[email protected] #ifdef HAVE_BLE 19100bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 19110bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 19120bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 19130bf6344aS[email protected] return; 1914a6725849S[email protected] #endif 19150bf6344aS[email protected] case SEND_DISCONNECT: 19160bf6344aS[email protected] connection->state = SENT_DISCONNECT; 19177851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 19180bf6344aS[email protected] return; 19190bf6344aS[email protected] 19200bf6344aS[email protected] default: 19210bf6344aS[email protected] break; 1922c7e0c5f6Smatthias.ringwald } 1923c7e0c5f6Smatthias.ringwald 192432ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 19259da54300S[email protected] log_info("responding to link key request"); 192634d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 192732ab9390Smatthias.ringwald link_key_t link_key; 1928c77e8838S[email protected] link_key_type_t link_key_type; 19293a9fb326S[email protected] if ( hci_stack->remote_device_db 19303a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 193134d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 19329ab95c90S[email protected] connection->link_key_type = link_key_type; 193332ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 193432ab9390Smatthias.ringwald } else { 193532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 193632ab9390Smatthias.ringwald } 1937dbe1a790S[email protected] return; 193832ab9390Smatthias.ringwald } 19391d6b20aeS[email protected] 1940899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 19419da54300S[email protected] log_info("denying to pin request"); 1942899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 194334d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 19444c57c146S[email protected] return; 19454c57c146S[email protected] } 19464c57c146S[email protected] 1947dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 194834d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 194982d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 195082d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 1951106d6d11S[email protected] // tweak authentication requirements 19523a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1953106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 19549faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 19559faad3abS[email protected] } 19569faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 19579faad3abS[email protected] authreq |= 1; 1958106d6d11S[email protected] } 19593a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1960f8fb5f6eS[email protected] } else { 1961f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1962f8fb5f6eS[email protected] } 1963dbe1a790S[email protected] return; 196432ab9390Smatthias.ringwald } 196532ab9390Smatthias.ringwald 1966dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1967dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 196834d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1969dbe1a790S[email protected] return; 1970dbe1a790S[email protected] } 1971dbe1a790S[email protected] 1972dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1973dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 197434d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1975dbe1a790S[email protected] return; 1976dbe1a790S[email protected] } 1977afd4e962S[email protected] 1978afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1979afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 198034d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 19812bd8b7e7S[email protected] return; 19822bd8b7e7S[email protected] } 19832bd8b7e7S[email protected] 19842bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 19852bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 198634d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 198734d2123cS[email protected] return; 198834d2123cS[email protected] } 1989ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1990ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 19911bd5283dS[email protected] connection->bonding_flags |= BONDING_EMIT_COMPLETE_ON_DISCONNECT; 199252d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 1993ad83dc6aS[email protected] return; 1994ad83dc6aS[email protected] } 199534d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 199634d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 199734d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 19982bd8b7e7S[email protected] return; 1999afd4e962S[email protected] } 2000dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 2001dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 2002dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 2003dce78009S[email protected] return; 2004dce78009S[email protected] } 2005da886c03S[email protected] 2006c37a3166S[email protected] #ifdef HAVE_BLE 2007da886c03S[email protected] if (connection->le_con_parameter_update_state == CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS){ 2008da886c03S[email protected] connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE; 2009da886c03S[email protected] 2010c37a3166S[email protected] uint16_t connection_interval_min = connection->le_conn_interval_min; 2011c37a3166S[email protected] connection->le_conn_interval_min = 0; 2012c37a3166S[email protected] hci_send_cmd(&hci_le_connection_update, connection->con_handle, connection_interval_min, 2013c37a3166S[email protected] connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout, 2014c37a3166S[email protected] 0x0000, 0xffff); 2015c37a3166S[email protected] } 2016c37a3166S[email protected] #endif 2017dbe1a790S[email protected] } 2018c7e0c5f6Smatthias.ringwald 20193a9fb326S[email protected] switch (hci_stack->state){ 20203429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 2021bd88fde9S[email protected] hci_initializing_state_machine(); 20223429f56bSmatthias.ringwald break; 2023c7e0c5f6Smatthias.ringwald 2024c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 2025c7e0c5f6Smatthias.ringwald 20269da54300S[email protected] log_info("HCI_STATE_HALTING"); 2027c7e0c5f6Smatthias.ringwald // close all open connections 20283a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 2029c7e0c5f6Smatthias.ringwald if (connection){ 203032ab9390Smatthias.ringwald 2031c7e0c5f6Smatthias.ringwald // send disconnect 2032d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 203332ab9390Smatthias.ringwald 20349da54300S[email protected] log_info("HCI_STATE_HALTING, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 20356ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 2036c7e0c5f6Smatthias.ringwald 2037c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 2038c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 2039c7e0c5f6Smatthias.ringwald return; 2040c7e0c5f6Smatthias.ringwald } 20419da54300S[email protected] log_info("HCI_STATE_HALTING, calling off"); 2042c7e0c5f6Smatthias.ringwald 204372ea5239Smatthias.ringwald // switch mode 2044c7e0c5f6Smatthias.ringwald hci_power_control_off(); 20459418f9c9Smatthias.ringwald 20469da54300S[email protected] log_info("HCI_STATE_HALTING, emitting state"); 204772ea5239Smatthias.ringwald hci_emit_state(); 20489da54300S[email protected] log_info("HCI_STATE_HALTING, done"); 204972ea5239Smatthias.ringwald break; 2050c7e0c5f6Smatthias.ringwald 205172ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 20523a9fb326S[email protected] switch(hci_stack->substate) { 205389db417bSmatthias.ringwald case 0: 20549da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP"); 205572ea5239Smatthias.ringwald // close all open connections 20563a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 205766da7044Smatthias.ringwald 205828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 205966da7044Smatthias.ringwald // don't close connections, if H4 supports power management 206066da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 206166da7044Smatthias.ringwald connection = NULL; 206266da7044Smatthias.ringwald } 206366da7044Smatthias.ringwald #endif 206472ea5239Smatthias.ringwald if (connection){ 206532ab9390Smatthias.ringwald 206672ea5239Smatthias.ringwald // send disconnect 2067d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 206832ab9390Smatthias.ringwald 20699da54300S[email protected] log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u", connection, (uint16_t)connection->con_handle); 20706ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 207172ea5239Smatthias.ringwald 207272ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 207372ea5239Smatthias.ringwald hci_shutdown_connection(connection); 207472ea5239Smatthias.ringwald return; 207572ea5239Smatthias.ringwald } 207672ea5239Smatthias.ringwald 207792368cd3S[email protected] if (hci_classic_supported()){ 207889db417bSmatthias.ringwald // disable page and inquiry scan 2079d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 208032ab9390Smatthias.ringwald 20819da54300S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans"); 20823a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 208389db417bSmatthias.ringwald 208489db417bSmatthias.ringwald // continue in next sub state 20853a9fb326S[email protected] hci_stack->substate++; 208689db417bSmatthias.ringwald break; 208792368cd3S[email protected] } 208892368cd3S[email protected] // fall through for ble-only chips 208992368cd3S[email protected] 209089db417bSmatthias.ringwald case 2: 20919da54300S[email protected] log_info("HCI_STATE_HALTING, calling sleep"); 209228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 209328171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 209428171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 209528171530Smatthias.ringwald // SLEEP MODE reached 20963a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 209728171530Smatthias.ringwald hci_emit_state(); 209828171530Smatthias.ringwald break; 209928171530Smatthias.ringwald } 210028171530Smatthias.ringwald #endif 210172ea5239Smatthias.ringwald // switch mode 21023a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 2103c7e0c5f6Smatthias.ringwald hci_emit_state(); 210428171530Smatthias.ringwald break; 210528171530Smatthias.ringwald 210689db417bSmatthias.ringwald default: 210789db417bSmatthias.ringwald break; 210889db417bSmatthias.ringwald } 2109c7e0c5f6Smatthias.ringwald break; 2110c7e0c5f6Smatthias.ringwald 21113429f56bSmatthias.ringwald default: 21123429f56bSmatthias.ringwald break; 21131f504dbdSmatthias.ringwald } 21143429f56bSmatthias.ringwald } 211516833f0aSmatthias.ringwald 211631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 2117c8e4258aSmatthias.ringwald bd_addr_t addr; 2118c8e4258aSmatthias.ringwald hci_connection_t * conn; 2119c8e4258aSmatthias.ringwald // house-keeping 2120c8e4258aSmatthias.ringwald 2121c8e4258aSmatthias.ringwald // create_connection? 2122c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 2123c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 21249da54300S[email protected] log_info("Create_connection to %s", bd_addr_to_str(addr)); 2125c8e4258aSmatthias.ringwald 212696a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 2127ad83dc6aS[email protected] if (!conn){ 212896a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 212917f1ba2aSmatthias.ringwald if (!conn){ 213017f1ba2aSmatthias.ringwald // notify client that alloc failed 213117f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 213217f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 213317f1ba2aSmatthias.ringwald } 2134ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 2135ad83dc6aS[email protected] } 2136ad83dc6aS[email protected] log_info("conn state %u", conn->state); 2137ad83dc6aS[email protected] switch (conn->state){ 2138ad83dc6aS[email protected] // if connection active exists 2139ad83dc6aS[email protected] case OPEN: 214062bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 2141ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 214262bda3fbS[email protected] return 0; 2143ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 2144ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 2145ad83dc6aS[email protected] break; 2146ad83dc6aS[email protected] default: 2147ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 2148ad83dc6aS[email protected] return 0; 2149ad83dc6aS[email protected] } 2150c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 2151c8e4258aSmatthias.ringwald } 2152c8e4258aSmatthias.ringwald 21537fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 21547fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 21557fde4af9Smatthias.ringwald } 21567fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 21577fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 21587fde4af9Smatthias.ringwald } 21597fde4af9Smatthias.ringwald 21608ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 21613a9fb326S[email protected] if (hci_stack->remote_device_db){ 21628ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 21633a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 21648ef73945Smatthias.ringwald } 21658ef73945Smatthias.ringwald } 2166c8e4258aSmatthias.ringwald 21676724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 21686724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 21696724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 217096a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 21716724cd9eS[email protected] if (conn){ 21726724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 21736724cd9eS[email protected] } 21746724cd9eS[email protected] } 21756724cd9eS[email protected] 21766724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 21776724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 21786724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 21796724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 21806724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 218196a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 21826724cd9eS[email protected] if (conn){ 21836724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 21846724cd9eS[email protected] } 21856724cd9eS[email protected] } 21866724cd9eS[email protected] 218769a97523S[email protected] #ifdef HAVE_BLE 218869a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 21893a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 219069a97523S[email protected] } 219169a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 21923a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 219369a97523S[email protected] } 219469a97523S[email protected] #endif 219569a97523S[email protected] 21963a9fb326S[email protected] hci_stack->num_cmd_packets--; 21975bb5bc3eS[email protected] 21985bb5bc3eS[email protected] hci_dump_packet(HCI_COMMAND_DATA_PACKET, 0, packet, size); 21996b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 22006b4af23dS[email protected] 2201d051460cS[email protected] // release packet buffer for synchronous transport implementations 2202c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 22036b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 22046b4af23dS[email protected] } 22056b4af23dS[email protected] 22066b4af23dS[email protected] return err; 220731452debSmatthias.ringwald } 22088adf0ddaSmatthias.ringwald 22092bd8b7e7S[email protected] // disconnect because of security block 22102bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 22112bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 22122bd8b7e7S[email protected] if (!connection) return; 22132bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 22142bd8b7e7S[email protected] } 22152bd8b7e7S[email protected] 22162bd8b7e7S[email protected] 2217dbe1a790S[email protected] // Configure Secure Simple Pairing 2218dbe1a790S[email protected] 2219dbe1a790S[email protected] // enable will enable SSP during init 2220dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 22213a9fb326S[email protected] hci_stack->ssp_enable = enable; 2222dbe1a790S[email protected] } 2223dbe1a790S[email protected] 22242bd8b7e7S[email protected] int hci_local_ssp_activated(){ 22253a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 22262bd8b7e7S[email protected] } 22272bd8b7e7S[email protected] 2228dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 2229dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 22303a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 2231dbe1a790S[email protected] } 2232dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 22333a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 2234dbe1a790S[email protected] } 2235dbe1a790S[email protected] 2236dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 2237dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 22383a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 2239dbe1a790S[email protected] } 2240dbe1a790S[email protected] 22411cd208adSmatthias.ringwald /** 22421cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 22431cd208adSmatthias.ringwald */ 2244fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 22455127cc62S[email protected] 2246d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 22479d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 22489d14b626S[email protected] return 0; 22499d14b626S[email protected] } 22509d14b626S[email protected] 22515127cc62S[email protected] // for HCI INITIALIZATION 22529da54300S[email protected] // log_info("hci_send_cmd: opcode %04x", cmd->opcode); 22535127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 22545127cc62S[email protected] 22559d14b626S[email protected] hci_reserve_packet_buffer(); 22569d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 22579d14b626S[email protected] 22581cd208adSmatthias.ringwald va_list argptr; 22591cd208adSmatthias.ringwald va_start(argptr, cmd); 22609d14b626S[email protected] uint16_t size = hci_create_cmd_internal(packet, cmd, argptr); 22611cd208adSmatthias.ringwald va_end(argptr); 22629d14b626S[email protected] 22639d14b626S[email protected] return hci_send_cmd_packet(packet, size); 226493b8dc03Smatthias.ringwald } 2265c8e4258aSmatthias.ringwald 2266ee091cf1Smatthias.ringwald // Create various non-HCI events. 2267ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 2268ee091cf1Smatthias.ringwald 2269c8e4258aSmatthias.ringwald void hci_emit_state(){ 22703a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 2271425d1371Smatthias.ringwald uint8_t event[3]; 227280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 2273425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 22743a9fb326S[email protected] event[2] = hci_stack->state; 2275425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 22763a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2277c8e4258aSmatthias.ringwald } 2278c8e4258aSmatthias.ringwald 227917f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 2280425d1371Smatthias.ringwald uint8_t event[13]; 2281c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 2282425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 228317f1ba2aSmatthias.ringwald event[2] = status; 2284c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 2285c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 2286c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 2287c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 2288425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 22893a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2290c8e4258aSmatthias.ringwald } 2291c8e4258aSmatthias.ringwald 22926e2e9a6bS[email protected] void hci_emit_le_connection_complete(uint8_t address_type, bd_addr_t * address, uint16_t conn_handle, uint8_t status){ 22934f3229d8S[email protected] uint8_t event[21]; 22944f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 22954f3229d8S[email protected] event[1] = sizeof(event) - 2; 22964f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 22974f3229d8S[email protected] event[3] = status; 22986e2e9a6bS[email protected] bt_store_16(event, 4, conn_handle); 22994f3229d8S[email protected] event[6] = 0; // TODO: role 23006e2e9a6bS[email protected] event[7] = address_type; 23016e2e9a6bS[email protected] bt_flip_addr(&event[8], *address); 23024f3229d8S[email protected] bt_store_16(event, 14, 0); // interval 23034f3229d8S[email protected] bt_store_16(event, 16, 0); // latency 23044f3229d8S[email protected] bt_store_16(event, 18, 0); // supervision timeout 23054f3229d8S[email protected] event[20] = 0; // master clock accuracy 23064f3229d8S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 23074f3229d8S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23084f3229d8S[email protected] } 23094f3229d8S[email protected] 23103c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 2311425d1371Smatthias.ringwald uint8_t event[6]; 23123c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 2313e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 23143c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 23153c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 23163c4d4b90Smatthias.ringwald event[5] = reason; 2317425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 23183a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23193c4d4b90Smatthias.ringwald } 23203c4d4b90Smatthias.ringwald 2321ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 232266fb9560S[email protected] if (disable_l2cap_timeouts) return; 2323e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 2324425d1371Smatthias.ringwald uint8_t event[4]; 232580d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 2326425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2327ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 2328425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 23293a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2330ee091cf1Smatthias.ringwald } 233143bfb1bdSmatthias.ringwald 233243bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 2333e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 2334425d1371Smatthias.ringwald uint8_t event[3]; 233580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 2336425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 233743bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 2338425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23393a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 234043bfb1bdSmatthias.ringwald } 2341038bc64cSmatthias.ringwald 2342038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 2343e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 2344425d1371Smatthias.ringwald uint8_t event[2]; 234580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 2346425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2347425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23483a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2349038bc64cSmatthias.ringwald } 23501b0e3922Smatthias.ringwald 235109ba8edeSmatthias.ringwald #ifndef EMBEDDED 23521b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 2353e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 2354425d1371Smatthias.ringwald uint8_t event[6]; 23551b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 2356425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2357425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 2358425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 2359425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 2360425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23613a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23621b0e3922Smatthias.ringwald } 236309ba8edeSmatthias.ringwald #endif 23641b0e3922Smatthias.ringwald 23652ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 2366e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 2367425d1371Smatthias.ringwald uint8_t event[3]; 23682ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 2369425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 23702ed6235cSmatthias.ringwald event[2] = enabled; 2371425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23723a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 23732ed6235cSmatthias.ringwald } 2374627c2f45Smatthias.ringwald 2375627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 2376e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 2377627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 2378e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 2379f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 23802f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 2381f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 2382e0abb8e7S[email protected] 2383e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 2384e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 2385e0abb8e7S[email protected] 2386e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 23873a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 2388627c2f45Smatthias.ringwald } 2389381fbed8Smatthias.ringwald 2390381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 2391e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 2392425d1371Smatthias.ringwald uint8_t event[3]; 2393381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 2394425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2395381fbed8Smatthias.ringwald event[2] = enabled; 2396425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 23973a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2398381fbed8Smatthias.ringwald } 2399458bf4e8S[email protected] 2400a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 2401df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 2402a00031e2S[email protected] uint8_t event[5]; 2403e00caf9cS[email protected] int pos = 0; 2404a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 2405e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 2406a00031e2S[email protected] bt_store_16(event, 2, con_handle); 2407e00caf9cS[email protected] pos += 2; 2408e00caf9cS[email protected] event[pos++] = level; 2409e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 24103a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2411e00caf9cS[email protected] } 2412e00caf9cS[email protected] 24131bd5283dS[email protected] void hci_emit_dedicated_bonding_result(bd_addr_t address, uint8_t status){ 2414ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 2415ad83dc6aS[email protected] uint8_t event[9]; 2416ad83dc6aS[email protected] int pos = 0; 2417ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 2418ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 2419ad83dc6aS[email protected] event[pos++] = status; 24201bd5283dS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], address); 2421ad83dc6aS[email protected] pos += 6; 2422ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 24233a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2424ad83dc6aS[email protected] } 2425ad83dc6aS[email protected] 24262bd8b7e7S[email protected] // query if remote side supports SSP 24272bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 24282bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 24292bd8b7e7S[email protected] if (!connection) return 0; 24302bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 24312bd8b7e7S[email protected] } 24322bd8b7e7S[email protected] 2433df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 2434df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 2435df3354fcS[email protected] } 2436df3354fcS[email protected] 2437458bf4e8S[email protected] // GAP API 2438458bf4e8S[email protected] /** 2439458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 2440458bf4e8S[email protected] * @praram enabled 2441458bf4e8S[email protected] */ 24424c57c146S[email protected] void gap_set_bondable_mode(int enable){ 24433a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 2444458bf4e8S[email protected] } 2445cb230b9dS[email protected] 2446cb230b9dS[email protected] /** 244734d2123cS[email protected] * @brief map link keys to security levels 2448cb230b9dS[email protected] */ 244934d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 245034d2123cS[email protected] switch (link_key_type){ 24513c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 24523c68dfa9S[email protected] return LEVEL_4; 24533c68dfa9S[email protected] case COMBINATION_KEY: 24543c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 24553c68dfa9S[email protected] return LEVEL_3; 24563c68dfa9S[email protected] default: 24573c68dfa9S[email protected] return LEVEL_2; 24583c68dfa9S[email protected] } 2459cb230b9dS[email protected] } 2460cb230b9dS[email protected] 2461a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 246234d2123cS[email protected] if (!connection) return LEVEL_0; 246334d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 246434d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 246534d2123cS[email protected] } 246634d2123cS[email protected] 246734d2123cS[email protected] 2468106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 24695127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 2470106d6d11S[email protected] return level > LEVEL_2; 2471106d6d11S[email protected] } 2472106d6d11S[email protected] 247334d2123cS[email protected] /** 247434d2123cS[email protected] * @brief get current security level 247534d2123cS[email protected] */ 247634d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 247734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 247834d2123cS[email protected] if (!connection) return LEVEL_0; 247934d2123cS[email protected] return gap_security_level_for_connection(connection); 248034d2123cS[email protected] } 248134d2123cS[email protected] 2482cb230b9dS[email protected] /** 2483cb230b9dS[email protected] * @brief request connection to device to 2484cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 2485cb230b9dS[email protected] */ 248634d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 248734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 248834d2123cS[email protected] if (!connection){ 2489a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 249034d2123cS[email protected] return; 249134d2123cS[email protected] } 249234d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 249334d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 249434d2123cS[email protected] if (current_level >= requested_level){ 2495a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 249634d2123cS[email protected] return; 249734d2123cS[email protected] } 2498a00031e2S[email protected] 249934d2123cS[email protected] connection->requested_security_level = requested_level; 2500a00031e2S[email protected] 250125bf5872S[email protected] #if 0 250225bf5872S[email protected] // sending encryption request without a link key results in an error. 250325bf5872S[email protected] // TODO: figure out how to use it properly 250425bf5872S[email protected] 2505fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 25063a9fb326S[email protected] if (hci_stack->remote_device_db){ 2507a00031e2S[email protected] link_key_type_t link_key_type; 2508a00031e2S[email protected] link_key_t link_key; 25093a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 2510a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 2511a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2512a00031e2S[email protected] return; 2513a00031e2S[email protected] } 2514a00031e2S[email protected] } 2515a00031e2S[email protected] } 251625bf5872S[email protected] #endif 2517a00031e2S[email protected] 25181eb2563eS[email protected] // try to authenticate connection 25191eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2520e80b2cf9S[email protected] hci_run(); 2521e00caf9cS[email protected] } 2522ad83dc6aS[email protected] 2523ad83dc6aS[email protected] /** 2524ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 2525ad83dc6aS[email protected] * @param device 2526ad83dc6aS[email protected] * @param request MITM protection 2527ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 2528ad83dc6aS[email protected] */ 2529ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 2530ad83dc6aS[email protected] 2531ad83dc6aS[email protected] // create connection state machine 253296a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 2533ad83dc6aS[email protected] 2534ad83dc6aS[email protected] if (!connection){ 2535ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 2536ad83dc6aS[email protected] } 2537ad83dc6aS[email protected] 2538ad83dc6aS[email protected] // delete linkn key 2539ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 2540ad83dc6aS[email protected] 2541ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 2542ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 2543ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 25445127cc62S[email protected] log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level); 2545ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 2546ad83dc6aS[email protected] 2547ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 2548ad83dc6aS[email protected] 2549ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 2550ad83dc6aS[email protected] // handle: authentication failure 2551ad83dc6aS[email protected] // handle: disconnect on done 2552ad83dc6aS[email protected] 2553ad83dc6aS[email protected] hci_run(); 2554ad83dc6aS[email protected] 2555ad83dc6aS[email protected] return 0; 2556ad83dc6aS[email protected] } 25578e618f72S[email protected] 25588e618f72S[email protected] void gap_set_local_name(const char * local_name){ 25598e618f72S[email protected] hci_stack->local_name = local_name; 25608e618f72S[email protected] } 25618e618f72S[email protected] 25627bdc6798S[email protected] le_command_status_t le_central_start_scan(){ 256336af3e18S[email protected] if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK; 25647bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 25657bdc6798S[email protected] hci_run(); 25667bdc6798S[email protected] return BLE_PERIPHERAL_OK; 25677bdc6798S[email protected] } 25688e618f72S[email protected] 25697bdc6798S[email protected] le_command_status_t le_central_stop_scan(){ 257036af3e18S[email protected] if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK; 25717bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 25727bdc6798S[email protected] hci_run(); 25737bdc6798S[email protected] return BLE_PERIPHERAL_OK; 25747bdc6798S[email protected] } 25754f3229d8S[email protected] 2576ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 2577ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 2578ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 2579ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 2580ef11999fSmatthias.ringwald hci_run(); 2581ef11999fSmatthias.ringwald } 25824f3229d8S[email protected] 25834f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){ 25844f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 25854f3229d8S[email protected] if (!conn){ 25861f479f8cS[email protected] log_info("le_central_connect: no connection exists yet, creating context"); 25874f3229d8S[email protected] conn = create_connection_for_bd_addr_and_type(*addr, addr_type); 25884f3229d8S[email protected] if (!conn){ 25894f3229d8S[email protected] // notify client that alloc failed 25906e2e9a6bS[email protected] hci_emit_le_connection_complete(addr_type, addr, 0, BTSTACK_MEMORY_ALLOC_FAILED); 25916e2e9a6bS[email protected] log_info("le_central_connect: failed to alloc hci_connection_t"); 25924f3229d8S[email protected] return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller 25934f3229d8S[email protected] } 25944f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 25951f479f8cS[email protected] log_info("le_central_connect: send create connection next"); 2596564fca32S[email protected] hci_run(); 25974f3229d8S[email protected] return BLE_PERIPHERAL_OK; 25984f3229d8S[email protected] } 25990bf6344aS[email protected] 26000bf6344aS[email protected] if (!hci_is_le_connection(conn) || 26010bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 26020bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 26036e2e9a6bS[email protected] hci_emit_le_connection_complete(conn->address_type, &conn->address, 0, ERROR_CODE_COMMAND_DISALLOWED); 26041f479f8cS[email protected] log_error("le_central_connect: classic connection or connect is already being created"); 26050bf6344aS[email protected] return BLE_PERIPHERAL_IN_WRONG_STATE; 26060bf6344aS[email protected] } 26070bf6344aS[email protected] 26081f479f8cS[email protected] log_info("le_central_connect: context exists with state %u", conn->state); 26096e2e9a6bS[email protected] hci_emit_le_connection_complete(conn->address_type, &conn->address, conn->con_handle, 0); 26104f3229d8S[email protected] hci_run(); 26114f3229d8S[email protected] return BLE_PERIPHERAL_OK; 26124f3229d8S[email protected] } 26134f3229d8S[email protected] 26147851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 26157851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){ 26160bf6344aS[email protected] linked_item_t *it; 26170bf6344aS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 26180bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 26190bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 26200bf6344aS[email protected] switch (conn->state){ 2621a6725849S[email protected] case SEND_CREATE_CONNECTION: 26227851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 26237851196eSmatthias.ringwald return conn; 26247851196eSmatthias.ringwald default: 26257851196eSmatthias.ringwald break; 26267851196eSmatthias.ringwald }; 26277851196eSmatthias.ringwald } 26287851196eSmatthias.ringwald return NULL; 26297851196eSmatthias.ringwald } 26307851196eSmatthias.ringwald 26317851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){ 26327851196eSmatthias.ringwald hci_connection_t * conn = le_central_get_outgoing_connection(); 26337851196eSmatthias.ringwald switch (conn->state){ 26347851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 26357851196eSmatthias.ringwald // skip sending create connection and emit event instead 26366e2e9a6bS[email protected] hci_emit_le_connection_complete(conn->address_type, &conn->address, 0, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 26377851196eSmatthias.ringwald linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 26387851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 26390bf6344aS[email protected] break; 2640a6725849S[email protected] case SENT_CREATE_CONNECTION: 26417851196eSmatthias.ringwald // request to send cancel connection 26420bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 26430bf6344aS[email protected] hci_run(); 26440bf6344aS[email protected] break; 26450bf6344aS[email protected] default: 26460bf6344aS[email protected] break; 26470bf6344aS[email protected] } 2648e31f89a7S[email protected] return BLE_PERIPHERAL_OK; 2649e31f89a7S[email protected] } 26504f3229d8S[email protected] 2651c37a3166S[email protected] /** 2652c37a3166S[email protected] * @brief Updates the connection parameters for a given LE connection 2653c37a3166S[email protected] * @param handle 2654c37a3166S[email protected] * @param conn_interval_min (unit: 1.25ms) 2655c37a3166S[email protected] * @param conn_interval_max (unit: 1.25ms) 2656c37a3166S[email protected] * @param conn_latency 2657c37a3166S[email protected] * @param supervision_timeout (unit: 10ms) 2658c37a3166S[email protected] * @returns 0 if ok 2659c37a3166S[email protected] */ 2660c37a3166S[email protected] int gap_update_connection_parameters(hci_con_handle_t con_handle, uint16_t conn_interval_min, 2661c37a3166S[email protected] uint16_t conn_interval_max, uint16_t conn_latency, uint16_t supervision_timeout){ 2662c37a3166S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 2663c37a3166S[email protected] if (!connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 2664c37a3166S[email protected] connection->le_conn_interval_min = conn_interval_min; 2665c37a3166S[email protected] connection->le_conn_interval_max = conn_interval_max; 2666c37a3166S[email protected] connection->le_conn_latency = conn_latency; 2667c37a3166S[email protected] connection->le_supervision_timeout = supervision_timeout; 2668c37a3166S[email protected] return 0; 2669c37a3166S[email protected] } 2670c37a3166S[email protected] 26715917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){ 26725917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 26735917a5c5S[email protected] if (!conn){ 26747851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 26755917a5c5S[email protected] return BLE_PERIPHERAL_OK; 26765917a5c5S[email protected] } 26775917a5c5S[email protected] conn->state = SEND_DISCONNECT; 26785917a5c5S[email protected] hci_run(); 26794f3229d8S[email protected] return BLE_PERIPHERAL_OK; 26804f3229d8S[email protected] } 268104a6ef8cSmatthias.ringwald 268204a6ef8cSmatthias.ringwald void hci_disconnect_all(){ 268304a6ef8cSmatthias.ringwald linked_list_iterator_t it; 268404a6ef8cSmatthias.ringwald linked_list_iterator_init(&it, &hci_stack->connections); 268504a6ef8cSmatthias.ringwald while (linked_list_iterator_has_next(&it)){ 268604a6ef8cSmatthias.ringwald hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it); 268704a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 268804a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 268904a6ef8cSmatthias.ringwald } 2690d31fba26S[email protected] hci_run(); 269104a6ef8cSmatthias.ringwald } 2692