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 39*c8901d41Smatthias.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 3986772a24cSmatthias.ringwald switch (packet[0]) { 39922909952Smatthias.ringwald 4006772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 4017ec5eeaaSmatthias.ringwald // get num cmd packets 4027b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 4037ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 4047ec5eeaaSmatthias.ringwald 405e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 406e2edc0c3Smatthias.ringwald // from offset 5 407e2edc0c3Smatthias.ringwald // status 4081d279b20Smatthias.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" 409e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 41056cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 411e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 41256cf178bSmatthias.ringwald // ignore: total num SCO packets 41356cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 414a7a04bd9Smatthias.ringwald // determine usable ACL payload size 4158fcba05dSmatthias.ringwald if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 4168fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 4178f8108aaSmatthias.ringwald } 418a7a04bd9Smatthias.ringwald // determine usable ACL packet types 41928c93ceeSmatthias.ringwald hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); 4208f8108aaSmatthias.ringwald 4218fcba05dSmatthias.ringwald log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", 4228f8108aaSmatthias.ringwald hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); 423e2edc0c3Smatthias.ringwald } 42456cf178bSmatthias.ringwald } 425381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 426381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 427381fbed8Smatthias.ringwald } 42856cf178bSmatthias.ringwald break; 42956cf178bSmatthias.ringwald 4307ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 4317ec5eeaaSmatthias.ringwald // get num cmd packets 4327b5fbe1fSmatthias.ringwald // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 4337ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 4347ec5eeaaSmatthias.ringwald break; 4357ec5eeaaSmatthias.ringwald 43656cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 43756cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 43856cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 43956cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 44056cf178bSmatthias.ringwald conn = connection_for_handle(handle); 44156cf178bSmatthias.ringwald if (!conn){ 4427d67539fSmatthias.ringwald log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 44356cf178bSmatthias.ringwald continue; 44456cf178bSmatthias.ringwald } 44556cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 4467b5fbe1fSmatthias.ringwald // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 44756cf178bSmatthias.ringwald } 4486772a24cSmatthias.ringwald break; 4496772a24cSmatthias.ringwald 4501f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 45137eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 45237eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 4532d00edd4Smatthias.ringwald link_type = packet[11]; 454339b6768Smatthias.ringwald log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 45537eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 4561f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 4571f7b95a1Smatthias.ringwald if (!conn) { 4581f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 4591f7b95a1Smatthias.ringwald } 460ce4c8fabSmatthias.ringwald if (!conn) { 461ce4c8fabSmatthias.ringwald // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 462ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0d; 463ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 464ce4c8fabSmatthias.ringwald break; 465ce4c8fabSmatthias.ringwald } 46632ab9390Smatthias.ringwald conn->state = RECEIVED_CONNECTION_REQUEST; 46732ab9390Smatthias.ringwald hci_run(); 46837eaa4cfSmatthias.ringwald } else { 469ce4c8fabSmatthias.ringwald // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 470ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0x0a; 471ce4c8fabSmatthias.ringwald BD_ADDR_COPY(hci_stack.decline_addr, addr); 47237eaa4cfSmatthias.ringwald } 4731f7b95a1Smatthias.ringwald break; 4741f7b95a1Smatthias.ringwald 4756772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 476fe1ed1b8Smatthias.ringwald // Connection management 477fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 478339b6768Smatthias.ringwald log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 4791f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 480fe1ed1b8Smatthias.ringwald if (conn) { 481b448a0e7Smatthias.ringwald if (!packet[2]){ 482c8e4258aSmatthias.ringwald conn->state = OPEN; 483fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 484ee091cf1Smatthias.ringwald 485c785ef68Smatthias.ringwald // restart timer 486c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 487ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 488c785ef68Smatthias.ringwald 489339b6768Smatthias.ringwald log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 49043bfb1bdSmatthias.ringwald 49143bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 492b448a0e7Smatthias.ringwald } else { 493b448a0e7Smatthias.ringwald // connection failed, remove entry 494b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 495a3b02b71Smatthias.ringwald btstack_memory_hci_connection_free( conn ); 496c12e46e7Smatthias.ringwald 497c12e46e7Smatthias.ringwald // if authentication error, also delete link key 498c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 499c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 500c12e46e7Smatthias.ringwald } 501fe1ed1b8Smatthias.ringwald } 502fe1ed1b8Smatthias.ringwald } 5036772a24cSmatthias.ringwald break; 504fe1ed1b8Smatthias.ringwald 5057fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 5067b5fbe1fSmatthias.ringwald log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 5077fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 50874ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 50932ab9390Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 51064472d52Smatthias.ringwald hci_run(); 51129d53098Smatthias.ringwald // request already answered 51229d53098Smatthias.ringwald return; 5137fde4af9Smatthias.ringwald 5147fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 5157fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 51674ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 51729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 518287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 51929d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 5207fde4af9Smatthias.ringwald break; 5217fde4af9Smatthias.ringwald 5227fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 5237fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 5247fde4af9Smatthias.ringwald break; 5257fde4af9Smatthias.ringwald 526223aafc1Smatthias.ringwald #ifndef EMBEDDED 52774ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 52874ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 52974ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 53074ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 5315a06394aSmatthias.ringwald // fix for invalid remote names - terminate on 0xff 5325a06394aSmatthias.ringwald for (i=0; i<248;i++){ 5335a06394aSmatthias.ringwald if (packet[9+i] == 0xff){ 5345a06394aSmatthias.ringwald packet[9+i] = 0; 5355a06394aSmatthias.ringwald break; 536cdc9101dSmatthias.ringwald } 5375a06394aSmatthias.ringwald } 538d2fe945cS[email protected] memset(&device_name, 0, sizeof(device_name_t)); 53974ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 54074ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 54174ec757aSmatthias.ringwald break; 54274ec757aSmatthias.ringwald 54374ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 54474ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 54574ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 54674ec757aSmatthias.ringwald // first send inq result packet 54774ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 54874ec757aSmatthias.ringwald // then send cached remote names 54974ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 55074ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 55174ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 55274ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 55374ec757aSmatthias.ringwald } 55474ec757aSmatthias.ringwald } 55574ec757aSmatthias.ringwald return; 556223aafc1Smatthias.ringwald #endif 55774ec757aSmatthias.ringwald 5586772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 559fe1ed1b8Smatthias.ringwald if (!packet[2]){ 560fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 561fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 562fe1ed1b8Smatthias.ringwald if (conn) { 563c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 564fe1ed1b8Smatthias.ringwald } 565fe1ed1b8Smatthias.ringwald } 5666772a24cSmatthias.ringwald break; 5676772a24cSmatthias.ringwald 568c68bdf90Smatthias.ringwald case HCI_EVENT_HARDWARE_ERROR: 569c68bdf90Smatthias.ringwald if(hci_stack.control->hw_error){ 570c68bdf90Smatthias.ringwald (*hci_stack.control->hw_error)(); 571c68bdf90Smatthias.ringwald } 572c68bdf90Smatthias.ringwald break; 573c68bdf90Smatthias.ringwald 5746772a24cSmatthias.ringwald default: 5756772a24cSmatthias.ringwald break; 576fe1ed1b8Smatthias.ringwald } 577fe1ed1b8Smatthias.ringwald 5783429f56bSmatthias.ringwald // handle BT initialization 5793429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 5807301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 5817301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 5827301ad89Smatthias.ringwald // hci_stack.substate = 0; 5837301ad89Smatthias.ringwald // } 5847301ad89Smatthias.ringwald // handle normal init sequence 5853429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 5863429f56bSmatthias.ringwald // odd: waiting for event 5873429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 5883429f56bSmatthias.ringwald hci_stack.substate++; 5893429f56bSmatthias.ringwald } 5903429f56bSmatthias.ringwald } 59122909952Smatthias.ringwald } 59222909952Smatthias.ringwald 59389db417bSmatthias.ringwald // help with BT sleep 59489db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 59589db417bSmatthias.ringwald && hci_stack.substate == 1 59689db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 59789db417bSmatthias.ringwald hci_stack.substate++; 59889db417bSmatthias.ringwald } 59989db417bSmatthias.ringwald 6002718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 60194ab26f8Smatthias.ringwald 60294ab26f8Smatthias.ringwald // execute main loop 60394ab26f8Smatthias.ringwald hci_run(); 60416833f0aSmatthias.ringwald } 60516833f0aSmatthias.ringwald 60610e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 60710e830c9Smatthias.ringwald switch (packet_type) { 60810e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 60910e830c9Smatthias.ringwald event_handler(packet, size); 61010e830c9Smatthias.ringwald break; 61110e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 61210e830c9Smatthias.ringwald acl_handler(packet, size); 61310e830c9Smatthias.ringwald break; 61410e830c9Smatthias.ringwald default: 61510e830c9Smatthias.ringwald break; 61610e830c9Smatthias.ringwald } 61710e830c9Smatthias.ringwald } 61810e830c9Smatthias.ringwald 619fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 6202718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 6212718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 62216833f0aSmatthias.ringwald } 62316833f0aSmatthias.ringwald 624404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){ 625475c8125Smatthias.ringwald 626475c8125Smatthias.ringwald // reference to use transport layer implementation 62716833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 628475c8125Smatthias.ringwald 62911e23e5fSmatthias.ringwald // references to used control implementation 63011e23e5fSmatthias.ringwald hci_stack.control = control; 63111e23e5fSmatthias.ringwald 63211e23e5fSmatthias.ringwald // reference to used config 63311e23e5fSmatthias.ringwald hci_stack.config = config; 63411e23e5fSmatthias.ringwald 635fe1ed1b8Smatthias.ringwald // no connections yet 636fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 6370a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 638fe1ed1b8Smatthias.ringwald 639b031bebbSmatthias.ringwald // no pending cmds 640b031bebbSmatthias.ringwald hci_stack.decline_reason = 0; 641b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 642b031bebbSmatthias.ringwald 64316833f0aSmatthias.ringwald // higher level handler 6442718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 64516833f0aSmatthias.ringwald 646404843c1Smatthias.ringwald // store and open remote device db 647404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 648404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 649404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 650404843c1Smatthias.ringwald } 65129d53098Smatthias.ringwald 6528fcba05dSmatthias.ringwald // max acl payload size defined in config.h 6538fcba05dSmatthias.ringwald hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 6548fcba05dSmatthias.ringwald 65516833f0aSmatthias.ringwald // register packet handlers with transport 65610e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 657475c8125Smatthias.ringwald } 658475c8125Smatthias.ringwald 659404843c1Smatthias.ringwald void hci_close(){ 660404843c1Smatthias.ringwald // close remote device db 661404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 662404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 663404843c1Smatthias.ringwald } 664404843c1Smatthias.ringwald } 665404843c1Smatthias.ringwald 6668d213e1aSmatthias.ringwald // State-Module-Driver overview 6678d213e1aSmatthias.ringwald // state module low-level 6688d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 6698d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 6708d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 6718d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 672d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 673d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 674c7e0c5f6Smatthias.ringwald 67540d1c7a4Smatthias.ringwald static int hci_power_control_on(void){ 6767301ad89Smatthias.ringwald 677038bc64cSmatthias.ringwald // power on 678f9a30166Smatthias.ringwald int err = 0; 679f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 680f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 681f9a30166Smatthias.ringwald } 682038bc64cSmatthias.ringwald if (err){ 6837d67539fSmatthias.ringwald log_error( "POWER_ON failed\n"); 684038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 685038bc64cSmatthias.ringwald return err; 686038bc64cSmatthias.ringwald } 687038bc64cSmatthias.ringwald 688038bc64cSmatthias.ringwald // open low-level device 689038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 690038bc64cSmatthias.ringwald if (err){ 6917d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 692f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 693f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 694f9a30166Smatthias.ringwald } 695038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 696038bc64cSmatthias.ringwald return err; 697038bc64cSmatthias.ringwald } 6988d213e1aSmatthias.ringwald return 0; 6998d213e1aSmatthias.ringwald } 700038bc64cSmatthias.ringwald 70140d1c7a4Smatthias.ringwald static void hci_power_control_off(void){ 7028d213e1aSmatthias.ringwald 7037b5fbe1fSmatthias.ringwald log_info("hci_power_control_off\n"); 7049418f9c9Smatthias.ringwald 7058d213e1aSmatthias.ringwald // close low-level device 7068d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 7078d213e1aSmatthias.ringwald 7087b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - hci_transport closed\n"); 7099418f9c9Smatthias.ringwald 7108d213e1aSmatthias.ringwald // power off 7118d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 7128d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 7138d213e1aSmatthias.ringwald } 7149418f9c9Smatthias.ringwald 7157b5fbe1fSmatthias.ringwald log_info("hci_power_control_off - control closed\n"); 7169418f9c9Smatthias.ringwald 71772ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 71872ea5239Smatthias.ringwald } 71972ea5239Smatthias.ringwald 72040d1c7a4Smatthias.ringwald static void hci_power_control_sleep(void){ 72172ea5239Smatthias.ringwald 7227b5fbe1fSmatthias.ringwald log_info("hci_power_control_sleep\n"); 7233144bce4Smatthias.ringwald 724b429b9b7Smatthias.ringwald #if 0 725b429b9b7Smatthias.ringwald // don't close serial port during sleep 726b429b9b7Smatthias.ringwald 72772ea5239Smatthias.ringwald // close low-level device 72872ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 729b429b9b7Smatthias.ringwald #endif 73072ea5239Smatthias.ringwald 73172ea5239Smatthias.ringwald // sleep mode 7323144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 73372ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 73472ea5239Smatthias.ringwald } 735b429b9b7Smatthias.ringwald 73672ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 7378d213e1aSmatthias.ringwald } 7388d213e1aSmatthias.ringwald 73940d1c7a4Smatthias.ringwald static int hci_power_control_wake(void){ 740b429b9b7Smatthias.ringwald 7417b5fbe1fSmatthias.ringwald log_info("hci_power_control_wake\n"); 742b429b9b7Smatthias.ringwald 743b429b9b7Smatthias.ringwald // wake on 744b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 745b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 746b429b9b7Smatthias.ringwald } 747b429b9b7Smatthias.ringwald 748b429b9b7Smatthias.ringwald #if 0 749b429b9b7Smatthias.ringwald // open low-level device 750b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 751b429b9b7Smatthias.ringwald if (err){ 7527d67539fSmatthias.ringwald log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 753b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 754b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 755b429b9b7Smatthias.ringwald } 756b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 757b429b9b7Smatthias.ringwald return err; 758b429b9b7Smatthias.ringwald } 759b429b9b7Smatthias.ringwald #endif 760b429b9b7Smatthias.ringwald 761b429b9b7Smatthias.ringwald return 0; 762b429b9b7Smatthias.ringwald } 763b429b9b7Smatthias.ringwald 764b429b9b7Smatthias.ringwald 7658d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 766d661ed19Smatthias.ringwald 7677b5fbe1fSmatthias.ringwald log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 768d661ed19Smatthias.ringwald 7698d213e1aSmatthias.ringwald int err = 0; 7708d213e1aSmatthias.ringwald switch (hci_stack.state){ 7718d213e1aSmatthias.ringwald 7728d213e1aSmatthias.ringwald case HCI_STATE_OFF: 7738d213e1aSmatthias.ringwald switch (power_mode){ 7748d213e1aSmatthias.ringwald case HCI_POWER_ON: 7758d213e1aSmatthias.ringwald err = hci_power_control_on(); 7768d213e1aSmatthias.ringwald if (err) return err; 7777301ad89Smatthias.ringwald // set up state machine 7787301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 7797301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 7807301ad89Smatthias.ringwald hci_stack.substate = 0; 7818d213e1aSmatthias.ringwald break; 7828d213e1aSmatthias.ringwald case HCI_POWER_OFF: 7838d213e1aSmatthias.ringwald // do nothing 7848d213e1aSmatthias.ringwald break; 7858d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 786b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 7878d213e1aSmatthias.ringwald break; 7888d213e1aSmatthias.ringwald } 7898d213e1aSmatthias.ringwald break; 7907301ad89Smatthias.ringwald 7918d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 7928d213e1aSmatthias.ringwald switch (power_mode){ 7938d213e1aSmatthias.ringwald case HCI_POWER_ON: 7948d213e1aSmatthias.ringwald // do nothing 7958d213e1aSmatthias.ringwald break; 7968d213e1aSmatthias.ringwald case HCI_POWER_OFF: 7978d213e1aSmatthias.ringwald // no connections yet, just turn it off 7988d213e1aSmatthias.ringwald hci_power_control_off(); 7998d213e1aSmatthias.ringwald break; 8008d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 801b546ac54Smatthias.ringwald // no connections yet, just turn it off 80272ea5239Smatthias.ringwald hci_power_control_sleep(); 8038d213e1aSmatthias.ringwald break; 8048d213e1aSmatthias.ringwald } 8058d213e1aSmatthias.ringwald break; 8067301ad89Smatthias.ringwald 8078d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 8088d213e1aSmatthias.ringwald switch (power_mode){ 8098d213e1aSmatthias.ringwald case HCI_POWER_ON: 8108d213e1aSmatthias.ringwald // do nothing 8118d213e1aSmatthias.ringwald break; 8128d213e1aSmatthias.ringwald case HCI_POWER_OFF: 813c7e0c5f6Smatthias.ringwald // see hci_run 814c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 8158d213e1aSmatthias.ringwald break; 8168d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 817b546ac54Smatthias.ringwald // see hci_run 818b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 81989db417bSmatthias.ringwald hci_stack.substate = 0; 8208d213e1aSmatthias.ringwald break; 8218d213e1aSmatthias.ringwald } 8228d213e1aSmatthias.ringwald break; 8237301ad89Smatthias.ringwald 8248d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 8258d213e1aSmatthias.ringwald switch (power_mode){ 8268d213e1aSmatthias.ringwald case HCI_POWER_ON: 8278d213e1aSmatthias.ringwald // set up state machine 8288d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8298d213e1aSmatthias.ringwald hci_stack.substate = 0; 8308d213e1aSmatthias.ringwald break; 8318d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8328d213e1aSmatthias.ringwald // do nothing 8338d213e1aSmatthias.ringwald break; 8348d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 835b546ac54Smatthias.ringwald // see hci_run 836b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 83789db417bSmatthias.ringwald hci_stack.substate = 0; 8388d213e1aSmatthias.ringwald break; 8398d213e1aSmatthias.ringwald } 8408d213e1aSmatthias.ringwald break; 8418d213e1aSmatthias.ringwald 8428d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 8438d213e1aSmatthias.ringwald switch (power_mode){ 8448d213e1aSmatthias.ringwald case HCI_POWER_ON: 84528171530Smatthias.ringwald 84628171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 84728171530Smatthias.ringwald // nothing to do, if H4 supports power management 84828171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 8498747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8508747d67fSmatthias.ringwald hci_stack.substate = 6; 85128171530Smatthias.ringwald break; 85228171530Smatthias.ringwald } 85328171530Smatthias.ringwald #endif 854b546ac54Smatthias.ringwald // set up state machine 855b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 856b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 857b546ac54Smatthias.ringwald hci_stack.substate = 0; 8588d213e1aSmatthias.ringwald break; 8598d213e1aSmatthias.ringwald case HCI_POWER_OFF: 860b546ac54Smatthias.ringwald // see hci_run 861b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 8628d213e1aSmatthias.ringwald break; 8638d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 864b546ac54Smatthias.ringwald // do nothing 8658d213e1aSmatthias.ringwald break; 8668d213e1aSmatthias.ringwald } 8678d213e1aSmatthias.ringwald break; 8688d213e1aSmatthias.ringwald 8698d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 8708d213e1aSmatthias.ringwald switch (power_mode){ 8718d213e1aSmatthias.ringwald case HCI_POWER_ON: 87228171530Smatthias.ringwald 87328171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 87428171530Smatthias.ringwald // nothing to do, if H4 supports power management 87528171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 8768747d67fSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 8778747d67fSmatthias.ringwald hci_stack.substate = 6; 87828171530Smatthias.ringwald break; 87928171530Smatthias.ringwald } 88028171530Smatthias.ringwald #endif 8813144bce4Smatthias.ringwald err = hci_power_control_wake(); 8823144bce4Smatthias.ringwald if (err) return err; 883b546ac54Smatthias.ringwald // set up state machine 884b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 885b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 886b546ac54Smatthias.ringwald hci_stack.substate = 0; 8878d213e1aSmatthias.ringwald break; 8888d213e1aSmatthias.ringwald case HCI_POWER_OFF: 8894ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 8908d213e1aSmatthias.ringwald break; 8918d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 892b546ac54Smatthias.ringwald // do nothing 8938d213e1aSmatthias.ringwald break; 8948d213e1aSmatthias.ringwald } 8958d213e1aSmatthias.ringwald break; 89611e23e5fSmatthias.ringwald } 89768d92d03Smatthias.ringwald 898038bc64cSmatthias.ringwald // create internal event 899ee8bf225Smatthias.ringwald hci_emit_state(); 900ee8bf225Smatthias.ringwald 90168d92d03Smatthias.ringwald // trigger next/first action 90268d92d03Smatthias.ringwald hci_run(); 90368d92d03Smatthias.ringwald 904475c8125Smatthias.ringwald return 0; 905475c8125Smatthias.ringwald } 906475c8125Smatthias.ringwald 907381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 908381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 909381fbed8Smatthias.ringwald 910381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 911381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 912381fbed8Smatthias.ringwald return; 913381fbed8Smatthias.ringwald } 914381fbed8Smatthias.ringwald 915b031bebbSmatthias.ringwald // store request to send command but accept in higher layer view 916b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 2 | enable; // 1 = inq scan, 2 = page scan 917381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 918b031bebbSmatthias.ringwald 919b031bebbSmatthias.ringwald hci_run(); 920381fbed8Smatthias.ringwald } 921381fbed8Smatthias.ringwald 92206b35ec0Smatthias.ringwald void hci_run(){ 9238a485f27Smatthias.ringwald 92432ab9390Smatthias.ringwald hci_connection_t * connection; 92532ab9390Smatthias.ringwald linked_item_t * it; 92632ab9390Smatthias.ringwald 927ce4c8fabSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 928ce4c8fabSmatthias.ringwald 929b031bebbSmatthias.ringwald // global/non-connection oriented commands 930b031bebbSmatthias.ringwald 931b031bebbSmatthias.ringwald // decline incoming connections 932ce4c8fabSmatthias.ringwald if (hci_stack.decline_reason){ 933ce4c8fabSmatthias.ringwald uint8_t reason = hci_stack.decline_reason; 934ce4c8fabSmatthias.ringwald hci_stack.decline_reason = 0; 935ce4c8fabSmatthias.ringwald hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 936ce4c8fabSmatthias.ringwald } 937ce4c8fabSmatthias.ringwald 938b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 939b031bebbSmatthias.ringwald 940b031bebbSmatthias.ringwald // send scan enable 941eda18bf8Smatthias.ringwald if (hci_stack.new_scan_enable_value != 0xff){ 942b031bebbSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 943b031bebbSmatthias.ringwald hci_stack.new_scan_enable_value = 0xff; 944b031bebbSmatthias.ringwald } 945b031bebbSmatthias.ringwald 94632ab9390Smatthias.ringwald // send pending HCI commands 94732ab9390Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 94832ab9390Smatthias.ringwald 949b031bebbSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 95032ab9390Smatthias.ringwald 951b031bebbSmatthias.ringwald connection = (hci_connection_t *) it; 95232ab9390Smatthias.ringwald 95332ab9390Smatthias.ringwald if (connection->state == RECEIVED_CONNECTION_REQUEST){ 9547b5fbe1fSmatthias.ringwald log_info("sending hci_accept_connection_request\n"); 95532ab9390Smatthias.ringwald hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 95632ab9390Smatthias.ringwald connection->state = ACCEPTED_CONNECTION_REQUEST; 957c7e0c5f6Smatthias.ringwald } 958c7e0c5f6Smatthias.ringwald 959de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 96032ab9390Smatthias.ringwald 96132ab9390Smatthias.ringwald if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 96232ab9390Smatthias.ringwald link_key_t link_key; 9637b5fbe1fSmatthias.ringwald log_info("responding to link key request\n"); 96432ab9390Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 96532ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 96632ab9390Smatthias.ringwald } else { 96732ab9390Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 96832ab9390Smatthias.ringwald } 96932ab9390Smatthias.ringwald connection->authentication_flags &= ~HANDLE_LINK_KEY_REQUEST; 97032ab9390Smatthias.ringwald } 97132ab9390Smatthias.ringwald } 97232ab9390Smatthias.ringwald 973de009a8cSmatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 974c7e0c5f6Smatthias.ringwald 9753429f56bSmatthias.ringwald switch (hci_stack.state){ 9763429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 9777b5fbe1fSmatthias.ringwald // log_info("hci_init: substate %u\n", hci_stack.substate); 9783429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 9793429f56bSmatthias.ringwald // odd: waiting for command completion 98006b35ec0Smatthias.ringwald return; 9813429f56bSmatthias.ringwald } 98290919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 983c7492964Smatthias.ringwald case 0: // RESET 98422909952Smatthias.ringwald hci_send_cmd(&hci_reset); 985c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 986c7492964Smatthias.ringwald // skip baud change 987c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 988c7492964Smatthias.ringwald } 9893429f56bSmatthias.ringwald break; 990c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 9917dc17943Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 9927dc17943Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 993c7492964Smatthias.ringwald break; 994c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 995c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 996c7492964Smatthias.ringwald hci_stack.substate += 2; 997c7492964Smatthias.ringwald // break missing here for fall through 998c7492964Smatthias.ringwald 999c7492964Smatthias.ringwald case 3: 100090919203Smatthias.ringwald // custom initialization 1001db8946afSmatthias.ringwald if (hci_stack.control && hci_stack.control->next_cmd){ 10027dc17943Smatthias.ringwald int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1003d62aca9fSmatthias.ringwald if (valid_cmd){ 10047dc17943Smatthias.ringwald int size = 3 + hci_stack.hci_packet_buffer[2]; 10057dc17943Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1006c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 100790919203Smatthias.ringwald break; 100890919203Smatthias.ringwald } 1009d62aca9fSmatthias.ringwald log_info("hci_run: init script done\n\r"); 101090919203Smatthias.ringwald } 10112f6c30e1Smatthias.ringwald // otherwise continue 1012f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 1013f432a6ddSmatthias.ringwald break; 1014c7492964Smatthias.ringwald case 4: 1015e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 1016e2edc0c3Smatthias.ringwald break; 1017c7492964Smatthias.ringwald case 5: 10183429f56bSmatthias.ringwald // ca. 15 sec 10193429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 10203429f56bSmatthias.ringwald break; 1021c7492964Smatthias.ringwald case 6: 10220a90cc40Smatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan 1023f432a6ddSmatthias.ringwald break; 1024c7492964Smatthias.ringwald case 7: 10258a485f27Smatthias.ringwald #ifndef EMBEDDED 10268a485f27Smatthias.ringwald { 10278a485f27Smatthias.ringwald char hostname[30]; 10288a485f27Smatthias.ringwald gethostname(hostname, 30); 10298a485f27Smatthias.ringwald hostname[29] = '\0'; 10308a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 10318a485f27Smatthias.ringwald break; 10328a485f27Smatthias.ringwald } 1033c7492964Smatthias.ringwald case 8: 10343c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 10353c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 10363c4d4b90Smatthias.ringwald break; 10373c4d4b90Smatthias.ringwald 1038c7492964Smatthias.ringwald case 9: 10393c4d4b90Smatthias.ringwald #endif 10408a485f27Smatthias.ringwald #endif 10413429f56bSmatthias.ringwald // done. 10423429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 1043b360b6adSmatthias.ringwald hci_emit_state(); 10443429f56bSmatthias.ringwald break; 10453429f56bSmatthias.ringwald default: 10463429f56bSmatthias.ringwald break; 1047475c8125Smatthias.ringwald } 10483429f56bSmatthias.ringwald hci_stack.substate++; 10493429f56bSmatthias.ringwald break; 1050c7e0c5f6Smatthias.ringwald 1051c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 1052c7e0c5f6Smatthias.ringwald 10537b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING\n"); 1054c7e0c5f6Smatthias.ringwald // close all open connections 1055c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 1056c7e0c5f6Smatthias.ringwald if (connection){ 105732ab9390Smatthias.ringwald 1058c7e0c5f6Smatthias.ringwald // send disconnect 105932ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 106032ab9390Smatthias.ringwald 10617b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle); 10626ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1063c7e0c5f6Smatthias.ringwald 1064c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 1065c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 1066c7e0c5f6Smatthias.ringwald return; 1067c7e0c5f6Smatthias.ringwald } 10687b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling off\n"); 1069c7e0c5f6Smatthias.ringwald 107072ea5239Smatthias.ringwald // switch mode 1071c7e0c5f6Smatthias.ringwald hci_power_control_off(); 10729418f9c9Smatthias.ringwald 10737b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, emitting state\n"); 107472ea5239Smatthias.ringwald hci_emit_state(); 10757b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, done\n"); 107672ea5239Smatthias.ringwald break; 1077c7e0c5f6Smatthias.ringwald 107872ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 107989db417bSmatthias.ringwald switch(hci_stack.substate) { 108089db417bSmatthias.ringwald case 0: 10817b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP\n"); 108272ea5239Smatthias.ringwald // close all open connections 108372ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 108466da7044Smatthias.ringwald 108528171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 108666da7044Smatthias.ringwald // don't close connections, if H4 supports power management 108766da7044Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 108866da7044Smatthias.ringwald connection = NULL; 108966da7044Smatthias.ringwald } 109066da7044Smatthias.ringwald #endif 109172ea5239Smatthias.ringwald if (connection){ 109232ab9390Smatthias.ringwald 109372ea5239Smatthias.ringwald // send disconnect 109432ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 109532ab9390Smatthias.ringwald 10967b5fbe1fSmatthias.ringwald log_info("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, (uint16_t)connection->con_handle); 10976ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 109872ea5239Smatthias.ringwald 109972ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 110072ea5239Smatthias.ringwald hci_shutdown_connection(connection); 110172ea5239Smatthias.ringwald return; 110272ea5239Smatthias.ringwald } 110372ea5239Smatthias.ringwald 110489db417bSmatthias.ringwald // disable page and inquiry scan 110532ab9390Smatthias.ringwald if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 110632ab9390Smatthias.ringwald 11077b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, disabling inq & page scans\n"); 110889db417bSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 0); // none 110989db417bSmatthias.ringwald 111089db417bSmatthias.ringwald // continue in next sub state 111189db417bSmatthias.ringwald hci_stack.substate++; 111289db417bSmatthias.ringwald break; 111389db417bSmatthias.ringwald case 1: 111489db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 111589db417bSmatthias.ringwald break; 111689db417bSmatthias.ringwald case 2: 11177b5fbe1fSmatthias.ringwald log_info("HCI_STATE_HALTING, calling sleep\n"); 111828171530Smatthias.ringwald #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 111928171530Smatthias.ringwald // don't actually go to sleep, if H4 supports power management 112028171530Smatthias.ringwald if (bt_control_iphone_power_management_enabled()){ 112128171530Smatthias.ringwald // SLEEP MODE reached 112228171530Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 112328171530Smatthias.ringwald hci_emit_state(); 112428171530Smatthias.ringwald break; 112528171530Smatthias.ringwald } 112628171530Smatthias.ringwald #endif 112772ea5239Smatthias.ringwald // switch mode 112889db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1129c7e0c5f6Smatthias.ringwald hci_emit_state(); 113028171530Smatthias.ringwald break; 113128171530Smatthias.ringwald 113289db417bSmatthias.ringwald default: 113389db417bSmatthias.ringwald break; 113489db417bSmatthias.ringwald } 1135c7e0c5f6Smatthias.ringwald break; 1136c7e0c5f6Smatthias.ringwald 11373429f56bSmatthias.ringwald default: 11383429f56bSmatthias.ringwald break; 11391f504dbdSmatthias.ringwald } 11403429f56bSmatthias.ringwald } 114116833f0aSmatthias.ringwald 114231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 1143c8e4258aSmatthias.ringwald bd_addr_t addr; 1144c8e4258aSmatthias.ringwald hci_connection_t * conn; 1145c8e4258aSmatthias.ringwald // house-keeping 1146c8e4258aSmatthias.ringwald 1147c8e4258aSmatthias.ringwald // create_connection? 1148c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 1149c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 1150339b6768Smatthias.ringwald log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1151c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 1152c8e4258aSmatthias.ringwald if (conn) { 1153c8e4258aSmatthias.ringwald // if connection exists 1154c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 115517f1ba2aSmatthias.ringwald // and OPEN, emit connection complete command 115617f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, 0); 1157c8e4258aSmatthias.ringwald } 115817f1ba2aSmatthias.ringwald // otherwise, just ignore as it is already in the open process 1159c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 1160c8e4258aSmatthias.ringwald 116117f1ba2aSmatthias.ringwald } 1162c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 116317f1ba2aSmatthias.ringwald conn = create_connection_for_addr(addr); 116417f1ba2aSmatthias.ringwald if (!conn){ 116517f1ba2aSmatthias.ringwald // notify client that alloc failed 116617f1ba2aSmatthias.ringwald hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 116717f1ba2aSmatthias.ringwald return 0; // don't sent packet to controller 116817f1ba2aSmatthias.ringwald } 1169c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 1170c8e4258aSmatthias.ringwald } 1171c8e4258aSmatthias.ringwald 11727fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 11737fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 11747fde4af9Smatthias.ringwald } 11757fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 11767fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 11777fde4af9Smatthias.ringwald } 11787fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 11797fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 11807fde4af9Smatthias.ringwald } 11817fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 11827fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 11837fde4af9Smatthias.ringwald } 11847fde4af9Smatthias.ringwald 11858ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 11868ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 11878ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 11888ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 11898ef73945Smatthias.ringwald } 11908ef73945Smatthias.ringwald } 1191c8e4258aSmatthias.ringwald 119231452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1193622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 119431452debSmatthias.ringwald } 11958adf0ddaSmatthias.ringwald 11961cd208adSmatthias.ringwald /** 11971cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 11981cd208adSmatthias.ringwald */ 1199fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 12001cd208adSmatthias.ringwald va_list argptr; 12011cd208adSmatthias.ringwald va_start(argptr, cmd); 12027dc17943Smatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 12031cd208adSmatthias.ringwald va_end(argptr); 12047dc17943Smatthias.ringwald return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 120593b8dc03Smatthias.ringwald } 1206c8e4258aSmatthias.ringwald 1207ee091cf1Smatthias.ringwald // Create various non-HCI events. 1208ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1209ee091cf1Smatthias.ringwald 1210c8e4258aSmatthias.ringwald void hci_emit_state(){ 1211425d1371Smatthias.ringwald uint8_t event[3]; 121280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 1213425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1214c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1215425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1216425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1217c8e4258aSmatthias.ringwald } 1218c8e4258aSmatthias.ringwald 121917f1ba2aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1220425d1371Smatthias.ringwald uint8_t event[13]; 1221c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1222425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 122317f1ba2aSmatthias.ringwald event[2] = status; 1224c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1225c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1226c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1227c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1228425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1229425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1230c8e4258aSmatthias.ringwald } 1231c8e4258aSmatthias.ringwald 12323c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1233425d1371Smatthias.ringwald uint8_t event[6]; 12343c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1235e518c4b8Smatthias.ringwald event[1] = sizeof(event) - 2; 12363c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 12373c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 12383c4d4b90Smatthias.ringwald event[5] = reason; 1239425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1240425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 12413c4d4b90Smatthias.ringwald } 12423c4d4b90Smatthias.ringwald 1243ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1244425d1371Smatthias.ringwald uint8_t event[4]; 124580d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1246425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1247ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1248425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1249425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1250ee091cf1Smatthias.ringwald } 125143bfb1bdSmatthias.ringwald 125243bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 1253425d1371Smatthias.ringwald uint8_t event[3]; 125480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1255425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 125643bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 1257425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1258425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 125943bfb1bdSmatthias.ringwald } 1260038bc64cSmatthias.ringwald 1261038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 1262425d1371Smatthias.ringwald uint8_t event[2]; 126380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 1264425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1265425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1266425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1267038bc64cSmatthias.ringwald } 12681b0e3922Smatthias.ringwald 126909ba8edeSmatthias.ringwald #ifndef EMBEDDED 12701b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 1271425d1371Smatthias.ringwald uint8_t event[6]; 12721b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 1273425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1274425d1371Smatthias.ringwald event[2] = BTSTACK_MAJOR; 1275425d1371Smatthias.ringwald event[3] = BTSTACK_MINOR; 1276425d1371Smatthias.ringwald bt_store_16(event, 4, BTSTACK_REVISION); 1277425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1278425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 12791b0e3922Smatthias.ringwald } 128009ba8edeSmatthias.ringwald #endif 12811b0e3922Smatthias.ringwald 12822ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1283425d1371Smatthias.ringwald uint8_t event[3]; 12842ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1285425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 12862ed6235cSmatthias.ringwald event[2] = enabled; 1287425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1288e518c4b8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 12892ed6235cSmatthias.ringwald } 1290627c2f45Smatthias.ringwald 1291627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1292425d1371Smatthias.ringwald uint8_t event[2+1+6+248]; 1293627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1294425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1295f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 12962f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1297f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1298425d1371Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1299425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1300627c2f45Smatthias.ringwald } 1301381fbed8Smatthias.ringwald 1302381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1303425d1371Smatthias.ringwald uint8_t event[3]; 1304381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1305425d1371Smatthias.ringwald event[1] = sizeof(event) - 2; 1306381fbed8Smatthias.ringwald event[2] = enabled; 1307425d1371Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1308425d1371Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1309381fbed8Smatthias.ringwald } 1310