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" 47*4c57c146S[email protected] #include "gap.h" 487f2435e6Smatthias.ringwald 4993b8dc03Smatthias.ringwald #include <stdarg.h> 5093b8dc03Smatthias.ringwald #include <string.h> 5156fe0872Smatthias.ringwald #include <stdio.h> 527f2435e6Smatthias.ringwald 53549e6ebeSmatthias.ringwald #ifndef EMBEDDED 54549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 5509ba8edeSmatthias.ringwald #include <btstack/version.h> 56549e6ebeSmatthias.ringwald #endif 57549e6ebeSmatthias.ringwald 58a3b02b71Smatthias.ringwald #include "btstack_memory.h" 597f2435e6Smatthias.ringwald #include "debug.h" 60d8905019Smatthias.ringwald #include "hci_dump.h" 6193b8dc03Smatthias.ringwald 62ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 631b0e3922Smatthias.ringwald 64169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 65ee091cf1Smatthias.ringwald 66a45d6b9fS[email protected] #define HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP 11 67da5275c5S[email protected] 6828171530Smatthias.ringwald #ifdef USE_BLUETOOL 6928171530Smatthias.ringwald #include "bt_control_iphone.h" 7028171530Smatthias.ringwald #endif 7128171530Smatthias.ringwald 72758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 73758b46ceSmatthias.ringwald 7406b35ec0Smatthias.ringwald // the STACK is here 7516833f0aSmatthias.ringwald static hci_stack_t hci_stack; 7616833f0aSmatthias.ringwald 7797addcc5Smatthias.ringwald /** 78ee091cf1Smatthias.ringwald * get connection for a given handle 79ee091cf1Smatthias.ringwald * 80ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 81ee091cf1Smatthias.ringwald */ 825061f3afS[email protected] hci_connection_t * hci_connection_for_handle(hci_con_handle_t con_handle){ 83ee091cf1Smatthias.ringwald linked_item_t *it; 84ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 85ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 86ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 87ee091cf1Smatthias.ringwald } 88ee091cf1Smatthias.ringwald } 89ee091cf1Smatthias.ringwald return NULL; 90ee091cf1Smatthias.ringwald } 91ee091cf1Smatthias.ringwald 922b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 9328ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 94c785ef68Smatthias.ringwald #ifdef HAVE_TIME 95ee091cf1Smatthias.ringwald struct timeval tv; 96ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 97c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 98ee091cf1Smatthias.ringwald // connections might be timed out 99ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 100ee091cf1Smatthias.ringwald } 1012b12a0b9Smatthias.ringwald #endif 102e5780900Smatthias.ringwald #ifdef HAVE_TICK 103c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 104c785ef68Smatthias.ringwald // connections might be timed out 105c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 106c785ef68Smatthias.ringwald } 107c785ef68Smatthias.ringwald #endif 108c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 109c785ef68Smatthias.ringwald run_loop_add_timer(timer); 110c785ef68Smatthias.ringwald } 111ee091cf1Smatthias.ringwald 112ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 113c7492964Smatthias.ringwald #ifdef HAVE_TIME 114ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 115c7492964Smatthias.ringwald #endif 116e5780900Smatthias.ringwald #ifdef HAVE_TICK 117c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 118c785ef68Smatthias.ringwald #endif 119ee091cf1Smatthias.ringwald } 120ee091cf1Smatthias.ringwald 121ee091cf1Smatthias.ringwald /** 122c8e4258aSmatthias.ringwald * create connection for given address 123c8e4258aSmatthias.ringwald * 12417f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 125c8e4258aSmatthias.ringwald */ 126c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 12728ca2b46S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 128c8e4258aSmatthias.ringwald if (!conn) return NULL; 129c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 130c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1317d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 132ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 133ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 134ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 135d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1367856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 13756cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 138c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 139c8e4258aSmatthias.ringwald return conn; 140c8e4258aSmatthias.ringwald } 141c8e4258aSmatthias.ringwald 142c8e4258aSmatthias.ringwald /** 14306b35ec0Smatthias.ringwald * get connection for given address 14497addcc5Smatthias.ringwald * 14597addcc5Smatthias.ringwald * @return connection OR NULL, if not found 14697addcc5Smatthias.ringwald */ 147fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 14806b35ec0Smatthias.ringwald linked_item_t *it; 14906b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 15006b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 15106b35ec0Smatthias.ringwald return (hci_connection_t *) it; 15206b35ec0Smatthias.ringwald } 15306b35ec0Smatthias.ringwald } 15406b35ec0Smatthias.ringwald return NULL; 15506b35ec0Smatthias.ringwald } 15606b35ec0Smatthias.ringwald 15728ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 15828ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 15928ca2b46S[email protected] } 16028ca2b46S[email protected] 16128ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 16228ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 16328ca2b46S[email protected] } 16428ca2b46S[email protected] 16528ca2b46S[email protected] 16643bfb1bdSmatthias.ringwald /** 16780ca58a0Smatthias.ringwald * add authentication flags and reset timer 1687fde4af9Smatthias.ringwald */ 1697fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1707fde4af9Smatthias.ringwald bd_addr_t addr; 1717fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1727fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1737fde4af9Smatthias.ringwald if (conn) { 17428ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 17580ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1767fde4af9Smatthias.ringwald } 1777fde4af9Smatthias.ringwald } 1787fde4af9Smatthias.ringwald 17980ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 1805061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 18180ca58a0Smatthias.ringwald if (!conn) return 0; 18280ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 18380ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 18480ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 18580ca58a0Smatthias.ringwald return 1; 18680ca58a0Smatthias.ringwald } 18780ca58a0Smatthias.ringwald 188c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 189c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 190c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 191c12e46e7Smatthias.ringwald } 192c12e46e7Smatthias.ringwald } 193c12e46e7Smatthias.ringwald 1947fde4af9Smatthias.ringwald 1957fde4af9Smatthias.ringwald /** 19643bfb1bdSmatthias.ringwald * count connections 19743bfb1bdSmatthias.ringwald */ 19840d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 19956c253c9Smatthias.ringwald int count = 0; 20043bfb1bdSmatthias.ringwald linked_item_t *it; 20143bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 20243bfb1bdSmatthias.ringwald return count; 20343bfb1bdSmatthias.ringwald } 204c8e4258aSmatthias.ringwald 20597addcc5Smatthias.ringwald /** 206ba681a6cSmatthias.ringwald * Dummy handler called by HCI 20716833f0aSmatthias.ringwald */ 2082718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 20916833f0aSmatthias.ringwald } 21016833f0aSmatthias.ringwald 211998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 2125061f3afS[email protected] hci_connection_t * connection = hci_connection_for_handle(handle); 213998906cdSmatthias.ringwald if (!connection) { 2147d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 215998906cdSmatthias.ringwald return 0; 216998906cdSmatthias.ringwald } 217998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 218998906cdSmatthias.ringwald } 219998906cdSmatthias.ringwald 220998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 221998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 222998906cdSmatthias.ringwald linked_item_t *it; 223998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 224998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 225998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2267d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 227998906cdSmatthias.ringwald return 0; 228998906cdSmatthias.ringwald } 229998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 230998906cdSmatthias.ringwald } 231998906cdSmatthias.ringwald return free_slots; 232998906cdSmatthias.ringwald } 233998906cdSmatthias.ringwald 234c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2352b12a0b9Smatthias.ringwald 2362b12a0b9Smatthias.ringwald // check for async hci transport implementations 2372b12a0b9Smatthias.ringwald if (hci_stack.hci_transport->can_send_packet_now){ 2382b12a0b9Smatthias.ringwald if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 2392b12a0b9Smatthias.ringwald return 0; 2402b12a0b9Smatthias.ringwald } 2412b12a0b9Smatthias.ringwald } 2422b12a0b9Smatthias.ringwald 2432b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 244c24735b1Smatthias.ringwald switch (packet_type) { 245c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 246c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 247c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 248de009a8cSmatthias.ringwald return hci_stack.num_cmd_packets; 249c24735b1Smatthias.ringwald default: 250c24735b1Smatthias.ringwald return 0; 251c24735b1Smatthias.ringwald } 252c24735b1Smatthias.ringwald } 253c24735b1Smatthias.ringwald 254ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2557856c818Smatthias.ringwald 2566218e6f1Smatthias.ringwald // check for free places on BT module 2575932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2586218e6f1Smatthias.ringwald 2597856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2605061f3afS[email protected] hci_connection_t *connection = hci_connection_for_handle( con_handle); 26156cf178bSmatthias.ringwald if (!connection) return 0; 26256cf178bSmatthias.ringwald hci_connection_timestamp(connection); 26356cf178bSmatthias.ringwald 26456cf178bSmatthias.ringwald // count packet 26556cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2667b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2677856c818Smatthias.ringwald 26800d8e42eSmatthias.ringwald // send packet 26900d8e42eSmatthias.ringwald int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2706218e6f1Smatthias.ringwald 27100d8e42eSmatthias.ringwald return err; 272ee091cf1Smatthias.ringwald } 273ee091cf1Smatthias.ringwald 27416833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2757856c818Smatthias.ringwald 276e76a89eeS[email protected] // log_info("acl_handler: size %u", size); 277e76a89eeS[email protected] 2787856c818Smatthias.ringwald // get info 2797856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2805061f3afS[email protected] hci_connection_t *conn = hci_connection_for_handle(con_handle); 2817856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2827856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2837856c818Smatthias.ringwald 2847856c818Smatthias.ringwald // ignore non-registered handle 2857856c818Smatthias.ringwald if (!conn){ 2867d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2877856c818Smatthias.ringwald return; 2887856c818Smatthias.ringwald } 2897856c818Smatthias.ringwald 290e76a89eeS[email protected] // assert packet is complete 2919ecc3e17S[email protected] if (acl_length + 4 != size){ 292e76a89eeS[email protected] log_error("hci.c: acl_handler called with ACL packet of wrong size %u, expected %u => dropping packet", size, acl_length + 4); 293e76a89eeS[email protected] return; 294e76a89eeS[email protected] } 295e76a89eeS[email protected] 2967856c818Smatthias.ringwald // update idle timestamp 2977856c818Smatthias.ringwald hci_connection_timestamp(conn); 2987856c818Smatthias.ringwald 2997856c818Smatthias.ringwald // handle different packet types 3007856c818Smatthias.ringwald switch (acl_flags & 0x03) { 3017856c818Smatthias.ringwald 3027856c818Smatthias.ringwald case 0x01: // continuation fragment 3037856c818Smatthias.ringwald 3047856c818Smatthias.ringwald // sanity check 3057856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 3067d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 3077856c818Smatthias.ringwald return; 3087856c818Smatthias.ringwald } 3097856c818Smatthias.ringwald 3107856c818Smatthias.ringwald // append fragment payload (header already stored) 3117856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 3127856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 3137856c818Smatthias.ringwald 3147d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 315decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3167856c818Smatthias.ringwald 3177856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3187856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3197856c818Smatthias.ringwald 3202718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3217856c818Smatthias.ringwald // reset recombination buffer 3227856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3237856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3247856c818Smatthias.ringwald } 3257856c818Smatthias.ringwald break; 3267856c818Smatthias.ringwald 3277856c818Smatthias.ringwald case 0x02: { // first fragment 3287856c818Smatthias.ringwald 3297856c818Smatthias.ringwald // sanity check 3307856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3317d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3327856c818Smatthias.ringwald return; 3337856c818Smatthias.ringwald } 3347856c818Smatthias.ringwald 3357856c818Smatthias.ringwald // peek into L2CAP packet! 3367856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3377856c818Smatthias.ringwald 338e76a89eeS[email protected] // log_info( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 339decc01a8Smatthias.ringwald 3407856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3417856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3427856c818Smatthias.ringwald 3437856c818Smatthias.ringwald // forward fragment as L2CAP packet 3442718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3457856c818Smatthias.ringwald 3467856c818Smatthias.ringwald } else { 3477856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3487856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3497856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3507856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 351decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3527856c818Smatthias.ringwald } 3537856c818Smatthias.ringwald break; 3547856c818Smatthias.ringwald 3557856c818Smatthias.ringwald } 3567856c818Smatthias.ringwald default: 3577d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3587856c818Smatthias.ringwald return; 3597856c818Smatthias.ringwald } 36094ab26f8Smatthias.ringwald 36194ab26f8Smatthias.ringwald // execute main loop 36294ab26f8Smatthias.ringwald hci_run(); 36316833f0aSmatthias.ringwald } 36422909952Smatthias.ringwald 36567a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 366339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3673c4d4b90Smatthias.ringwald 3683c4d4b90Smatthias.ringwald // cancel all l2cap connections 3693c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3703c4d4b90Smatthias.ringwald 371c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 372c785ef68Smatthias.ringwald 373c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 374a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3753c4d4b90Smatthias.ringwald 3763c4d4b90Smatthias.ringwald // now it's gone 377c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 378c7e0c5f6Smatthias.ringwald } 379c7e0c5f6Smatthias.ringwald 3800c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3818f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3828f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3838f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3848f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3858f8108aaSmatthias.ringwald }; 38665389bfcS[email protected] static const uint8_t packet_type_feature_requirement_bit[] = { 38765389bfcS[email protected] 0, // 3 slot packets 38865389bfcS[email protected] 1, // 5 slot packets 38965389bfcS[email protected] 25, // EDR 2 mpbs 39065389bfcS[email protected] 26, // EDR 3 mbps 39165389bfcS[email protected] 39, // 3 slot EDR packts 39265389bfcS[email protected] 40, // 5 slot EDR packet 39365389bfcS[email protected] }; 39465389bfcS[email protected] static const uint16_t packet_type_feature_packet_mask[] = { 39565389bfcS[email protected] 0x0f00, // 3 slot packets 39665389bfcS[email protected] 0xf000, // 5 slot packets 39765389bfcS[email protected] 0x1102, // EDR 2 mpbs 39865389bfcS[email protected] 0x2204, // EDR 3 mbps 39965389bfcS[email protected] 0x0300, // 3 slot EDR packts 40065389bfcS[email protected] 0x3000, // 5 slot EDR packet 40165389bfcS[email protected] }; 4028f8108aaSmatthias.ringwald 40365389bfcS[email protected] static uint16_t hci_acl_packet_types_for_buffer_size_and_local_features(uint16_t buffer_size, uint8_t * local_supported_features){ 40465389bfcS[email protected] // enable packet types based on size 4058f8108aaSmatthias.ringwald uint16_t packet_types = 0; 4068f8108aaSmatthias.ringwald int i; 4078f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 4088f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 4098f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 4108f8108aaSmatthias.ringwald packet_types |= 1 << i; 4118f8108aaSmatthias.ringwald } 4128f8108aaSmatthias.ringwald } 41365389bfcS[email protected] // disable packet types due to missing local supported features 41465389bfcS[email protected] for (i=0;i<sizeof(packet_type_feature_requirement_bit);i++){ 41565389bfcS[email protected] int bit_idx = packet_type_feature_requirement_bit[i]; 41665389bfcS[email protected] int feature_set = (local_supported_features[bit_idx >> 3] & (1<<(bit_idx & 7))) != 0; 41765389bfcS[email protected] if (feature_set) continue; 41865389bfcS[email protected] log_info("Features bit %02u is not set, removing packet types 0x%04x", bit_idx, packet_type_feature_packet_mask[i]); 41965389bfcS[email protected] packet_types &= ~packet_type_feature_packet_mask[i]; 42065389bfcS[email protected] } 4218f8108aaSmatthias.ringwald // flip bits for "may not be used" 4228f8108aaSmatthias.ringwald packet_types ^= 0x3306; 4238f8108aaSmatthias.ringwald return packet_types; 4248f8108aaSmatthias.ringwald } 4258f8108aaSmatthias.ringwald 4268f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 4278f8108aaSmatthias.ringwald return hci_stack.packet_types; 4288f8108aaSmatthias.ringwald } 4298f8108aaSmatthias.ringwald 4307dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 4317dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 4327dc17943Smatthias.ringwald return hci_stack.hci_packet_buffer; 4337dc17943Smatthias.ringwald } 4347dc17943Smatthias.ringwald 435f5d8d141S[email protected] uint16_t hci_max_acl_data_packet_length(void){ 4367dc17943Smatthias.ringwald return hci_stack.acl_data_packet_length; 4377dc17943Smatthias.ringwald } 4387dc17943Smatthias.ringwald 439f5d8d141S[email protected] int hci_ssp_supported(void){ 440f5d8d141S[email protected] // No 51, byte 6, bit 3 441f5d8d141S[email protected] return (hci_stack.local_supported_features[6] & (1 << 3)) != 0; 442f5d8d141S[email protected] } 443f5d8d141S[email protected] 444f5d8d141S[email protected] int hci_classic_supported(void){ 445f5d8d141S[email protected] // No 37, byte 4, bit 5, = No BR/EDR Support 446f5d8d141S[email protected] return (hci_stack.local_supported_features[4] & (1 << 5)) == 0; 447f5d8d141S[email protected] } 448f5d8d141S[email protected] 449f5d8d141S[email protected] int hci_le_supported(void){ 450f5d8d141S[email protected] // No 37, byte 4, bit 6 = LE Supported (Controller) 451f5d8d141S[email protected] #ifdef HAVE_BLE 452f5d8d141S[email protected] return (hci_stack.local_supported_features[4] & (1 << 6)) != 0; 453f5d8d141S[email protected] #else 454f5d8d141S[email protected] return 0; 455f5d8d141S[email protected] #endif 456f5d8d141S[email protected] } 457f5d8d141S[email protected] 45869a97523S[email protected] // get addr type and address used in advertisement packets 45918904abdS[email protected] void hci_le_advertisement_address(uint8_t * addr_type, bd_addr_t * addr){ 46069a97523S[email protected] *addr_type = hci_stack.adv_addr_type; 46169a97523S[email protected] if (hci_stack.adv_addr_type){ 46269a97523S[email protected] memcpy(addr, hci_stack.adv_address, 6); 46369a97523S[email protected] } else { 46469a97523S[email protected] memcpy(addr, hci_stack.local_bd_addr, 6); 46569a97523S[email protected] } 46669a97523S[email protected] } 46769a97523S[email protected] 46874ec757aSmatthias.ringwald // avoid huge local variables 469223aafc1Smatthias.ringwald #ifndef EMBEDDED 47074ec757aSmatthias.ringwald static device_name_t device_name; 471223aafc1Smatthias.ringwald #endif 47216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 473e76a89eeS[email protected] 474e76a89eeS[email protected] uint16_t event_length = packet[1]; 475e76a89eeS[email protected] 476e76a89eeS[email protected] // assert packet is complete 477e76a89eeS[email protected] if (size != event_length + 2){ 478e76a89eeS[email protected] log_error("hci.c: event_handler called with event packet of wrong size %u, expected %u => dropping packet", size, event_length + 2); 479e76a89eeS[email protected] return; 480e76a89eeS[email protected] } 481e76a89eeS[email protected] 4821281a47eSmatthias.ringwald bd_addr_t addr; 4832d00edd4Smatthias.ringwald uint8_t link_type; 484fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 4851f7b95a1Smatthias.ringwald hci_connection_t * conn; 48656cf178bSmatthias.ringwald int i; 48722909952Smatthias.ringwald 4885909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 4895909f7f2Smatthias.ringwald 4906772a24cSmatthias.ringwald switch (packet[0]) { 49122909952Smatthias.ringwald 4926772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4937ec5eeaaSmatthias.ringwald // get num cmd packets 4947b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4957ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4967ec5eeaaSmatthias.ringwald 497e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 498e2edc0c3Smatthias.ringwald // from offset 5 499e2edc0c3Smatthias.ringwald // status 5001d279b20Smatthias.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" 501e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 50256cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 503e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 50456cf178bSmatthias.ringwald // ignore: total num SCO packets 50556cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 506a7a04bd9Smatthias.ringwald // determine usable ACL payload size 5078fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 5088fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 5098f8108aaSmatthias.ringwald } 51065389bfcS[email protected] log_info("hci_read_buffer_size: used size %u, count %u\n", 51165389bfcS[email protected] hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets); 512e2edc0c3Smatthias.ringwald } 51356cf178bSmatthias.ringwald } 51465a46ef3S[email protected] #ifdef HAVE_BLE 51565a46ef3S[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_le_read_buffer_size)){ 51665a46ef3S[email protected] hci_stack.le_data_packet_length = READ_BT_16(packet, 6); 51765a46ef3S[email protected] hci_stack.total_num_le_packets = packet[8]; 51865a46ef3S[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); 51965a46ef3S[email protected] } 52065a46ef3S[email protected] #endif 521188981d2Smatthias.ringwald // Dump local address 522188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 523e2386ba1S[email protected] bt_flip_addr(hci_stack.local_bd_addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 524188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 525e2386ba1S[email protected] packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack.local_bd_addr)); 526188981d2Smatthias.ringwald } 527381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 528381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 529381fbed8Smatthias.ringwald } 53065389bfcS[email protected] // Note: HCI init checks 531559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 53265389bfcS[email protected] memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE+1], 8); 533559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 534559e517eS[email protected] hci_stack.local_supported_features[0], hci_stack.local_supported_features[1], 535559e517eS[email protected] hci_stack.local_supported_features[2], hci_stack.local_supported_features[3], 536559e517eS[email protected] hci_stack.local_supported_features[4], hci_stack.local_supported_features[5], 537559e517eS[email protected] hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]); 53865389bfcS[email protected] 53965389bfcS[email protected] // determine usable ACL packet types based buffer size and supported features 54065389bfcS[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]); 541f5d8d141S[email protected] log_info("packet types %04x", hci_stack.packet_types); 542f5d8d141S[email protected] 543f5d8d141S[email protected] // Classic/LE 544f5d8d141S[email protected] log_info("BR/EDR support %u, LE support %u", hci_classic_supported(), hci_le_supported()); 545559e517eS[email protected] } 54656cf178bSmatthias.ringwald break; 54756cf178bSmatthias.ringwald 5487ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 5497ec5eeaaSmatthias.ringwald // get num cmd packets 5507b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 5517ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 5527ec5eeaaSmatthias.ringwald break; 5537ec5eeaaSmatthias.ringwald 55456cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 55556cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 55656cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 55756cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 5585061f3afS[email protected] conn = hci_connection_for_handle(handle); 55956cf178bSmatthias.ringwald if (!conn){ 5607d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 56156cf178bSmatthias.ringwald continue; 56256cf178bSmatthias.ringwald } 56356cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 5647b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 56556cf178bSmatthias.ringwald } 5666772a24cSmatthias.ringwald break; 5676772a24cSmatthias.ringwald 5681f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 56937eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 57037eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 5712d00edd4Smatthias.ringwald link_type = packet[11]; 572339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 57337eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 5741f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 5751f7b95a1Smatthias.ringwald if (!conn) { 5761f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 5771f7b95a1Smatthias.ringwald } 578ce4c8fabSmatthias.ringwald if (!conn) { 579ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 580ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 581ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 582ce4c8fabSmatthias.ringwald break; 583ce4c8fabSmatthias.ringwald } 58432ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 58532ab9390Smatthias.ringwald hci_run(); 58637eaa4cfSmatthias.ringwald } else { 587ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 588ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 589ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 59037eaa4cfSmatthias.ringwald } 5911f7b95a1Smatthias.ringwald break; 5921f7b95a1Smatthias.ringwald 5936772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 594fe1ed1b8Smatthias.ringwald // Connection management 595fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 596339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 5971f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 598fe1ed1b8Smatthias.ringwald if (conn) { 599b448a0e7Smatthias.ringwald if (!packet[2]){ 600c8e4258aSmatthias.ringwald conn->state = OPEN; 601fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 602ee091cf1Smatthias.ringwald 603c785ef68Smatthias.ringwald // restart timer 604c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 605ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 606c785ef68Smatthias.ringwald 607339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 60843bfb1bdSmatthias.ringwald 60943bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 610b448a0e7Smatthias.ringwald } else { 611b448a0e7Smatthias.ringwald // connection failed, remove entry 612b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 613a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 614c12e46e7Smatthias.ringwald 615c12e46e7Smatthias.ringwald // if authentication error, also delete link key 616c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 617c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 618c12e46e7Smatthias.ringwald } 619fe1ed1b8Smatthias.ringwald } 620fe1ed1b8Smatthias.ringwald } 6216772a24cSmatthias.ringwald break; 622fe1ed1b8Smatthias.ringwald 6237fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 6247b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 6257fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 62674d716b5S[email protected] // non-bondable mode: link key negative reply will be sent by HANDLE_LINK_KEY_REQUEST 62774d716b5S[email protected] if (hci_stack.bondable && !hci_stack.remote_device_db) break; 62832ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 62964472d52Smatthias.ringwald hci_run(); 630d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 63129d53098Smatthias.ringwald return; 6327fde4af9Smatthias.ringwald 6337fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 6347fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 63574ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 63629d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 637287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 63829d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 6397fde4af9Smatthias.ringwald break; 6407fde4af9Smatthias.ringwald 6417fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 6427fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 643*4c57c146S[email protected] // non-bondable mode: pin code negative reply will be sent 644*4c57c146S[email protected] if (!hci_stack.bondable){ 645*4c57c146S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_PIN_CODE_REQUEST); 646*4c57c146S[email protected] break; 647*4c57c146S[email protected] } 648d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 649d9a327f9Smatthias.ringwald if (!hci_stack.remote_device_db) break; 650d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 651d9a327f9Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 6527fde4af9Smatthias.ringwald break; 6537fde4af9Smatthias.ringwald 6541d6b20aeS[email protected] case HCI_EVENT_IO_CAPABILITY_REQUEST: 6551d6b20aeS[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_IO_CAPABILITIES_REQUEST); 6561d6b20aeS[email protected] if (hci_stack.ssp_io_capability == SSP_IO_CAPABILITY_UNKNOWN) break; 657dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_IO_CAPABILITIES_REPLY); 658dbe1a790S[email protected] break; 659dbe1a790S[email protected] 660dbe1a790S[email protected] case HCI_EVENT_USER_CONFIRMATION_REQUEST: 661dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_CONFIRM_REQUEST); 662dbe1a790S[email protected] if (!hci_stack.ssp_auto_accept) break; 663dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_CONFIRM_REPLY); 664dbe1a790S[email protected] break; 665dbe1a790S[email protected] 666dbe1a790S[email protected] case HCI_EVENT_USER_PASSKEY_REQUEST: 667dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_USER_PASSKEY_REQUEST); 668dbe1a790S[email protected] if (!hci_stack.ssp_auto_accept) break; 669dbe1a790S[email protected] hci_add_connection_flags_for_flipped_bd_addr(&packet[2], SEND_USER_PASSKEY_REPLY); 6701d6b20aeS[email protected] break; 6711d6b20aeS[email protected] 672223aafc1Smatthias.ringwald #ifndef EMBEDDED 67374ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 67474ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 67574ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 67674ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 6775a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 6785a06394aSmatthias.ringwald for (i=0; i<248;i++){ 6795a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 6805a06394aSmatthias.ringwald packet[9+i] = 0; 6815a06394aSmatthias.ringwald break; 682cdc9101dSmatthias.ringwald } 6835a06394aSmatthias.ringwald } 684d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 68574ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 68674ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 68774ec757aSmatthias.ringwald break; 68874ec757aSmatthias.ringwald 68974ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 69074ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 69174ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 69274ec757aSmatthias.ringwald // first send inq result packet 69374ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 69474ec757aSmatthias.ringwald // then send cached remote names 69574ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 69674ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 69774ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 69874ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 69974ec757aSmatthias.ringwald } 70074ec757aSmatthias.ringwald } 70174ec757aSmatthias.ringwald return; 702223aafc1Smatthias.ringwald #endif 70374ec757aSmatthias.ringwald 7046772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 705fe1ed1b8Smatthias.ringwald if (!packet[2]){ 706fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 7075061f3afS[email protected] hci_connection_t * conn = hci_connection_for_handle(handle); 708fe1ed1b8Smatthias.ringwald if (conn) { 709c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 710fe1ed1b8Smatthias.ringwald } 711fe1ed1b8Smatthias.ringwald } 7126772a24cSmatthias.ringwald break; 7136772a24cSmatthias.ringwald 714c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 7156c062428S[email protected] if(hci_stack.control && hci_stack.control->hw_error){ 716c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 717c68bdf90Smatthias.ringwald } 718c68bdf90Smatthias.ringwald break; 719c68bdf90Smatthias.ringwald 7205909f7f2Smatthias.ringwald #ifdef HAVE_BLE 7215909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 7225909f7f2Smatthias.ringwald switch (packet[2]) { 7235909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 7245909f7f2Smatthias.ringwald // Connection management 7255909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 7265909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 7275909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 7285909f7f2Smatthias.ringwald conn = connection_for_address(addr); 7295909f7f2Smatthias.ringwald if (packet[3]){ 7305909f7f2Smatthias.ringwald if (conn){ 7315909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 7325909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 7335909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 7345909f7f2Smatthias.ringwald 7355909f7f2Smatthias.ringwald } 7365909f7f2Smatthias.ringwald // if authentication error, also delete link key 7375909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 7385909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 7395909f7f2Smatthias.ringwald } 7405909f7f2Smatthias.ringwald break; 7415909f7f2Smatthias.ringwald } 7425909f7f2Smatthias.ringwald if (!conn){ 7435909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 7445909f7f2Smatthias.ringwald } 7455909f7f2Smatthias.ringwald if (!conn){ 7465909f7f2Smatthias.ringwald // no memory 7475909f7f2Smatthias.ringwald break; 7485909f7f2Smatthias.ringwald } 7495909f7f2Smatthias.ringwald 7505909f7f2Smatthias.ringwald conn->state = OPEN; 7515909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 7525909f7f2Smatthias.ringwald 7535909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 7545909f7f2Smatthias.ringwald 7555909f7f2Smatthias.ringwald // restart timer 7565909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 7575909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 7585909f7f2Smatthias.ringwald 7595909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 7605909f7f2Smatthias.ringwald 7615909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 7625909f7f2Smatthias.ringwald break; 7635909f7f2Smatthias.ringwald 76465a46ef3S[email protected] // printf("LE buffer size: %u, count %u\n", READ_BT_16(packet,6), packet[8]); 76565a46ef3S[email protected] 7665909f7f2Smatthias.ringwald default: 7675909f7f2Smatthias.ringwald break; 7685909f7f2Smatthias.ringwald } 7695909f7f2Smatthias.ringwald break; 7705909f7f2Smatthias.ringwald #endif 7715909f7f2Smatthias.ringwald 7726772a24cSmatthias.ringwald default: 7736772a24cSmatthias.ringwald break; 774fe1ed1b8Smatthias.ringwald } 775fe1ed1b8Smatthias.ringwald 7763429f56bSmatthias.ringwald // handle BT initialization 7773429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 7783429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 7793429f56bSmatthias.ringwald // odd: waiting for event 780f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 781c9af4d3fS[email protected] // wait for explicit COMMAND COMPLETE on RESET 782c9af4d3fS[email protected] if (hci_stack.substate > 1 || COMMAND_COMPLETE_EVENT(packet, hci_reset)) { 7833429f56bSmatthias.ringwald hci_stack.substate++; 7843429f56bSmatthias.ringwald } 7853429f56bSmatthias.ringwald } 78622909952Smatthias.ringwald } 787c9af4d3fS[email protected] } 78822909952Smatthias.ringwald 78989db417bSmatthias.ringwald // help with BT sleep 79089db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 79189db417bSmatthias.ringwald && hci_stack.substate == 1 79289db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 79389db417bSmatthias.ringwald hci_stack.substate++; 79489db417bSmatthias.ringwald } 79589db417bSmatthias.ringwald 7962718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 79794ab26f8Smatthias.ringwald 79894ab26f8Smatthias.ringwald // execute main loop 79994ab26f8Smatthias.ringwald hci_run(); 80016833f0aSmatthias.ringwald } 80116833f0aSmatthias.ringwald 8020a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 80310e830c9Smatthias.ringwald switch (packet_type) { 80410e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 80510e830c9Smatthias.ringwald event_handler(packet, size); 80610e830c9Smatthias.ringwald break; 80710e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 80810e830c9Smatthias.ringwald acl_handler(packet, size); 80910e830c9Smatthias.ringwald break; 81010e830c9Smatthias.ringwald default: 81110e830c9Smatthias.ringwald break; 81210e830c9Smatthias.ringwald } 81310e830c9Smatthias.ringwald } 81410e830c9Smatthias.ringwald 815fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 8162718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 8172718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 81816833f0aSmatthias.ringwald } 81916833f0aSmatthias.ringwald 8204f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 821475c8125Smatthias.ringwald 822475c8125Smatthias.ringwald // reference to use transport layer implementation 82316833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 824475c8125Smatthias.ringwald 82511e23e5fSmatthias.ringwald // references to used control implementation 82611e23e5fSmatthias.ringwald hci_stack.control = control; 82711e23e5fSmatthias.ringwald 82811e23e5fSmatthias.ringwald // reference to used config 82911e23e5fSmatthias.ringwald hci_stack.config = config; 83011e23e5fSmatthias.ringwald 831fe1ed1b8Smatthias.ringwald // no connections yet 832fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 8330a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 834c0e866bfSmatthias.ringwald hci_stack.connectable = 0; 835458bf4e8S[email protected] hci_stack.bondable = 1; 836758b46ceSmatthias.ringwald 837b031bebbSmatthias.ringwald // no pending cmds 838b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 839b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 840b031bebbSmatthias.ringwald 84116833f0aSmatthias.ringwald // higher level handler 8422718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 84316833f0aSmatthias.ringwald 844404843c1Smatthias.ringwald // store and open remote device db 845404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 846404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 847404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 848404843c1Smatthias.ringwald } 84929d53098Smatthias.ringwald 8508fcba05dSmatthias.ringwald // max acl payload size defined in config.h 8518fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 8528fcba05dSmatthias.ringwald 85316833f0aSmatthias.ringwald // register packet handlers with transport 85410e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 855f5454fc6Smatthias.ringwald 856f5454fc6Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 857e2386ba1S[email protected] 858e2386ba1S[email protected] // class of device 859e2386ba1S[email protected] hci_stack.class_of_device = 0x007a020c; // Smartphone 860a45d6b9fS[email protected] 861ae5d8360S[email protected] // Secure Simple Pairing default: enable, no I/O capabilities, auto accept 862ae5d8360S[email protected] hci_stack.ssp_enable = 1; 863ae5d8360S[email protected] hci_stack.ssp_io_capability = SSP_IO_CAPABILITY_NO_INPUT_NO_OUTPUT; 864a45d6b9fS[email protected] hci_stack.ssp_authentication_requirement = 0; 865ae5d8360S[email protected] hci_stack.ssp_auto_accept = 1; 86669a97523S[email protected] 86769a97523S[email protected] // LE 86869a97523S[email protected] hci_stack.adv_addr_type = 0; 86969a97523S[email protected] memset(hci_stack.adv_address, 0, 6); 870475c8125Smatthias.ringwald } 871475c8125Smatthias.ringwald 872404843c1Smatthias.ringwald void hci_close(){ 873404843c1Smatthias.ringwald // close remote device db 874404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 875404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 876404843c1Smatthias.ringwald } 877f5454fc6Smatthias.ringwald while (hci_stack.connections) { 878f5454fc6Smatthias.ringwald hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 879f5454fc6Smatthias.ringwald } 880f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 881404843c1Smatthias.ringwald } 882404843c1Smatthias.ringwald 8838d213e1aSmatthias.ringwald // State-Module-Driver overview 8848d213e1aSmatthias.ringwald // state module low-level 8858d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 8868d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 8878d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 8888d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 889d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 890d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 891c7e0c5f6Smatthias.ringwald 89240d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 8937301ad89Smatthias.ringwald 894038bc64cSmatthias.ringwald // power on 895f9a30166Smatthias.ringwald int err = 0; 896f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 897f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 898f9a30166Smatthias.ringwald } 899038bc64cSmatthias.ringwald if (err){ 9007d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 901038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 902038bc64cSmatthias.ringwald return err; 903038bc64cSmatthias.ringwald } 904038bc64cSmatthias.ringwald 905038bc64cSmatthias.ringwald // open low-level device 906038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 907038bc64cSmatthias.ringwald if (err){ 9087d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 909f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 910f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 911f9a30166Smatthias.ringwald } 912038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 913038bc64cSmatthias.ringwald return err; 914038bc64cSmatthias.ringwald } 9158d213e1aSmatthias.ringwald return 0; 9168d213e1aSmatthias.ringwald } 917038bc64cSmatthias.ringwald 91840d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 9198d213e1aSmatthias.ringwald 9207b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 9219418f9c9Smatthias.ringwald 9228d213e1aSmatthias.ringwald // close low-level device 9238d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 9248d213e1aSmatthias.ringwald 9257b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 9269418f9c9Smatthias.ringwald 9278d213e1aSmatthias.ringwald // power off 9288d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 9298d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 9308d213e1aSmatthias.ringwald } 9319418f9c9Smatthias.ringwald 9327b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 9339418f9c9Smatthias.ringwald 93472ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 93572ea5239Smatthias.ringwald } 93672ea5239Smatthias.ringwald 93740d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 93872ea5239Smatthias.ringwald 9397b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 9403144bce4Smatthias.ringwald 941b429b9b7Smatthias.ringwald #if 0 942b429b9b7Smatthias.ringwald // don't close serial port during sleep 943b429b9b7Smatthias.ringwald 94472ea5239Smatthias.ringwald // close low-level device 94572ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 946b429b9b7Smatthias.ringwald #endif 94772ea5239Smatthias.ringwald 94872ea5239Smatthias.ringwald // sleep mode 9493144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 95072ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 95172ea5239Smatthias.ringwald } 952b429b9b7Smatthias.ringwald 95372ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 9548d213e1aSmatthias.ringwald } 9558d213e1aSmatthias.ringwald 95640d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 957b429b9b7Smatthias.ringwald 9587b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 959b429b9b7Smatthias.ringwald 960b429b9b7Smatthias.ringwald // wake on 961b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 962b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 963b429b9b7Smatthias.ringwald } 964b429b9b7Smatthias.ringwald 965b429b9b7Smatthias.ringwald #if 0 966b429b9b7Smatthias.ringwald // open low-level device 967b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 968b429b9b7Smatthias.ringwald if (err){ 9697d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 970b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 971b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 972b429b9b7Smatthias.ringwald } 973b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 974b429b9b7Smatthias.ringwald return err; 975b429b9b7Smatthias.ringwald } 976b429b9b7Smatthias.ringwald #endif 977b429b9b7Smatthias.ringwald 978b429b9b7Smatthias.ringwald return 0; 979b429b9b7Smatthias.ringwald } 980b429b9b7Smatthias.ringwald 981b429b9b7Smatthias.ringwald 9828d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 983d661ed19Smatthias.ringwald 9847b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 985d661ed19Smatthias.ringwald 9868d213e1aSmatthias.ringwald int err = 0; 9878d213e1aSmatthias.ringwald switch (hci_stack.state){ 9888d213e1aSmatthias.ringwald 9898d213e1aSmatthias.ringwald case HCI_STATE_OFF: 9908d213e1aSmatthias.ringwald switch (power_mode){ 9918d213e1aSmatthias.ringwald case HCI_POWER_ON: 9928d213e1aSmatthias.ringwald err = hci_power_control_on(); 9938d213e1aSmatthias.ringwald if (err) return err; 9947301ad89Smatthias.ringwald // set up state machine 9957301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 9967301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9977301ad89Smatthias.ringwald hci_stack.substate = 0; 9988d213e1aSmatthias.ringwald break; 9998d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10008d213e1aSmatthias.ringwald // do nothing 10018d213e1aSmatthias.ringwald break; 10028d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1003b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 10048d213e1aSmatthias.ringwald break; 10058d213e1aSmatthias.ringwald } 10068d213e1aSmatthias.ringwald break; 10077301ad89Smatthias.ringwald 10088d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 10098d213e1aSmatthias.ringwald switch (power_mode){ 10108d213e1aSmatthias.ringwald case HCI_POWER_ON: 10118d213e1aSmatthias.ringwald // do nothing 10128d213e1aSmatthias.ringwald break; 10138d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10148d213e1aSmatthias.ringwald // no connections yet, just turn it off 10158d213e1aSmatthias.ringwald hci_power_control_off(); 10168d213e1aSmatthias.ringwald break; 10178d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1018b546ac54Smatthias.ringwald // no connections yet, just turn it off 101972ea5239Smatthias.ringwald hci_power_control_sleep(); 10208d213e1aSmatthias.ringwald break; 10218d213e1aSmatthias.ringwald } 10228d213e1aSmatthias.ringwald break; 10237301ad89Smatthias.ringwald 10248d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 10258d213e1aSmatthias.ringwald switch (power_mode){ 10268d213e1aSmatthias.ringwald case HCI_POWER_ON: 10278d213e1aSmatthias.ringwald // do nothing 10288d213e1aSmatthias.ringwald break; 10298d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1030c7e0c5f6Smatthias.ringwald // see hci_run 1031c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 10328d213e1aSmatthias.ringwald break; 10338d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1034b546ac54Smatthias.ringwald // see hci_run 1035b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 103689db417bSmatthias.ringwald hci_stack.substate = 0; 10378d213e1aSmatthias.ringwald break; 10388d213e1aSmatthias.ringwald } 10398d213e1aSmatthias.ringwald break; 10407301ad89Smatthias.ringwald 10418d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 10428d213e1aSmatthias.ringwald switch (power_mode){ 10438d213e1aSmatthias.ringwald case HCI_POWER_ON: 10448d213e1aSmatthias.ringwald // set up state machine 10458d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 10468d213e1aSmatthias.ringwald hci_stack.substate = 0; 10478d213e1aSmatthias.ringwald break; 10488d213e1aSmatthias.ringwald case HCI_POWER_OFF: 10498d213e1aSmatthias.ringwald // do nothing 10508d213e1aSmatthias.ringwald break; 10518d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1052b546ac54Smatthias.ringwald // see hci_run 1053b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 105489db417bSmatthias.ringwald hci_stack.substate = 0; 10558d213e1aSmatthias.ringwald break; 10568d213e1aSmatthias.ringwald } 10578d213e1aSmatthias.ringwald break; 10588d213e1aSmatthias.ringwald 10598d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 10608d213e1aSmatthias.ringwald switch (power_mode){ 10618d213e1aSmatthias.ringwald case HCI_POWER_ON: 106228171530Smatthias.ringwald 106328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 106428171530Smatthias.ringwald // nothing to do, if H4 supports power management 106528171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 10668747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1067da5275c5S[email protected] hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 106828171530Smatthias.ringwald break; 106928171530Smatthias.ringwald } 107028171530Smatthias.ringwald #endif 1071b546ac54Smatthias.ringwald // set up state machine 1072b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 1073b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1074b546ac54Smatthias.ringwald hci_stack.substate = 0; 10758d213e1aSmatthias.ringwald break; 10768d213e1aSmatthias.ringwald case HCI_POWER_OFF: 1077b546ac54Smatthias.ringwald // see hci_run 1078b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 10798d213e1aSmatthias.ringwald break; 10808d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1081b546ac54Smatthias.ringwald // do nothing 10828d213e1aSmatthias.ringwald break; 10838d213e1aSmatthias.ringwald } 10848d213e1aSmatthias.ringwald break; 10858d213e1aSmatthias.ringwald 10868d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 10878d213e1aSmatthias.ringwald switch (power_mode){ 10888d213e1aSmatthias.ringwald case HCI_POWER_ON: 108928171530Smatthias.ringwald 109028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 109128171530Smatthias.ringwald // nothing to do, if H4 supports power management 109228171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 10938747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1094da5275c5S[email protected] hci_stack.substate = HCI_INTIALIZING_SUBSTATE_AFTER_SLEEP; 1095758b46ceSmatthias.ringwald hci_update_scan_enable(); 109628171530Smatthias.ringwald break; 109728171530Smatthias.ringwald } 109828171530Smatthias.ringwald #endif 10993144bce4Smatthias.ringwald err = hci_power_control_wake(); 11003144bce4Smatthias.ringwald if (err) return err; 1101b546ac54Smatthias.ringwald // set up state machine 1102b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 1103b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1104b546ac54Smatthias.ringwald hci_stack.substate = 0; 11058d213e1aSmatthias.ringwald break; 11068d213e1aSmatthias.ringwald case HCI_POWER_OFF: 11074ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 11088d213e1aSmatthias.ringwald break; 11098d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 1110b546ac54Smatthias.ringwald // do nothing 11118d213e1aSmatthias.ringwald break; 11128d213e1aSmatthias.ringwald } 11138d213e1aSmatthias.ringwald break; 111411e23e5fSmatthias.ringwald } 111568d92d03Smatthias.ringwald 1116038bc64cSmatthias.ringwald // create internal event 1117ee8bf225Smatthias.ringwald hci_emit_state(); 1118ee8bf225Smatthias.ringwald 111968d92d03Smatthias.ringwald // trigger next/first action 112068d92d03Smatthias.ringwald hci_run(); 112168d92d03Smatthias.ringwald 1122475c8125Smatthias.ringwald return 0; 1123475c8125Smatthias.ringwald } 1124475c8125Smatthias.ringwald 1125758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1126758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 1127758b46ceSmatthias.ringwald hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 1128758b46ceSmatthias.ringwald hci_run(); 1129758b46ceSmatthias.ringwald } 1130758b46ceSmatthias.ringwald 1131381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1132381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1133381fbed8Smatthias.ringwald 1134381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 1135381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 1136381fbed8Smatthias.ringwald return; 1137381fbed8Smatthias.ringwald } 1138381fbed8Smatthias.ringwald 1139381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 1140758b46ceSmatthias.ringwald hci_update_scan_enable(); 1141758b46ceSmatthias.ringwald } 1142b031bebbSmatthias.ringwald 1143758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1144758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1145758b46ceSmatthias.ringwald 1146758b46ceSmatthias.ringwald // don't emit event 1147758b46ceSmatthias.ringwald if (hci_stack.connectable == enable) return; 1148758b46ceSmatthias.ringwald 1149758b46ceSmatthias.ringwald hci_stack.connectable = enable; 1150758b46ceSmatthias.ringwald hci_update_scan_enable(); 1151381fbed8Smatthias.ringwald } 1152381fbed8Smatthias.ringwald 11535061f3afS[email protected] bd_addr_t * hci_local_bd_addr(void){ 11545061f3afS[email protected] return &hci_stack.local_bd_addr; 11555061f3afS[email protected] } 11565061f3afS[email protected] 115706b35ec0Smatthias.ringwald void hci_run(){ 11588a485f27Smatthias.ringwald 115932ab9390Smatthias.ringwald hci_connection_t * connection; 116032ab9390Smatthias.ringwald linked_item_t * it; 116132ab9390Smatthias.ringwald 1162ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1163ce4c8fabSmatthias.ringwald 1164b031bebbSmatthias.ringwald // global/non-connection oriented commands 1165b031bebbSmatthias.ringwald 1166b031bebbSmatthias.ringwald // decline incoming connections 1167ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 1168ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 1169ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 1170ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1171dbe1a790S[email protected] return; 1172ce4c8fabSmatthias.ringwald } 1173ce4c8fabSmatthias.ringwald 1174b031bebbSmatthias.ringwald // send scan enable 117592368cd3S[email protected] if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff && hci_classic_supported()){ 1176b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1177b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 1178dbe1a790S[email protected] return; 1179b031bebbSmatthias.ringwald } 1180b031bebbSmatthias.ringwald 118132ab9390Smatthias.ringwald // send pending HCI commands 118232ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 118332ab9390Smatthias.ringwald 1184b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 118532ab9390Smatthias.ringwald 118632ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 11877b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 118832ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 118932ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1190dbe1a790S[email protected] return; 1191c7e0c5f6Smatthias.ringwald } 1192c7e0c5f6Smatthias.ringwald 119332ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 119432ab9390Smatthias.ringwald link_key_t link_key; 11957b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 119674d716b5S[email protected] if ( hci_stack.bondable && hci_stack.remote_device_db && hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 119732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 119832ab9390Smatthias.ringwald } else { 119932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 120032ab9390Smatthias.ringwald } 120128ca2b46S[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 1202dbe1a790S[email protected] return; 120332ab9390Smatthias.ringwald } 12041d6b20aeS[email protected] 1205*4c57c146S[email protected] if (connection->authentication_flags & HANDLE_PIN_CODE_REQUEST){ 1206*4c57c146S[email protected] log_info("denying to pin request\n"); 1207*4c57c146S[email protected] hci_send_cmd(&hci_pin_code_request_negative_reply, connection->address); 1208*4c57c146S[email protected] connectionClearAuthenticationFlags(connection, HANDLE_PIN_CODE_REQUEST); 1209*4c57c146S[email protected] return; 1210*4c57c146S[email protected] } 1211*4c57c146S[email protected] 1212dbe1a790S[email protected] if (connection->authentication_flags & SEND_IO_CAPABILITIES_REPLY){ 1213dbe1a790S[email protected] hci_send_cmd(&hci_io_capability_request_reply, &connection->address, hci_stack.ssp_io_capability, NULL, hci_stack.ssp_authentication_requirement); 1214dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_IO_CAPABILITIES_REPLY); 1215dbe1a790S[email protected] return; 121632ab9390Smatthias.ringwald } 121732ab9390Smatthias.ringwald 1218dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_CONFIRM_REPLY){ 1219dbe1a790S[email protected] hci_send_cmd(&hci_user_confirmation_request_reply, &connection->address); 1220dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_CONFIRM_REPLY); 1221dbe1a790S[email protected] return; 1222dbe1a790S[email protected] } 1223dbe1a790S[email protected] 1224dbe1a790S[email protected] if (connection->authentication_flags & SEND_USER_PASSKEY_REPLY){ 1225dbe1a790S[email protected] hci_send_cmd(&hci_user_passkey_request_reply, &connection->address, 000000); 1226dbe1a790S[email protected] connectionClearAuthenticationFlags(connection, SEND_USER_PASSKEY_REPLY); 1227dbe1a790S[email protected] return; 1228dbe1a790S[email protected] } 1229dbe1a790S[email protected] } 1230c7e0c5f6Smatthias.ringwald 12313429f56bSmatthias.ringwald switch (hci_stack.state){ 12323429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 12337b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 12343429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 12353429f56bSmatthias.ringwald // odd: waiting for command completion 123606b35ec0Smatthias.ringwald return; 12373429f56bSmatthias.ringwald } 123890919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1239c7492964Smatthias.ringwald case 0: // RESET 124022909952Smatthias.ringwald hci_send_cmd(&hci_reset); 124124052c2aS[email protected] 1242c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1243c7492964Smatthias.ringwald // skip baud change 1244c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1245c7492964Smatthias.ringwald } 12463429f56bSmatthias.ringwald break; 1247c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 12487dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 12497dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1250c7492964Smatthias.ringwald break; 1251c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1252c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1253c7492964Smatthias.ringwald hci_stack.substate += 2; 1254c7492964Smatthias.ringwald // break missing here for fall through 1255c7492964Smatthias.ringwald 1256c7492964Smatthias.ringwald case 3: 125724052c2aS[email protected] // Custom initialization 1258db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 12597dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1260d62aca9fSmatthias.ringwald if (valid_cmd){ 12617dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 12627dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1263c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 126490919203Smatthias.ringwald break; 126590919203Smatthias.ringwald } 1266d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 126790919203Smatthias.ringwald } 12682f6c30e1Smatthias.ringwald // otherwise continue 1269f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1270f432a6ddSmatthias.ringwald break; 1271c7492964Smatthias.ringwald case 4: 1272e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1273e2edc0c3Smatthias.ringwald break; 1274c7492964Smatthias.ringwald case 5: 127565389bfcS[email protected] hci_send_cmd(&hci_read_local_supported_features); 12763429f56bSmatthias.ringwald break; 1277c7492964Smatthias.ringwald case 6: 1278d7e0e3a8S[email protected] if (hci_le_supported()){ 1279d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x3FFFFFFF); 1280d7e0e3a8S[email protected] } else { 1281d7e0e3a8S[email protected] // Kensington Bluetoot 2.1 USB Dongle (CSR Chipset) returns an error for 0xffff... 1282d7e0e3a8S[email protected] hci_send_cmd(&hci_set_event_mask,0xffffffff, 0x1FFFFFFF); 1283d7e0e3a8S[email protected] } 1284f5d8d141S[email protected] 1285f5d8d141S[email protected] // skip Classic init commands for LE only chipsets 128624052c2aS[email protected] if (!hci_classic_supported()){ 128724052c2aS[email protected] if (hci_le_supported()){ 1288f5d8d141S[email protected] hci_stack.substate = 11 << 1; // skip all classic command 128924052c2aS[email protected] } else { 129024052c2aS[email protected] log_error("Neither BR/EDR nor LE supported"); 1291d7e0e3a8S[email protected] hci_stack.substate = 13 << 1; // skip all 129224052c2aS[email protected] } 1293f5d8d141S[email protected] } 1294f5d8d141S[email protected] break; 1295f5d8d141S[email protected] case 7: 129624052c2aS[email protected] if (hci_ssp_supported()){ 1297f5d8d141S[email protected] hci_send_cmd(&hci_write_simple_pairing_mode, hci_stack.ssp_enable); 1298f5d8d141S[email protected] break; 129924052c2aS[email protected] } 130024052c2aS[email protected] hci_stack.substate += 2; 130124052c2aS[email protected] // break missing here for fall through 130224052c2aS[email protected] 1303f5d8d141S[email protected] case 8: 130465389bfcS[email protected] // ca. 15 sec 130565389bfcS[email protected] hci_send_cmd(&hci_write_page_timeout, 0x6000); 1306559e517eS[email protected] break; 1307f5d8d141S[email protected] case 9: 1308e2386ba1S[email protected] hci_send_cmd(&hci_write_class_of_device, hci_stack.class_of_device); 1309e2386ba1S[email protected] break; 1310f5d8d141S[email protected] case 10: 1311e2386ba1S[email protected] if (hci_stack.local_name){ 1312e2386ba1S[email protected] hci_send_cmd(&hci_write_local_name, hci_stack.local_name); 1313e2386ba1S[email protected] } else { 13148a485f27Smatthias.ringwald char hostname[30]; 1315e2386ba1S[email protected] #ifdef EMBEDDED 1316e2386ba1S[email protected] // BTstack-11:22:33:44:55:66 1317e2386ba1S[email protected] strcpy(hostname, "BTstack "); 1318e2386ba1S[email protected] strcat(hostname, bd_addr_to_str(hci_stack.local_bd_addr)); 1319e2386ba1S[email protected] printf("---> Name %s\n", hostname); 1320e2386ba1S[email protected] #else 1321e2386ba1S[email protected] // hostname for POSIX systems 13228a485f27Smatthias.ringwald gethostname(hostname, 30); 13238a485f27Smatthias.ringwald hostname[29] = '\0'; 1324e2386ba1S[email protected] #endif 13258a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 13268a485f27Smatthias.ringwald } 13273c4d4b90Smatthias.ringwald break; 1328e0dbca83S[email protected] case 11: 1329a45d6b9fS[email protected] hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 133024052c2aS[email protected] if (!hci_le_supported()){ 133124052c2aS[email protected] // SKIP LE init for Classic only configuration 133224052c2aS[email protected] hci_stack.substate = 13 << 1; 133324052c2aS[email protected] } 1334a45d6b9fS[email protected] break; 133524052c2aS[email protected] 133643aa7007S[email protected] #ifdef HAVE_BLE 133724052c2aS[email protected] // LE INIT 1338a45d6b9fS[email protected] case 12: 133924052c2aS[email protected] hci_send_cmd(&hci_le_read_buffer_size); 134024052c2aS[email protected] break; 134124052c2aS[email protected] case 13: 134224052c2aS[email protected] // LE Supported Host = 1, Simultaneous Host = 0 134324052c2aS[email protected] hci_send_cmd(&hci_write_le_host_supported, 1, 0); 134424052c2aS[email protected] break; 134543aa7007S[email protected] #endif 134624052c2aS[email protected] 134724052c2aS[email protected] // DONE 134824052c2aS[email protected] case 14: 13493429f56bSmatthias.ringwald // done. 13503429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1351b360b6adSmatthias.ringwald hci_emit_state(); 13523429f56bSmatthias.ringwald break; 13533429f56bSmatthias.ringwald default: 13543429f56bSmatthias.ringwald break; 1355475c8125Smatthias.ringwald } 13563429f56bSmatthias.ringwald hci_stack.substate++; 13573429f56bSmatthias.ringwald break; 1358c7e0c5f6Smatthias.ringwald 1359c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1360c7e0c5f6Smatthias.ringwald 13617b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1362c7e0c5f6Smatthias.ringwald // close all open connections 1363c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1364c7e0c5f6Smatthias.ringwald if (connection){ 136532ab9390Smatthias.ringwald 1366c7e0c5f6Smatthias.ringwald // send disconnect 136732ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 136832ab9390Smatthias.ringwald 136979bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 13706ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1371c7e0c5f6Smatthias.ringwald 1372c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1373c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1374c7e0c5f6Smatthias.ringwald return; 1375c7e0c5f6Smatthias.ringwald } 13767b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1377c7e0c5f6Smatthias.ringwald 137872ea5239Smatthias.ringwald // switch mode 1379c7e0c5f6Smatthias.ringwald hci_power_control_off(); 13809418f9c9Smatthias.ringwald 13817b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 138272ea5239Smatthias.ringwald hci_emit_state(); 13837b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 138472ea5239Smatthias.ringwald break; 1385c7e0c5f6Smatthias.ringwald 138672ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 138789db417bSmatthias.ringwald switch(hci_stack.substate) { 138889db417bSmatthias.ringwald case 0: 13897b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 139072ea5239Smatthias.ringwald // close all open connections 139172ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 139266da7044Smatthias.ringwald 139328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 139466da7044Smatthias.ringwald // don't close connections, if H4 supports power management 139566da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 139666da7044Smatthias.ringwald connection = NULL; 139766da7044Smatthias.ringwald } 139866da7044Smatthias.ringwald #endif 139972ea5239Smatthias.ringwald if (connection){ 140032ab9390Smatthias.ringwald 140172ea5239Smatthias.ringwald // send disconnect 140232ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 140332ab9390Smatthias.ringwald 140479bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 14056ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 140672ea5239Smatthias.ringwald 140772ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 140872ea5239Smatthias.ringwald hci_shutdown_connection(connection); 140972ea5239Smatthias.ringwald return; 141072ea5239Smatthias.ringwald } 141172ea5239Smatthias.ringwald 141292368cd3S[email protected] if (hci_classic_supported()){ 141389db417bSmatthias.ringwald // disable page and inquiry scan 141432ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 141532ab9390Smatthias.ringwald 141692368cd3S[email protected] log_info("HCI_STATE_HALTING, disabling inq scans\n"); 1417758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 141889db417bSmatthias.ringwald 141989db417bSmatthias.ringwald // continue in next sub state 142089db417bSmatthias.ringwald hci_stack.substate++; 142189db417bSmatthias.ringwald break; 142292368cd3S[email protected] } 142392368cd3S[email protected] // fall through for ble-only chips 142492368cd3S[email protected] 142589db417bSmatthias.ringwald case 2: 14267b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 142728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 142828171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 142928171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 143028171530Smatthias.ringwald // SLEEP MODE reached 143128171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 143228171530Smatthias.ringwald hci_emit_state(); 143328171530Smatthias.ringwald break; 143428171530Smatthias.ringwald } 143528171530Smatthias.ringwald #endif 143672ea5239Smatthias.ringwald // switch mode 143789db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1438c7e0c5f6Smatthias.ringwald hci_emit_state(); 143928171530Smatthias.ringwald break; 144028171530Smatthias.ringwald 144189db417bSmatthias.ringwald default: 144289db417bSmatthias.ringwald break; 144389db417bSmatthias.ringwald } 1444c7e0c5f6Smatthias.ringwald break; 1445c7e0c5f6Smatthias.ringwald 14463429f56bSmatthias.ringwald default: 14473429f56bSmatthias.ringwald break; 14481f504dbdSmatthias.ringwald } 14493429f56bSmatthias.ringwald } 145016833f0aSmatthias.ringwald 145131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1452c8e4258aSmatthias.ringwald bd_addr_t addr; 1453c8e4258aSmatthias.ringwald hci_connection_t * conn; 1454c8e4258aSmatthias.ringwald // house-keeping 1455c8e4258aSmatthias.ringwald 1456c8e4258aSmatthias.ringwald // create_connection? 1457c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1458c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1459339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1460c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1461c8e4258aSmatthias.ringwald if (conn) { 1462c8e4258aSmatthias.ringwald // if connection exists 1463c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 146417f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 146517f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1466c8e4258aSmatthias.ringwald } 146717f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1468c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1469c8e4258aSmatthias.ringwald 147017f1ba2aSmatthias.ringwald } 1471c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 147217f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 147317f1ba2aSmatthias.ringwald if (!conn){ 147417f1ba2aSmatthias.ringwald // notify client that alloc failed 147517f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 147617f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 147717f1ba2aSmatthias.ringwald } 1478c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1479c8e4258aSmatthias.ringwald } 1480c8e4258aSmatthias.ringwald 14817fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 14827fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 14837fde4af9Smatthias.ringwald } 14847fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 14857fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 14867fde4af9Smatthias.ringwald } 14877fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 14887fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 14897fde4af9Smatthias.ringwald } 14907fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 14917fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 14927fde4af9Smatthias.ringwald } 14937fde4af9Smatthias.ringwald 14948ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 14958ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 14968ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 14978ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 14988ef73945Smatthias.ringwald } 14998ef73945Smatthias.ringwald } 1500c8e4258aSmatthias.ringwald 150169a97523S[email protected] #ifdef HAVE_BLE 150269a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_advertising_parameters)){ 150369a97523S[email protected] hci_stack.adv_addr_type = packet[8]; 150469a97523S[email protected] } 150569a97523S[email protected] if (IS_COMMAND(packet, hci_le_set_random_address)){ 150669a97523S[email protected] bt_flip_addr(hci_stack.adv_address, &packet[3]); 150769a97523S[email protected] } 150869a97523S[email protected] #endif 150969a97523S[email protected] 151069a97523S[email protected] 151131452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1512622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 151331452debSmatthias.ringwald } 15148adf0ddaSmatthias.ringwald 1515dbe1a790S[email protected] // Configure Secure Simple Pairing 1516dbe1a790S[email protected] 1517dbe1a790S[email protected] // enable will enable SSP during init 1518dbe1a790S[email protected] void hci_ssp_set_enable(int enable){ 1519dbe1a790S[email protected] hci_stack.ssp_enable = enable; 1520dbe1a790S[email protected] } 1521dbe1a790S[email protected] 1522dbe1a790S[email protected] // if set, BTstack will respond to io capability request using authentication requirement 1523dbe1a790S[email protected] void hci_ssp_set_io_capability(int io_capability){ 1524dbe1a790S[email protected] hci_stack.ssp_io_capability = io_capability; 1525dbe1a790S[email protected] } 1526dbe1a790S[email protected] void hci_ssp_set_authentication_requirement(int authentication_requirement){ 1527dbe1a790S[email protected] hci_stack.ssp_authentication_requirement = authentication_requirement; 1528dbe1a790S[email protected] } 1529dbe1a790S[email protected] 1530dbe1a790S[email protected] // if set, BTstack will confirm a numberic comparion and enter '000000' if requested 1531dbe1a790S[email protected] void hci_ssp_set_auto_accept(int auto_accept){ 1532dbe1a790S[email protected] hci_stack.ssp_auto_accept = auto_accept; 1533dbe1a790S[email protected] } 1534dbe1a790S[email protected] 15351cd208adSmatthias.ringwald /** 15361cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 15371cd208adSmatthias.ringwald */ 1538fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 15391cd208adSmatthias.ringwald va_list argptr; 15401cd208adSmatthias.ringwald va_start(argptr, cmd); 15417dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 15421cd208adSmatthias.ringwald va_end(argptr); 15437dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 154493b8dc03Smatthias.ringwald } 1545c8e4258aSmatthias.ringwald 1546ee091cf1Smatthias.ringwald // Create various non-HCI events. 1547ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1548ee091cf1Smatthias.ringwald 1549c8e4258aSmatthias.ringwald void hci_emit_state(){ 1550e0abb8e7S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack.state); 1551425d1371Smatthias.ringwald uint8_t event[3]; 155280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1553425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1554c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1555425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1556425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1557c8e4258aSmatthias.ringwald } 1558c8e4258aSmatthias.ringwald 155917f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1560425d1371Smatthias.ringwald uint8_t event[13]; 1561c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1562425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 156317f1ba2aSmatthias.ringwald event[2] = status; 1564c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1565c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1566c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1567c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1568425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1569425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1570c8e4258aSmatthias.ringwald } 1571c8e4258aSmatthias.ringwald 15723c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1573425d1371Smatthias.ringwald uint8_t event[6]; 15743c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1575e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 15763c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 15773c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 15783c4d4b90Smatthias.ringwald event[5] = reason; 1579425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1580425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 15813c4d4b90Smatthias.ringwald } 15823c4d4b90Smatthias.ringwald 1583ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1584e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1585425d1371Smatthias.ringwald uint8_t event[4]; 158680d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1587425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1588ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1589425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1590425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1591ee091cf1Smatthias.ringwald } 159243bfb1bdSmatthias.ringwald 159343bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1594e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1595425d1371Smatthias.ringwald uint8_t event[3]; 159680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1597425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 159843bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1599425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1600425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 160143bfb1bdSmatthias.ringwald } 1602038bc64cSmatthias.ringwald 1603038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1604e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1605425d1371Smatthias.ringwald uint8_t event[2]; 160680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1607425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1608425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1609425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1610038bc64cSmatthias.ringwald } 16111b0e3922Smatthias.ringwald 161209ba8edeSmatthias.ringwald #ifndef EMBEDDED 16131b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1614e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1615425d1371Smatthias.ringwald uint8_t event[6]; 16161b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1617425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1618425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1619425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1620425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1621425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1622425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 16231b0e3922Smatthias.ringwald } 162409ba8edeSmatthias.ringwald #endif 16251b0e3922Smatthias.ringwald 16262ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1627e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1628425d1371Smatthias.ringwald uint8_t event[3]; 16292ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1630425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 16312ed6235cSmatthias.ringwald event[2] = enabled; 1632425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1633e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 16342ed6235cSmatthias.ringwald } 1635627c2f45Smatthias.ringwald 1636627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1637e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1638627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1639e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1640f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 16412f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1642f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1643e0abb8e7S[email protected] 1644e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1645e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1646e0abb8e7S[email protected] 1647e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 1648e0abb8e7S[email protected] hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1649627c2f45Smatthias.ringwald } 1650381fbed8Smatthias.ringwald 1651381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1652e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1653425d1371Smatthias.ringwald uint8_t event[3]; 1654381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1655425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1656381fbed8Smatthias.ringwald event[2] = enabled; 1657425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1658425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1659381fbed8Smatthias.ringwald } 1660458bf4e8S[email protected] 1661458bf4e8S[email protected] // GAP API 1662458bf4e8S[email protected] /** 1663458bf4e8S[email protected] * @bbrief enable/disable bonding. default is enabled 1664458bf4e8S[email protected] * @praram enabled 1665458bf4e8S[email protected] */ 1666*4c57c146S[email protected] void gap_set_bondable_mode(int enable){ 1667458bf4e8S[email protected] hci_stack.bondable = enable ? 1 : 0; 1668458bf4e8S[email protected] } 1669