11f504dbdSmatthias.ringwald /* 21713bceaSmatthias.ringwald * Copyright (C) 2009 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. 161713bceaSmatthias.ringwald * 171713bceaSmatthias.ringwald * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 181713bceaSmatthias.ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 191713bceaSmatthias.ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 201713bceaSmatthias.ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 211713bceaSmatthias.ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 221713bceaSmatthias.ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 231713bceaSmatthias.ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 241713bceaSmatthias.ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 251713bceaSmatthias.ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 261713bceaSmatthias.ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 271713bceaSmatthias.ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 281713bceaSmatthias.ringwald * SUCH DAMAGE. 291713bceaSmatthias.ringwald * 301713bceaSmatthias.ringwald */ 311713bceaSmatthias.ringwald 321713bceaSmatthias.ringwald /* 331f504dbdSmatthias.ringwald * hci.c 341f504dbdSmatthias.ringwald * 351f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 361f504dbdSmatthias.ringwald * 371f504dbdSmatthias.ringwald */ 381f504dbdSmatthias.ringwald 39c8901d41Smatthias.ringwald #include "config.h" 4028171530Smatthias.ringwald 417f2435e6Smatthias.ringwald #include "hci.h" 427f2435e6Smatthias.ringwald 4393b8dc03Smatthias.ringwald #include <stdarg.h> 4493b8dc03Smatthias.ringwald #include <string.h> 4556fe0872Smatthias.ringwald #include <stdio.h> 467f2435e6Smatthias.ringwald 47549e6ebeSmatthias.ringwald #ifndef EMBEDDED 48549e6ebeSmatthias.ringwald #include <unistd.h> // gethostbyname 4909ba8edeSmatthias.ringwald #include <btstack/version.h> 50549e6ebeSmatthias.ringwald #endif 51549e6ebeSmatthias.ringwald 52a3b02b71Smatthias.ringwald #include "btstack_memory.h" 537f2435e6Smatthias.ringwald #include "debug.h" 54d8905019Smatthias.ringwald #include "hci_dump.h" 5593b8dc03Smatthias.ringwald 56ae1fd9f3Smatthias.ringwald #include <btstack/hci_cmds.h> 571b0e3922Smatthias.ringwald 58169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 59ee091cf1Smatthias.ringwald 6028171530Smatthias.ringwald #ifdef USE_BLUETOOL 6128171530Smatthias.ringwald #include "bt_control_iphone.h" 6228171530Smatthias.ringwald #endif 6328171530Smatthias.ringwald 64758b46ceSmatthias.ringwald static void hci_update_scan_enable(void); 65758b46ceSmatthias.ringwald 6606b35ec0Smatthias.ringwald // the STACK is here 6716833f0aSmatthias.ringwald static hci_stack_t hci_stack; 6816833f0aSmatthias.ringwald 6997addcc5Smatthias.ringwald /** 70ee091cf1Smatthias.ringwald * get connection for a given handle 71ee091cf1Smatthias.ringwald * 72ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 73ee091cf1Smatthias.ringwald */ 74645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 75ee091cf1Smatthias.ringwald linked_item_t *it; 76ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 77ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 78ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 79ee091cf1Smatthias.ringwald } 80ee091cf1Smatthias.ringwald } 81ee091cf1Smatthias.ringwald return NULL; 82ee091cf1Smatthias.ringwald } 83ee091cf1Smatthias.ringwald 842b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 85*28ca2b46S[email protected] hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 86c785ef68Smatthias.ringwald #ifdef HAVE_TIME 87ee091cf1Smatthias.ringwald struct timeval tv; 88ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 89c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 90ee091cf1Smatthias.ringwald // connections might be timed out 91ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 92ee091cf1Smatthias.ringwald } 932b12a0b9Smatthias.ringwald #endif 94e5780900Smatthias.ringwald #ifdef HAVE_TICK 95c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 96c785ef68Smatthias.ringwald // connections might be timed out 97c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 98c785ef68Smatthias.ringwald } 99c785ef68Smatthias.ringwald #endif 100c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 101c785ef68Smatthias.ringwald run_loop_add_timer(timer); 102c785ef68Smatthias.ringwald } 103ee091cf1Smatthias.ringwald 104ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 105c7492964Smatthias.ringwald #ifdef HAVE_TIME 106ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 107c7492964Smatthias.ringwald #endif 108e5780900Smatthias.ringwald #ifdef HAVE_TICK 109c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 110c785ef68Smatthias.ringwald #endif 111ee091cf1Smatthias.ringwald } 112ee091cf1Smatthias.ringwald 113ee091cf1Smatthias.ringwald /** 114c8e4258aSmatthias.ringwald * create connection for given address 115c8e4258aSmatthias.ringwald * 11617f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 117c8e4258aSmatthias.ringwald */ 118c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 119*28ca2b46S[email protected] hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 120c8e4258aSmatthias.ringwald if (!conn) return NULL; 121c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 122c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1237d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 124ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 125ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 126ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 127d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1287856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 12956cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 130c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 131c8e4258aSmatthias.ringwald return conn; 132c8e4258aSmatthias.ringwald } 133c8e4258aSmatthias.ringwald 134c8e4258aSmatthias.ringwald /** 13506b35ec0Smatthias.ringwald * get connection for given address 13697addcc5Smatthias.ringwald * 13797addcc5Smatthias.ringwald * @return connection OR NULL, if not found 13897addcc5Smatthias.ringwald */ 139fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 14006b35ec0Smatthias.ringwald linked_item_t *it; 14106b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 14206b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 14306b35ec0Smatthias.ringwald return (hci_connection_t *) it; 14406b35ec0Smatthias.ringwald } 14506b35ec0Smatthias.ringwald } 14606b35ec0Smatthias.ringwald return NULL; 14706b35ec0Smatthias.ringwald } 14806b35ec0Smatthias.ringwald 149*28ca2b46S[email protected] inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 150*28ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 151*28ca2b46S[email protected] } 152*28ca2b46S[email protected] 153*28ca2b46S[email protected] inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 154*28ca2b46S[email protected] conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 155*28ca2b46S[email protected] } 156*28ca2b46S[email protected] 157*28ca2b46S[email protected] 15843bfb1bdSmatthias.ringwald /** 15980ca58a0Smatthias.ringwald * add authentication flags and reset timer 1607fde4af9Smatthias.ringwald */ 1617fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1627fde4af9Smatthias.ringwald bd_addr_t addr; 1637fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1647fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1657fde4af9Smatthias.ringwald if (conn) { 166*28ca2b46S[email protected] connectionSetAuthenticationFlags(conn, flags); 16780ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1687fde4af9Smatthias.ringwald } 1697fde4af9Smatthias.ringwald } 1707fde4af9Smatthias.ringwald 17180ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 17280ca58a0Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 17380ca58a0Smatthias.ringwald if (!conn) return 0; 17480ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 17580ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 17680ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 17780ca58a0Smatthias.ringwald return 1; 17880ca58a0Smatthias.ringwald } 17980ca58a0Smatthias.ringwald 180c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 181c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 182c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 183c12e46e7Smatthias.ringwald } 184c12e46e7Smatthias.ringwald } 185c12e46e7Smatthias.ringwald 1867fde4af9Smatthias.ringwald 1877fde4af9Smatthias.ringwald /** 18843bfb1bdSmatthias.ringwald * count connections 18943bfb1bdSmatthias.ringwald */ 19040d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 19156c253c9Smatthias.ringwald int count = 0; 19243bfb1bdSmatthias.ringwald linked_item_t *it; 19343bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 19443bfb1bdSmatthias.ringwald return count; 19543bfb1bdSmatthias.ringwald } 196c8e4258aSmatthias.ringwald 19797addcc5Smatthias.ringwald /** 198ba681a6cSmatthias.ringwald * Dummy handler called by HCI 19916833f0aSmatthias.ringwald */ 2002718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 20116833f0aSmatthias.ringwald } 20216833f0aSmatthias.ringwald 203998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 204998906cdSmatthias.ringwald hci_connection_t * connection = connection_for_handle(handle); 205998906cdSmatthias.ringwald if (!connection) { 2067d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 207998906cdSmatthias.ringwald return 0; 208998906cdSmatthias.ringwald } 209998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 210998906cdSmatthias.ringwald } 211998906cdSmatthias.ringwald 212998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 213998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 214998906cdSmatthias.ringwald linked_item_t *it; 215998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 216998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 217998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2187d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 219998906cdSmatthias.ringwald return 0; 220998906cdSmatthias.ringwald } 221998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 222998906cdSmatthias.ringwald } 223998906cdSmatthias.ringwald return free_slots; 224998906cdSmatthias.ringwald } 225998906cdSmatthias.ringwald 226c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2272b12a0b9Smatthias.ringwald 2282b12a0b9Smatthias.ringwald // check for async hci transport implementations 2292b12a0b9Smatthias.ringwald if (hci_stack.hci_transport->can_send_packet_now){ 2302b12a0b9Smatthias.ringwald if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 2312b12a0b9Smatthias.ringwald return 0; 2322b12a0b9Smatthias.ringwald } 2332b12a0b9Smatthias.ringwald } 2342b12a0b9Smatthias.ringwald 2352b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 236c24735b1Smatthias.ringwald switch (packet_type) { 237c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 238c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 239c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 240de009a8cSmatthias.ringwald return hci_stack.num_cmd_packets; 241c24735b1Smatthias.ringwald default: 242c24735b1Smatthias.ringwald return 0; 243c24735b1Smatthias.ringwald } 244c24735b1Smatthias.ringwald } 245c24735b1Smatthias.ringwald 246ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2477856c818Smatthias.ringwald 2486218e6f1Smatthias.ringwald // check for free places on BT module 2495932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2506218e6f1Smatthias.ringwald 2517856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2527856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 25356cf178bSmatthias.ringwald if (!connection) return 0; 25456cf178bSmatthias.ringwald hci_connection_timestamp(connection); 25556cf178bSmatthias.ringwald 25656cf178bSmatthias.ringwald // count packet 25756cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2587b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2597856c818Smatthias.ringwald 26000d8e42eSmatthias.ringwald // send packet 26100d8e42eSmatthias.ringwald int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2626218e6f1Smatthias.ringwald 26300d8e42eSmatthias.ringwald return err; 264ee091cf1Smatthias.ringwald } 265ee091cf1Smatthias.ringwald 26616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2677856c818Smatthias.ringwald 2687856c818Smatthias.ringwald // get info 2697856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2707856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 2717856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2727856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2737856c818Smatthias.ringwald 2747856c818Smatthias.ringwald // ignore non-registered handle 2757856c818Smatthias.ringwald if (!conn){ 2767d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2777856c818Smatthias.ringwald return; 2787856c818Smatthias.ringwald } 2797856c818Smatthias.ringwald 2807856c818Smatthias.ringwald // update idle timestamp 2817856c818Smatthias.ringwald hci_connection_timestamp(conn); 2827856c818Smatthias.ringwald 2837856c818Smatthias.ringwald // handle different packet types 2847856c818Smatthias.ringwald switch (acl_flags & 0x03) { 2857856c818Smatthias.ringwald 2867856c818Smatthias.ringwald case 0x01: // continuation fragment 2877856c818Smatthias.ringwald 2887856c818Smatthias.ringwald // sanity check 2897856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 2907d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 2917856c818Smatthias.ringwald return; 2927856c818Smatthias.ringwald } 2937856c818Smatthias.ringwald 2947856c818Smatthias.ringwald // append fragment payload (header already stored) 2957856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 2967856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 2977856c818Smatthias.ringwald 2987d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 299decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 3007856c818Smatthias.ringwald 3017856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 3027856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 3037856c818Smatthias.ringwald 3042718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 3057856c818Smatthias.ringwald // reset recombination buffer 3067856c818Smatthias.ringwald conn->acl_recombination_length = 0; 3077856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 3087856c818Smatthias.ringwald } 3097856c818Smatthias.ringwald break; 3107856c818Smatthias.ringwald 3117856c818Smatthias.ringwald case 0x02: { // first fragment 3127856c818Smatthias.ringwald 3137856c818Smatthias.ringwald // sanity check 3147856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3157d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3167856c818Smatthias.ringwald return; 3177856c818Smatthias.ringwald } 3187856c818Smatthias.ringwald 3197856c818Smatthias.ringwald // peek into L2CAP packet! 3207856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3217856c818Smatthias.ringwald 3227d67539fSmatthias.ringwald // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 323decc01a8Smatthias.ringwald 3247856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3257856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3267856c818Smatthias.ringwald 3277856c818Smatthias.ringwald // forward fragment as L2CAP packet 3282718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3297856c818Smatthias.ringwald 3307856c818Smatthias.ringwald } else { 3317856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3327856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3337856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3347856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 335decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3367856c818Smatthias.ringwald } 3377856c818Smatthias.ringwald break; 3387856c818Smatthias.ringwald 3397856c818Smatthias.ringwald } 3407856c818Smatthias.ringwald default: 3417d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3427856c818Smatthias.ringwald return; 3437856c818Smatthias.ringwald } 34494ab26f8Smatthias.ringwald 34594ab26f8Smatthias.ringwald // execute main loop 34694ab26f8Smatthias.ringwald hci_run(); 34716833f0aSmatthias.ringwald } 34822909952Smatthias.ringwald 34967a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 350339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3513c4d4b90Smatthias.ringwald 3523c4d4b90Smatthias.ringwald // cancel all l2cap connections 3533c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3543c4d4b90Smatthias.ringwald 355c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 356c785ef68Smatthias.ringwald 357c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 358a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3593c4d4b90Smatthias.ringwald 3603c4d4b90Smatthias.ringwald // now it's gone 361c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 362c7e0c5f6Smatthias.ringwald } 363c7e0c5f6Smatthias.ringwald 3640c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3658f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3668f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3678f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3688f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3698f8108aaSmatthias.ringwald }; 3708f8108aaSmatthias.ringwald 3718f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){ 3728f8108aaSmatthias.ringwald uint16_t packet_types = 0; 3738f8108aaSmatthias.ringwald int i; 3748f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 3758f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 3768f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 3778f8108aaSmatthias.ringwald packet_types |= 1 << i; 3788f8108aaSmatthias.ringwald } 3798f8108aaSmatthias.ringwald } 3808f8108aaSmatthias.ringwald // flip bits for "may not be used" 3818f8108aaSmatthias.ringwald packet_types ^= 0x3306; 3828f8108aaSmatthias.ringwald return packet_types; 3838f8108aaSmatthias.ringwald } 3848f8108aaSmatthias.ringwald 3858f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 3868f8108aaSmatthias.ringwald return hci_stack.packet_types; 3878f8108aaSmatthias.ringwald } 3888f8108aaSmatthias.ringwald 3897dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 3907dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 3917dc17943Smatthias.ringwald return hci_stack.hci_packet_buffer; 3927dc17943Smatthias.ringwald } 3937dc17943Smatthias.ringwald 3947dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){ 3957dc17943Smatthias.ringwald return hci_stack.acl_data_packet_length; 3967dc17943Smatthias.ringwald } 3977dc17943Smatthias.ringwald 39874ec757aSmatthias.ringwald // avoid huge local variables 399223aafc1Smatthias.ringwald #ifndef EMBEDDED 40074ec757aSmatthias.ringwald static device_name_t device_name; 401223aafc1Smatthias.ringwald #endif 40216833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 4031281a47eSmatthias.ringwald bd_addr_t addr; 4042d00edd4Smatthias.ringwald uint8_t link_type; 405fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 4061f7b95a1Smatthias.ringwald hci_connection_t * conn; 40756cf178bSmatthias.ringwald int i; 40822909952Smatthias.ringwald 4095909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 4105909f7f2Smatthias.ringwald 4116772a24cSmatthias.ringwald switch (packet[0]) { 41222909952Smatthias.ringwald 4136772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4147ec5eeaaSmatthias.ringwald // get num cmd packets 4157b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4167ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4177ec5eeaaSmatthias.ringwald 418e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 419e2edc0c3Smatthias.ringwald // from offset 5 420e2edc0c3Smatthias.ringwald // status 4211d279b20Smatthias.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" 422e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 42356cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 424e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 42556cf178bSmatthias.ringwald // ignore: total num SCO packets 42656cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 427a7a04bd9Smatthias.ringwald // determine usable ACL payload size 4288fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 4298fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 4308f8108aaSmatthias.ringwald } 431a7a04bd9Smatthias.ringwald // determine usable ACL packet types 43228c93ceeSmatthias.ringwald hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); 4338f8108aaSmatthias.ringwald 4348fcba05dSmatthias.ringwald log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", 4358f8108aaSmatthias.ringwald hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); 436e2edc0c3Smatthias.ringwald } 43756cf178bSmatthias.ringwald } 438188981d2Smatthias.ringwald // Dump local address 439188981d2Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 440188981d2Smatthias.ringwald bd_addr_t addr; 441188981d2Smatthias.ringwald bt_flip_addr(addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 442188981d2Smatthias.ringwald log_info("Local Address, Status: 0x%02x: Addr: %s\n", 443188981d2Smatthias.ringwald packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(addr)); 444188981d2Smatthias.ringwald } 445381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 446381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 447381fbed8Smatthias.ringwald } 44856cf178bSmatthias.ringwald break; 44956cf178bSmatthias.ringwald 4507ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 4517ec5eeaaSmatthias.ringwald // get num cmd packets 4527b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 4537ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 4547ec5eeaaSmatthias.ringwald break; 4557ec5eeaaSmatthias.ringwald 45656cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 45756cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 45856cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 45956cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 46056cf178bSmatthias.ringwald conn = connection_for_handle(handle); 46156cf178bSmatthias.ringwald if (!conn){ 4627d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 46356cf178bSmatthias.ringwald continue; 46456cf178bSmatthias.ringwald } 46556cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 4667b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 46756cf178bSmatthias.ringwald } 4686772a24cSmatthias.ringwald break; 4696772a24cSmatthias.ringwald 4701f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 47137eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 47237eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 4732d00edd4Smatthias.ringwald link_type = packet[11]; 474339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 47537eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 4761f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 4771f7b95a1Smatthias.ringwald if (!conn) { 4781f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 4791f7b95a1Smatthias.ringwald } 480ce4c8fabSmatthias.ringwald if (!conn) { 481ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 482ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 483ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 484ce4c8fabSmatthias.ringwald break; 485ce4c8fabSmatthias.ringwald } 48632ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 48732ab9390Smatthias.ringwald hci_run(); 48837eaa4cfSmatthias.ringwald } else { 489ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 490ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 491ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 49237eaa4cfSmatthias.ringwald } 4931f7b95a1Smatthias.ringwald break; 4941f7b95a1Smatthias.ringwald 4956772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 496fe1ed1b8Smatthias.ringwald // Connection management 497fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 498339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 4991f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 500fe1ed1b8Smatthias.ringwald if (conn) { 501b448a0e7Smatthias.ringwald if (!packet[2]){ 502c8e4258aSmatthias.ringwald conn->state = OPEN; 503fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 504ee091cf1Smatthias.ringwald 505c785ef68Smatthias.ringwald // restart timer 506c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 507ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 508c785ef68Smatthias.ringwald 509339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 51043bfb1bdSmatthias.ringwald 51143bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 512b448a0e7Smatthias.ringwald } else { 513b448a0e7Smatthias.ringwald // connection failed, remove entry 514b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 515a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 516c12e46e7Smatthias.ringwald 517c12e46e7Smatthias.ringwald // if authentication error, also delete link key 518c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 519c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 520c12e46e7Smatthias.ringwald } 521fe1ed1b8Smatthias.ringwald } 522fe1ed1b8Smatthias.ringwald } 5236772a24cSmatthias.ringwald break; 524fe1ed1b8Smatthias.ringwald 5257fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 5267b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 5277fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 52874ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 52932ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 53064472d52Smatthias.ringwald hci_run(); 531d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 53229d53098Smatthias.ringwald return; 5337fde4af9Smatthias.ringwald 5347fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 5357fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 53674ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 53729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 538287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 53929d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 5407fde4af9Smatthias.ringwald break; 5417fde4af9Smatthias.ringwald 5427fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 5437fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 544d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 545d9a327f9Smatthias.ringwald if (!hci_stack.remote_device_db) break; 546d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 547d9a327f9Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 5487fde4af9Smatthias.ringwald break; 5497fde4af9Smatthias.ringwald 550223aafc1Smatthias.ringwald #ifndef EMBEDDED 55174ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 55274ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 55374ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 55474ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 5555a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 5565a06394aSmatthias.ringwald for (i=0; i<248;i++){ 5575a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 5585a06394aSmatthias.ringwald packet[9+i] = 0; 5595a06394aSmatthias.ringwald break; 560cdc9101dSmatthias.ringwald } 5615a06394aSmatthias.ringwald } 562d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 56374ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 56474ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 56574ec757aSmatthias.ringwald break; 56674ec757aSmatthias.ringwald 56774ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 56874ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 56974ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 57074ec757aSmatthias.ringwald // first send inq result packet 57174ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 57274ec757aSmatthias.ringwald // then send cached remote names 57374ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 57474ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 57574ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 57674ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 57774ec757aSmatthias.ringwald } 57874ec757aSmatthias.ringwald } 57974ec757aSmatthias.ringwald return; 580223aafc1Smatthias.ringwald #endif 58174ec757aSmatthias.ringwald 5826772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 583fe1ed1b8Smatthias.ringwald if (!packet[2]){ 584fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 585fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 586fe1ed1b8Smatthias.ringwald if (conn) { 587c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 588fe1ed1b8Smatthias.ringwald } 589fe1ed1b8Smatthias.ringwald } 5906772a24cSmatthias.ringwald break; 5916772a24cSmatthias.ringwald 592c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 593c68bdf90Smatthias.ringwald if(hci_stack.control->hw_error){ 594c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 595c68bdf90Smatthias.ringwald } 596c68bdf90Smatthias.ringwald break; 597c68bdf90Smatthias.ringwald 5985909f7f2Smatthias.ringwald #ifdef HAVE_BLE 5995909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 6005909f7f2Smatthias.ringwald switch (packet[2]) { 6015909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 6025909f7f2Smatthias.ringwald // Connection management 6035909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 6045909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 6055909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 6065909f7f2Smatthias.ringwald conn = connection_for_address(addr); 6075909f7f2Smatthias.ringwald if (packet[3]){ 6085909f7f2Smatthias.ringwald if (conn){ 6095909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 6105909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 6115909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 6125909f7f2Smatthias.ringwald 6135909f7f2Smatthias.ringwald } 6145909f7f2Smatthias.ringwald // if authentication error, also delete link key 6155909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 6165909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 6175909f7f2Smatthias.ringwald } 6185909f7f2Smatthias.ringwald break; 6195909f7f2Smatthias.ringwald } 6205909f7f2Smatthias.ringwald if (!conn){ 6215909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 6225909f7f2Smatthias.ringwald } 6235909f7f2Smatthias.ringwald if (!conn){ 6245909f7f2Smatthias.ringwald // no memory 6255909f7f2Smatthias.ringwald break; 6265909f7f2Smatthias.ringwald } 6275909f7f2Smatthias.ringwald 6285909f7f2Smatthias.ringwald conn->state = OPEN; 6295909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 6305909f7f2Smatthias.ringwald 6315909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 6325909f7f2Smatthias.ringwald 6335909f7f2Smatthias.ringwald // restart timer 6345909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 6355909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 6365909f7f2Smatthias.ringwald 6375909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 6385909f7f2Smatthias.ringwald 6395909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 6405909f7f2Smatthias.ringwald break; 6415909f7f2Smatthias.ringwald 6425909f7f2Smatthias.ringwald default: 6435909f7f2Smatthias.ringwald break; 6445909f7f2Smatthias.ringwald } 6455909f7f2Smatthias.ringwald break; 6465909f7f2Smatthias.ringwald #endif 6475909f7f2Smatthias.ringwald 6486772a24cSmatthias.ringwald default: 6496772a24cSmatthias.ringwald break; 650fe1ed1b8Smatthias.ringwald } 651fe1ed1b8Smatthias.ringwald 6523429f56bSmatthias.ringwald // handle BT initialization 6533429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 6547301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 6557301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 6567301ad89Smatthias.ringwald // hci_stack.substate = 0; 6577301ad89Smatthias.ringwald // } 6587301ad89Smatthias.ringwald // handle normal init sequence 6593429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 6603429f56bSmatthias.ringwald // odd: waiting for event 6613429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 6623429f56bSmatthias.ringwald hci_stack.substate++; 6633429f56bSmatthias.ringwald } 6643429f56bSmatthias.ringwald } 66522909952Smatthias.ringwald } 66622909952Smatthias.ringwald 66789db417bSmatthias.ringwald // help with BT sleep 66889db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 66989db417bSmatthias.ringwald && hci_stack.substate == 1 67089db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 67189db417bSmatthias.ringwald hci_stack.substate++; 67289db417bSmatthias.ringwald } 67389db417bSmatthias.ringwald 6742718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 67594ab26f8Smatthias.ringwald 67694ab26f8Smatthias.ringwald // execute main loop 67794ab26f8Smatthias.ringwald hci_run(); 67816833f0aSmatthias.ringwald } 67916833f0aSmatthias.ringwald 68010e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 68110e830c9Smatthias.ringwald switch (packet_type) { 68210e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 68310e830c9Smatthias.ringwald event_handler(packet, size); 68410e830c9Smatthias.ringwald break; 68510e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 68610e830c9Smatthias.ringwald acl_handler(packet, size); 68710e830c9Smatthias.ringwald break; 68810e830c9Smatthias.ringwald default: 68910e830c9Smatthias.ringwald break; 69010e830c9Smatthias.ringwald } 69110e830c9Smatthias.ringwald } 69210e830c9Smatthias.ringwald 693fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 6942718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 6952718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 69616833f0aSmatthias.ringwald } 69716833f0aSmatthias.ringwald 6984f4fc1dfSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 699475c8125Smatthias.ringwald 700475c8125Smatthias.ringwald // reference to use transport layer implementation 70116833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 702475c8125Smatthias.ringwald 70311e23e5fSmatthias.ringwald // references to used control implementation 70411e23e5fSmatthias.ringwald hci_stack.control = control; 70511e23e5fSmatthias.ringwald 70611e23e5fSmatthias.ringwald // reference to used config 70711e23e5fSmatthias.ringwald hci_stack.config = config; 70811e23e5fSmatthias.ringwald 709fe1ed1b8Smatthias.ringwald // no connections yet 710fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 7110a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 712c0e866bfSmatthias.ringwald hci_stack.connectable = 0; 713758b46ceSmatthias.ringwald 714b031bebbSmatthias.ringwald // no pending cmds 715b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 716b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 717b031bebbSmatthias.ringwald 71816833f0aSmatthias.ringwald // higher level handler 7192718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 72016833f0aSmatthias.ringwald 721404843c1Smatthias.ringwald // store and open remote device db 722404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 723404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 724404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 725404843c1Smatthias.ringwald } 72629d53098Smatthias.ringwald 7278fcba05dSmatthias.ringwald // max acl payload size defined in config.h 7288fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 7298fcba05dSmatthias.ringwald 73016833f0aSmatthias.ringwald // register packet handlers with transport 73110e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 732f5454fc6Smatthias.ringwald 733f5454fc6Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 734475c8125Smatthias.ringwald } 735475c8125Smatthias.ringwald 736404843c1Smatthias.ringwald void hci_close(){ 737404843c1Smatthias.ringwald // close remote device db 738404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 739404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 740404843c1Smatthias.ringwald } 741f5454fc6Smatthias.ringwald while (hci_stack.connections) { 742f5454fc6Smatthias.ringwald hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 743f5454fc6Smatthias.ringwald } 744f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 745404843c1Smatthias.ringwald } 746404843c1Smatthias.ringwald 7478d213e1aSmatthias.ringwald // State-Module-Driver overview 7488d213e1aSmatthias.ringwald // state module low-level 7498d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 7508d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 7518d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 7528d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 753d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 754d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 755c7e0c5f6Smatthias.ringwald 75640d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 7577301ad89Smatthias.ringwald 758038bc64cSmatthias.ringwald // power on 759f9a30166Smatthias.ringwald int err = 0; 760f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 761f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 762f9a30166Smatthias.ringwald } 763038bc64cSmatthias.ringwald if (err){ 7647d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 765038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 766038bc64cSmatthias.ringwald return err; 767038bc64cSmatthias.ringwald } 768038bc64cSmatthias.ringwald 769038bc64cSmatthias.ringwald // open low-level device 770038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 771038bc64cSmatthias.ringwald if (err){ 7727d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 773f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 774f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 775f9a30166Smatthias.ringwald } 776038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 777038bc64cSmatthias.ringwald return err; 778038bc64cSmatthias.ringwald } 7798d213e1aSmatthias.ringwald return 0; 7808d213e1aSmatthias.ringwald } 781038bc64cSmatthias.ringwald 78240d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 7838d213e1aSmatthias.ringwald 7847b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 7859418f9c9Smatthias.ringwald 7868d213e1aSmatthias.ringwald // close low-level device 7878d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 7888d213e1aSmatthias.ringwald 7897b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 7909418f9c9Smatthias.ringwald 7918d213e1aSmatthias.ringwald // power off 7928d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 7938d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 7948d213e1aSmatthias.ringwald } 7959418f9c9Smatthias.ringwald 7967b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 7979418f9c9Smatthias.ringwald 79872ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 79972ea5239Smatthias.ringwald } 80072ea5239Smatthias.ringwald 80140d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 80272ea5239Smatthias.ringwald 8037b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 8043144bce4Smatthias.ringwald 805b429b9b7Smatthias.ringwald #if 0 806b429b9b7Smatthias.ringwald // don't close serial port during sleep 807b429b9b7Smatthias.ringwald 80872ea5239Smatthias.ringwald // close low-level device 80972ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 810b429b9b7Smatthias.ringwald #endif 81172ea5239Smatthias.ringwald 81272ea5239Smatthias.ringwald // sleep mode 8133144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 81472ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 81572ea5239Smatthias.ringwald } 816b429b9b7Smatthias.ringwald 81772ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 8188d213e1aSmatthias.ringwald } 8198d213e1aSmatthias.ringwald 82040d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 821b429b9b7Smatthias.ringwald 8227b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 823b429b9b7Smatthias.ringwald 824b429b9b7Smatthias.ringwald // wake on 825b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 826b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 827b429b9b7Smatthias.ringwald } 828b429b9b7Smatthias.ringwald 829b429b9b7Smatthias.ringwald #if 0 830b429b9b7Smatthias.ringwald // open low-level device 831b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 832b429b9b7Smatthias.ringwald if (err){ 8337d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 834b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 835b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 836b429b9b7Smatthias.ringwald } 837b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 838b429b9b7Smatthias.ringwald return err; 839b429b9b7Smatthias.ringwald } 840b429b9b7Smatthias.ringwald #endif 841b429b9b7Smatthias.ringwald 842b429b9b7Smatthias.ringwald return 0; 843b429b9b7Smatthias.ringwald } 844b429b9b7Smatthias.ringwald 845b429b9b7Smatthias.ringwald 8468d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 847d661ed19Smatthias.ringwald 8487b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 849d661ed19Smatthias.ringwald 8508d213e1aSmatthias.ringwald int err = 0; 8518d213e1aSmatthias.ringwald switch (hci_stack.state){ 8528d213e1aSmatthias.ringwald 8538d213e1aSmatthias.ringwald case HCI_STATE_OFF: 8548d213e1aSmatthias.ringwald switch (power_mode){ 8558d213e1aSmatthias.ringwald case HCI_POWER_ON: 8568d213e1aSmatthias.ringwald err = hci_power_control_on(); 8578d213e1aSmatthias.ringwald if (err) return err; 8587301ad89Smatthias.ringwald // set up state machine 8597301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 8607301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8617301ad89Smatthias.ringwald hci_stack.substate = 0; 8628d213e1aSmatthias.ringwald break; 8638d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8648d213e1aSmatthias.ringwald // do nothing 8658d213e1aSmatthias.ringwald break; 8668d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 867b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 8688d213e1aSmatthias.ringwald break; 8698d213e1aSmatthias.ringwald } 8708d213e1aSmatthias.ringwald break; 8717301ad89Smatthias.ringwald 8728d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 8738d213e1aSmatthias.ringwald switch (power_mode){ 8748d213e1aSmatthias.ringwald case HCI_POWER_ON: 8758d213e1aSmatthias.ringwald // do nothing 8768d213e1aSmatthias.ringwald break; 8778d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8788d213e1aSmatthias.ringwald // no connections yet, just turn it off 8798d213e1aSmatthias.ringwald hci_power_control_off(); 8808d213e1aSmatthias.ringwald break; 8818d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 882b546ac54Smatthias.ringwald // no connections yet, just turn it off 88372ea5239Smatthias.ringwald hci_power_control_sleep(); 8848d213e1aSmatthias.ringwald break; 8858d213e1aSmatthias.ringwald } 8868d213e1aSmatthias.ringwald break; 8877301ad89Smatthias.ringwald 8888d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 8898d213e1aSmatthias.ringwald switch (power_mode){ 8908d213e1aSmatthias.ringwald case HCI_POWER_ON: 8918d213e1aSmatthias.ringwald // do nothing 8928d213e1aSmatthias.ringwald break; 8938d213e1aSmatthias.ringwald case HCI_POWER_OFF: 894c7e0c5f6Smatthias.ringwald // see hci_run 895c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 8968d213e1aSmatthias.ringwald break; 8978d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 898b546ac54Smatthias.ringwald // see hci_run 899b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 90089db417bSmatthias.ringwald hci_stack.substate = 0; 9018d213e1aSmatthias.ringwald break; 9028d213e1aSmatthias.ringwald } 9038d213e1aSmatthias.ringwald break; 9047301ad89Smatthias.ringwald 9058d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 9068d213e1aSmatthias.ringwald switch (power_mode){ 9078d213e1aSmatthias.ringwald case HCI_POWER_ON: 9088d213e1aSmatthias.ringwald // set up state machine 9098d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9108d213e1aSmatthias.ringwald hci_stack.substate = 0; 9118d213e1aSmatthias.ringwald break; 9128d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9138d213e1aSmatthias.ringwald // do nothing 9148d213e1aSmatthias.ringwald break; 9158d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 916b546ac54Smatthias.ringwald // see hci_run 917b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 91889db417bSmatthias.ringwald hci_stack.substate = 0; 9198d213e1aSmatthias.ringwald break; 9208d213e1aSmatthias.ringwald } 9218d213e1aSmatthias.ringwald break; 9228d213e1aSmatthias.ringwald 9238d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 9248d213e1aSmatthias.ringwald switch (power_mode){ 9258d213e1aSmatthias.ringwald case HCI_POWER_ON: 92628171530Smatthias.ringwald 92728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 92828171530Smatthias.ringwald // nothing to do, if H4 supports power management 92928171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9308747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9318747d67fSmatthias.ringwald hci_stack.substate = 6; 93228171530Smatthias.ringwald break; 93328171530Smatthias.ringwald } 93428171530Smatthias.ringwald #endif 935b546ac54Smatthias.ringwald // set up state machine 936b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 937b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 938b546ac54Smatthias.ringwald hci_stack.substate = 0; 9398d213e1aSmatthias.ringwald break; 9408d213e1aSmatthias.ringwald case HCI_POWER_OFF: 941b546ac54Smatthias.ringwald // see hci_run 942b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9438d213e1aSmatthias.ringwald break; 9448d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 945b546ac54Smatthias.ringwald // do nothing 9468d213e1aSmatthias.ringwald break; 9478d213e1aSmatthias.ringwald } 9488d213e1aSmatthias.ringwald break; 9498d213e1aSmatthias.ringwald 9508d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 9518d213e1aSmatthias.ringwald switch (power_mode){ 9528d213e1aSmatthias.ringwald case HCI_POWER_ON: 95328171530Smatthias.ringwald 95428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 95528171530Smatthias.ringwald // nothing to do, if H4 supports power management 95628171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9578747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9588747d67fSmatthias.ringwald hci_stack.substate = 6; 959758b46ceSmatthias.ringwald hci_update_scan_enable(); 96028171530Smatthias.ringwald break; 96128171530Smatthias.ringwald } 96228171530Smatthias.ringwald #endif 9633144bce4Smatthias.ringwald err = hci_power_control_wake(); 9643144bce4Smatthias.ringwald if (err) return err; 965b546ac54Smatthias.ringwald // set up state machine 966b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 967b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 968b546ac54Smatthias.ringwald hci_stack.substate = 0; 9698d213e1aSmatthias.ringwald break; 9708d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9714ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9728d213e1aSmatthias.ringwald break; 9738d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 974b546ac54Smatthias.ringwald // do nothing 9758d213e1aSmatthias.ringwald break; 9768d213e1aSmatthias.ringwald } 9778d213e1aSmatthias.ringwald break; 97811e23e5fSmatthias.ringwald } 97968d92d03Smatthias.ringwald 980038bc64cSmatthias.ringwald // create internal event 981ee8bf225Smatthias.ringwald hci_emit_state(); 982ee8bf225Smatthias.ringwald 98368d92d03Smatthias.ringwald // trigger next/first action 98468d92d03Smatthias.ringwald hci_run(); 98568d92d03Smatthias.ringwald 986475c8125Smatthias.ringwald return 0; 987475c8125Smatthias.ringwald } 988475c8125Smatthias.ringwald 989758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 990758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 991758b46ceSmatthias.ringwald hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 992758b46ceSmatthias.ringwald hci_run(); 993758b46ceSmatthias.ringwald } 994758b46ceSmatthias.ringwald 995381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 996381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 997381fbed8Smatthias.ringwald 998381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 999381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 1000381fbed8Smatthias.ringwald return; 1001381fbed8Smatthias.ringwald } 1002381fbed8Smatthias.ringwald 1003381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 1004758b46ceSmatthias.ringwald hci_update_scan_enable(); 1005758b46ceSmatthias.ringwald } 1006b031bebbSmatthias.ringwald 1007758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 1008758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 1009758b46ceSmatthias.ringwald 1010758b46ceSmatthias.ringwald // don't emit event 1011758b46ceSmatthias.ringwald if (hci_stack.connectable == enable) return; 1012758b46ceSmatthias.ringwald 1013758b46ceSmatthias.ringwald hci_stack.connectable = enable; 1014758b46ceSmatthias.ringwald hci_update_scan_enable(); 1015381fbed8Smatthias.ringwald } 1016381fbed8Smatthias.ringwald 101706b35ec0Smatthias.ringwald void hci_run(){ 10188a485f27Smatthias.ringwald 101932ab9390Smatthias.ringwald hci_connection_t * connection; 102032ab9390Smatthias.ringwald linked_item_t * it; 102132ab9390Smatthias.ringwald 1022ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1023ce4c8fabSmatthias.ringwald 1024b031bebbSmatthias.ringwald // global/non-connection oriented commands 1025b031bebbSmatthias.ringwald 1026b031bebbSmatthias.ringwald // decline incoming connections 1027ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 1028ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 1029ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 1030ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1031ce4c8fabSmatthias.ringwald } 1032ce4c8fabSmatthias.ringwald 1033b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1034b031bebbSmatthias.ringwald 1035b031bebbSmatthias.ringwald // send scan enable 1036eda18bf8Smatthias.ringwald if (hci_stack.new_scan_enable_value != 0xff){ 1037b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1038b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 1039b031bebbSmatthias.ringwald } 1040b031bebbSmatthias.ringwald 104132ab9390Smatthias.ringwald // send pending HCI commands 104232ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 104332ab9390Smatthias.ringwald 1044b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 104532ab9390Smatthias.ringwald 1046b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 104732ab9390Smatthias.ringwald 104832ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 10497b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 105032ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 105132ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1052c7e0c5f6Smatthias.ringwald } 1053c7e0c5f6Smatthias.ringwald 1054de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 105532ab9390Smatthias.ringwald 105632ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 105732ab9390Smatthias.ringwald link_key_t link_key; 10587b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 105932ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 106032ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 106132ab9390Smatthias.ringwald } else { 106232ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 106332ab9390Smatthias.ringwald } 1064*28ca2b46S[email protected] connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 106532ab9390Smatthias.ringwald } 106632ab9390Smatthias.ringwald } 106732ab9390Smatthias.ringwald 1068de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1069c7e0c5f6Smatthias.ringwald 10703429f56bSmatthias.ringwald switch (hci_stack.state){ 10713429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 10727b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 10733429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 10743429f56bSmatthias.ringwald // odd: waiting for command completion 107506b35ec0Smatthias.ringwald return; 10763429f56bSmatthias.ringwald } 107790919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1078c7492964Smatthias.ringwald case 0: // RESET 107922909952Smatthias.ringwald hci_send_cmd(&hci_reset); 1080c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1081c7492964Smatthias.ringwald // skip baud change 1082c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1083c7492964Smatthias.ringwald } 10843429f56bSmatthias.ringwald break; 1085c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 10867dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 10877dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1088c7492964Smatthias.ringwald break; 1089c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1090c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1091c7492964Smatthias.ringwald hci_stack.substate += 2; 1092c7492964Smatthias.ringwald // break missing here for fall through 1093c7492964Smatthias.ringwald 1094c7492964Smatthias.ringwald case 3: 109590919203Smatthias.ringwald // custom initialization 1096db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 10977dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1098d62aca9fSmatthias.ringwald if (valid_cmd){ 10997dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 11007dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1101c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 110290919203Smatthias.ringwald break; 110390919203Smatthias.ringwald } 1104d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 110590919203Smatthias.ringwald } 11062f6c30e1Smatthias.ringwald // otherwise continue 1107f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1108f432a6ddSmatthias.ringwald break; 1109c7492964Smatthias.ringwald case 4: 1110e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1111e2edc0c3Smatthias.ringwald break; 1112c7492964Smatthias.ringwald case 5: 11133429f56bSmatthias.ringwald // ca. 15 sec 11143429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 11153429f56bSmatthias.ringwald break; 1116c7492964Smatthias.ringwald case 6: 1117758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 1118f432a6ddSmatthias.ringwald break; 1119c7492964Smatthias.ringwald case 7: 11208a485f27Smatthias.ringwald #ifndef EMBEDDED 11218a485f27Smatthias.ringwald { 11228a485f27Smatthias.ringwald char hostname[30]; 11238a485f27Smatthias.ringwald gethostname(hostname, 30); 11248a485f27Smatthias.ringwald hostname[29] = '\0'; 11258a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 11268a485f27Smatthias.ringwald break; 11278a485f27Smatthias.ringwald } 1128c7492964Smatthias.ringwald case 8: 11293c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 11303c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 11313c4d4b90Smatthias.ringwald break; 11323c4d4b90Smatthias.ringwald 1133c7492964Smatthias.ringwald case 9: 11343c4d4b90Smatthias.ringwald #endif 11358a485f27Smatthias.ringwald #endif 11363429f56bSmatthias.ringwald // done. 11373429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1138b360b6adSmatthias.ringwald hci_emit_state(); 11393429f56bSmatthias.ringwald break; 11403429f56bSmatthias.ringwald default: 11413429f56bSmatthias.ringwald break; 1142475c8125Smatthias.ringwald } 11433429f56bSmatthias.ringwald hci_stack.substate++; 11443429f56bSmatthias.ringwald break; 1145c7e0c5f6Smatthias.ringwald 1146c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1147c7e0c5f6Smatthias.ringwald 11487b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1149c7e0c5f6Smatthias.ringwald // close all open connections 1150c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1151c7e0c5f6Smatthias.ringwald if (connection){ 115232ab9390Smatthias.ringwald 1153c7e0c5f6Smatthias.ringwald // send disconnect 115432ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 115532ab9390Smatthias.ringwald 115679bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11576ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1158c7e0c5f6Smatthias.ringwald 1159c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1160c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1161c7e0c5f6Smatthias.ringwald return; 1162c7e0c5f6Smatthias.ringwald } 11637b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1164c7e0c5f6Smatthias.ringwald 116572ea5239Smatthias.ringwald // switch mode 1166c7e0c5f6Smatthias.ringwald hci_power_control_off(); 11679418f9c9Smatthias.ringwald 11687b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 116972ea5239Smatthias.ringwald hci_emit_state(); 11707b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 117172ea5239Smatthias.ringwald break; 1172c7e0c5f6Smatthias.ringwald 117372ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 117489db417bSmatthias.ringwald switch(hci_stack.substate) { 117589db417bSmatthias.ringwald case 0: 11767b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 117772ea5239Smatthias.ringwald // close all open connections 117872ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 117966da7044Smatthias.ringwald 118028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 118166da7044Smatthias.ringwald // don't close connections, if H4 supports power management 118266da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 118366da7044Smatthias.ringwald connection = NULL; 118466da7044Smatthias.ringwald } 118566da7044Smatthias.ringwald #endif 118672ea5239Smatthias.ringwald if (connection){ 118732ab9390Smatthias.ringwald 118872ea5239Smatthias.ringwald // send disconnect 118932ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 119032ab9390Smatthias.ringwald 119179bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11926ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 119372ea5239Smatthias.ringwald 119472ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 119572ea5239Smatthias.ringwald hci_shutdown_connection(connection); 119672ea5239Smatthias.ringwald return; 119772ea5239Smatthias.ringwald } 119872ea5239Smatthias.ringwald 119989db417bSmatthias.ringwald // disable page and inquiry scan 120032ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 120132ab9390Smatthias.ringwald 1202758b46ceSmatthias.ringwald log_info("HCI_STATE_HALTING, disabling inq cans\n"); 1203758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 120489db417bSmatthias.ringwald 120589db417bSmatthias.ringwald // continue in next sub state 120689db417bSmatthias.ringwald hci_stack.substate++; 120789db417bSmatthias.ringwald break; 120889db417bSmatthias.ringwald case 1: 120989db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 121089db417bSmatthias.ringwald break; 121189db417bSmatthias.ringwald case 2: 12127b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 121328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 121428171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 121528171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 121628171530Smatthias.ringwald // SLEEP MODE reached 121728171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 121828171530Smatthias.ringwald hci_emit_state(); 121928171530Smatthias.ringwald break; 122028171530Smatthias.ringwald } 122128171530Smatthias.ringwald #endif 122272ea5239Smatthias.ringwald // switch mode 122389db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1224c7e0c5f6Smatthias.ringwald hci_emit_state(); 122528171530Smatthias.ringwald break; 122628171530Smatthias.ringwald 122789db417bSmatthias.ringwald default: 122889db417bSmatthias.ringwald break; 122989db417bSmatthias.ringwald } 1230c7e0c5f6Smatthias.ringwald break; 1231c7e0c5f6Smatthias.ringwald 12323429f56bSmatthias.ringwald default: 12333429f56bSmatthias.ringwald break; 12341f504dbdSmatthias.ringwald } 12353429f56bSmatthias.ringwald } 123616833f0aSmatthias.ringwald 123731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1238c8e4258aSmatthias.ringwald bd_addr_t addr; 1239c8e4258aSmatthias.ringwald hci_connection_t * conn; 1240c8e4258aSmatthias.ringwald // house-keeping 1241c8e4258aSmatthias.ringwald 1242c8e4258aSmatthias.ringwald // create_connection? 1243c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1244c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1245339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1246c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1247c8e4258aSmatthias.ringwald if (conn) { 1248c8e4258aSmatthias.ringwald // if connection exists 1249c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 125017f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 125117f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1252c8e4258aSmatthias.ringwald } 125317f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1254c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1255c8e4258aSmatthias.ringwald 125617f1ba2aSmatthias.ringwald } 1257c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 125817f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 125917f1ba2aSmatthias.ringwald if (!conn){ 126017f1ba2aSmatthias.ringwald // notify client that alloc failed 126117f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 126217f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 126317f1ba2aSmatthias.ringwald } 1264c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1265c8e4258aSmatthias.ringwald } 1266c8e4258aSmatthias.ringwald 12677fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 12687fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 12697fde4af9Smatthias.ringwald } 12707fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 12717fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 12727fde4af9Smatthias.ringwald } 12737fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 12747fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 12757fde4af9Smatthias.ringwald } 12767fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 12777fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 12787fde4af9Smatthias.ringwald } 12797fde4af9Smatthias.ringwald 12808ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 12818ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 12828ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 12838ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 12848ef73945Smatthias.ringwald } 12858ef73945Smatthias.ringwald } 1286c8e4258aSmatthias.ringwald 128731452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1288622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 128931452debSmatthias.ringwald } 12908adf0ddaSmatthias.ringwald 12911cd208adSmatthias.ringwald /** 12921cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 12931cd208adSmatthias.ringwald */ 1294fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 12951cd208adSmatthias.ringwald va_list argptr; 12961cd208adSmatthias.ringwald va_start(argptr, cmd); 12977dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 12981cd208adSmatthias.ringwald va_end(argptr); 12997dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 130093b8dc03Smatthias.ringwald } 1301c8e4258aSmatthias.ringwald 1302ee091cf1Smatthias.ringwald // Create various non-HCI events. 1303ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1304ee091cf1Smatthias.ringwald 1305c8e4258aSmatthias.ringwald void hci_emit_state(){ 1306425d1371Smatthias.ringwald uint8_t event[3]; 130780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1308425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1309c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1310425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1311425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1312c8e4258aSmatthias.ringwald } 1313c8e4258aSmatthias.ringwald 131417f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1315425d1371Smatthias.ringwald uint8_t event[13]; 1316c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1317425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 131817f1ba2aSmatthias.ringwald event[2] = status; 1319c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1320c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1321c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1322c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1323425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1324425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1325c8e4258aSmatthias.ringwald } 1326c8e4258aSmatthias.ringwald 13273c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1328425d1371Smatthias.ringwald uint8_t event[6]; 13293c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1330e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 13313c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 13323c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 13333c4d4b90Smatthias.ringwald event[5] = reason; 1334425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1335425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13363c4d4b90Smatthias.ringwald } 13373c4d4b90Smatthias.ringwald 1338ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1339425d1371Smatthias.ringwald uint8_t event[4]; 134080d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1341425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1342ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1343425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1344425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1345ee091cf1Smatthias.ringwald } 134643bfb1bdSmatthias.ringwald 134743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1348425d1371Smatthias.ringwald uint8_t event[3]; 134980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1350425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 135143bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1352425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1353425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 135443bfb1bdSmatthias.ringwald } 1355038bc64cSmatthias.ringwald 1356038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1357425d1371Smatthias.ringwald uint8_t event[2]; 135880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1359425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1360425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1361425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1362038bc64cSmatthias.ringwald } 13631b0e3922Smatthias.ringwald 136409ba8edeSmatthias.ringwald #ifndef EMBEDDED 13651b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1366425d1371Smatthias.ringwald uint8_t event[6]; 13671b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1368425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1369425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1370425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1371425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1372425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1373425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13741b0e3922Smatthias.ringwald } 137509ba8edeSmatthias.ringwald #endif 13761b0e3922Smatthias.ringwald 13772ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1378425d1371Smatthias.ringwald uint8_t event[3]; 13792ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1380425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 13812ed6235cSmatthias.ringwald event[2] = enabled; 1382425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1383e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13842ed6235cSmatthias.ringwald } 1385627c2f45Smatthias.ringwald 1386627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1387425d1371Smatthias.ringwald uint8_t event[2+1+6+248]; 1388627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1389425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1390f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 13912f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1392f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1393425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1394425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1395627c2f45Smatthias.ringwald } 1396381fbed8Smatthias.ringwald 1397381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1398425d1371Smatthias.ringwald uint8_t event[3]; 1399381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1400425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1401381fbed8Smatthias.ringwald event[2] = enabled; 1402425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1403425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1404381fbed8Smatthias.ringwald } 1405