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 } 45356cf178bSmatthias.ringwald break; 45456cf178bSmatthias.ringwald 4557ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 4567ec5eeaaSmatthias.ringwald // get num cmd packets 4577b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 4587ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 4597ec5eeaaSmatthias.ringwald break; 4607ec5eeaaSmatthias.ringwald 46156cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 46256cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 46356cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 46456cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 46556cf178bSmatthias.ringwald conn = connection_for_handle(handle); 46656cf178bSmatthias.ringwald if (!conn){ 4677d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 46856cf178bSmatthias.ringwald continue; 46956cf178bSmatthias.ringwald } 47056cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 4717b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 47256cf178bSmatthias.ringwald } 4736772a24cSmatthias.ringwald break; 4746772a24cSmatthias.ringwald 4751f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 47637eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 47737eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 4782d00edd4Smatthias.ringwald link_type = packet[11]; 479339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 48037eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 4811f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 4821f7b95a1Smatthias.ringwald if (!conn) { 4831f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 4841f7b95a1Smatthias.ringwald } 485ce4c8fabSmatthias.ringwald if (!conn) { 486ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 487ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 488ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 489ce4c8fabSmatthias.ringwald break; 490ce4c8fabSmatthias.ringwald } 49132ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 49232ab9390Smatthias.ringwald hci_run(); 49337eaa4cfSmatthias.ringwald } else { 494ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 495ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 496ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 49737eaa4cfSmatthias.ringwald } 4981f7b95a1Smatthias.ringwald break; 4991f7b95a1Smatthias.ringwald 5006772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 501fe1ed1b8Smatthias.ringwald // Connection management 502fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 503339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 5041f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 505fe1ed1b8Smatthias.ringwald if (conn) { 506b448a0e7Smatthias.ringwald if (!packet[2]){ 507c8e4258aSmatthias.ringwald conn->state = OPEN; 508fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 509ee091cf1Smatthias.ringwald 510c785ef68Smatthias.ringwald // restart timer 511c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 512ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 513c785ef68Smatthias.ringwald 514339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 51543bfb1bdSmatthias.ringwald 51643bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 517b448a0e7Smatthias.ringwald } else { 518b448a0e7Smatthias.ringwald // connection failed, remove entry 519b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 520a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 521c12e46e7Smatthias.ringwald 522c12e46e7Smatthias.ringwald // if authentication error, also delete link key 523c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 524c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 525c12e46e7Smatthias.ringwald } 526fe1ed1b8Smatthias.ringwald } 527fe1ed1b8Smatthias.ringwald } 5286772a24cSmatthias.ringwald break; 529fe1ed1b8Smatthias.ringwald 5307fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 5317b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 5327fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 53374ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 53432ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 53564472d52Smatthias.ringwald hci_run(); 536d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 53729d53098Smatthias.ringwald return; 5387fde4af9Smatthias.ringwald 5397fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 5407fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 54174ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 54229d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 543287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 54429d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 5457fde4af9Smatthias.ringwald break; 5467fde4af9Smatthias.ringwald 5477fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 5487fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 549d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 550d9a327f9Smatthias.ringwald if (!hci_stack.remote_device_db) break; 551d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 552d9a327f9Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 5537fde4af9Smatthias.ringwald break; 5547fde4af9Smatthias.ringwald 555223aafc1Smatthias.ringwald #ifndef EMBEDDED 55674ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 55774ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 55874ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 55974ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 5605a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 5615a06394aSmatthias.ringwald for (i=0; i<248;i++){ 5625a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 5635a06394aSmatthias.ringwald packet[9+i] = 0; 5645a06394aSmatthias.ringwald break; 565cdc9101dSmatthias.ringwald } 5665a06394aSmatthias.ringwald } 567d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 56874ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 56974ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 57074ec757aSmatthias.ringwald break; 57174ec757aSmatthias.ringwald 57274ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 57374ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 57474ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 57574ec757aSmatthias.ringwald // first send inq result packet 57674ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 57774ec757aSmatthias.ringwald // then send cached remote names 57874ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 57974ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 58074ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 58174ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 58274ec757aSmatthias.ringwald } 58374ec757aSmatthias.ringwald } 58474ec757aSmatthias.ringwald return; 585223aafc1Smatthias.ringwald #endif 58674ec757aSmatthias.ringwald 5876772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 588fe1ed1b8Smatthias.ringwald if (!packet[2]){ 589fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 590fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 591fe1ed1b8Smatthias.ringwald if (conn) { 592c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 593fe1ed1b8Smatthias.ringwald } 594fe1ed1b8Smatthias.ringwald } 5956772a24cSmatthias.ringwald break; 5966772a24cSmatthias.ringwald 597c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 598c68bdf90Smatthias.ringwald if(hci_stack.control->hw_error){ 599c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 600c68bdf90Smatthias.ringwald } 601c68bdf90Smatthias.ringwald break; 602c68bdf90Smatthias.ringwald 6035909f7f2Smatthias.ringwald #ifdef HAVE_BLE 6045909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 6055909f7f2Smatthias.ringwald switch (packet[2]) { 6065909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 6075909f7f2Smatthias.ringwald // Connection management 6085909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 6095909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 6105909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 6115909f7f2Smatthias.ringwald conn = connection_for_address(addr); 6125909f7f2Smatthias.ringwald if (packet[3]){ 6135909f7f2Smatthias.ringwald if (conn){ 6145909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 6155909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 6165909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 6175909f7f2Smatthias.ringwald 6185909f7f2Smatthias.ringwald } 6195909f7f2Smatthias.ringwald // if authentication error, also delete link key 6205909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 6215909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 6225909f7f2Smatthias.ringwald } 6235909f7f2Smatthias.ringwald break; 6245909f7f2Smatthias.ringwald } 6255909f7f2Smatthias.ringwald if (!conn){ 6265909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 6275909f7f2Smatthias.ringwald } 6285909f7f2Smatthias.ringwald if (!conn){ 6295909f7f2Smatthias.ringwald // no memory 6305909f7f2Smatthias.ringwald break; 6315909f7f2Smatthias.ringwald } 6325909f7f2Smatthias.ringwald 6335909f7f2Smatthias.ringwald conn->state = OPEN; 6345909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 6355909f7f2Smatthias.ringwald 6365909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 6375909f7f2Smatthias.ringwald 6385909f7f2Smatthias.ringwald // restart timer 6395909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 6405909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 6415909f7f2Smatthias.ringwald 6425909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 6435909f7f2Smatthias.ringwald 6445909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 6455909f7f2Smatthias.ringwald break; 6465909f7f2Smatthias.ringwald 6475909f7f2Smatthias.ringwald default: 6485909f7f2Smatthias.ringwald break; 6495909f7f2Smatthias.ringwald } 6505909f7f2Smatthias.ringwald break; 6515909f7f2Smatthias.ringwald #endif 6525909f7f2Smatthias.ringwald 6536772a24cSmatthias.ringwald default: 6546772a24cSmatthias.ringwald break; 655fe1ed1b8Smatthias.ringwald } 656fe1ed1b8Smatthias.ringwald 6573429f56bSmatthias.ringwald // handle BT initialization 6583429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 6597301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 6607301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 6617301ad89Smatthias.ringwald // hci_stack.substate = 0; 6627301ad89Smatthias.ringwald // } 6637301ad89Smatthias.ringwald // handle normal init sequence 6643429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 6653429f56bSmatthias.ringwald // odd: waiting for event 6663429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 6673429f56bSmatthias.ringwald hci_stack.substate++; 6683429f56bSmatthias.ringwald } 6693429f56bSmatthias.ringwald } 67022909952Smatthias.ringwald } 67122909952Smatthias.ringwald 67289db417bSmatthias.ringwald // help with BT sleep 67389db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 67489db417bSmatthias.ringwald && hci_stack.substate == 1 67589db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 67689db417bSmatthias.ringwald hci_stack.substate++; 67789db417bSmatthias.ringwald } 67889db417bSmatthias.ringwald 6792718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 68094ab26f8Smatthias.ringwald 68194ab26f8Smatthias.ringwald // execute main loop 68294ab26f8Smatthias.ringwald hci_run(); 68316833f0aSmatthias.ringwald } 68416833f0aSmatthias.ringwald 685*0a57e69fSmatthias.ringwald static void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 68610e830c9Smatthias.ringwald switch (packet_type) { 68710e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 68810e830c9Smatthias.ringwald event_handler(packet, size); 68910e830c9Smatthias.ringwald break; 69010e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 69110e830c9Smatthias.ringwald acl_handler(packet, size); 69210e830c9Smatthias.ringwald break; 69310e830c9Smatthias.ringwald default: 69410e830c9Smatthias.ringwald break; 69510e830c9Smatthias.ringwald } 69610e830c9Smatthias.ringwald } 69710e830c9Smatthias.ringwald 698fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 6992718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 7002718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 70116833f0aSmatthias.ringwald } 70216833f0aSmatthias.ringwald 7034f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 704475c8125Smatthias.ringwald 705475c8125Smatthias.ringwald // reference to use transport layer implementation 70616833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 707475c8125Smatthias.ringwald 70811e23e5fSmatthias.ringwald // references to used control implementation 70911e23e5fSmatthias.ringwald hci_stack.control = control; 71011e23e5fSmatthias.ringwald 71111e23e5fSmatthias.ringwald // reference to used config 71211e23e5fSmatthias.ringwald hci_stack.config = config; 71311e23e5fSmatthias.ringwald 714fe1ed1b8Smatthias.ringwald // no connections yet 715fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 7160a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 717c0e866bfSmatthias.ringwald hci_stack.connectable = 0; 718758b46ceSmatthias.ringwald 719b031bebbSmatthias.ringwald // no pending cmds 720b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 721b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 722b031bebbSmatthias.ringwald 72316833f0aSmatthias.ringwald // higher level handler 7242718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 72516833f0aSmatthias.ringwald 726404843c1Smatthias.ringwald // store and open remote device db 727404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 728404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 729404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 730404843c1Smatthias.ringwald } 73129d53098Smatthias.ringwald 7328fcba05dSmatthias.ringwald // max acl payload size defined in config.h 7338fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 7348fcba05dSmatthias.ringwald 73516833f0aSmatthias.ringwald // register packet handlers with transport 73610e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 737f5454fc6Smatthias.ringwald 738f5454fc6Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 739475c8125Smatthias.ringwald } 740475c8125Smatthias.ringwald 741404843c1Smatthias.ringwald void hci_close(){ 742404843c1Smatthias.ringwald // close remote device db 743404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 744404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 745404843c1Smatthias.ringwald } 746f5454fc6Smatthias.ringwald while (hci_stack.connections) { 747f5454fc6Smatthias.ringwald hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 748f5454fc6Smatthias.ringwald } 749f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 750404843c1Smatthias.ringwald } 751404843c1Smatthias.ringwald 7528d213e1aSmatthias.ringwald // State-Module-Driver overview 7538d213e1aSmatthias.ringwald // state module low-level 7548d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 7558d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 7568d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 7578d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 758d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 759d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 760c7e0c5f6Smatthias.ringwald 76140d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 7627301ad89Smatthias.ringwald 763038bc64cSmatthias.ringwald // power on 764f9a30166Smatthias.ringwald int err = 0; 765f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 766f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 767f9a30166Smatthias.ringwald } 768038bc64cSmatthias.ringwald if (err){ 7697d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 770038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 771038bc64cSmatthias.ringwald return err; 772038bc64cSmatthias.ringwald } 773038bc64cSmatthias.ringwald 774038bc64cSmatthias.ringwald // open low-level device 775038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 776038bc64cSmatthias.ringwald if (err){ 7777d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 778f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 779f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 780f9a30166Smatthias.ringwald } 781038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 782038bc64cSmatthias.ringwald return err; 783038bc64cSmatthias.ringwald } 7848d213e1aSmatthias.ringwald return 0; 7858d213e1aSmatthias.ringwald } 786038bc64cSmatthias.ringwald 78740d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 7888d213e1aSmatthias.ringwald 7897b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 7909418f9c9Smatthias.ringwald 7918d213e1aSmatthias.ringwald // close low-level device 7928d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 7938d213e1aSmatthias.ringwald 7947b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 7959418f9c9Smatthias.ringwald 7968d213e1aSmatthias.ringwald // power off 7978d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 7988d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 7998d213e1aSmatthias.ringwald } 8009418f9c9Smatthias.ringwald 8017b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 8029418f9c9Smatthias.ringwald 80372ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 80472ea5239Smatthias.ringwald } 80572ea5239Smatthias.ringwald 80640d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 80772ea5239Smatthias.ringwald 8087b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 8093144bce4Smatthias.ringwald 810b429b9b7Smatthias.ringwald #if 0 811b429b9b7Smatthias.ringwald // don't close serial port during sleep 812b429b9b7Smatthias.ringwald 81372ea5239Smatthias.ringwald // close low-level device 81472ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 815b429b9b7Smatthias.ringwald #endif 81672ea5239Smatthias.ringwald 81772ea5239Smatthias.ringwald // sleep mode 8183144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 81972ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 82072ea5239Smatthias.ringwald } 821b429b9b7Smatthias.ringwald 82272ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 8238d213e1aSmatthias.ringwald } 8248d213e1aSmatthias.ringwald 82540d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 826b429b9b7Smatthias.ringwald 8277b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 828b429b9b7Smatthias.ringwald 829b429b9b7Smatthias.ringwald // wake on 830b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 831b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 832b429b9b7Smatthias.ringwald } 833b429b9b7Smatthias.ringwald 834b429b9b7Smatthias.ringwald #if 0 835b429b9b7Smatthias.ringwald // open low-level device 836b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 837b429b9b7Smatthias.ringwald if (err){ 8387d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 839b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 840b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 841b429b9b7Smatthias.ringwald } 842b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 843b429b9b7Smatthias.ringwald return err; 844b429b9b7Smatthias.ringwald } 845b429b9b7Smatthias.ringwald #endif 846b429b9b7Smatthias.ringwald 847b429b9b7Smatthias.ringwald return 0; 848b429b9b7Smatthias.ringwald } 849b429b9b7Smatthias.ringwald 850b429b9b7Smatthias.ringwald 8518d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 852d661ed19Smatthias.ringwald 8537b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 854d661ed19Smatthias.ringwald 8558d213e1aSmatthias.ringwald int err = 0; 8568d213e1aSmatthias.ringwald switch (hci_stack.state){ 8578d213e1aSmatthias.ringwald 8588d213e1aSmatthias.ringwald case HCI_STATE_OFF: 8598d213e1aSmatthias.ringwald switch (power_mode){ 8608d213e1aSmatthias.ringwald case HCI_POWER_ON: 8618d213e1aSmatthias.ringwald err = hci_power_control_on(); 8628d213e1aSmatthias.ringwald if (err) return err; 8637301ad89Smatthias.ringwald // set up state machine 8647301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 8657301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8667301ad89Smatthias.ringwald hci_stack.substate = 0; 8678d213e1aSmatthias.ringwald break; 8688d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8698d213e1aSmatthias.ringwald // do nothing 8708d213e1aSmatthias.ringwald break; 8718d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 872b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 8738d213e1aSmatthias.ringwald break; 8748d213e1aSmatthias.ringwald } 8758d213e1aSmatthias.ringwald break; 8767301ad89Smatthias.ringwald 8778d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 8788d213e1aSmatthias.ringwald switch (power_mode){ 8798d213e1aSmatthias.ringwald case HCI_POWER_ON: 8808d213e1aSmatthias.ringwald // do nothing 8818d213e1aSmatthias.ringwald break; 8828d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8838d213e1aSmatthias.ringwald // no connections yet, just turn it off 8848d213e1aSmatthias.ringwald hci_power_control_off(); 8858d213e1aSmatthias.ringwald break; 8868d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 887b546ac54Smatthias.ringwald // no connections yet, just turn it off 88872ea5239Smatthias.ringwald hci_power_control_sleep(); 8898d213e1aSmatthias.ringwald break; 8908d213e1aSmatthias.ringwald } 8918d213e1aSmatthias.ringwald break; 8927301ad89Smatthias.ringwald 8938d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 8948d213e1aSmatthias.ringwald switch (power_mode){ 8958d213e1aSmatthias.ringwald case HCI_POWER_ON: 8968d213e1aSmatthias.ringwald // do nothing 8978d213e1aSmatthias.ringwald break; 8988d213e1aSmatthias.ringwald case HCI_POWER_OFF: 899c7e0c5f6Smatthias.ringwald // see hci_run 900c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9018d213e1aSmatthias.ringwald break; 9028d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 903b546ac54Smatthias.ringwald // see hci_run 904b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 90589db417bSmatthias.ringwald hci_stack.substate = 0; 9068d213e1aSmatthias.ringwald break; 9078d213e1aSmatthias.ringwald } 9088d213e1aSmatthias.ringwald break; 9097301ad89Smatthias.ringwald 9108d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 9118d213e1aSmatthias.ringwald switch (power_mode){ 9128d213e1aSmatthias.ringwald case HCI_POWER_ON: 9138d213e1aSmatthias.ringwald // set up state machine 9148d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9158d213e1aSmatthias.ringwald hci_stack.substate = 0; 9168d213e1aSmatthias.ringwald break; 9178d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9188d213e1aSmatthias.ringwald // do nothing 9198d213e1aSmatthias.ringwald break; 9208d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 921b546ac54Smatthias.ringwald // see hci_run 922b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 92389db417bSmatthias.ringwald hci_stack.substate = 0; 9248d213e1aSmatthias.ringwald break; 9258d213e1aSmatthias.ringwald } 9268d213e1aSmatthias.ringwald break; 9278d213e1aSmatthias.ringwald 9288d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 9298d213e1aSmatthias.ringwald switch (power_mode){ 9308d213e1aSmatthias.ringwald case HCI_POWER_ON: 93128171530Smatthias.ringwald 93228171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 93328171530Smatthias.ringwald // nothing to do, if H4 supports power management 93428171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9358747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9368747d67fSmatthias.ringwald hci_stack.substate = 6; 93728171530Smatthias.ringwald break; 93828171530Smatthias.ringwald } 93928171530Smatthias.ringwald #endif 940b546ac54Smatthias.ringwald // set up state machine 941b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 942b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 943b546ac54Smatthias.ringwald hci_stack.substate = 0; 9448d213e1aSmatthias.ringwald break; 9458d213e1aSmatthias.ringwald case HCI_POWER_OFF: 946b546ac54Smatthias.ringwald // see hci_run 947b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9488d213e1aSmatthias.ringwald break; 9498d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 950b546ac54Smatthias.ringwald // do nothing 9518d213e1aSmatthias.ringwald break; 9528d213e1aSmatthias.ringwald } 9538d213e1aSmatthias.ringwald break; 9548d213e1aSmatthias.ringwald 9558d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 9568d213e1aSmatthias.ringwald switch (power_mode){ 9578d213e1aSmatthias.ringwald case HCI_POWER_ON: 95828171530Smatthias.ringwald 95928171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 96028171530Smatthias.ringwald // nothing to do, if H4 supports power management 96128171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9628747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9638747d67fSmatthias.ringwald hci_stack.substate = 6; 964758b46ceSmatthias.ringwald hci_update_scan_enable(); 96528171530Smatthias.ringwald break; 96628171530Smatthias.ringwald } 96728171530Smatthias.ringwald #endif 9683144bce4Smatthias.ringwald err = hci_power_control_wake(); 9693144bce4Smatthias.ringwald if (err) return err; 970b546ac54Smatthias.ringwald // set up state machine 971b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 972b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 973b546ac54Smatthias.ringwald hci_stack.substate = 0; 9748d213e1aSmatthias.ringwald break; 9758d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9764ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9778d213e1aSmatthias.ringwald break; 9788d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 979b546ac54Smatthias.ringwald // do nothing 9808d213e1aSmatthias.ringwald break; 9818d213e1aSmatthias.ringwald } 9828d213e1aSmatthias.ringwald break; 98311e23e5fSmatthias.ringwald } 98468d92d03Smatthias.ringwald 985038bc64cSmatthias.ringwald // create internal event 986ee8bf225Smatthias.ringwald hci_emit_state(); 987ee8bf225Smatthias.ringwald 98868d92d03Smatthias.ringwald // trigger next/first action 98968d92d03Smatthias.ringwald hci_run(); 99068d92d03Smatthias.ringwald 991475c8125Smatthias.ringwald return 0; 992475c8125Smatthias.ringwald } 993475c8125Smatthias.ringwald 994758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 995758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 996758b46ceSmatthias.ringwald hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 997758b46ceSmatthias.ringwald hci_run(); 998758b46ceSmatthias.ringwald } 999758b46ceSmatthias.ringwald 1000381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 1001381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 1002381fbed8Smatthias.ringwald 1003381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 1004381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 1005381fbed8Smatthias.ringwald return; 1006381fbed8Smatthias.ringwald } 1007381fbed8Smatthias.ringwald 1008381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 1009758b46ceSmatthias.ringwald hci_update_scan_enable(); 1010758b46ceSmatthias.ringwald } 1011b031bebbSmatthias.ringwald 1012758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1013758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1014758b46ceSmatthias.ringwald 1015758b46ceSmatthias.ringwald // don't emit event 1016758b46ceSmatthias.ringwald if (hci_stack.connectable == enable) return; 1017758b46ceSmatthias.ringwald 1018758b46ceSmatthias.ringwald hci_stack.connectable = enable; 1019758b46ceSmatthias.ringwald hci_update_scan_enable(); 1020381fbed8Smatthias.ringwald } 1021381fbed8Smatthias.ringwald 102206b35ec0Smatthias.ringwald void hci_run(){ 10238a485f27Smatthias.ringwald 102432ab9390Smatthias.ringwald hci_connection_t * connection; 102532ab9390Smatthias.ringwald linked_item_t * it; 102632ab9390Smatthias.ringwald 1027ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1028ce4c8fabSmatthias.ringwald 1029b031bebbSmatthias.ringwald // global/non-connection oriented commands 1030b031bebbSmatthias.ringwald 1031b031bebbSmatthias.ringwald // decline incoming connections 1032ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 1033ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 1034ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 1035ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1036ce4c8fabSmatthias.ringwald } 1037ce4c8fabSmatthias.ringwald 1038b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1039b031bebbSmatthias.ringwald 1040b031bebbSmatthias.ringwald // send scan enable 104196774428Smatthias.ringwald if (hci_stack.state == HCI_STATE_WORKING && hci_stack.new_scan_enable_value != 0xff){ 1042b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1043b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 1044b031bebbSmatthias.ringwald } 1045b031bebbSmatthias.ringwald 104632ab9390Smatthias.ringwald // send pending HCI commands 104732ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 104832ab9390Smatthias.ringwald 1049b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 105032ab9390Smatthias.ringwald 1051b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 105232ab9390Smatthias.ringwald 105332ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 10547b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 105532ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 105632ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1057c7e0c5f6Smatthias.ringwald } 1058c7e0c5f6Smatthias.ringwald 1059de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 106032ab9390Smatthias.ringwald 106132ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 106232ab9390Smatthias.ringwald link_key_t link_key; 10637b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 106432ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 106532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 106632ab9390Smatthias.ringwald } else { 106732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 106832ab9390Smatthias.ringwald } 106928ca2b46S[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 107032ab9390Smatthias.ringwald } 107132ab9390Smatthias.ringwald } 107232ab9390Smatthias.ringwald 1073de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1074c7e0c5f6Smatthias.ringwald 10753429f56bSmatthias.ringwald switch (hci_stack.state){ 10763429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 10777b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 10783429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 10793429f56bSmatthias.ringwald // odd: waiting for command completion 108006b35ec0Smatthias.ringwald return; 10813429f56bSmatthias.ringwald } 108290919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1083c7492964Smatthias.ringwald case 0: // RESET 108422909952Smatthias.ringwald hci_send_cmd(&hci_reset); 1085c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1086c7492964Smatthias.ringwald // skip baud change 1087c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1088c7492964Smatthias.ringwald } 10893429f56bSmatthias.ringwald break; 1090c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 10917dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 10927dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1093c7492964Smatthias.ringwald break; 1094c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1095c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1096c7492964Smatthias.ringwald hci_stack.substate += 2; 1097c7492964Smatthias.ringwald // break missing here for fall through 1098c7492964Smatthias.ringwald 1099c7492964Smatthias.ringwald case 3: 110090919203Smatthias.ringwald // custom initialization 1101db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 11027dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1103d62aca9fSmatthias.ringwald if (valid_cmd){ 11047dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 11057dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1106c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 110790919203Smatthias.ringwald break; 110890919203Smatthias.ringwald } 1109d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 111090919203Smatthias.ringwald } 11112f6c30e1Smatthias.ringwald // otherwise continue 1112f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1113f432a6ddSmatthias.ringwald break; 1114c7492964Smatthias.ringwald case 4: 1115e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1116e2edc0c3Smatthias.ringwald break; 1117c7492964Smatthias.ringwald case 5: 11183429f56bSmatthias.ringwald // ca. 15 sec 11193429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 11203429f56bSmatthias.ringwald break; 1121c7492964Smatthias.ringwald case 6: 1122758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 1123f432a6ddSmatthias.ringwald break; 1124c7492964Smatthias.ringwald case 7: 11258a485f27Smatthias.ringwald #ifndef EMBEDDED 11268a485f27Smatthias.ringwald { 11278a485f27Smatthias.ringwald char hostname[30]; 11288a485f27Smatthias.ringwald gethostname(hostname, 30); 11298a485f27Smatthias.ringwald hostname[29] = '\0'; 11308a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 11318a485f27Smatthias.ringwald break; 11328a485f27Smatthias.ringwald } 1133c7492964Smatthias.ringwald case 8: 11343c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 11353c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 11363c4d4b90Smatthias.ringwald break; 11373c4d4b90Smatthias.ringwald 1138c7492964Smatthias.ringwald case 9: 11393c4d4b90Smatthias.ringwald #endif 11408a485f27Smatthias.ringwald #endif 11413429f56bSmatthias.ringwald // done. 11423429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1143b360b6adSmatthias.ringwald hci_emit_state(); 11443429f56bSmatthias.ringwald break; 11453429f56bSmatthias.ringwald default: 11463429f56bSmatthias.ringwald break; 1147475c8125Smatthias.ringwald } 11483429f56bSmatthias.ringwald hci_stack.substate++; 11493429f56bSmatthias.ringwald break; 1150c7e0c5f6Smatthias.ringwald 1151c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1152c7e0c5f6Smatthias.ringwald 11537b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1154c7e0c5f6Smatthias.ringwald // close all open connections 1155c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1156c7e0c5f6Smatthias.ringwald if (connection){ 115732ab9390Smatthias.ringwald 1158c7e0c5f6Smatthias.ringwald // send disconnect 115932ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 116032ab9390Smatthias.ringwald 116179bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11626ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1163c7e0c5f6Smatthias.ringwald 1164c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1165c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1166c7e0c5f6Smatthias.ringwald return; 1167c7e0c5f6Smatthias.ringwald } 11687b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1169c7e0c5f6Smatthias.ringwald 117072ea5239Smatthias.ringwald // switch mode 1171c7e0c5f6Smatthias.ringwald hci_power_control_off(); 11729418f9c9Smatthias.ringwald 11737b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 117472ea5239Smatthias.ringwald hci_emit_state(); 11757b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 117672ea5239Smatthias.ringwald break; 1177c7e0c5f6Smatthias.ringwald 117872ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 117989db417bSmatthias.ringwald switch(hci_stack.substate) { 118089db417bSmatthias.ringwald case 0: 11817b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 118272ea5239Smatthias.ringwald // close all open connections 118372ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 118466da7044Smatthias.ringwald 118528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 118666da7044Smatthias.ringwald // don't close connections, if H4 supports power management 118766da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 118866da7044Smatthias.ringwald connection = NULL; 118966da7044Smatthias.ringwald } 119066da7044Smatthias.ringwald #endif 119172ea5239Smatthias.ringwald if (connection){ 119232ab9390Smatthias.ringwald 119372ea5239Smatthias.ringwald // send disconnect 119432ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 119532ab9390Smatthias.ringwald 119679bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11976ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 119872ea5239Smatthias.ringwald 119972ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 120072ea5239Smatthias.ringwald hci_shutdown_connection(connection); 120172ea5239Smatthias.ringwald return; 120272ea5239Smatthias.ringwald } 120372ea5239Smatthias.ringwald 120489db417bSmatthias.ringwald // disable page and inquiry scan 120532ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 120632ab9390Smatthias.ringwald 1207758b46ceSmatthias.ringwald log_info("HCI_STATE_HALTING, disabling inq cans\n"); 1208758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 120989db417bSmatthias.ringwald 121089db417bSmatthias.ringwald // continue in next sub state 121189db417bSmatthias.ringwald hci_stack.substate++; 121289db417bSmatthias.ringwald break; 121389db417bSmatthias.ringwald case 1: 121489db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 121589db417bSmatthias.ringwald break; 121689db417bSmatthias.ringwald case 2: 12177b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 121828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 121928171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 122028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 122128171530Smatthias.ringwald // SLEEP MODE reached 122228171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 122328171530Smatthias.ringwald hci_emit_state(); 122428171530Smatthias.ringwald break; 122528171530Smatthias.ringwald } 122628171530Smatthias.ringwald #endif 122772ea5239Smatthias.ringwald // switch mode 122889db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1229c7e0c5f6Smatthias.ringwald hci_emit_state(); 123028171530Smatthias.ringwald break; 123128171530Smatthias.ringwald 123289db417bSmatthias.ringwald default: 123389db417bSmatthias.ringwald break; 123489db417bSmatthias.ringwald } 1235c7e0c5f6Smatthias.ringwald break; 1236c7e0c5f6Smatthias.ringwald 12373429f56bSmatthias.ringwald default: 12383429f56bSmatthias.ringwald break; 12391f504dbdSmatthias.ringwald } 12403429f56bSmatthias.ringwald } 124116833f0aSmatthias.ringwald 124231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1243c8e4258aSmatthias.ringwald bd_addr_t addr; 1244c8e4258aSmatthias.ringwald hci_connection_t * conn; 1245c8e4258aSmatthias.ringwald // house-keeping 1246c8e4258aSmatthias.ringwald 1247c8e4258aSmatthias.ringwald // create_connection? 1248c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1249c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1250339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1251c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1252c8e4258aSmatthias.ringwald if (conn) { 1253c8e4258aSmatthias.ringwald // if connection exists 1254c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 125517f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 125617f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1257c8e4258aSmatthias.ringwald } 125817f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1259c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1260c8e4258aSmatthias.ringwald 126117f1ba2aSmatthias.ringwald } 1262c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 126317f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 126417f1ba2aSmatthias.ringwald if (!conn){ 126517f1ba2aSmatthias.ringwald // notify client that alloc failed 126617f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 126717f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 126817f1ba2aSmatthias.ringwald } 1269c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1270c8e4258aSmatthias.ringwald } 1271c8e4258aSmatthias.ringwald 12727fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 12737fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 12747fde4af9Smatthias.ringwald } 12757fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 12767fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 12777fde4af9Smatthias.ringwald } 12787fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 12797fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 12807fde4af9Smatthias.ringwald } 12817fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 12827fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 12837fde4af9Smatthias.ringwald } 12847fde4af9Smatthias.ringwald 12858ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 12868ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 12878ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 12888ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 12898ef73945Smatthias.ringwald } 12908ef73945Smatthias.ringwald } 1291c8e4258aSmatthias.ringwald 129231452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1293622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 129431452debSmatthias.ringwald } 12958adf0ddaSmatthias.ringwald 12961cd208adSmatthias.ringwald /** 12971cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 12981cd208adSmatthias.ringwald */ 1299fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 13001cd208adSmatthias.ringwald va_list argptr; 13011cd208adSmatthias.ringwald va_start(argptr, cmd); 13027dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 13031cd208adSmatthias.ringwald va_end(argptr); 13047dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 130593b8dc03Smatthias.ringwald } 1306c8e4258aSmatthias.ringwald 1307ee091cf1Smatthias.ringwald // Create various non-HCI events. 1308ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1309ee091cf1Smatthias.ringwald 1310c8e4258aSmatthias.ringwald void hci_emit_state(){ 1311e0abb8e7S[email protected] log_info("BTSTACK_EVENT_STATE %u", hci_stack.state); 1312425d1371Smatthias.ringwald uint8_t event[3]; 131380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1314425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1315c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1316425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1317425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1318c8e4258aSmatthias.ringwald } 1319c8e4258aSmatthias.ringwald 132017f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1321425d1371Smatthias.ringwald uint8_t event[13]; 1322c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1323425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 132417f1ba2aSmatthias.ringwald event[2] = status; 1325c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1326c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1327c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1328c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1329425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1330425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1331c8e4258aSmatthias.ringwald } 1332c8e4258aSmatthias.ringwald 13333c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1334425d1371Smatthias.ringwald uint8_t event[6]; 13353c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1336e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 13373c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 13383c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 13393c4d4b90Smatthias.ringwald event[5] = reason; 1340425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1341425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13423c4d4b90Smatthias.ringwald } 13433c4d4b90Smatthias.ringwald 1344ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1345e0abb8e7S[email protected] log_info("L2CAP_EVENT_TIMEOUT_CHECK"); 1346425d1371Smatthias.ringwald uint8_t event[4]; 134780d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1348425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1349ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1350425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1351425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1352ee091cf1Smatthias.ringwald } 135343bfb1bdSmatthias.ringwald 135443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1355e0abb8e7S[email protected] log_info("BTSTACK_EVENT_NR_CONNECTIONS_CHANGED %u", nr_hci_connections()); 1356425d1371Smatthias.ringwald uint8_t event[3]; 135780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1358425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 135943bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1360425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1361425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 136243bfb1bdSmatthias.ringwald } 1363038bc64cSmatthias.ringwald 1364038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1365e0abb8e7S[email protected] log_info("BTSTACK_EVENT_POWERON_FAILED"); 1366425d1371Smatthias.ringwald uint8_t event[2]; 136780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1368425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1369425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1370425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1371038bc64cSmatthias.ringwald } 13721b0e3922Smatthias.ringwald 137309ba8edeSmatthias.ringwald #ifndef EMBEDDED 13741b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1375e0abb8e7S[email protected] log_info("BTSTACK_EVENT_VERSION %u.%u", BTSTACK_MAJOR, BTSTACK_MINOR); 1376425d1371Smatthias.ringwald uint8_t event[6]; 13771b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1378425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1379425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1380425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1381425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1382425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1383425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13841b0e3922Smatthias.ringwald } 138509ba8edeSmatthias.ringwald #endif 13861b0e3922Smatthias.ringwald 13872ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1388e0abb8e7S[email protected] log_info("BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED %u", enabled); 1389425d1371Smatthias.ringwald uint8_t event[3]; 13902ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1391425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 13922ed6235cSmatthias.ringwald event[2] = enabled; 1393425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1394e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13952ed6235cSmatthias.ringwald } 1396627c2f45Smatthias.ringwald 1397627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1398e0abb8e7S[email protected] uint8_t event[2+1+6+248+1]; // +1 for \0 in log_info 1399627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1400e0abb8e7S[email protected] event[1] = sizeof(event) - 2 - 1; 1401f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 14022f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1403f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1404e0abb8e7S[email protected] 1405e0abb8e7S[email protected] event[9+248] = 0; // assert \0 for log_info 1406e0abb8e7S[email protected] log_info("BTSTACK_EVENT_REMOTE_NAME_CACHED %s = '%s'", bd_addr_to_str(*addr), &event[9]); 1407e0abb8e7S[email protected] 1408e0abb8e7S[email protected] hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)-1); 1409e0abb8e7S[email protected] hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)-1); 1410627c2f45Smatthias.ringwald } 1411381fbed8Smatthias.ringwald 1412381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1413e0abb8e7S[email protected] log_info("BTSTACK_EVENT_DISCOVERABLE_ENABLED %u", enabled); 1414425d1371Smatthias.ringwald uint8_t event[3]; 1415381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1416425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1417381fbed8Smatthias.ringwald event[2] = enabled; 1418425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1419425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1420381fbed8Smatthias.ringwald } 1421