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 6406b35ec0Smatthias.ringwald // the STACK is here 6516833f0aSmatthias.ringwald static hci_stack_t hci_stack; 6616833f0aSmatthias.ringwald 6797addcc5Smatthias.ringwald /** 68ee091cf1Smatthias.ringwald * get connection for a given handle 69ee091cf1Smatthias.ringwald * 70ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 71ee091cf1Smatthias.ringwald */ 72645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 73ee091cf1Smatthias.ringwald linked_item_t *it; 74ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 75ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 76ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 77ee091cf1Smatthias.ringwald } 78ee091cf1Smatthias.ringwald } 79ee091cf1Smatthias.ringwald return NULL; 80ee091cf1Smatthias.ringwald } 81ee091cf1Smatthias.ringwald 822b12a0b9Smatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 83ee091cf1Smatthias.ringwald hci_connection_t * connection = linked_item_get_user(&timer->item); 84c785ef68Smatthias.ringwald #ifdef HAVE_TIME 85ee091cf1Smatthias.ringwald struct timeval tv; 86ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 87c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 88ee091cf1Smatthias.ringwald // connections might be timed out 89ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 90ee091cf1Smatthias.ringwald } 912b12a0b9Smatthias.ringwald #endif 92e5780900Smatthias.ringwald #ifdef HAVE_TICK 93c785ef68Smatthias.ringwald if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 94c785ef68Smatthias.ringwald // connections might be timed out 95c785ef68Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 96c785ef68Smatthias.ringwald } 97c785ef68Smatthias.ringwald #endif 98c785ef68Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 99c785ef68Smatthias.ringwald run_loop_add_timer(timer); 100c785ef68Smatthias.ringwald } 101ee091cf1Smatthias.ringwald 102ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 103c7492964Smatthias.ringwald #ifdef HAVE_TIME 104ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 105c7492964Smatthias.ringwald #endif 106e5780900Smatthias.ringwald #ifdef HAVE_TICK 107c785ef68Smatthias.ringwald connection->timestamp = embedded_get_ticks(); 108c785ef68Smatthias.ringwald #endif 109ee091cf1Smatthias.ringwald } 110ee091cf1Smatthias.ringwald 111ee091cf1Smatthias.ringwald /** 112c8e4258aSmatthias.ringwald * create connection for given address 113c8e4258aSmatthias.ringwald * 11417f1ba2aSmatthias.ringwald * @return connection OR NULL, if no memory left 115c8e4258aSmatthias.ringwald */ 116c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 117a3b02b71Smatthias.ringwald hci_connection_t * conn = btstack_memory_hci_connection_get(); 118c8e4258aSmatthias.ringwald if (!conn) return NULL; 119c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 120c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1217d3b3569Smatthias.ringwald conn->authentication_flags = AUTH_FLAGS_NONE; 122ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 123ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 124ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 125d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1267856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 12756cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 128c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 129c8e4258aSmatthias.ringwald return conn; 130c8e4258aSmatthias.ringwald } 131c8e4258aSmatthias.ringwald 132c8e4258aSmatthias.ringwald /** 13306b35ec0Smatthias.ringwald * get connection for given address 13497addcc5Smatthias.ringwald * 13597addcc5Smatthias.ringwald * @return connection OR NULL, if not found 13697addcc5Smatthias.ringwald */ 137fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 13806b35ec0Smatthias.ringwald linked_item_t *it; 13906b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 14006b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 14106b35ec0Smatthias.ringwald return (hci_connection_t *) it; 14206b35ec0Smatthias.ringwald } 14306b35ec0Smatthias.ringwald } 14406b35ec0Smatthias.ringwald return NULL; 14506b35ec0Smatthias.ringwald } 14606b35ec0Smatthias.ringwald 14743bfb1bdSmatthias.ringwald /** 14880ca58a0Smatthias.ringwald * add authentication flags and reset timer 1497fde4af9Smatthias.ringwald */ 1507fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1517fde4af9Smatthias.ringwald bd_addr_t addr; 1527fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1537fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1547fde4af9Smatthias.ringwald if (conn) { 1557fde4af9Smatthias.ringwald conn->authentication_flags |= flags; 15680ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1577fde4af9Smatthias.ringwald } 1587fde4af9Smatthias.ringwald } 1597fde4af9Smatthias.ringwald 16080ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 16180ca58a0Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 16280ca58a0Smatthias.ringwald if (!conn) return 0; 16380ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 16480ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 16580ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 16680ca58a0Smatthias.ringwald return 1; 16780ca58a0Smatthias.ringwald } 16880ca58a0Smatthias.ringwald 169c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 170c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 171c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 172c12e46e7Smatthias.ringwald } 173c12e46e7Smatthias.ringwald } 174c12e46e7Smatthias.ringwald 1757fde4af9Smatthias.ringwald 1767fde4af9Smatthias.ringwald /** 17743bfb1bdSmatthias.ringwald * count connections 17843bfb1bdSmatthias.ringwald */ 17940d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 18056c253c9Smatthias.ringwald int count = 0; 18143bfb1bdSmatthias.ringwald linked_item_t *it; 18243bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 18343bfb1bdSmatthias.ringwald return count; 18443bfb1bdSmatthias.ringwald } 185c8e4258aSmatthias.ringwald 18697addcc5Smatthias.ringwald /** 187ba681a6cSmatthias.ringwald * Dummy handler called by HCI 18816833f0aSmatthias.ringwald */ 1892718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 19016833f0aSmatthias.ringwald } 19116833f0aSmatthias.ringwald 192998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 193998906cdSmatthias.ringwald hci_connection_t * connection = connection_for_handle(handle); 194998906cdSmatthias.ringwald if (!connection) { 1957d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 196998906cdSmatthias.ringwald return 0; 197998906cdSmatthias.ringwald } 198998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 199998906cdSmatthias.ringwald } 200998906cdSmatthias.ringwald 201998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 202998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 203998906cdSmatthias.ringwald linked_item_t *it; 204998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 205998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 206998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2077d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 208998906cdSmatthias.ringwald return 0; 209998906cdSmatthias.ringwald } 210998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 211998906cdSmatthias.ringwald } 212998906cdSmatthias.ringwald return free_slots; 213998906cdSmatthias.ringwald } 214998906cdSmatthias.ringwald 215c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2162b12a0b9Smatthias.ringwald 2172b12a0b9Smatthias.ringwald // check for async hci transport implementations 2182b12a0b9Smatthias.ringwald if (hci_stack.hci_transport->can_send_packet_now){ 2192b12a0b9Smatthias.ringwald if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 2202b12a0b9Smatthias.ringwald return 0; 2212b12a0b9Smatthias.ringwald } 2222b12a0b9Smatthias.ringwald } 2232b12a0b9Smatthias.ringwald 2242b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 225c24735b1Smatthias.ringwald switch (packet_type) { 226c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 227c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 228c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 229de009a8cSmatthias.ringwald return hci_stack.num_cmd_packets; 230c24735b1Smatthias.ringwald default: 231c24735b1Smatthias.ringwald return 0; 232c24735b1Smatthias.ringwald } 233c24735b1Smatthias.ringwald } 234c24735b1Smatthias.ringwald 235ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2367856c818Smatthias.ringwald 2376218e6f1Smatthias.ringwald // check for free places on BT module 2385932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2396218e6f1Smatthias.ringwald 2407856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2417856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 24256cf178bSmatthias.ringwald if (!connection) return 0; 24356cf178bSmatthias.ringwald hci_connection_timestamp(connection); 24456cf178bSmatthias.ringwald 24556cf178bSmatthias.ringwald // count packet 24656cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2477b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2487856c818Smatthias.ringwald 24900d8e42eSmatthias.ringwald // send packet 25000d8e42eSmatthias.ringwald int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2516218e6f1Smatthias.ringwald 25200d8e42eSmatthias.ringwald return err; 253ee091cf1Smatthias.ringwald } 254ee091cf1Smatthias.ringwald 25516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2567856c818Smatthias.ringwald 2577856c818Smatthias.ringwald // get info 2587856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2597856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 2607856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2617856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2627856c818Smatthias.ringwald 2637856c818Smatthias.ringwald // ignore non-registered handle 2647856c818Smatthias.ringwald if (!conn){ 2657d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2667856c818Smatthias.ringwald return; 2677856c818Smatthias.ringwald } 2687856c818Smatthias.ringwald 2697856c818Smatthias.ringwald // update idle timestamp 2707856c818Smatthias.ringwald hci_connection_timestamp(conn); 2717856c818Smatthias.ringwald 2727856c818Smatthias.ringwald // handle different packet types 2737856c818Smatthias.ringwald switch (acl_flags & 0x03) { 2747856c818Smatthias.ringwald 2757856c818Smatthias.ringwald case 0x01: // continuation fragment 2767856c818Smatthias.ringwald 2777856c818Smatthias.ringwald // sanity check 2787856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 2797d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 2807856c818Smatthias.ringwald return; 2817856c818Smatthias.ringwald } 2827856c818Smatthias.ringwald 2837856c818Smatthias.ringwald // append fragment payload (header already stored) 2847856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 2857856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 2867856c818Smatthias.ringwald 2877d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 288decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 2897856c818Smatthias.ringwald 2907856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 2917856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 2927856c818Smatthias.ringwald 2932718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 2947856c818Smatthias.ringwald // reset recombination buffer 2957856c818Smatthias.ringwald conn->acl_recombination_length = 0; 2967856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 2977856c818Smatthias.ringwald } 2987856c818Smatthias.ringwald break; 2997856c818Smatthias.ringwald 3007856c818Smatthias.ringwald case 0x02: { // first fragment 3017856c818Smatthias.ringwald 3027856c818Smatthias.ringwald // sanity check 3037856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3047d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3057856c818Smatthias.ringwald return; 3067856c818Smatthias.ringwald } 3077856c818Smatthias.ringwald 3087856c818Smatthias.ringwald // peek into L2CAP packet! 3097856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3107856c818Smatthias.ringwald 3117d67539fSmatthias.ringwald // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 312decc01a8Smatthias.ringwald 3137856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3147856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3157856c818Smatthias.ringwald 3167856c818Smatthias.ringwald // forward fragment as L2CAP packet 3172718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3187856c818Smatthias.ringwald 3197856c818Smatthias.ringwald } else { 3207856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3217856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3227856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3237856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 324decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3257856c818Smatthias.ringwald } 3267856c818Smatthias.ringwald break; 3277856c818Smatthias.ringwald 3287856c818Smatthias.ringwald } 3297856c818Smatthias.ringwald default: 3307d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3317856c818Smatthias.ringwald return; 3327856c818Smatthias.ringwald } 33394ab26f8Smatthias.ringwald 33494ab26f8Smatthias.ringwald // execute main loop 33594ab26f8Smatthias.ringwald hci_run(); 33616833f0aSmatthias.ringwald } 33722909952Smatthias.ringwald 33867a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 339339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3403c4d4b90Smatthias.ringwald 3413c4d4b90Smatthias.ringwald // cancel all l2cap connections 3423c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3433c4d4b90Smatthias.ringwald 344c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 345c785ef68Smatthias.ringwald 346c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 347a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3483c4d4b90Smatthias.ringwald 3493c4d4b90Smatthias.ringwald // now it's gone 350c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 351c7e0c5f6Smatthias.ringwald } 352c7e0c5f6Smatthias.ringwald 3530c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3548f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3558f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3568f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3578f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3588f8108aaSmatthias.ringwald }; 3598f8108aaSmatthias.ringwald 3608f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){ 3618f8108aaSmatthias.ringwald uint16_t packet_types = 0; 3628f8108aaSmatthias.ringwald int i; 3638f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 3648f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 3658f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 3668f8108aaSmatthias.ringwald packet_types |= 1 << i; 3678f8108aaSmatthias.ringwald } 3688f8108aaSmatthias.ringwald } 3698f8108aaSmatthias.ringwald // flip bits for "may not be used" 3708f8108aaSmatthias.ringwald packet_types ^= 0x3306; 3718f8108aaSmatthias.ringwald return packet_types; 3728f8108aaSmatthias.ringwald } 3738f8108aaSmatthias.ringwald 3748f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 3758f8108aaSmatthias.ringwald return hci_stack.packet_types; 3768f8108aaSmatthias.ringwald } 3778f8108aaSmatthias.ringwald 3787dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 3797dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 3807dc17943Smatthias.ringwald return hci_stack.hci_packet_buffer; 3817dc17943Smatthias.ringwald } 3827dc17943Smatthias.ringwald 3837dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){ 3847dc17943Smatthias.ringwald return hci_stack.acl_data_packet_length; 3857dc17943Smatthias.ringwald } 3867dc17943Smatthias.ringwald 38774ec757aSmatthias.ringwald // avoid huge local variables 388223aafc1Smatthias.ringwald #ifndef EMBEDDED 38974ec757aSmatthias.ringwald static device_name_t device_name; 390223aafc1Smatthias.ringwald #endif 39116833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 3921281a47eSmatthias.ringwald bd_addr_t addr; 3932d00edd4Smatthias.ringwald uint8_t link_type; 394fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 3951f7b95a1Smatthias.ringwald hci_connection_t * conn; 39656cf178bSmatthias.ringwald int i; 39722909952Smatthias.ringwald 398*5909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 399*5909f7f2Smatthias.ringwald 4006772a24cSmatthias.ringwald switch (packet[0]) { 40122909952Smatthias.ringwald 4026772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4037ec5eeaaSmatthias.ringwald // get num cmd packets 4047b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4057ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4067ec5eeaaSmatthias.ringwald 407e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 408e2edc0c3Smatthias.ringwald // from offset 5 409e2edc0c3Smatthias.ringwald // status 4101d279b20Smatthias.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" 411e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 41256cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 413e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 41456cf178bSmatthias.ringwald // ignore: total num SCO packets 41556cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 416a7a04bd9Smatthias.ringwald // determine usable ACL payload size 4178fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 4188fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 4198f8108aaSmatthias.ringwald } 420a7a04bd9Smatthias.ringwald // determine usable ACL packet types 42128c93ceeSmatthias.ringwald hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); 4228f8108aaSmatthias.ringwald 4238fcba05dSmatthias.ringwald log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", 4248f8108aaSmatthias.ringwald hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); 425e2edc0c3Smatthias.ringwald } 42656cf178bSmatthias.ringwald } 427381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 428381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 429381fbed8Smatthias.ringwald } 43056cf178bSmatthias.ringwald break; 43156cf178bSmatthias.ringwald 4327ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 4337ec5eeaaSmatthias.ringwald // get num cmd packets 4347b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 4357ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 4367ec5eeaaSmatthias.ringwald break; 4377ec5eeaaSmatthias.ringwald 43856cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 43956cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 44056cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 44156cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 44256cf178bSmatthias.ringwald conn = connection_for_handle(handle); 44356cf178bSmatthias.ringwald if (!conn){ 4447d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 44556cf178bSmatthias.ringwald continue; 44656cf178bSmatthias.ringwald } 44756cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 4487b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 44956cf178bSmatthias.ringwald } 4506772a24cSmatthias.ringwald break; 4516772a24cSmatthias.ringwald 4521f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 45337eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 45437eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 4552d00edd4Smatthias.ringwald link_type = packet[11]; 456339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 45737eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 4581f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 4591f7b95a1Smatthias.ringwald if (!conn) { 4601f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 4611f7b95a1Smatthias.ringwald } 462ce4c8fabSmatthias.ringwald if (!conn) { 463ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 464ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 465ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 466ce4c8fabSmatthias.ringwald break; 467ce4c8fabSmatthias.ringwald } 46832ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 46932ab9390Smatthias.ringwald hci_run(); 47037eaa4cfSmatthias.ringwald } else { 471ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 472ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 473ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 47437eaa4cfSmatthias.ringwald } 4751f7b95a1Smatthias.ringwald break; 4761f7b95a1Smatthias.ringwald 4776772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 478fe1ed1b8Smatthias.ringwald // Connection management 479fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 480339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 4811f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 482fe1ed1b8Smatthias.ringwald if (conn) { 483b448a0e7Smatthias.ringwald if (!packet[2]){ 484c8e4258aSmatthias.ringwald conn->state = OPEN; 485fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 486ee091cf1Smatthias.ringwald 487c785ef68Smatthias.ringwald // restart timer 488c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 489ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 490c785ef68Smatthias.ringwald 491339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 49243bfb1bdSmatthias.ringwald 49343bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 494b448a0e7Smatthias.ringwald } else { 495b448a0e7Smatthias.ringwald // connection failed, remove entry 496b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 497a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 498c12e46e7Smatthias.ringwald 499c12e46e7Smatthias.ringwald // if authentication error, also delete link key 500c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 501c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 502c12e46e7Smatthias.ringwald } 503fe1ed1b8Smatthias.ringwald } 504fe1ed1b8Smatthias.ringwald } 5056772a24cSmatthias.ringwald break; 506fe1ed1b8Smatthias.ringwald 5077fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 5087b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 5097fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 51074ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 51132ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 51264472d52Smatthias.ringwald hci_run(); 51329d53098Smatthias.ringwald // request already answered 51429d53098Smatthias.ringwald return; 5157fde4af9Smatthias.ringwald 5167fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 5177fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 51874ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 51929d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 520287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 52129d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 5227fde4af9Smatthias.ringwald break; 5237fde4af9Smatthias.ringwald 5247fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 5257fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 5267fde4af9Smatthias.ringwald break; 5277fde4af9Smatthias.ringwald 528223aafc1Smatthias.ringwald #ifndef EMBEDDED 52974ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 53074ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 53174ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 53274ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 5335a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 5345a06394aSmatthias.ringwald for (i=0; i<248;i++){ 5355a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 5365a06394aSmatthias.ringwald packet[9+i] = 0; 5375a06394aSmatthias.ringwald break; 538cdc9101dSmatthias.ringwald } 5395a06394aSmatthias.ringwald } 540d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 54174ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 54274ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 54374ec757aSmatthias.ringwald break; 54474ec757aSmatthias.ringwald 54574ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 54674ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 54774ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 54874ec757aSmatthias.ringwald // first send inq result packet 54974ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 55074ec757aSmatthias.ringwald // then send cached remote names 55174ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 55274ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 55374ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 55474ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 55574ec757aSmatthias.ringwald } 55674ec757aSmatthias.ringwald } 55774ec757aSmatthias.ringwald return; 558223aafc1Smatthias.ringwald #endif 55974ec757aSmatthias.ringwald 5606772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 561fe1ed1b8Smatthias.ringwald if (!packet[2]){ 562fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 563fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 564fe1ed1b8Smatthias.ringwald if (conn) { 565c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 566fe1ed1b8Smatthias.ringwald } 567fe1ed1b8Smatthias.ringwald } 5686772a24cSmatthias.ringwald break; 5696772a24cSmatthias.ringwald 570c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 571c68bdf90Smatthias.ringwald if(hci_stack.control->hw_error){ 572c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 573c68bdf90Smatthias.ringwald } 574c68bdf90Smatthias.ringwald break; 575c68bdf90Smatthias.ringwald 576*5909f7f2Smatthias.ringwald #ifdef HAVE_BLE 577*5909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 578*5909f7f2Smatthias.ringwald switch (packet[2]) { 579*5909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 580*5909f7f2Smatthias.ringwald // Connection management 581*5909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 582*5909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 583*5909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 584*5909f7f2Smatthias.ringwald conn = connection_for_address(addr); 585*5909f7f2Smatthias.ringwald if (packet[3]){ 586*5909f7f2Smatthias.ringwald if (conn){ 587*5909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 588*5909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 589*5909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 590*5909f7f2Smatthias.ringwald 591*5909f7f2Smatthias.ringwald } 592*5909f7f2Smatthias.ringwald // if authentication error, also delete link key 593*5909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 594*5909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 595*5909f7f2Smatthias.ringwald } 596*5909f7f2Smatthias.ringwald break; 597*5909f7f2Smatthias.ringwald } 598*5909f7f2Smatthias.ringwald if (!conn){ 599*5909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 600*5909f7f2Smatthias.ringwald } 601*5909f7f2Smatthias.ringwald if (!conn){ 602*5909f7f2Smatthias.ringwald // no memory 603*5909f7f2Smatthias.ringwald break; 604*5909f7f2Smatthias.ringwald } 605*5909f7f2Smatthias.ringwald 606*5909f7f2Smatthias.ringwald conn->state = OPEN; 607*5909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 608*5909f7f2Smatthias.ringwald 609*5909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 610*5909f7f2Smatthias.ringwald 611*5909f7f2Smatthias.ringwald // restart timer 612*5909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 613*5909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 614*5909f7f2Smatthias.ringwald 615*5909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 616*5909f7f2Smatthias.ringwald 617*5909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 618*5909f7f2Smatthias.ringwald break; 619*5909f7f2Smatthias.ringwald 620*5909f7f2Smatthias.ringwald default: 621*5909f7f2Smatthias.ringwald break; 622*5909f7f2Smatthias.ringwald } 623*5909f7f2Smatthias.ringwald break; 624*5909f7f2Smatthias.ringwald #endif 625*5909f7f2Smatthias.ringwald 6266772a24cSmatthias.ringwald default: 6276772a24cSmatthias.ringwald break; 628fe1ed1b8Smatthias.ringwald } 629fe1ed1b8Smatthias.ringwald 6303429f56bSmatthias.ringwald // handle BT initialization 6313429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 6327301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 6337301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 6347301ad89Smatthias.ringwald // hci_stack.substate = 0; 6357301ad89Smatthias.ringwald // } 6367301ad89Smatthias.ringwald // handle normal init sequence 6373429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 6383429f56bSmatthias.ringwald // odd: waiting for event 6393429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 6403429f56bSmatthias.ringwald hci_stack.substate++; 6413429f56bSmatthias.ringwald } 6423429f56bSmatthias.ringwald } 64322909952Smatthias.ringwald } 64422909952Smatthias.ringwald 64589db417bSmatthias.ringwald // help with BT sleep 64689db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 64789db417bSmatthias.ringwald && hci_stack.substate == 1 64889db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 64989db417bSmatthias.ringwald hci_stack.substate++; 65089db417bSmatthias.ringwald } 65189db417bSmatthias.ringwald 6522718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 65394ab26f8Smatthias.ringwald 65494ab26f8Smatthias.ringwald // execute main loop 65594ab26f8Smatthias.ringwald hci_run(); 65616833f0aSmatthias.ringwald } 65716833f0aSmatthias.ringwald 65810e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 65910e830c9Smatthias.ringwald switch (packet_type) { 66010e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 66110e830c9Smatthias.ringwald event_handler(packet, size); 66210e830c9Smatthias.ringwald break; 66310e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 66410e830c9Smatthias.ringwald acl_handler(packet, size); 66510e830c9Smatthias.ringwald break; 66610e830c9Smatthias.ringwald default: 66710e830c9Smatthias.ringwald break; 66810e830c9Smatthias.ringwald } 66910e830c9Smatthias.ringwald } 67010e830c9Smatthias.ringwald 671fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 6722718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 6732718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 67416833f0aSmatthias.ringwald } 67516833f0aSmatthias.ringwald 676404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){ 677475c8125Smatthias.ringwald 678475c8125Smatthias.ringwald // reference to use transport layer implementation 67916833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 680475c8125Smatthias.ringwald 68111e23e5fSmatthias.ringwald // references to used control implementation 68211e23e5fSmatthias.ringwald hci_stack.control = control; 68311e23e5fSmatthias.ringwald 68411e23e5fSmatthias.ringwald // reference to used config 68511e23e5fSmatthias.ringwald hci_stack.config = config; 68611e23e5fSmatthias.ringwald 687fe1ed1b8Smatthias.ringwald // no connections yet 688fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 6890a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 690fe1ed1b8Smatthias.ringwald 691b031bebbSmatthias.ringwald // no pending cmds 692b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 693b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 694b031bebbSmatthias.ringwald 69516833f0aSmatthias.ringwald // higher level handler 6962718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 69716833f0aSmatthias.ringwald 698404843c1Smatthias.ringwald // store and open remote device db 699404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 700404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 701404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 702404843c1Smatthias.ringwald } 70329d53098Smatthias.ringwald 7048fcba05dSmatthias.ringwald // max acl payload size defined in config.h 7058fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 7068fcba05dSmatthias.ringwald 70716833f0aSmatthias.ringwald // register packet handlers with transport 70810e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 709475c8125Smatthias.ringwald } 710475c8125Smatthias.ringwald 711404843c1Smatthias.ringwald void hci_close(){ 712404843c1Smatthias.ringwald // close remote device db 713404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 714404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 715404843c1Smatthias.ringwald } 716404843c1Smatthias.ringwald } 717404843c1Smatthias.ringwald 7188d213e1aSmatthias.ringwald // State-Module-Driver overview 7198d213e1aSmatthias.ringwald // state module low-level 7208d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 7218d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 7228d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 7238d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 724d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 725d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 726c7e0c5f6Smatthias.ringwald 72740d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 7287301ad89Smatthias.ringwald 729038bc64cSmatthias.ringwald // power on 730f9a30166Smatthias.ringwald int err = 0; 731f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 732f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 733f9a30166Smatthias.ringwald } 734038bc64cSmatthias.ringwald if (err){ 7357d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 736038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 737038bc64cSmatthias.ringwald return err; 738038bc64cSmatthias.ringwald } 739038bc64cSmatthias.ringwald 740038bc64cSmatthias.ringwald // open low-level device 741038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 742038bc64cSmatthias.ringwald if (err){ 7437d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 744f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 745f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 746f9a30166Smatthias.ringwald } 747038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 748038bc64cSmatthias.ringwald return err; 749038bc64cSmatthias.ringwald } 7508d213e1aSmatthias.ringwald return 0; 7518d213e1aSmatthias.ringwald } 752038bc64cSmatthias.ringwald 75340d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 7548d213e1aSmatthias.ringwald 7557b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 7569418f9c9Smatthias.ringwald 7578d213e1aSmatthias.ringwald // close low-level device 7588d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 7598d213e1aSmatthias.ringwald 7607b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 7619418f9c9Smatthias.ringwald 7628d213e1aSmatthias.ringwald // power off 7638d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 7648d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 7658d213e1aSmatthias.ringwald } 7669418f9c9Smatthias.ringwald 7677b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 7689418f9c9Smatthias.ringwald 76972ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 77072ea5239Smatthias.ringwald } 77172ea5239Smatthias.ringwald 77240d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 77372ea5239Smatthias.ringwald 7747b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 7753144bce4Smatthias.ringwald 776b429b9b7Smatthias.ringwald #if 0 777b429b9b7Smatthias.ringwald // don't close serial port during sleep 778b429b9b7Smatthias.ringwald 77972ea5239Smatthias.ringwald // close low-level device 78072ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 781b429b9b7Smatthias.ringwald #endif 78272ea5239Smatthias.ringwald 78372ea5239Smatthias.ringwald // sleep mode 7843144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 78572ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 78672ea5239Smatthias.ringwald } 787b429b9b7Smatthias.ringwald 78872ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 7898d213e1aSmatthias.ringwald } 7908d213e1aSmatthias.ringwald 79140d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 792b429b9b7Smatthias.ringwald 7937b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 794b429b9b7Smatthias.ringwald 795b429b9b7Smatthias.ringwald // wake on 796b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 797b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 798b429b9b7Smatthias.ringwald } 799b429b9b7Smatthias.ringwald 800b429b9b7Smatthias.ringwald #if 0 801b429b9b7Smatthias.ringwald // open low-level device 802b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 803b429b9b7Smatthias.ringwald if (err){ 8047d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 805b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 806b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 807b429b9b7Smatthias.ringwald } 808b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 809b429b9b7Smatthias.ringwald return err; 810b429b9b7Smatthias.ringwald } 811b429b9b7Smatthias.ringwald #endif 812b429b9b7Smatthias.ringwald 813b429b9b7Smatthias.ringwald return 0; 814b429b9b7Smatthias.ringwald } 815b429b9b7Smatthias.ringwald 816b429b9b7Smatthias.ringwald 8178d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 818d661ed19Smatthias.ringwald 8197b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 820d661ed19Smatthias.ringwald 8218d213e1aSmatthias.ringwald int err = 0; 8228d213e1aSmatthias.ringwald switch (hci_stack.state){ 8238d213e1aSmatthias.ringwald 8248d213e1aSmatthias.ringwald case HCI_STATE_OFF: 8258d213e1aSmatthias.ringwald switch (power_mode){ 8268d213e1aSmatthias.ringwald case HCI_POWER_ON: 8278d213e1aSmatthias.ringwald err = hci_power_control_on(); 8288d213e1aSmatthias.ringwald if (err) return err; 8297301ad89Smatthias.ringwald // set up state machine 8307301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 8317301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8327301ad89Smatthias.ringwald hci_stack.substate = 0; 8338d213e1aSmatthias.ringwald break; 8348d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8358d213e1aSmatthias.ringwald // do nothing 8368d213e1aSmatthias.ringwald break; 8378d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 838b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 8398d213e1aSmatthias.ringwald break; 8408d213e1aSmatthias.ringwald } 8418d213e1aSmatthias.ringwald break; 8427301ad89Smatthias.ringwald 8438d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 8448d213e1aSmatthias.ringwald switch (power_mode){ 8458d213e1aSmatthias.ringwald case HCI_POWER_ON: 8468d213e1aSmatthias.ringwald // do nothing 8478d213e1aSmatthias.ringwald break; 8488d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8498d213e1aSmatthias.ringwald // no connections yet, just turn it off 8508d213e1aSmatthias.ringwald hci_power_control_off(); 8518d213e1aSmatthias.ringwald break; 8528d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 853b546ac54Smatthias.ringwald // no connections yet, just turn it off 85472ea5239Smatthias.ringwald hci_power_control_sleep(); 8558d213e1aSmatthias.ringwald break; 8568d213e1aSmatthias.ringwald } 8578d213e1aSmatthias.ringwald break; 8587301ad89Smatthias.ringwald 8598d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 8608d213e1aSmatthias.ringwald switch (power_mode){ 8618d213e1aSmatthias.ringwald case HCI_POWER_ON: 8628d213e1aSmatthias.ringwald // do nothing 8638d213e1aSmatthias.ringwald break; 8648d213e1aSmatthias.ringwald case HCI_POWER_OFF: 865c7e0c5f6Smatthias.ringwald // see hci_run 866c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 8678d213e1aSmatthias.ringwald break; 8688d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 869b546ac54Smatthias.ringwald // see hci_run 870b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 87189db417bSmatthias.ringwald hci_stack.substate = 0; 8728d213e1aSmatthias.ringwald break; 8738d213e1aSmatthias.ringwald } 8748d213e1aSmatthias.ringwald break; 8757301ad89Smatthias.ringwald 8768d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 8778d213e1aSmatthias.ringwald switch (power_mode){ 8788d213e1aSmatthias.ringwald case HCI_POWER_ON: 8798d213e1aSmatthias.ringwald // set up state machine 8808d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8818d213e1aSmatthias.ringwald hci_stack.substate = 0; 8828d213e1aSmatthias.ringwald break; 8838d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8848d213e1aSmatthias.ringwald // do nothing 8858d213e1aSmatthias.ringwald break; 8868d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 887b546ac54Smatthias.ringwald // see hci_run 888b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 88989db417bSmatthias.ringwald hci_stack.substate = 0; 8908d213e1aSmatthias.ringwald break; 8918d213e1aSmatthias.ringwald } 8928d213e1aSmatthias.ringwald break; 8938d213e1aSmatthias.ringwald 8948d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 8958d213e1aSmatthias.ringwald switch (power_mode){ 8968d213e1aSmatthias.ringwald case HCI_POWER_ON: 89728171530Smatthias.ringwald 89828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 89928171530Smatthias.ringwald // nothing to do, if H4 supports power management 90028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9018747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9028747d67fSmatthias.ringwald hci_stack.substate = 6; 90328171530Smatthias.ringwald break; 90428171530Smatthias.ringwald } 90528171530Smatthias.ringwald #endif 906b546ac54Smatthias.ringwald // set up state machine 907b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 908b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 909b546ac54Smatthias.ringwald hci_stack.substate = 0; 9108d213e1aSmatthias.ringwald break; 9118d213e1aSmatthias.ringwald case HCI_POWER_OFF: 912b546ac54Smatthias.ringwald // see hci_run 913b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9148d213e1aSmatthias.ringwald break; 9158d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 916b546ac54Smatthias.ringwald // do nothing 9178d213e1aSmatthias.ringwald break; 9188d213e1aSmatthias.ringwald } 9198d213e1aSmatthias.ringwald break; 9208d213e1aSmatthias.ringwald 9218d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 9228d213e1aSmatthias.ringwald switch (power_mode){ 9238d213e1aSmatthias.ringwald case HCI_POWER_ON: 92428171530Smatthias.ringwald 92528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 92628171530Smatthias.ringwald // nothing to do, if H4 supports power management 92728171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9288747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9298747d67fSmatthias.ringwald hci_stack.substate = 6; 93028171530Smatthias.ringwald break; 93128171530Smatthias.ringwald } 93228171530Smatthias.ringwald #endif 9333144bce4Smatthias.ringwald err = hci_power_control_wake(); 9343144bce4Smatthias.ringwald if (err) return err; 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: 9414ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9428d213e1aSmatthias.ringwald break; 9438d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 944b546ac54Smatthias.ringwald // do nothing 9458d213e1aSmatthias.ringwald break; 9468d213e1aSmatthias.ringwald } 9478d213e1aSmatthias.ringwald break; 94811e23e5fSmatthias.ringwald } 94968d92d03Smatthias.ringwald 950038bc64cSmatthias.ringwald // create internal event 951ee8bf225Smatthias.ringwald hci_emit_state(); 952ee8bf225Smatthias.ringwald 95368d92d03Smatthias.ringwald // trigger next/first action 95468d92d03Smatthias.ringwald hci_run(); 95568d92d03Smatthias.ringwald 956475c8125Smatthias.ringwald return 0; 957475c8125Smatthias.ringwald } 958475c8125Smatthias.ringwald 959381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 960381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 961381fbed8Smatthias.ringwald 962381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 963381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 964381fbed8Smatthias.ringwald return; 965381fbed8Smatthias.ringwald } 966381fbed8Smatthias.ringwald 967b031bebbSmatthias.ringwald // store request to send command but accept in higher layer view 968b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 2 | enable; // 1 = inq scan, 2 = page scan 969381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 970b031bebbSmatthias.ringwald 971b031bebbSmatthias.ringwald hci_run(); 972381fbed8Smatthias.ringwald } 973381fbed8Smatthias.ringwald 97406b35ec0Smatthias.ringwald void hci_run(){ 9758a485f27Smatthias.ringwald 97632ab9390Smatthias.ringwald hci_connection_t * connection; 97732ab9390Smatthias.ringwald linked_item_t * it; 97832ab9390Smatthias.ringwald 979ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 980ce4c8fabSmatthias.ringwald 981b031bebbSmatthias.ringwald // global/non-connection oriented commands 982b031bebbSmatthias.ringwald 983b031bebbSmatthias.ringwald // decline incoming connections 984ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 985ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 986ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 987ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 988ce4c8fabSmatthias.ringwald } 989ce4c8fabSmatthias.ringwald 990b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 991b031bebbSmatthias.ringwald 992b031bebbSmatthias.ringwald // send scan enable 993eda18bf8Smatthias.ringwald if (hci_stack.new_scan_enable_value != 0xff){ 994b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 995b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 996b031bebbSmatthias.ringwald } 997b031bebbSmatthias.ringwald 99832ab9390Smatthias.ringwald // send pending HCI commands 99932ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 100032ab9390Smatthias.ringwald 1001b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 100232ab9390Smatthias.ringwald 1003b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 100432ab9390Smatthias.ringwald 100532ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 10067b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 100732ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 100832ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1009c7e0c5f6Smatthias.ringwald } 1010c7e0c5f6Smatthias.ringwald 1011de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 101232ab9390Smatthias.ringwald 101332ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 101432ab9390Smatthias.ringwald link_key_t link_key; 10157b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 101632ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 101732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 101832ab9390Smatthias.ringwald } else { 101932ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 102032ab9390Smatthias.ringwald } 102132ab9390Smatthias.ringwald connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST; 102232ab9390Smatthias.ringwald } 102332ab9390Smatthias.ringwald } 102432ab9390Smatthias.ringwald 1025de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1026c7e0c5f6Smatthias.ringwald 10273429f56bSmatthias.ringwald switch (hci_stack.state){ 10283429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 10297b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 10303429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 10313429f56bSmatthias.ringwald // odd: waiting for command completion 103206b35ec0Smatthias.ringwald return; 10333429f56bSmatthias.ringwald } 103490919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1035c7492964Smatthias.ringwald case 0: // RESET 103622909952Smatthias.ringwald hci_send_cmd(&hci_reset); 1037c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1038c7492964Smatthias.ringwald // skip baud change 1039c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1040c7492964Smatthias.ringwald } 10413429f56bSmatthias.ringwald break; 1042c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 10437dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 10447dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1045c7492964Smatthias.ringwald break; 1046c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1047c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1048c7492964Smatthias.ringwald hci_stack.substate += 2; 1049c7492964Smatthias.ringwald // break missing here for fall through 1050c7492964Smatthias.ringwald 1051c7492964Smatthias.ringwald case 3: 105290919203Smatthias.ringwald // custom initialization 1053db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 10547dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1055d62aca9fSmatthias.ringwald if (valid_cmd){ 10567dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 10577dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1058c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 105990919203Smatthias.ringwald break; 106090919203Smatthias.ringwald } 1061d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 106290919203Smatthias.ringwald } 10632f6c30e1Smatthias.ringwald // otherwise continue 1064f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1065f432a6ddSmatthias.ringwald break; 1066c7492964Smatthias.ringwald case 4: 1067e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1068e2edc0c3Smatthias.ringwald break; 1069c7492964Smatthias.ringwald case 5: 10703429f56bSmatthias.ringwald // ca. 15 sec 10713429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 10723429f56bSmatthias.ringwald break; 1073c7492964Smatthias.ringwald case 6: 10740a90cc40Smatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan 1075f432a6ddSmatthias.ringwald break; 1076c7492964Smatthias.ringwald case 7: 10778a485f27Smatthias.ringwald #ifndef EMBEDDED 10788a485f27Smatthias.ringwald { 10798a485f27Smatthias.ringwald char hostname[30]; 10808a485f27Smatthias.ringwald gethostname(hostname, 30); 10818a485f27Smatthias.ringwald hostname[29] = '\0'; 10828a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 10838a485f27Smatthias.ringwald break; 10848a485f27Smatthias.ringwald } 1085c7492964Smatthias.ringwald case 8: 10863c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 10873c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 10883c4d4b90Smatthias.ringwald break; 10893c4d4b90Smatthias.ringwald 1090c7492964Smatthias.ringwald case 9: 10913c4d4b90Smatthias.ringwald #endif 10928a485f27Smatthias.ringwald #endif 10933429f56bSmatthias.ringwald // done. 10943429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1095b360b6adSmatthias.ringwald hci_emit_state(); 10963429f56bSmatthias.ringwald break; 10973429f56bSmatthias.ringwald default: 10983429f56bSmatthias.ringwald break; 1099475c8125Smatthias.ringwald } 11003429f56bSmatthias.ringwald hci_stack.substate++; 11013429f56bSmatthias.ringwald break; 1102c7e0c5f6Smatthias.ringwald 1103c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1104c7e0c5f6Smatthias.ringwald 11057b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1106c7e0c5f6Smatthias.ringwald // close all open connections 1107c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1108c7e0c5f6Smatthias.ringwald if (connection){ 110932ab9390Smatthias.ringwald 1110c7e0c5f6Smatthias.ringwald // send disconnect 111132ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 111232ab9390Smatthias.ringwald 11137b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle); 11146ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1115c7e0c5f6Smatthias.ringwald 1116c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1117c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1118c7e0c5f6Smatthias.ringwald return; 1119c7e0c5f6Smatthias.ringwald } 11207b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1121c7e0c5f6Smatthias.ringwald 112272ea5239Smatthias.ringwald // switch mode 1123c7e0c5f6Smatthias.ringwald hci_power_control_off(); 11249418f9c9Smatthias.ringwald 11257b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 112672ea5239Smatthias.ringwald hci_emit_state(); 11277b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 112872ea5239Smatthias.ringwald break; 1129c7e0c5f6Smatthias.ringwald 113072ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 113189db417bSmatthias.ringwald switch(hci_stack.substate) { 113289db417bSmatthias.ringwald case 0: 11337b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 113472ea5239Smatthias.ringwald // close all open connections 113572ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 113666da7044Smatthias.ringwald 113728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 113866da7044Smatthias.ringwald // don't close connections, if H4 supports power management 113966da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 114066da7044Smatthias.ringwald connection = NULL; 114166da7044Smatthias.ringwald } 114266da7044Smatthias.ringwald #endif 114372ea5239Smatthias.ringwald if (connection){ 114432ab9390Smatthias.ringwald 114572ea5239Smatthias.ringwald // send disconnect 114632ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 114732ab9390Smatthias.ringwald 11487b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle); 11496ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 115072ea5239Smatthias.ringwald 115172ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 115272ea5239Smatthias.ringwald hci_shutdown_connection(connection); 115372ea5239Smatthias.ringwald return; 115472ea5239Smatthias.ringwald } 115572ea5239Smatthias.ringwald 115689db417bSmatthias.ringwald // disable page and inquiry scan 115732ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 115832ab9390Smatthias.ringwald 11597b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, disabling inq & page scans\n"); 116089db417bSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 0); // none 116189db417bSmatthias.ringwald 116289db417bSmatthias.ringwald // continue in next sub state 116389db417bSmatthias.ringwald hci_stack.substate++; 116489db417bSmatthias.ringwald break; 116589db417bSmatthias.ringwald case 1: 116689db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 116789db417bSmatthias.ringwald break; 116889db417bSmatthias.ringwald case 2: 11697b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 117028171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 117128171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 117228171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 117328171530Smatthias.ringwald // SLEEP MODE reached 117428171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 117528171530Smatthias.ringwald hci_emit_state(); 117628171530Smatthias.ringwald break; 117728171530Smatthias.ringwald } 117828171530Smatthias.ringwald #endif 117972ea5239Smatthias.ringwald // switch mode 118089db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1181c7e0c5f6Smatthias.ringwald hci_emit_state(); 118228171530Smatthias.ringwald break; 118328171530Smatthias.ringwald 118489db417bSmatthias.ringwald default: 118589db417bSmatthias.ringwald break; 118689db417bSmatthias.ringwald } 1187c7e0c5f6Smatthias.ringwald break; 1188c7e0c5f6Smatthias.ringwald 11893429f56bSmatthias.ringwald default: 11903429f56bSmatthias.ringwald break; 11911f504dbdSmatthias.ringwald } 11923429f56bSmatthias.ringwald } 119316833f0aSmatthias.ringwald 119431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1195c8e4258aSmatthias.ringwald bd_addr_t addr; 1196c8e4258aSmatthias.ringwald hci_connection_t * conn; 1197c8e4258aSmatthias.ringwald // house-keeping 1198c8e4258aSmatthias.ringwald 1199c8e4258aSmatthias.ringwald // create_connection? 1200c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1201c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1202339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1203c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1204c8e4258aSmatthias.ringwald if (conn) { 1205c8e4258aSmatthias.ringwald // if connection exists 1206c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 120717f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 120817f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1209c8e4258aSmatthias.ringwald } 121017f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1211c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1212c8e4258aSmatthias.ringwald 121317f1ba2aSmatthias.ringwald } 1214c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 121517f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 121617f1ba2aSmatthias.ringwald if (!conn){ 121717f1ba2aSmatthias.ringwald // notify client that alloc failed 121817f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 121917f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 122017f1ba2aSmatthias.ringwald } 1221c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1222c8e4258aSmatthias.ringwald } 1223c8e4258aSmatthias.ringwald 12247fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 12257fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 12267fde4af9Smatthias.ringwald } 12277fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 12287fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 12297fde4af9Smatthias.ringwald } 12307fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 12317fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 12327fde4af9Smatthias.ringwald } 12337fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 12347fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 12357fde4af9Smatthias.ringwald } 12367fde4af9Smatthias.ringwald 12378ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 12388ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 12398ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 12408ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 12418ef73945Smatthias.ringwald } 12428ef73945Smatthias.ringwald } 1243c8e4258aSmatthias.ringwald 124431452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1245622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 124631452debSmatthias.ringwald } 12478adf0ddaSmatthias.ringwald 12481cd208adSmatthias.ringwald /** 12491cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 12501cd208adSmatthias.ringwald */ 1251fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 12521cd208adSmatthias.ringwald va_list argptr; 12531cd208adSmatthias.ringwald va_start(argptr, cmd); 12547dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 12551cd208adSmatthias.ringwald va_end(argptr); 12567dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 125793b8dc03Smatthias.ringwald } 1258c8e4258aSmatthias.ringwald 1259ee091cf1Smatthias.ringwald // Create various non-HCI events. 1260ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1261ee091cf1Smatthias.ringwald 1262c8e4258aSmatthias.ringwald void hci_emit_state(){ 1263425d1371Smatthias.ringwald uint8_t event[3]; 126480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1265425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1266c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1267425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1268425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1269c8e4258aSmatthias.ringwald } 1270c8e4258aSmatthias.ringwald 127117f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1272425d1371Smatthias.ringwald uint8_t event[13]; 1273c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1274425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 127517f1ba2aSmatthias.ringwald event[2] = status; 1276c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1277c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1278c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1279c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1280425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1281425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1282c8e4258aSmatthias.ringwald } 1283c8e4258aSmatthias.ringwald 12843c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1285425d1371Smatthias.ringwald uint8_t event[6]; 12863c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1287e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 12883c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 12893c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 12903c4d4b90Smatthias.ringwald event[5] = reason; 1291425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1292425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 12933c4d4b90Smatthias.ringwald } 12943c4d4b90Smatthias.ringwald 1295ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1296425d1371Smatthias.ringwald uint8_t event[4]; 129780d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1298425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1299ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1300425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1301425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1302ee091cf1Smatthias.ringwald } 130343bfb1bdSmatthias.ringwald 130443bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1305425d1371Smatthias.ringwald uint8_t event[3]; 130680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1307425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 130843bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1309425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1310425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 131143bfb1bdSmatthias.ringwald } 1312038bc64cSmatthias.ringwald 1313038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1314425d1371Smatthias.ringwald uint8_t event[2]; 131580d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1316425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1317425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1318425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1319038bc64cSmatthias.ringwald } 13201b0e3922Smatthias.ringwald 132109ba8edeSmatthias.ringwald #ifndef EMBEDDED 13221b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1323425d1371Smatthias.ringwald uint8_t event[6]; 13241b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1325425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1326425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1327425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1328425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1329425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1330425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13311b0e3922Smatthias.ringwald } 133209ba8edeSmatthias.ringwald #endif 13331b0e3922Smatthias.ringwald 13342ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1335425d1371Smatthias.ringwald uint8_t event[3]; 13362ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1337425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 13382ed6235cSmatthias.ringwald event[2] = enabled; 1339425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1340e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13412ed6235cSmatthias.ringwald } 1342627c2f45Smatthias.ringwald 1343627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1344425d1371Smatthias.ringwald uint8_t event[2+1+6+248]; 1345627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1346425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1347f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 13482f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1349f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1350425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1351425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1352627c2f45Smatthias.ringwald } 1353381fbed8Smatthias.ringwald 1354381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1355425d1371Smatthias.ringwald uint8_t event[3]; 1356381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1357425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1358381fbed8Smatthias.ringwald event[2] = enabled; 1359425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1360425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1361381fbed8Smatthias.ringwald } 1362