11f504dbdSmatthias.ringwald /* 26b64433eSmatthias.ringwald * Copyright (C) 2009-2012 by Matthias Ringwald 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 201713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 336b64433eSmatthias.ringwald * Please inquire about commercial licensing options at [email protected] 346b64433eSmatthias.ringwald * 351713bceaSmatthias.ringwald */ 361713bceaSmatthias.ringwald 371713bceaSmatthias.ringwald /* 381f504dbdSmatthias.ringwald * hci.c 391f504dbdSmatthias.ringwald * 401f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 411f504dbdSmatthias.ringwald * 421f504dbdSmatthias.ringwald */ 431f504dbdSmatthias.ringwald 44bde315ceS[email protected] #include "btstack-config.h" 4528171530Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "hci.h" 474c57c146S[email protected] #include "gap.h" 487f2435e6Smatthias.ringwald 4993b8dc03Smatthias.ringwald #include <stdarg.h> 5093b8dc03Smatthias.ringwald #include <string.h> 5156fe0872Smatthias.ringwald #include <stdio.h> 527f2435e6Smatthias.ringwald 53549e6ebeSmatthias.ringwald #ifndef EMBEDDED 54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 5509ba8edeSmatthias.ringwald #include <btstack/version.h> 56549e6ebeSmatthias.ringwald #endif 57549e6ebeSmatthias.ringwald 58a3b02b71Smatthias.ringwald #include "btstack_memory.h" 597f2435e6Smatthias.ringwald #include "debug.h" 60d8905019Smatthias.ringwald #include "hci_dump.h" 6193b8dc03Smatthias.ringwald 62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 631b0e3922Smatthias.ringwald 64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 65ee091cf1Smatthias.ringwald 66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 67da5275c5S[email protected] 6828171530Smatthias.ringwald #ifdef USE_BLUETOOL 6928171530Smatthias.ringwald #include "bt_control_iphone.h" 7028171530Smatthias.ringwald #endif 7128171530Smatthias.ringwald 72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 73a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection); 7496a45072S[email protected] static void hci_connection_timeout_handler(timer_source_t *timer); 7596a45072S[email protected] static void hci_connection_timestamp(hci_connection_t *connection); 767586ee35S[email protected] static int hci_power_control_on(void); 777586ee35S[email protected] static void hci_power_control_off(void); 786155b3d3S[email protected] static void hci_state_reset(); 79758b46ceSmatthias.ringwald 8006b35ec0Smatthias.ringwald // the STACK is here 813a9fb326S[email protected] #ifndef HAVE_MALLOC 823a9fb326S[email protected] static hci_stack_t hci_stack_static; 833a9fb326S[email protected] #endif 843a9fb326S[email protected] static hci_stack_t * hci_stack = NULL; 8516833f0aSmatthias.ringwald 8666fb9560S[email protected] // test helper 8766fb9560S[email protected] static uint8_t disable_l2cap_timeouts = 0; 8866fb9560S[email protected] 8996a45072S[email protected] /** 9096a45072S[email protected] * create connection for given address 9196a45072S[email protected] * 9296a45072S[email protected] * @return connection OR NULL, if no memory left 9396a45072S[email protected] */ 9496a45072S[email protected] static hci_connection_t * create_connection_for_bd_addr_and_type(bd_addr_t addr, bd_addr_type_t addr_type){ 9596a45072S[email protected] 965127cc62S[email protected] log_info("create_connection_for_addr %s", bd_addr_to_str(addr)); 9796a45072S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 9896a45072S[email protected] if (!conn) return NULL; 9996a45072S[email protected] BD_ADDR_COPY(conn->address, addr); 10096a45072S[email protected] conn->address_type = addr_type; 10196a45072S[email protected] conn->con_handle = 0xffff; 10296a45072S[email protected] conn->authentication_flags = AUTH_FLAGS_NONE; 10396a45072S[email protected] conn->bonding_flags = 0; 10496a45072S[email protected] conn->requested_security_level = LEVEL_0; 10596a45072S[email protected] linked_item_set_user(&conn->timeout.item, conn); 10696a45072S[email protected] conn->timeout.process = hci_connection_timeout_handler; 10796a45072S[email protected] hci_connection_timestamp(conn); 10896a45072S[email protected] conn->acl_recombination_length = 0; 10996a45072S[email protected] conn->acl_recombination_pos = 0; 11096a45072S[email protected] conn->num_acl_packets_sent = 0; 11196a45072S[email protected] linked_list_add(&hci_stack->connections, (linked_item_t *) conn); 11296a45072S[email protected] return conn; 11396a45072S[email protected] } 11466fb9560S[email protected] 11597addcc5Smatthias.ringwald /** 116ee091cf1Smatthias.ringwald * get connection for a given handle 117ee091cf1Smatthias.ringwald * 118ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 119ee091cf1Smatthias.ringwald */ 1205061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 121ee091cf1Smatthias.ringwald linked_item_t *it; 1223a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 123ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 124ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 125ee091cf1Smatthias.ringwald } 126ee091cf1Smatthias.ringwald } 127ee091cf1Smatthias.ringwald return NULL; 128ee091cf1Smatthias.ringwald } 129ee091cf1Smatthias.ringwald 13096a45072S[email protected] /** 13196a45072S[email protected] * get connection for given address 13296a45072S[email protected] * 13396a45072S[email protected] * @return connection OR NULL, if not found 13496a45072S[email protected] */ 13596a45072S[email protected] hci_connection_t * hci_connection_for_bd_addr_and_type(bd_addr_t * addr, bd_addr_type_t addr_type){ 13662bda3fbS[email protected] linked_item_t *it; 13762bda3fbS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 13862bda3fbS[email protected] hci_connection_t * connection = (hci_connection_t *) it; 13996a45072S[email protected] if (connection->address_type != addr_type) continue; 14096a45072S[email protected] if (memcmp(addr, connection->address, 6) != 0) continue; 14162bda3fbS[email protected] return connection; 14262bda3fbS[email protected] } 14362bda3fbS[email protected] return NULL; 14462bda3fbS[email protected] } 14562bda3fbS[email protected] 1462b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 14728ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 148c785ef68Smatthias.ringwald #ifdef HAVE_TIME 149ee091cf1Smatthias.ringwald struct timeval tv; 150ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 151c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 152ee091cf1Smatthias.ringwald // connections might be timed out 153ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 154ee091cf1Smatthias.ringwald } 1552b12a0b9Smatthias.ringwald #endif 156e5780900Smatthias.ringwald #ifdef HAVE_TICK 157c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 158c785ef68Smatthias.ringwald // connections might be timed out 159c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 160c785ef68Smatthias.ringwald } 161c785ef68Smatthias.ringwald #endif 162c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 163c785ef68Smatthias.ringwald run_loop_add_timer(timer); 164c785ef68Smatthias.ringwald } 165ee091cf1Smatthias.ringwald 166ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 167c7492964Smatthias.ringwald #ifdef HAVE_TIME 168ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 169c7492964Smatthias.ringwald #endif 170e5780900Smatthias.ringwald #ifdef HAVE_TICK 171c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 172c785ef68Smatthias.ringwald #endif 173ee091cf1Smatthias.ringwald } 174ee091cf1Smatthias.ringwald 17506b35ec0Smatthias.ringwald 17628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 17728ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 17828ca2b46S[email protected] } 17928ca2b46S[email protected] 18028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 18128ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 18228ca2b46S[email protected] } 18328ca2b46S[email protected] 18428ca2b46S[email protected] 18543bfb1bdSmatthias.ringwald /** 18680ca58a0Smatthias.ringwald * add authentication flags and reset timer 18796a45072S[email protected] * @note: assumes classic connection 1887fde4af9Smatthias.ringwald */ 1897fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1907fde4af9Smatthias.ringwald bd_addr_t addr; 1917fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 19296a45072S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 1937fde4af9Smatthias.ringwald if (conn) { 19428ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 19580ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1967fde4af9Smatthias.ringwald } 1977fde4af9Smatthias.ringwald } 1987fde4af9Smatthias.ringwald 19980ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 2005061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 20180ca58a0Smatthias.ringwald if (!conn) return 0; 2026724cd9eS[email protected] if (conn->authentication_flags & LEGACY_PAIRING_ACTIVE) return 1; 2036724cd9eS[email protected] if (conn->authentication_flags & SSP_PAIRING_ACTIVE) return 1; 2046724cd9eS[email protected] return 0; 20580ca58a0Smatthias.ringwald } 20680ca58a0Smatthias.ringwald 207c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 2083a9fb326S[email protected] if (hci_stack->remote_device_db) { 2093a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(addr); 210c12e46e7Smatthias.ringwald } 211c12e46e7Smatthias.ringwald } 212c12e46e7Smatthias.ringwald 2130bf6344aS[email protected] int hci_is_le_connection(hci_connection_t * connection){ 2140bf6344aS[email protected] return connection->address_type == BD_ADDR_TYPE_LE_PUBLIC || 2150bf6344aS[email protected] connection->address_type == BD_ADDR_TYPE_LE_RANDOM; 2160bf6344aS[email protected] } 2170bf6344aS[email protected] 2187fde4af9Smatthias.ringwald 2197fde4af9Smatthias.ringwald /** 22043bfb1bdSmatthias.ringwald * count connections 22143bfb1bdSmatthias.ringwald */ 22240d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 22356c253c9Smatthias.ringwald int count = 0; 22443bfb1bdSmatthias.ringwald linked_item_t *it; 2253a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next, count++); 22643bfb1bdSmatthias.ringwald return count; 22743bfb1bdSmatthias.ringwald } 228c8e4258aSmatthias.ringwald 22997addcc5Smatthias.ringwald /** 230ba681a6cSmatthias.ringwald * Dummy handler called by HCI 23116833f0aSmatthias.ringwald */ 2322718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 23316833f0aSmatthias.ringwald } 23416833f0aSmatthias.ringwald 235998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2365061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 237998906cdSmatthias.ringwald if (!connection) { 2387d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 239998906cdSmatthias.ringwald return 0; 240998906cdSmatthias.ringwald } 241998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 242998906cdSmatthias.ringwald } 243998906cdSmatthias.ringwald 244*e79abdd6S[email protected] uint8_t hci_number_free_acl_slots_for_handle(hci_con_handle_t con_handle){ 2453a9fb326S[email protected] uint8_t free_slots = hci_stack->total_num_acl_packets; 246998906cdSmatthias.ringwald linked_item_t *it; 2473a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 248998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 249998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2507d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 251998906cdSmatthias.ringwald return 0; 252998906cdSmatthias.ringwald } 253998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 254998906cdSmatthias.ringwald } 255998906cdSmatthias.ringwald return free_slots; 256998906cdSmatthias.ringwald } 257998906cdSmatthias.ringwald 258ac928cc2S[email protected] 259ac928cc2S[email protected] // @deprecated 260c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 261c24735b1Smatthias.ringwald switch (packet_type) { 262c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 263ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(0); 264c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 265ac928cc2S[email protected] return hci_can_send_command_packet_now(); 266c24735b1Smatthias.ringwald default: 267c24735b1Smatthias.ringwald return 0; 268c24735b1Smatthias.ringwald } 269c24735b1Smatthias.ringwald } 270c24735b1Smatthias.ringwald 271ac928cc2S[email protected] // @deprecated 2726b4af23dS[email protected] // same as hci_can_send_packet_now, but also checks if packet buffer is free for use 2736b4af23dS[email protected] int hci_can_send_packet_now_using_packet_buffer(uint8_t packet_type){ 274ac928cc2S[email protected] 2756b4af23dS[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 276ac928cc2S[email protected] 277ac928cc2S[email protected] switch (packet_type) { 278ac928cc2S[email protected] case HCI_ACL_DATA_PACKET: 279ac928cc2S[email protected] return hci_can_send_acl_packet_now(0); 280ac928cc2S[email protected] case HCI_COMMAND_DATA_PACKET: 281ac928cc2S[email protected] return hci_can_send_command_packet_now(); 282ac928cc2S[email protected] default: 283ac928cc2S[email protected] return 0; 284ac928cc2S[email protected] } 285ac928cc2S[email protected] } 286ac928cc2S[email protected] 287ac928cc2S[email protected] 288ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 289ac928cc2S[email protected] int hci_can_send_command_packet_now(void){ 290ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 291ac928cc2S[email protected] 292ac928cc2S[email protected] // check for async hci transport implementations 293ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 294ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 295ac928cc2S[email protected] return 0; 296ac928cc2S[email protected] } 297ac928cc2S[email protected] } 298ac928cc2S[email protected] 299ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 300ac928cc2S[email protected] } 301ac928cc2S[email protected] 302ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 303ac928cc2S[email protected] // check for async hci transport implementations 304ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 305ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){ 306ac928cc2S[email protected] return 0; 307ac928cc2S[email protected] } 308ac928cc2S[email protected] } 309*e79abdd6S[email protected] return hci_number_free_acl_slots_for_handle(con_handle) > 0; 310ac928cc2S[email protected] } 311ac928cc2S[email protected] 312ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 313ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 314ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(con_handle); 3156b4af23dS[email protected] } 3166b4af23dS[email protected] 317c8b9416aS[email protected] // used for internal checks in l2cap[-le].c 318c8b9416aS[email protected] int hci_is_packet_buffer_reserved(void){ 319c8b9416aS[email protected] return hci_stack->hci_packet_buffer_reserved; 320c8b9416aS[email protected] } 321c8b9416aS[email protected] 3226b4af23dS[email protected] // reserves outgoing packet buffer. @returns 1 if successful 3236b4af23dS[email protected] int hci_reserve_packet_buffer(void){ 3249d14b626S[email protected] if (hci_stack->hci_packet_buffer_reserved) { 3259d14b626S[email protected] log_error("hci_reserve_packet_buffer called but buffer already reserved"); 3269d14b626S[email protected] return 0; 3279d14b626S[email protected] } 3286b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 1; 3296b4af23dS[email protected] return 1; 3306b4af23dS[email protected] } 3316b4af23dS[email protected] 33268a0fcf7S[email protected] void hci_release_packet_buffer(void){ 33368a0fcf7S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 33468a0fcf7S[email protected] } 33568a0fcf7S[email protected] 3366b4af23dS[email protected] // assumption: synchronous implementations don't provide can_send_packet_now as they don't keep the buffer after the call 3376b4af23dS[email protected] int hci_transport_synchronous(void){ 3386b4af23dS[email protected] return hci_stack->hci_transport->can_send_packet_now == NULL; 3396b4af23dS[email protected] } 3406b4af23dS[email protected] 341826f7347S[email protected] // pre: caller has reserved the packet buffer 342826f7347S[email protected] int hci_send_acl_packet_buffer(int size){ 3437856c818Smatthias.ringwald 344826f7347S[email protected] if (!hci_stack->hci_packet_buffer_reserved) { 345826f7347S[email protected] log_error("hci_send_acl_packet_buffer called without reserving packet buffer"); 346826f7347S[email protected] return 0; 347826f7347S[email protected] } 348826f7347S[email protected] 349d713a683S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 350d713a683S[email protected] hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 351d713a683S[email protected] 352826f7347S[email protected] // check for free places on Bluetooth module 353d713a683S[email protected] if (!hci_can_send_prepared_acl_packet_now(con_handle)) { 354826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 35597b61c7bS[email protected] hci_release_packet_buffer(); 35697b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 35797b61c7bS[email protected] } 3586218e6f1Smatthias.ringwald 3595061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 36097b61c7bS[email protected] if (!connection) { 3615fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 36297b61c7bS[email protected] hci_release_packet_buffer(); 36397b61c7bS[email protected] return 0; 36497b61c7bS[email protected] } 36556cf178bSmatthias.ringwald hci_connection_timestamp(connection); 36656cf178bSmatthias.ringwald 36756cf178bSmatthias.ringwald // count packet 36856cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 3697b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 3707856c818Smatthias.ringwald 37100d8e42eSmatthias.ringwald // send packet 3723a9fb326S[email protected] int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 3736218e6f1Smatthias.ringwald 3746b4af23dS[email protected] // free packet buffer for synchronous transport implementations 375826f7347S[email protected] if (hci_transport_synchronous()){ 37697b61c7bS[email protected] hci_release_packet_buffer(); 3776b4af23dS[email protected] } 3786b4af23dS[email protected] 37900d8e42eSmatthias.ringwald return err; 380ee091cf1Smatthias.ringwald } 381ee091cf1Smatthias.ringwald 38216833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 3837856c818Smatthias.ringwald 384e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 385e76a89eeS[email protected] 3867856c818Smatthias.ringwald // get info 3877856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 3885061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 3897856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 3907856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 3917856c818Smatthias.ringwald 3927856c818Smatthias.ringwald // ignore non-registered handle 3937856c818Smatthias.ringwald if (!conn){ 3947d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 3957856c818Smatthias.ringwald return; 3967856c818Smatthias.ringwald } 3977856c818Smatthias.ringwald 398e76a89eeS[email protected] // assert packet is complete 3999ecc3e17S[email protected] if (acl_length + 4 != size){ 400e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 401e76a89eeS[email protected] return; 402e76a89eeS[email protected] } 403e76a89eeS[email protected] 4047856c818Smatthias.ringwald // update idle timestamp 4057856c818Smatthias.ringwald hci_connection_timestamp(conn); 4067856c818Smatthias.ringwald 4077856c818Smatthias.ringwald // handle different packet types 4087856c818Smatthias.ringwald switch (acl_flags & 0x03) { 4097856c818Smatthias.ringwald 4107856c818Smatthias.ringwald case 0x01: // continuation fragment 4117856c818Smatthias.ringwald 4127856c818Smatthias.ringwald // sanity check 4137856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 4147d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 4157856c818Smatthias.ringwald return; 4167856c818Smatthias.ringwald } 4177856c818Smatthias.ringwald 4187856c818Smatthias.ringwald // append fragment payload (header already stored) 4197856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 4207856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 4217856c818Smatthias.ringwald 4227d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 423decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 4247856c818Smatthias.ringwald 4257856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 4267856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 4277856c818Smatthias.ringwald 4283a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 4297856c818Smatthias.ringwald // reset recombination buffer 4307856c818Smatthias.ringwald conn->acl_recombination_length = 0; 4317856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 4327856c818Smatthias.ringwald } 4337856c818Smatthias.ringwald break; 4347856c818Smatthias.ringwald 4357856c818Smatthias.ringwald case 0x02: { // first fragment 4367856c818Smatthias.ringwald 4377856c818Smatthias.ringwald // sanity check 4387856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 4397d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 4407856c818Smatthias.ringwald return; 4417856c818Smatthias.ringwald } 4427856c818Smatthias.ringwald 4437856c818Smatthias.ringwald // peek into L2CAP packet! 4447856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 4457856c818Smatthias.ringwald 446e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 447decc01a8Smatthias.ringwald 4487856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 4497856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 4507856c818Smatthias.ringwald 4517856c818Smatthias.ringwald // forward fragment as L2CAP packet 4523a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 4537856c818Smatthias.ringwald 4547856c818Smatthias.ringwald } else { 4557856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 4567856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 4577856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 4587856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 459decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 4607856c818Smatthias.ringwald } 4617856c818Smatthias.ringwald break; 4627856c818Smatthias.ringwald 4637856c818Smatthias.ringwald } 4647856c818Smatthias.ringwald default: 4657d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 4667856c818Smatthias.ringwald return; 4677856c818Smatthias.ringwald } 46894ab26f8Smatthias.ringwald 46994ab26f8Smatthias.ringwald // execute main loop 47094ab26f8Smatthias.ringwald hci_run(); 47116833f0aSmatthias.ringwald } 47222909952Smatthias.ringwald 47367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 4741f479f8cS[email protected] log_info("Connection closed: handle 0x%x, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 4753c4d4b90Smatthias.ringwald 476c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 477c785ef68Smatthias.ringwald 4783a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 479a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 4803c4d4b90Smatthias.ringwald 4813c4d4b90Smatthias.ringwald // now it's gone 482c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 483c7e0c5f6Smatthias.ringwald } 484c7e0c5f6Smatthias.ringwald 4850c042179S[email protected] static const uint16_t packet_type_sizes[] = { 4868f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 4878f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 4888f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 4898f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 4908f8108aaSmatthias.ringwald }; 49165389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 49265389bfcS[email protected] 0, // 3 slot packets 49365389bfcS[email protected] 1, // 5 slot packets 49465389bfcS[email protected] 25, // EDR 2 mpbs 49565389bfcS[email protected] 26, // EDR 3 mbps 49665389bfcS[email protected] 39, // 3 slot EDR packts 49765389bfcS[email protected] 40, // 5 slot EDR packet 49865389bfcS[email protected] }; 49965389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 50065389bfcS[email protected] 0x0f00, // 3 slot packets 50165389bfcS[email protected] 0xf000, // 5 slot packets 50265389bfcS[email protected] 0x1102, // EDR 2 mpbs 50365389bfcS[email protected] 0x2204, // EDR 3 mbps 50465389bfcS[email protected] 0x0300, // 3 slot EDR packts 50565389bfcS[email protected] 0x3000, // 5 slot EDR packet 50665389bfcS[email protected] }; 5078f8108aaSmatthias.ringwald 50865389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 50965389bfcS[email protected] // enable packet types based on size 5108f8108aaSmatthias.ringwald uint16_t packet_types = 0; 5118f8108aaSmatthias.ringwald int i; 5128f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 5138f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 5148f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 5158f8108aaSmatthias.ringwald packet_types |= 1 << i; 5168f8108aaSmatthias.ringwald } 5178f8108aaSmatthias.ringwald } 51865389bfcS[email protected] // disable packet types due to missing local supported features 51965389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 52065389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 52165389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 52265389bfcS[email protected] if (feature_set) continue; 52365389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 52465389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 52565389bfcS[email protected] } 5268f8108aaSmatthias.ringwald // flip bits for "may not be used" 5278f8108aaSmatthias.ringwald packet_types ^= 0x3306; 5288f8108aaSmatthias.ringwald return packet_types; 5298f8108aaSmatthias.ringwald } 5308f8108aaSmatthias.ringwald 5318f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 5323a9fb326S[email protected] return hci_stack->packet_types; 5338f8108aaSmatthias.ringwald } 5348f8108aaSmatthias.ringwald 535facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 5367dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 5373a9fb326S[email protected] return hci_stack->hci_packet_buffer; 5387dc17943Smatthias.ringwald } 5397dc17943Smatthias.ringwald 540f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 5413a9fb326S[email protected] return hci_stack->acl_data_packet_length; 5427dc17943Smatthias.ringwald } 5437dc17943Smatthias.ringwald 5446ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 5456ac9a97eS[email protected] // No. 54, byte 6, bit 6 5466ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 5476ac9a97eS[email protected] } 5486ac9a97eS[email protected] 549f5d8d141S[email protected] int hci_ssp_supported(void){ 5506ac9a97eS[email protected] // No. 51, byte 6, bit 3 5513a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 552f5d8d141S[email protected] } 553f5d8d141S[email protected] 554f5d8d141S[email protected] int hci_classic_supported(void){ 5556ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 5563a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 557f5d8d141S[email protected] } 558f5d8d141S[email protected] 559f5d8d141S[email protected] int hci_le_supported(void){ 560f5d8d141S[email protected] #ifdef HAVE_BLE 5616ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 5623a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 563f5d8d141S[email protected] #else 564f5d8d141S[email protected] return 0; 565f5d8d141S[email protected] #endif 566f5d8d141S[email protected] } 567f5d8d141S[email protected] 56869a97523S[email protected] // get addr type and address used in advertisement packets 56918904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 5703a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 5713a9fb326S[email protected] if (hci_stack->adv_addr_type){ 5723a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 57369a97523S[email protected] } else { 5743a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 57569a97523S[email protected] } 57669a97523S[email protected] } 57769a97523S[email protected] 578b2f949feS[email protected] #ifdef HAVE_BLE 579e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){ 58057c9da5bS[email protected] int num_reports = packet[3]; 58157c9da5bS[email protected] int i; 58257c9da5bS[email protected] int total_data_length = 0; 58357c9da5bS[email protected] int data_offset = 0; 58457c9da5bS[email protected] 58557c9da5bS[email protected] for (i=0; i<num_reports;i++){ 58657c9da5bS[email protected] total_data_length += packet[4+num_reports*8+i]; 58757c9da5bS[email protected] } 58857c9da5bS[email protected] 5892e440c8aS[email protected] log_info("num reports: %d, ", num_reports); 59057c9da5bS[email protected] for (i=0; i<num_reports;i++){ 59157c9da5bS[email protected] int pos = 0; 59257c9da5bS[email protected] uint8_t data_length = packet[4+num_reports*8+i]; 5932b552b23S[email protected] uint8_t event_size = 10 + data_length; 5942b552b23S[email protected] uint8_t event[2 + event_size ]; 5952b552b23S[email protected] event[pos++] = GAP_LE_ADVERTISING_REPORT; 59657c9da5bS[email protected] event[pos++] = event_size; 59757c9da5bS[email protected] event[pos++] = packet[4+i]; // event_type; 59857c9da5bS[email protected] event[pos++] = packet[4+num_reports+i]; // address_type; 599564fca32S[email protected] memcpy(&event[pos], &packet[4+num_reports*2+i*6], 6); // bt address 60057c9da5bS[email protected] pos += 6; 6012b552b23S[email protected] event[pos++] = packet[4+num_reports*9+total_data_length + i]; 6022e440c8aS[email protected] log_info("rssi: %d, ", event[pos-1]); 60357c9da5bS[email protected] event[pos++] = data_length; 60488ed79cfS[email protected] memcpy(&event[pos], &packet[4+num_reports*9+data_offset], data_length); 60557c9da5bS[email protected] data_offset += data_length; 60657c9da5bS[email protected] pos += data_length; 60757c9da5bS[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 60857c9da5bS[email protected] } 60957c9da5bS[email protected] } 610b2f949feS[email protected] #endif 61157c9da5bS[email protected] 6126155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 613a2481739S[email protected] uint8_t command_completed = 0; 614a2481739S[email protected] if ((hci_stack->substate % 2) == 0) return; 6156155b3d3S[email protected] // odd: waiting for event 6166155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 6176155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,3); 6186155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 619a2481739S[email protected] command_completed = 1; 6206155b3d3S[email protected] log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate); 6216155b3d3S[email protected] } else { 6226155b3d3S[email protected] log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 6236155b3d3S[email protected] } 6246155b3d3S[email protected] } 6256155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_STATUS){ 6266155b3d3S[email protected] uint8_t status = packet[2]; 6276155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,4); 6286155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 6296155b3d3S[email protected] if (status){ 630a2481739S[email protected] command_completed = 1; 6316155b3d3S[email protected] log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate); 6326155b3d3S[email protected] } else { 6336155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 6346155b3d3S[email protected] } 6356155b3d3S[email protected] } else { 6366155b3d3S[email protected] log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 6376155b3d3S[email protected] } 6386155b3d3S[email protected] } 639a2481739S[email protected] 640a2481739S[email protected] if (!command_completed) return; 641a2481739S[email protected] 642a2481739S[email protected] switch(hci_stack->substate >> 1){ 643a2481739S[email protected] default: 644a2481739S[email protected] hci_stack->substate++; 645a2481739S[email protected] break; 6466155b3d3S[email protected] } 6476155b3d3S[email protected] } 6486155b3d3S[email protected] 6496155b3d3S[email protected] static void hci_initializing_state_machine(){ 6506155b3d3S[email protected] // log_info("hci_init: substate %u\n", hci_stack->substate); 6516155b3d3S[email protected] if (hci_stack->substate % 2) { 6526155b3d3S[email protected] // odd: waiting for command completion 6536155b3d3S[email protected] return; 6546155b3d3S[email protected] } 6556155b3d3S[email protected] switch (hci_stack->substate >> 1){ 6566155b3d3S[email protected] case 0: // RESET 6576155b3d3S[email protected] hci_state_reset(); 6586155b3d3S[email protected] 6596155b3d3S[email protected] hci_send_cmd(&hci_reset); 6606155b3d3S[email protected] if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 6616155b3d3S[email protected] // skip baud change 6626155b3d3S[email protected] hci_stack->substate = 4; // >> 1 = 2 6636155b3d3S[email protected] } 6646155b3d3S[email protected] break; 6656155b3d3S[email protected] case 1: // SEND BAUD CHANGE 6666155b3d3S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 66788789c61Smatthias.ringwald hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 6686155b3d3S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 6696155b3d3S[email protected] break; 6706155b3d3S[email protected] case 2: // LOCAL BAUD CHANGE 6716155b3d3S[email protected] log_info("Local baud rate change"); 6726155b3d3S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 6736155b3d3S[email protected] hci_stack->substate += 2; 6746155b3d3S[email protected] // break missing here for fall through 6756155b3d3S[email protected] 6766155b3d3S[email protected] case 3: 6776155b3d3S[email protected] log_info("Custom init"); 6786155b3d3S[email protected] // Custom initialization 6796155b3d3S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 6806155b3d3S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 6816155b3d3S[email protected] if (valid_cmd){ 6826155b3d3S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 6836155b3d3S[email protected] hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 6846155b3d3S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 6856155b3d3S[email protected] hci_stack->substate = 4; // more init commands 6866155b3d3S[email protected] break; 6876155b3d3S[email protected] } 6886155b3d3S[email protected] log_info("hci_run: init script done\n\r"); 6896155b3d3S[email protected] } 6906155b3d3S[email protected] // otherwise continue 6916155b3d3S[email protected] hci_send_cmd(&hci_read_bd_addr); 6926155b3d3S[email protected] break; 6936155b3d3S[email protected] case 4: 6946155b3d3S[email protected] hci_send_cmd(&hci_read_buffer_size); 6956155b3d3S[email protected] break; 6966155b3d3S[email protected] case 5: 6976155b3d3S[email protected] hci_send_cmd(&hci_read_local_supported_features); 6986155b3d3S[email protected] break; 6996155b3d3S[email protected] case 6: 7006155b3d3S[email protected] if (hci_le_supported()){ 7016155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 7026155b3d3S[email protected] } else { 7036155b3d3S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 7046155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 7056155b3d3S[email protected] } 7066155b3d3S[email protected] 7076155b3d3S[email protected] // skip Classic init commands for LE only chipsets 7086155b3d3S[email protected] if (!hci_classic_supported()){ 7096155b3d3S[email protected] if (hci_le_supported()){ 7106155b3d3S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 7116155b3d3S[email protected] } else { 7126155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 7136155b3d3S[email protected] hci_stack->substate = 14 << 1; // skip all 7146155b3d3S[email protected] } 7156155b3d3S[email protected] } 7166155b3d3S[email protected] break; 7176155b3d3S[email protected] case 7: 7186155b3d3S[email protected] if (hci_ssp_supported()){ 7196155b3d3S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 7206155b3d3S[email protected] break; 7216155b3d3S[email protected] } 7226155b3d3S[email protected] hci_stack->substate += 2; 7236155b3d3S[email protected] // break missing here for fall through 7246155b3d3S[email protected] 7256155b3d3S[email protected] case 8: 7266155b3d3S[email protected] // ca. 15 sec 7276155b3d3S[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 7286155b3d3S[email protected] break; 7296155b3d3S[email protected] case 9: 7306155b3d3S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 7316155b3d3S[email protected] break; 7326155b3d3S[email protected] case 10: 7336155b3d3S[email protected] if (hci_stack->local_name){ 7346155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 7356155b3d3S[email protected] } else { 7366155b3d3S[email protected] char hostname[30]; 7376155b3d3S[email protected] #ifdef EMBEDDED 7386155b3d3S[email protected] // BTstack-11:22:33:44:55:66 7396155b3d3S[email protected] strcpy(hostname, "BTstack "); 7406155b3d3S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 7416155b3d3S[email protected] log_info("---> Name %s", hostname); 7426155b3d3S[email protected] #else 7436155b3d3S[email protected] // hostname for POSIX systems 7446155b3d3S[email protected] gethostname(hostname, 30); 7456155b3d3S[email protected] hostname[29] = '\0'; 7466155b3d3S[email protected] #endif 7476155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hostname); 7486155b3d3S[email protected] } 7496155b3d3S[email protected] break; 7506155b3d3S[email protected] case 11: 7516155b3d3S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 7526155b3d3S[email protected] if (!hci_le_supported()){ 7536155b3d3S[email protected] // SKIP LE init for Classic only configuration 7546155b3d3S[email protected] hci_stack->substate = 14 << 1; 7556155b3d3S[email protected] } 7566155b3d3S[email protected] break; 7576155b3d3S[email protected] 7586155b3d3S[email protected] #ifdef HAVE_BLE 7596155b3d3S[email protected] // LE INIT 7606155b3d3S[email protected] case 12: 7616155b3d3S[email protected] hci_send_cmd(&hci_le_read_buffer_size); 7626155b3d3S[email protected] break; 7636155b3d3S[email protected] case 13: 7646155b3d3S[email protected] // LE Supported Host = 1, Simultaneous Host = 0 7656155b3d3S[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 7666155b3d3S[email protected] break; 7676155b3d3S[email protected] case 14: 7686155b3d3S[email protected] // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs 7696155b3d3S[email protected] hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0); 7706155b3d3S[email protected] break; 7716155b3d3S[email protected] #endif 7726155b3d3S[email protected] 7736155b3d3S[email protected] // DONE 7746155b3d3S[email protected] case 15: 7756155b3d3S[email protected] // done. 7766155b3d3S[email protected] hci_stack->state = HCI_STATE_WORKING; 7776155b3d3S[email protected] hci_emit_state(); 7786155b3d3S[email protected] break; 7796155b3d3S[email protected] default: 7806155b3d3S[email protected] break; 7816155b3d3S[email protected] } 7826155b3d3S[email protected] hci_stack->substate++; 7836155b3d3S[email protected] } 7846155b3d3S[email protected] 78574ec757aSmatthias.ringwald // avoid huge local variables 786223aafc1Smatthias.ringwald #ifndef EMBEDDED 78774ec757aSmatthias.ringwald static device_name_t device_name; 788223aafc1Smatthias.ringwald #endif 78916833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 790e76a89eeS[email protected] 791e76a89eeS[email protected] uint16_t event_length = packet[1]; 792e76a89eeS[email protected] 793e76a89eeS[email protected] // assert packet is complete 794e76a89eeS[email protected] if (size != event_length + 2){ 795e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 796e76a89eeS[email protected] return; 797e76a89eeS[email protected] } 798e76a89eeS[email protected] 7991281a47eSmatthias.ringwald bd_addr_t addr; 80096a45072S[email protected] bd_addr_type_t addr_type; 8012d00edd4Smatthias.ringwald uint8_t link_type; 802fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 8031f7b95a1Smatthias.ringwald hci_connection_t * conn; 80456cf178bSmatthias.ringwald int i; 80522909952Smatthias.ringwald 8065909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 8075909f7f2Smatthias.ringwald 8086772a24cSmatthias.ringwald switch (packet[0]) { 80922909952Smatthias.ringwald 8106772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 8117ec5eeaaSmatthias.ringwald // get num cmd packets 8123a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]); 8133a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 8147ec5eeaaSmatthias.ringwald 815e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 816e2edc0c3Smatthias.ringwald // from offset 5 817e2edc0c3Smatthias.ringwald // status 8181d279b20Smatthias.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" 8193a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 82056cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 8213a9fb326S[email protected] hci_stack->total_num_acl_packets = packet[9]; 82256cf178bSmatthias.ringwald // ignore: total num SCO packets 8233a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 824a7a04bd9Smatthias.ringwald // determine usable ACL payload size 8253a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 8263a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 8278f8108aaSmatthias.ringwald } 82865389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 8293a9fb326S[email protected] hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets); 830e2edc0c3Smatthias.ringwald } 83156cf178bSmatthias.ringwald } 83265a46ef3S[email protected] #ifdef HAVE_BLE 83365a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 8343a9fb326S[email protected] hci_stack->le_data_packet_length = READ_BT_16(packet, 6); 8353a9fb326S[email protected] hci_stack->total_num_le_packets = packet[8]; 8363a9fb326S[email protected] log_info("hci_le_read_buffer_size: size %u, count %u\n", hci_stack->le_data_packet_length, hci_stack->total_num_le_packets); 83701f1657cS[email protected] 83801f1657cS[email protected] // use LE buffers if no clasic buffers have been reported 83901f1657cS[email protected] if (hci_stack->total_num_acl_packets == 0){ 84001f1657cS[email protected] log_info("use le buffers instead of classic ones"); 84101f1657cS[email protected] hci_stack->total_num_acl_packets = hci_stack->total_num_le_packets; 84201f1657cS[email protected] hci_stack->acl_data_packet_length = hci_stack->le_data_packet_length; 84301f1657cS[email protected] // determine usable ACL payload size 84401f1657cS[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 84501f1657cS[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 84601f1657cS[email protected] } 84701f1657cS[email protected] } 84865a46ef3S[email protected] } 84965a46ef3S[email protected] #endif 850188981d2Smatthias.ringwald // Dump local address 851188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 8523a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 853188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 8543a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 855188981d2Smatthias.ringwald } 856381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 8573a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 858381fbed8Smatthias.ringwald } 85965389bfcS[email protected] // Note: HCI init checks 860559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 8613a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 862559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 8633a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 8643a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 8653a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 8663a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 86765389bfcS[email protected] 86865389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 8693a9fb326S[email protected] hci_stack->packet_types = hci_acl_packet_types_for_buffer_size_and_local_features(hci_stack->acl_data_packet_length, &hci_stack->local_supported_features[0]); 8703a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 871f5d8d141S[email protected] 872f5d8d141S[email protected] // Classic/LE 873f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 874559e517eS[email protected] } 87556cf178bSmatthias.ringwald break; 87656cf178bSmatthias.ringwald 8777ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 8787ec5eeaaSmatthias.ringwald // get num cmd packets 8793a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]); 8803a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 8817ec5eeaaSmatthias.ringwald break; 8827ec5eeaaSmatthias.ringwald 8832e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 8842e440c8aS[email protected] int offset = 3; 88556cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 8862e440c8aS[email protected] handle = READ_BT_16(packet, offset); 8872e440c8aS[email protected] offset += 2; 8882e440c8aS[email protected] uint16_t num_packets = READ_BT_16(packet, offset); 8892e440c8aS[email protected] offset += 2; 8902e440c8aS[email protected] 8915061f3afS[email protected] conn = hci_connection_for_handle(handle); 89256cf178bSmatthias.ringwald if (!conn){ 8937d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 89456cf178bSmatthias.ringwald continue; 89556cf178bSmatthias.ringwald } 89656cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 8977b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 89856cf178bSmatthias.ringwald } 8996772a24cSmatthias.ringwald break; 9002e440c8aS[email protected] } 9011f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 90237eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 90337eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 9042d00edd4Smatthias.ringwald link_type = packet[11]; 905339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 90637eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 90796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 9081f7b95a1Smatthias.ringwald if (!conn) { 90996a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 9101f7b95a1Smatthias.ringwald } 911ce4c8fabSmatthias.ringwald if (!conn) { 912ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 9133a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 9143a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 915ce4c8fabSmatthias.ringwald break; 916ce4c8fabSmatthias.ringwald } 91732ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 91832ab9390Smatthias.ringwald hci_run(); 91937eaa4cfSmatthias.ringwald } else { 920ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 9213a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 9223a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 92337eaa4cfSmatthias.ringwald } 9241f7b95a1Smatthias.ringwald break; 9251f7b95a1Smatthias.ringwald 9266772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 927fe1ed1b8Smatthias.ringwald // Connection management 928fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 929339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 93096a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 93196a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 932fe1ed1b8Smatthias.ringwald if (conn) { 933b448a0e7Smatthias.ringwald if (!packet[2]){ 934c8e4258aSmatthias.ringwald conn->state = OPEN; 935fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 936ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 937ee091cf1Smatthias.ringwald 938c785ef68Smatthias.ringwald // restart timer 939c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 940ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 941c785ef68Smatthias.ringwald 942339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 94343bfb1bdSmatthias.ringwald 94443bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 945b448a0e7Smatthias.ringwald } else { 946ad83dc6aS[email protected] // notify client if dedicated bonding 947ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 948ad83dc6aS[email protected] hci_emit_dedicated_bonding_result(conn, packet[2]); 949ad83dc6aS[email protected] } 950ad83dc6aS[email protected] 951b448a0e7Smatthias.ringwald // connection failed, remove entry 9523a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 953a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 954c12e46e7Smatthias.ringwald 955c12e46e7Smatthias.ringwald // if authentication error, also delete link key 956c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 957c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 958c12e46e7Smatthias.ringwald } 959fe1ed1b8Smatthias.ringwald } 960fe1ed1b8Smatthias.ringwald } 9616772a24cSmatthias.ringwald break; 962fe1ed1b8Smatthias.ringwald 963afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 964afd4e962S[email protected] handle = READ_BT_16(packet, 3); 965afd4e962S[email protected] conn = hci_connection_for_handle(handle); 966afd4e962S[email protected] if (!conn) break; 967afd4e962S[email protected] if (!packet[2]){ 968afd4e962S[email protected] uint8_t * features = &packet[5]; 969afd4e962S[email protected] if (features[6] & (1 << 3)){ 970afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 971afd4e962S[email protected] } 972afd4e962S[email protected] } 973afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 974ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 975ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 976ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 977ad83dc6aS[email protected] } 978afd4e962S[email protected] break; 979afd4e962S[email protected] 9807fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 9817b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 9827fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 98374d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 9843a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 98532ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 98664472d52Smatthias.ringwald hci_run(); 987d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 98829d53098Smatthias.ringwald return; 9897fde4af9Smatthias.ringwald 9909ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 99129d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 99296a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 9939ab95c90S[email protected] if (!conn) break; 9949ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 9957bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 9969ab95c90S[email protected] // Change Connection Encryption keeps link key type 9979ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 9989ab95c90S[email protected] conn->link_key_type = link_key_type; 9999ab95c90S[email protected] } 10003a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 10013a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 100229d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 10037fde4af9Smatthias.ringwald break; 10049ab95c90S[email protected] } 10057fde4af9Smatthias.ringwald 10067fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 10076724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 10084c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 10093a9fb326S[email protected] if (!hci_stack->bondable){ 1010899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1011f8fb5f6eS[email protected] hci_run(); 1012f8fb5f6eS[email protected] return; 10134c57c146S[email protected] } 1014d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 10153a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 1016d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 10173a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 10187fde4af9Smatthias.ringwald break; 10197fde4af9Smatthias.ringwald 10201d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 10211d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1022dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1023dbe1a790S[email protected] break; 1024dbe1a790S[email protected] 1025dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 10266724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 10273a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1028dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1029dbe1a790S[email protected] break; 1030dbe1a790S[email protected] 1031dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 10326724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 10333a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1034dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 10351d6b20aeS[email protected] break; 10361d6b20aeS[email protected] 1037f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1038f0944df2S[email protected] handle = READ_BT_16(packet, 3); 1039f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1040f0944df2S[email protected] if (!conn) break; 1041ad83dc6aS[email protected] if (packet[2] == 0) { 1042f0944df2S[email protected] if (packet[5]){ 1043f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1044f0944df2S[email protected] } else { 1045536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1046f0944df2S[email protected] } 1047ad83dc6aS[email protected] } 1048a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 1049f0944df2S[email protected] break; 1050f0944df2S[email protected] 10511eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 10521eb2563eS[email protected] handle = READ_BT_16(packet, 3); 10531eb2563eS[email protected] conn = hci_connection_for_handle(handle); 10541eb2563eS[email protected] if (!conn) break; 1055ad83dc6aS[email protected] 1056ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1057ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1058ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1059ad83dc6aS[email protected] hci_emit_dedicated_bonding_result( conn, packet[2]); 1060ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 1061ad83dc6aS[email protected] break; 1062ad83dc6aS[email protected] } 1063ad83dc6aS[email protected] 1064b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 10651eb2563eS[email protected] // link key sufficient for requested security 10661eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1067ad83dc6aS[email protected] break; 1068ad83dc6aS[email protected] } 10691eb2563eS[email protected] // not enough 10701eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 10711eb2563eS[email protected] break; 107234d2123cS[email protected] 1073223aafc1Smatthias.ringwald #ifndef EMBEDDED 107474ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 10753a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 107674ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 107774ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 10785a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 10795a06394aSmatthias.ringwald for (i=0; i<248;i++){ 10805a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 10815a06394aSmatthias.ringwald packet[9+i] = 0; 10825a06394aSmatthias.ringwald break; 1083cdc9101dSmatthias.ringwald } 10845a06394aSmatthias.ringwald } 1085d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 108674ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 10873a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 108874ec757aSmatthias.ringwald break; 108974ec757aSmatthias.ringwald 109074ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 1091206a9031S[email protected] case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ 10923a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 109374ec757aSmatthias.ringwald // first send inq result packet 10943a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 109574ec757aSmatthias.ringwald // then send cached remote names 1096206a9031S[email protected] int offset = 3; 109774ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 1098206a9031S[email protected] bt_flip_addr(addr, &packet[offset]); 1099206a9031S[email protected] offset += 14; // 6 + 1 + 1 + 1 + 3 + 2; 11003a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 110174ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 110274ec757aSmatthias.ringwald } 110374ec757aSmatthias.ringwald } 110474ec757aSmatthias.ringwald return; 1105206a9031S[email protected] } 1106223aafc1Smatthias.ringwald #endif 110774ec757aSmatthias.ringwald 11086772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1109fe1ed1b8Smatthias.ringwald if (!packet[2]){ 1110fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 11115061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 1112fe1ed1b8Smatthias.ringwald if (conn) { 1113c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 1114fe1ed1b8Smatthias.ringwald } 1115fe1ed1b8Smatthias.ringwald } 11166772a24cSmatthias.ringwald break; 11176772a24cSmatthias.ringwald 1118c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 11193a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 11203a9fb326S[email protected] (*hci_stack->control->hw_error)(); 11217586ee35S[email protected] } else { 11227586ee35S[email protected] // if no special requests, just reboot stack 11237586ee35S[email protected] hci_power_control_off(); 11247586ee35S[email protected] hci_power_control_on(); 1125c68bdf90Smatthias.ringwald } 1126c68bdf90Smatthias.ringwald break; 1127c68bdf90Smatthias.ringwald 11286b4af23dS[email protected] case DAEMON_EVENT_HCI_PACKET_SENT: 11296b4af23dS[email protected] // free packet buffer for asynchronous transport 11306b4af23dS[email protected] if (hci_transport_synchronous()) break; 11316b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 11326b4af23dS[email protected] break; 11336b4af23dS[email protected] 11345909f7f2Smatthias.ringwald #ifdef HAVE_BLE 11355909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 11365909f7f2Smatthias.ringwald switch (packet[2]){ 113757c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 11382e440c8aS[email protected] log_info("advertising report received\n"); 11397bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 114057c9da5bS[email protected] le_handle_advertisement_report(packet, size); 11417bdc6798S[email protected] break; 11425909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 11435909f7f2Smatthias.ringwald // Connection management 11445909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 11457bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 114696a45072S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr)); 11475909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 114896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 11495909f7f2Smatthias.ringwald if (packet[3]){ 11505909f7f2Smatthias.ringwald if (conn){ 11515909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 11523a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 11535909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 11545909f7f2Smatthias.ringwald } 11555909f7f2Smatthias.ringwald // if authentication error, also delete link key 11565909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 11575909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 11585909f7f2Smatthias.ringwald } 11595909f7f2Smatthias.ringwald break; 11605909f7f2Smatthias.ringwald } 11615909f7f2Smatthias.ringwald if (!conn){ 116296a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 11635909f7f2Smatthias.ringwald } 11645909f7f2Smatthias.ringwald if (!conn){ 11655909f7f2Smatthias.ringwald // no memory 11665909f7f2Smatthias.ringwald break; 11675909f7f2Smatthias.ringwald } 11685909f7f2Smatthias.ringwald 11695909f7f2Smatthias.ringwald conn->state = OPEN; 11705909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 11715909f7f2Smatthias.ringwald 11725909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 11735909f7f2Smatthias.ringwald 11745909f7f2Smatthias.ringwald // restart timer 11755909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 11765909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 11775909f7f2Smatthias.ringwald 11785909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 11795909f7f2Smatthias.ringwald 11805909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 11815909f7f2Smatthias.ringwald break; 11825909f7f2Smatthias.ringwald 118365a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 118465a46ef3S[email protected] 11855909f7f2Smatthias.ringwald default: 11865909f7f2Smatthias.ringwald break; 11875909f7f2Smatthias.ringwald } 11885909f7f2Smatthias.ringwald break; 11895909f7f2Smatthias.ringwald #endif 11906772a24cSmatthias.ringwald default: 11916772a24cSmatthias.ringwald break; 1192fe1ed1b8Smatthias.ringwald } 1193fe1ed1b8Smatthias.ringwald 11943429f56bSmatthias.ringwald // handle BT initialization 11953a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 11966155b3d3S[email protected] hci_initializing_event_handler(packet, size); 1197c9af4d3fS[email protected] } 119822909952Smatthias.ringwald 119989db417bSmatthias.ringwald // help with BT sleep 12003a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 12013a9fb326S[email protected] && hci_stack->substate == 1 120289db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 12033a9fb326S[email protected] hci_stack->substate++; 120489db417bSmatthias.ringwald } 120589db417bSmatthias.ringwald 12063a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 120794ab26f8Smatthias.ringwald 120894ab26f8Smatthias.ringwald // execute main loop 120994ab26f8Smatthias.ringwald hci_run(); 121016833f0aSmatthias.ringwald } 121116833f0aSmatthias.ringwald 12120a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 121310e830c9Smatthias.ringwald switch (packet_type) { 121410e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 121510e830c9Smatthias.ringwald event_handler(packet, size); 121610e830c9Smatthias.ringwald break; 121710e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 121810e830c9Smatthias.ringwald acl_handler(packet, size); 121910e830c9Smatthias.ringwald break; 122010e830c9Smatthias.ringwald default: 122110e830c9Smatthias.ringwald break; 122210e830c9Smatthias.ringwald } 122310e830c9Smatthias.ringwald } 122410e830c9Smatthias.ringwald 1225fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 12262718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 12273a9fb326S[email protected] hci_stack->packet_handler = handler; 122816833f0aSmatthias.ringwald } 122916833f0aSmatthias.ringwald 12306155b3d3S[email protected] static void hci_state_reset(){ 1231595bdbfbS[email protected] // no connections yet 1232595bdbfbS[email protected] hci_stack->connections = NULL; 123374308b23Smatthias.ringwald 123474308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 123574308b23Smatthias.ringwald // hci_stack->discoverable = 0; 123674308b23Smatthias.ringwald // hci_stack->connectable = 0; 123774308b23Smatthias.ringwald // hci_stack->bondable = 1; 1238595bdbfbS[email protected] 123944935e40S[email protected] // buffer is free 124044935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 124144935e40S[email protected] 1242595bdbfbS[email protected] // no pending cmds 1243595bdbfbS[email protected] hci_stack->decline_reason = 0; 1244595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 1245595bdbfbS[email protected] 1246595bdbfbS[email protected] // LE 1247595bdbfbS[email protected] hci_stack->adv_addr_type = 0; 1248595bdbfbS[email protected] memset(hci_stack->adv_address, 0, 6); 1249595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1250e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1251595bdbfbS[email protected] } 1252595bdbfbS[email protected] 12534f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 1254475c8125Smatthias.ringwald 12553a9fb326S[email protected] #ifdef HAVE_MALLOC 12563a9fb326S[email protected] if (!hci_stack) { 12573a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 12583a9fb326S[email protected] } 12593a9fb326S[email protected] #else 12603a9fb326S[email protected] hci_stack = &hci_stack_static; 12613a9fb326S[email protected] #endif 126266fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 12633a9fb326S[email protected] 1264475c8125Smatthias.ringwald // reference to use transport layer implementation 12653a9fb326S[email protected] hci_stack->hci_transport = transport; 1266475c8125Smatthias.ringwald 126711e23e5fSmatthias.ringwald // references to used control implementation 12683a9fb326S[email protected] hci_stack->control = control; 126911e23e5fSmatthias.ringwald 127011e23e5fSmatthias.ringwald // reference to used config 12713a9fb326S[email protected] hci_stack->config = config; 127211e23e5fSmatthias.ringwald 127316833f0aSmatthias.ringwald // higher level handler 12743a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 127516833f0aSmatthias.ringwald 1276404843c1Smatthias.ringwald // store and open remote device db 12773a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 12783a9fb326S[email protected] if (hci_stack->remote_device_db) { 12793a9fb326S[email protected] hci_stack->remote_device_db->open(); 1280404843c1Smatthias.ringwald } 128129d53098Smatthias.ringwald 12828fcba05dSmatthias.ringwald // max acl payload size defined in config.h 12833a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 12848fcba05dSmatthias.ringwald 128516833f0aSmatthias.ringwald // register packet handlers with transport 128610e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 1287f5454fc6Smatthias.ringwald 12883a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 1289e2386ba1S[email protected] 1290e2386ba1S[email protected] // class of device 12913a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 1292a45d6b9fS[email protected] 1293f20168b8Smatthias.ringwald // bondable by default 1294f20168b8Smatthias.ringwald hci_stack->bondable = 1; 1295f20168b8Smatthias.ringwald 129663048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 12973a9fb326S[email protected] hci_stack->ssp_enable = 1; 12983a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 12993a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 13003a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 130169a97523S[email protected] 1302595bdbfbS[email protected] hci_state_reset(); 1303475c8125Smatthias.ringwald } 1304475c8125Smatthias.ringwald 1305404843c1Smatthias.ringwald void hci_close(){ 1306404843c1Smatthias.ringwald // close remote device db 13073a9fb326S[email protected] if (hci_stack->remote_device_db) { 13083a9fb326S[email protected] hci_stack->remote_device_db->close(); 1309404843c1Smatthias.ringwald } 13103a9fb326S[email protected] while (hci_stack->connections) { 1311bc68afe2S[email protected] // cancel all l2cap connections 1312bc68afe2S[email protected] hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host 13133a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 1314f5454fc6Smatthias.ringwald } 1315f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 13163a9fb326S[email protected] 13173a9fb326S[email protected] #ifdef HAVE_MALLOC 13183a9fb326S[email protected] free(hci_stack); 13193a9fb326S[email protected] #endif 13203a9fb326S[email protected] hci_stack = NULL; 1321404843c1Smatthias.ringwald } 1322404843c1Smatthias.ringwald 13239e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 13249e61646fS[email protected] hci_stack->class_of_device = class_of_device; 13259e61646fS[email protected] } 13269e61646fS[email protected] 132766fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 132866fb9560S[email protected] disable_l2cap_timeouts = 1; 132966fb9560S[email protected] } 13308d213e1aSmatthias.ringwald // State-Module-Driver overview 13318d213e1aSmatthias.ringwald // state module low-level 13328d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 13338d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 13348d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 13358d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 1336d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 1337d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 1338c7e0c5f6Smatthias.ringwald 133940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 13407301ad89Smatthias.ringwald 1341038bc64cSmatthias.ringwald // power on 1342f9a30166Smatthias.ringwald int err = 0; 13433a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 13443a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1345f9a30166Smatthias.ringwald } 1346038bc64cSmatthias.ringwald if (err){ 13477d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 1348038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1349038bc64cSmatthias.ringwald return err; 1350038bc64cSmatthias.ringwald } 1351038bc64cSmatthias.ringwald 1352038bc64cSmatthias.ringwald // open low-level device 13533a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1354038bc64cSmatthias.ringwald if (err){ 13557d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 13563a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 13573a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1358f9a30166Smatthias.ringwald } 1359038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1360038bc64cSmatthias.ringwald return err; 1361038bc64cSmatthias.ringwald } 13628d213e1aSmatthias.ringwald return 0; 13638d213e1aSmatthias.ringwald } 1364038bc64cSmatthias.ringwald 136540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 13668d213e1aSmatthias.ringwald 13677b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 13689418f9c9Smatthias.ringwald 13698d213e1aSmatthias.ringwald // close low-level device 13703a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 13718d213e1aSmatthias.ringwald 13727b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 13739418f9c9Smatthias.ringwald 13748d213e1aSmatthias.ringwald // power off 13753a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 13763a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 13778d213e1aSmatthias.ringwald } 13789418f9c9Smatthias.ringwald 13797b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 13809418f9c9Smatthias.ringwald 13813a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 138272ea5239Smatthias.ringwald } 138372ea5239Smatthias.ringwald 138440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 138572ea5239Smatthias.ringwald 13867b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 13873144bce4Smatthias.ringwald 1388b429b9b7Smatthias.ringwald #if 0 1389b429b9b7Smatthias.ringwald // don't close serial port during sleep 1390b429b9b7Smatthias.ringwald 139172ea5239Smatthias.ringwald // close low-level device 13923a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1393b429b9b7Smatthias.ringwald #endif 139472ea5239Smatthias.ringwald 139572ea5239Smatthias.ringwald // sleep mode 13963a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 13973a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 139872ea5239Smatthias.ringwald } 1399b429b9b7Smatthias.ringwald 14003a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 14018d213e1aSmatthias.ringwald } 14028d213e1aSmatthias.ringwald 140340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1404b429b9b7Smatthias.ringwald 14057b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 1406b429b9b7Smatthias.ringwald 1407b429b9b7Smatthias.ringwald // wake on 14083a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 14093a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1410b429b9b7Smatthias.ringwald } 1411b429b9b7Smatthias.ringwald 1412b429b9b7Smatthias.ringwald #if 0 1413b429b9b7Smatthias.ringwald // open low-level device 14143a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1415b429b9b7Smatthias.ringwald if (err){ 14167d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 14173a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 14183a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1419b429b9b7Smatthias.ringwald } 1420b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1421b429b9b7Smatthias.ringwald return err; 1422b429b9b7Smatthias.ringwald } 1423b429b9b7Smatthias.ringwald #endif 1424b429b9b7Smatthias.ringwald 1425b429b9b7Smatthias.ringwald return 0; 1426b429b9b7Smatthias.ringwald } 1427b429b9b7Smatthias.ringwald 142844935e40S[email protected] static void hci_power_transition_to_initializing(void){ 142944935e40S[email protected] // set up state machine 143044935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 143144935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 143244935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 143344935e40S[email protected] hci_stack->substate = 0; 143444935e40S[email protected] } 1435b429b9b7Smatthias.ringwald 14368d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1437d661ed19Smatthias.ringwald 14383a9fb326S[email protected] log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state); 1439d661ed19Smatthias.ringwald 14408d213e1aSmatthias.ringwald int err = 0; 14413a9fb326S[email protected] switch (hci_stack->state){ 14428d213e1aSmatthias.ringwald 14438d213e1aSmatthias.ringwald case HCI_STATE_OFF: 14448d213e1aSmatthias.ringwald switch (power_mode){ 14458d213e1aSmatthias.ringwald case HCI_POWER_ON: 14468d213e1aSmatthias.ringwald err = hci_power_control_on(); 144797b61c7bS[email protected] if (err) { 144897b61c7bS[email protected] log_error("hci_power_control_on() error %u", err); 144997b61c7bS[email protected] return err; 145097b61c7bS[email protected] } 145144935e40S[email protected] hci_power_transition_to_initializing(); 14528d213e1aSmatthias.ringwald break; 14538d213e1aSmatthias.ringwald case HCI_POWER_OFF: 14548d213e1aSmatthias.ringwald // do nothing 14558d213e1aSmatthias.ringwald break; 14568d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1457b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 14588d213e1aSmatthias.ringwald break; 14598d213e1aSmatthias.ringwald } 14608d213e1aSmatthias.ringwald break; 14617301ad89Smatthias.ringwald 14628d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 14638d213e1aSmatthias.ringwald switch (power_mode){ 14648d213e1aSmatthias.ringwald case HCI_POWER_ON: 14658d213e1aSmatthias.ringwald // do nothing 14668d213e1aSmatthias.ringwald break; 14678d213e1aSmatthias.ringwald case HCI_POWER_OFF: 14688d213e1aSmatthias.ringwald // no connections yet, just turn it off 14698d213e1aSmatthias.ringwald hci_power_control_off(); 14708d213e1aSmatthias.ringwald break; 14718d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1472b546ac54Smatthias.ringwald // no connections yet, just turn it off 147372ea5239Smatthias.ringwald hci_power_control_sleep(); 14748d213e1aSmatthias.ringwald break; 14758d213e1aSmatthias.ringwald } 14768d213e1aSmatthias.ringwald break; 14777301ad89Smatthias.ringwald 14788d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 14798d213e1aSmatthias.ringwald switch (power_mode){ 14808d213e1aSmatthias.ringwald case HCI_POWER_ON: 14818d213e1aSmatthias.ringwald // do nothing 14828d213e1aSmatthias.ringwald break; 14838d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1484c7e0c5f6Smatthias.ringwald // see hci_run 14853a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 14868d213e1aSmatthias.ringwald break; 14878d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1488b546ac54Smatthias.ringwald // see hci_run 14893a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 14903a9fb326S[email protected] hci_stack->substate = 0; 14918d213e1aSmatthias.ringwald break; 14928d213e1aSmatthias.ringwald } 14938d213e1aSmatthias.ringwald break; 14947301ad89Smatthias.ringwald 14958d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 14968d213e1aSmatthias.ringwald switch (power_mode){ 14978d213e1aSmatthias.ringwald case HCI_POWER_ON: 149844935e40S[email protected] hci_power_transition_to_initializing(); 14998d213e1aSmatthias.ringwald break; 15008d213e1aSmatthias.ringwald case HCI_POWER_OFF: 15018d213e1aSmatthias.ringwald // do nothing 15028d213e1aSmatthias.ringwald break; 15038d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1504b546ac54Smatthias.ringwald // see hci_run 15053a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 15063a9fb326S[email protected] hci_stack->substate = 0; 15078d213e1aSmatthias.ringwald break; 15088d213e1aSmatthias.ringwald } 15098d213e1aSmatthias.ringwald break; 15108d213e1aSmatthias.ringwald 15118d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 15128d213e1aSmatthias.ringwald switch (power_mode){ 15138d213e1aSmatthias.ringwald case HCI_POWER_ON: 151428171530Smatthias.ringwald 151528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 151628171530Smatthias.ringwald // nothing to do, if H4 supports power management 151728171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 15183a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 15193a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 152028171530Smatthias.ringwald break; 152128171530Smatthias.ringwald } 152228171530Smatthias.ringwald #endif 152344935e40S[email protected] hci_power_transition_to_initializing(); 15248d213e1aSmatthias.ringwald break; 15258d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1526b546ac54Smatthias.ringwald // see hci_run 15273a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 15288d213e1aSmatthias.ringwald break; 15298d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1530b546ac54Smatthias.ringwald // do nothing 15318d213e1aSmatthias.ringwald break; 15328d213e1aSmatthias.ringwald } 15338d213e1aSmatthias.ringwald break; 15348d213e1aSmatthias.ringwald 15358d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 15368d213e1aSmatthias.ringwald switch (power_mode){ 15378d213e1aSmatthias.ringwald case HCI_POWER_ON: 153828171530Smatthias.ringwald 153928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 154028171530Smatthias.ringwald // nothing to do, if H4 supports power management 154128171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 15423a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 15433a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1544758b46ceSmatthias.ringwald hci_update_scan_enable(); 154528171530Smatthias.ringwald break; 154628171530Smatthias.ringwald } 154728171530Smatthias.ringwald #endif 15483144bce4Smatthias.ringwald err = hci_power_control_wake(); 15493144bce4Smatthias.ringwald if (err) return err; 155044935e40S[email protected] hci_power_transition_to_initializing(); 15518d213e1aSmatthias.ringwald break; 15528d213e1aSmatthias.ringwald case HCI_POWER_OFF: 15533a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 15548d213e1aSmatthias.ringwald break; 15558d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1556b546ac54Smatthias.ringwald // do nothing 15578d213e1aSmatthias.ringwald break; 15588d213e1aSmatthias.ringwald } 15598d213e1aSmatthias.ringwald break; 156011e23e5fSmatthias.ringwald } 156168d92d03Smatthias.ringwald 1562038bc64cSmatthias.ringwald // create internal event 1563ee8bf225Smatthias.ringwald hci_emit_state(); 1564ee8bf225Smatthias.ringwald 156568d92d03Smatthias.ringwald // trigger next/first action 156668d92d03Smatthias.ringwald hci_run(); 156768d92d03Smatthias.ringwald 1568475c8125Smatthias.ringwald return 0; 1569475c8125Smatthias.ringwald } 1570475c8125Smatthias.ringwald 1571758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1572758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 15733a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1574758b46ceSmatthias.ringwald hci_run(); 1575758b46ceSmatthias.ringwald } 1576758b46ceSmatthias.ringwald 1577381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1578381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1579381fbed8Smatthias.ringwald 15803a9fb326S[email protected] if (hci_stack->discoverable == enable){ 15813a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1582381fbed8Smatthias.ringwald return; 1583381fbed8Smatthias.ringwald } 1584381fbed8Smatthias.ringwald 15853a9fb326S[email protected] hci_stack->discoverable = enable; 1586758b46ceSmatthias.ringwald hci_update_scan_enable(); 1587758b46ceSmatthias.ringwald } 1588b031bebbSmatthias.ringwald 1589758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1590758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1591758b46ceSmatthias.ringwald 1592758b46ceSmatthias.ringwald // don't emit event 15933a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1594758b46ceSmatthias.ringwald 15953a9fb326S[email protected] hci_stack->connectable = enable; 1596758b46ceSmatthias.ringwald hci_update_scan_enable(); 1597381fbed8Smatthias.ringwald } 1598381fbed8Smatthias.ringwald 15995061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 16003a9fb326S[email protected] return &hci_stack->local_bd_addr; 16015061f3afS[email protected] } 16025061f3afS[email protected] 160306b35ec0Smatthias.ringwald void hci_run(){ 16048a485f27Smatthias.ringwald 160532ab9390Smatthias.ringwald hci_connection_t * connection; 160632ab9390Smatthias.ringwald linked_item_t * it; 160732ab9390Smatthias.ringwald 1608d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 1609ce4c8fabSmatthias.ringwald 1610b031bebbSmatthias.ringwald // global/non-connection oriented commands 1611b031bebbSmatthias.ringwald 1612b031bebbSmatthias.ringwald // decline incoming connections 16133a9fb326S[email protected] if (hci_stack->decline_reason){ 16143a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 16153a9fb326S[email protected] hci_stack->decline_reason = 0; 16163a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1617dbe1a790S[email protected] return; 1618ce4c8fabSmatthias.ringwald } 1619ce4c8fabSmatthias.ringwald 1620b031bebbSmatthias.ringwald // send scan enable 16213a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 16223a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 16233a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1624dbe1a790S[email protected] return; 1625b031bebbSmatthias.ringwald } 1626b031bebbSmatthias.ringwald 1627b2f949feS[email protected] #ifdef HAVE_BLE 16287bdc6798S[email protected] // handle le scan 16297bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 16307bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 16317bdc6798S[email protected] case LE_START_SCAN: 16327bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 16337bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 16347bdc6798S[email protected] return; 16357bdc6798S[email protected] 16367bdc6798S[email protected] case LE_STOP_SCAN: 16377bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 16387bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 16397bdc6798S[email protected] return; 16407bdc6798S[email protected] default: 16417bdc6798S[email protected] break; 16427bdc6798S[email protected] } 1643e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 1644e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 1645e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 1646e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1647e2602ea2Smatthias.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); 1648e2602ea2Smatthias.ringwald return; 1649e2602ea2Smatthias.ringwald } 16507bdc6798S[email protected] } 1651b2f949feS[email protected] #endif 16527bdc6798S[email protected] 165332ab9390Smatthias.ringwald // send pending HCI commands 16543a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 1655b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 165632ab9390Smatthias.ringwald 16570bf6344aS[email protected] switch(connection->state){ 16580bf6344aS[email protected] case SEND_CREATE_CONNECTION: 16594f3229d8S[email protected] switch(connection->address_type){ 16604f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 1661564fca32S[email protected] log_info("sending hci_create_connection\n"); 1662ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 16634f3229d8S[email protected] break; 16644f3229d8S[email protected] default: 1665b2f949feS[email protected] #ifdef HAVE_BLE 1666564fca32S[email protected] log_info("sending hci_le_create_connection\n"); 16674f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 1668b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 1669b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 16704f3229d8S[email protected] 0, // don't use whitelist 16714f3229d8S[email protected] connection->address_type, // peer address type 16724f3229d8S[email protected] connection->address, // peer bd addr 1673b2571179Smatthias.ringwald hci_stack->adv_addr_type, // our addr type: 1674b2571179Smatthias.ringwald 0x0008, // conn interval min 1675b2571179Smatthias.ringwald 0x0018, // conn interval max 16764f3229d8S[email protected] 0, // conn latency 1677b2571179Smatthias.ringwald 0x0048, // supervision timeout 1678b2571179Smatthias.ringwald 0x0001, // min ce length 1679b2571179Smatthias.ringwald 0x0001 // max ce length 16804f3229d8S[email protected] ); 16814f3229d8S[email protected] 16824f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 1683b2f949feS[email protected] #endif 16844f3229d8S[email protected] break; 16854f3229d8S[email protected] } 1686ad83dc6aS[email protected] return; 1687ad83dc6aS[email protected] 16880bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 16897b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 169032ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 169134d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1692dbe1a790S[email protected] return; 16930bf6344aS[email protected] 1694a6725849S[email protected] #ifdef HAVE_BLE 16950bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 16960bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 16970bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 16980bf6344aS[email protected] return; 1699a6725849S[email protected] #endif 17000bf6344aS[email protected] case SEND_DISCONNECT: 17010bf6344aS[email protected] connection->state = SENT_DISCONNECT; 17027851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 17030bf6344aS[email protected] return; 17040bf6344aS[email protected] 17050bf6344aS[email protected] default: 17060bf6344aS[email protected] break; 1707c7e0c5f6Smatthias.ringwald } 1708c7e0c5f6Smatthias.ringwald 170932ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 171034d2123cS[email protected] log_info("responding to link key request\n"); 171134d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 171232ab9390Smatthias.ringwald link_key_t link_key; 1713c77e8838S[email protected] link_key_type_t link_key_type; 17143a9fb326S[email protected] if ( hci_stack->remote_device_db 17153a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 171634d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 17179ab95c90S[email protected] connection->link_key_type = link_key_type; 171832ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 171932ab9390Smatthias.ringwald } else { 172032ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 172132ab9390Smatthias.ringwald } 1722dbe1a790S[email protected] return; 172332ab9390Smatthias.ringwald } 17241d6b20aeS[email protected] 1725899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 17264c57c146S[email protected] log_info("denying to pin request\n"); 1727899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 172834d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 17294c57c146S[email protected] return; 17304c57c146S[email protected] } 17314c57c146S[email protected] 1732dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 173334d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 173482d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 173582d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 1736106d6d11S[email protected] // tweak authentication requirements 17373a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1738106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 17399faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 17409faad3abS[email protected] } 17419faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 17429faad3abS[email protected] authreq |= 1; 1743106d6d11S[email protected] } 17443a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1745f8fb5f6eS[email protected] } else { 1746f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1747f8fb5f6eS[email protected] } 1748dbe1a790S[email protected] return; 174932ab9390Smatthias.ringwald } 175032ab9390Smatthias.ringwald 1751dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1752dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 175334d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1754dbe1a790S[email protected] return; 1755dbe1a790S[email protected] } 1756dbe1a790S[email protected] 1757dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1758dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 175934d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1760dbe1a790S[email protected] return; 1761dbe1a790S[email protected] } 1762afd4e962S[email protected] 1763afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1764afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 176534d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 17662bd8b7e7S[email protected] return; 17672bd8b7e7S[email protected] } 17682bd8b7e7S[email protected] 17692bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 17702bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 177134d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 177234d2123cS[email protected] return; 177334d2123cS[email protected] } 1774ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1775ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 177652d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 1777ad83dc6aS[email protected] return; 1778ad83dc6aS[email protected] } 177934d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 178034d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 178134d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 17822bd8b7e7S[email protected] return; 1783afd4e962S[email protected] } 1784dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 1785dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 1786dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 1787dce78009S[email protected] return; 1788dce78009S[email protected] } 1789dbe1a790S[email protected] } 1790c7e0c5f6Smatthias.ringwald 17913a9fb326S[email protected] switch (hci_stack->state){ 17923429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 1793bd88fde9S[email protected] hci_initializing_state_machine(); 17943429f56bSmatthias.ringwald break; 1795c7e0c5f6Smatthias.ringwald 1796c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1797c7e0c5f6Smatthias.ringwald 17987b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1799c7e0c5f6Smatthias.ringwald // close all open connections 18003a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 1801c7e0c5f6Smatthias.ringwald if (connection){ 180232ab9390Smatthias.ringwald 1803c7e0c5f6Smatthias.ringwald // send disconnect 1804d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 180532ab9390Smatthias.ringwald 180679bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 18076ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1808c7e0c5f6Smatthias.ringwald 1809c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1810c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1811c7e0c5f6Smatthias.ringwald return; 1812c7e0c5f6Smatthias.ringwald } 18137b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1814c7e0c5f6Smatthias.ringwald 181572ea5239Smatthias.ringwald // switch mode 1816c7e0c5f6Smatthias.ringwald hci_power_control_off(); 18179418f9c9Smatthias.ringwald 18187b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 181972ea5239Smatthias.ringwald hci_emit_state(); 18207b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 182172ea5239Smatthias.ringwald break; 1822c7e0c5f6Smatthias.ringwald 182372ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 18243a9fb326S[email protected] switch(hci_stack->substate) { 182589db417bSmatthias.ringwald case 0: 18267b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 182772ea5239Smatthias.ringwald // close all open connections 18283a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 182966da7044Smatthias.ringwald 183028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 183166da7044Smatthias.ringwald // don't close connections, if H4 supports power management 183266da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 183366da7044Smatthias.ringwald connection = NULL; 183466da7044Smatthias.ringwald } 183566da7044Smatthias.ringwald #endif 183672ea5239Smatthias.ringwald if (connection){ 183732ab9390Smatthias.ringwald 183872ea5239Smatthias.ringwald // send disconnect 1839d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 184032ab9390Smatthias.ringwald 184179bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 18426ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 184372ea5239Smatthias.ringwald 184472ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 184572ea5239Smatthias.ringwald hci_shutdown_connection(connection); 184672ea5239Smatthias.ringwald return; 184772ea5239Smatthias.ringwald } 184872ea5239Smatthias.ringwald 184992368cd3S[email protected] if (hci_classic_supported()){ 185089db417bSmatthias.ringwald // disable page and inquiry scan 1851d94d3cafS[email protected] if (!hci_can_send_command_packet_now()) return; 185232ab9390Smatthias.ringwald 185392368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 18543a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 185589db417bSmatthias.ringwald 185689db417bSmatthias.ringwald // continue in next sub state 18573a9fb326S[email protected] hci_stack->substate++; 185889db417bSmatthias.ringwald break; 185992368cd3S[email protected] } 186092368cd3S[email protected] // fall through for ble-only chips 186192368cd3S[email protected] 186289db417bSmatthias.ringwald case 2: 18637b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 186428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 186528171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 186628171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 186728171530Smatthias.ringwald // SLEEP MODE reached 18683a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 186928171530Smatthias.ringwald hci_emit_state(); 187028171530Smatthias.ringwald break; 187128171530Smatthias.ringwald } 187228171530Smatthias.ringwald #endif 187372ea5239Smatthias.ringwald // switch mode 18743a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 1875c7e0c5f6Smatthias.ringwald hci_emit_state(); 187628171530Smatthias.ringwald break; 187728171530Smatthias.ringwald 187889db417bSmatthias.ringwald default: 187989db417bSmatthias.ringwald break; 188089db417bSmatthias.ringwald } 1881c7e0c5f6Smatthias.ringwald break; 1882c7e0c5f6Smatthias.ringwald 18833429f56bSmatthias.ringwald default: 18843429f56bSmatthias.ringwald break; 18851f504dbdSmatthias.ringwald } 18863429f56bSmatthias.ringwald } 188716833f0aSmatthias.ringwald 188831452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1889c8e4258aSmatthias.ringwald bd_addr_t addr; 1890c8e4258aSmatthias.ringwald hci_connection_t * conn; 1891c8e4258aSmatthias.ringwald // house-keeping 1892c8e4258aSmatthias.ringwald 1893c8e4258aSmatthias.ringwald // create_connection? 1894c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1895c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1896339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1897c8e4258aSmatthias.ringwald 189896a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 1899ad83dc6aS[email protected] if (!conn){ 190096a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 190117f1ba2aSmatthias.ringwald if (!conn){ 190217f1ba2aSmatthias.ringwald // notify client that alloc failed 190317f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 190417f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 190517f1ba2aSmatthias.ringwald } 1906ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 1907ad83dc6aS[email protected] } 1908ad83dc6aS[email protected] log_info("conn state %u", conn->state); 1909ad83dc6aS[email protected] switch (conn->state){ 1910ad83dc6aS[email protected] // if connection active exists 1911ad83dc6aS[email protected] case OPEN: 191262bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 1913ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 191462bda3fbS[email protected] return 0; 1915ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 1916ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 1917ad83dc6aS[email protected] break; 1918ad83dc6aS[email protected] default: 1919ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 1920ad83dc6aS[email protected] return 0; 1921ad83dc6aS[email protected] } 1922c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1923c8e4258aSmatthias.ringwald } 1924c8e4258aSmatthias.ringwald 19257fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 19267fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 19277fde4af9Smatthias.ringwald } 19287fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 19297fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 19307fde4af9Smatthias.ringwald } 19317fde4af9Smatthias.ringwald 19328ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 19333a9fb326S[email protected] if (hci_stack->remote_device_db){ 19348ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 19353a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 19368ef73945Smatthias.ringwald } 19378ef73945Smatthias.ringwald } 1938c8e4258aSmatthias.ringwald 19396724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 19406724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 19416724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 194296a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 19436724cd9eS[email protected] if (conn){ 19446724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 19456724cd9eS[email protected] } 19466724cd9eS[email protected] } 19476724cd9eS[email protected] 19486724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 19496724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 19506724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 19516724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 19526724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 195396a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 19546724cd9eS[email protected] if (conn){ 19556724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 19566724cd9eS[email protected] } 19576724cd9eS[email protected] } 19586724cd9eS[email protected] 195969a97523S[email protected] #ifdef HAVE_BLE 196069a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 19613a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 196269a97523S[email protected] } 196369a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 19643a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 196569a97523S[email protected] } 196669a97523S[email protected] #endif 196769a97523S[email protected] 19683a9fb326S[email protected] hci_stack->num_cmd_packets--; 19696b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 19706b4af23dS[email protected] 19716b4af23dS[email protected] // free packet buffer for synchronous transport implementations 1972c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 19736b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 19746b4af23dS[email protected] } 19756b4af23dS[email protected] 19766b4af23dS[email protected] return err; 197731452debSmatthias.ringwald } 19788adf0ddaSmatthias.ringwald 19792bd8b7e7S[email protected] // disconnect because of security block 19802bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 19812bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 19822bd8b7e7S[email protected] if (!connection) return; 19832bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 19842bd8b7e7S[email protected] } 19852bd8b7e7S[email protected] 19862bd8b7e7S[email protected] 1987dbe1a790S[email protected] // Configure Secure Simple Pairing 1988dbe1a790S[email protected] 1989dbe1a790S[email protected] // enable will enable SSP during init 1990dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 19913a9fb326S[email protected] hci_stack->ssp_enable = enable; 1992dbe1a790S[email protected] } 1993dbe1a790S[email protected] 19942bd8b7e7S[email protected] int hci_local_ssp_activated(){ 19953a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 19962bd8b7e7S[email protected] } 19972bd8b7e7S[email protected] 1998dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1999dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 20003a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 2001dbe1a790S[email protected] } 2002dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 20033a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 2004dbe1a790S[email protected] } 2005dbe1a790S[email protected] 2006dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 2007dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 20083a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 2009dbe1a790S[email protected] } 2010dbe1a790S[email protected] 20111cd208adSmatthias.ringwald /** 20121cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 20131cd208adSmatthias.ringwald */ 2014fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 20155127cc62S[email protected] 2016d94d3cafS[email protected] if (!hci_can_send_command_packet_now()){ 20179d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 20189d14b626S[email protected] return 0; 20199d14b626S[email protected] } 20209d14b626S[email protected] 20215127cc62S[email protected] // for HCI INITIALIZATION 20225127cc62S[email protected] // printf("hci_send_cmd: opcode %04x\n", cmd->opcode); 20235127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 20245127cc62S[email protected] 20259d14b626S[email protected] hci_reserve_packet_buffer(); 20269d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 20279d14b626S[email protected] 20281cd208adSmatthias.ringwald va_list argptr; 20291cd208adSmatthias.ringwald va_start(argptr, cmd); 20309d14b626S[email protected] uint16_t size = hci_create_cmd_internal(packet, cmd, argptr); 20311cd208adSmatthias.ringwald va_end(argptr); 20329d14b626S[email protected] 20339d14b626S[email protected] return hci_send_cmd_packet(packet, size); 203493b8dc03Smatthias.ringwald } 2035c8e4258aSmatthias.ringwald 2036ee091cf1Smatthias.ringwald // Create various non-HCI events. 2037ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 2038ee091cf1Smatthias.ringwald 2039c8e4258aSmatthias.ringwald void hci_emit_state(){ 20403a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 2041425d1371Smatthias.ringwald uint8_t event[3]; 204280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 2043425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 20443a9fb326S[email protected] event[2] = hci_stack->state; 2045425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20463a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2047c8e4258aSmatthias.ringwald } 2048c8e4258aSmatthias.ringwald 204917f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 2050425d1371Smatthias.ringwald uint8_t event[13]; 2051c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 2052425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 205317f1ba2aSmatthias.ringwald event[2] = status; 2054c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 2055c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 2056c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 2057c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 2058425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20593a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2060c8e4258aSmatthias.ringwald } 2061c8e4258aSmatthias.ringwald 20624f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){ 20634f3229d8S[email protected] uint8_t event[21]; 20644f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 20654f3229d8S[email protected] event[1] = sizeof(event) - 2; 20664f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 20674f3229d8S[email protected] event[3] = status; 20684f3229d8S[email protected] bt_store_16(event, 4, conn->con_handle); 20694f3229d8S[email protected] event[6] = 0; // TODO: role 20704f3229d8S[email protected] event[7] = conn->address_type; 20714f3229d8S[email protected] bt_flip_addr(&event[8], conn->address); 20724f3229d8S[email protected] bt_store_16(event, 14, 0); // interval 20734f3229d8S[email protected] bt_store_16(event, 16, 0); // latency 20744f3229d8S[email protected] bt_store_16(event, 18, 0); // supervision timeout 20754f3229d8S[email protected] event[20] = 0; // master clock accuracy 20764f3229d8S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20774f3229d8S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 20784f3229d8S[email protected] } 20794f3229d8S[email protected] 20803c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 2081425d1371Smatthias.ringwald uint8_t event[6]; 20823c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 2083e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 20843c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 20853c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 20863c4d4b90Smatthias.ringwald event[5] = reason; 2087425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20883a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 20893c4d4b90Smatthias.ringwald } 20903c4d4b90Smatthias.ringwald 2091ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 209266fb9560S[email protected] if (disable_l2cap_timeouts) return; 2093e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 2094425d1371Smatthias.ringwald uint8_t event[4]; 209580d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 2096425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2097ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 2098425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20993a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2100ee091cf1Smatthias.ringwald } 210143bfb1bdSmatthias.ringwald 210243bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 2103e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 2104425d1371Smatthias.ringwald uint8_t event[3]; 210580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 2106425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 210743bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 2108425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21093a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 211043bfb1bdSmatthias.ringwald } 2111038bc64cSmatthias.ringwald 2112038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 2113e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 2114425d1371Smatthias.ringwald uint8_t event[2]; 211580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 2116425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2117425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21183a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2119038bc64cSmatthias.ringwald } 21201b0e3922Smatthias.ringwald 212109ba8edeSmatthias.ringwald #ifndef EMBEDDED 21221b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 2123e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 2124425d1371Smatthias.ringwald uint8_t event[6]; 21251b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 2126425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2127425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 2128425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 2129425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 2130425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21313a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 21321b0e3922Smatthias.ringwald } 213309ba8edeSmatthias.ringwald #endif 21341b0e3922Smatthias.ringwald 21352ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 2136e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 2137425d1371Smatthias.ringwald uint8_t event[3]; 21382ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 2139425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 21402ed6235cSmatthias.ringwald event[2] = enabled; 2141425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21423a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 21432ed6235cSmatthias.ringwald } 2144627c2f45Smatthias.ringwald 2145627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 2146e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 2147627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 2148e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 2149f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 21502f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 2151f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 2152e0abb8e7S[email protected] 2153e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 2154e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 2155e0abb8e7S[email protected] 2156e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 21573a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 2158627c2f45Smatthias.ringwald } 2159381fbed8Smatthias.ringwald 2160381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 2161e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 2162425d1371Smatthias.ringwald uint8_t event[3]; 2163381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 2164425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2165381fbed8Smatthias.ringwald event[2] = enabled; 2166425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21673a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2168381fbed8Smatthias.ringwald } 2169458bf4e8S[email protected] 2170a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 2171df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 2172a00031e2S[email protected] uint8_t event[5]; 2173e00caf9cS[email protected] int pos = 0; 2174a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 2175e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 2176a00031e2S[email protected] bt_store_16(event, 2, con_handle); 2177e00caf9cS[email protected] pos += 2; 2178e00caf9cS[email protected] event[pos++] = level; 2179e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21803a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2181e00caf9cS[email protected] } 2182e00caf9cS[email protected] 2183ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){ 2184ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 2185ad83dc6aS[email protected] uint8_t event[9]; 2186ad83dc6aS[email protected] int pos = 0; 2187ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 2188ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 2189ad83dc6aS[email protected] event[pos++] = status; 2190ad83dc6aS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address); 2191ad83dc6aS[email protected] pos += 6; 2192ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21933a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2194ad83dc6aS[email protected] } 2195ad83dc6aS[email protected] 21962bd8b7e7S[email protected] // query if remote side supports SSP 21972bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 21982bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 21992bd8b7e7S[email protected] if (!connection) return 0; 22002bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 22012bd8b7e7S[email protected] } 22022bd8b7e7S[email protected] 2203df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 2204df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 2205df3354fcS[email protected] } 2206df3354fcS[email protected] 2207458bf4e8S[email protected] // GAP API 2208458bf4e8S[email protected] /** 2209458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 2210458bf4e8S[email protected] * @praram enabled 2211458bf4e8S[email protected] */ 22124c57c146S[email protected] void gap_set_bondable_mode(int enable){ 22133a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 2214458bf4e8S[email protected] } 2215cb230b9dS[email protected] 2216cb230b9dS[email protected] /** 221734d2123cS[email protected] * @brief map link keys to security levels 2218cb230b9dS[email protected] */ 221934d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 222034d2123cS[email protected] switch (link_key_type){ 22213c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 22223c68dfa9S[email protected] return LEVEL_4; 22233c68dfa9S[email protected] case COMBINATION_KEY: 22243c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 22253c68dfa9S[email protected] return LEVEL_3; 22263c68dfa9S[email protected] default: 22273c68dfa9S[email protected] return LEVEL_2; 22283c68dfa9S[email protected] } 2229cb230b9dS[email protected] } 2230cb230b9dS[email protected] 2231a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 223234d2123cS[email protected] if (!connection) return LEVEL_0; 223334d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 223434d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 223534d2123cS[email protected] } 223634d2123cS[email protected] 223734d2123cS[email protected] 2238106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 22395127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 2240106d6d11S[email protected] return level > LEVEL_2; 2241106d6d11S[email protected] } 2242106d6d11S[email protected] 224334d2123cS[email protected] /** 224434d2123cS[email protected] * @brief get current security level 224534d2123cS[email protected] */ 224634d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 224734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 224834d2123cS[email protected] if (!connection) return LEVEL_0; 224934d2123cS[email protected] return gap_security_level_for_connection(connection); 225034d2123cS[email protected] } 225134d2123cS[email protected] 2252cb230b9dS[email protected] /** 2253cb230b9dS[email protected] * @brief request connection to device to 2254cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 2255cb230b9dS[email protected] */ 225634d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 225734d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 225834d2123cS[email protected] if (!connection){ 2259a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 226034d2123cS[email protected] return; 226134d2123cS[email protected] } 226234d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 226334d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 226434d2123cS[email protected] if (current_level >= requested_level){ 2265a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 226634d2123cS[email protected] return; 226734d2123cS[email protected] } 2268a00031e2S[email protected] 226934d2123cS[email protected] connection->requested_security_level = requested_level; 2270a00031e2S[email protected] 2271fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 22723a9fb326S[email protected] if (hci_stack->remote_device_db){ 2273a00031e2S[email protected] link_key_type_t link_key_type; 2274a00031e2S[email protected] link_key_t link_key; 22753a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 2276a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 2277a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2278a00031e2S[email protected] return; 2279a00031e2S[email protected] } 2280a00031e2S[email protected] } 2281a00031e2S[email protected] } 2282a00031e2S[email protected] 22831eb2563eS[email protected] // try to authenticate connection 22841eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2285e80b2cf9S[email protected] hci_run(); 2286e00caf9cS[email protected] } 2287ad83dc6aS[email protected] 2288ad83dc6aS[email protected] /** 2289ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 2290ad83dc6aS[email protected] * @param device 2291ad83dc6aS[email protected] * @param request MITM protection 2292ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 2293ad83dc6aS[email protected] */ 2294ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 2295ad83dc6aS[email protected] 2296ad83dc6aS[email protected] // create connection state machine 229796a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 2298ad83dc6aS[email protected] 2299ad83dc6aS[email protected] if (!connection){ 2300ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 2301ad83dc6aS[email protected] } 2302ad83dc6aS[email protected] 2303ad83dc6aS[email protected] // delete linkn key 2304ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 2305ad83dc6aS[email protected] 2306ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 2307ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 2308ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 23095127cc62S[email protected] log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level); 2310ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 2311ad83dc6aS[email protected] 2312ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 2313ad83dc6aS[email protected] 2314ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 2315ad83dc6aS[email protected] // handle: authentication failure 2316ad83dc6aS[email protected] // handle: disconnect on done 2317ad83dc6aS[email protected] 2318ad83dc6aS[email protected] hci_run(); 2319ad83dc6aS[email protected] 2320ad83dc6aS[email protected] return 0; 2321ad83dc6aS[email protected] } 23228e618f72S[email protected] 23238e618f72S[email protected] void gap_set_local_name(const char * local_name){ 23248e618f72S[email protected] hci_stack->local_name = local_name; 23258e618f72S[email protected] } 23268e618f72S[email protected] 23277bdc6798S[email protected] le_command_status_t le_central_start_scan(){ 232836af3e18S[email protected] if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK; 23297bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 23307bdc6798S[email protected] hci_run(); 23317bdc6798S[email protected] return BLE_PERIPHERAL_OK; 23327bdc6798S[email protected] } 23338e618f72S[email protected] 23347bdc6798S[email protected] le_command_status_t le_central_stop_scan(){ 233536af3e18S[email protected] if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK; 23367bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 23377bdc6798S[email protected] hci_run(); 23387bdc6798S[email protected] return BLE_PERIPHERAL_OK; 23397bdc6798S[email protected] } 23404f3229d8S[email protected] 2341ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 2342ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 2343ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 2344ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 2345ef11999fSmatthias.ringwald hci_run(); 2346ef11999fSmatthias.ringwald } 23474f3229d8S[email protected] 23484f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){ 23494f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 23504f3229d8S[email protected] if (!conn){ 23511f479f8cS[email protected] log_info("le_central_connect: no connection exists yet, creating context"); 23524f3229d8S[email protected] conn = create_connection_for_bd_addr_and_type(*addr, addr_type); 23534f3229d8S[email protected] if (!conn){ 23544f3229d8S[email protected] // notify client that alloc failed 23554f3229d8S[email protected] hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 23561f479f8cS[email protected] log_info("le_central_connect: failed to alloc context"); 23574f3229d8S[email protected] return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller 23584f3229d8S[email protected] } 23594f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 23601f479f8cS[email protected] log_info("le_central_connect: send create connection next"); 2361564fca32S[email protected] hci_run(); 23624f3229d8S[email protected] return BLE_PERIPHERAL_OK; 23634f3229d8S[email protected] } 23640bf6344aS[email protected] 23650bf6344aS[email protected] if (!hci_is_le_connection(conn) || 23660bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 23670bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 23680bf6344aS[email protected] hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED); 23691f479f8cS[email protected] log_error("le_central_connect: classic connection or connect is already being created"); 23700bf6344aS[email protected] return BLE_PERIPHERAL_IN_WRONG_STATE; 23710bf6344aS[email protected] } 23720bf6344aS[email protected] 23731f479f8cS[email protected] log_info("le_central_connect: context exists with state %u", conn->state); 23744f3229d8S[email protected] hci_emit_le_connection_complete(conn, 0); 23754f3229d8S[email protected] hci_run(); 23764f3229d8S[email protected] return BLE_PERIPHERAL_OK; 23774f3229d8S[email protected] } 23784f3229d8S[email protected] 23797851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 23807851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){ 23810bf6344aS[email protected] linked_item_t *it; 23820bf6344aS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 23830bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 23840bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 23850bf6344aS[email protected] switch (conn->state){ 2386a6725849S[email protected] case SEND_CREATE_CONNECTION: 23877851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 23887851196eSmatthias.ringwald return conn; 23897851196eSmatthias.ringwald default: 23907851196eSmatthias.ringwald break; 23917851196eSmatthias.ringwald }; 23927851196eSmatthias.ringwald } 23937851196eSmatthias.ringwald return NULL; 23947851196eSmatthias.ringwald } 23957851196eSmatthias.ringwald 23967851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){ 23977851196eSmatthias.ringwald hci_connection_t * conn = le_central_get_outgoing_connection(); 23987851196eSmatthias.ringwald switch (conn->state){ 23997851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 24007851196eSmatthias.ringwald // skip sending create connection and emit event instead 24010bf6344aS[email protected] hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 24027851196eSmatthias.ringwald linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 24037851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 24040bf6344aS[email protected] break; 2405a6725849S[email protected] case SENT_CREATE_CONNECTION: 24067851196eSmatthias.ringwald // request to send cancel connection 24070bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 24080bf6344aS[email protected] hci_run(); 24090bf6344aS[email protected] break; 24100bf6344aS[email protected] default: 24110bf6344aS[email protected] break; 24120bf6344aS[email protected] } 2413e31f89a7S[email protected] return BLE_PERIPHERAL_OK; 2414e31f89a7S[email protected] } 24154f3229d8S[email protected] 24165917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){ 24175917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 24185917a5c5S[email protected] if (!conn){ 24197851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 24205917a5c5S[email protected] return BLE_PERIPHERAL_OK; 24215917a5c5S[email protected] } 24225917a5c5S[email protected] conn->state = SEND_DISCONNECT; 24235917a5c5S[email protected] hci_run(); 24244f3229d8S[email protected] return BLE_PERIPHERAL_OK; 24254f3229d8S[email protected] } 242604a6ef8cSmatthias.ringwald 242704a6ef8cSmatthias.ringwald void hci_disconnect_all(){ 242804a6ef8cSmatthias.ringwald linked_list_iterator_t it; 242904a6ef8cSmatthias.ringwald linked_list_iterator_init(&it, &hci_stack->connections); 243004a6ef8cSmatthias.ringwald while (linked_list_iterator_has_next(&it)){ 243104a6ef8cSmatthias.ringwald hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it); 243204a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 243304a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 243404a6ef8cSmatthias.ringwald } 2435d31fba26S[email protected] hci_run(); 243604a6ef8cSmatthias.ringwald } 2437