11f504dbdSmatthias.ringwald /* 26b64433eSmatthias.ringwald * Copyright (C) 2009-2012 by Matthias Ringwald 31713bceaSmatthias.ringwald * 41713bceaSmatthias.ringwald * Redistribution and use in source and binary forms, with or without 51713bceaSmatthias.ringwald * modification, are permitted provided that the following conditions 61713bceaSmatthias.ringwald * are met: 71713bceaSmatthias.ringwald * 81713bceaSmatthias.ringwald * 1. Redistributions of source code must retain the above copyright 91713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer. 101713bceaSmatthias.ringwald * 2. Redistributions in binary form must reproduce the above copyright 111713bceaSmatthias.ringwald * notice, this list of conditions and the following disclaimer in the 121713bceaSmatthias.ringwald * documentation and/or other materials provided with the distribution. 131713bceaSmatthias.ringwald * 3. Neither the name of the copyright holders nor the names of 141713bceaSmatthias.ringwald * contributors may be used to endorse or promote products derived 151713bceaSmatthias.ringwald * from this software without specific prior written permission. 166b64433eSmatthias.ringwald * 4. Any redistribution, use, or modification is done solely for 176b64433eSmatthias.ringwald * personal benefit and not for any commercial purpose or for 186b64433eSmatthias.ringwald * monetary gain. 191713bceaSmatthias.ringwald * 201713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 211713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 221713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 231713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 241713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 251713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 261713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 271713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 281713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 291713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 301713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311713bceaSmatthias.ringwald * SUCH DAMAGE. 321713bceaSmatthias.ringwald * 336b64433eSmatthias.ringwald * Please inquire about commercial licensing options at [email protected] 346b64433eSmatthias.ringwald * 351713bceaSmatthias.ringwald */ 361713bceaSmatthias.ringwald 371713bceaSmatthias.ringwald /* 381f504dbdSmatthias.ringwald * hci.c 391f504dbdSmatthias.ringwald * 401f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 411f504dbdSmatthias.ringwald * 421f504dbdSmatthias.ringwald */ 431f504dbdSmatthias.ringwald 44c8901d41Smatthias.ringwald #include "config.h" 4528171530Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "hci.h" 477f2435e6Smatthias.ringwald 4893b8dc03Smatthias.ringwald #include <stdarg.h> 4993b8dc03Smatthias.ringwald #include <string.h> 5056fe0872Smatthias.ringwald #include <stdio.h> 517f2435e6Smatthias.ringwald 52549e6ebeSmatthias.ringwald #ifndef EMBEDDED 53549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 5409ba8edeSmatthias.ringwald #include <btstack/version.h> 55549e6ebeSmatthias.ringwald #endif 56549e6ebeSmatthias.ringwald 57a3b02b71Smatthias.ringwald #include "btstack_memory.h" 587f2435e6Smatthias.ringwald #include "debug.h" 59d8905019Smatthias.ringwald #include "hci_dump.h" 6093b8dc03Smatthias.ringwald 61ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 621b0e3922Smatthias.ringwald 63169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 64ee091cf1Smatthias.ringwald 6528171530Smatthias.ringwald #ifdef USE_BLUETOOL 6628171530Smatthias.ringwald #include "bt_control_iphone.h" 6728171530Smatthias.ringwald #endif 6828171530Smatthias.ringwald 69758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 70758b46ceSmatthias.ringwald 7106b35ec0Smatthias.ringwald // the STACK is here 7216833f0aSmatthias.ringwald static hci_stack_t hci_stack; 7316833f0aSmatthias.ringwald 7497addcc5Smatthias.ringwald /** 75ee091cf1Smatthias.ringwald * get connection for a given handle 76ee091cf1Smatthias.ringwald * 77ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 78ee091cf1Smatthias.ringwald */ 79645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 80ee091cf1Smatthias.ringwald linked_item_t *it; 81ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 82ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 83ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 84ee091cf1Smatthias.ringwald } 85ee091cf1Smatthias.ringwald } 86ee091cf1Smatthias.ringwald return NULL; 87ee091cf1Smatthias.ringwald } 88ee091cf1Smatthias.ringwald 892b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 9028ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 91c785ef68Smatthias.ringwald #ifdef HAVE_TIME 92ee091cf1Smatthias.ringwald struct timeval tv; 93ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 94c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 95ee091cf1Smatthias.ringwald // connections might be timed out 96ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 97ee091cf1Smatthias.ringwald } 982b12a0b9Smatthias.ringwald #endif 99e5780900Smatthias.ringwald #ifdef HAVE_TICK 100c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 101c785ef68Smatthias.ringwald // connections might be timed out 102c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 103c785ef68Smatthias.ringwald } 104c785ef68Smatthias.ringwald #endif 105c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 106c785ef68Smatthias.ringwald run_loop_add_timer(timer); 107c785ef68Smatthias.ringwald } 108ee091cf1Smatthias.ringwald 109ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 110c7492964Smatthias.ringwald #ifdef HAVE_TIME 111ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 112c7492964Smatthias.ringwald #endif 113e5780900Smatthias.ringwald #ifdef HAVE_TICK 114c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 115c785ef68Smatthias.ringwald #endif 116ee091cf1Smatthias.ringwald } 117ee091cf1Smatthias.ringwald 118ee091cf1Smatthias.ringwald /** 119c8e4258aSmatthias.ringwald * create connection for given address 120c8e4258aSmatthias.ringwald * 12117f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 122c8e4258aSmatthias.ringwald */ 123c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 12428ca2b46S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 125c8e4258aSmatthias.ringwald if (!conn) return NULL; 126c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 127c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1287d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 129ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 130ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 131ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 132d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1337856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 13456cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 135c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 136c8e4258aSmatthias.ringwald return conn; 137c8e4258aSmatthias.ringwald } 138c8e4258aSmatthias.ringwald 139c8e4258aSmatthias.ringwald /** 14006b35ec0Smatthias.ringwald * get connection for given address 14197addcc5Smatthias.ringwald * 14297addcc5Smatthias.ringwald * @return connection OR NULL, if not found 14397addcc5Smatthias.ringwald */ 144fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 14506b35ec0Smatthias.ringwald linked_item_t *it; 14606b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 14706b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 14806b35ec0Smatthias.ringwald return (hci_connection_t *) it; 14906b35ec0Smatthias.ringwald } 15006b35ec0Smatthias.ringwald } 15106b35ec0Smatthias.ringwald return NULL; 15206b35ec0Smatthias.ringwald } 15306b35ec0Smatthias.ringwald 15428ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 15528ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 15628ca2b46S[email protected] } 15728ca2b46S[email protected] 15828ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 15928ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 16028ca2b46S[email protected] } 16128ca2b46S[email protected] 16228ca2b46S[email protected] 16343bfb1bdSmatthias.ringwald /** 16480ca58a0Smatthias.ringwald * add authentication flags and reset timer 1657fde4af9Smatthias.ringwald */ 1667fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1677fde4af9Smatthias.ringwald bd_addr_t addr; 1687fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1697fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1707fde4af9Smatthias.ringwald if (conn) { 17128ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 17280ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1737fde4af9Smatthias.ringwald } 1747fde4af9Smatthias.ringwald } 1757fde4af9Smatthias.ringwald 17680ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 17780ca58a0Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 17880ca58a0Smatthias.ringwald if (!conn) return 0; 17980ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 18080ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 18180ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 18280ca58a0Smatthias.ringwald return 1; 18380ca58a0Smatthias.ringwald } 18480ca58a0Smatthias.ringwald 185c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 186c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 187c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 188c12e46e7Smatthias.ringwald } 189c12e46e7Smatthias.ringwald } 190c12e46e7Smatthias.ringwald 1917fde4af9Smatthias.ringwald 1927fde4af9Smatthias.ringwald /** 19343bfb1bdSmatthias.ringwald * count connections 19443bfb1bdSmatthias.ringwald */ 19540d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 19656c253c9Smatthias.ringwald int count = 0; 19743bfb1bdSmatthias.ringwald linked_item_t *it; 19843bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 19943bfb1bdSmatthias.ringwald return count; 20043bfb1bdSmatthias.ringwald } 201c8e4258aSmatthias.ringwald 20297addcc5Smatthias.ringwald /** 203ba681a6cSmatthias.ringwald * Dummy handler called by HCI 20416833f0aSmatthias.ringwald */ 2052718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 20616833f0aSmatthias.ringwald } 20716833f0aSmatthias.ringwald 208998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 209998906cdSmatthias.ringwald hci_connection_t * connection = connection_for_handle(handle); 210998906cdSmatthias.ringwald if (!connection) { 2117d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 212998906cdSmatthias.ringwald return 0; 213998906cdSmatthias.ringwald } 214998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 215998906cdSmatthias.ringwald } 216998906cdSmatthias.ringwald 217998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 218998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 219998906cdSmatthias.ringwald linked_item_t *it; 220998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 221998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 222998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2237d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 224998906cdSmatthias.ringwald return 0; 225998906cdSmatthias.ringwald } 226998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 227998906cdSmatthias.ringwald } 228998906cdSmatthias.ringwald return free_slots; 229998906cdSmatthias.ringwald } 230998906cdSmatthias.ringwald 231c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2322b12a0b9Smatthias.ringwald 2332b12a0b9Smatthias.ringwald // check for async hci transport implementations 2342b12a0b9Smatthias.ringwald if (hci_stack.hci_transport->can_send_packet_now){ 2352b12a0b9Smatthias.ringwald if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 2362b12a0b9Smatthias.ringwald return 0; 2372b12a0b9Smatthias.ringwald } 2382b12a0b9Smatthias.ringwald } 2392b12a0b9Smatthias.ringwald 2402b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 241c24735b1Smatthias.ringwald switch (packet_type) { 242c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 243c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 244c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 245de009a8cSmatthias.ringwald return hci_stack.num_cmd_packets; 246c24735b1Smatthias.ringwald default: 247c24735b1Smatthias.ringwald return 0; 248c24735b1Smatthias.ringwald } 249c24735b1Smatthias.ringwald } 250c24735b1Smatthias.ringwald 251ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2527856c818Smatthias.ringwald 2536218e6f1Smatthias.ringwald // check for free places on BT module 2545932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2556218e6f1Smatthias.ringwald 2567856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2577856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 25856cf178bSmatthias.ringwald if (!connection) return 0; 25956cf178bSmatthias.ringwald hci_connection_timestamp(connection); 26056cf178bSmatthias.ringwald 26156cf178bSmatthias.ringwald // count packet 26256cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2637b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2647856c818Smatthias.ringwald 26500d8e42eSmatthias.ringwald // send packet 26600d8e42eSmatthias.ringwald int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2676218e6f1Smatthias.ringwald 26800d8e42eSmatthias.ringwald return err; 269ee091cf1Smatthias.ringwald } 270ee091cf1Smatthias.ringwald 27116833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2727856c818Smatthias.ringwald 2737856c818Smatthias.ringwald // get info 2747856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2757856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 2767856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2777856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2787856c818Smatthias.ringwald 2797856c818Smatthias.ringwald // ignore non-registered handle 2807856c818Smatthias.ringwald if (!conn){ 2817d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2827856c818Smatthias.ringwald return; 2837856c818Smatthias.ringwald } 2847856c818Smatthias.ringwald 2857856c818Smatthias.ringwald // update idle timestamp 2867856c818Smatthias.ringwald hci_connection_timestamp(conn); 2877856c818Smatthias.ringwald 2887856c818Smatthias.ringwald // handle different packet types 2897856c818Smatthias.ringwald switch (acl_flags & 0x03) { 2907856c818Smatthias.ringwald 2917856c818Smatthias.ringwald case 0x01: // continuation fragment 2927856c818Smatthias.ringwald 2937856c818Smatthias.ringwald // sanity check 2947856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 2957d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 2967856c818Smatthias.ringwald return; 2977856c818Smatthias.ringwald } 2987856c818Smatthias.ringwald 2997856c818Smatthias.ringwald // append fragment payload (header already stored) 3007856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 3017856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 3027856c818Smatthias.ringwald 3037d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 304decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3057856c818Smatthias.ringwald 3067856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3077856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3087856c818Smatthias.ringwald 3092718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3107856c818Smatthias.ringwald // reset recombination buffer 3117856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3127856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3137856c818Smatthias.ringwald } 3147856c818Smatthias.ringwald break; 3157856c818Smatthias.ringwald 3167856c818Smatthias.ringwald case 0x02: { // first fragment 3177856c818Smatthias.ringwald 3187856c818Smatthias.ringwald // sanity check 3197856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3207d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3217856c818Smatthias.ringwald return; 3227856c818Smatthias.ringwald } 3237856c818Smatthias.ringwald 3247856c818Smatthias.ringwald // peek into L2CAP packet! 3257856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3267856c818Smatthias.ringwald 3277d67539fSmatthias.ringwald // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 328decc01a8Smatthias.ringwald 3297856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3307856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3317856c818Smatthias.ringwald 3327856c818Smatthias.ringwald // forward fragment as L2CAP packet 3332718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3347856c818Smatthias.ringwald 3357856c818Smatthias.ringwald } else { 3367856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3377856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3387856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3397856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 340decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3417856c818Smatthias.ringwald } 3427856c818Smatthias.ringwald break; 3437856c818Smatthias.ringwald 3447856c818Smatthias.ringwald } 3457856c818Smatthias.ringwald default: 3467d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3477856c818Smatthias.ringwald return; 3487856c818Smatthias.ringwald } 34994ab26f8Smatthias.ringwald 35094ab26f8Smatthias.ringwald // execute main loop 35194ab26f8Smatthias.ringwald hci_run(); 35216833f0aSmatthias.ringwald } 35322909952Smatthias.ringwald 35467a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 355339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3563c4d4b90Smatthias.ringwald 3573c4d4b90Smatthias.ringwald // cancel all l2cap connections 3583c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3593c4d4b90Smatthias.ringwald 360c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 361c785ef68Smatthias.ringwald 362c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 363a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3643c4d4b90Smatthias.ringwald 3653c4d4b90Smatthias.ringwald // now it's gone 366c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 367c7e0c5f6Smatthias.ringwald } 368c7e0c5f6Smatthias.ringwald 3690c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3708f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3718f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3728f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3738f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3748f8108aaSmatthias.ringwald }; 3758f8108aaSmatthias.ringwald 3768f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){ 3778f8108aaSmatthias.ringwald uint16_t packet_types = 0; 3788f8108aaSmatthias.ringwald int i; 3798f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 3808f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 3818f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 3828f8108aaSmatthias.ringwald packet_types |= 1 << i; 3838f8108aaSmatthias.ringwald } 3848f8108aaSmatthias.ringwald } 3858f8108aaSmatthias.ringwald // flip bits for "may not be used" 3868f8108aaSmatthias.ringwald packet_types ^= 0x3306; 3878f8108aaSmatthias.ringwald return packet_types; 3888f8108aaSmatthias.ringwald } 3898f8108aaSmatthias.ringwald 3908f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 3918f8108aaSmatthias.ringwald return hci_stack.packet_types; 3928f8108aaSmatthias.ringwald } 3938f8108aaSmatthias.ringwald 3947dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 3957dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 3967dc17943Smatthias.ringwald return hci_stack.hci_packet_buffer; 3977dc17943Smatthias.ringwald } 3987dc17943Smatthias.ringwald 3997dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){ 4007dc17943Smatthias.ringwald return hci_stack.acl_data_packet_length; 4017dc17943Smatthias.ringwald } 4027dc17943Smatthias.ringwald 40374ec757aSmatthias.ringwald // avoid huge local variables 404223aafc1Smatthias.ringwald #ifndef EMBEDDED 40574ec757aSmatthias.ringwald static device_name_t device_name; 406223aafc1Smatthias.ringwald #endif 40716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 4081281a47eSmatthias.ringwald bd_addr_t addr; 4092d00edd4Smatthias.ringwald uint8_t link_type; 410fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 4111f7b95a1Smatthias.ringwald hci_connection_t * conn; 41256cf178bSmatthias.ringwald int i; 41322909952Smatthias.ringwald 4145909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 4155909f7f2Smatthias.ringwald 4166772a24cSmatthias.ringwald switch (packet[0]) { 41722909952Smatthias.ringwald 4186772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4197ec5eeaaSmatthias.ringwald // get num cmd packets 4207b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4217ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4227ec5eeaaSmatthias.ringwald 423e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 424e2edc0c3Smatthias.ringwald // from offset 5 425e2edc0c3Smatthias.ringwald // status 4261d279b20Smatthias.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" 427e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 42856cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 429e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 43056cf178bSmatthias.ringwald // ignore: total num SCO packets 43156cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 432a7a04bd9Smatthias.ringwald // determine usable ACL payload size 4338fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 4348fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 4358f8108aaSmatthias.ringwald } 436a7a04bd9Smatthias.ringwald // determine usable ACL packet types 43728c93ceeSmatthias.ringwald hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); 4388f8108aaSmatthias.ringwald 43997fdcd42Smatthias.ringwald log_info("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", 4408f8108aaSmatthias.ringwald hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); 441e2edc0c3Smatthias.ringwald } 44256cf178bSmatthias.ringwald } 443188981d2Smatthias.ringwald // Dump local address 444188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 445188981d2Smatthias.ringwald bd_addr_t addr; 446188981d2Smatthias.ringwald bt_flip_addr(addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 447188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 448188981d2Smatthias.ringwald packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(addr)); 449188981d2Smatthias.ringwald } 450381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 451381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 452381fbed8Smatthias.ringwald } 453559e517eS[email protected] if (COMMAND_COMPLETE_EVENT(packet, hci_read_local_supported_features)){ 454559e517eS[email protected] memcpy(hci_stack.local_supported_features, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], 8); 455559e517eS[email protected] log_info("Local Supported Features: 0x%02x%02x%02x%02x%02x%02x%02x%02x", 456559e517eS[email protected] hci_stack.local_supported_features[0], hci_stack.local_supported_features[1], 457559e517eS[email protected] hci_stack.local_supported_features[2], hci_stack.local_supported_features[3], 458559e517eS[email protected] hci_stack.local_supported_features[4], hci_stack.local_supported_features[5], 459559e517eS[email protected] hci_stack.local_supported_features[6], hci_stack.local_supported_features[7]); 460559e517eS[email protected] } 46156cf178bSmatthias.ringwald break; 46256cf178bSmatthias.ringwald 4637ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 4647ec5eeaaSmatthias.ringwald // get num cmd packets 4657b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 4667ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 4677ec5eeaaSmatthias.ringwald break; 4687ec5eeaaSmatthias.ringwald 46956cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 47056cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 47156cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 47256cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 47356cf178bSmatthias.ringwald conn = connection_for_handle(handle); 47456cf178bSmatthias.ringwald if (!conn){ 4757d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 47656cf178bSmatthias.ringwald continue; 47756cf178bSmatthias.ringwald } 47856cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 4797b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 48056cf178bSmatthias.ringwald } 4816772a24cSmatthias.ringwald break; 4826772a24cSmatthias.ringwald 4831f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 48437eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 48537eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 4862d00edd4Smatthias.ringwald link_type = packet[11]; 487339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 48837eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 4891f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 4901f7b95a1Smatthias.ringwald if (!conn) { 4911f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 4921f7b95a1Smatthias.ringwald } 493ce4c8fabSmatthias.ringwald if (!conn) { 494ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 495ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 496ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 497ce4c8fabSmatthias.ringwald break; 498ce4c8fabSmatthias.ringwald } 49932ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 50032ab9390Smatthias.ringwald hci_run(); 50137eaa4cfSmatthias.ringwald } else { 502ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 503ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 504ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 50537eaa4cfSmatthias.ringwald } 5061f7b95a1Smatthias.ringwald break; 5071f7b95a1Smatthias.ringwald 5086772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 509fe1ed1b8Smatthias.ringwald // Connection management 510fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 511339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 5121f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 513fe1ed1b8Smatthias.ringwald if (conn) { 514b448a0e7Smatthias.ringwald if (!packet[2]){ 515c8e4258aSmatthias.ringwald conn->state = OPEN; 516fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 517ee091cf1Smatthias.ringwald 518c785ef68Smatthias.ringwald // restart timer 519c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 520ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 521c785ef68Smatthias.ringwald 522339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 52343bfb1bdSmatthias.ringwald 52443bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 525b448a0e7Smatthias.ringwald } else { 526b448a0e7Smatthias.ringwald // connection failed, remove entry 527b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 528a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 529c12e46e7Smatthias.ringwald 530c12e46e7Smatthias.ringwald // if authentication error, also delete link key 531c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 532c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 533c12e46e7Smatthias.ringwald } 534fe1ed1b8Smatthias.ringwald } 535fe1ed1b8Smatthias.ringwald } 5366772a24cSmatthias.ringwald break; 537fe1ed1b8Smatthias.ringwald 5387fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 5397b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 5407fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 54174ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 54232ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 54364472d52Smatthias.ringwald hci_run(); 544d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 54529d53098Smatthias.ringwald return; 5467fde4af9Smatthias.ringwald 5477fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 5487fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 54974ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 55029d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 551287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 55229d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 5537fde4af9Smatthias.ringwald break; 5547fde4af9Smatthias.ringwald 5557fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 5567fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 557d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 558d9a327f9Smatthias.ringwald if (!hci_stack.remote_device_db) break; 559d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 560d9a327f9Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 5617fde4af9Smatthias.ringwald break; 5627fde4af9Smatthias.ringwald 563223aafc1Smatthias.ringwald #ifndef EMBEDDED 56474ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 56574ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 56674ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 56774ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 5685a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 5695a06394aSmatthias.ringwald for (i=0; i<248;i++){ 5705a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 5715a06394aSmatthias.ringwald packet[9+i] = 0; 5725a06394aSmatthias.ringwald break; 573cdc9101dSmatthias.ringwald } 5745a06394aSmatthias.ringwald } 575d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 57674ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 57774ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 57874ec757aSmatthias.ringwald break; 57974ec757aSmatthias.ringwald 58074ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 58174ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 58274ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 58374ec757aSmatthias.ringwald // first send inq result packet 58474ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 58574ec757aSmatthias.ringwald // then send cached remote names 58674ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 58774ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 58874ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 58974ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 59074ec757aSmatthias.ringwald } 59174ec757aSmatthias.ringwald } 59274ec757aSmatthias.ringwald return; 593223aafc1Smatthias.ringwald #endif 59474ec757aSmatthias.ringwald 5956772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 596fe1ed1b8Smatthias.ringwald if (!packet[2]){ 597fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 598fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 599fe1ed1b8Smatthias.ringwald if (conn) { 600c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 601fe1ed1b8Smatthias.ringwald } 602fe1ed1b8Smatthias.ringwald } 6036772a24cSmatthias.ringwald break; 6046772a24cSmatthias.ringwald 605c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 606c68bdf90Smatthias.ringwald if(hci_stack.control->hw_error){ 607c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 608c68bdf90Smatthias.ringwald } 609c68bdf90Smatthias.ringwald break; 610c68bdf90Smatthias.ringwald 6115909f7f2Smatthias.ringwald #ifdef HAVE_BLE 6125909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 6135909f7f2Smatthias.ringwald switch (packet[2]) { 6145909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 6155909f7f2Smatthias.ringwald // Connection management 6165909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 6175909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 6185909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 6195909f7f2Smatthias.ringwald conn = connection_for_address(addr); 6205909f7f2Smatthias.ringwald if (packet[3]){ 6215909f7f2Smatthias.ringwald if (conn){ 6225909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 6235909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 6245909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 6255909f7f2Smatthias.ringwald 6265909f7f2Smatthias.ringwald } 6275909f7f2Smatthias.ringwald // if authentication error, also delete link key 6285909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 6295909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 6305909f7f2Smatthias.ringwald } 6315909f7f2Smatthias.ringwald break; 6325909f7f2Smatthias.ringwald } 6335909f7f2Smatthias.ringwald if (!conn){ 6345909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 6355909f7f2Smatthias.ringwald } 6365909f7f2Smatthias.ringwald if (!conn){ 6375909f7f2Smatthias.ringwald // no memory 6385909f7f2Smatthias.ringwald break; 6395909f7f2Smatthias.ringwald } 6405909f7f2Smatthias.ringwald 6415909f7f2Smatthias.ringwald conn->state = OPEN; 6425909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 6435909f7f2Smatthias.ringwald 6445909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 6455909f7f2Smatthias.ringwald 6465909f7f2Smatthias.ringwald // restart timer 6475909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 6485909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 6495909f7f2Smatthias.ringwald 6505909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 6515909f7f2Smatthias.ringwald 6525909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 6535909f7f2Smatthias.ringwald break; 6545909f7f2Smatthias.ringwald 6555909f7f2Smatthias.ringwald default: 6565909f7f2Smatthias.ringwald break; 6575909f7f2Smatthias.ringwald } 6585909f7f2Smatthias.ringwald break; 6595909f7f2Smatthias.ringwald #endif 6605909f7f2Smatthias.ringwald 6616772a24cSmatthias.ringwald default: 6626772a24cSmatthias.ringwald break; 663fe1ed1b8Smatthias.ringwald } 664fe1ed1b8Smatthias.ringwald 6653429f56bSmatthias.ringwald // handle BT initialization 6663429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 6677301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 6687301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 6697301ad89Smatthias.ringwald // hci_stack.substate = 0; 6707301ad89Smatthias.ringwald // } 6717301ad89Smatthias.ringwald // handle normal init sequence 6723429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 6733429f56bSmatthias.ringwald // odd: waiting for event 674*f155fca3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 6753429f56bSmatthias.ringwald hci_stack.substate++; 6763429f56bSmatthias.ringwald } 6773429f56bSmatthias.ringwald } 67822909952Smatthias.ringwald } 67922909952Smatthias.ringwald 68089db417bSmatthias.ringwald // help with BT sleep 68189db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 68289db417bSmatthias.ringwald && hci_stack.substate == 1 68389db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 68489db417bSmatthias.ringwald hci_stack.substate++; 68589db417bSmatthias.ringwald } 68689db417bSmatthias.ringwald 6872718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 68894ab26f8Smatthias.ringwald 68994ab26f8Smatthias.ringwald // execute main loop 69094ab26f8Smatthias.ringwald hci_run(); 69116833f0aSmatthias.ringwald } 69216833f0aSmatthias.ringwald 6930a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 69410e830c9Smatthias.ringwald switch (packet_type) { 69510e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 69610e830c9Smatthias.ringwald event_handler(packet, size); 69710e830c9Smatthias.ringwald break; 69810e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 69910e830c9Smatthias.ringwald acl_handler(packet, size); 70010e830c9Smatthias.ringwald break; 70110e830c9Smatthias.ringwald default: 70210e830c9Smatthias.ringwald break; 70310e830c9Smatthias.ringwald } 70410e830c9Smatthias.ringwald } 70510e830c9Smatthias.ringwald 706fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 7072718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 7082718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 70916833f0aSmatthias.ringwald } 71016833f0aSmatthias.ringwald 7114f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 712475c8125Smatthias.ringwald 713475c8125Smatthias.ringwald // reference to use transport layer implementation 71416833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 715475c8125Smatthias.ringwald 71611e23e5fSmatthias.ringwald // references to used control implementation 71711e23e5fSmatthias.ringwald hci_stack.control = control; 71811e23e5fSmatthias.ringwald 71911e23e5fSmatthias.ringwald // reference to used config 72011e23e5fSmatthias.ringwald hci_stack.config = config; 72111e23e5fSmatthias.ringwald 722fe1ed1b8Smatthias.ringwald // no connections yet 723fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 7240a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 725c0e866bfSmatthias.ringwald hci_stack.connectable = 0; 726758b46ceSmatthias.ringwald 727b031bebbSmatthias.ringwald // no pending cmds 728b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 729b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 730b031bebbSmatthias.ringwald 73116833f0aSmatthias.ringwald // higher level handler 7322718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 73316833f0aSmatthias.ringwald 734404843c1Smatthias.ringwald // store and open remote device db 735404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 736404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 737404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 738404843c1Smatthias.ringwald } 73929d53098Smatthias.ringwald 7408fcba05dSmatthias.ringwald // max acl payload size defined in config.h 7418fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 7428fcba05dSmatthias.ringwald 74316833f0aSmatthias.ringwald // register packet handlers with transport 74410e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 745f5454fc6Smatthias.ringwald 746f5454fc6Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 747475c8125Smatthias.ringwald } 748475c8125Smatthias.ringwald 749404843c1Smatthias.ringwald void hci_close(){ 750404843c1Smatthias.ringwald // close remote device db 751404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 752404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 753404843c1Smatthias.ringwald } 754f5454fc6Smatthias.ringwald while (hci_stack.connections) { 755f5454fc6Smatthias.ringwald hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 756f5454fc6Smatthias.ringwald } 757f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 758404843c1Smatthias.ringwald } 759404843c1Smatthias.ringwald 7608d213e1aSmatthias.ringwald // State-Module-Driver overview 7618d213e1aSmatthias.ringwald // state module low-level 7628d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 7638d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 7648d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 7658d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 766d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 767d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 768c7e0c5f6Smatthias.ringwald 76940d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 7707301ad89Smatthias.ringwald 771038bc64cSmatthias.ringwald // power on 772f9a30166Smatthias.ringwald int err = 0; 773f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 774f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 775f9a30166Smatthias.ringwald } 776038bc64cSmatthias.ringwald if (err){ 7777d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 778038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 779038bc64cSmatthias.ringwald return err; 780038bc64cSmatthias.ringwald } 781038bc64cSmatthias.ringwald 782038bc64cSmatthias.ringwald // open low-level device 783038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 784038bc64cSmatthias.ringwald if (err){ 7857d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 786f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 787f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 788f9a30166Smatthias.ringwald } 789038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 790038bc64cSmatthias.ringwald return err; 791038bc64cSmatthias.ringwald } 7928d213e1aSmatthias.ringwald return 0; 7938d213e1aSmatthias.ringwald } 794038bc64cSmatthias.ringwald 79540d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 7968d213e1aSmatthias.ringwald 7977b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 7989418f9c9Smatthias.ringwald 7998d213e1aSmatthias.ringwald // close low-level device 8008d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 8018d213e1aSmatthias.ringwald 8027b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 8039418f9c9Smatthias.ringwald 8048d213e1aSmatthias.ringwald // power off 8058d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 8068d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 8078d213e1aSmatthias.ringwald } 8089418f9c9Smatthias.ringwald 8097b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 8109418f9c9Smatthias.ringwald 81172ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 81272ea5239Smatthias.ringwald } 81372ea5239Smatthias.ringwald 81440d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 81572ea5239Smatthias.ringwald 8167b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 8173144bce4Smatthias.ringwald 818b429b9b7Smatthias.ringwald #if 0 819b429b9b7Smatthias.ringwald // don't close serial port during sleep 820b429b9b7Smatthias.ringwald 82172ea5239Smatthias.ringwald // close low-level device 82272ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 823b429b9b7Smatthias.ringwald #endif 82472ea5239Smatthias.ringwald 82572ea5239Smatthias.ringwald // sleep mode 8263144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 82772ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 82872ea5239Smatthias.ringwald } 829b429b9b7Smatthias.ringwald 83072ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 8318d213e1aSmatthias.ringwald } 8328d213e1aSmatthias.ringwald 83340d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 834b429b9b7Smatthias.ringwald 8357b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 836b429b9b7Smatthias.ringwald 837b429b9b7Smatthias.ringwald // wake on 838b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 839b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 840b429b9b7Smatthias.ringwald } 841b429b9b7Smatthias.ringwald 842b429b9b7Smatthias.ringwald #if 0 843b429b9b7Smatthias.ringwald // open low-level device 844b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 845b429b9b7Smatthias.ringwald if (err){ 8467d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 847b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 848b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 849b429b9b7Smatthias.ringwald } 850b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 851b429b9b7Smatthias.ringwald return err; 852b429b9b7Smatthias.ringwald } 853b429b9b7Smatthias.ringwald #endif 854b429b9b7Smatthias.ringwald 855b429b9b7Smatthias.ringwald return 0; 856b429b9b7Smatthias.ringwald } 857b429b9b7Smatthias.ringwald 858b429b9b7Smatthias.ringwald 8598d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 860d661ed19Smatthias.ringwald 8617b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 862d661ed19Smatthias.ringwald 8638d213e1aSmatthias.ringwald int err = 0; 8648d213e1aSmatthias.ringwald switch (hci_stack.state){ 8658d213e1aSmatthias.ringwald 8668d213e1aSmatthias.ringwald case HCI_STATE_OFF: 8678d213e1aSmatthias.ringwald switch (power_mode){ 8688d213e1aSmatthias.ringwald case HCI_POWER_ON: 8698d213e1aSmatthias.ringwald err = hci_power_control_on(); 8708d213e1aSmatthias.ringwald if (err) return err; 8717301ad89Smatthias.ringwald // set up state machine 8727301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 8737301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8747301ad89Smatthias.ringwald hci_stack.substate = 0; 8758d213e1aSmatthias.ringwald break; 8768d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8778d213e1aSmatthias.ringwald // do nothing 8788d213e1aSmatthias.ringwald break; 8798d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 880b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 8818d213e1aSmatthias.ringwald break; 8828d213e1aSmatthias.ringwald } 8838d213e1aSmatthias.ringwald break; 8847301ad89Smatthias.ringwald 8858d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 8868d213e1aSmatthias.ringwald switch (power_mode){ 8878d213e1aSmatthias.ringwald case HCI_POWER_ON: 8888d213e1aSmatthias.ringwald // do nothing 8898d213e1aSmatthias.ringwald break; 8908d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8918d213e1aSmatthias.ringwald // no connections yet, just turn it off 8928d213e1aSmatthias.ringwald hci_power_control_off(); 8938d213e1aSmatthias.ringwald break; 8948d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 895b546ac54Smatthias.ringwald // no connections yet, just turn it off 89672ea5239Smatthias.ringwald hci_power_control_sleep(); 8978d213e1aSmatthias.ringwald break; 8988d213e1aSmatthias.ringwald } 8998d213e1aSmatthias.ringwald break; 9007301ad89Smatthias.ringwald 9018d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 9028d213e1aSmatthias.ringwald switch (power_mode){ 9038d213e1aSmatthias.ringwald case HCI_POWER_ON: 9048d213e1aSmatthias.ringwald // do nothing 9058d213e1aSmatthias.ringwald break; 9068d213e1aSmatthias.ringwald case HCI_POWER_OFF: 907c7e0c5f6Smatthias.ringwald // see hci_run 908c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9098d213e1aSmatthias.ringwald break; 9108d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 911b546ac54Smatthias.ringwald // see hci_run 912b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 91389db417bSmatthias.ringwald hci_stack.substate = 0; 9148d213e1aSmatthias.ringwald break; 9158d213e1aSmatthias.ringwald } 9168d213e1aSmatthias.ringwald break; 9177301ad89Smatthias.ringwald 9188d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 9198d213e1aSmatthias.ringwald switch (power_mode){ 9208d213e1aSmatthias.ringwald case HCI_POWER_ON: 9218d213e1aSmatthias.ringwald // set up state machine 9228d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9238d213e1aSmatthias.ringwald hci_stack.substate = 0; 9248d213e1aSmatthias.ringwald break; 9258d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9268d213e1aSmatthias.ringwald // do nothing 9278d213e1aSmatthias.ringwald break; 9288d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 929b546ac54Smatthias.ringwald // see hci_run 930b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 93189db417bSmatthias.ringwald hci_stack.substate = 0; 9328d213e1aSmatthias.ringwald break; 9338d213e1aSmatthias.ringwald } 9348d213e1aSmatthias.ringwald break; 9358d213e1aSmatthias.ringwald 9368d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 9378d213e1aSmatthias.ringwald switch (power_mode){ 9388d213e1aSmatthias.ringwald case HCI_POWER_ON: 93928171530Smatthias.ringwald 94028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 94128171530Smatthias.ringwald // nothing to do, if H4 supports power management 94228171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9438747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9448747d67fSmatthias.ringwald hci_stack.substate = 6; 94528171530Smatthias.ringwald break; 94628171530Smatthias.ringwald } 94728171530Smatthias.ringwald #endif 948b546ac54Smatthias.ringwald // set up state machine 949b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 950b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 951b546ac54Smatthias.ringwald hci_stack.substate = 0; 9528d213e1aSmatthias.ringwald break; 9538d213e1aSmatthias.ringwald case HCI_POWER_OFF: 954b546ac54Smatthias.ringwald // see hci_run 955b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9568d213e1aSmatthias.ringwald break; 9578d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 958b546ac54Smatthias.ringwald // do nothing 9598d213e1aSmatthias.ringwald break; 9608d213e1aSmatthias.ringwald } 9618d213e1aSmatthias.ringwald break; 9628d213e1aSmatthias.ringwald 9638d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 9648d213e1aSmatthias.ringwald switch (power_mode){ 9658d213e1aSmatthias.ringwald case HCI_POWER_ON: 96628171530Smatthias.ringwald 96728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 96828171530Smatthias.ringwald // nothing to do, if H4 supports power management 96928171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9708747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9718747d67fSmatthias.ringwald hci_stack.substate = 6; 972758b46ceSmatthias.ringwald hci_update_scan_enable(); 97328171530Smatthias.ringwald break; 97428171530Smatthias.ringwald } 97528171530Smatthias.ringwald #endif 9763144bce4Smatthias.ringwald err = hci_power_control_wake(); 9773144bce4Smatthias.ringwald if (err) return err; 978b546ac54Smatthias.ringwald // set up state machine 979b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 980b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 981b546ac54Smatthias.ringwald hci_stack.substate = 0; 9828d213e1aSmatthias.ringwald break; 9838d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9844ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9858d213e1aSmatthias.ringwald break; 9868d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 987b546ac54Smatthias.ringwald // do nothing 9888d213e1aSmatthias.ringwald break; 9898d213e1aSmatthias.ringwald } 9908d213e1aSmatthias.ringwald break; 99111e23e5fSmatthias.ringwald } 99268d92d03Smatthias.ringwald 993038bc64cSmatthias.ringwald // create internal event 994ee8bf225Smatthias.ringwald hci_emit_state(); 995ee8bf225Smatthias.ringwald 99668d92d03Smatthias.ringwald // trigger next/first action 99768d92d03Smatthias.ringwald hci_run(); 99868d92d03Smatthias.ringwald 999475c8125Smatthias.ringwald return 0; 1000475c8125Smatthias.ringwald } 1001475c8125Smatthias.ringwald 1002758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 1003758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 1004758b46ceSmatthias.ringwald hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 1005758b46ceSmatthias.ringwald hci_run(); 1006758b46ceSmatthias.ringwald } 1007758b46ceSmatthias.ringwald 1008381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1009381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1010381fbed8Smatthias.ringwald 1011381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 1012381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 1013381fbed8Smatthias.ringwald return; 1014381fbed8Smatthias.ringwald } 1015381fbed8Smatthias.ringwald 1016381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 1017758b46ceSmatthias.ringwald hci_update_scan_enable(); 1018758b46ceSmatthias.ringwald } 1019b031bebbSmatthias.ringwald 1020758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1021758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1022758b46ceSmatthias.ringwald 1023758b46ceSmatthias.ringwald // don't emit event 1024758b46ceSmatthias.ringwald if (hci_stack.connectable == enable) return; 1025758b46ceSmatthias.ringwald 1026758b46ceSmatthias.ringwald hci_stack.connectable = enable; 1027758b46ceSmatthias.ringwald hci_update_scan_enable(); 1028381fbed8Smatthias.ringwald } 1029381fbed8Smatthias.ringwald 103006b35ec0Smatthias.ringwald void hci_run(){ 10318a485f27Smatthias.ringwald 103232ab9390Smatthias.ringwald hci_connection_t * connection; 103332ab9390Smatthias.ringwald linked_item_t * it; 103432ab9390Smatthias.ringwald 1035ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1036ce4c8fabSmatthias.ringwald 1037b031bebbSmatthias.ringwald // global/non-connection oriented commands 1038b031bebbSmatthias.ringwald 1039b031bebbSmatthias.ringwald // decline incoming connections 1040ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 1041ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 1042ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 1043ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1044ce4c8fabSmatthias.ringwald } 1045ce4c8fabSmatthias.ringwald 1046b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1047b031bebbSmatthias.ringwald 1048b031bebbSmatthias.ringwald // send scan enable 104996774428Smatthias.ringwald if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff){ 1050b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1051b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 1052b031bebbSmatthias.ringwald } 1053b031bebbSmatthias.ringwald 105432ab9390Smatthias.ringwald // send pending HCI commands 105532ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 105632ab9390Smatthias.ringwald 1057b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 105832ab9390Smatthias.ringwald 1059b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 106032ab9390Smatthias.ringwald 106132ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 10627b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 106332ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 106432ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1065c7e0c5f6Smatthias.ringwald } 1066c7e0c5f6Smatthias.ringwald 1067de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 106832ab9390Smatthias.ringwald 106932ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 107032ab9390Smatthias.ringwald link_key_t link_key; 10717b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 107232ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 107332ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 107432ab9390Smatthias.ringwald } else { 107532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 107632ab9390Smatthias.ringwald } 107728ca2b46S[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 107832ab9390Smatthias.ringwald } 107932ab9390Smatthias.ringwald } 108032ab9390Smatthias.ringwald 1081de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1082c7e0c5f6Smatthias.ringwald 10833429f56bSmatthias.ringwald switch (hci_stack.state){ 10843429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 10857b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 10863429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 10873429f56bSmatthias.ringwald // odd: waiting for command completion 108806b35ec0Smatthias.ringwald return; 10893429f56bSmatthias.ringwald } 109090919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1091c7492964Smatthias.ringwald case 0: // RESET 109222909952Smatthias.ringwald hci_send_cmd(&hci_reset); 1093c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1094c7492964Smatthias.ringwald // skip baud change 1095c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1096c7492964Smatthias.ringwald } 10973429f56bSmatthias.ringwald break; 1098c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 10997dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 11007dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1101c7492964Smatthias.ringwald break; 1102c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1103c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1104c7492964Smatthias.ringwald hci_stack.substate += 2; 1105c7492964Smatthias.ringwald // break missing here for fall through 1106c7492964Smatthias.ringwald 1107c7492964Smatthias.ringwald case 3: 110890919203Smatthias.ringwald // custom initialization 1109db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 11107dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1111d62aca9fSmatthias.ringwald if (valid_cmd){ 11127dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 11137dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1114c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 111590919203Smatthias.ringwald break; 111690919203Smatthias.ringwald } 1117d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 111890919203Smatthias.ringwald } 11192f6c30e1Smatthias.ringwald // otherwise continue 1120f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1121f432a6ddSmatthias.ringwald break; 1122c7492964Smatthias.ringwald case 4: 1123e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1124e2edc0c3Smatthias.ringwald break; 1125c7492964Smatthias.ringwald case 5: 11263429f56bSmatthias.ringwald // ca. 15 sec 11273429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 11283429f56bSmatthias.ringwald break; 1129c7492964Smatthias.ringwald case 6: 1130758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 1131f432a6ddSmatthias.ringwald break; 1132c7492964Smatthias.ringwald case 7: 1133559e517eS[email protected] hci_send_cmd(&hci_read_local_supported_features); 1134559e517eS[email protected] break; 1135559e517eS[email protected] case 8: 11368a485f27Smatthias.ringwald #ifndef EMBEDDED 11378a485f27Smatthias.ringwald { 11388a485f27Smatthias.ringwald char hostname[30]; 11398a485f27Smatthias.ringwald gethostname(hostname, 30); 11408a485f27Smatthias.ringwald hostname[29] = '\0'; 11418a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 11428a485f27Smatthias.ringwald break; 11438a485f27Smatthias.ringwald } 1144559e517eS[email protected] case 9: 11453c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 11463c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 11473c4d4b90Smatthias.ringwald break; 11483c4d4b90Smatthias.ringwald 1149559e517eS[email protected] case 10: 11503c4d4b90Smatthias.ringwald #endif 11518a485f27Smatthias.ringwald #endif 11523429f56bSmatthias.ringwald // done. 11533429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1154b360b6adSmatthias.ringwald hci_emit_state(); 11553429f56bSmatthias.ringwald break; 11563429f56bSmatthias.ringwald default: 11573429f56bSmatthias.ringwald break; 1158475c8125Smatthias.ringwald } 11593429f56bSmatthias.ringwald hci_stack.substate++; 11603429f56bSmatthias.ringwald break; 1161c7e0c5f6Smatthias.ringwald 1162c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1163c7e0c5f6Smatthias.ringwald 11647b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1165c7e0c5f6Smatthias.ringwald // close all open connections 1166c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1167c7e0c5f6Smatthias.ringwald if (connection){ 116832ab9390Smatthias.ringwald 1169c7e0c5f6Smatthias.ringwald // send disconnect 117032ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 117132ab9390Smatthias.ringwald 117279bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11736ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1174c7e0c5f6Smatthias.ringwald 1175c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1176c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1177c7e0c5f6Smatthias.ringwald return; 1178c7e0c5f6Smatthias.ringwald } 11797b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1180c7e0c5f6Smatthias.ringwald 118172ea5239Smatthias.ringwald // switch mode 1182c7e0c5f6Smatthias.ringwald hci_power_control_off(); 11839418f9c9Smatthias.ringwald 11847b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 118572ea5239Smatthias.ringwald hci_emit_state(); 11867b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 118772ea5239Smatthias.ringwald break; 1188c7e0c5f6Smatthias.ringwald 118972ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 119089db417bSmatthias.ringwald switch(hci_stack.substate) { 119189db417bSmatthias.ringwald case 0: 11927b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 119372ea5239Smatthias.ringwald // close all open connections 119472ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 119566da7044Smatthias.ringwald 119628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 119766da7044Smatthias.ringwald // don't close connections, if H4 supports power management 119866da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 119966da7044Smatthias.ringwald connection = NULL; 120066da7044Smatthias.ringwald } 120166da7044Smatthias.ringwald #endif 120272ea5239Smatthias.ringwald if (connection){ 120332ab9390Smatthias.ringwald 120472ea5239Smatthias.ringwald // send disconnect 120532ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 120632ab9390Smatthias.ringwald 120779bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 12086ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 120972ea5239Smatthias.ringwald 121072ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 121172ea5239Smatthias.ringwald hci_shutdown_connection(connection); 121272ea5239Smatthias.ringwald return; 121372ea5239Smatthias.ringwald } 121472ea5239Smatthias.ringwald 121589db417bSmatthias.ringwald // disable page and inquiry scan 121632ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 121732ab9390Smatthias.ringwald 1218758b46ceSmatthias.ringwald log_info("HCI_STATE_HALTING, disabling inq cans\n"); 1219758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 122089db417bSmatthias.ringwald 122189db417bSmatthias.ringwald // continue in next sub state 122289db417bSmatthias.ringwald hci_stack.substate++; 122389db417bSmatthias.ringwald break; 122489db417bSmatthias.ringwald case 1: 122589db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 122689db417bSmatthias.ringwald break; 122789db417bSmatthias.ringwald case 2: 12287b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 122928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 123028171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 123128171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 123228171530Smatthias.ringwald // SLEEP MODE reached 123328171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 123428171530Smatthias.ringwald hci_emit_state(); 123528171530Smatthias.ringwald break; 123628171530Smatthias.ringwald } 123728171530Smatthias.ringwald #endif 123872ea5239Smatthias.ringwald // switch mode 123989db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1240c7e0c5f6Smatthias.ringwald hci_emit_state(); 124128171530Smatthias.ringwald break; 124228171530Smatthias.ringwald 124389db417bSmatthias.ringwald default: 124489db417bSmatthias.ringwald break; 124589db417bSmatthias.ringwald } 1246c7e0c5f6Smatthias.ringwald break; 1247c7e0c5f6Smatthias.ringwald 12483429f56bSmatthias.ringwald default: 12493429f56bSmatthias.ringwald break; 12501f504dbdSmatthias.ringwald } 12513429f56bSmatthias.ringwald } 125216833f0aSmatthias.ringwald 125331452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1254c8e4258aSmatthias.ringwald bd_addr_t addr; 1255c8e4258aSmatthias.ringwald hci_connection_t * conn; 1256c8e4258aSmatthias.ringwald // house-keeping 1257c8e4258aSmatthias.ringwald 1258c8e4258aSmatthias.ringwald // create_connection? 1259c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1260c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1261339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1262c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1263c8e4258aSmatthias.ringwald if (conn) { 1264c8e4258aSmatthias.ringwald // if connection exists 1265c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 126617f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 126717f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1268c8e4258aSmatthias.ringwald } 126917f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1270c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1271c8e4258aSmatthias.ringwald 127217f1ba2aSmatthias.ringwald } 1273c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 127417f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 127517f1ba2aSmatthias.ringwald if (!conn){ 127617f1ba2aSmatthias.ringwald // notify client that alloc failed 127717f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 127817f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 127917f1ba2aSmatthias.ringwald } 1280c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1281c8e4258aSmatthias.ringwald } 1282c8e4258aSmatthias.ringwald 12837fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 12847fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 12857fde4af9Smatthias.ringwald } 12867fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 12877fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 12887fde4af9Smatthias.ringwald } 12897fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 12907fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 12917fde4af9Smatthias.ringwald } 12927fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 12937fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 12947fde4af9Smatthias.ringwald } 12957fde4af9Smatthias.ringwald 12968ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 12978ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 12988ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 12998ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 13008ef73945Smatthias.ringwald } 13018ef73945Smatthias.ringwald } 1302c8e4258aSmatthias.ringwald 130331452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1304622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 130531452debSmatthias.ringwald } 13068adf0ddaSmatthias.ringwald 13071cd208adSmatthias.ringwald /** 13081cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 13091cd208adSmatthias.ringwald */ 1310fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 13111cd208adSmatthias.ringwald va_list argptr; 13121cd208adSmatthias.ringwald va_start(argptr, cmd); 13137dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 13141cd208adSmatthias.ringwald va_end(argptr); 13157dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 131693b8dc03Smatthias.ringwald } 1317c8e4258aSmatthias.ringwald 1318ee091cf1Smatthias.ringwald // Create various non-HCI events. 1319ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1320ee091cf1Smatthias.ringwald 1321c8e4258aSmatthias.ringwald void hci_emit_state(){ 1322e0abb8e7S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack.state); 1323425d1371Smatthias.ringwald uint8_t event[3]; 132480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1325425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1326c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1327425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1328425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1329c8e4258aSmatthias.ringwald } 1330c8e4258aSmatthias.ringwald 133117f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1332425d1371Smatthias.ringwald uint8_t event[13]; 1333c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1334425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 133517f1ba2aSmatthias.ringwald event[2] = status; 1336c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1337c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1338c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1339c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1340425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1341425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1342c8e4258aSmatthias.ringwald } 1343c8e4258aSmatthias.ringwald 13443c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1345425d1371Smatthias.ringwald uint8_t event[6]; 13463c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1347e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 13483c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 13493c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 13503c4d4b90Smatthias.ringwald event[5] = reason; 1351425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1352425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13533c4d4b90Smatthias.ringwald } 13543c4d4b90Smatthias.ringwald 1355ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1356e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1357425d1371Smatthias.ringwald uint8_t event[4]; 135880d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1359425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1360ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1361425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1362425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1363ee091cf1Smatthias.ringwald } 136443bfb1bdSmatthias.ringwald 136543bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1366e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1367425d1371Smatthias.ringwald uint8_t event[3]; 136880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1369425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 137043bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1371425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1372425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 137343bfb1bdSmatthias.ringwald } 1374038bc64cSmatthias.ringwald 1375038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1376e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1377425d1371Smatthias.ringwald uint8_t event[2]; 137880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1379425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1380425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1381425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1382038bc64cSmatthias.ringwald } 13831b0e3922Smatthias.ringwald 138409ba8edeSmatthias.ringwald #ifndef EMBEDDED 13851b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1386e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1387425d1371Smatthias.ringwald uint8_t event[6]; 13881b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1389425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1390425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1391425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1392425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1393425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1394425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13951b0e3922Smatthias.ringwald } 139609ba8edeSmatthias.ringwald #endif 13971b0e3922Smatthias.ringwald 13982ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1399e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1400425d1371Smatthias.ringwald uint8_t event[3]; 14012ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1402425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 14032ed6235cSmatthias.ringwald event[2] = enabled; 1404425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1405e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 14062ed6235cSmatthias.ringwald } 1407627c2f45Smatthias.ringwald 1408627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1409e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1410627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1411e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1412f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 14132f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1414f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1415e0abb8e7S[email protected] 1416e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1417e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1418e0abb8e7S[email protected] 1419e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 1420e0abb8e7S[email protected] hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1421627c2f45Smatthias.ringwald } 1422381fbed8Smatthias.ringwald 1423381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1424e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1425425d1371Smatthias.ringwald uint8_t event[3]; 1426381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1427425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1428381fbed8Smatthias.ringwald event[2] = enabled; 1429425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1430425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1431381fbed8Smatthias.ringwald } 1432