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 44c8901d41Smatthias.ringwald #include "config.h" 4528171530Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "hci.h" 477f2435e6Smatthias.ringwald 4893b8dc03Smatthias.ringwald #include <stdarg.h> 4993b8dc03Smatthias.ringwald #include <string.h> 5056fe0872Smatthias.ringwald #include <stdio.h> 517f2435e6Smatthias.ringwald 52549e6ebeSmatthias.ringwald #ifndef EMBEDDED 53549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 5409ba8edeSmatthias.ringwald #include <btstack/version.h> 55549e6ebeSmatthias.ringwald #endif 56549e6ebeSmatthias.ringwald 57a3b02b71Smatthias.ringwald #include "btstack_memory.h" 587f2435e6Smatthias.ringwald #include "debug.h" 59d8905019Smatthias.ringwald #include "hci_dump.h" 6093b8dc03Smatthias.ringwald 61ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 621b0e3922Smatthias.ringwald 63169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 64ee091cf1Smatthias.ringwald 65a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 66da5275c5S[email protected] 6728171530Smatthias.ringwald #ifdef USE_BLUETOOL 6828171530Smatthias.ringwald #include "bt_control_iphone.h" 6928171530Smatthias.ringwald #endif 7028171530Smatthias.ringwald 71758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 72758b46ceSmatthias.ringwald 7306b35ec0Smatthias.ringwald // the STACK is here 7416833f0aSmatthias.ringwald static hci_stack_t hci_stack; 7516833f0aSmatthias.ringwald 7697addcc5Smatthias.ringwald /** 77ee091cf1Smatthias.ringwald * get connection for a given handle 78ee091cf1Smatthias.ringwald * 79ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 80ee091cf1Smatthias.ringwald */ 815061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 82ee091cf1Smatthias.ringwald linked_item_t *it; 83ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 84ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 85ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 86ee091cf1Smatthias.ringwald } 87ee091cf1Smatthias.ringwald } 88ee091cf1Smatthias.ringwald return NULL; 89ee091cf1Smatthias.ringwald } 90ee091cf1Smatthias.ringwald 912b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 9228ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 93c785ef68Smatthias.ringwald #ifdef HAVE_TIME 94ee091cf1Smatthias.ringwald struct timeval tv; 95ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 96c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 97ee091cf1Smatthias.ringwald // connections might be timed out 98ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 99ee091cf1Smatthias.ringwald } 1002b12a0b9Smatthias.ringwald #endif 101e5780900Smatthias.ringwald #ifdef HAVE_TICK 102c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 103c785ef68Smatthias.ringwald // connections might be timed out 104c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 105c785ef68Smatthias.ringwald } 106c785ef68Smatthias.ringwald #endif 107c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 108c785ef68Smatthias.ringwald run_loop_add_timer(timer); 109c785ef68Smatthias.ringwald } 110ee091cf1Smatthias.ringwald 111ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 112c7492964Smatthias.ringwald #ifdef HAVE_TIME 113ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 114c7492964Smatthias.ringwald #endif 115e5780900Smatthias.ringwald #ifdef HAVE_TICK 116c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 117c785ef68Smatthias.ringwald #endif 118ee091cf1Smatthias.ringwald } 119ee091cf1Smatthias.ringwald 120ee091cf1Smatthias.ringwald /** 121c8e4258aSmatthias.ringwald * create connection for given address 122c8e4258aSmatthias.ringwald * 12317f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 124c8e4258aSmatthias.ringwald */ 125c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 12628ca2b46S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 127c8e4258aSmatthias.ringwald if (!conn) return NULL; 128c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 129c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1307d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 131ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 132ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 133ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 134d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1357856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 13656cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 137c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 138c8e4258aSmatthias.ringwald return conn; 139c8e4258aSmatthias.ringwald } 140c8e4258aSmatthias.ringwald 141c8e4258aSmatthias.ringwald /** 14206b35ec0Smatthias.ringwald * get connection for given address 14397addcc5Smatthias.ringwald * 14497addcc5Smatthias.ringwald * @return connection OR NULL, if not found 14597addcc5Smatthias.ringwald */ 146fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 14706b35ec0Smatthias.ringwald linked_item_t *it; 14806b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 14906b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 15006b35ec0Smatthias.ringwald return (hci_connection_t *) it; 15106b35ec0Smatthias.ringwald } 15206b35ec0Smatthias.ringwald } 15306b35ec0Smatthias.ringwald return NULL; 15406b35ec0Smatthias.ringwald } 15506b35ec0Smatthias.ringwald 15628ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 15728ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 15828ca2b46S[email protected] } 15928ca2b46S[email protected] 16028ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 16128ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 16228ca2b46S[email protected] } 16328ca2b46S[email protected] 16428ca2b46S[email protected] 16543bfb1bdSmatthias.ringwald /** 16680ca58a0Smatthias.ringwald * add authentication flags and reset timer 1677fde4af9Smatthias.ringwald */ 1687fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1697fde4af9Smatthias.ringwald bd_addr_t addr; 1707fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1717fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1727fde4af9Smatthias.ringwald if (conn) { 17328ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 17480ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1757fde4af9Smatthias.ringwald } 1767fde4af9Smatthias.ringwald } 1777fde4af9Smatthias.ringwald 17880ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 1795061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 18080ca58a0Smatthias.ringwald if (!conn) return 0; 18180ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 18280ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 18380ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 18480ca58a0Smatthias.ringwald return 1; 18580ca58a0Smatthias.ringwald } 18680ca58a0Smatthias.ringwald 187c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 188c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 189c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 190c12e46e7Smatthias.ringwald } 191c12e46e7Smatthias.ringwald } 192c12e46e7Smatthias.ringwald 1937fde4af9Smatthias.ringwald 1947fde4af9Smatthias.ringwald /** 19543bfb1bdSmatthias.ringwald * count connections 19643bfb1bdSmatthias.ringwald */ 19740d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 19856c253c9Smatthias.ringwald int count = 0; 19943bfb1bdSmatthias.ringwald linked_item_t *it; 20043bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 20143bfb1bdSmatthias.ringwald return count; 20243bfb1bdSmatthias.ringwald } 203c8e4258aSmatthias.ringwald 20497addcc5Smatthias.ringwald /** 205ba681a6cSmatthias.ringwald * Dummy handler called by HCI 20616833f0aSmatthias.ringwald */ 2072718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 20816833f0aSmatthias.ringwald } 20916833f0aSmatthias.ringwald 210998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2115061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 212998906cdSmatthias.ringwald if (!connection) { 2137d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 214998906cdSmatthias.ringwald return 0; 215998906cdSmatthias.ringwald } 216998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 217998906cdSmatthias.ringwald } 218998906cdSmatthias.ringwald 219998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 220998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 221998906cdSmatthias.ringwald linked_item_t *it; 222998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 223998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 224998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2257d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 226998906cdSmatthias.ringwald return 0; 227998906cdSmatthias.ringwald } 228998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 229998906cdSmatthias.ringwald } 230998906cdSmatthias.ringwald return free_slots; 231998906cdSmatthias.ringwald } 232998906cdSmatthias.ringwald 233c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2342b12a0b9Smatthias.ringwald 2352b12a0b9Smatthias.ringwald // check for async hci transport implementations 2362b12a0b9Smatthias.ringwald if (hci_stack.hci_transport->can_send_packet_now){ 2372b12a0b9Smatthias.ringwald if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 2382b12a0b9Smatthias.ringwald return 0; 2392b12a0b9Smatthias.ringwald } 2402b12a0b9Smatthias.ringwald } 2412b12a0b9Smatthias.ringwald 2422b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 243c24735b1Smatthias.ringwald switch (packet_type) { 244c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 245c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 246c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 247de009a8cSmatthias.ringwald return hci_stack.num_cmd_packets; 248c24735b1Smatthias.ringwald default: 249c24735b1Smatthias.ringwald return 0; 250c24735b1Smatthias.ringwald } 251c24735b1Smatthias.ringwald } 252c24735b1Smatthias.ringwald 253ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2547856c818Smatthias.ringwald 2556218e6f1Smatthias.ringwald // check for free places on BT module 2565932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2576218e6f1Smatthias.ringwald 2587856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2595061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 26056cf178bSmatthias.ringwald if (!connection) return 0; 26156cf178bSmatthias.ringwald hci_connection_timestamp(connection); 26256cf178bSmatthias.ringwald 26356cf178bSmatthias.ringwald // count packet 26456cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2657b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2667856c818Smatthias.ringwald 26700d8e42eSmatthias.ringwald // send packet 26800d8e42eSmatthias.ringwald int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2696218e6f1Smatthias.ringwald 27000d8e42eSmatthias.ringwald return err; 271ee091cf1Smatthias.ringwald } 272ee091cf1Smatthias.ringwald 27316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2747856c818Smatthias.ringwald 275e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 276e76a89eeS[email protected] 2777856c818Smatthias.ringwald // get info 2787856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2795061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 2807856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2817856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2827856c818Smatthias.ringwald 2837856c818Smatthias.ringwald // ignore non-registered handle 2847856c818Smatthias.ringwald if (!conn){ 2857d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2867856c818Smatthias.ringwald return; 2877856c818Smatthias.ringwald } 2887856c818Smatthias.ringwald 289e76a89eeS[email protected] // assert packet is complete 2909ecc3e17S[email protected] if (acl_length + 4 != size){ 291e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 292e76a89eeS[email protected] return; 293e76a89eeS[email protected] } 294e76a89eeS[email protected] 2957856c818Smatthias.ringwald // update idle timestamp 2967856c818Smatthias.ringwald hci_connection_timestamp(conn); 2977856c818Smatthias.ringwald 2987856c818Smatthias.ringwald // handle different packet types 2997856c818Smatthias.ringwald switch (acl_flags & 0x03) { 3007856c818Smatthias.ringwald 3017856c818Smatthias.ringwald case 0x01: // continuation fragment 3027856c818Smatthias.ringwald 3037856c818Smatthias.ringwald // sanity check 3047856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 3057d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 3067856c818Smatthias.ringwald return; 3077856c818Smatthias.ringwald } 3087856c818Smatthias.ringwald 3097856c818Smatthias.ringwald // append fragment payload (header already stored) 3107856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 3117856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 3127856c818Smatthias.ringwald 3137d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 314decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3157856c818Smatthias.ringwald 3167856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3177856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3187856c818Smatthias.ringwald 3192718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3207856c818Smatthias.ringwald // reset recombination buffer 3217856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3227856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3237856c818Smatthias.ringwald } 3247856c818Smatthias.ringwald break; 3257856c818Smatthias.ringwald 3267856c818Smatthias.ringwald case 0x02: { // first fragment 3277856c818Smatthias.ringwald 3287856c818Smatthias.ringwald // sanity check 3297856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3307d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3317856c818Smatthias.ringwald return; 3327856c818Smatthias.ringwald } 3337856c818Smatthias.ringwald 3347856c818Smatthias.ringwald // peek into L2CAP packet! 3357856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3367856c818Smatthias.ringwald 337e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 338decc01a8Smatthias.ringwald 3397856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3407856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3417856c818Smatthias.ringwald 3427856c818Smatthias.ringwald // forward fragment as L2CAP packet 3432718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3447856c818Smatthias.ringwald 3457856c818Smatthias.ringwald } else { 3467856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3477856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3487856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3497856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 350decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3517856c818Smatthias.ringwald } 3527856c818Smatthias.ringwald break; 3537856c818Smatthias.ringwald 3547856c818Smatthias.ringwald } 3557856c818Smatthias.ringwald default: 3567d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3577856c818Smatthias.ringwald return; 3587856c818Smatthias.ringwald } 35994ab26f8Smatthias.ringwald 36094ab26f8Smatthias.ringwald // execute main loop 36194ab26f8Smatthias.ringwald hci_run(); 36216833f0aSmatthias.ringwald } 36322909952Smatthias.ringwald 36467a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 365339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3663c4d4b90Smatthias.ringwald 3673c4d4b90Smatthias.ringwald // cancel all l2cap connections 3683c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3693c4d4b90Smatthias.ringwald 370c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 371c785ef68Smatthias.ringwald 372c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 373a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3743c4d4b90Smatthias.ringwald 3753c4d4b90Smatthias.ringwald // now it's gone 376c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 377c7e0c5f6Smatthias.ringwald } 378c7e0c5f6Smatthias.ringwald 3790c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3808f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3818f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3828f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3838f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3848f8108aaSmatthias.ringwald }; 38565389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 38665389bfcS[email protected] 0, // 3 slot packets 38765389bfcS[email protected] 1, // 5 slot packets 38865389bfcS[email protected] 25, // EDR 2 mpbs 38965389bfcS[email protected] 26, // EDR 3 mbps 39065389bfcS[email protected] 39, // 3 slot EDR packts 39165389bfcS[email protected] 40, // 5 slot EDR packet 39265389bfcS[email protected] }; 39365389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 39465389bfcS[email protected] 0x0f00, // 3 slot packets 39565389bfcS[email protected] 0xf000, // 5 slot packets 39665389bfcS[email protected] 0x1102, // EDR 2 mpbs 39765389bfcS[email protected] 0x2204, // EDR 3 mbps 39865389bfcS[email protected] 0x0300, // 3 slot EDR packts 39965389bfcS[email protected] 0x3000, // 5 slot EDR packet 40065389bfcS[email protected] }; 4018f8108aaSmatthias.ringwald 40265389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 40365389bfcS[email protected] // enable packet types based on size 4048f8108aaSmatthias.ringwald uint16_t packet_types = 0; 4058f8108aaSmatthias.ringwald int i; 4068f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 4078f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 4088f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 4098f8108aaSmatthias.ringwald packet_types |= 1 << i; 4108f8108aaSmatthias.ringwald } 4118f8108aaSmatthias.ringwald } 41265389bfcS[email protected] // disable packet types due to missing local supported features 41365389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 41465389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 41565389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 41665389bfcS[email protected] if (feature_set) continue; 41765389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 41865389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 41965389bfcS[email protected] } 4208f8108aaSmatthias.ringwald // flip bits for "may not be used" 4218f8108aaSmatthias.ringwald packet_types ^= 0x3306; 4228f8108aaSmatthias.ringwald return packet_types; 4238f8108aaSmatthias.ringwald } 4248f8108aaSmatthias.ringwald 4258f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 4268f8108aaSmatthias.ringwald return hci_stack.packet_types; 4278f8108aaSmatthias.ringwald } 4288f8108aaSmatthias.ringwald 4297dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 4307dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 4317dc17943Smatthias.ringwald return hci_stack.hci_packet_buffer; 4327dc17943Smatthias.ringwald } 4337dc17943Smatthias.ringwald 434f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 4357dc17943Smatthias.ringwald return hci_stack.acl_data_packet_length; 4367dc17943Smatthias.ringwald } 4377dc17943Smatthias.ringwald 438f5d8d141S[email protected] int hci_ssp_supported(void){ 439f5d8d141S[email protected] // No 51, byte 6, bit 3 440f5d8d141S[email protected] return (hci_stack.local_supported_features[6] & (1 << 3)) != 0; 441f5d8d141S[email protected] } 442f5d8d141S[email protected] 443f5d8d141S[email protected] int hci_classic_supported(void){ 444f5d8d141S[email protected] // No 37, byte 4, bit 5, = No BR/EDR Support 445f5d8d141S[email protected] return (hci_stack.local_supported_features[4] & (1 << 5)) == 0; 446f5d8d141S[email protected] } 447f5d8d141S[email protected] 448f5d8d141S[email protected] int hci_le_supported(void){ 449f5d8d141S[email protected] // No 37, byte 4, bit 6 = LE Supported (Controller) 450f5d8d141S[email protected] #ifdef HAVE_BLE 451f5d8d141S[email protected] return (hci_stack.local_supported_features[4] & (1 << 6)) != 0; 452f5d8d141S[email protected] #else 453f5d8d141S[email protected] return 0; 454f5d8d141S[email protected] #endif 455f5d8d141S[email protected] } 456f5d8d141S[email protected] 457*69a97523S[email protected] // get addr type and address used in advertisement packets 458*69a97523S[email protected] void hci_le_advertisement_address(int * addr_type, bd_addr_t * addr){ 459*69a97523S[email protected] *addr_type = hci_stack.adv_addr_type; 460*69a97523S[email protected] if (hci_stack.adv_addr_type){ 461*69a97523S[email protected] memcpy(addr, hci_stack.adv_address, 6); 462*69a97523S[email protected] } else { 463*69a97523S[email protected] memcpy(addr, hci_stack.local_bd_addr, 6); 464*69a97523S[email protected] } 465*69a97523S[email protected] } 466*69a97523S[email protected] 46774ec757aSmatthias.ringwald // avoid huge local variables 468223aafc1Smatthias.ringwald #ifndef EMBEDDED 46974ec757aSmatthias.ringwald static device_name_t device_name; 470223aafc1Smatthias.ringwald #endif 47116833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 472e76a89eeS[email protected] 473e76a89eeS[email protected] uint16_t event_length = packet[1]; 474e76a89eeS[email protected] 475e76a89eeS[email protected] // assert packet is complete 476e76a89eeS[email protected] if (size != event_length + 2){ 477e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 478e76a89eeS[email protected] return; 479e76a89eeS[email protected] } 480e76a89eeS[email protected] 4811281a47eSmatthias.ringwald bd_addr_t addr; 4822d00edd4Smatthias.ringwald uint8_t link_type; 483fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 4841f7b95a1Smatthias.ringwald hci_connection_t * conn; 48556cf178bSmatthias.ringwald int i; 48622909952Smatthias.ringwald 4875909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 4885909f7f2Smatthias.ringwald 4896772a24cSmatthias.ringwald switch (packet[0]) { 49022909952Smatthias.ringwald 4916772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4927ec5eeaaSmatthias.ringwald // get num cmd packets 4937b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4947ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4957ec5eeaaSmatthias.ringwald 496e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 497e2edc0c3Smatthias.ringwald // from offset 5 498e2edc0c3Smatthias.ringwald // status 4991d279b20Smatthias.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" 500e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 50156cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 502e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 50356cf178bSmatthias.ringwald // ignore: total num SCO packets 50456cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 505a7a04bd9Smatthias.ringwald // determine usable ACL payload size 5068fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 5078fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 5088f8108aaSmatthias.ringwald } 50965389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 51065389bfcS[email protected] hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets); 511e2edc0c3Smatthias.ringwald } 51256cf178bSmatthias.ringwald } 51365a46ef3S[email protected] #ifdef HAVE_BLE 51465a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 51565a46ef3S[email protected] hci_stack.le_data_packet_length = READ_BT_16(packet, 6); 51665a46ef3S[email protected] hci_stack.total_num_le_packets = packet[8]; 51765a46ef3S[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); 51865a46ef3S[email protected] } 51965a46ef3S[email protected] #endif 520188981d2Smatthias.ringwald // Dump local address 521188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 522e2386ba1S[email protected] bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 523188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 524e2386ba1S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr)); 525188981d2Smatthias.ringwald } 526381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 527381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 528381fbed8Smatthias.ringwald } 52965389bfcS[email protected] // Note: HCI init checks 530559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 53165389bfcS[email protected] memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 532559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 533559e517eS[email protected] hci_stack.local_supported_features[0], hci_stack.local_supported_features[1], 534559e517eS[email protected] hci_stack.local_supported_features[2], hci_stack.local_supported_features[3], 535559e517eS[email protected] hci_stack.local_supported_features[4], hci_stack.local_supported_features[5], 536559e517eS[email protected] hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]); 53765389bfcS[email protected] 53865389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 53965389bfcS[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]); 540f5d8d141S[email protected] log_info("packet types %04x", hci_stack.packet_types); 541f5d8d141S[email protected] 542f5d8d141S[email protected] // Classic/LE 543f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 544559e517eS[email protected] } 54556cf178bSmatthias.ringwald break; 54656cf178bSmatthias.ringwald 5477ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 5487ec5eeaaSmatthias.ringwald // get num cmd packets 5497b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 5507ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 5517ec5eeaaSmatthias.ringwald break; 5527ec5eeaaSmatthias.ringwald 55356cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 55456cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 55556cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 55656cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 5575061f3afS[email protected] conn = hci_connection_for_handle(handle); 55856cf178bSmatthias.ringwald if (!conn){ 5597d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 56056cf178bSmatthias.ringwald continue; 56156cf178bSmatthias.ringwald } 56256cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 5637b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 56456cf178bSmatthias.ringwald } 5656772a24cSmatthias.ringwald break; 5666772a24cSmatthias.ringwald 5671f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 56837eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 56937eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 5702d00edd4Smatthias.ringwald link_type = packet[11]; 571339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 57237eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 5731f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 5741f7b95a1Smatthias.ringwald if (!conn) { 5751f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 5761f7b95a1Smatthias.ringwald } 577ce4c8fabSmatthias.ringwald if (!conn) { 578ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 579ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 580ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 581ce4c8fabSmatthias.ringwald break; 582ce4c8fabSmatthias.ringwald } 58332ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 58432ab9390Smatthias.ringwald hci_run(); 58537eaa4cfSmatthias.ringwald } else { 586ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 587ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 588ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 58937eaa4cfSmatthias.ringwald } 5901f7b95a1Smatthias.ringwald break; 5911f7b95a1Smatthias.ringwald 5926772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 593fe1ed1b8Smatthias.ringwald // Connection management 594fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 595339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 5961f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 597fe1ed1b8Smatthias.ringwald if (conn) { 598b448a0e7Smatthias.ringwald if (!packet[2]){ 599c8e4258aSmatthias.ringwald conn->state = OPEN; 600fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 601ee091cf1Smatthias.ringwald 602c785ef68Smatthias.ringwald // restart timer 603c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 604ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 605c785ef68Smatthias.ringwald 606339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 60743bfb1bdSmatthias.ringwald 60843bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 609b448a0e7Smatthias.ringwald } else { 610b448a0e7Smatthias.ringwald // connection failed, remove entry 611b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 612a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 613c12e46e7Smatthias.ringwald 614c12e46e7Smatthias.ringwald // if authentication error, also delete link key 615c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 616c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 617c12e46e7Smatthias.ringwald } 618fe1ed1b8Smatthias.ringwald } 619fe1ed1b8Smatthias.ringwald } 6206772a24cSmatthias.ringwald break; 621fe1ed1b8Smatthias.ringwald 6227fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 6237b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 6247fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 62574ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 62632ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 62764472d52Smatthias.ringwald hci_run(); 628d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 62929d53098Smatthias.ringwald return; 6307fde4af9Smatthias.ringwald 6317fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 6327fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 63374ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 63429d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 635287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 63629d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 6377fde4af9Smatthias.ringwald break; 6387fde4af9Smatthias.ringwald 6397fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 6407fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 641d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 642d9a327f9Smatthias.ringwald if (!hci_stack.remote_device_db) break; 643d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 644d9a327f9Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 6457fde4af9Smatthias.ringwald break; 6467fde4af9Smatthias.ringwald 6471d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 6481d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 6491d6b20aeS[email protected] if (hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break; 650dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 651dbe1a790S[email protected] break; 652dbe1a790S[email protected] 653dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 654dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST); 655dbe1a790S[email protected] if (!hci_stack.ssp_auto_accept) break; 656dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 657dbe1a790S[email protected] break; 658dbe1a790S[email protected] 659dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 660dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST); 661dbe1a790S[email protected] if (!hci_stack.ssp_auto_accept) break; 662dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 6631d6b20aeS[email protected] break; 6641d6b20aeS[email protected] 665223aafc1Smatthias.ringwald #ifndef EMBEDDED 66674ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 66774ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 66874ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 66974ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 6705a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 6715a06394aSmatthias.ringwald for (i=0; i<248;i++){ 6725a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 6735a06394aSmatthias.ringwald packet[9+i] = 0; 6745a06394aSmatthias.ringwald break; 675cdc9101dSmatthias.ringwald } 6765a06394aSmatthias.ringwald } 677d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 67874ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 67974ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 68074ec757aSmatthias.ringwald break; 68174ec757aSmatthias.ringwald 68274ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 68374ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 68474ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 68574ec757aSmatthias.ringwald // first send inq result packet 68674ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 68774ec757aSmatthias.ringwald // then send cached remote names 68874ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 68974ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 69074ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 69174ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 69274ec757aSmatthias.ringwald } 69374ec757aSmatthias.ringwald } 69474ec757aSmatthias.ringwald return; 695223aafc1Smatthias.ringwald #endif 69674ec757aSmatthias.ringwald 6976772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 698fe1ed1b8Smatthias.ringwald if (!packet[2]){ 699fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 7005061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 701fe1ed1b8Smatthias.ringwald if (conn) { 702c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 703fe1ed1b8Smatthias.ringwald } 704fe1ed1b8Smatthias.ringwald } 7056772a24cSmatthias.ringwald break; 7066772a24cSmatthias.ringwald 707c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 7086c062428S[email protected] if(hci_stack.control && hci_stack.control->hw_error){ 709c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 710c68bdf90Smatthias.ringwald } 711c68bdf90Smatthias.ringwald break; 712c68bdf90Smatthias.ringwald 7135909f7f2Smatthias.ringwald #ifdef HAVE_BLE 7145909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 7155909f7f2Smatthias.ringwald switch (packet[2]) { 7165909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 7175909f7f2Smatthias.ringwald // Connection management 7185909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 7195909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 7205909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 7215909f7f2Smatthias.ringwald conn = connection_for_address(addr); 7225909f7f2Smatthias.ringwald if (packet[3]){ 7235909f7f2Smatthias.ringwald if (conn){ 7245909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 7255909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 7265909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 7275909f7f2Smatthias.ringwald 7285909f7f2Smatthias.ringwald } 7295909f7f2Smatthias.ringwald // if authentication error, also delete link key 7305909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 7315909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 7325909f7f2Smatthias.ringwald } 7335909f7f2Smatthias.ringwald break; 7345909f7f2Smatthias.ringwald } 7355909f7f2Smatthias.ringwald if (!conn){ 7365909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 7375909f7f2Smatthias.ringwald } 7385909f7f2Smatthias.ringwald if (!conn){ 7395909f7f2Smatthias.ringwald // no memory 7405909f7f2Smatthias.ringwald break; 7415909f7f2Smatthias.ringwald } 7425909f7f2Smatthias.ringwald 7435909f7f2Smatthias.ringwald conn->state = OPEN; 7445909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 7455909f7f2Smatthias.ringwald 7465909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 7475909f7f2Smatthias.ringwald 7485909f7f2Smatthias.ringwald // restart timer 7495909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 7505909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 7515909f7f2Smatthias.ringwald 7525909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 7535909f7f2Smatthias.ringwald 7545909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 7555909f7f2Smatthias.ringwald break; 7565909f7f2Smatthias.ringwald 75765a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 75865a46ef3S[email protected] 7595909f7f2Smatthias.ringwald default: 7605909f7f2Smatthias.ringwald break; 7615909f7f2Smatthias.ringwald } 7625909f7f2Smatthias.ringwald break; 7635909f7f2Smatthias.ringwald #endif 7645909f7f2Smatthias.ringwald 7656772a24cSmatthias.ringwald default: 7666772a24cSmatthias.ringwald break; 767fe1ed1b8Smatthias.ringwald } 768fe1ed1b8Smatthias.ringwald 7693429f56bSmatthias.ringwald // handle BT initialization 7703429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 7713429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 7723429f56bSmatthias.ringwald // odd: waiting for event 773f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 774c9af4d3fS[email protected] // wait for explicit COMMAND COMPLETE on RESET 775c9af4d3fS[email protected] if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) { 7763429f56bSmatthias.ringwald hci_stack.substate++; 7773429f56bSmatthias.ringwald } 7783429f56bSmatthias.ringwald } 77922909952Smatthias.ringwald } 780c9af4d3fS[email protected] } 78122909952Smatthias.ringwald 78289db417bSmatthias.ringwald // help with BT sleep 78389db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 78489db417bSmatthias.ringwald && hci_stack.substate == 1 78589db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 78689db417bSmatthias.ringwald hci_stack.substate++; 78789db417bSmatthias.ringwald } 78889db417bSmatthias.ringwald 7892718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 79094ab26f8Smatthias.ringwald 79194ab26f8Smatthias.ringwald // execute main loop 79294ab26f8Smatthias.ringwald hci_run(); 79316833f0aSmatthias.ringwald } 79416833f0aSmatthias.ringwald 7950a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 79610e830c9Smatthias.ringwald switch (packet_type) { 79710e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 79810e830c9Smatthias.ringwald event_handler(packet, size); 79910e830c9Smatthias.ringwald break; 80010e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 80110e830c9Smatthias.ringwald acl_handler(packet, size); 80210e830c9Smatthias.ringwald break; 80310e830c9Smatthias.ringwald default: 80410e830c9Smatthias.ringwald break; 80510e830c9Smatthias.ringwald } 80610e830c9Smatthias.ringwald } 80710e830c9Smatthias.ringwald 808fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 8092718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 8102718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 81116833f0aSmatthias.ringwald } 81216833f0aSmatthias.ringwald 8134f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 814475c8125Smatthias.ringwald 815475c8125Smatthias.ringwald // reference to use transport layer implementation 81616833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 817475c8125Smatthias.ringwald 81811e23e5fSmatthias.ringwald // references to used control implementation 81911e23e5fSmatthias.ringwald hci_stack.control = control; 82011e23e5fSmatthias.ringwald 82111e23e5fSmatthias.ringwald // reference to used config 82211e23e5fSmatthias.ringwald hci_stack.config = config; 82311e23e5fSmatthias.ringwald 824fe1ed1b8Smatthias.ringwald // no connections yet 825fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 8260a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 827c0e866bfSmatthias.ringwald hci_stack.connectable = 0; 828758b46ceSmatthias.ringwald 829b031bebbSmatthias.ringwald // no pending cmds 830b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 831b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 832b031bebbSmatthias.ringwald 83316833f0aSmatthias.ringwald // higher level handler 8342718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 83516833f0aSmatthias.ringwald 836404843c1Smatthias.ringwald // store and open remote device db 837404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 838404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 839404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 840404843c1Smatthias.ringwald } 84129d53098Smatthias.ringwald 8428fcba05dSmatthias.ringwald // max acl payload size defined in config.h 8438fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 8448fcba05dSmatthias.ringwald 84516833f0aSmatthias.ringwald // register packet handlers with transport 84610e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 847f5454fc6Smatthias.ringwald 848f5454fc6Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 849e2386ba1S[email protected] 850e2386ba1S[email protected] // class of device 851e2386ba1S[email protected] hci_stack.class_of_device = 0x007a020c; // Smartphone 852a45d6b9fS[email protected] 853ae5d8360S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, auto accept 854ae5d8360S[email protected] hci_stack.ssp_enable = 1; 855ae5d8360S[email protected] hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 856a45d6b9fS[email protected] hci_stack.ssp_authentication_requirement = 0; 857ae5d8360S[email protected] hci_stack.ssp_auto_accept = 1; 858*69a97523S[email protected] 859*69a97523S[email protected] // LE 860*69a97523S[email protected] hci_stack.adv_addr_type = 0; 861*69a97523S[email protected] memset(hci_stack.adv_address, 0, 6); 862475c8125Smatthias.ringwald } 863475c8125Smatthias.ringwald 864404843c1Smatthias.ringwald void hci_close(){ 865404843c1Smatthias.ringwald // close remote device db 866404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 867404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 868404843c1Smatthias.ringwald } 869f5454fc6Smatthias.ringwald while (hci_stack.connections) { 870f5454fc6Smatthias.ringwald hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 871f5454fc6Smatthias.ringwald } 872f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 873404843c1Smatthias.ringwald } 874404843c1Smatthias.ringwald 8758d213e1aSmatthias.ringwald // State-Module-Driver overview 8768d213e1aSmatthias.ringwald // state module low-level 8778d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 8788d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 8798d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 8808d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 881d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 882d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 883c7e0c5f6Smatthias.ringwald 88440d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 8857301ad89Smatthias.ringwald 886038bc64cSmatthias.ringwald // power on 887f9a30166Smatthias.ringwald int err = 0; 888f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 889f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 890f9a30166Smatthias.ringwald } 891038bc64cSmatthias.ringwald if (err){ 8927d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 893038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 894038bc64cSmatthias.ringwald return err; 895038bc64cSmatthias.ringwald } 896038bc64cSmatthias.ringwald 897038bc64cSmatthias.ringwald // open low-level device 898038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 899038bc64cSmatthias.ringwald if (err){ 9007d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 901f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 902f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 903f9a30166Smatthias.ringwald } 904038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 905038bc64cSmatthias.ringwald return err; 906038bc64cSmatthias.ringwald } 9078d213e1aSmatthias.ringwald return 0; 9088d213e1aSmatthias.ringwald } 909038bc64cSmatthias.ringwald 91040d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 9118d213e1aSmatthias.ringwald 9127b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 9139418f9c9Smatthias.ringwald 9148d213e1aSmatthias.ringwald // close low-level device 9158d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 9168d213e1aSmatthias.ringwald 9177b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 9189418f9c9Smatthias.ringwald 9198d213e1aSmatthias.ringwald // power off 9208d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 9218d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 9228d213e1aSmatthias.ringwald } 9239418f9c9Smatthias.ringwald 9247b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 9259418f9c9Smatthias.ringwald 92672ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 92772ea5239Smatthias.ringwald } 92872ea5239Smatthias.ringwald 92940d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 93072ea5239Smatthias.ringwald 9317b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 9323144bce4Smatthias.ringwald 933b429b9b7Smatthias.ringwald #if 0 934b429b9b7Smatthias.ringwald // don't close serial port during sleep 935b429b9b7Smatthias.ringwald 93672ea5239Smatthias.ringwald // close low-level device 93772ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 938b429b9b7Smatthias.ringwald #endif 93972ea5239Smatthias.ringwald 94072ea5239Smatthias.ringwald // sleep mode 9413144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 94272ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 94372ea5239Smatthias.ringwald } 944b429b9b7Smatthias.ringwald 94572ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 9468d213e1aSmatthias.ringwald } 9478d213e1aSmatthias.ringwald 94840d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 949b429b9b7Smatthias.ringwald 9507b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 951b429b9b7Smatthias.ringwald 952b429b9b7Smatthias.ringwald // wake on 953b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 954b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 955b429b9b7Smatthias.ringwald } 956b429b9b7Smatthias.ringwald 957b429b9b7Smatthias.ringwald #if 0 958b429b9b7Smatthias.ringwald // open low-level device 959b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 960b429b9b7Smatthias.ringwald if (err){ 9617d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 962b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 963b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 964b429b9b7Smatthias.ringwald } 965b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 966b429b9b7Smatthias.ringwald return err; 967b429b9b7Smatthias.ringwald } 968b429b9b7Smatthias.ringwald #endif 969b429b9b7Smatthias.ringwald 970b429b9b7Smatthias.ringwald return 0; 971b429b9b7Smatthias.ringwald } 972b429b9b7Smatthias.ringwald 973b429b9b7Smatthias.ringwald 9748d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 975d661ed19Smatthias.ringwald 9767b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 977d661ed19Smatthias.ringwald 9788d213e1aSmatthias.ringwald int err = 0; 9798d213e1aSmatthias.ringwald switch (hci_stack.state){ 9808d213e1aSmatthias.ringwald 9818d213e1aSmatthias.ringwald case HCI_STATE_OFF: 9828d213e1aSmatthias.ringwald switch (power_mode){ 9838d213e1aSmatthias.ringwald case HCI_POWER_ON: 9848d213e1aSmatthias.ringwald err = hci_power_control_on(); 9858d213e1aSmatthias.ringwald if (err) return err; 9867301ad89Smatthias.ringwald // set up state machine 9877301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 9887301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9897301ad89Smatthias.ringwald hci_stack.substate = 0; 9908d213e1aSmatthias.ringwald break; 9918d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9928d213e1aSmatthias.ringwald // do nothing 9938d213e1aSmatthias.ringwald break; 9948d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 995b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 9968d213e1aSmatthias.ringwald break; 9978d213e1aSmatthias.ringwald } 9988d213e1aSmatthias.ringwald break; 9997301ad89Smatthias.ringwald 10008d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 10018d213e1aSmatthias.ringwald switch (power_mode){ 10028d213e1aSmatthias.ringwald case HCI_POWER_ON: 10038d213e1aSmatthias.ringwald // do nothing 10048d213e1aSmatthias.ringwald break; 10058d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10068d213e1aSmatthias.ringwald // no connections yet, just turn it off 10078d213e1aSmatthias.ringwald hci_power_control_off(); 10088d213e1aSmatthias.ringwald break; 10098d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1010b546ac54Smatthias.ringwald // no connections yet, just turn it off 101172ea5239Smatthias.ringwald hci_power_control_sleep(); 10128d213e1aSmatthias.ringwald break; 10138d213e1aSmatthias.ringwald } 10148d213e1aSmatthias.ringwald break; 10157301ad89Smatthias.ringwald 10168d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 10178d213e1aSmatthias.ringwald switch (power_mode){ 10188d213e1aSmatthias.ringwald case HCI_POWER_ON: 10198d213e1aSmatthias.ringwald // do nothing 10208d213e1aSmatthias.ringwald break; 10218d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1022c7e0c5f6Smatthias.ringwald // see hci_run 1023c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 10248d213e1aSmatthias.ringwald break; 10258d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1026b546ac54Smatthias.ringwald // see hci_run 1027b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 102889db417bSmatthias.ringwald hci_stack.substate = 0; 10298d213e1aSmatthias.ringwald break; 10308d213e1aSmatthias.ringwald } 10318d213e1aSmatthias.ringwald break; 10327301ad89Smatthias.ringwald 10338d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 10348d213e1aSmatthias.ringwald switch (power_mode){ 10358d213e1aSmatthias.ringwald case HCI_POWER_ON: 10368d213e1aSmatthias.ringwald // set up state machine 10378d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 10388d213e1aSmatthias.ringwald hci_stack.substate = 0; 10398d213e1aSmatthias.ringwald break; 10408d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10418d213e1aSmatthias.ringwald // do nothing 10428d213e1aSmatthias.ringwald break; 10438d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1044b546ac54Smatthias.ringwald // see hci_run 1045b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 104689db417bSmatthias.ringwald hci_stack.substate = 0; 10478d213e1aSmatthias.ringwald break; 10488d213e1aSmatthias.ringwald } 10498d213e1aSmatthias.ringwald break; 10508d213e1aSmatthias.ringwald 10518d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 10528d213e1aSmatthias.ringwald switch (power_mode){ 10538d213e1aSmatthias.ringwald case HCI_POWER_ON: 105428171530Smatthias.ringwald 105528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 105628171530Smatthias.ringwald // nothing to do, if H4 supports power management 105728171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 10588747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1059da5275c5S[email protected] hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 106028171530Smatthias.ringwald break; 106128171530Smatthias.ringwald } 106228171530Smatthias.ringwald #endif 1063b546ac54Smatthias.ringwald // set up state machine 1064b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 1065b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1066b546ac54Smatthias.ringwald hci_stack.substate = 0; 10678d213e1aSmatthias.ringwald break; 10688d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1069b546ac54Smatthias.ringwald // see hci_run 1070b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 10718d213e1aSmatthias.ringwald break; 10728d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1073b546ac54Smatthias.ringwald // do nothing 10748d213e1aSmatthias.ringwald break; 10758d213e1aSmatthias.ringwald } 10768d213e1aSmatthias.ringwald break; 10778d213e1aSmatthias.ringwald 10788d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 10798d213e1aSmatthias.ringwald switch (power_mode){ 10808d213e1aSmatthias.ringwald case HCI_POWER_ON: 108128171530Smatthias.ringwald 108228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 108328171530Smatthias.ringwald // nothing to do, if H4 supports power management 108428171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 10858747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1086da5275c5S[email protected] hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1087758b46ceSmatthias.ringwald hci_update_scan_enable(); 108828171530Smatthias.ringwald break; 108928171530Smatthias.ringwald } 109028171530Smatthias.ringwald #endif 10913144bce4Smatthias.ringwald err = hci_power_control_wake(); 10923144bce4Smatthias.ringwald if (err) return err; 1093b546ac54Smatthias.ringwald // set up state machine 1094b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 1095b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1096b546ac54Smatthias.ringwald hci_stack.substate = 0; 10978d213e1aSmatthias.ringwald break; 10988d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10994ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 11008d213e1aSmatthias.ringwald break; 11018d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1102b546ac54Smatthias.ringwald // do nothing 11038d213e1aSmatthias.ringwald break; 11048d213e1aSmatthias.ringwald } 11058d213e1aSmatthias.ringwald break; 110611e23e5fSmatthias.ringwald } 110768d92d03Smatthias.ringwald 1108038bc64cSmatthias.ringwald // create internal event 1109ee8bf225Smatthias.ringwald hci_emit_state(); 1110ee8bf225Smatthias.ringwald 111168d92d03Smatthias.ringwald // trigger next/first action 111268d92d03Smatthias.ringwald hci_run(); 111368d92d03Smatthias.ringwald 1114475c8125Smatthias.ringwald return 0; 1115475c8125Smatthias.ringwald } 1116475c8125Smatthias.ringwald 1117758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1118758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 1119758b46ceSmatthias.ringwald hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 1120758b46ceSmatthias.ringwald hci_run(); 1121758b46ceSmatthias.ringwald } 1122758b46ceSmatthias.ringwald 1123381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1124381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1125381fbed8Smatthias.ringwald 1126381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 1127381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 1128381fbed8Smatthias.ringwald return; 1129381fbed8Smatthias.ringwald } 1130381fbed8Smatthias.ringwald 1131381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 1132758b46ceSmatthias.ringwald hci_update_scan_enable(); 1133758b46ceSmatthias.ringwald } 1134b031bebbSmatthias.ringwald 1135758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1136758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1137758b46ceSmatthias.ringwald 1138758b46ceSmatthias.ringwald // don't emit event 1139758b46ceSmatthias.ringwald if (hci_stack.connectable == enable) return; 1140758b46ceSmatthias.ringwald 1141758b46ceSmatthias.ringwald hci_stack.connectable = enable; 1142758b46ceSmatthias.ringwald hci_update_scan_enable(); 1143381fbed8Smatthias.ringwald } 1144381fbed8Smatthias.ringwald 11455061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 11465061f3afS[email protected] return &hci_stack.local_bd_addr; 11475061f3afS[email protected] } 11485061f3afS[email protected] 114906b35ec0Smatthias.ringwald void hci_run(){ 11508a485f27Smatthias.ringwald 115132ab9390Smatthias.ringwald hci_connection_t * connection; 115232ab9390Smatthias.ringwald linked_item_t * it; 115332ab9390Smatthias.ringwald 1154ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1155ce4c8fabSmatthias.ringwald 1156b031bebbSmatthias.ringwald // global/non-connection oriented commands 1157b031bebbSmatthias.ringwald 1158b031bebbSmatthias.ringwald // decline incoming connections 1159ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 1160ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 1161ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 1162ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1163dbe1a790S[email protected] return; 1164ce4c8fabSmatthias.ringwald } 1165ce4c8fabSmatthias.ringwald 1166b031bebbSmatthias.ringwald // send scan enable 116792368cd3S[email protected] if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){ 1168b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1169b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 1170dbe1a790S[email protected] return; 1171b031bebbSmatthias.ringwald } 1172b031bebbSmatthias.ringwald 117332ab9390Smatthias.ringwald // send pending HCI commands 117432ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 117532ab9390Smatthias.ringwald 1176b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 117732ab9390Smatthias.ringwald 117832ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 11797b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 118032ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 118132ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1182dbe1a790S[email protected] return; 1183c7e0c5f6Smatthias.ringwald } 1184c7e0c5f6Smatthias.ringwald 118532ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 118632ab9390Smatthias.ringwald link_key_t link_key; 11877b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 118832ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 118932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 119032ab9390Smatthias.ringwald } else { 119132ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 119232ab9390Smatthias.ringwald } 119328ca2b46S[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 1194dbe1a790S[email protected] return; 119532ab9390Smatthias.ringwald } 11961d6b20aeS[email protected] 1197dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 1198dbe1a790S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement); 1199dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 1200dbe1a790S[email protected] return; 120132ab9390Smatthias.ringwald } 120232ab9390Smatthias.ringwald 1203dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1204dbe1a790S[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1205dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 1206dbe1a790S[email protected] return; 1207dbe1a790S[email protected] } 1208dbe1a790S[email protected] 1209dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1210dbe1a790S[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1211dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 1212dbe1a790S[email protected] return; 1213dbe1a790S[email protected] } 1214dbe1a790S[email protected] } 1215c7e0c5f6Smatthias.ringwald 12163429f56bSmatthias.ringwald switch (hci_stack.state){ 12173429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 12187b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 12193429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 12203429f56bSmatthias.ringwald // odd: waiting for command completion 122106b35ec0Smatthias.ringwald return; 12223429f56bSmatthias.ringwald } 122390919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1224c7492964Smatthias.ringwald case 0: // RESET 122522909952Smatthias.ringwald hci_send_cmd(&hci_reset); 122624052c2aS[email protected] 1227c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1228c7492964Smatthias.ringwald // skip baud change 1229c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1230c7492964Smatthias.ringwald } 12313429f56bSmatthias.ringwald break; 1232c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 12337dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 12347dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1235c7492964Smatthias.ringwald break; 1236c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1237c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1238c7492964Smatthias.ringwald hci_stack.substate += 2; 1239c7492964Smatthias.ringwald // break missing here for fall through 1240c7492964Smatthias.ringwald 1241c7492964Smatthias.ringwald case 3: 124224052c2aS[email protected] // Custom initialization 1243db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 12447dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1245d62aca9fSmatthias.ringwald if (valid_cmd){ 12467dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 12477dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1248c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 124990919203Smatthias.ringwald break; 125090919203Smatthias.ringwald } 1251d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 125290919203Smatthias.ringwald } 12532f6c30e1Smatthias.ringwald // otherwise continue 1254f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1255f432a6ddSmatthias.ringwald break; 1256c7492964Smatthias.ringwald case 4: 1257e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1258e2edc0c3Smatthias.ringwald break; 1259c7492964Smatthias.ringwald case 5: 126065389bfcS[email protected] hci_send_cmd(&hci_read_local_supported_features); 12613429f56bSmatthias.ringwald break; 1262c7492964Smatthias.ringwald case 6: 1263d7e0e3a8S[email protected] if (hci_le_supported()){ 1264d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 1265d7e0e3a8S[email protected] } else { 1266d7e0e3a8S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 1267d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 1268d7e0e3a8S[email protected] } 1269f5d8d141S[email protected] 1270f5d8d141S[email protected] // skip Classic init commands for LE only chipsets 127124052c2aS[email protected] if (!hci_classic_supported()){ 127224052c2aS[email protected] if (hci_le_supported()){ 1273f5d8d141S[email protected] hci_stack.substate = 11 << 1; // skip all classic command 127424052c2aS[email protected] } else { 127524052c2aS[email protected] log_error("Neither BR/EDR nor LE supported"); 1276d7e0e3a8S[email protected] hci_stack.substate = 13 << 1; // skip all 127724052c2aS[email protected] } 1278f5d8d141S[email protected] } 1279f5d8d141S[email protected] break; 1280f5d8d141S[email protected] case 7: 128124052c2aS[email protected] if (hci_ssp_supported()){ 1282f5d8d141S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable); 1283f5d8d141S[email protected] break; 128424052c2aS[email protected] } 128524052c2aS[email protected] hci_stack.substate += 2; 128624052c2aS[email protected] // break missing here for fall through 128724052c2aS[email protected] 1288f5d8d141S[email protected] case 8: 128965389bfcS[email protected] // ca. 15 sec 129065389bfcS[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 1291559e517eS[email protected] break; 1292f5d8d141S[email protected] case 9: 1293e2386ba1S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device); 1294e2386ba1S[email protected] break; 1295f5d8d141S[email protected] case 10: 1296e2386ba1S[email protected] if (hci_stack.local_name){ 1297e2386ba1S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack.local_name); 1298e2386ba1S[email protected] } else { 12998a485f27Smatthias.ringwald char hostname[30]; 1300e2386ba1S[email protected] #ifdef EMBEDDED 1301e2386ba1S[email protected] // BTstack-11:22:33:44:55:66 1302e2386ba1S[email protected] strcpy(hostname, "BTstack "); 1303e2386ba1S[email protected] strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr)); 1304e2386ba1S[email protected] printf("---> Name %s\n", hostname); 1305e2386ba1S[email protected] #else 1306e2386ba1S[email protected] // hostname for POSIX systems 13078a485f27Smatthias.ringwald gethostname(hostname, 30); 13088a485f27Smatthias.ringwald hostname[29] = '\0'; 1309e2386ba1S[email protected] #endif 13108a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 13118a485f27Smatthias.ringwald } 13123c4d4b90Smatthias.ringwald break; 1313e0dbca83S[email protected] case 11: 1314a45d6b9fS[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 131524052c2aS[email protected] if (!hci_le_supported()){ 131624052c2aS[email protected] // SKIP LE init for Classic only configuration 131724052c2aS[email protected] hci_stack.substate = 13 << 1; 131824052c2aS[email protected] } 1319a45d6b9fS[email protected] break; 132024052c2aS[email protected] 132143aa7007S[email protected] #ifdef HAVE_BLE 132224052c2aS[email protected] // LE INIT 1323a45d6b9fS[email protected] case 12: 132424052c2aS[email protected] hci_send_cmd(&hci_le_read_buffer_size); 132524052c2aS[email protected] break; 132624052c2aS[email protected] case 13: 132724052c2aS[email protected] // LE Supported Host = 1, Simultaneous Host = 0 132824052c2aS[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 132924052c2aS[email protected] break; 133043aa7007S[email protected] #endif 133124052c2aS[email protected] 133224052c2aS[email protected] // DONE 133324052c2aS[email protected] case 14: 13343429f56bSmatthias.ringwald // done. 13353429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1336b360b6adSmatthias.ringwald hci_emit_state(); 13373429f56bSmatthias.ringwald break; 13383429f56bSmatthias.ringwald default: 13393429f56bSmatthias.ringwald break; 1340475c8125Smatthias.ringwald } 13413429f56bSmatthias.ringwald hci_stack.substate++; 13423429f56bSmatthias.ringwald break; 1343c7e0c5f6Smatthias.ringwald 1344c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1345c7e0c5f6Smatthias.ringwald 13467b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1347c7e0c5f6Smatthias.ringwald // close all open connections 1348c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1349c7e0c5f6Smatthias.ringwald if (connection){ 135032ab9390Smatthias.ringwald 1351c7e0c5f6Smatthias.ringwald // send disconnect 135232ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 135332ab9390Smatthias.ringwald 135479bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 13556ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1356c7e0c5f6Smatthias.ringwald 1357c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1358c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1359c7e0c5f6Smatthias.ringwald return; 1360c7e0c5f6Smatthias.ringwald } 13617b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1362c7e0c5f6Smatthias.ringwald 136372ea5239Smatthias.ringwald // switch mode 1364c7e0c5f6Smatthias.ringwald hci_power_control_off(); 13659418f9c9Smatthias.ringwald 13667b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 136772ea5239Smatthias.ringwald hci_emit_state(); 13687b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 136972ea5239Smatthias.ringwald break; 1370c7e0c5f6Smatthias.ringwald 137172ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 137289db417bSmatthias.ringwald switch(hci_stack.substate) { 137389db417bSmatthias.ringwald case 0: 13747b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 137572ea5239Smatthias.ringwald // close all open connections 137672ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 137766da7044Smatthias.ringwald 137828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 137966da7044Smatthias.ringwald // don't close connections, if H4 supports power management 138066da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 138166da7044Smatthias.ringwald connection = NULL; 138266da7044Smatthias.ringwald } 138366da7044Smatthias.ringwald #endif 138472ea5239Smatthias.ringwald if (connection){ 138532ab9390Smatthias.ringwald 138672ea5239Smatthias.ringwald // send disconnect 138732ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 138832ab9390Smatthias.ringwald 138979bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 13906ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 139172ea5239Smatthias.ringwald 139272ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 139372ea5239Smatthias.ringwald hci_shutdown_connection(connection); 139472ea5239Smatthias.ringwald return; 139572ea5239Smatthias.ringwald } 139672ea5239Smatthias.ringwald 139792368cd3S[email protected] if (hci_classic_supported()){ 139889db417bSmatthias.ringwald // disable page and inquiry scan 139932ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 140032ab9390Smatthias.ringwald 140192368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 1402758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 140389db417bSmatthias.ringwald 140489db417bSmatthias.ringwald // continue in next sub state 140589db417bSmatthias.ringwald hci_stack.substate++; 140689db417bSmatthias.ringwald break; 140792368cd3S[email protected] } 140892368cd3S[email protected] // fall through for ble-only chips 140992368cd3S[email protected] 141089db417bSmatthias.ringwald case 2: 14117b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 141228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 141328171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 141428171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 141528171530Smatthias.ringwald // SLEEP MODE reached 141628171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 141728171530Smatthias.ringwald hci_emit_state(); 141828171530Smatthias.ringwald break; 141928171530Smatthias.ringwald } 142028171530Smatthias.ringwald #endif 142172ea5239Smatthias.ringwald // switch mode 142289db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1423c7e0c5f6Smatthias.ringwald hci_emit_state(); 142428171530Smatthias.ringwald break; 142528171530Smatthias.ringwald 142689db417bSmatthias.ringwald default: 142789db417bSmatthias.ringwald break; 142889db417bSmatthias.ringwald } 1429c7e0c5f6Smatthias.ringwald break; 1430c7e0c5f6Smatthias.ringwald 14313429f56bSmatthias.ringwald default: 14323429f56bSmatthias.ringwald break; 14331f504dbdSmatthias.ringwald } 14343429f56bSmatthias.ringwald } 143516833f0aSmatthias.ringwald 143631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1437c8e4258aSmatthias.ringwald bd_addr_t addr; 1438c8e4258aSmatthias.ringwald hci_connection_t * conn; 1439c8e4258aSmatthias.ringwald // house-keeping 1440c8e4258aSmatthias.ringwald 1441c8e4258aSmatthias.ringwald // create_connection? 1442c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1443c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1444339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1445c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1446c8e4258aSmatthias.ringwald if (conn) { 1447c8e4258aSmatthias.ringwald // if connection exists 1448c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 144917f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 145017f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1451c8e4258aSmatthias.ringwald } 145217f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1453c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1454c8e4258aSmatthias.ringwald 145517f1ba2aSmatthias.ringwald } 1456c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 145717f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 145817f1ba2aSmatthias.ringwald if (!conn){ 145917f1ba2aSmatthias.ringwald // notify client that alloc failed 146017f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 146117f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 146217f1ba2aSmatthias.ringwald } 1463c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1464c8e4258aSmatthias.ringwald } 1465c8e4258aSmatthias.ringwald 14667fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 14677fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 14687fde4af9Smatthias.ringwald } 14697fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 14707fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 14717fde4af9Smatthias.ringwald } 14727fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 14737fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 14747fde4af9Smatthias.ringwald } 14757fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 14767fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 14777fde4af9Smatthias.ringwald } 14787fde4af9Smatthias.ringwald 14798ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 14808ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 14818ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 14828ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 14838ef73945Smatthias.ringwald } 14848ef73945Smatthias.ringwald } 1485c8e4258aSmatthias.ringwald 1486*69a97523S[email protected] #ifdef HAVE_BLE 1487*69a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 1488*69a97523S[email protected] hci_stack.adv_addr_type = packet[8]; 1489*69a97523S[email protected] } 1490*69a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 1491*69a97523S[email protected] bt_flip_addr(hci_stack.adv_address, &packet[3]); 1492*69a97523S[email protected] } 1493*69a97523S[email protected] #endif 1494*69a97523S[email protected] 1495*69a97523S[email protected] 149631452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1497622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 149831452debSmatthias.ringwald } 14998adf0ddaSmatthias.ringwald 1500dbe1a790S[email protected] // Configure Secure Simple Pairing 1501dbe1a790S[email protected] 1502dbe1a790S[email protected] // enable will enable SSP during init 1503dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 1504dbe1a790S[email protected] hci_stack.ssp_enable = enable; 1505dbe1a790S[email protected] } 1506dbe1a790S[email protected] 1507dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1508dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 1509dbe1a790S[email protected] hci_stack.ssp_io_capability = io_capability; 1510dbe1a790S[email protected] } 1511dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 1512dbe1a790S[email protected] hci_stack.ssp_authentication_requirement = authentication_requirement; 1513dbe1a790S[email protected] } 1514dbe1a790S[email protected] 1515dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 1516dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 1517dbe1a790S[email protected] hci_stack.ssp_auto_accept = auto_accept; 1518dbe1a790S[email protected] } 1519dbe1a790S[email protected] 15201cd208adSmatthias.ringwald /** 15211cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 15221cd208adSmatthias.ringwald */ 1523fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 15241cd208adSmatthias.ringwald va_list argptr; 15251cd208adSmatthias.ringwald va_start(argptr, cmd); 15267dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 15271cd208adSmatthias.ringwald va_end(argptr); 15287dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 152993b8dc03Smatthias.ringwald } 1530c8e4258aSmatthias.ringwald 1531ee091cf1Smatthias.ringwald // Create various non-HCI events. 1532ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1533ee091cf1Smatthias.ringwald 1534c8e4258aSmatthias.ringwald void hci_emit_state(){ 1535e0abb8e7S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack.state); 1536425d1371Smatthias.ringwald uint8_t event[3]; 153780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1538425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1539c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1540425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1541425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1542c8e4258aSmatthias.ringwald } 1543c8e4258aSmatthias.ringwald 154417f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1545425d1371Smatthias.ringwald uint8_t event[13]; 1546c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1547425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 154817f1ba2aSmatthias.ringwald event[2] = status; 1549c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1550c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1551c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1552c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1553425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1554425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1555c8e4258aSmatthias.ringwald } 1556c8e4258aSmatthias.ringwald 15573c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1558425d1371Smatthias.ringwald uint8_t event[6]; 15593c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1560e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 15613c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 15623c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 15633c4d4b90Smatthias.ringwald event[5] = reason; 1564425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1565425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 15663c4d4b90Smatthias.ringwald } 15673c4d4b90Smatthias.ringwald 1568ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1569e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1570425d1371Smatthias.ringwald uint8_t event[4]; 157180d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1572425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1573ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1574425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1575425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1576ee091cf1Smatthias.ringwald } 157743bfb1bdSmatthias.ringwald 157843bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1579e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1580425d1371Smatthias.ringwald uint8_t event[3]; 158180d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1582425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 158343bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1584425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1585425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 158643bfb1bdSmatthias.ringwald } 1587038bc64cSmatthias.ringwald 1588038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1589e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1590425d1371Smatthias.ringwald uint8_t event[2]; 159180d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1592425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1593425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1594425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1595038bc64cSmatthias.ringwald } 15961b0e3922Smatthias.ringwald 159709ba8edeSmatthias.ringwald #ifndef EMBEDDED 15981b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1599e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1600425d1371Smatthias.ringwald uint8_t event[6]; 16011b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1602425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1603425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1604425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1605425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1606425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1607425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 16081b0e3922Smatthias.ringwald } 160909ba8edeSmatthias.ringwald #endif 16101b0e3922Smatthias.ringwald 16112ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1612e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1613425d1371Smatthias.ringwald uint8_t event[3]; 16142ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1615425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 16162ed6235cSmatthias.ringwald event[2] = enabled; 1617425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1618e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 16192ed6235cSmatthias.ringwald } 1620627c2f45Smatthias.ringwald 1621627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1622e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1623627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1624e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1625f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 16262f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1627f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1628e0abb8e7S[email protected] 1629e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1630e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1631e0abb8e7S[email protected] 1632e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 1633e0abb8e7S[email protected] hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1634627c2f45Smatthias.ringwald } 1635381fbed8Smatthias.ringwald 1636381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1637e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1638425d1371Smatthias.ringwald uint8_t event[3]; 1639381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1640425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1641381fbed8Smatthias.ringwald event[2] = enabled; 1642425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1643425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1644381fbed8Smatthias.ringwald } 1645