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 244998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 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 258*ac928cc2S[email protected] 259*ac928cc2S[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: 263*ac928cc2S[email protected] return hci_can_send_prepared_acl_packet_now(0); 264c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 265*ac928cc2S[email protected] return hci_can_send_command_packet_now(); 266c24735b1Smatthias.ringwald default: 267c24735b1Smatthias.ringwald return 0; 268c24735b1Smatthias.ringwald } 269c24735b1Smatthias.ringwald } 270c24735b1Smatthias.ringwald 271*ac928cc2S[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){ 274*ac928cc2S[email protected] 2756b4af23dS[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 276*ac928cc2S[email protected] 277*ac928cc2S[email protected] switch (packet_type) { 278*ac928cc2S[email protected] case HCI_ACL_DATA_PACKET: 279*ac928cc2S[email protected] return hci_can_send_acl_packet_now(0); 280*ac928cc2S[email protected] case HCI_COMMAND_DATA_PACKET: 281*ac928cc2S[email protected] return hci_can_send_command_packet_now(); 282*ac928cc2S[email protected] default: 283*ac928cc2S[email protected] return 0; 284*ac928cc2S[email protected] } 285*ac928cc2S[email protected] } 286*ac928cc2S[email protected] 287*ac928cc2S[email protected] 288*ac928cc2S[email protected] // new functions replacing hci_can_send_packet_now[_using_packet_buffer] 289*ac928cc2S[email protected] int hci_can_send_command_packet_now(void){ 290*ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 291*ac928cc2S[email protected] 292*ac928cc2S[email protected] // check for async hci transport implementations 293*ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 294*ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_COMMAND_DATA_PACKET)){ 295*ac928cc2S[email protected] return 0; 296*ac928cc2S[email protected] } 297*ac928cc2S[email protected] } 298*ac928cc2S[email protected] 299*ac928cc2S[email protected] return hci_stack->num_cmd_packets > 0; 300*ac928cc2S[email protected] } 301*ac928cc2S[email protected] 302*ac928cc2S[email protected] int hci_can_send_prepared_acl_packet_now(hci_con_handle_t con_handle) { 303*ac928cc2S[email protected] // check for async hci transport implementations 304*ac928cc2S[email protected] if (hci_stack->hci_transport->can_send_packet_now){ 305*ac928cc2S[email protected] if (!hci_stack->hci_transport->can_send_packet_now(HCI_ACL_DATA_PACKET)){ 306*ac928cc2S[email protected] return 0; 307*ac928cc2S[email protected] } 308*ac928cc2S[email protected] } 309*ac928cc2S[email protected] return hci_number_free_acl_slots() > 0; 310*ac928cc2S[email protected] } 311*ac928cc2S[email protected] 312*ac928cc2S[email protected] int hci_can_send_acl_packet_now(hci_con_handle_t con_handle){ 313*ac928cc2S[email protected] if (hci_stack->hci_packet_buffer_reserved) return 0; 314*ac928cc2S[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] 349826f7347S[email protected] // check for free places on Bluetooth module 35097b61c7bS[email protected] if (!hci_number_free_acl_slots()) { 351826f7347S[email protected] log_error("hci_send_acl_packet_buffer called but no free ACL buffers on controller"); 35297b61c7bS[email protected] hci_release_packet_buffer(); 35397b61c7bS[email protected] return BTSTACK_ACL_BUFFERS_FULL; 35497b61c7bS[email protected] } 3556218e6f1Smatthias.ringwald 356826f7347S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 3577856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 3585061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 35997b61c7bS[email protected] if (!connection) { 3605fa0b7cfS[email protected] log_error("hci_send_acl_packet_buffer called but no connection for handle 0x%04x", con_handle); 36197b61c7bS[email protected] hci_release_packet_buffer(); 36297b61c7bS[email protected] return 0; 36397b61c7bS[email protected] } 36456cf178bSmatthias.ringwald hci_connection_timestamp(connection); 36556cf178bSmatthias.ringwald 36656cf178bSmatthias.ringwald // count packet 36756cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 3687b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 3697856c818Smatthias.ringwald 37000d8e42eSmatthias.ringwald // send packet 3713a9fb326S[email protected] int err = hci_stack->hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 3726218e6f1Smatthias.ringwald 3736b4af23dS[email protected] // free packet buffer for synchronous transport implementations 374826f7347S[email protected] if (hci_transport_synchronous()){ 37597b61c7bS[email protected] hci_release_packet_buffer(); 3766b4af23dS[email protected] } 3776b4af23dS[email protected] 37800d8e42eSmatthias.ringwald return err; 379ee091cf1Smatthias.ringwald } 380ee091cf1Smatthias.ringwald 38116833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 3827856c818Smatthias.ringwald 383e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 384e76a89eeS[email protected] 3857856c818Smatthias.ringwald // get info 3867856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 3875061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 3887856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 3897856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 3907856c818Smatthias.ringwald 3917856c818Smatthias.ringwald // ignore non-registered handle 3927856c818Smatthias.ringwald if (!conn){ 3937d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 3947856c818Smatthias.ringwald return; 3957856c818Smatthias.ringwald } 3967856c818Smatthias.ringwald 397e76a89eeS[email protected] // assert packet is complete 3989ecc3e17S[email protected] if (acl_length + 4 != size){ 399e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 400e76a89eeS[email protected] return; 401e76a89eeS[email protected] } 402e76a89eeS[email protected] 4037856c818Smatthias.ringwald // update idle timestamp 4047856c818Smatthias.ringwald hci_connection_timestamp(conn); 4057856c818Smatthias.ringwald 4067856c818Smatthias.ringwald // handle different packet types 4077856c818Smatthias.ringwald switch (acl_flags & 0x03) { 4087856c818Smatthias.ringwald 4097856c818Smatthias.ringwald case 0x01: // continuation fragment 4107856c818Smatthias.ringwald 4117856c818Smatthias.ringwald // sanity check 4127856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 4137d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 4147856c818Smatthias.ringwald return; 4157856c818Smatthias.ringwald } 4167856c818Smatthias.ringwald 4177856c818Smatthias.ringwald // append fragment payload (header already stored) 4187856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 4197856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 4207856c818Smatthias.ringwald 4217d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 422decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 4237856c818Smatthias.ringwald 4247856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 4257856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 4267856c818Smatthias.ringwald 4273a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 4287856c818Smatthias.ringwald // reset recombination buffer 4297856c818Smatthias.ringwald conn->acl_recombination_length = 0; 4307856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 4317856c818Smatthias.ringwald } 4327856c818Smatthias.ringwald break; 4337856c818Smatthias.ringwald 4347856c818Smatthias.ringwald case 0x02: { // first fragment 4357856c818Smatthias.ringwald 4367856c818Smatthias.ringwald // sanity check 4377856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 4387d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 4397856c818Smatthias.ringwald return; 4407856c818Smatthias.ringwald } 4417856c818Smatthias.ringwald 4427856c818Smatthias.ringwald // peek into L2CAP packet! 4437856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 4447856c818Smatthias.ringwald 445e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 446decc01a8Smatthias.ringwald 4477856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 4487856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 4497856c818Smatthias.ringwald 4507856c818Smatthias.ringwald // forward fragment as L2CAP packet 4513a9fb326S[email protected] hci_stack->packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 4527856c818Smatthias.ringwald 4537856c818Smatthias.ringwald } else { 4547856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 4557856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 4567856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 4577856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 458decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 4597856c818Smatthias.ringwald } 4607856c818Smatthias.ringwald break; 4617856c818Smatthias.ringwald 4627856c818Smatthias.ringwald } 4637856c818Smatthias.ringwald default: 4647d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 4657856c818Smatthias.ringwald return; 4667856c818Smatthias.ringwald } 46794ab26f8Smatthias.ringwald 46894ab26f8Smatthias.ringwald // execute main loop 46994ab26f8Smatthias.ringwald hci_run(); 47016833f0aSmatthias.ringwald } 47122909952Smatthias.ringwald 47267a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 4731f479f8cS[email protected] log_info("Connection closed: handle 0x%x, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 4743c4d4b90Smatthias.ringwald 475c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 476c785ef68Smatthias.ringwald 4773a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 478a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 4793c4d4b90Smatthias.ringwald 4803c4d4b90Smatthias.ringwald // now it's gone 481c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 482c7e0c5f6Smatthias.ringwald } 483c7e0c5f6Smatthias.ringwald 4840c042179S[email protected] static const uint16_t packet_type_sizes[] = { 4858f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 4868f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 4878f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 4888f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 4898f8108aaSmatthias.ringwald }; 49065389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 49165389bfcS[email protected] 0, // 3 slot packets 49265389bfcS[email protected] 1, // 5 slot packets 49365389bfcS[email protected] 25, // EDR 2 mpbs 49465389bfcS[email protected] 26, // EDR 3 mbps 49565389bfcS[email protected] 39, // 3 slot EDR packts 49665389bfcS[email protected] 40, // 5 slot EDR packet 49765389bfcS[email protected] }; 49865389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 49965389bfcS[email protected] 0x0f00, // 3 slot packets 50065389bfcS[email protected] 0xf000, // 5 slot packets 50165389bfcS[email protected] 0x1102, // EDR 2 mpbs 50265389bfcS[email protected] 0x2204, // EDR 3 mbps 50365389bfcS[email protected] 0x0300, // 3 slot EDR packts 50465389bfcS[email protected] 0x3000, // 5 slot EDR packet 50565389bfcS[email protected] }; 5068f8108aaSmatthias.ringwald 50765389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 50865389bfcS[email protected] // enable packet types based on size 5098f8108aaSmatthias.ringwald uint16_t packet_types = 0; 5108f8108aaSmatthias.ringwald int i; 5118f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 5128f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 5138f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 5148f8108aaSmatthias.ringwald packet_types |= 1 << i; 5158f8108aaSmatthias.ringwald } 5168f8108aaSmatthias.ringwald } 51765389bfcS[email protected] // disable packet types due to missing local supported features 51865389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 51965389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 52065389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 52165389bfcS[email protected] if (feature_set) continue; 52265389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 52365389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 52465389bfcS[email protected] } 5258f8108aaSmatthias.ringwald // flip bits for "may not be used" 5268f8108aaSmatthias.ringwald packet_types ^= 0x3306; 5278f8108aaSmatthias.ringwald return packet_types; 5288f8108aaSmatthias.ringwald } 5298f8108aaSmatthias.ringwald 5308f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 5313a9fb326S[email protected] return hci_stack->packet_types; 5328f8108aaSmatthias.ringwald } 5338f8108aaSmatthias.ringwald 534facf93fdS[email protected] uint8_t* hci_get_outgoing_packet_buffer(void){ 5357dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 5363a9fb326S[email protected] return hci_stack->hci_packet_buffer; 5377dc17943Smatthias.ringwald } 5387dc17943Smatthias.ringwald 539f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 5403a9fb326S[email protected] return hci_stack->acl_data_packet_length; 5417dc17943Smatthias.ringwald } 5427dc17943Smatthias.ringwald 5436ac9a97eS[email protected] int hci_non_flushable_packet_boundary_flag_supported(void){ 5446ac9a97eS[email protected] // No. 54, byte 6, bit 6 5456ac9a97eS[email protected] return (hci_stack->local_supported_features[6] & (1 << 6)) != 0; 5466ac9a97eS[email protected] } 5476ac9a97eS[email protected] 548f5d8d141S[email protected] int hci_ssp_supported(void){ 5496ac9a97eS[email protected] // No. 51, byte 6, bit 3 5503a9fb326S[email protected] return (hci_stack->local_supported_features[6] & (1 << 3)) != 0; 551f5d8d141S[email protected] } 552f5d8d141S[email protected] 553f5d8d141S[email protected] int hci_classic_supported(void){ 5546ac9a97eS[email protected] // No. 37, byte 4, bit 5, = No BR/EDR Support 5553a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 5)) == 0; 556f5d8d141S[email protected] } 557f5d8d141S[email protected] 558f5d8d141S[email protected] int hci_le_supported(void){ 559f5d8d141S[email protected] #ifdef HAVE_BLE 5606ac9a97eS[email protected] // No. 37, byte 4, bit 6 = LE Supported (Controller) 5613a9fb326S[email protected] return (hci_stack->local_supported_features[4] & (1 << 6)) != 0; 562f5d8d141S[email protected] #else 563f5d8d141S[email protected] return 0; 564f5d8d141S[email protected] #endif 565f5d8d141S[email protected] } 566f5d8d141S[email protected] 56769a97523S[email protected] // get addr type and address used in advertisement packets 56818904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 5693a9fb326S[email protected] *addr_type = hci_stack->adv_addr_type; 5703a9fb326S[email protected] if (hci_stack->adv_addr_type){ 5713a9fb326S[email protected] memcpy(addr, hci_stack->adv_address, 6); 57269a97523S[email protected] } else { 5733a9fb326S[email protected] memcpy(addr, hci_stack->local_bd_addr, 6); 57469a97523S[email protected] } 57569a97523S[email protected] } 57669a97523S[email protected] 577b2f949feS[email protected] #ifdef HAVE_BLE 578e01fe8b6S[email protected] void le_handle_advertisement_report(uint8_t *packet, int size){ 57957c9da5bS[email protected] int num_reports = packet[3]; 58057c9da5bS[email protected] int i; 58157c9da5bS[email protected] int total_data_length = 0; 58257c9da5bS[email protected] int data_offset = 0; 58357c9da5bS[email protected] 58457c9da5bS[email protected] for (i=0; i<num_reports;i++){ 58557c9da5bS[email protected] total_data_length += packet[4+num_reports*8+i]; 58657c9da5bS[email protected] } 58757c9da5bS[email protected] 5882e440c8aS[email protected] log_info("num reports: %d, ", num_reports); 58957c9da5bS[email protected] for (i=0; i<num_reports;i++){ 59057c9da5bS[email protected] int pos = 0; 59157c9da5bS[email protected] uint8_t data_length = packet[4+num_reports*8+i]; 5922b552b23S[email protected] uint8_t event_size = 10 + data_length; 5932b552b23S[email protected] uint8_t event[2 + event_size ]; 5942b552b23S[email protected] event[pos++] = GAP_LE_ADVERTISING_REPORT; 59557c9da5bS[email protected] event[pos++] = event_size; 59657c9da5bS[email protected] event[pos++] = packet[4+i]; // event_type; 59757c9da5bS[email protected] event[pos++] = packet[4+num_reports+i]; // address_type; 598564fca32S[email protected] memcpy(&event[pos], &packet[4+num_reports*2+i*6], 6); // bt address 59957c9da5bS[email protected] pos += 6; 6002b552b23S[email protected] event[pos++] = packet[4+num_reports*9+total_data_length + i]; 6012e440c8aS[email protected] log_info("rssi: %d, ", event[pos-1]); 60257c9da5bS[email protected] event[pos++] = data_length; 60388ed79cfS[email protected] memcpy(&event[pos], &packet[4+num_reports*9+data_offset], data_length); 60457c9da5bS[email protected] data_offset += data_length; 60557c9da5bS[email protected] pos += data_length; 60657c9da5bS[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 60757c9da5bS[email protected] } 60857c9da5bS[email protected] } 609b2f949feS[email protected] #endif 61057c9da5bS[email protected] 6116155b3d3S[email protected] static void hci_initializing_event_handler(uint8_t * packet, uint16_t size){ 612a2481739S[email protected] uint8_t command_completed = 0; 613a2481739S[email protected] if ((hci_stack->substate % 2) == 0) return; 6146155b3d3S[email protected] // odd: waiting for event 6156155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 6166155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,3); 6176155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 618a2481739S[email protected] command_completed = 1; 6196155b3d3S[email protected] log_info("Command complete for expected opcode %04x -> new substate %u", opcode, hci_stack->substate); 6206155b3d3S[email protected] } else { 6216155b3d3S[email protected] log_info("Command complete for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 6226155b3d3S[email protected] } 6236155b3d3S[email protected] } 6246155b3d3S[email protected] if (packet[0] == HCI_EVENT_COMMAND_STATUS){ 6256155b3d3S[email protected] uint8_t status = packet[2]; 6266155b3d3S[email protected] uint16_t opcode = READ_BT_16(packet,4); 6276155b3d3S[email protected] if (opcode == hci_stack->last_cmd_opcode){ 6286155b3d3S[email protected] if (status){ 629a2481739S[email protected] command_completed = 1; 6306155b3d3S[email protected] log_error("Command status error 0x%02x for expected opcode %04x -> new substate %u", status, opcode, hci_stack->substate); 6316155b3d3S[email protected] } else { 6326155b3d3S[email protected] log_info("Command status OK for expected opcode %04x, waiting for command complete", opcode); 6336155b3d3S[email protected] } 6346155b3d3S[email protected] } else { 6356155b3d3S[email protected] log_info("Command status for opcode %04x, expected %04x", opcode, hci_stack->last_cmd_opcode); 6366155b3d3S[email protected] } 6376155b3d3S[email protected] } 638a2481739S[email protected] 639a2481739S[email protected] if (!command_completed) return; 640a2481739S[email protected] 641a2481739S[email protected] switch(hci_stack->substate >> 1){ 642a2481739S[email protected] default: 643a2481739S[email protected] hci_stack->substate++; 644a2481739S[email protected] break; 6456155b3d3S[email protected] } 6466155b3d3S[email protected] } 6476155b3d3S[email protected] 6486155b3d3S[email protected] static void hci_initializing_state_machine(){ 6496155b3d3S[email protected] // log_info("hci_init: substate %u\n", hci_stack->substate); 6506155b3d3S[email protected] if (hci_stack->substate % 2) { 6516155b3d3S[email protected] // odd: waiting for command completion 6526155b3d3S[email protected] return; 6536155b3d3S[email protected] } 6546155b3d3S[email protected] switch (hci_stack->substate >> 1){ 6556155b3d3S[email protected] case 0: // RESET 6566155b3d3S[email protected] hci_state_reset(); 6576155b3d3S[email protected] 6586155b3d3S[email protected] hci_send_cmd(&hci_reset); 6596155b3d3S[email protected] if (hci_stack->config == NULL || ((hci_uart_config_t *)hci_stack->config)->baudrate_main == 0){ 6606155b3d3S[email protected] // skip baud change 6616155b3d3S[email protected] hci_stack->substate = 4; // >> 1 = 2 6626155b3d3S[email protected] } 6636155b3d3S[email protected] break; 6646155b3d3S[email protected] case 1: // SEND BAUD CHANGE 6656155b3d3S[email protected] hci_stack->control->baudrate_cmd(hci_stack->config, ((hci_uart_config_t *)hci_stack->config)->baudrate_main, hci_stack->hci_packet_buffer); 66688789c61Smatthias.ringwald hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 6676155b3d3S[email protected] hci_send_cmd_packet(hci_stack->hci_packet_buffer, 3 + hci_stack->hci_packet_buffer[2]); 6686155b3d3S[email protected] break; 6696155b3d3S[email protected] case 2: // LOCAL BAUD CHANGE 6706155b3d3S[email protected] log_info("Local baud rate change"); 6716155b3d3S[email protected] hci_stack->hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack->config)->baudrate_main); 6726155b3d3S[email protected] hci_stack->substate += 2; 6736155b3d3S[email protected] // break missing here for fall through 6746155b3d3S[email protected] 6756155b3d3S[email protected] case 3: 6766155b3d3S[email protected] log_info("Custom init"); 6776155b3d3S[email protected] // Custom initialization 6786155b3d3S[email protected] if (hci_stack->control && hci_stack->control->next_cmd){ 6796155b3d3S[email protected] int valid_cmd = (*hci_stack->control->next_cmd)(hci_stack->config, hci_stack->hci_packet_buffer); 6806155b3d3S[email protected] if (valid_cmd){ 6816155b3d3S[email protected] int size = 3 + hci_stack->hci_packet_buffer[2]; 6826155b3d3S[email protected] hci_stack->last_cmd_opcode = READ_BT_16(hci_stack->hci_packet_buffer, 0); 6836155b3d3S[email protected] hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack->hci_packet_buffer, size); 6846155b3d3S[email protected] hci_stack->substate = 4; // more init commands 6856155b3d3S[email protected] break; 6866155b3d3S[email protected] } 6876155b3d3S[email protected] log_info("hci_run: init script done\n\r"); 6886155b3d3S[email protected] } 6896155b3d3S[email protected] // otherwise continue 6906155b3d3S[email protected] hci_send_cmd(&hci_read_bd_addr); 6916155b3d3S[email protected] break; 6926155b3d3S[email protected] case 4: 6936155b3d3S[email protected] hci_send_cmd(&hci_read_buffer_size); 6946155b3d3S[email protected] break; 6956155b3d3S[email protected] case 5: 6966155b3d3S[email protected] hci_send_cmd(&hci_read_local_supported_features); 6976155b3d3S[email protected] break; 6986155b3d3S[email protected] case 6: 6996155b3d3S[email protected] if (hci_le_supported()){ 7006155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 7016155b3d3S[email protected] } else { 7026155b3d3S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 7036155b3d3S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 7046155b3d3S[email protected] } 7056155b3d3S[email protected] 7066155b3d3S[email protected] // skip Classic init commands for LE only chipsets 7076155b3d3S[email protected] if (!hci_classic_supported()){ 7086155b3d3S[email protected] if (hci_le_supported()){ 7096155b3d3S[email protected] hci_stack->substate = 11 << 1; // skip all classic command 7106155b3d3S[email protected] } else { 7116155b3d3S[email protected] log_error("Neither BR/EDR nor LE supported"); 7126155b3d3S[email protected] hci_stack->substate = 14 << 1; // skip all 7136155b3d3S[email protected] } 7146155b3d3S[email protected] } 7156155b3d3S[email protected] break; 7166155b3d3S[email protected] case 7: 7176155b3d3S[email protected] if (hci_ssp_supported()){ 7186155b3d3S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack->ssp_enable); 7196155b3d3S[email protected] break; 7206155b3d3S[email protected] } 7216155b3d3S[email protected] hci_stack->substate += 2; 7226155b3d3S[email protected] // break missing here for fall through 7236155b3d3S[email protected] 7246155b3d3S[email protected] case 8: 7256155b3d3S[email protected] // ca. 15 sec 7266155b3d3S[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 7276155b3d3S[email protected] break; 7286155b3d3S[email protected] case 9: 7296155b3d3S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack->class_of_device); 7306155b3d3S[email protected] break; 7316155b3d3S[email protected] case 10: 7326155b3d3S[email protected] if (hci_stack->local_name){ 7336155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack->local_name); 7346155b3d3S[email protected] } else { 7356155b3d3S[email protected] char hostname[30]; 7366155b3d3S[email protected] #ifdef EMBEDDED 7376155b3d3S[email protected] // BTstack-11:22:33:44:55:66 7386155b3d3S[email protected] strcpy(hostname, "BTstack "); 7396155b3d3S[email protected] strcat(hostname, bd_addr_to_str(hci_stack->local_bd_addr)); 7406155b3d3S[email protected] log_info("---> Name %s", hostname); 7416155b3d3S[email protected] #else 7426155b3d3S[email protected] // hostname for POSIX systems 7436155b3d3S[email protected] gethostname(hostname, 30); 7446155b3d3S[email protected] hostname[29] = '\0'; 7456155b3d3S[email protected] #endif 7466155b3d3S[email protected] hci_send_cmd(&hci_write_local_name, hostname); 7476155b3d3S[email protected] } 7486155b3d3S[email protected] break; 7496155b3d3S[email protected] case 11: 7506155b3d3S[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack->connectable << 1) | hci_stack->discoverable); // page scan 7516155b3d3S[email protected] if (!hci_le_supported()){ 7526155b3d3S[email protected] // SKIP LE init for Classic only configuration 7536155b3d3S[email protected] hci_stack->substate = 14 << 1; 7546155b3d3S[email protected] } 7556155b3d3S[email protected] break; 7566155b3d3S[email protected] 7576155b3d3S[email protected] #ifdef HAVE_BLE 7586155b3d3S[email protected] // LE INIT 7596155b3d3S[email protected] case 12: 7606155b3d3S[email protected] hci_send_cmd(&hci_le_read_buffer_size); 7616155b3d3S[email protected] break; 7626155b3d3S[email protected] case 13: 7636155b3d3S[email protected] // LE Supported Host = 1, Simultaneous Host = 0 7646155b3d3S[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 7656155b3d3S[email protected] break; 7666155b3d3S[email protected] case 14: 7676155b3d3S[email protected] // LE Scan Parameters: active scanning, 300 ms interval, 30 ms window, public address, accept all advs 7686155b3d3S[email protected] hci_send_cmd(&hci_le_set_scan_parameters, 1, 0x1e0, 0x30, 0, 0); 7696155b3d3S[email protected] break; 7706155b3d3S[email protected] #endif 7716155b3d3S[email protected] 7726155b3d3S[email protected] // DONE 7736155b3d3S[email protected] case 15: 7746155b3d3S[email protected] // done. 7756155b3d3S[email protected] hci_stack->state = HCI_STATE_WORKING; 7766155b3d3S[email protected] hci_emit_state(); 7776155b3d3S[email protected] break; 7786155b3d3S[email protected] default: 7796155b3d3S[email protected] break; 7806155b3d3S[email protected] } 7816155b3d3S[email protected] hci_stack->substate++; 7826155b3d3S[email protected] } 7836155b3d3S[email protected] 78474ec757aSmatthias.ringwald // avoid huge local variables 785223aafc1Smatthias.ringwald #ifndef EMBEDDED 78674ec757aSmatthias.ringwald static device_name_t device_name; 787223aafc1Smatthias.ringwald #endif 78816833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 789e76a89eeS[email protected] 790e76a89eeS[email protected] uint16_t event_length = packet[1]; 791e76a89eeS[email protected] 792e76a89eeS[email protected] // assert packet is complete 793e76a89eeS[email protected] if (size != event_length + 2){ 794e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 795e76a89eeS[email protected] return; 796e76a89eeS[email protected] } 797e76a89eeS[email protected] 7981281a47eSmatthias.ringwald bd_addr_t addr; 79996a45072S[email protected] bd_addr_type_t addr_type; 8002d00edd4Smatthias.ringwald uint8_t link_type; 801fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 8021f7b95a1Smatthias.ringwald hci_connection_t * conn; 80356cf178bSmatthias.ringwald int i; 80422909952Smatthias.ringwald 8055909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 8065909f7f2Smatthias.ringwald 8076772a24cSmatthias.ringwald switch (packet[0]) { 80822909952Smatthias.ringwald 8096772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 8107ec5eeaaSmatthias.ringwald // get num cmd packets 8113a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack->num_cmd_packets, packet[2]); 8123a9fb326S[email protected] hci_stack->num_cmd_packets = packet[2]; 8137ec5eeaaSmatthias.ringwald 814e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 815e2edc0c3Smatthias.ringwald // from offset 5 816e2edc0c3Smatthias.ringwald // status 8171d279b20Smatthias.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" 8183a9fb326S[email protected] hci_stack->acl_data_packet_length = READ_BT_16(packet, 6); 81956cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 8203a9fb326S[email protected] hci_stack->total_num_acl_packets = packet[9]; 82156cf178bSmatthias.ringwald // ignore: total num SCO packets 8223a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 823a7a04bd9Smatthias.ringwald // determine usable ACL payload size 8243a9fb326S[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 8253a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 8268f8108aaSmatthias.ringwald } 82765389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 8283a9fb326S[email protected] hci_stack->acl_data_packet_length, hci_stack->total_num_acl_packets); 829e2edc0c3Smatthias.ringwald } 83056cf178bSmatthias.ringwald } 83165a46ef3S[email protected] #ifdef HAVE_BLE 83265a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 8333a9fb326S[email protected] hci_stack->le_data_packet_length = READ_BT_16(packet, 6); 8343a9fb326S[email protected] hci_stack->total_num_le_packets = packet[8]; 8353a9fb326S[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); 83601f1657cS[email protected] 83701f1657cS[email protected] // use LE buffers if no clasic buffers have been reported 83801f1657cS[email protected] if (hci_stack->total_num_acl_packets == 0){ 83901f1657cS[email protected] log_info("use le buffers instead of classic ones"); 84001f1657cS[email protected] hci_stack->total_num_acl_packets = hci_stack->total_num_le_packets; 84101f1657cS[email protected] hci_stack->acl_data_packet_length = hci_stack->le_data_packet_length; 84201f1657cS[email protected] // determine usable ACL payload size 84301f1657cS[email protected] if (HCI_ACL_PAYLOAD_SIZE < hci_stack->acl_data_packet_length){ 84401f1657cS[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 84501f1657cS[email protected] } 84601f1657cS[email protected] } 84765a46ef3S[email protected] } 84865a46ef3S[email protected] #endif 849188981d2Smatthias.ringwald // Dump local address 850188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 8513a9fb326S[email protected] bt_flip_addr(hci_stack->local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 852188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 8533a9fb326S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); 854188981d2Smatthias.ringwald } 855381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 8563a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 857381fbed8Smatthias.ringwald } 85865389bfcS[email protected] // Note: HCI init checks 859559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 8603a9fb326S[email protected] memcpy(hci_stack->local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 861559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 8623a9fb326S[email protected] hci_stack->local_supported_features[0], hci_stack->local_supported_features[1], 8633a9fb326S[email protected] hci_stack->local_supported_features[2], hci_stack->local_supported_features[3], 8643a9fb326S[email protected] hci_stack->local_supported_features[4], hci_stack->local_supported_features[5], 8653a9fb326S[email protected] hci_stack->local_supported_features[6], hci_stack->local_supported_features[7]); 86665389bfcS[email protected] 86765389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 8683a9fb326S[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]); 8693a9fb326S[email protected] log_info("packet types %04x", hci_stack->packet_types); 870f5d8d141S[email protected] 871f5d8d141S[email protected] // Classic/LE 872f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 873559e517eS[email protected] } 87456cf178bSmatthias.ringwald break; 87556cf178bSmatthias.ringwald 8767ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 8777ec5eeaaSmatthias.ringwald // get num cmd packets 8783a9fb326S[email protected] // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack->num_cmd_packets, packet[3]); 8793a9fb326S[email protected] hci_stack->num_cmd_packets = packet[3]; 8807ec5eeaaSmatthias.ringwald break; 8817ec5eeaaSmatthias.ringwald 8822e440c8aS[email protected] case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:{ 8832e440c8aS[email protected] int offset = 3; 88456cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 8852e440c8aS[email protected] handle = READ_BT_16(packet, offset); 8862e440c8aS[email protected] offset += 2; 8872e440c8aS[email protected] uint16_t num_packets = READ_BT_16(packet, offset); 8882e440c8aS[email protected] offset += 2; 8892e440c8aS[email protected] 8905061f3afS[email protected] conn = hci_connection_for_handle(handle); 89156cf178bSmatthias.ringwald if (!conn){ 8927d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 89356cf178bSmatthias.ringwald continue; 89456cf178bSmatthias.ringwald } 89556cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 8967b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 89756cf178bSmatthias.ringwald } 8986772a24cSmatthias.ringwald break; 8992e440c8aS[email protected] } 9001f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 90137eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 90237eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 9032d00edd4Smatthias.ringwald link_type = packet[11]; 904339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 90537eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 90696a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 9071f7b95a1Smatthias.ringwald if (!conn) { 90896a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 9091f7b95a1Smatthias.ringwald } 910ce4c8fabSmatthias.ringwald if (!conn) { 911ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 9123a9fb326S[email protected] hci_stack->decline_reason = 0x0d; 9133a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 914ce4c8fabSmatthias.ringwald break; 915ce4c8fabSmatthias.ringwald } 91632ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 91732ab9390Smatthias.ringwald hci_run(); 91837eaa4cfSmatthias.ringwald } else { 919ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 9203a9fb326S[email protected] hci_stack->decline_reason = 0x0a; 9213a9fb326S[email protected] BD_ADDR_COPY(hci_stack->decline_addr, addr); 92237eaa4cfSmatthias.ringwald } 9231f7b95a1Smatthias.ringwald break; 9241f7b95a1Smatthias.ringwald 9256772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 926fe1ed1b8Smatthias.ringwald // Connection management 927fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 928339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 92996a45072S[email protected] addr_type = BD_ADDR_TYPE_CLASSIC; 93096a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 931fe1ed1b8Smatthias.ringwald if (conn) { 932b448a0e7Smatthias.ringwald if (!packet[2]){ 933c8e4258aSmatthias.ringwald conn->state = OPEN; 934fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 935ad83dc6aS[email protected] conn->bonding_flags |= BONDING_REQUEST_REMOTE_FEATURES; 936ee091cf1Smatthias.ringwald 937c785ef68Smatthias.ringwald // restart timer 938c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 939ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 940c785ef68Smatthias.ringwald 941339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 94243bfb1bdSmatthias.ringwald 94343bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 944b448a0e7Smatthias.ringwald } else { 945ad83dc6aS[email protected] // notify client if dedicated bonding 946ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 947ad83dc6aS[email protected] hci_emit_dedicated_bonding_result(conn, packet[2]); 948ad83dc6aS[email protected] } 949ad83dc6aS[email protected] 950b448a0e7Smatthias.ringwald // connection failed, remove entry 9513a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 952a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 953c12e46e7Smatthias.ringwald 954c12e46e7Smatthias.ringwald // if authentication error, also delete link key 955c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 956c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 957c12e46e7Smatthias.ringwald } 958fe1ed1b8Smatthias.ringwald } 959fe1ed1b8Smatthias.ringwald } 9606772a24cSmatthias.ringwald break; 961fe1ed1b8Smatthias.ringwald 962afd4e962S[email protected] case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE: 963afd4e962S[email protected] handle = READ_BT_16(packet, 3); 964afd4e962S[email protected] conn = hci_connection_for_handle(handle); 965afd4e962S[email protected] if (!conn) break; 966afd4e962S[email protected] if (!packet[2]){ 967afd4e962S[email protected] uint8_t * features = &packet[5]; 968afd4e962S[email protected] if (features[6] & (1 << 3)){ 969afd4e962S[email protected] conn->bonding_flags |= BONDING_REMOTE_SUPPORTS_SSP; 970afd4e962S[email protected] } 971afd4e962S[email protected] } 972afd4e962S[email protected] conn->bonding_flags |= BONDING_RECEIVED_REMOTE_FEATURES; 973ad83dc6aS[email protected] log_info("HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE, bonding flags %x", conn->bonding_flags); 974ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 975ad83dc6aS[email protected] conn->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 976ad83dc6aS[email protected] } 977afd4e962S[email protected] break; 978afd4e962S[email protected] 9797fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 9807b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 9817fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 98274d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 9833a9fb326S[email protected] if (hci_stack->bondable && !hci_stack->remote_device_db) break; 98432ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 98564472d52Smatthias.ringwald hci_run(); 986d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 98729d53098Smatthias.ringwald return; 9887fde4af9Smatthias.ringwald 9899ab95c90S[email protected] case HCI_EVENT_LINK_KEY_NOTIFICATION: { 99029d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 99196a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 9929ab95c90S[email protected] if (!conn) break; 9939ab95c90S[email protected] conn->authentication_flags |= RECV_LINK_KEY_NOTIFICATION; 9947bdc6798S[email protected] link_key_type_t link_key_type = (link_key_type_t)packet[24]; 9959ab95c90S[email protected] // Change Connection Encryption keeps link key type 9969ab95c90S[email protected] if (link_key_type != CHANGED_COMBINATION_KEY){ 9979ab95c90S[email protected] conn->link_key_type = link_key_type; 9989ab95c90S[email protected] } 9993a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 10003a9fb326S[email protected] hci_stack->remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8], conn->link_key_type); 100129d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 10027fde4af9Smatthias.ringwald break; 10039ab95c90S[email protected] } 10047fde4af9Smatthias.ringwald 10057fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 10066724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], LEGACY_PAIRING_ACTIVE); 10074c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 10083a9fb326S[email protected] if (!hci_stack->bondable){ 1009899283eaS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], DENY_PIN_CODE_REQUEST); 1010f8fb5f6eS[email protected] hci_run(); 1011f8fb5f6eS[email protected] return; 10124c57c146S[email protected] } 1013d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 10143a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 1015d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 10163a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 10177fde4af9Smatthias.ringwald break; 10187fde4af9Smatthias.ringwald 10191d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 10201d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 1021dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 1022dbe1a790S[email protected] break; 1023dbe1a790S[email protected] 1024dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 10256724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 10263a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1027dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 1028dbe1a790S[email protected] break; 1029dbe1a790S[email protected] 1030dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 10316724cd9eS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SSP_PAIRING_ACTIVE); 10323a9fb326S[email protected] if (!hci_stack->ssp_auto_accept) break; 1033dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 10341d6b20aeS[email protected] break; 10351d6b20aeS[email protected] 1036f0944df2S[email protected] case HCI_EVENT_ENCRYPTION_CHANGE: 1037f0944df2S[email protected] handle = READ_BT_16(packet, 3); 1038f0944df2S[email protected] conn = hci_connection_for_handle(handle); 1039f0944df2S[email protected] if (!conn) break; 1040ad83dc6aS[email protected] if (packet[2] == 0) { 1041f0944df2S[email protected] if (packet[5]){ 1042f0944df2S[email protected] conn->authentication_flags |= CONNECTION_ENCRYPTED; 1043f0944df2S[email protected] } else { 1044536f9994S[email protected] conn->authentication_flags &= ~CONNECTION_ENCRYPTED; 1045f0944df2S[email protected] } 1046ad83dc6aS[email protected] } 1047a00031e2S[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 1048f0944df2S[email protected] break; 1049f0944df2S[email protected] 10501eb2563eS[email protected] case HCI_EVENT_AUTHENTICATION_COMPLETE_EVENT: 10511eb2563eS[email protected] handle = READ_BT_16(packet, 3); 10521eb2563eS[email protected] conn = hci_connection_for_handle(handle); 10531eb2563eS[email protected] if (!conn) break; 1054ad83dc6aS[email protected] 1055ad83dc6aS[email protected] // dedicated bonding: send result and disconnect 1056ad83dc6aS[email protected] if (conn->bonding_flags & BONDING_DEDICATED){ 1057ad83dc6aS[email protected] conn->bonding_flags &= ~BONDING_DEDICATED; 1058ad83dc6aS[email protected] hci_emit_dedicated_bonding_result( conn, packet[2]); 1059ad83dc6aS[email protected] conn->bonding_flags |= BONDING_DISCONNECT_DEDICATED_DONE; 1060ad83dc6aS[email protected] break; 1061ad83dc6aS[email protected] } 1062ad83dc6aS[email protected] 1063b5cb6874S[email protected] if (packet[2] == 0 && gap_security_level_for_link_key_type(conn->link_key_type) >= conn->requested_security_level){ 10641eb2563eS[email protected] // link key sufficient for requested security 10651eb2563eS[email protected] conn->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 1066ad83dc6aS[email protected] break; 1067ad83dc6aS[email protected] } 10681eb2563eS[email protected] // not enough 10691eb2563eS[email protected] hci_emit_security_level(handle, gap_security_level_for_connection(conn)); 10701eb2563eS[email protected] break; 107134d2123cS[email protected] 1072223aafc1Smatthias.ringwald #ifndef EMBEDDED 107374ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 10743a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 107574ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 107674ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 10775a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 10785a06394aSmatthias.ringwald for (i=0; i<248;i++){ 10795a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 10805a06394aSmatthias.ringwald packet[9+i] = 0; 10815a06394aSmatthias.ringwald break; 1082cdc9101dSmatthias.ringwald } 10835a06394aSmatthias.ringwald } 1084d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 108574ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 10863a9fb326S[email protected] hci_stack->remote_device_db->put_name(&addr, &device_name); 108774ec757aSmatthias.ringwald break; 108874ec757aSmatthias.ringwald 108974ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 1090206a9031S[email protected] case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI:{ 10913a9fb326S[email protected] if (!hci_stack->remote_device_db) break; 109274ec757aSmatthias.ringwald // first send inq result packet 10933a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 109474ec757aSmatthias.ringwald // then send cached remote names 1095206a9031S[email protected] int offset = 3; 109674ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 1097206a9031S[email protected] bt_flip_addr(addr, &packet[offset]); 1098206a9031S[email protected] offset += 14; // 6 + 1 + 1 + 1 + 3 + 2; 10993a9fb326S[email protected] if (hci_stack->remote_device_db->get_name(&addr, &device_name)){ 110074ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 110174ec757aSmatthias.ringwald } 110274ec757aSmatthias.ringwald } 110374ec757aSmatthias.ringwald return; 1104206a9031S[email protected] } 1105223aafc1Smatthias.ringwald #endif 110674ec757aSmatthias.ringwald 11076772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1108fe1ed1b8Smatthias.ringwald if (!packet[2]){ 1109fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 11105061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 1111fe1ed1b8Smatthias.ringwald if (conn) { 1112c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 1113fe1ed1b8Smatthias.ringwald } 1114fe1ed1b8Smatthias.ringwald } 11156772a24cSmatthias.ringwald break; 11166772a24cSmatthias.ringwald 1117c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 11183a9fb326S[email protected] if(hci_stack->control && hci_stack->control->hw_error){ 11193a9fb326S[email protected] (*hci_stack->control->hw_error)(); 11207586ee35S[email protected] } else { 11217586ee35S[email protected] // if no special requests, just reboot stack 11227586ee35S[email protected] hci_power_control_off(); 11237586ee35S[email protected] hci_power_control_on(); 1124c68bdf90Smatthias.ringwald } 1125c68bdf90Smatthias.ringwald break; 1126c68bdf90Smatthias.ringwald 11276b4af23dS[email protected] case DAEMON_EVENT_HCI_PACKET_SENT: 11286b4af23dS[email protected] // free packet buffer for asynchronous transport 11296b4af23dS[email protected] if (hci_transport_synchronous()) break; 11306b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 11316b4af23dS[email protected] break; 11326b4af23dS[email protected] 11335909f7f2Smatthias.ringwald #ifdef HAVE_BLE 11345909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 11355909f7f2Smatthias.ringwald switch (packet[2]){ 113657c9da5bS[email protected] case HCI_SUBEVENT_LE_ADVERTISING_REPORT: 11372e440c8aS[email protected] log_info("advertising report received\n"); 11387bdc6798S[email protected] if (hci_stack->le_scanning_state != LE_SCANNING) break; 113957c9da5bS[email protected] le_handle_advertisement_report(packet, size); 11407bdc6798S[email protected] break; 11415909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 11425909f7f2Smatthias.ringwald // Connection management 11435909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 11447bdc6798S[email protected] addr_type = (bd_addr_type_t)packet[7]; 114596a45072S[email protected] log_info("LE Connection_complete (status=%u) type %u, %s\n", packet[3], addr_type, bd_addr_to_str(addr)); 11465909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 114796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, addr_type); 11485909f7f2Smatthias.ringwald if (packet[3]){ 11495909f7f2Smatthias.ringwald if (conn){ 11505909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 11513a9fb326S[email protected] linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 11525909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 11535909f7f2Smatthias.ringwald } 11545909f7f2Smatthias.ringwald // if authentication error, also delete link key 11555909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 11565909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 11575909f7f2Smatthias.ringwald } 11585909f7f2Smatthias.ringwald break; 11595909f7f2Smatthias.ringwald } 11605909f7f2Smatthias.ringwald if (!conn){ 116196a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, addr_type); 11625909f7f2Smatthias.ringwald } 11635909f7f2Smatthias.ringwald if (!conn){ 11645909f7f2Smatthias.ringwald // no memory 11655909f7f2Smatthias.ringwald break; 11665909f7f2Smatthias.ringwald } 11675909f7f2Smatthias.ringwald 11685909f7f2Smatthias.ringwald conn->state = OPEN; 11695909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 11705909f7f2Smatthias.ringwald 11715909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 11725909f7f2Smatthias.ringwald 11735909f7f2Smatthias.ringwald // restart timer 11745909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 11755909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 11765909f7f2Smatthias.ringwald 11775909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 11785909f7f2Smatthias.ringwald 11795909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 11805909f7f2Smatthias.ringwald break; 11815909f7f2Smatthias.ringwald 118265a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 118365a46ef3S[email protected] 11845909f7f2Smatthias.ringwald default: 11855909f7f2Smatthias.ringwald break; 11865909f7f2Smatthias.ringwald } 11875909f7f2Smatthias.ringwald break; 11885909f7f2Smatthias.ringwald #endif 11896772a24cSmatthias.ringwald default: 11906772a24cSmatthias.ringwald break; 1191fe1ed1b8Smatthias.ringwald } 1192fe1ed1b8Smatthias.ringwald 11933429f56bSmatthias.ringwald // handle BT initialization 11943a9fb326S[email protected] if (hci_stack->state == HCI_STATE_INITIALIZING){ 11956155b3d3S[email protected] hci_initializing_event_handler(packet, size); 1196c9af4d3fS[email protected] } 119722909952Smatthias.ringwald 119889db417bSmatthias.ringwald // help with BT sleep 11993a9fb326S[email protected] if (hci_stack->state == HCI_STATE_FALLING_ASLEEP 12003a9fb326S[email protected] && hci_stack->substate == 1 120189db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 12023a9fb326S[email protected] hci_stack->substate++; 120389db417bSmatthias.ringwald } 120489db417bSmatthias.ringwald 12053a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, packet, size); 120694ab26f8Smatthias.ringwald 120794ab26f8Smatthias.ringwald // execute main loop 120894ab26f8Smatthias.ringwald hci_run(); 120916833f0aSmatthias.ringwald } 121016833f0aSmatthias.ringwald 12110a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 121210e830c9Smatthias.ringwald switch (packet_type) { 121310e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 121410e830c9Smatthias.ringwald event_handler(packet, size); 121510e830c9Smatthias.ringwald break; 121610e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 121710e830c9Smatthias.ringwald acl_handler(packet, size); 121810e830c9Smatthias.ringwald break; 121910e830c9Smatthias.ringwald default: 122010e830c9Smatthias.ringwald break; 122110e830c9Smatthias.ringwald } 122210e830c9Smatthias.ringwald } 122310e830c9Smatthias.ringwald 1224fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 12252718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 12263a9fb326S[email protected] hci_stack->packet_handler = handler; 122716833f0aSmatthias.ringwald } 122816833f0aSmatthias.ringwald 12296155b3d3S[email protected] static void hci_state_reset(){ 1230595bdbfbS[email protected] // no connections yet 1231595bdbfbS[email protected] hci_stack->connections = NULL; 123274308b23Smatthias.ringwald 123374308b23Smatthias.ringwald // keep discoverable/connectable as this has been requested by the client(s) 123474308b23Smatthias.ringwald // hci_stack->discoverable = 0; 123574308b23Smatthias.ringwald // hci_stack->connectable = 0; 123674308b23Smatthias.ringwald // hci_stack->bondable = 1; 1237595bdbfbS[email protected] 123844935e40S[email protected] // buffer is free 123944935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 124044935e40S[email protected] 1241595bdbfbS[email protected] // no pending cmds 1242595bdbfbS[email protected] hci_stack->decline_reason = 0; 1243595bdbfbS[email protected] hci_stack->new_scan_enable_value = 0xff; 1244595bdbfbS[email protected] 1245595bdbfbS[email protected] // LE 1246595bdbfbS[email protected] hci_stack->adv_addr_type = 0; 1247595bdbfbS[email protected] memset(hci_stack->adv_address, 0, 6); 1248595bdbfbS[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 1249e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1250595bdbfbS[email protected] } 1251595bdbfbS[email protected] 12524f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 1253475c8125Smatthias.ringwald 12543a9fb326S[email protected] #ifdef HAVE_MALLOC 12553a9fb326S[email protected] if (!hci_stack) { 12563a9fb326S[email protected] hci_stack = (hci_stack_t*) malloc(sizeof(hci_stack_t)); 12573a9fb326S[email protected] } 12583a9fb326S[email protected] #else 12593a9fb326S[email protected] hci_stack = &hci_stack_static; 12603a9fb326S[email protected] #endif 126166fb9560S[email protected] memset(hci_stack, 0, sizeof(hci_stack_t)); 12623a9fb326S[email protected] 1263475c8125Smatthias.ringwald // reference to use transport layer implementation 12643a9fb326S[email protected] hci_stack->hci_transport = transport; 1265475c8125Smatthias.ringwald 126611e23e5fSmatthias.ringwald // references to used control implementation 12673a9fb326S[email protected] hci_stack->control = control; 126811e23e5fSmatthias.ringwald 126911e23e5fSmatthias.ringwald // reference to used config 12703a9fb326S[email protected] hci_stack->config = config; 127111e23e5fSmatthias.ringwald 127216833f0aSmatthias.ringwald // higher level handler 12733a9fb326S[email protected] hci_stack->packet_handler = dummy_handler; 127416833f0aSmatthias.ringwald 1275404843c1Smatthias.ringwald // store and open remote device db 12763a9fb326S[email protected] hci_stack->remote_device_db = remote_device_db; 12773a9fb326S[email protected] if (hci_stack->remote_device_db) { 12783a9fb326S[email protected] hci_stack->remote_device_db->open(); 1279404843c1Smatthias.ringwald } 128029d53098Smatthias.ringwald 12818fcba05dSmatthias.ringwald // max acl payload size defined in config.h 12823a9fb326S[email protected] hci_stack->acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 12838fcba05dSmatthias.ringwald 128416833f0aSmatthias.ringwald // register packet handlers with transport 128510e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 1286f5454fc6Smatthias.ringwald 12873a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 1288e2386ba1S[email protected] 1289e2386ba1S[email protected] // class of device 12903a9fb326S[email protected] hci_stack->class_of_device = 0x007a020c; // Smartphone 1291a45d6b9fS[email protected] 1292f20168b8Smatthias.ringwald // bondable by default 1293f20168b8Smatthias.ringwald hci_stack->bondable = 1; 1294f20168b8Smatthias.ringwald 129563048403S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, general bonding, mitm not required, auto accept 12963a9fb326S[email protected] hci_stack->ssp_enable = 1; 12973a9fb326S[email protected] hci_stack->ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 12983a9fb326S[email protected] hci_stack->ssp_authentication_requirement = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_GENERAL_BONDING; 12993a9fb326S[email protected] hci_stack->ssp_auto_accept = 1; 130069a97523S[email protected] 1301595bdbfbS[email protected] hci_state_reset(); 1302475c8125Smatthias.ringwald } 1303475c8125Smatthias.ringwald 1304404843c1Smatthias.ringwald void hci_close(){ 1305404843c1Smatthias.ringwald // close remote device db 13063a9fb326S[email protected] if (hci_stack->remote_device_db) { 13073a9fb326S[email protected] hci_stack->remote_device_db->close(); 1308404843c1Smatthias.ringwald } 13093a9fb326S[email protected] while (hci_stack->connections) { 1310bc68afe2S[email protected] // cancel all l2cap connections 1311bc68afe2S[email protected] hci_emit_disconnection_complete(((hci_connection_t *) hci_stack->connections)->con_handle, 0x16); // terminated by local host 13123a9fb326S[email protected] hci_shutdown_connection((hci_connection_t *) hci_stack->connections); 1313f5454fc6Smatthias.ringwald } 1314f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 13153a9fb326S[email protected] 13163a9fb326S[email protected] #ifdef HAVE_MALLOC 13173a9fb326S[email protected] free(hci_stack); 13183a9fb326S[email protected] #endif 13193a9fb326S[email protected] hci_stack = NULL; 1320404843c1Smatthias.ringwald } 1321404843c1Smatthias.ringwald 13229e61646fS[email protected] void hci_set_class_of_device(uint32_t class_of_device){ 13239e61646fS[email protected] hci_stack->class_of_device = class_of_device; 13249e61646fS[email protected] } 13259e61646fS[email protected] 132666fb9560S[email protected] void hci_disable_l2cap_timeout_check(){ 132766fb9560S[email protected] disable_l2cap_timeouts = 1; 132866fb9560S[email protected] } 13298d213e1aSmatthias.ringwald // State-Module-Driver overview 13308d213e1aSmatthias.ringwald // state module low-level 13318d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 13328d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 13338d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 13348d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 1335d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 1336d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 1337c7e0c5f6Smatthias.ringwald 133840d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 13397301ad89Smatthias.ringwald 1340038bc64cSmatthias.ringwald // power on 1341f9a30166Smatthias.ringwald int err = 0; 13423a9fb326S[email protected] if (hci_stack->control && hci_stack->control->on){ 13433a9fb326S[email protected] err = (*hci_stack->control->on)(hci_stack->config); 1344f9a30166Smatthias.ringwald } 1345038bc64cSmatthias.ringwald if (err){ 13467d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 1347038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1348038bc64cSmatthias.ringwald return err; 1349038bc64cSmatthias.ringwald } 1350038bc64cSmatthias.ringwald 1351038bc64cSmatthias.ringwald // open low-level device 13523a9fb326S[email protected] err = hci_stack->hci_transport->open(hci_stack->config); 1353038bc64cSmatthias.ringwald if (err){ 13547d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 13553a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 13563a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1357f9a30166Smatthias.ringwald } 1358038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 1359038bc64cSmatthias.ringwald return err; 1360038bc64cSmatthias.ringwald } 13618d213e1aSmatthias.ringwald return 0; 13628d213e1aSmatthias.ringwald } 1363038bc64cSmatthias.ringwald 136440d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 13658d213e1aSmatthias.ringwald 13667b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 13679418f9c9Smatthias.ringwald 13688d213e1aSmatthias.ringwald // close low-level device 13693a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 13708d213e1aSmatthias.ringwald 13717b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 13729418f9c9Smatthias.ringwald 13738d213e1aSmatthias.ringwald // power off 13743a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 13753a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 13768d213e1aSmatthias.ringwald } 13779418f9c9Smatthias.ringwald 13787b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 13799418f9c9Smatthias.ringwald 13803a9fb326S[email protected] hci_stack->state = HCI_STATE_OFF; 138172ea5239Smatthias.ringwald } 138272ea5239Smatthias.ringwald 138340d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 138472ea5239Smatthias.ringwald 13857b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 13863144bce4Smatthias.ringwald 1387b429b9b7Smatthias.ringwald #if 0 1388b429b9b7Smatthias.ringwald // don't close serial port during sleep 1389b429b9b7Smatthias.ringwald 139072ea5239Smatthias.ringwald // close low-level device 13913a9fb326S[email protected] hci_stack->hci_transport->close(hci_stack->config); 1392b429b9b7Smatthias.ringwald #endif 139372ea5239Smatthias.ringwald 139472ea5239Smatthias.ringwald // sleep mode 13953a9fb326S[email protected] if (hci_stack->control && hci_stack->control->sleep){ 13963a9fb326S[email protected] (*hci_stack->control->sleep)(hci_stack->config); 139772ea5239Smatthias.ringwald } 1398b429b9b7Smatthias.ringwald 13993a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 14008d213e1aSmatthias.ringwald } 14018d213e1aSmatthias.ringwald 140240d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 1403b429b9b7Smatthias.ringwald 14047b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 1405b429b9b7Smatthias.ringwald 1406b429b9b7Smatthias.ringwald // wake on 14073a9fb326S[email protected] if (hci_stack->control && hci_stack->control->wake){ 14083a9fb326S[email protected] (*hci_stack->control->wake)(hci_stack->config); 1409b429b9b7Smatthias.ringwald } 1410b429b9b7Smatthias.ringwald 1411b429b9b7Smatthias.ringwald #if 0 1412b429b9b7Smatthias.ringwald // open low-level device 14133a9fb326S[email protected] int err = hci_stack->hci_transport->open(hci_stack->config); 1414b429b9b7Smatthias.ringwald if (err){ 14157d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 14163a9fb326S[email protected] if (hci_stack->control && hci_stack->control->off){ 14173a9fb326S[email protected] (*hci_stack->control->off)(hci_stack->config); 1418b429b9b7Smatthias.ringwald } 1419b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 1420b429b9b7Smatthias.ringwald return err; 1421b429b9b7Smatthias.ringwald } 1422b429b9b7Smatthias.ringwald #endif 1423b429b9b7Smatthias.ringwald 1424b429b9b7Smatthias.ringwald return 0; 1425b429b9b7Smatthias.ringwald } 1426b429b9b7Smatthias.ringwald 142744935e40S[email protected] static void hci_power_transition_to_initializing(void){ 142844935e40S[email protected] // set up state machine 142944935e40S[email protected] hci_stack->num_cmd_packets = 1; // assume that one cmd can be sent 143044935e40S[email protected] hci_stack->hci_packet_buffer_reserved = 0; 143144935e40S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 143244935e40S[email protected] hci_stack->substate = 0; 143344935e40S[email protected] } 1434b429b9b7Smatthias.ringwald 14358d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 1436d661ed19Smatthias.ringwald 14373a9fb326S[email protected] log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack->state); 1438d661ed19Smatthias.ringwald 14398d213e1aSmatthias.ringwald int err = 0; 14403a9fb326S[email protected] switch (hci_stack->state){ 14418d213e1aSmatthias.ringwald 14428d213e1aSmatthias.ringwald case HCI_STATE_OFF: 14438d213e1aSmatthias.ringwald switch (power_mode){ 14448d213e1aSmatthias.ringwald case HCI_POWER_ON: 14458d213e1aSmatthias.ringwald err = hci_power_control_on(); 144697b61c7bS[email protected] if (err) { 144797b61c7bS[email protected] log_error("hci_power_control_on() error %u", err); 144897b61c7bS[email protected] return err; 144997b61c7bS[email protected] } 145044935e40S[email protected] hci_power_transition_to_initializing(); 14518d213e1aSmatthias.ringwald break; 14528d213e1aSmatthias.ringwald case HCI_POWER_OFF: 14538d213e1aSmatthias.ringwald // do nothing 14548d213e1aSmatthias.ringwald break; 14558d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1456b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 14578d213e1aSmatthias.ringwald break; 14588d213e1aSmatthias.ringwald } 14598d213e1aSmatthias.ringwald break; 14607301ad89Smatthias.ringwald 14618d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 14628d213e1aSmatthias.ringwald switch (power_mode){ 14638d213e1aSmatthias.ringwald case HCI_POWER_ON: 14648d213e1aSmatthias.ringwald // do nothing 14658d213e1aSmatthias.ringwald break; 14668d213e1aSmatthias.ringwald case HCI_POWER_OFF: 14678d213e1aSmatthias.ringwald // no connections yet, just turn it off 14688d213e1aSmatthias.ringwald hci_power_control_off(); 14698d213e1aSmatthias.ringwald break; 14708d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1471b546ac54Smatthias.ringwald // no connections yet, just turn it off 147272ea5239Smatthias.ringwald hci_power_control_sleep(); 14738d213e1aSmatthias.ringwald break; 14748d213e1aSmatthias.ringwald } 14758d213e1aSmatthias.ringwald break; 14767301ad89Smatthias.ringwald 14778d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 14788d213e1aSmatthias.ringwald switch (power_mode){ 14798d213e1aSmatthias.ringwald case HCI_POWER_ON: 14808d213e1aSmatthias.ringwald // do nothing 14818d213e1aSmatthias.ringwald break; 14828d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1483c7e0c5f6Smatthias.ringwald // see hci_run 14843a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 14858d213e1aSmatthias.ringwald break; 14868d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1487b546ac54Smatthias.ringwald // see hci_run 14883a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 14893a9fb326S[email protected] hci_stack->substate = 0; 14908d213e1aSmatthias.ringwald break; 14918d213e1aSmatthias.ringwald } 14928d213e1aSmatthias.ringwald break; 14937301ad89Smatthias.ringwald 14948d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 14958d213e1aSmatthias.ringwald switch (power_mode){ 14968d213e1aSmatthias.ringwald case HCI_POWER_ON: 149744935e40S[email protected] hci_power_transition_to_initializing(); 14988d213e1aSmatthias.ringwald break; 14998d213e1aSmatthias.ringwald case HCI_POWER_OFF: 15008d213e1aSmatthias.ringwald // do nothing 15018d213e1aSmatthias.ringwald break; 15028d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1503b546ac54Smatthias.ringwald // see hci_run 15043a9fb326S[email protected] hci_stack->state = HCI_STATE_FALLING_ASLEEP; 15053a9fb326S[email protected] hci_stack->substate = 0; 15068d213e1aSmatthias.ringwald break; 15078d213e1aSmatthias.ringwald } 15088d213e1aSmatthias.ringwald break; 15098d213e1aSmatthias.ringwald 15108d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 15118d213e1aSmatthias.ringwald switch (power_mode){ 15128d213e1aSmatthias.ringwald case HCI_POWER_ON: 151328171530Smatthias.ringwald 151428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 151528171530Smatthias.ringwald // nothing to do, if H4 supports power management 151628171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 15173a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 15183a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 151928171530Smatthias.ringwald break; 152028171530Smatthias.ringwald } 152128171530Smatthias.ringwald #endif 152244935e40S[email protected] hci_power_transition_to_initializing(); 15238d213e1aSmatthias.ringwald break; 15248d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1525b546ac54Smatthias.ringwald // see hci_run 15263a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 15278d213e1aSmatthias.ringwald break; 15288d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1529b546ac54Smatthias.ringwald // do nothing 15308d213e1aSmatthias.ringwald break; 15318d213e1aSmatthias.ringwald } 15328d213e1aSmatthias.ringwald break; 15338d213e1aSmatthias.ringwald 15348d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 15358d213e1aSmatthias.ringwald switch (power_mode){ 15368d213e1aSmatthias.ringwald case HCI_POWER_ON: 153728171530Smatthias.ringwald 153828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 153928171530Smatthias.ringwald // nothing to do, if H4 supports power management 154028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 15413a9fb326S[email protected] hci_stack->state = HCI_STATE_INITIALIZING; 15423a9fb326S[email protected] hci_stack->substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1543758b46ceSmatthias.ringwald hci_update_scan_enable(); 154428171530Smatthias.ringwald break; 154528171530Smatthias.ringwald } 154628171530Smatthias.ringwald #endif 15473144bce4Smatthias.ringwald err = hci_power_control_wake(); 15483144bce4Smatthias.ringwald if (err) return err; 154944935e40S[email protected] hci_power_transition_to_initializing(); 15508d213e1aSmatthias.ringwald break; 15518d213e1aSmatthias.ringwald case HCI_POWER_OFF: 15523a9fb326S[email protected] hci_stack->state = HCI_STATE_HALTING; 15538d213e1aSmatthias.ringwald break; 15548d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1555b546ac54Smatthias.ringwald // do nothing 15568d213e1aSmatthias.ringwald break; 15578d213e1aSmatthias.ringwald } 15588d213e1aSmatthias.ringwald break; 155911e23e5fSmatthias.ringwald } 156068d92d03Smatthias.ringwald 1561038bc64cSmatthias.ringwald // create internal event 1562ee8bf225Smatthias.ringwald hci_emit_state(); 1563ee8bf225Smatthias.ringwald 156468d92d03Smatthias.ringwald // trigger next/first action 156568d92d03Smatthias.ringwald hci_run(); 156668d92d03Smatthias.ringwald 1567475c8125Smatthias.ringwald return 0; 1568475c8125Smatthias.ringwald } 1569475c8125Smatthias.ringwald 1570758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1571758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 15723a9fb326S[email protected] hci_stack->new_scan_enable_value = hci_stack->connectable << 1 | hci_stack->discoverable; 1573758b46ceSmatthias.ringwald hci_run(); 1574758b46ceSmatthias.ringwald } 1575758b46ceSmatthias.ringwald 1576381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1577381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1578381fbed8Smatthias.ringwald 15793a9fb326S[email protected] if (hci_stack->discoverable == enable){ 15803a9fb326S[email protected] hci_emit_discoverable_enabled(hci_stack->discoverable); 1581381fbed8Smatthias.ringwald return; 1582381fbed8Smatthias.ringwald } 1583381fbed8Smatthias.ringwald 15843a9fb326S[email protected] hci_stack->discoverable = enable; 1585758b46ceSmatthias.ringwald hci_update_scan_enable(); 1586758b46ceSmatthias.ringwald } 1587b031bebbSmatthias.ringwald 1588758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1589758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1590758b46ceSmatthias.ringwald 1591758b46ceSmatthias.ringwald // don't emit event 15923a9fb326S[email protected] if (hci_stack->connectable == enable) return; 1593758b46ceSmatthias.ringwald 15943a9fb326S[email protected] hci_stack->connectable = enable; 1595758b46ceSmatthias.ringwald hci_update_scan_enable(); 1596381fbed8Smatthias.ringwald } 1597381fbed8Smatthias.ringwald 15985061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 15993a9fb326S[email protected] return &hci_stack->local_bd_addr; 16005061f3afS[email protected] } 16015061f3afS[email protected] 160206b35ec0Smatthias.ringwald void hci_run(){ 16038a485f27Smatthias.ringwald 160432ab9390Smatthias.ringwald hci_connection_t * connection; 160532ab9390Smatthias.ringwald linked_item_t * it; 160632ab9390Smatthias.ringwald 160764f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 1608ce4c8fabSmatthias.ringwald 1609b031bebbSmatthias.ringwald // global/non-connection oriented commands 1610b031bebbSmatthias.ringwald 1611b031bebbSmatthias.ringwald // decline incoming connections 16123a9fb326S[email protected] if (hci_stack->decline_reason){ 16133a9fb326S[email protected] uint8_t reason = hci_stack->decline_reason; 16143a9fb326S[email protected] hci_stack->decline_reason = 0; 16153a9fb326S[email protected] hci_send_cmd(&hci_reject_connection_request, hci_stack->decline_addr, reason); 1616dbe1a790S[email protected] return; 1617ce4c8fabSmatthias.ringwald } 1618ce4c8fabSmatthias.ringwald 1619b031bebbSmatthias.ringwald // send scan enable 16203a9fb326S[email protected] if (hci_stack->state == HCI_STATE_WORKING && hci_stack->new_scan_enable_value != 0xff && hci_classic_supported()){ 16213a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->new_scan_enable_value); 16223a9fb326S[email protected] hci_stack->new_scan_enable_value = 0xff; 1623dbe1a790S[email protected] return; 1624b031bebbSmatthias.ringwald } 1625b031bebbSmatthias.ringwald 1626b2f949feS[email protected] #ifdef HAVE_BLE 16277bdc6798S[email protected] // handle le scan 16287bdc6798S[email protected] if (hci_stack->state == HCI_STATE_WORKING){ 16297bdc6798S[email protected] switch(hci_stack->le_scanning_state){ 16307bdc6798S[email protected] case LE_START_SCAN: 16317bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCANNING; 16327bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 1, 0); 16337bdc6798S[email protected] return; 16347bdc6798S[email protected] 16357bdc6798S[email protected] case LE_STOP_SCAN: 16367bdc6798S[email protected] hci_stack->le_scanning_state = LE_SCAN_IDLE; 16377bdc6798S[email protected] hci_send_cmd(&hci_le_set_scan_enable, 0, 0); 16387bdc6798S[email protected] return; 16397bdc6798S[email protected] default: 16407bdc6798S[email protected] break; 16417bdc6798S[email protected] } 1642e2602ea2Smatthias.ringwald if (hci_stack->le_scan_type != 0xff){ 1643e2602ea2Smatthias.ringwald // defaults: active scanning, accept all advertisement packets 1644e2602ea2Smatthias.ringwald int scan_type = hci_stack->le_scan_type; 1645e2602ea2Smatthias.ringwald hci_stack->le_scan_type = 0xff; 1646e2602ea2Smatthias.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); 1647e2602ea2Smatthias.ringwald return; 1648e2602ea2Smatthias.ringwald } 16497bdc6798S[email protected] } 1650b2f949feS[email protected] #endif 16517bdc6798S[email protected] 165232ab9390Smatthias.ringwald // send pending HCI commands 16533a9fb326S[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 1654b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 165532ab9390Smatthias.ringwald 16560bf6344aS[email protected] switch(connection->state){ 16570bf6344aS[email protected] case SEND_CREATE_CONNECTION: 16584f3229d8S[email protected] switch(connection->address_type){ 16594f3229d8S[email protected] case BD_ADDR_TYPE_CLASSIC: 1660564fca32S[email protected] log_info("sending hci_create_connection\n"); 1661ad83dc6aS[email protected] hci_send_cmd(&hci_create_connection, connection->address, hci_usable_acl_packet_types(), 0, 0, 0, 1); 16624f3229d8S[email protected] break; 16634f3229d8S[email protected] default: 1664b2f949feS[email protected] #ifdef HAVE_BLE 1665564fca32S[email protected] log_info("sending hci_le_create_connection\n"); 16664f3229d8S[email protected] hci_send_cmd(&hci_le_create_connection, 1667b2571179Smatthias.ringwald 0x0060, // scan interval: 60 ms 1668b2571179Smatthias.ringwald 0x0030, // scan interval: 30 ms 16694f3229d8S[email protected] 0, // don't use whitelist 16704f3229d8S[email protected] connection->address_type, // peer address type 16714f3229d8S[email protected] connection->address, // peer bd addr 1672b2571179Smatthias.ringwald hci_stack->adv_addr_type, // our addr type: 1673b2571179Smatthias.ringwald 0x0008, // conn interval min 1674b2571179Smatthias.ringwald 0x0018, // conn interval max 16754f3229d8S[email protected] 0, // conn latency 1676b2571179Smatthias.ringwald 0x0048, // supervision timeout 1677b2571179Smatthias.ringwald 0x0001, // min ce length 1678b2571179Smatthias.ringwald 0x0001 // max ce length 16794f3229d8S[email protected] ); 16804f3229d8S[email protected] 16814f3229d8S[email protected] connection->state = SENT_CREATE_CONNECTION; 1682b2f949feS[email protected] #endif 16834f3229d8S[email protected] break; 16844f3229d8S[email protected] } 1685ad83dc6aS[email protected] return; 1686ad83dc6aS[email protected] 16870bf6344aS[email protected] case RECEIVED_CONNECTION_REQUEST: 16887b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 168932ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 169034d2123cS[email protected] hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1691dbe1a790S[email protected] return; 16920bf6344aS[email protected] 1693a6725849S[email protected] #ifdef HAVE_BLE 16940bf6344aS[email protected] case SEND_CANCEL_CONNECTION: 16950bf6344aS[email protected] connection->state = SENT_CANCEL_CONNECTION; 16960bf6344aS[email protected] hci_send_cmd(&hci_le_create_connection_cancel); 16970bf6344aS[email protected] return; 1698a6725849S[email protected] #endif 16990bf6344aS[email protected] case SEND_DISCONNECT: 17000bf6344aS[email protected] connection->state = SENT_DISCONNECT; 17017851196eSmatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 17020bf6344aS[email protected] return; 17030bf6344aS[email protected] 17040bf6344aS[email protected] default: 17050bf6344aS[email protected] break; 1706c7e0c5f6Smatthias.ringwald } 1707c7e0c5f6Smatthias.ringwald 170832ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 170934d2123cS[email protected] log_info("responding to link key request\n"); 171034d2123cS[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 171132ab9390Smatthias.ringwald link_key_t link_key; 1712c77e8838S[email protected] link_key_type_t link_key_type; 17133a9fb326S[email protected] if ( hci_stack->remote_device_db 17143a9fb326S[email protected] && hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type) 171534d2123cS[email protected] && gap_security_level_for_link_key_type(link_key_type) >= connection->requested_security_level){ 17169ab95c90S[email protected] connection->link_key_type = link_key_type; 171732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 171832ab9390Smatthias.ringwald } else { 171932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 172032ab9390Smatthias.ringwald } 1721dbe1a790S[email protected] return; 172232ab9390Smatthias.ringwald } 17231d6b20aeS[email protected] 1724899283eaS[email protected] if (connection->authentication_flags & DENY_PIN_CODE_REQUEST){ 17254c57c146S[email protected] log_info("denying to pin request\n"); 1726899283eaS[email protected] connectionClearAuthenticationFlags(connection, DENY_PIN_CODE_REQUEST); 172734d2123cS[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 17284c57c146S[email protected] return; 17294c57c146S[email protected] } 17304c57c146S[email protected] 1731dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 173234d2123cS[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 173382d8f825S[email protected] log_info("IO Capability Request received, stack bondable %u, io cap %u", hci_stack->bondable, hci_stack->ssp_io_capability); 173482d8f825S[email protected] if (hci_stack->bondable && (hci_stack->ssp_io_capability != SSP_IO_CAPABILITY_UNKNOWN)){ 1735106d6d11S[email protected] // tweak authentication requirements 17363a9fb326S[email protected] uint8_t authreq = hci_stack->ssp_authentication_requirement; 1737106d6d11S[email protected] if (connection->bonding_flags & BONDING_DEDICATED){ 17389faad3abS[email protected] authreq = SSP_IO_AUTHREQ_MITM_PROTECTION_NOT_REQUIRED_DEDICATED_BONDING; 17399faad3abS[email protected] } 17409faad3abS[email protected] if (gap_mitm_protection_required_for_security_level(connection->requested_security_level)){ 17419faad3abS[email protected] authreq |= 1; 1742106d6d11S[email protected] } 17433a9fb326S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack->ssp_io_capability, NULL, authreq); 1744f8fb5f6eS[email protected] } else { 1745f8fb5f6eS[email protected] hci_send_cmd(&hci_io_capability_request_negative_reply, &connection->address, ERROR_CODE_PAIRING_NOT_ALLOWED); 1746f8fb5f6eS[email protected] } 1747dbe1a790S[email protected] return; 174832ab9390Smatthias.ringwald } 174932ab9390Smatthias.ringwald 1750dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1751dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 175234d2123cS[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1753dbe1a790S[email protected] return; 1754dbe1a790S[email protected] } 1755dbe1a790S[email protected] 1756dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1757dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 175834d2123cS[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1759dbe1a790S[email protected] return; 1760dbe1a790S[email protected] } 1761afd4e962S[email protected] 1762afd4e962S[email protected] if (connection->bonding_flags & BONDING_REQUEST_REMOTE_FEATURES){ 1763afd4e962S[email protected] connection->bonding_flags &= ~BONDING_REQUEST_REMOTE_FEATURES; 176434d2123cS[email protected] hci_send_cmd(&hci_read_remote_supported_features_command, connection->con_handle); 17652bd8b7e7S[email protected] return; 17662bd8b7e7S[email protected] } 17672bd8b7e7S[email protected] 17682bd8b7e7S[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_SECURITY_BLOCK){ 17692bd8b7e7S[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_SECURITY_BLOCK; 177034d2123cS[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x0005); // authentication failure 177134d2123cS[email protected] return; 177234d2123cS[email protected] } 1773ad83dc6aS[email protected] if (connection->bonding_flags & BONDING_DISCONNECT_DEDICATED_DONE){ 1774ad83dc6aS[email protected] connection->bonding_flags &= ~BONDING_DISCONNECT_DEDICATED_DONE; 177552d34f98S[email protected] hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // authentication done 1776ad83dc6aS[email protected] return; 1777ad83dc6aS[email protected] } 177834d2123cS[email protected] if (connection->bonding_flags & BONDING_SEND_AUTHENTICATE_REQUEST){ 177934d2123cS[email protected] connection->bonding_flags &= ~BONDING_SEND_AUTHENTICATE_REQUEST; 178034d2123cS[email protected] hci_send_cmd(&hci_authentication_requested, connection->con_handle); 17812bd8b7e7S[email protected] return; 1782afd4e962S[email protected] } 1783dce78009S[email protected] if (connection->bonding_flags & BONDING_SEND_ENCRYPTION_REQUEST){ 1784dce78009S[email protected] connection->bonding_flags &= ~BONDING_SEND_ENCRYPTION_REQUEST; 1785dce78009S[email protected] hci_send_cmd(&hci_set_connection_encryption, connection->con_handle, 1); 1786dce78009S[email protected] return; 1787dce78009S[email protected] } 1788dbe1a790S[email protected] } 1789c7e0c5f6Smatthias.ringwald 17903a9fb326S[email protected] switch (hci_stack->state){ 17913429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 1792bd88fde9S[email protected] hci_initializing_state_machine(); 17933429f56bSmatthias.ringwald break; 1794c7e0c5f6Smatthias.ringwald 1795c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1796c7e0c5f6Smatthias.ringwald 17977b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1798c7e0c5f6Smatthias.ringwald // close all open connections 17993a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 1800c7e0c5f6Smatthias.ringwald if (connection){ 180132ab9390Smatthias.ringwald 1802c7e0c5f6Smatthias.ringwald // send disconnect 180364f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 180432ab9390Smatthias.ringwald 180579bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 18066ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1807c7e0c5f6Smatthias.ringwald 1808c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1809c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1810c7e0c5f6Smatthias.ringwald return; 1811c7e0c5f6Smatthias.ringwald } 18127b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1813c7e0c5f6Smatthias.ringwald 181472ea5239Smatthias.ringwald // switch mode 1815c7e0c5f6Smatthias.ringwald hci_power_control_off(); 18169418f9c9Smatthias.ringwald 18177b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 181872ea5239Smatthias.ringwald hci_emit_state(); 18197b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 182072ea5239Smatthias.ringwald break; 1821c7e0c5f6Smatthias.ringwald 182272ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 18233a9fb326S[email protected] switch(hci_stack->substate) { 182489db417bSmatthias.ringwald case 0: 18257b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 182672ea5239Smatthias.ringwald // close all open connections 18273a9fb326S[email protected] connection = (hci_connection_t *) hci_stack->connections; 182866da7044Smatthias.ringwald 182928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 183066da7044Smatthias.ringwald // don't close connections, if H4 supports power management 183166da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 183266da7044Smatthias.ringwald connection = NULL; 183366da7044Smatthias.ringwald } 183466da7044Smatthias.ringwald #endif 183572ea5239Smatthias.ringwald if (connection){ 183632ab9390Smatthias.ringwald 183772ea5239Smatthias.ringwald // send disconnect 18385e451dd1S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 183932ab9390Smatthias.ringwald 184079bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 18416ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 184272ea5239Smatthias.ringwald 184372ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 184472ea5239Smatthias.ringwald hci_shutdown_connection(connection); 184572ea5239Smatthias.ringwald return; 184672ea5239Smatthias.ringwald } 184772ea5239Smatthias.ringwald 184892368cd3S[email protected] if (hci_classic_supported()){ 184989db417bSmatthias.ringwald // disable page and inquiry scan 185064f0b431S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)) return; 185132ab9390Smatthias.ringwald 185292368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 18533a9fb326S[email protected] hci_send_cmd(&hci_write_scan_enable, hci_stack->connectable << 1); // drop inquiry scan but keep page scan 185489db417bSmatthias.ringwald 185589db417bSmatthias.ringwald // continue in next sub state 18563a9fb326S[email protected] hci_stack->substate++; 185789db417bSmatthias.ringwald break; 185892368cd3S[email protected] } 185992368cd3S[email protected] // fall through for ble-only chips 186092368cd3S[email protected] 186189db417bSmatthias.ringwald case 2: 18627b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 186328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 186428171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 186528171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 186628171530Smatthias.ringwald // SLEEP MODE reached 18673a9fb326S[email protected] hci_stack->state = HCI_STATE_SLEEPING; 186828171530Smatthias.ringwald hci_emit_state(); 186928171530Smatthias.ringwald break; 187028171530Smatthias.ringwald } 187128171530Smatthias.ringwald #endif 187272ea5239Smatthias.ringwald // switch mode 18733a9fb326S[email protected] hci_power_control_sleep(); // changes hci_stack->state to SLEEP 1874c7e0c5f6Smatthias.ringwald hci_emit_state(); 187528171530Smatthias.ringwald break; 187628171530Smatthias.ringwald 187789db417bSmatthias.ringwald default: 187889db417bSmatthias.ringwald break; 187989db417bSmatthias.ringwald } 1880c7e0c5f6Smatthias.ringwald break; 1881c7e0c5f6Smatthias.ringwald 18823429f56bSmatthias.ringwald default: 18833429f56bSmatthias.ringwald break; 18841f504dbdSmatthias.ringwald } 18853429f56bSmatthias.ringwald } 188616833f0aSmatthias.ringwald 188731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1888c8e4258aSmatthias.ringwald bd_addr_t addr; 1889c8e4258aSmatthias.ringwald hci_connection_t * conn; 1890c8e4258aSmatthias.ringwald // house-keeping 1891c8e4258aSmatthias.ringwald 1892c8e4258aSmatthias.ringwald // create_connection? 1893c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1894c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1895339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1896c8e4258aSmatthias.ringwald 189796a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 1898ad83dc6aS[email protected] if (!conn){ 189996a45072S[email protected] conn = create_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_CLASSIC); 190017f1ba2aSmatthias.ringwald if (!conn){ 190117f1ba2aSmatthias.ringwald // notify client that alloc failed 190217f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 190317f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 190417f1ba2aSmatthias.ringwald } 1905ad83dc6aS[email protected] conn->state = SEND_CREATE_CONNECTION; 1906ad83dc6aS[email protected] } 1907ad83dc6aS[email protected] log_info("conn state %u", conn->state); 1908ad83dc6aS[email protected] switch (conn->state){ 1909ad83dc6aS[email protected] // if connection active exists 1910ad83dc6aS[email protected] case OPEN: 191162bda3fbS[email protected] // and OPEN, emit connection complete command, don't send to controller 1912ad83dc6aS[email protected] hci_emit_connection_complete(conn, 0); 191362bda3fbS[email protected] return 0; 1914ad83dc6aS[email protected] case SEND_CREATE_CONNECTION: 1915ad83dc6aS[email protected] // connection created by hci, e.g. dedicated bonding 1916ad83dc6aS[email protected] break; 1917ad83dc6aS[email protected] default: 1918ad83dc6aS[email protected] // otherwise, just ignore as it is already in the open process 1919ad83dc6aS[email protected] return 0; 1920ad83dc6aS[email protected] } 1921c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1922c8e4258aSmatthias.ringwald } 1923c8e4258aSmatthias.ringwald 19247fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 19257fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 19267fde4af9Smatthias.ringwald } 19277fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 19287fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 19297fde4af9Smatthias.ringwald } 19307fde4af9Smatthias.ringwald 19318ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 19323a9fb326S[email protected] if (hci_stack->remote_device_db){ 19338ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 19343a9fb326S[email protected] hci_stack->remote_device_db->delete_link_key(&addr); 19358ef73945Smatthias.ringwald } 19368ef73945Smatthias.ringwald } 1937c8e4258aSmatthias.ringwald 19386724cd9eS[email protected] if (IS_COMMAND(packet, hci_pin_code_request_negative_reply) 19396724cd9eS[email protected] || IS_COMMAND(packet, hci_pin_code_request_reply)){ 19406724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 194196a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 19426724cd9eS[email protected] if (conn){ 19436724cd9eS[email protected] connectionClearAuthenticationFlags(conn, LEGACY_PAIRING_ACTIVE); 19446724cd9eS[email protected] } 19456724cd9eS[email protected] } 19466724cd9eS[email protected] 19476724cd9eS[email protected] if (IS_COMMAND(packet, hci_user_confirmation_request_negative_reply) 19486724cd9eS[email protected] || IS_COMMAND(packet, hci_user_confirmation_request_reply) 19496724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_negative_reply) 19506724cd9eS[email protected] || IS_COMMAND(packet, hci_user_passkey_request_reply)) { 19516724cd9eS[email protected] bt_flip_addr(addr, &packet[3]); 195296a45072S[email protected] conn = hci_connection_for_bd_addr_and_type(&addr, BD_ADDR_TYPE_CLASSIC); 19536724cd9eS[email protected] if (conn){ 19546724cd9eS[email protected] connectionClearAuthenticationFlags(conn, SSP_PAIRING_ACTIVE); 19556724cd9eS[email protected] } 19566724cd9eS[email protected] } 19576724cd9eS[email protected] 195869a97523S[email protected] #ifdef HAVE_BLE 195969a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 19603a9fb326S[email protected] hci_stack->adv_addr_type = packet[8]; 196169a97523S[email protected] } 196269a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 19633a9fb326S[email protected] bt_flip_addr(hci_stack->adv_address, &packet[3]); 196469a97523S[email protected] } 196569a97523S[email protected] #endif 196669a97523S[email protected] 19673a9fb326S[email protected] hci_stack->num_cmd_packets--; 19686b4af23dS[email protected] int err = hci_stack->hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 19696b4af23dS[email protected] 19706b4af23dS[email protected] // free packet buffer for synchronous transport implementations 1971c8b9416aS[email protected] if (hci_transport_synchronous() && (packet == hci_stack->hci_packet_buffer)){ 19726b4af23dS[email protected] hci_stack->hci_packet_buffer_reserved = 0; 19736b4af23dS[email protected] } 19746b4af23dS[email protected] 19756b4af23dS[email protected] return err; 197631452debSmatthias.ringwald } 19778adf0ddaSmatthias.ringwald 19782bd8b7e7S[email protected] // disconnect because of security block 19792bd8b7e7S[email protected] void hci_disconnect_security_block(hci_con_handle_t con_handle){ 19802bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 19812bd8b7e7S[email protected] if (!connection) return; 19822bd8b7e7S[email protected] connection->bonding_flags |= BONDING_DISCONNECT_SECURITY_BLOCK; 19832bd8b7e7S[email protected] } 19842bd8b7e7S[email protected] 19852bd8b7e7S[email protected] 1986dbe1a790S[email protected] // Configure Secure Simple Pairing 1987dbe1a790S[email protected] 1988dbe1a790S[email protected] // enable will enable SSP during init 1989dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 19903a9fb326S[email protected] hci_stack->ssp_enable = enable; 1991dbe1a790S[email protected] } 1992dbe1a790S[email protected] 19932bd8b7e7S[email protected] int hci_local_ssp_activated(){ 19943a9fb326S[email protected] return hci_ssp_supported() && hci_stack->ssp_enable; 19952bd8b7e7S[email protected] } 19962bd8b7e7S[email protected] 1997dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1998dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 19993a9fb326S[email protected] hci_stack->ssp_io_capability = io_capability; 2000dbe1a790S[email protected] } 2001dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 20023a9fb326S[email protected] hci_stack->ssp_authentication_requirement = authentication_requirement; 2003dbe1a790S[email protected] } 2004dbe1a790S[email protected] 2005dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 2006dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 20073a9fb326S[email protected] hci_stack->ssp_auto_accept = auto_accept; 2008dbe1a790S[email protected] } 2009dbe1a790S[email protected] 20101cd208adSmatthias.ringwald /** 20111cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 20121cd208adSmatthias.ringwald */ 2013fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 20145127cc62S[email protected] 20159d14b626S[email protected] if (!hci_can_send_packet_now_using_packet_buffer(HCI_COMMAND_DATA_PACKET)){ 20169d14b626S[email protected] log_error("hci_send_cmd called but cannot send packet now"); 20179d14b626S[email protected] return 0; 20189d14b626S[email protected] } 20199d14b626S[email protected] 20205127cc62S[email protected] // for HCI INITIALIZATION 20215127cc62S[email protected] // printf("hci_send_cmd: opcode %04x\n", cmd->opcode); 20225127cc62S[email protected] hci_stack->last_cmd_opcode = cmd->opcode; 20235127cc62S[email protected] 20249d14b626S[email protected] hci_reserve_packet_buffer(); 20259d14b626S[email protected] uint8_t * packet = hci_stack->hci_packet_buffer; 20269d14b626S[email protected] 20271cd208adSmatthias.ringwald va_list argptr; 20281cd208adSmatthias.ringwald va_start(argptr, cmd); 20299d14b626S[email protected] uint16_t size = hci_create_cmd_internal(packet, cmd, argptr); 20301cd208adSmatthias.ringwald va_end(argptr); 20319d14b626S[email protected] 20329d14b626S[email protected] return hci_send_cmd_packet(packet, size); 203393b8dc03Smatthias.ringwald } 2034c8e4258aSmatthias.ringwald 2035ee091cf1Smatthias.ringwald // Create various non-HCI events. 2036ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 2037ee091cf1Smatthias.ringwald 2038c8e4258aSmatthias.ringwald void hci_emit_state(){ 20393a9fb326S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack->state); 2040425d1371Smatthias.ringwald uint8_t event[3]; 204180d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 2042425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 20433a9fb326S[email protected] event[2] = hci_stack->state; 2044425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 20453a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2046c8e4258aSmatthias.ringwald } 2047c8e4258aSmatthias.ringwald 204817f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 2049425d1371Smatthias.ringwald uint8_t event[13]; 2050c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 2051425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 205217f1ba2aSmatthias.ringwald event[2] = status; 2053c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 2054c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 2055c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 2056c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 2057425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20583a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2059c8e4258aSmatthias.ringwald } 2060c8e4258aSmatthias.ringwald 20614f3229d8S[email protected] void hci_emit_le_connection_complete(hci_connection_t *conn, uint8_t status){ 20624f3229d8S[email protected] uint8_t event[21]; 20634f3229d8S[email protected] event[0] = HCI_EVENT_LE_META; 20644f3229d8S[email protected] event[1] = sizeof(event) - 2; 20654f3229d8S[email protected] event[2] = HCI_SUBEVENT_LE_CONNECTION_COMPLETE; 20664f3229d8S[email protected] event[3] = status; 20674f3229d8S[email protected] bt_store_16(event, 4, conn->con_handle); 20684f3229d8S[email protected] event[6] = 0; // TODO: role 20694f3229d8S[email protected] event[7] = conn->address_type; 20704f3229d8S[email protected] bt_flip_addr(&event[8], conn->address); 20714f3229d8S[email protected] bt_store_16(event, 14, 0); // interval 20724f3229d8S[email protected] bt_store_16(event, 16, 0); // latency 20734f3229d8S[email protected] bt_store_16(event, 18, 0); // supervision timeout 20744f3229d8S[email protected] event[20] = 0; // master clock accuracy 20754f3229d8S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20764f3229d8S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 20774f3229d8S[email protected] } 20784f3229d8S[email protected] 20793c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 2080425d1371Smatthias.ringwald uint8_t event[6]; 20813c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 2082e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 20833c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 20843c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 20853c4d4b90Smatthias.ringwald event[5] = reason; 2086425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20873a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 20883c4d4b90Smatthias.ringwald } 20893c4d4b90Smatthias.ringwald 2090ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 209166fb9560S[email protected] if (disable_l2cap_timeouts) return; 2092e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 2093425d1371Smatthias.ringwald uint8_t event[4]; 209480d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 2095425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2096ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 2097425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 20983a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2099ee091cf1Smatthias.ringwald } 210043bfb1bdSmatthias.ringwald 210143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 2102e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 2103425d1371Smatthias.ringwald uint8_t event[3]; 210480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 2105425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 210643bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 2107425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21083a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 210943bfb1bdSmatthias.ringwald } 2110038bc64cSmatthias.ringwald 2111038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 2112e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 2113425d1371Smatthias.ringwald uint8_t event[2]; 211480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 2115425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2116425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21173a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2118038bc64cSmatthias.ringwald } 21191b0e3922Smatthias.ringwald 212009ba8edeSmatthias.ringwald #ifndef EMBEDDED 21211b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 2122e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 2123425d1371Smatthias.ringwald uint8_t event[6]; 21241b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 2125425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2126425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 2127425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 2128425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 2129425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21303a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 21311b0e3922Smatthias.ringwald } 213209ba8edeSmatthias.ringwald #endif 21331b0e3922Smatthias.ringwald 21342ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 2135e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 2136425d1371Smatthias.ringwald uint8_t event[3]; 21372ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 2138425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 21392ed6235cSmatthias.ringwald event[2] = enabled; 2140425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21413a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 21422ed6235cSmatthias.ringwald } 2143627c2f45Smatthias.ringwald 2144627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 2145e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 2146627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 2147e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 2148f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 21492f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 2150f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 2151e0abb8e7S[email protected] 2152e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 2153e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 2154e0abb8e7S[email protected] 2155e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 21563a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 2157627c2f45Smatthias.ringwald } 2158381fbed8Smatthias.ringwald 2159381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 2160e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 2161425d1371Smatthias.ringwald uint8_t event[3]; 2162381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 2163425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 2164381fbed8Smatthias.ringwald event[2] = enabled; 2165425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21663a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2167381fbed8Smatthias.ringwald } 2168458bf4e8S[email protected] 2169a00031e2S[email protected] void hci_emit_security_level(hci_con_handle_t con_handle, gap_security_level_t level){ 2170df3354fcS[email protected] log_info("hci_emit_security_level %u for handle %x", level, con_handle); 2171a00031e2S[email protected] uint8_t event[5]; 2172e00caf9cS[email protected] int pos = 0; 2173a00031e2S[email protected] event[pos++] = GAP_SECURITY_LEVEL; 2174e00caf9cS[email protected] event[pos++] = sizeof(event) - 2; 2175a00031e2S[email protected] bt_store_16(event, 2, con_handle); 2176e00caf9cS[email protected] pos += 2; 2177e00caf9cS[email protected] event[pos++] = level; 2178e00caf9cS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21793a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2180e00caf9cS[email protected] } 2181e00caf9cS[email protected] 2182ad83dc6aS[email protected] void hci_emit_dedicated_bonding_result(hci_connection_t * connection, uint8_t status){ 2183ad83dc6aS[email protected] log_info("hci_emit_dedicated_bonding_result %u ", status); 2184ad83dc6aS[email protected] uint8_t event[9]; 2185ad83dc6aS[email protected] int pos = 0; 2186ad83dc6aS[email protected] event[pos++] = GAP_DEDICATED_BONDING_COMPLETED; 2187ad83dc6aS[email protected] event[pos++] = sizeof(event) - 2; 2188ad83dc6aS[email protected] event[pos++] = status; 2189ad83dc6aS[email protected] bt_flip_addr( * (bd_addr_t *) &event[pos], connection->address); 2190ad83dc6aS[email protected] pos += 6; 2191ad83dc6aS[email protected] hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 21923a9fb326S[email protected] hci_stack->packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 2193ad83dc6aS[email protected] } 2194ad83dc6aS[email protected] 21952bd8b7e7S[email protected] // query if remote side supports SSP 21962bd8b7e7S[email protected] int hci_remote_ssp_supported(hci_con_handle_t con_handle){ 21972bd8b7e7S[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 21982bd8b7e7S[email protected] if (!connection) return 0; 21992bd8b7e7S[email protected] return (connection->bonding_flags & BONDING_REMOTE_SUPPORTS_SSP) ? 1 : 0; 22002bd8b7e7S[email protected] } 22012bd8b7e7S[email protected] 2202df3354fcS[email protected] int hci_ssp_supported_on_both_sides(hci_con_handle_t handle){ 2203df3354fcS[email protected] return hci_local_ssp_activated() && hci_remote_ssp_supported(handle); 2204df3354fcS[email protected] } 2205df3354fcS[email protected] 2206458bf4e8S[email protected] // GAP API 2207458bf4e8S[email protected] /** 2208458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 2209458bf4e8S[email protected] * @praram enabled 2210458bf4e8S[email protected] */ 22114c57c146S[email protected] void gap_set_bondable_mode(int enable){ 22123a9fb326S[email protected] hci_stack->bondable = enable ? 1 : 0; 2213458bf4e8S[email protected] } 2214cb230b9dS[email protected] 2215cb230b9dS[email protected] /** 221634d2123cS[email protected] * @brief map link keys to security levels 2217cb230b9dS[email protected] */ 221834d2123cS[email protected] gap_security_level_t gap_security_level_for_link_key_type(link_key_type_t link_key_type){ 221934d2123cS[email protected] switch (link_key_type){ 22203c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P256: 22213c68dfa9S[email protected] return LEVEL_4; 22223c68dfa9S[email protected] case COMBINATION_KEY: 22233c68dfa9S[email protected] case AUTHENTICATED_COMBINATION_KEY_GENERATED_FROM_P192: 22243c68dfa9S[email protected] return LEVEL_3; 22253c68dfa9S[email protected] default: 22263c68dfa9S[email protected] return LEVEL_2; 22273c68dfa9S[email protected] } 2228cb230b9dS[email protected] } 2229cb230b9dS[email protected] 2230a00031e2S[email protected] static gap_security_level_t gap_security_level_for_connection(hci_connection_t * connection){ 223134d2123cS[email protected] if (!connection) return LEVEL_0; 223234d2123cS[email protected] if ((connection->authentication_flags & CONNECTION_ENCRYPTED) == 0) return LEVEL_0; 223334d2123cS[email protected] return gap_security_level_for_link_key_type(connection->link_key_type); 223434d2123cS[email protected] } 223534d2123cS[email protected] 223634d2123cS[email protected] 2237106d6d11S[email protected] int gap_mitm_protection_required_for_security_level(gap_security_level_t level){ 22385127cc62S[email protected] log_info("gap_mitm_protection_required_for_security_level %u", level); 2239106d6d11S[email protected] return level > LEVEL_2; 2240106d6d11S[email protected] } 2241106d6d11S[email protected] 224234d2123cS[email protected] /** 224334d2123cS[email protected] * @brief get current security level 224434d2123cS[email protected] */ 224534d2123cS[email protected] gap_security_level_t gap_security_level(hci_con_handle_t con_handle){ 224634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 224734d2123cS[email protected] if (!connection) return LEVEL_0; 224834d2123cS[email protected] return gap_security_level_for_connection(connection); 224934d2123cS[email protected] } 225034d2123cS[email protected] 2251cb230b9dS[email protected] /** 2252cb230b9dS[email protected] * @brief request connection to device to 2253cb230b9dS[email protected] * @result GAP_AUTHENTICATION_RESULT 2254cb230b9dS[email protected] */ 225534d2123cS[email protected] void gap_request_security_level(hci_con_handle_t con_handle, gap_security_level_t requested_level){ 225634d2123cS[email protected] hci_connection_t * connection = hci_connection_for_handle(con_handle); 225734d2123cS[email protected] if (!connection){ 2258a00031e2S[email protected] hci_emit_security_level(con_handle, LEVEL_0); 225934d2123cS[email protected] return; 226034d2123cS[email protected] } 226134d2123cS[email protected] gap_security_level_t current_level = gap_security_level(con_handle); 226234d2123cS[email protected] log_info("gap_request_security_level %u, current level %u", requested_level, current_level); 226334d2123cS[email protected] if (current_level >= requested_level){ 2264a00031e2S[email protected] hci_emit_security_level(con_handle, current_level); 226534d2123cS[email protected] return; 226634d2123cS[email protected] } 2267a00031e2S[email protected] 226834d2123cS[email protected] connection->requested_security_level = requested_level; 2269a00031e2S[email protected] 2270fb8ba0dbS[email protected] // would enabling ecnryption suffice (>= LEVEL_2)? 22713a9fb326S[email protected] if (hci_stack->remote_device_db){ 2272a00031e2S[email protected] link_key_type_t link_key_type; 2273a00031e2S[email protected] link_key_t link_key; 22743a9fb326S[email protected] if (hci_stack->remote_device_db->get_link_key( &connection->address, &link_key, &link_key_type)){ 2275a00031e2S[email protected] if (gap_security_level_for_link_key_type(link_key_type) >= requested_level){ 2276a00031e2S[email protected] connection->bonding_flags |= BONDING_SEND_ENCRYPTION_REQUEST; 2277a00031e2S[email protected] return; 2278a00031e2S[email protected] } 2279a00031e2S[email protected] } 2280a00031e2S[email protected] } 2281a00031e2S[email protected] 22821eb2563eS[email protected] // try to authenticate connection 22831eb2563eS[email protected] connection->bonding_flags |= BONDING_SEND_AUTHENTICATE_REQUEST; 2284e80b2cf9S[email protected] hci_run(); 2285e00caf9cS[email protected] } 2286ad83dc6aS[email protected] 2287ad83dc6aS[email protected] /** 2288ad83dc6aS[email protected] * @brief start dedicated bonding with device. disconnect after bonding 2289ad83dc6aS[email protected] * @param device 2290ad83dc6aS[email protected] * @param request MITM protection 2291ad83dc6aS[email protected] * @result GAP_DEDICATED_BONDING_COMPLETE 2292ad83dc6aS[email protected] */ 2293ad83dc6aS[email protected] int gap_dedicated_bonding(bd_addr_t device, int mitm_protection_required){ 2294ad83dc6aS[email protected] 2295ad83dc6aS[email protected] // create connection state machine 229696a45072S[email protected] hci_connection_t * connection = create_connection_for_bd_addr_and_type(device, BD_ADDR_TYPE_CLASSIC); 2297ad83dc6aS[email protected] 2298ad83dc6aS[email protected] if (!connection){ 2299ad83dc6aS[email protected] return BTSTACK_MEMORY_ALLOC_FAILED; 2300ad83dc6aS[email protected] } 2301ad83dc6aS[email protected] 2302ad83dc6aS[email protected] // delete linkn key 2303ad83dc6aS[email protected] hci_drop_link_key_for_bd_addr( (bd_addr_t *) &device); 2304ad83dc6aS[email protected] 2305ad83dc6aS[email protected] // configure LEVEL_2/3, dedicated bonding 2306ad83dc6aS[email protected] connection->state = SEND_CREATE_CONNECTION; 2307ad83dc6aS[email protected] connection->requested_security_level = mitm_protection_required ? LEVEL_3 : LEVEL_2; 23085127cc62S[email protected] log_info("gap_dedicated_bonding, mitm %u -> level %u", mitm_protection_required, connection->requested_security_level); 2309ad83dc6aS[email protected] connection->bonding_flags = BONDING_DEDICATED; 2310ad83dc6aS[email protected] 2311ad83dc6aS[email protected] // wait for GAP Security Result and send GAP Dedicated Bonding complete 2312ad83dc6aS[email protected] 2313ad83dc6aS[email protected] // handle: connnection failure (connection complete != ok) 2314ad83dc6aS[email protected] // handle: authentication failure 2315ad83dc6aS[email protected] // handle: disconnect on done 2316ad83dc6aS[email protected] 2317ad83dc6aS[email protected] hci_run(); 2318ad83dc6aS[email protected] 2319ad83dc6aS[email protected] return 0; 2320ad83dc6aS[email protected] } 23218e618f72S[email protected] 23228e618f72S[email protected] void gap_set_local_name(const char * local_name){ 23238e618f72S[email protected] hci_stack->local_name = local_name; 23248e618f72S[email protected] } 23258e618f72S[email protected] 23267bdc6798S[email protected] le_command_status_t le_central_start_scan(){ 232736af3e18S[email protected] if (hci_stack->le_scanning_state == LE_SCANNING) return BLE_PERIPHERAL_OK; 23287bdc6798S[email protected] hci_stack->le_scanning_state = LE_START_SCAN; 23297bdc6798S[email protected] hci_run(); 23307bdc6798S[email protected] return BLE_PERIPHERAL_OK; 23317bdc6798S[email protected] } 23328e618f72S[email protected] 23337bdc6798S[email protected] le_command_status_t le_central_stop_scan(){ 233436af3e18S[email protected] if ( hci_stack->le_scanning_state == LE_SCAN_IDLE) return BLE_PERIPHERAL_OK; 23357bdc6798S[email protected] hci_stack->le_scanning_state = LE_STOP_SCAN; 23367bdc6798S[email protected] hci_run(); 23377bdc6798S[email protected] return BLE_PERIPHERAL_OK; 23387bdc6798S[email protected] } 23394f3229d8S[email protected] 2340ef11999fSmatthias.ringwald void le_central_set_scan_parameters(uint8_t scan_type, uint16_t scan_interval, uint16_t scan_window){ 2341ef11999fSmatthias.ringwald hci_stack->le_scan_type = scan_type; 2342ef11999fSmatthias.ringwald hci_stack->le_scan_interval = scan_interval; 2343ef11999fSmatthias.ringwald hci_stack->le_scan_window = scan_window; 2344ef11999fSmatthias.ringwald hci_run(); 2345ef11999fSmatthias.ringwald } 23464f3229d8S[email protected] 23474f3229d8S[email protected] le_command_status_t le_central_connect(bd_addr_t * addr, bd_addr_type_t addr_type){ 23484f3229d8S[email protected] hci_connection_t * conn = hci_connection_for_bd_addr_and_type(addr, addr_type); 23494f3229d8S[email protected] if (!conn){ 23501f479f8cS[email protected] log_info("le_central_connect: no connection exists yet, creating context"); 23514f3229d8S[email protected] conn = create_connection_for_bd_addr_and_type(*addr, addr_type); 23524f3229d8S[email protected] if (!conn){ 23534f3229d8S[email protected] // notify client that alloc failed 23544f3229d8S[email protected] hci_emit_le_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 23551f479f8cS[email protected] log_info("le_central_connect: failed to alloc context"); 23564f3229d8S[email protected] return BLE_PERIPHERAL_NOT_CONNECTED; // don't sent packet to controller 23574f3229d8S[email protected] } 23584f3229d8S[email protected] conn->state = SEND_CREATE_CONNECTION; 23591f479f8cS[email protected] log_info("le_central_connect: send create connection next"); 2360564fca32S[email protected] hci_run(); 23614f3229d8S[email protected] return BLE_PERIPHERAL_OK; 23624f3229d8S[email protected] } 23630bf6344aS[email protected] 23640bf6344aS[email protected] if (!hci_is_le_connection(conn) || 23650bf6344aS[email protected] conn->state == SEND_CREATE_CONNECTION || 23660bf6344aS[email protected] conn->state == SENT_CREATE_CONNECTION) { 23670bf6344aS[email protected] hci_emit_le_connection_complete(conn, ERROR_CODE_COMMAND_DISALLOWED); 23681f479f8cS[email protected] log_error("le_central_connect: classic connection or connect is already being created"); 23690bf6344aS[email protected] return BLE_PERIPHERAL_IN_WRONG_STATE; 23700bf6344aS[email protected] } 23710bf6344aS[email protected] 23721f479f8cS[email protected] log_info("le_central_connect: context exists with state %u", conn->state); 23734f3229d8S[email protected] hci_emit_le_connection_complete(conn, 0); 23744f3229d8S[email protected] hci_run(); 23754f3229d8S[email protected] return BLE_PERIPHERAL_OK; 23764f3229d8S[email protected] } 23774f3229d8S[email protected] 23787851196eSmatthias.ringwald // @assumption: only a single outgoing LE Connection exists 23797851196eSmatthias.ringwald static hci_connection_t * le_central_get_outgoing_connection(){ 23800bf6344aS[email protected] linked_item_t *it; 23810bf6344aS[email protected] for (it = (linked_item_t *) hci_stack->connections; it ; it = it->next){ 23820bf6344aS[email protected] hci_connection_t * conn = (hci_connection_t *) it; 23830bf6344aS[email protected] if (!hci_is_le_connection(conn)) continue; 23840bf6344aS[email protected] switch (conn->state){ 2385a6725849S[email protected] case SEND_CREATE_CONNECTION: 23867851196eSmatthias.ringwald case SENT_CREATE_CONNECTION: 23877851196eSmatthias.ringwald return conn; 23887851196eSmatthias.ringwald default: 23897851196eSmatthias.ringwald break; 23907851196eSmatthias.ringwald }; 23917851196eSmatthias.ringwald } 23927851196eSmatthias.ringwald return NULL; 23937851196eSmatthias.ringwald } 23947851196eSmatthias.ringwald 23957851196eSmatthias.ringwald le_command_status_t le_central_connect_cancel(){ 23967851196eSmatthias.ringwald hci_connection_t * conn = le_central_get_outgoing_connection(); 23977851196eSmatthias.ringwald switch (conn->state){ 23987851196eSmatthias.ringwald case SEND_CREATE_CONNECTION: 23997851196eSmatthias.ringwald // skip sending create connection and emit event instead 24000bf6344aS[email protected] hci_emit_le_connection_complete(conn, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER); 24017851196eSmatthias.ringwald linked_list_remove(&hci_stack->connections, (linked_item_t *) conn); 24027851196eSmatthias.ringwald btstack_memory_hci_connection_free( conn ); 24030bf6344aS[email protected] break; 2404a6725849S[email protected] case SENT_CREATE_CONNECTION: 24057851196eSmatthias.ringwald // request to send cancel connection 24060bf6344aS[email protected] conn->state = SEND_CANCEL_CONNECTION; 24070bf6344aS[email protected] hci_run(); 24080bf6344aS[email protected] break; 24090bf6344aS[email protected] default: 24100bf6344aS[email protected] break; 24110bf6344aS[email protected] } 2412e31f89a7S[email protected] return BLE_PERIPHERAL_OK; 2413e31f89a7S[email protected] } 24144f3229d8S[email protected] 24155917a5c5S[email protected] le_command_status_t gap_disconnect(hci_con_handle_t handle){ 24165917a5c5S[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 24175917a5c5S[email protected] if (!conn){ 24187851196eSmatthias.ringwald hci_emit_disconnection_complete(handle, 0); 24195917a5c5S[email protected] return BLE_PERIPHERAL_OK; 24205917a5c5S[email protected] } 24215917a5c5S[email protected] conn->state = SEND_DISCONNECT; 24225917a5c5S[email protected] hci_run(); 24234f3229d8S[email protected] return BLE_PERIPHERAL_OK; 24244f3229d8S[email protected] } 242504a6ef8cSmatthias.ringwald 242604a6ef8cSmatthias.ringwald void hci_disconnect_all(){ 242704a6ef8cSmatthias.ringwald linked_list_iterator_t it; 242804a6ef8cSmatthias.ringwald linked_list_iterator_init(&it, &hci_stack->connections); 242904a6ef8cSmatthias.ringwald while (linked_list_iterator_has_next(&it)){ 243004a6ef8cSmatthias.ringwald hci_connection_t * con = (hci_connection_t*) linked_list_iterator_next(&it); 243104a6ef8cSmatthias.ringwald if (con->state == SENT_DISCONNECT) continue; 243204a6ef8cSmatthias.ringwald con->state = SEND_DISCONNECT; 243304a6ef8cSmatthias.ringwald } 2434d31fba26S[email protected] hci_run(); 243504a6ef8cSmatthias.ringwald } 2436