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){ 85ee091cf1Smatthias.ringwald hci_connection_t * connection = 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){ 119a3b02b71Smatthias.ringwald hci_connection_t * conn = 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 14943bfb1bdSmatthias.ringwald /** 15080ca58a0Smatthias.ringwald * add authentication flags and reset timer 1517fde4af9Smatthias.ringwald */ 1527fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1537fde4af9Smatthias.ringwald bd_addr_t addr; 1547fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1557fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1567fde4af9Smatthias.ringwald if (conn) { 1577fde4af9Smatthias.ringwald conn->authentication_flags |= flags; 15880ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1597fde4af9Smatthias.ringwald } 1607fde4af9Smatthias.ringwald } 1617fde4af9Smatthias.ringwald 16280ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 16380ca58a0Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 16480ca58a0Smatthias.ringwald if (!conn) return 0; 16580ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 16680ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 16780ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 16880ca58a0Smatthias.ringwald return 1; 16980ca58a0Smatthias.ringwald } 17080ca58a0Smatthias.ringwald 171c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 172c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 173c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 174c12e46e7Smatthias.ringwald } 175c12e46e7Smatthias.ringwald } 176c12e46e7Smatthias.ringwald 1777fde4af9Smatthias.ringwald 1787fde4af9Smatthias.ringwald /** 17943bfb1bdSmatthias.ringwald * count connections 18043bfb1bdSmatthias.ringwald */ 18140d1c7a4Smatthias.ringwald static int nr_hci_connections(void){ 18256c253c9Smatthias.ringwald int count = 0; 18343bfb1bdSmatthias.ringwald linked_item_t *it; 18443bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 18543bfb1bdSmatthias.ringwald return count; 18643bfb1bdSmatthias.ringwald } 187c8e4258aSmatthias.ringwald 18897addcc5Smatthias.ringwald /** 189ba681a6cSmatthias.ringwald * Dummy handler called by HCI 19016833f0aSmatthias.ringwald */ 1912718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 19216833f0aSmatthias.ringwald } 19316833f0aSmatthias.ringwald 194998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 195998906cdSmatthias.ringwald hci_connection_t * connection = connection_for_handle(handle); 196998906cdSmatthias.ringwald if (!connection) { 1977d67539fSmatthias.ringwald log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 198998906cdSmatthias.ringwald return 0; 199998906cdSmatthias.ringwald } 200998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 201998906cdSmatthias.ringwald } 202998906cdSmatthias.ringwald 203998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 204998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 205998906cdSmatthias.ringwald linked_item_t *it; 206998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 207998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 208998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 2097d67539fSmatthias.ringwald log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 210998906cdSmatthias.ringwald return 0; 211998906cdSmatthias.ringwald } 212998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 213998906cdSmatthias.ringwald } 214998906cdSmatthias.ringwald return free_slots; 215998906cdSmatthias.ringwald } 216998906cdSmatthias.ringwald 217c24735b1Smatthias.ringwald int hci_can_send_packet_now(uint8_t packet_type){ 2182b12a0b9Smatthias.ringwald 2192b12a0b9Smatthias.ringwald // check for async hci transport implementations 2202b12a0b9Smatthias.ringwald if (hci_stack.hci_transport->can_send_packet_now){ 2212b12a0b9Smatthias.ringwald if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 2222b12a0b9Smatthias.ringwald return 0; 2232b12a0b9Smatthias.ringwald } 2242b12a0b9Smatthias.ringwald } 2252b12a0b9Smatthias.ringwald 2262b12a0b9Smatthias.ringwald // check regular Bluetooth flow control 227c24735b1Smatthias.ringwald switch (packet_type) { 228c24735b1Smatthias.ringwald case HCI_ACL_DATA_PACKET: 229c24735b1Smatthias.ringwald return hci_number_free_acl_slots(); 230c24735b1Smatthias.ringwald case HCI_COMMAND_DATA_PACKET: 231de009a8cSmatthias.ringwald return hci_stack.num_cmd_packets; 232c24735b1Smatthias.ringwald default: 233c24735b1Smatthias.ringwald return 0; 234c24735b1Smatthias.ringwald } 235c24735b1Smatthias.ringwald } 236c24735b1Smatthias.ringwald 237ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2387856c818Smatthias.ringwald 2396218e6f1Smatthias.ringwald // check for free places on BT module 2405932f1b4Smatthias.ringwald if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 2416218e6f1Smatthias.ringwald 2427856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2437856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 24456cf178bSmatthias.ringwald if (!connection) return 0; 24556cf178bSmatthias.ringwald hci_connection_timestamp(connection); 24656cf178bSmatthias.ringwald 24756cf178bSmatthias.ringwald // count packet 24856cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2497b5fbe1fSmatthias.ringwald // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2507856c818Smatthias.ringwald 25100d8e42eSmatthias.ringwald // send packet 25200d8e42eSmatthias.ringwald int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2536218e6f1Smatthias.ringwald 25400d8e42eSmatthias.ringwald return err; 255ee091cf1Smatthias.ringwald } 256ee091cf1Smatthias.ringwald 25716833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2587856c818Smatthias.ringwald 2597856c818Smatthias.ringwald // get info 2607856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2617856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 2627856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2637856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2647856c818Smatthias.ringwald 2657856c818Smatthias.ringwald // ignore non-registered handle 2667856c818Smatthias.ringwald if (!conn){ 2677d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2687856c818Smatthias.ringwald return; 2697856c818Smatthias.ringwald } 2707856c818Smatthias.ringwald 2717856c818Smatthias.ringwald // update idle timestamp 2727856c818Smatthias.ringwald hci_connection_timestamp(conn); 2737856c818Smatthias.ringwald 2747856c818Smatthias.ringwald // handle different packet types 2757856c818Smatthias.ringwald switch (acl_flags & 0x03) { 2767856c818Smatthias.ringwald 2777856c818Smatthias.ringwald case 0x01: // continuation fragment 2787856c818Smatthias.ringwald 2797856c818Smatthias.ringwald // sanity check 2807856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 2817d67539fSmatthias.ringwald log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 2827856c818Smatthias.ringwald return; 2837856c818Smatthias.ringwald } 2847856c818Smatthias.ringwald 2857856c818Smatthias.ringwald // append fragment payload (header already stored) 2867856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 2877856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 2887856c818Smatthias.ringwald 2897d67539fSmatthias.ringwald // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 290decc01a8Smatthias.ringwald // conn->acl_recombination_pos, conn->acl_recombination_length); 2917856c818Smatthias.ringwald 2927856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 2937856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 2947856c818Smatthias.ringwald 2952718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 2967856c818Smatthias.ringwald // reset recombination buffer 2977856c818Smatthias.ringwald conn->acl_recombination_length = 0; 2987856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 2997856c818Smatthias.ringwald } 3007856c818Smatthias.ringwald break; 3017856c818Smatthias.ringwald 3027856c818Smatthias.ringwald case 0x02: { // first fragment 3037856c818Smatthias.ringwald 3047856c818Smatthias.ringwald // sanity check 3057856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 3067d67539fSmatthias.ringwald log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 3077856c818Smatthias.ringwald return; 3087856c818Smatthias.ringwald } 3097856c818Smatthias.ringwald 3107856c818Smatthias.ringwald // peek into L2CAP packet! 3117856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 3127856c818Smatthias.ringwald 3137d67539fSmatthias.ringwald // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 314decc01a8Smatthias.ringwald 3157856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 3167856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 3177856c818Smatthias.ringwald 3187856c818Smatthias.ringwald // forward fragment as L2CAP packet 3192718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 3207856c818Smatthias.ringwald 3217856c818Smatthias.ringwald } else { 3227856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 3237856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 3247856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 3257856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 326decc01a8Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 3277856c818Smatthias.ringwald } 3287856c818Smatthias.ringwald break; 3297856c818Smatthias.ringwald 3307856c818Smatthias.ringwald } 3317856c818Smatthias.ringwald default: 3327d67539fSmatthias.ringwald log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3337856c818Smatthias.ringwald return; 3347856c818Smatthias.ringwald } 33594ab26f8Smatthias.ringwald 33694ab26f8Smatthias.ringwald // execute main loop 33794ab26f8Smatthias.ringwald hci_run(); 33816833f0aSmatthias.ringwald } 33922909952Smatthias.ringwald 34067a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 341339b6768Smatthias.ringwald log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 3423c4d4b90Smatthias.ringwald 3433c4d4b90Smatthias.ringwald // cancel all l2cap connections 3443c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3453c4d4b90Smatthias.ringwald 346c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 347c785ef68Smatthias.ringwald 348c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 349a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 3503c4d4b90Smatthias.ringwald 3513c4d4b90Smatthias.ringwald // now it's gone 352c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 353c7e0c5f6Smatthias.ringwald } 354c7e0c5f6Smatthias.ringwald 3550c042179S[email protected] static const uint16_t packet_type_sizes[] = { 3568f8108aaSmatthias.ringwald 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 3578f8108aaSmatthias.ringwald HCI_ACL_DH1_SIZE, 0, 0, 0, 3588f8108aaSmatthias.ringwald HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 3598f8108aaSmatthias.ringwald HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 3608f8108aaSmatthias.ringwald }; 3618f8108aaSmatthias.ringwald 3628f8108aaSmatthias.ringwald static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){ 3638f8108aaSmatthias.ringwald uint16_t packet_types = 0; 3648f8108aaSmatthias.ringwald int i; 3658f8108aaSmatthias.ringwald for (i=0;i<16;i++){ 3668f8108aaSmatthias.ringwald if (packet_type_sizes[i] == 0) continue; 3678f8108aaSmatthias.ringwald if (packet_type_sizes[i] <= buffer_size){ 3688f8108aaSmatthias.ringwald packet_types |= 1 << i; 3698f8108aaSmatthias.ringwald } 3708f8108aaSmatthias.ringwald } 3718f8108aaSmatthias.ringwald // flip bits for "may not be used" 3728f8108aaSmatthias.ringwald packet_types ^= 0x3306; 3738f8108aaSmatthias.ringwald return packet_types; 3748f8108aaSmatthias.ringwald } 3758f8108aaSmatthias.ringwald 3768f8108aaSmatthias.ringwald uint16_t hci_usable_acl_packet_types(void){ 3778f8108aaSmatthias.ringwald return hci_stack.packet_types; 3788f8108aaSmatthias.ringwald } 3798f8108aaSmatthias.ringwald 3807dc17943Smatthias.ringwald uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 3817dc17943Smatthias.ringwald // hci packet buffer is >= acl data packet length 3827dc17943Smatthias.ringwald return hci_stack.hci_packet_buffer; 3837dc17943Smatthias.ringwald } 3847dc17943Smatthias.ringwald 3857dc17943Smatthias.ringwald uint16_t hci_max_acl_data_packet_length(){ 3867dc17943Smatthias.ringwald return hci_stack.acl_data_packet_length; 3877dc17943Smatthias.ringwald } 3887dc17943Smatthias.ringwald 38974ec757aSmatthias.ringwald // avoid huge local variables 390223aafc1Smatthias.ringwald #ifndef EMBEDDED 39174ec757aSmatthias.ringwald static device_name_t device_name; 392223aafc1Smatthias.ringwald #endif 39316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 3941281a47eSmatthias.ringwald bd_addr_t addr; 3952d00edd4Smatthias.ringwald uint8_t link_type; 396fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 3971f7b95a1Smatthias.ringwald hci_connection_t * conn; 39856cf178bSmatthias.ringwald int i; 39922909952Smatthias.ringwald 4005909f7f2Smatthias.ringwald // printf("HCI:EVENT:%02x\n", packet[0]); 4015909f7f2Smatthias.ringwald 4026772a24cSmatthias.ringwald switch (packet[0]) { 40322909952Smatthias.ringwald 4046772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4057ec5eeaaSmatthias.ringwald // get num cmd packets 4067b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4077ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4087ec5eeaaSmatthias.ringwald 409e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 410e2edc0c3Smatthias.ringwald // from offset 5 411e2edc0c3Smatthias.ringwald // status 4121d279b20Smatthias.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" 413e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 41456cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 415e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 41656cf178bSmatthias.ringwald // ignore: total num SCO packets 41756cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 418a7a04bd9Smatthias.ringwald // determine usable ACL payload size 4198fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 4208fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 4218f8108aaSmatthias.ringwald } 422a7a04bd9Smatthias.ringwald // determine usable ACL packet types 42328c93ceeSmatthias.ringwald hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); 4248f8108aaSmatthias.ringwald 4258fcba05dSmatthias.ringwald log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", 4268f8108aaSmatthias.ringwald hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); 427e2edc0c3Smatthias.ringwald } 42856cf178bSmatthias.ringwald } 429381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 430381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 431381fbed8Smatthias.ringwald } 43256cf178bSmatthias.ringwald break; 43356cf178bSmatthias.ringwald 4347ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 4357ec5eeaaSmatthias.ringwald // get num cmd packets 4367b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 4377ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 4387ec5eeaaSmatthias.ringwald break; 4397ec5eeaaSmatthias.ringwald 44056cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 44156cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 44256cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 44356cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 44456cf178bSmatthias.ringwald conn = connection_for_handle(handle); 44556cf178bSmatthias.ringwald if (!conn){ 4467d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 44756cf178bSmatthias.ringwald continue; 44856cf178bSmatthias.ringwald } 44956cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 4507b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 45156cf178bSmatthias.ringwald } 4526772a24cSmatthias.ringwald break; 4536772a24cSmatthias.ringwald 4541f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 45537eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 45637eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 4572d00edd4Smatthias.ringwald link_type = packet[11]; 458339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 45937eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 4601f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 4611f7b95a1Smatthias.ringwald if (!conn) { 4621f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 4631f7b95a1Smatthias.ringwald } 464ce4c8fabSmatthias.ringwald if (!conn) { 465ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 466ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 467ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 468ce4c8fabSmatthias.ringwald break; 469ce4c8fabSmatthias.ringwald } 47032ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 47132ab9390Smatthias.ringwald hci_run(); 47237eaa4cfSmatthias.ringwald } else { 473ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 474ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 475ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 47637eaa4cfSmatthias.ringwald } 4771f7b95a1Smatthias.ringwald break; 4781f7b95a1Smatthias.ringwald 4796772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 480fe1ed1b8Smatthias.ringwald // Connection management 481fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 482339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 4831f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 484fe1ed1b8Smatthias.ringwald if (conn) { 485b448a0e7Smatthias.ringwald if (!packet[2]){ 486c8e4258aSmatthias.ringwald conn->state = OPEN; 487fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 488ee091cf1Smatthias.ringwald 489c785ef68Smatthias.ringwald // restart timer 490c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 491ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 492c785ef68Smatthias.ringwald 493339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 49443bfb1bdSmatthias.ringwald 49543bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 496b448a0e7Smatthias.ringwald } else { 497b448a0e7Smatthias.ringwald // connection failed, remove entry 498b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 499a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 500c12e46e7Smatthias.ringwald 501c12e46e7Smatthias.ringwald // if authentication error, also delete link key 502c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 503c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 504c12e46e7Smatthias.ringwald } 505fe1ed1b8Smatthias.ringwald } 506fe1ed1b8Smatthias.ringwald } 5076772a24cSmatthias.ringwald break; 508fe1ed1b8Smatthias.ringwald 5097fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 5107b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 5117fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 51274ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 51332ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 51464472d52Smatthias.ringwald hci_run(); 515*d9a327f9Smatthias.ringwald // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 51629d53098Smatthias.ringwald return; 5177fde4af9Smatthias.ringwald 5187fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 5197fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 52074ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 52129d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 522287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 52329d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 5247fde4af9Smatthias.ringwald break; 5257fde4af9Smatthias.ringwald 5267fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 5277fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 528*d9a327f9Smatthias.ringwald // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 529*d9a327f9Smatthias.ringwald if (!hci_stack.remote_device_db) break; 530*d9a327f9Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 531*d9a327f9Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 5327fde4af9Smatthias.ringwald break; 5337fde4af9Smatthias.ringwald 534223aafc1Smatthias.ringwald #ifndef EMBEDDED 53574ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 53674ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 53774ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 53874ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 5395a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 5405a06394aSmatthias.ringwald for (i=0; i<248;i++){ 5415a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 5425a06394aSmatthias.ringwald packet[9+i] = 0; 5435a06394aSmatthias.ringwald break; 544cdc9101dSmatthias.ringwald } 5455a06394aSmatthias.ringwald } 546d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 54774ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 54874ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 54974ec757aSmatthias.ringwald break; 55074ec757aSmatthias.ringwald 55174ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 55274ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 55374ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 55474ec757aSmatthias.ringwald // first send inq result packet 55574ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 55674ec757aSmatthias.ringwald // then send cached remote names 55774ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 55874ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 55974ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 56074ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 56174ec757aSmatthias.ringwald } 56274ec757aSmatthias.ringwald } 56374ec757aSmatthias.ringwald return; 564223aafc1Smatthias.ringwald #endif 56574ec757aSmatthias.ringwald 5666772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 567fe1ed1b8Smatthias.ringwald if (!packet[2]){ 568fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 569fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 570fe1ed1b8Smatthias.ringwald if (conn) { 571c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 572fe1ed1b8Smatthias.ringwald } 573fe1ed1b8Smatthias.ringwald } 5746772a24cSmatthias.ringwald break; 5756772a24cSmatthias.ringwald 576c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 577c68bdf90Smatthias.ringwald if(hci_stack.control->hw_error){ 578c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 579c68bdf90Smatthias.ringwald } 580c68bdf90Smatthias.ringwald break; 581c68bdf90Smatthias.ringwald 5825909f7f2Smatthias.ringwald #ifdef HAVE_BLE 5835909f7f2Smatthias.ringwald case HCI_EVENT_LE_META: 5845909f7f2Smatthias.ringwald switch (packet[2]) { 5855909f7f2Smatthias.ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 5865909f7f2Smatthias.ringwald // Connection management 5875909f7f2Smatthias.ringwald bt_flip_addr(addr, &packet[8]); 5885909f7f2Smatthias.ringwald log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 5895909f7f2Smatthias.ringwald // LE connections are auto-accepted, so just create a connection if there isn't one already 5905909f7f2Smatthias.ringwald conn = connection_for_address(addr); 5915909f7f2Smatthias.ringwald if (packet[3]){ 5925909f7f2Smatthias.ringwald if (conn){ 5935909f7f2Smatthias.ringwald // outgoing connection failed, remove entry 5945909f7f2Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 5955909f7f2Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 5965909f7f2Smatthias.ringwald 5975909f7f2Smatthias.ringwald } 5985909f7f2Smatthias.ringwald // if authentication error, also delete link key 5995909f7f2Smatthias.ringwald if (packet[3] == 0x05) { 6005909f7f2Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 6015909f7f2Smatthias.ringwald } 6025909f7f2Smatthias.ringwald break; 6035909f7f2Smatthias.ringwald } 6045909f7f2Smatthias.ringwald if (!conn){ 6055909f7f2Smatthias.ringwald conn = create_connection_for_addr(addr); 6065909f7f2Smatthias.ringwald } 6075909f7f2Smatthias.ringwald if (!conn){ 6085909f7f2Smatthias.ringwald // no memory 6095909f7f2Smatthias.ringwald break; 6105909f7f2Smatthias.ringwald } 6115909f7f2Smatthias.ringwald 6125909f7f2Smatthias.ringwald conn->state = OPEN; 6135909f7f2Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 4); 6145909f7f2Smatthias.ringwald 6155909f7f2Smatthias.ringwald // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 6165909f7f2Smatthias.ringwald 6175909f7f2Smatthias.ringwald // restart timer 6185909f7f2Smatthias.ringwald // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 6195909f7f2Smatthias.ringwald // run_loop_add_timer(&conn->timeout); 6205909f7f2Smatthias.ringwald 6215909f7f2Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 6225909f7f2Smatthias.ringwald 6235909f7f2Smatthias.ringwald hci_emit_nr_connections_changed(); 6245909f7f2Smatthias.ringwald break; 6255909f7f2Smatthias.ringwald 6265909f7f2Smatthias.ringwald default: 6275909f7f2Smatthias.ringwald break; 6285909f7f2Smatthias.ringwald } 6295909f7f2Smatthias.ringwald break; 6305909f7f2Smatthias.ringwald #endif 6315909f7f2Smatthias.ringwald 6326772a24cSmatthias.ringwald default: 6336772a24cSmatthias.ringwald break; 634fe1ed1b8Smatthias.ringwald } 635fe1ed1b8Smatthias.ringwald 6363429f56bSmatthias.ringwald // handle BT initialization 6373429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 6387301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 6397301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 6407301ad89Smatthias.ringwald // hci_stack.substate = 0; 6417301ad89Smatthias.ringwald // } 6427301ad89Smatthias.ringwald // handle normal init sequence 6433429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 6443429f56bSmatthias.ringwald // odd: waiting for event 6453429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 6463429f56bSmatthias.ringwald hci_stack.substate++; 6473429f56bSmatthias.ringwald } 6483429f56bSmatthias.ringwald } 64922909952Smatthias.ringwald } 65022909952Smatthias.ringwald 65189db417bSmatthias.ringwald // help with BT sleep 65289db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 65389db417bSmatthias.ringwald && hci_stack.substate == 1 65489db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 65589db417bSmatthias.ringwald hci_stack.substate++; 65689db417bSmatthias.ringwald } 65789db417bSmatthias.ringwald 6582718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 65994ab26f8Smatthias.ringwald 66094ab26f8Smatthias.ringwald // execute main loop 66194ab26f8Smatthias.ringwald hci_run(); 66216833f0aSmatthias.ringwald } 66316833f0aSmatthias.ringwald 66410e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 66510e830c9Smatthias.ringwald switch (packet_type) { 66610e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 66710e830c9Smatthias.ringwald event_handler(packet, size); 66810e830c9Smatthias.ringwald break; 66910e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 67010e830c9Smatthias.ringwald acl_handler(packet, size); 67110e830c9Smatthias.ringwald break; 67210e830c9Smatthias.ringwald default: 67310e830c9Smatthias.ringwald break; 67410e830c9Smatthias.ringwald } 67510e830c9Smatthias.ringwald } 67610e830c9Smatthias.ringwald 677fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 6782718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 6792718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 68016833f0aSmatthias.ringwald } 68116833f0aSmatthias.ringwald 682404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){ 683475c8125Smatthias.ringwald 684475c8125Smatthias.ringwald // reference to use transport layer implementation 68516833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 686475c8125Smatthias.ringwald 68711e23e5fSmatthias.ringwald // references to used control implementation 68811e23e5fSmatthias.ringwald hci_stack.control = control; 68911e23e5fSmatthias.ringwald 69011e23e5fSmatthias.ringwald // reference to used config 69111e23e5fSmatthias.ringwald hci_stack.config = config; 69211e23e5fSmatthias.ringwald 693fe1ed1b8Smatthias.ringwald // no connections yet 694fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 6950a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 696c0e866bfSmatthias.ringwald hci_stack.connectable = 0; 697758b46ceSmatthias.ringwald 698b031bebbSmatthias.ringwald // no pending cmds 699b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 700b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 701b031bebbSmatthias.ringwald 70216833f0aSmatthias.ringwald // higher level handler 7032718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 70416833f0aSmatthias.ringwald 705404843c1Smatthias.ringwald // store and open remote device db 706404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 707404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 708404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 709404843c1Smatthias.ringwald } 71029d53098Smatthias.ringwald 7118fcba05dSmatthias.ringwald // max acl payload size defined in config.h 7128fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 7138fcba05dSmatthias.ringwald 71416833f0aSmatthias.ringwald // register packet handlers with transport 71510e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 716f5454fc6Smatthias.ringwald 717f5454fc6Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 718475c8125Smatthias.ringwald } 719475c8125Smatthias.ringwald 720404843c1Smatthias.ringwald void hci_close(){ 721404843c1Smatthias.ringwald // close remote device db 722404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 723404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 724404843c1Smatthias.ringwald } 725f5454fc6Smatthias.ringwald while (hci_stack.connections) { 726f5454fc6Smatthias.ringwald hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 727f5454fc6Smatthias.ringwald } 728f5454fc6Smatthias.ringwald hci_power_control(HCI_POWER_OFF); 729404843c1Smatthias.ringwald } 730404843c1Smatthias.ringwald 7318d213e1aSmatthias.ringwald // State-Module-Driver overview 7328d213e1aSmatthias.ringwald // state module low-level 7338d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 7348d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 7358d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 7368d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 737d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 738d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 739c7e0c5f6Smatthias.ringwald 74040d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 7417301ad89Smatthias.ringwald 742038bc64cSmatthias.ringwald // power on 743f9a30166Smatthias.ringwald int err = 0; 744f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 745f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 746f9a30166Smatthias.ringwald } 747038bc64cSmatthias.ringwald if (err){ 7487d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 749038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 750038bc64cSmatthias.ringwald return err; 751038bc64cSmatthias.ringwald } 752038bc64cSmatthias.ringwald 753038bc64cSmatthias.ringwald // open low-level device 754038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 755038bc64cSmatthias.ringwald if (err){ 7567d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 757f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 758f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 759f9a30166Smatthias.ringwald } 760038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 761038bc64cSmatthias.ringwald return err; 762038bc64cSmatthias.ringwald } 7638d213e1aSmatthias.ringwald return 0; 7648d213e1aSmatthias.ringwald } 765038bc64cSmatthias.ringwald 76640d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 7678d213e1aSmatthias.ringwald 7687b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 7699418f9c9Smatthias.ringwald 7708d213e1aSmatthias.ringwald // close low-level device 7718d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 7728d213e1aSmatthias.ringwald 7737b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 7749418f9c9Smatthias.ringwald 7758d213e1aSmatthias.ringwald // power off 7768d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 7778d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 7788d213e1aSmatthias.ringwald } 7799418f9c9Smatthias.ringwald 7807b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 7819418f9c9Smatthias.ringwald 78272ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 78372ea5239Smatthias.ringwald } 78472ea5239Smatthias.ringwald 78540d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 78672ea5239Smatthias.ringwald 7877b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 7883144bce4Smatthias.ringwald 789b429b9b7Smatthias.ringwald #if 0 790b429b9b7Smatthias.ringwald // don't close serial port during sleep 791b429b9b7Smatthias.ringwald 79272ea5239Smatthias.ringwald // close low-level device 79372ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 794b429b9b7Smatthias.ringwald #endif 79572ea5239Smatthias.ringwald 79672ea5239Smatthias.ringwald // sleep mode 7973144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 79872ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 79972ea5239Smatthias.ringwald } 800b429b9b7Smatthias.ringwald 80172ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 8028d213e1aSmatthias.ringwald } 8038d213e1aSmatthias.ringwald 80440d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 805b429b9b7Smatthias.ringwald 8067b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 807b429b9b7Smatthias.ringwald 808b429b9b7Smatthias.ringwald // wake on 809b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 810b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 811b429b9b7Smatthias.ringwald } 812b429b9b7Smatthias.ringwald 813b429b9b7Smatthias.ringwald #if 0 814b429b9b7Smatthias.ringwald // open low-level device 815b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 816b429b9b7Smatthias.ringwald if (err){ 8177d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 818b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 819b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 820b429b9b7Smatthias.ringwald } 821b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 822b429b9b7Smatthias.ringwald return err; 823b429b9b7Smatthias.ringwald } 824b429b9b7Smatthias.ringwald #endif 825b429b9b7Smatthias.ringwald 826b429b9b7Smatthias.ringwald return 0; 827b429b9b7Smatthias.ringwald } 828b429b9b7Smatthias.ringwald 829b429b9b7Smatthias.ringwald 8308d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 831d661ed19Smatthias.ringwald 8327b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 833d661ed19Smatthias.ringwald 8348d213e1aSmatthias.ringwald int err = 0; 8358d213e1aSmatthias.ringwald switch (hci_stack.state){ 8368d213e1aSmatthias.ringwald 8378d213e1aSmatthias.ringwald case HCI_STATE_OFF: 8388d213e1aSmatthias.ringwald switch (power_mode){ 8398d213e1aSmatthias.ringwald case HCI_POWER_ON: 8408d213e1aSmatthias.ringwald err = hci_power_control_on(); 8418d213e1aSmatthias.ringwald if (err) return err; 8427301ad89Smatthias.ringwald // set up state machine 8437301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 8447301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8457301ad89Smatthias.ringwald hci_stack.substate = 0; 8468d213e1aSmatthias.ringwald break; 8478d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8488d213e1aSmatthias.ringwald // do nothing 8498d213e1aSmatthias.ringwald break; 8508d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 851b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 8528d213e1aSmatthias.ringwald break; 8538d213e1aSmatthias.ringwald } 8548d213e1aSmatthias.ringwald break; 8557301ad89Smatthias.ringwald 8568d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 8578d213e1aSmatthias.ringwald switch (power_mode){ 8588d213e1aSmatthias.ringwald case HCI_POWER_ON: 8598d213e1aSmatthias.ringwald // do nothing 8608d213e1aSmatthias.ringwald break; 8618d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8628d213e1aSmatthias.ringwald // no connections yet, just turn it off 8638d213e1aSmatthias.ringwald hci_power_control_off(); 8648d213e1aSmatthias.ringwald break; 8658d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 866b546ac54Smatthias.ringwald // no connections yet, just turn it off 86772ea5239Smatthias.ringwald hci_power_control_sleep(); 8688d213e1aSmatthias.ringwald break; 8698d213e1aSmatthias.ringwald } 8708d213e1aSmatthias.ringwald break; 8717301ad89Smatthias.ringwald 8728d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 8738d213e1aSmatthias.ringwald switch (power_mode){ 8748d213e1aSmatthias.ringwald case HCI_POWER_ON: 8758d213e1aSmatthias.ringwald // do nothing 8768d213e1aSmatthias.ringwald break; 8778d213e1aSmatthias.ringwald case HCI_POWER_OFF: 878c7e0c5f6Smatthias.ringwald // see hci_run 879c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 8808d213e1aSmatthias.ringwald break; 8818d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 882b546ac54Smatthias.ringwald // see hci_run 883b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 88489db417bSmatthias.ringwald hci_stack.substate = 0; 8858d213e1aSmatthias.ringwald break; 8868d213e1aSmatthias.ringwald } 8878d213e1aSmatthias.ringwald break; 8887301ad89Smatthias.ringwald 8898d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 8908d213e1aSmatthias.ringwald switch (power_mode){ 8918d213e1aSmatthias.ringwald case HCI_POWER_ON: 8928d213e1aSmatthias.ringwald // set up state machine 8938d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8948d213e1aSmatthias.ringwald hci_stack.substate = 0; 8958d213e1aSmatthias.ringwald break; 8968d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8978d213e1aSmatthias.ringwald // do nothing 8988d213e1aSmatthias.ringwald break; 8998d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 900b546ac54Smatthias.ringwald // see hci_run 901b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 90289db417bSmatthias.ringwald hci_stack.substate = 0; 9038d213e1aSmatthias.ringwald break; 9048d213e1aSmatthias.ringwald } 9058d213e1aSmatthias.ringwald break; 9068d213e1aSmatthias.ringwald 9078d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 9088d213e1aSmatthias.ringwald switch (power_mode){ 9098d213e1aSmatthias.ringwald case HCI_POWER_ON: 91028171530Smatthias.ringwald 91128171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 91228171530Smatthias.ringwald // nothing to do, if H4 supports power management 91328171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9148747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9158747d67fSmatthias.ringwald hci_stack.substate = 6; 91628171530Smatthias.ringwald break; 91728171530Smatthias.ringwald } 91828171530Smatthias.ringwald #endif 919b546ac54Smatthias.ringwald // set up state machine 920b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 921b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 922b546ac54Smatthias.ringwald hci_stack.substate = 0; 9238d213e1aSmatthias.ringwald break; 9248d213e1aSmatthias.ringwald case HCI_POWER_OFF: 925b546ac54Smatthias.ringwald // see hci_run 926b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9278d213e1aSmatthias.ringwald break; 9288d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 929b546ac54Smatthias.ringwald // do nothing 9308d213e1aSmatthias.ringwald break; 9318d213e1aSmatthias.ringwald } 9328d213e1aSmatthias.ringwald break; 9338d213e1aSmatthias.ringwald 9348d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 9358d213e1aSmatthias.ringwald switch (power_mode){ 9368d213e1aSmatthias.ringwald case HCI_POWER_ON: 93728171530Smatthias.ringwald 93828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 93928171530Smatthias.ringwald // nothing to do, if H4 supports power management 94028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 9418747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 9428747d67fSmatthias.ringwald hci_stack.substate = 6; 943758b46ceSmatthias.ringwald hci_update_scan_enable(); 94428171530Smatthias.ringwald break; 94528171530Smatthias.ringwald } 94628171530Smatthias.ringwald #endif 9473144bce4Smatthias.ringwald err = hci_power_control_wake(); 9483144bce4Smatthias.ringwald if (err) return err; 949b546ac54Smatthias.ringwald // set up state machine 950b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 951b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 952b546ac54Smatthias.ringwald hci_stack.substate = 0; 9538d213e1aSmatthias.ringwald break; 9548d213e1aSmatthias.ringwald case HCI_POWER_OFF: 9554ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 9568d213e1aSmatthias.ringwald break; 9578d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 958b546ac54Smatthias.ringwald // do nothing 9598d213e1aSmatthias.ringwald break; 9608d213e1aSmatthias.ringwald } 9618d213e1aSmatthias.ringwald break; 96211e23e5fSmatthias.ringwald } 96368d92d03Smatthias.ringwald 964038bc64cSmatthias.ringwald // create internal event 965ee8bf225Smatthias.ringwald hci_emit_state(); 966ee8bf225Smatthias.ringwald 96768d92d03Smatthias.ringwald // trigger next/first action 96868d92d03Smatthias.ringwald hci_run(); 96968d92d03Smatthias.ringwald 970475c8125Smatthias.ringwald return 0; 971475c8125Smatthias.ringwald } 972475c8125Smatthias.ringwald 973758b46ceSmatthias.ringwald static void hci_update_scan_enable(void){ 974758b46ceSmatthias.ringwald // 2 = page scan, 1 = inq scan 975758b46ceSmatthias.ringwald hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 976758b46ceSmatthias.ringwald hci_run(); 977758b46ceSmatthias.ringwald } 978758b46ceSmatthias.ringwald 979381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 980381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 981381fbed8Smatthias.ringwald 982381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 983381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 984381fbed8Smatthias.ringwald return; 985381fbed8Smatthias.ringwald } 986381fbed8Smatthias.ringwald 987381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 988758b46ceSmatthias.ringwald hci_update_scan_enable(); 989758b46ceSmatthias.ringwald } 990b031bebbSmatthias.ringwald 991758b46ceSmatthias.ringwald void hci_connectable_control(uint8_t enable){ 992758b46ceSmatthias.ringwald if (enable) enable = 1; // normalize argument 993758b46ceSmatthias.ringwald 994758b46ceSmatthias.ringwald // don't emit event 995758b46ceSmatthias.ringwald if (hci_stack.connectable == enable) return; 996758b46ceSmatthias.ringwald 997758b46ceSmatthias.ringwald hci_stack.connectable = enable; 998758b46ceSmatthias.ringwald hci_update_scan_enable(); 999381fbed8Smatthias.ringwald } 1000381fbed8Smatthias.ringwald 100106b35ec0Smatthias.ringwald void hci_run(){ 10028a485f27Smatthias.ringwald 100332ab9390Smatthias.ringwald hci_connection_t * connection; 100432ab9390Smatthias.ringwald linked_item_t * it; 100532ab9390Smatthias.ringwald 1006ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1007ce4c8fabSmatthias.ringwald 1008b031bebbSmatthias.ringwald // global/non-connection oriented commands 1009b031bebbSmatthias.ringwald 1010b031bebbSmatthias.ringwald // decline incoming connections 1011ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 1012ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 1013ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 1014ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1015ce4c8fabSmatthias.ringwald } 1016ce4c8fabSmatthias.ringwald 1017b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1018b031bebbSmatthias.ringwald 1019b031bebbSmatthias.ringwald // send scan enable 1020eda18bf8Smatthias.ringwald if (hci_stack.new_scan_enable_value != 0xff){ 1021b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1022b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 1023b031bebbSmatthias.ringwald } 1024b031bebbSmatthias.ringwald 102532ab9390Smatthias.ringwald // send pending HCI commands 102632ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 102732ab9390Smatthias.ringwald 1028b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 102932ab9390Smatthias.ringwald 1030b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 103132ab9390Smatthias.ringwald 103232ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 10337b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 103432ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 103532ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 1036c7e0c5f6Smatthias.ringwald } 1037c7e0c5f6Smatthias.ringwald 1038de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 103932ab9390Smatthias.ringwald 104032ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 104132ab9390Smatthias.ringwald link_key_t link_key; 10427b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 104332ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 104432ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 104532ab9390Smatthias.ringwald } else { 104632ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 104732ab9390Smatthias.ringwald } 104832ab9390Smatthias.ringwald connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST; 104932ab9390Smatthias.ringwald } 105032ab9390Smatthias.ringwald } 105132ab9390Smatthias.ringwald 1052de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1053c7e0c5f6Smatthias.ringwald 10543429f56bSmatthias.ringwald switch (hci_stack.state){ 10553429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 10567b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 10573429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 10583429f56bSmatthias.ringwald // odd: waiting for command completion 105906b35ec0Smatthias.ringwald return; 10603429f56bSmatthias.ringwald } 106190919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 1062c7492964Smatthias.ringwald case 0: // RESET 106322909952Smatthias.ringwald hci_send_cmd(&hci_reset); 1064c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1065c7492964Smatthias.ringwald // skip baud change 1066c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 1067c7492964Smatthias.ringwald } 10683429f56bSmatthias.ringwald break; 1069c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 10707dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 10717dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1072c7492964Smatthias.ringwald break; 1073c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 1074c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1075c7492964Smatthias.ringwald hci_stack.substate += 2; 1076c7492964Smatthias.ringwald // break missing here for fall through 1077c7492964Smatthias.ringwald 1078c7492964Smatthias.ringwald case 3: 107990919203Smatthias.ringwald // custom initialization 1080db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 10817dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1082d62aca9fSmatthias.ringwald if (valid_cmd){ 10837dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 10847dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1085c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 108690919203Smatthias.ringwald break; 108790919203Smatthias.ringwald } 1088d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 108990919203Smatthias.ringwald } 10902f6c30e1Smatthias.ringwald // otherwise continue 1091f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1092f432a6ddSmatthias.ringwald break; 1093c7492964Smatthias.ringwald case 4: 1094e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1095e2edc0c3Smatthias.ringwald break; 1096c7492964Smatthias.ringwald case 5: 10973429f56bSmatthias.ringwald // ca. 15 sec 10983429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 10993429f56bSmatthias.ringwald break; 1100c7492964Smatthias.ringwald case 6: 1101758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 1102f432a6ddSmatthias.ringwald break; 1103c7492964Smatthias.ringwald case 7: 11048a485f27Smatthias.ringwald #ifndef EMBEDDED 11058a485f27Smatthias.ringwald { 11068a485f27Smatthias.ringwald char hostname[30]; 11078a485f27Smatthias.ringwald gethostname(hostname, 30); 11088a485f27Smatthias.ringwald hostname[29] = '\0'; 11098a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 11108a485f27Smatthias.ringwald break; 11118a485f27Smatthias.ringwald } 1112c7492964Smatthias.ringwald case 8: 11133c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 11143c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 11153c4d4b90Smatthias.ringwald break; 11163c4d4b90Smatthias.ringwald 1117c7492964Smatthias.ringwald case 9: 11183c4d4b90Smatthias.ringwald #endif 11198a485f27Smatthias.ringwald #endif 11203429f56bSmatthias.ringwald // done. 11213429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1122b360b6adSmatthias.ringwald hci_emit_state(); 11233429f56bSmatthias.ringwald break; 11243429f56bSmatthias.ringwald default: 11253429f56bSmatthias.ringwald break; 1126475c8125Smatthias.ringwald } 11273429f56bSmatthias.ringwald hci_stack.substate++; 11283429f56bSmatthias.ringwald break; 1129c7e0c5f6Smatthias.ringwald 1130c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1131c7e0c5f6Smatthias.ringwald 11327b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1133c7e0c5f6Smatthias.ringwald // close all open connections 1134c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1135c7e0c5f6Smatthias.ringwald if (connection){ 113632ab9390Smatthias.ringwald 1137c7e0c5f6Smatthias.ringwald // send disconnect 113832ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 113932ab9390Smatthias.ringwald 114079bbfa0bSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11416ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1142c7e0c5f6Smatthias.ringwald 1143c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1144c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1145c7e0c5f6Smatthias.ringwald return; 1146c7e0c5f6Smatthias.ringwald } 11477b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1148c7e0c5f6Smatthias.ringwald 114972ea5239Smatthias.ringwald // switch mode 1150c7e0c5f6Smatthias.ringwald hci_power_control_off(); 11519418f9c9Smatthias.ringwald 11527b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 115372ea5239Smatthias.ringwald hci_emit_state(); 11547b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 115572ea5239Smatthias.ringwald break; 1156c7e0c5f6Smatthias.ringwald 115772ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 115889db417bSmatthias.ringwald switch(hci_stack.substate) { 115989db417bSmatthias.ringwald case 0: 11607b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 116172ea5239Smatthias.ringwald // close all open connections 116272ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 116366da7044Smatthias.ringwald 116428171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 116566da7044Smatthias.ringwald // don't close connections, if H4 supports power management 116666da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 116766da7044Smatthias.ringwald connection = NULL; 116866da7044Smatthias.ringwald } 116966da7044Smatthias.ringwald #endif 117072ea5239Smatthias.ringwald if (connection){ 117132ab9390Smatthias.ringwald 117272ea5239Smatthias.ringwald // send disconnect 117332ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 117432ab9390Smatthias.ringwald 117579bbfa0bSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 11766ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 117772ea5239Smatthias.ringwald 117872ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 117972ea5239Smatthias.ringwald hci_shutdown_connection(connection); 118072ea5239Smatthias.ringwald return; 118172ea5239Smatthias.ringwald } 118272ea5239Smatthias.ringwald 118389db417bSmatthias.ringwald // disable page and inquiry scan 118432ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 118532ab9390Smatthias.ringwald 1186758b46ceSmatthias.ringwald log_info("HCI_STATE_HALTING, disabling inq cans\n"); 1187758b46ceSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 118889db417bSmatthias.ringwald 118989db417bSmatthias.ringwald // continue in next sub state 119089db417bSmatthias.ringwald hci_stack.substate++; 119189db417bSmatthias.ringwald break; 119289db417bSmatthias.ringwald case 1: 119389db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 119489db417bSmatthias.ringwald break; 119589db417bSmatthias.ringwald case 2: 11967b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 119728171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 119828171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 119928171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 120028171530Smatthias.ringwald // SLEEP MODE reached 120128171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 120228171530Smatthias.ringwald hci_emit_state(); 120328171530Smatthias.ringwald break; 120428171530Smatthias.ringwald } 120528171530Smatthias.ringwald #endif 120672ea5239Smatthias.ringwald // switch mode 120789db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1208c7e0c5f6Smatthias.ringwald hci_emit_state(); 120928171530Smatthias.ringwald break; 121028171530Smatthias.ringwald 121189db417bSmatthias.ringwald default: 121289db417bSmatthias.ringwald break; 121389db417bSmatthias.ringwald } 1214c7e0c5f6Smatthias.ringwald break; 1215c7e0c5f6Smatthias.ringwald 12163429f56bSmatthias.ringwald default: 12173429f56bSmatthias.ringwald break; 12181f504dbdSmatthias.ringwald } 12193429f56bSmatthias.ringwald } 122016833f0aSmatthias.ringwald 122131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1222c8e4258aSmatthias.ringwald bd_addr_t addr; 1223c8e4258aSmatthias.ringwald hci_connection_t * conn; 1224c8e4258aSmatthias.ringwald // house-keeping 1225c8e4258aSmatthias.ringwald 1226c8e4258aSmatthias.ringwald // create_connection? 1227c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1228c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1229339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1230c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1231c8e4258aSmatthias.ringwald if (conn) { 1232c8e4258aSmatthias.ringwald // if connection exists 1233c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 123417f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 123517f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1236c8e4258aSmatthias.ringwald } 123717f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1238c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1239c8e4258aSmatthias.ringwald 124017f1ba2aSmatthias.ringwald } 1241c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 124217f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 124317f1ba2aSmatthias.ringwald if (!conn){ 124417f1ba2aSmatthias.ringwald // notify client that alloc failed 124517f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 124617f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 124717f1ba2aSmatthias.ringwald } 1248c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1249c8e4258aSmatthias.ringwald } 1250c8e4258aSmatthias.ringwald 12517fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 12527fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 12537fde4af9Smatthias.ringwald } 12547fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 12557fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 12567fde4af9Smatthias.ringwald } 12577fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 12587fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 12597fde4af9Smatthias.ringwald } 12607fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 12617fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 12627fde4af9Smatthias.ringwald } 12637fde4af9Smatthias.ringwald 12648ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 12658ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 12668ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 12678ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 12688ef73945Smatthias.ringwald } 12698ef73945Smatthias.ringwald } 1270c8e4258aSmatthias.ringwald 127131452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1272622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 127331452debSmatthias.ringwald } 12748adf0ddaSmatthias.ringwald 12751cd208adSmatthias.ringwald /** 12761cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 12771cd208adSmatthias.ringwald */ 1278fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 12791cd208adSmatthias.ringwald va_list argptr; 12801cd208adSmatthias.ringwald va_start(argptr, cmd); 12817dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 12821cd208adSmatthias.ringwald va_end(argptr); 12837dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 128493b8dc03Smatthias.ringwald } 1285c8e4258aSmatthias.ringwald 1286ee091cf1Smatthias.ringwald // Create various non-HCI events. 1287ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1288ee091cf1Smatthias.ringwald 1289c8e4258aSmatthias.ringwald void hci_emit_state(){ 1290425d1371Smatthias.ringwald uint8_t event[3]; 129180d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1292425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1293c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1294425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1295425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1296c8e4258aSmatthias.ringwald } 1297c8e4258aSmatthias.ringwald 129817f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1299425d1371Smatthias.ringwald uint8_t event[13]; 1300c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1301425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 130217f1ba2aSmatthias.ringwald event[2] = status; 1303c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1304c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1305c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1306c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1307425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1308425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1309c8e4258aSmatthias.ringwald } 1310c8e4258aSmatthias.ringwald 13113c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1312425d1371Smatthias.ringwald uint8_t event[6]; 13133c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1314e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 13153c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 13163c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 13173c4d4b90Smatthias.ringwald event[5] = reason; 1318425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1319425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13203c4d4b90Smatthias.ringwald } 13213c4d4b90Smatthias.ringwald 1322ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1323425d1371Smatthias.ringwald uint8_t event[4]; 132480d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1325425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1326ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1327425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1328425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1329ee091cf1Smatthias.ringwald } 133043bfb1bdSmatthias.ringwald 133143bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1332425d1371Smatthias.ringwald uint8_t event[3]; 133380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1334425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 133543bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1336425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1337425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 133843bfb1bdSmatthias.ringwald } 1339038bc64cSmatthias.ringwald 1340038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1341425d1371Smatthias.ringwald uint8_t event[2]; 134280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1343425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1344425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1345425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1346038bc64cSmatthias.ringwald } 13471b0e3922Smatthias.ringwald 134809ba8edeSmatthias.ringwald #ifndef EMBEDDED 13491b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1350425d1371Smatthias.ringwald uint8_t event[6]; 13511b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1352425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1353425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1354425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1355425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1356425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1357425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13581b0e3922Smatthias.ringwald } 135909ba8edeSmatthias.ringwald #endif 13601b0e3922Smatthias.ringwald 13612ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1362425d1371Smatthias.ringwald uint8_t event[3]; 13632ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1364425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 13652ed6235cSmatthias.ringwald event[2] = enabled; 1366425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1367e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 13682ed6235cSmatthias.ringwald } 1369627c2f45Smatthias.ringwald 1370627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1371425d1371Smatthias.ringwald uint8_t event[2+1+6+248]; 1372627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1373425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1374f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 13752f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1376f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1377425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1378425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1379627c2f45Smatthias.ringwald } 1380381fbed8Smatthias.ringwald 1381381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1382425d1371Smatthias.ringwald uint8_t event[3]; 1383381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1384425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1385381fbed8Smatthias.ringwald event[2] = enabled; 1386425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1387425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1388381fbed8Smatthias.ringwald } 1389