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 397f2435e6Smatthias.ringwald #include "hci.h" 407f2435e6Smatthias.ringwald 4193b8dc03Smatthias.ringwald #include <stdarg.h> 4293b8dc03Smatthias.ringwald #include <string.h> 4356fe0872Smatthias.ringwald #include <stdio.h> 447f2435e6Smatthias.ringwald 457f2435e6Smatthias.ringwald #include "debug.h" 46d8905019Smatthias.ringwald #include "hci_dump.h" 4793b8dc03Smatthias.ringwald 482ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h" 492ed6235cSmatthias.ringwald #include "../include/btstack/version.h" 501b0e3922Smatthias.ringwald 511e6aba47Smatthias.ringwald // temp 521e6aba47Smatthias.ringwald #include "l2cap.h" 531e6aba47Smatthias.ringwald 54169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 55ee091cf1Smatthias.ringwald 5606b35ec0Smatthias.ringwald // the STACK is here 5716833f0aSmatthias.ringwald static hci_stack_t hci_stack; 5816833f0aSmatthias.ringwald 5997addcc5Smatthias.ringwald /** 60ee091cf1Smatthias.ringwald * get connection for a given handle 61ee091cf1Smatthias.ringwald * 62ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 63ee091cf1Smatthias.ringwald */ 64645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 65ee091cf1Smatthias.ringwald linked_item_t *it; 66ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 67ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 68ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 69ee091cf1Smatthias.ringwald } 70ee091cf1Smatthias.ringwald } 71ee091cf1Smatthias.ringwald return NULL; 72ee091cf1Smatthias.ringwald } 73ee091cf1Smatthias.ringwald 74981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 75*c7492964Smatthias.ringwald #ifdef HAVE_TIME 76ee091cf1Smatthias.ringwald hci_connection_t * connection = linked_item_get_user(&timer->item); 77ee091cf1Smatthias.ringwald struct timeval tv; 78ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 79c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 80ee091cf1Smatthias.ringwald // connections might be timed out 81ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 82c21e6239Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 83ee091cf1Smatthias.ringwald } else { 84ee091cf1Smatthias.ringwald // next timeout check at 85c21e6239Smatthias.ringwald timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000; 86ee091cf1Smatthias.ringwald } 87ee091cf1Smatthias.ringwald run_loop_add_timer(timer); 88*c7492964Smatthias.ringwald #endif 89ee091cf1Smatthias.ringwald } 90ee091cf1Smatthias.ringwald 91ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 92*c7492964Smatthias.ringwald #ifdef HAVE_TIME 93ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 94*c7492964Smatthias.ringwald #endif 95ee091cf1Smatthias.ringwald } 96ee091cf1Smatthias.ringwald 97ee091cf1Smatthias.ringwald /** 98c8e4258aSmatthias.ringwald * create connection for given address 99c8e4258aSmatthias.ringwald * 100c8e4258aSmatthias.ringwald * @return connection OR NULL, if not found 101c8e4258aSmatthias.ringwald */ 102c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 103c8e4258aSmatthias.ringwald hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 104c8e4258aSmatthias.ringwald if (!conn) return NULL; 105c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 106c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 1077fde4af9Smatthias.ringwald conn->authentication_flags = 0; 108*c7492964Smatthias.ringwald #ifdef HAVE_TIME 109ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 110ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 111ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 112*c7492964Smatthias.ringwald #endif 113d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1147856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 11556cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 116c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 117c8e4258aSmatthias.ringwald return conn; 118c8e4258aSmatthias.ringwald } 119c8e4258aSmatthias.ringwald 120c8e4258aSmatthias.ringwald /** 12106b35ec0Smatthias.ringwald * get connection for given address 12297addcc5Smatthias.ringwald * 12397addcc5Smatthias.ringwald * @return connection OR NULL, if not found 12497addcc5Smatthias.ringwald */ 125fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 12606b35ec0Smatthias.ringwald linked_item_t *it; 12706b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 12806b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 12906b35ec0Smatthias.ringwald return (hci_connection_t *) it; 13006b35ec0Smatthias.ringwald } 13106b35ec0Smatthias.ringwald } 13206b35ec0Smatthias.ringwald return NULL; 13306b35ec0Smatthias.ringwald } 13406b35ec0Smatthias.ringwald 13543bfb1bdSmatthias.ringwald /** 13680ca58a0Smatthias.ringwald * add authentication flags and reset timer 1377fde4af9Smatthias.ringwald */ 1387fde4af9Smatthias.ringwald static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 1397fde4af9Smatthias.ringwald bd_addr_t addr; 1407fde4af9Smatthias.ringwald bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 1417fde4af9Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 1427fde4af9Smatthias.ringwald if (conn) { 1437fde4af9Smatthias.ringwald conn->authentication_flags |= flags; 14480ca58a0Smatthias.ringwald hci_connection_timestamp(conn); 1457fde4af9Smatthias.ringwald } 1467fde4af9Smatthias.ringwald } 1477fde4af9Smatthias.ringwald 14880ca58a0Smatthias.ringwald int hci_authentication_active_for_handle(hci_con_handle_t handle){ 14980ca58a0Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 15080ca58a0Smatthias.ringwald if (!conn) return 0; 15180ca58a0Smatthias.ringwald if (!conn->authentication_flags) return 0; 15280ca58a0Smatthias.ringwald if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 15380ca58a0Smatthias.ringwald if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 15480ca58a0Smatthias.ringwald return 1; 15580ca58a0Smatthias.ringwald } 15680ca58a0Smatthias.ringwald 157c12e46e7Smatthias.ringwald void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 158c12e46e7Smatthias.ringwald if (hci_stack.remote_device_db) { 159c12e46e7Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(addr); 160c12e46e7Smatthias.ringwald } 161c12e46e7Smatthias.ringwald } 162c12e46e7Smatthias.ringwald 1637fde4af9Smatthias.ringwald 1647fde4af9Smatthias.ringwald /** 16543bfb1bdSmatthias.ringwald * count connections 16643bfb1bdSmatthias.ringwald */ 16743bfb1bdSmatthias.ringwald static int nr_hci_connections(){ 16856c253c9Smatthias.ringwald int count = 0; 16943bfb1bdSmatthias.ringwald linked_item_t *it; 17043bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 17143bfb1bdSmatthias.ringwald return count; 17243bfb1bdSmatthias.ringwald } 173c8e4258aSmatthias.ringwald 17497addcc5Smatthias.ringwald /** 175ba681a6cSmatthias.ringwald * Dummy handler called by HCI 17616833f0aSmatthias.ringwald */ 1772718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 17816833f0aSmatthias.ringwald } 17916833f0aSmatthias.ringwald 180998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 181998906cdSmatthias.ringwald hci_connection_t * connection = connection_for_handle(handle); 182998906cdSmatthias.ringwald if (!connection) { 183998906cdSmatthias.ringwald log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 184998906cdSmatthias.ringwald return 0; 185998906cdSmatthias.ringwald } 186998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 187998906cdSmatthias.ringwald } 188998906cdSmatthias.ringwald 189998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 190998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 191998906cdSmatthias.ringwald linked_item_t *it; 192998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 193998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 194998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 195998906cdSmatthias.ringwald log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 196998906cdSmatthias.ringwald return 0; 197998906cdSmatthias.ringwald } 198998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 199998906cdSmatthias.ringwald } 200998906cdSmatthias.ringwald return free_slots; 201998906cdSmatthias.ringwald } 202998906cdSmatthias.ringwald 2035250fb9eSmatthias.ringwald uint16_t hci_max_acl_data_packet_length(){ 2045250fb9eSmatthias.ringwald return hci_stack.acl_data_packet_length; 2055250fb9eSmatthias.ringwald } 2065250fb9eSmatthias.ringwald 207998906cdSmatthias.ringwald int hci_ready_to_send(hci_con_handle_t handle){ 208998906cdSmatthias.ringwald return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2; 209998906cdSmatthias.ringwald } 210c8e4258aSmatthias.ringwald 211ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 2127856c818Smatthias.ringwald 2136218e6f1Smatthias.ringwald // check for free places on BT module 2146218e6f1Smatthias.ringwald if (!hci_number_free_acl_slots()) return -1; 2156218e6f1Smatthias.ringwald 2167856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2177856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 21856cf178bSmatthias.ringwald if (!connection) return 0; 21956cf178bSmatthias.ringwald hci_connection_timestamp(connection); 22056cf178bSmatthias.ringwald 22156cf178bSmatthias.ringwald // count packet 22256cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 2238ea03fa5Smatthias.ringwald // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2247856c818Smatthias.ringwald 2256218e6f1Smatthias.ringwald // send packet - ignore errors 226622d0de9Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 2276218e6f1Smatthias.ringwald 2286218e6f1Smatthias.ringwald return 0; 229ee091cf1Smatthias.ringwald } 230ee091cf1Smatthias.ringwald 23116833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2327856c818Smatthias.ringwald 2337856c818Smatthias.ringwald // get info 2347856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2357856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 2367856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2377856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2387856c818Smatthias.ringwald 2397856c818Smatthias.ringwald // ignore non-registered handle 2407856c818Smatthias.ringwald if (!conn){ 2417f2435e6Smatthias.ringwald log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2427856c818Smatthias.ringwald return; 2437856c818Smatthias.ringwald } 2447856c818Smatthias.ringwald 2457856c818Smatthias.ringwald // update idle timestamp 2467856c818Smatthias.ringwald hci_connection_timestamp(conn); 2477856c818Smatthias.ringwald 2487856c818Smatthias.ringwald // handle different packet types 2497856c818Smatthias.ringwald switch (acl_flags & 0x03) { 2507856c818Smatthias.ringwald 2517856c818Smatthias.ringwald case 0x01: // continuation fragment 2527856c818Smatthias.ringwald 2537856c818Smatthias.ringwald // sanity check 2547856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 2557f2435e6Smatthias.ringwald log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 2567856c818Smatthias.ringwald return; 2577856c818Smatthias.ringwald } 2587856c818Smatthias.ringwald 2597856c818Smatthias.ringwald // append fragment payload (header already stored) 2607856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 2617856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 2627856c818Smatthias.ringwald 2637f2435e6Smatthias.ringwald // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", 2647856c818Smatthias.ringwald // acl_length, connection->acl_recombination_pos, connection->acl_recombination_length); 2657856c818Smatthias.ringwald 2667856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 2677856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 2687856c818Smatthias.ringwald 2692718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 2707856c818Smatthias.ringwald // reset recombination buffer 2717856c818Smatthias.ringwald conn->acl_recombination_length = 0; 2727856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 2737856c818Smatthias.ringwald } 2747856c818Smatthias.ringwald break; 2757856c818Smatthias.ringwald 2767856c818Smatthias.ringwald case 0x02: { // first fragment 2777856c818Smatthias.ringwald 2787856c818Smatthias.ringwald // sanity check 2797856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 2807f2435e6Smatthias.ringwald log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 2817856c818Smatthias.ringwald return; 2827856c818Smatthias.ringwald } 2837856c818Smatthias.ringwald 2847856c818Smatthias.ringwald // peek into L2CAP packet! 2857856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 2867856c818Smatthias.ringwald 2877856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 2887856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 2897856c818Smatthias.ringwald 2907856c818Smatthias.ringwald // forward fragment as L2CAP packet 2912718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 2927856c818Smatthias.ringwald 2937856c818Smatthias.ringwald } else { 2947856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 2957856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 2967856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 2977856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 2987856c818Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4); 2997f2435e6Smatthias.ringwald // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 3007856c818Smatthias.ringwald } 3017856c818Smatthias.ringwald break; 3027856c818Smatthias.ringwald 3037856c818Smatthias.ringwald } 3047856c818Smatthias.ringwald default: 3057f2435e6Smatthias.ringwald log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 3067856c818Smatthias.ringwald return; 3077856c818Smatthias.ringwald } 30894ab26f8Smatthias.ringwald 30994ab26f8Smatthias.ringwald // execute main loop 31094ab26f8Smatthias.ringwald hci_run(); 31116833f0aSmatthias.ringwald } 31222909952Smatthias.ringwald 31367a3e8ecSmatthias.ringwald static void hci_shutdown_connection(hci_connection_t *conn){ 314c7e0c5f6Smatthias.ringwald log_dbg("Connection closed: handle %u, ", conn->con_handle); 315c7e0c5f6Smatthias.ringwald print_bd_addr( conn->address ); 316c7e0c5f6Smatthias.ringwald log_dbg("\n"); 3173c4d4b90Smatthias.ringwald 3183c4d4b90Smatthias.ringwald // cancel all l2cap connections 3193c4d4b90Smatthias.ringwald hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 3203c4d4b90Smatthias.ringwald 321*c7492964Smatthias.ringwald #ifdef HAVE_TIME 322c7e0c5f6Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 323*c7492964Smatthias.ringwald #endif 324c7e0c5f6Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 325c7e0c5f6Smatthias.ringwald free( conn ); 3263c4d4b90Smatthias.ringwald 3273c4d4b90Smatthias.ringwald // now it's gone 328c7e0c5f6Smatthias.ringwald hci_emit_nr_connections_changed(); 329c7e0c5f6Smatthias.ringwald } 330c7e0c5f6Smatthias.ringwald 33174ec757aSmatthias.ringwald // avoid huge local variables 33274ec757aSmatthias.ringwald static device_name_t device_name; 33316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 3341281a47eSmatthias.ringwald bd_addr_t addr; 335fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 3361f7b95a1Smatthias.ringwald hci_connection_t * conn; 33756cf178bSmatthias.ringwald int i; 33829d53098Smatthias.ringwald link_key_t link_key; 33922909952Smatthias.ringwald 3406772a24cSmatthias.ringwald switch (packet[0]) { 34122909952Smatthias.ringwald 3426772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 3437ec5eeaaSmatthias.ringwald // get num cmd packets 3447ec5eeaaSmatthias.ringwald // log_dbg("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 3457ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 3467ec5eeaaSmatthias.ringwald 347e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 348e2edc0c3Smatthias.ringwald // from offset 5 349e2edc0c3Smatthias.ringwald // status 350e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 35156cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 352e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 35356cf178bSmatthias.ringwald // ignore: total num SCO packets 35456cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 355e2edc0c3Smatthias.ringwald log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets); 356e2edc0c3Smatthias.ringwald } 35756cf178bSmatthias.ringwald } 358381fbed8Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 359381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 360381fbed8Smatthias.ringwald } 36156cf178bSmatthias.ringwald break; 36256cf178bSmatthias.ringwald 3637ec5eeaaSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 3647ec5eeaaSmatthias.ringwald // get num cmd packets 3657ec5eeaaSmatthias.ringwald // log_dbg("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 3667ec5eeaaSmatthias.ringwald hci_stack.num_cmd_packets = packet[3]; 3677ec5eeaaSmatthias.ringwald break; 3687ec5eeaaSmatthias.ringwald 36956cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 37056cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 37156cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 37256cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 37356cf178bSmatthias.ringwald conn = connection_for_handle(handle); 37456cf178bSmatthias.ringwald if (!conn){ 37556cf178bSmatthias.ringwald log_err("hci_number_completed_packet lists unused con handle %u\n", handle); 37656cf178bSmatthias.ringwald continue; 37756cf178bSmatthias.ringwald } 37856cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 3798ea03fa5Smatthias.ringwald // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 38056cf178bSmatthias.ringwald } 3816772a24cSmatthias.ringwald break; 3826772a24cSmatthias.ringwald 3831f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 38437eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 38537eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 38637eaa4cfSmatthias.ringwald uint8_t link_type = packet[11]; 387cf0b66f0Smatthias.ringwald log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type); 38837eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 3891f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 3901f7b95a1Smatthias.ringwald if (!conn) { 3911f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 3921f7b95a1Smatthias.ringwald } 3931f7b95a1Smatthias.ringwald // TODO: check for malloc failure 3941f7b95a1Smatthias.ringwald conn->state = ACCEPTED_CONNECTION_REQUEST; 39537eaa4cfSmatthias.ringwald hci_send_cmd(&hci_accept_connection_request, addr, 1); 39637eaa4cfSmatthias.ringwald } else { 39737eaa4cfSmatthias.ringwald // TODO: decline request 39837eaa4cfSmatthias.ringwald } 3991f7b95a1Smatthias.ringwald break; 4001f7b95a1Smatthias.ringwald 4016772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 402fe1ed1b8Smatthias.ringwald // Connection management 403fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 404cf0b66f0Smatthias.ringwald log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n"); 4051f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 406fe1ed1b8Smatthias.ringwald if (conn) { 407b448a0e7Smatthias.ringwald if (!packet[2]){ 408c8e4258aSmatthias.ringwald conn->state = OPEN; 409fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 410ee091cf1Smatthias.ringwald 411*c7492964Smatthias.ringwald #ifdef HAVE_TIME 412ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 413c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 414ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 415*c7492964Smatthias.ringwald #endif 416cf0b66f0Smatthias.ringwald log_dbg("New connection: handle %u, ", conn->con_handle); 417fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 418cf0b66f0Smatthias.ringwald log_dbg("\n"); 41943bfb1bdSmatthias.ringwald 42043bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 421b448a0e7Smatthias.ringwald } else { 422b448a0e7Smatthias.ringwald // connection failed, remove entry 423b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 424b448a0e7Smatthias.ringwald free( conn ); 425c12e46e7Smatthias.ringwald 426c12e46e7Smatthias.ringwald // if authentication error, also delete link key 427c12e46e7Smatthias.ringwald if (packet[2] == 0x05) { 428c12e46e7Smatthias.ringwald hci_drop_link_key_for_bd_addr(&addr); 429c12e46e7Smatthias.ringwald } 430fe1ed1b8Smatthias.ringwald } 431fe1ed1b8Smatthias.ringwald } 4326772a24cSmatthias.ringwald break; 433fe1ed1b8Smatthias.ringwald 4347fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_REQUEST: 4357fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 43674ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 43729d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 43829d53098Smatthias.ringwald if ( hci_stack.remote_device_db->get_link_key( &addr, &link_key)){ 43929d53098Smatthias.ringwald hci_send_cmd(&hci_link_key_request_reply, &addr, &link_key); 44029d53098Smatthias.ringwald } else { 44129d53098Smatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 44229d53098Smatthias.ringwald } 44329d53098Smatthias.ringwald // request already answered 44429d53098Smatthias.ringwald return; 4457fde4af9Smatthias.ringwald 4467fde4af9Smatthias.ringwald case HCI_EVENT_LINK_KEY_NOTIFICATION: 4477fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 44874ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 44929d53098Smatthias.ringwald bt_flip_addr(addr, &packet[2]); 450287d19b8Smatthias.ringwald hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 45129d53098Smatthias.ringwald // still forward event to allow dismiss of pairing dialog 4527fde4af9Smatthias.ringwald break; 4537fde4af9Smatthias.ringwald 4547fde4af9Smatthias.ringwald case HCI_EVENT_PIN_CODE_REQUEST: 4557fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 4567fde4af9Smatthias.ringwald break; 4577fde4af9Smatthias.ringwald 45874ec757aSmatthias.ringwald case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 45974ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 46074ec757aSmatthias.ringwald if (packet[2]) break; // status not ok 46174ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 46274ec757aSmatthias.ringwald bzero(&device_name, sizeof(device_name_t)); 46374ec757aSmatthias.ringwald strncpy((char*) device_name, (char*) &packet[9], 248); 46474ec757aSmatthias.ringwald hci_stack.remote_device_db->put_name(&addr, &device_name); 46574ec757aSmatthias.ringwald break; 46674ec757aSmatthias.ringwald 46774ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT: 46874ec757aSmatthias.ringwald case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 46974ec757aSmatthias.ringwald if (!hci_stack.remote_device_db) break; 47074ec757aSmatthias.ringwald // first send inq result packet 47174ec757aSmatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 47274ec757aSmatthias.ringwald // then send cached remote names 47374ec757aSmatthias.ringwald for (i=0; i<packet[2];i++){ 47474ec757aSmatthias.ringwald bt_flip_addr(addr, &packet[3+i*6]); 47574ec757aSmatthias.ringwald if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 47674ec757aSmatthias.ringwald hci_emit_remote_name_cached(&addr, &device_name); 47774ec757aSmatthias.ringwald } 47874ec757aSmatthias.ringwald } 47974ec757aSmatthias.ringwald return; 48074ec757aSmatthias.ringwald 4816772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 482fe1ed1b8Smatthias.ringwald if (!packet[2]){ 483fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 484fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 485fe1ed1b8Smatthias.ringwald if (conn) { 486c7e0c5f6Smatthias.ringwald hci_shutdown_connection(conn); 487fe1ed1b8Smatthias.ringwald } 488fe1ed1b8Smatthias.ringwald } 4896772a24cSmatthias.ringwald break; 4906772a24cSmatthias.ringwald 4916772a24cSmatthias.ringwald default: 4926772a24cSmatthias.ringwald break; 493fe1ed1b8Smatthias.ringwald } 494fe1ed1b8Smatthias.ringwald 4953429f56bSmatthias.ringwald // handle BT initialization 4963429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 4977301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 4987301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 4997301ad89Smatthias.ringwald // hci_stack.substate = 0; 5007301ad89Smatthias.ringwald // } 5017301ad89Smatthias.ringwald // handle normal init sequence 5023429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 5033429f56bSmatthias.ringwald // odd: waiting for event 5043429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 5053429f56bSmatthias.ringwald hci_stack.substate++; 5063429f56bSmatthias.ringwald } 5073429f56bSmatthias.ringwald } 50822909952Smatthias.ringwald } 50922909952Smatthias.ringwald 51089db417bSmatthias.ringwald // help with BT sleep 51189db417bSmatthias.ringwald if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 51289db417bSmatthias.ringwald && hci_stack.substate == 1 51389db417bSmatthias.ringwald && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 51489db417bSmatthias.ringwald hci_stack.substate++; 51589db417bSmatthias.ringwald } 51689db417bSmatthias.ringwald 5172718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 51894ab26f8Smatthias.ringwald 51994ab26f8Smatthias.ringwald // execute main loop 52094ab26f8Smatthias.ringwald hci_run(); 52116833f0aSmatthias.ringwald } 52216833f0aSmatthias.ringwald 52310e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 52410e830c9Smatthias.ringwald switch (packet_type) { 52510e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 52610e830c9Smatthias.ringwald event_handler(packet, size); 52710e830c9Smatthias.ringwald break; 52810e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 52910e830c9Smatthias.ringwald acl_handler(packet, size); 53010e830c9Smatthias.ringwald break; 53110e830c9Smatthias.ringwald default: 53210e830c9Smatthias.ringwald break; 53310e830c9Smatthias.ringwald } 53410e830c9Smatthias.ringwald } 53510e830c9Smatthias.ringwald 536fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 5372718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 5382718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 53916833f0aSmatthias.ringwald } 54016833f0aSmatthias.ringwald 541404843c1Smatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t * remote_device_db){ 542475c8125Smatthias.ringwald 543475c8125Smatthias.ringwald // reference to use transport layer implementation 54416833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 545475c8125Smatthias.ringwald 54611e23e5fSmatthias.ringwald // references to used control implementation 54711e23e5fSmatthias.ringwald hci_stack.control = control; 54811e23e5fSmatthias.ringwald 54911e23e5fSmatthias.ringwald // reference to used config 55011e23e5fSmatthias.ringwald hci_stack.config = config; 55111e23e5fSmatthias.ringwald 552fe1ed1b8Smatthias.ringwald // no connections yet 553fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 5540a90cc40Smatthias.ringwald hci_stack.discoverable = 0; 555fe1ed1b8Smatthias.ringwald 55602ea9861Smatthias.ringwald // empty cmd buffer 55716833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 55816833f0aSmatthias.ringwald 55916833f0aSmatthias.ringwald // higher level handler 5602718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 56116833f0aSmatthias.ringwald 562404843c1Smatthias.ringwald // store and open remote device db 563404843c1Smatthias.ringwald hci_stack.remote_device_db = remote_device_db; 564404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 565404843c1Smatthias.ringwald hci_stack.remote_device_db->open(); 566404843c1Smatthias.ringwald } 56729d53098Smatthias.ringwald 56816833f0aSmatthias.ringwald // register packet handlers with transport 56910e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 570475c8125Smatthias.ringwald } 571475c8125Smatthias.ringwald 572404843c1Smatthias.ringwald void hci_close(){ 573404843c1Smatthias.ringwald // close remote device db 574404843c1Smatthias.ringwald if (hci_stack.remote_device_db) { 575404843c1Smatthias.ringwald hci_stack.remote_device_db->close(); 576404843c1Smatthias.ringwald } 577404843c1Smatthias.ringwald } 578404843c1Smatthias.ringwald 5798d213e1aSmatthias.ringwald // State-Module-Driver overview 5808d213e1aSmatthias.ringwald // state module low-level 5818d213e1aSmatthias.ringwald // HCI_STATE_OFF off close 5828d213e1aSmatthias.ringwald // HCI_STATE_INITIALIZING, on open 5838d213e1aSmatthias.ringwald // HCI_STATE_WORKING, on open 5848d213e1aSmatthias.ringwald // HCI_STATE_HALTING, on open 585d661ed19Smatthias.ringwald // HCI_STATE_SLEEPING, off/sleep close 586d661ed19Smatthias.ringwald // HCI_STATE_FALLING_ASLEEP on open 587c7e0c5f6Smatthias.ringwald 5888d213e1aSmatthias.ringwald static int hci_power_control_on(){ 5897301ad89Smatthias.ringwald 590038bc64cSmatthias.ringwald // power on 591f9a30166Smatthias.ringwald int err = 0; 592f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->on){ 593f9a30166Smatthias.ringwald err = (*hci_stack.control->on)(hci_stack.config); 594f9a30166Smatthias.ringwald } 595038bc64cSmatthias.ringwald if (err){ 5967f2435e6Smatthias.ringwald log_err( "POWER_ON failed\n"); 597038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 598038bc64cSmatthias.ringwald return err; 599038bc64cSmatthias.ringwald } 600038bc64cSmatthias.ringwald 601038bc64cSmatthias.ringwald // open low-level device 602038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 603038bc64cSmatthias.ringwald if (err){ 6047f2435e6Smatthias.ringwald log_err( "HCI_INIT failed, turning Bluetooth off again\n"); 605f9a30166Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 606f9a30166Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 607f9a30166Smatthias.ringwald } 608038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 609038bc64cSmatthias.ringwald return err; 610038bc64cSmatthias.ringwald } 6118d213e1aSmatthias.ringwald return 0; 6128d213e1aSmatthias.ringwald } 613038bc64cSmatthias.ringwald 6148d213e1aSmatthias.ringwald static void hci_power_control_off(){ 6158d213e1aSmatthias.ringwald 6169418f9c9Smatthias.ringwald log_dbg("hci_power_control_off\n"); 6179418f9c9Smatthias.ringwald 6188d213e1aSmatthias.ringwald // close low-level device 6198d213e1aSmatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 6208d213e1aSmatthias.ringwald 6219418f9c9Smatthias.ringwald log_dbg("hci_power_control_off - hci_transport closed\n"); 6229418f9c9Smatthias.ringwald 6238d213e1aSmatthias.ringwald // power off 6248d213e1aSmatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 6258d213e1aSmatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 6268d213e1aSmatthias.ringwald } 6279418f9c9Smatthias.ringwald 6289418f9c9Smatthias.ringwald log_dbg("hci_power_control_off - control closed\n"); 6299418f9c9Smatthias.ringwald 63072ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_OFF; 63172ea5239Smatthias.ringwald } 63272ea5239Smatthias.ringwald 63372ea5239Smatthias.ringwald static void hci_power_control_sleep(){ 63472ea5239Smatthias.ringwald 6359418f9c9Smatthias.ringwald log_dbg("hci_power_control_sleep\n"); 6363144bce4Smatthias.ringwald 637b429b9b7Smatthias.ringwald #if 0 638b429b9b7Smatthias.ringwald // don't close serial port during sleep 639b429b9b7Smatthias.ringwald 64072ea5239Smatthias.ringwald // close low-level device 64172ea5239Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 642b429b9b7Smatthias.ringwald #endif 64372ea5239Smatthias.ringwald 64472ea5239Smatthias.ringwald // sleep mode 6453144bce4Smatthias.ringwald if (hci_stack.control && hci_stack.control->sleep){ 64672ea5239Smatthias.ringwald (*hci_stack.control->sleep)(hci_stack.config); 64772ea5239Smatthias.ringwald } 648b429b9b7Smatthias.ringwald 64972ea5239Smatthias.ringwald hci_stack.state = HCI_STATE_SLEEPING; 6508d213e1aSmatthias.ringwald } 6518d213e1aSmatthias.ringwald 652b429b9b7Smatthias.ringwald static int hci_power_control_wake(){ 653b429b9b7Smatthias.ringwald 6549418f9c9Smatthias.ringwald log_dbg("hci_power_control_wake\n"); 655b429b9b7Smatthias.ringwald 656b429b9b7Smatthias.ringwald // wake on 657b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->wake){ 658b429b9b7Smatthias.ringwald (*hci_stack.control->wake)(hci_stack.config); 659b429b9b7Smatthias.ringwald } 660b429b9b7Smatthias.ringwald 661b429b9b7Smatthias.ringwald #if 0 662b429b9b7Smatthias.ringwald // open low-level device 663b429b9b7Smatthias.ringwald int err = hci_stack.hci_transport->open(hci_stack.config); 664b429b9b7Smatthias.ringwald if (err){ 665b429b9b7Smatthias.ringwald log_err( "HCI_INIT failed, turning Bluetooth off again\n"); 666b429b9b7Smatthias.ringwald if (hci_stack.control && hci_stack.control->off){ 667b429b9b7Smatthias.ringwald (*hci_stack.control->off)(hci_stack.config); 668b429b9b7Smatthias.ringwald } 669b429b9b7Smatthias.ringwald hci_emit_hci_open_failed(); 670b429b9b7Smatthias.ringwald return err; 671b429b9b7Smatthias.ringwald } 672b429b9b7Smatthias.ringwald #endif 673b429b9b7Smatthias.ringwald 674b429b9b7Smatthias.ringwald return 0; 675b429b9b7Smatthias.ringwald } 676b429b9b7Smatthias.ringwald 677b429b9b7Smatthias.ringwald 6788d213e1aSmatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 679d661ed19Smatthias.ringwald 6807ec5eeaaSmatthias.ringwald log_dbg("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 681d661ed19Smatthias.ringwald 6828d213e1aSmatthias.ringwald int err = 0; 6838d213e1aSmatthias.ringwald switch (hci_stack.state){ 6848d213e1aSmatthias.ringwald 6858d213e1aSmatthias.ringwald case HCI_STATE_OFF: 6868d213e1aSmatthias.ringwald switch (power_mode){ 6878d213e1aSmatthias.ringwald case HCI_POWER_ON: 6888d213e1aSmatthias.ringwald err = hci_power_control_on(); 6898d213e1aSmatthias.ringwald if (err) return err; 6907301ad89Smatthias.ringwald // set up state machine 6917301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 6927301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 6937301ad89Smatthias.ringwald hci_stack.substate = 0; 6948d213e1aSmatthias.ringwald break; 6958d213e1aSmatthias.ringwald case HCI_POWER_OFF: 6968d213e1aSmatthias.ringwald // do nothing 6978d213e1aSmatthias.ringwald break; 6988d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 699b546ac54Smatthias.ringwald // do nothing (with SLEEP == OFF) 7008d213e1aSmatthias.ringwald break; 7018d213e1aSmatthias.ringwald } 7028d213e1aSmatthias.ringwald break; 7037301ad89Smatthias.ringwald 7048d213e1aSmatthias.ringwald case HCI_STATE_INITIALIZING: 7058d213e1aSmatthias.ringwald switch (power_mode){ 7068d213e1aSmatthias.ringwald case HCI_POWER_ON: 7078d213e1aSmatthias.ringwald // do nothing 7088d213e1aSmatthias.ringwald break; 7098d213e1aSmatthias.ringwald case HCI_POWER_OFF: 7108d213e1aSmatthias.ringwald // no connections yet, just turn it off 7118d213e1aSmatthias.ringwald hci_power_control_off(); 7128d213e1aSmatthias.ringwald break; 7138d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 714b546ac54Smatthias.ringwald // no connections yet, just turn it off 71572ea5239Smatthias.ringwald hci_power_control_sleep(); 7168d213e1aSmatthias.ringwald break; 7178d213e1aSmatthias.ringwald } 7188d213e1aSmatthias.ringwald break; 7197301ad89Smatthias.ringwald 7208d213e1aSmatthias.ringwald case HCI_STATE_WORKING: 7218d213e1aSmatthias.ringwald switch (power_mode){ 7228d213e1aSmatthias.ringwald case HCI_POWER_ON: 7238d213e1aSmatthias.ringwald // do nothing 7248d213e1aSmatthias.ringwald break; 7258d213e1aSmatthias.ringwald case HCI_POWER_OFF: 726c7e0c5f6Smatthias.ringwald // see hci_run 727c7e0c5f6Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 7288d213e1aSmatthias.ringwald break; 7298d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 730b546ac54Smatthias.ringwald // see hci_run 731b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 73289db417bSmatthias.ringwald hci_stack.substate = 0; 7338d213e1aSmatthias.ringwald break; 7348d213e1aSmatthias.ringwald } 7358d213e1aSmatthias.ringwald break; 7367301ad89Smatthias.ringwald 7378d213e1aSmatthias.ringwald case HCI_STATE_HALTING: 7388d213e1aSmatthias.ringwald switch (power_mode){ 7398d213e1aSmatthias.ringwald case HCI_POWER_ON: 7408d213e1aSmatthias.ringwald // set up state machine 7418d213e1aSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 7428d213e1aSmatthias.ringwald hci_stack.substate = 0; 7438d213e1aSmatthias.ringwald break; 7448d213e1aSmatthias.ringwald case HCI_POWER_OFF: 7458d213e1aSmatthias.ringwald // do nothing 7468d213e1aSmatthias.ringwald break; 7478d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 748b546ac54Smatthias.ringwald // see hci_run 749b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_FALLING_ASLEEP; 75089db417bSmatthias.ringwald hci_stack.substate = 0; 7518d213e1aSmatthias.ringwald break; 7528d213e1aSmatthias.ringwald } 7538d213e1aSmatthias.ringwald break; 7548d213e1aSmatthias.ringwald 7558d213e1aSmatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 7568d213e1aSmatthias.ringwald switch (power_mode){ 7578d213e1aSmatthias.ringwald case HCI_POWER_ON: 758b546ac54Smatthias.ringwald // set up state machine 759b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 760b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 761b546ac54Smatthias.ringwald hci_stack.substate = 0; 7628d213e1aSmatthias.ringwald break; 7638d213e1aSmatthias.ringwald case HCI_POWER_OFF: 764b546ac54Smatthias.ringwald // see hci_run 765b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 7668d213e1aSmatthias.ringwald break; 7678d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 768b546ac54Smatthias.ringwald // do nothing 7698d213e1aSmatthias.ringwald break; 7708d213e1aSmatthias.ringwald } 7718d213e1aSmatthias.ringwald break; 7728d213e1aSmatthias.ringwald 7738d213e1aSmatthias.ringwald case HCI_STATE_SLEEPING: 7748d213e1aSmatthias.ringwald switch (power_mode){ 7758d213e1aSmatthias.ringwald case HCI_POWER_ON: 7763144bce4Smatthias.ringwald err = hci_power_control_wake(); 7773144bce4Smatthias.ringwald if (err) return err; 778b546ac54Smatthias.ringwald // set up state machine 779b546ac54Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 780b546ac54Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 781b546ac54Smatthias.ringwald hci_stack.substate = 0; 7828d213e1aSmatthias.ringwald break; 7838d213e1aSmatthias.ringwald case HCI_POWER_OFF: 7844ddb5bedSmatthias.ringwald hci_stack.state = HCI_STATE_HALTING; 7858d213e1aSmatthias.ringwald break; 7868d213e1aSmatthias.ringwald case HCI_POWER_SLEEP: 787b546ac54Smatthias.ringwald // do nothing 7888d213e1aSmatthias.ringwald break; 7898d213e1aSmatthias.ringwald } 7908d213e1aSmatthias.ringwald break; 79111e23e5fSmatthias.ringwald } 79268d92d03Smatthias.ringwald 793038bc64cSmatthias.ringwald // create internal event 794ee8bf225Smatthias.ringwald hci_emit_state(); 795ee8bf225Smatthias.ringwald 79668d92d03Smatthias.ringwald // trigger next/first action 79768d92d03Smatthias.ringwald hci_run(); 79868d92d03Smatthias.ringwald 799475c8125Smatthias.ringwald return 0; 800475c8125Smatthias.ringwald } 801475c8125Smatthias.ringwald 802381fbed8Smatthias.ringwald void hci_discoverable_control(uint8_t enable){ 803381fbed8Smatthias.ringwald if (enable) enable = 1; // normalize argument 804381fbed8Smatthias.ringwald 805381fbed8Smatthias.ringwald if (hci_stack.discoverable == enable){ 806381fbed8Smatthias.ringwald hci_emit_discoverable_enabled(hci_stack.discoverable); 807381fbed8Smatthias.ringwald return; 808381fbed8Smatthias.ringwald } 809381fbed8Smatthias.ringwald 810381fbed8Smatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 2 | enable); // 1 = inq scan, 2 = page scan 811381fbed8Smatthias.ringwald hci_stack.discoverable = enable; 812381fbed8Smatthias.ringwald } 813381fbed8Smatthias.ringwald 81406b35ec0Smatthias.ringwald void hci_run(){ 8158a485f27Smatthias.ringwald 816c7e0c5f6Smatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 817c7e0c5f6Smatthias.ringwald // cannot send command yet 818c7e0c5f6Smatthias.ringwald return; 819c7e0c5f6Smatthias.ringwald } 820c7e0c5f6Smatthias.ringwald 821c7e0c5f6Smatthias.ringwald hci_connection_t * connection; 822c7e0c5f6Smatthias.ringwald 8233429f56bSmatthias.ringwald switch (hci_stack.state){ 8243429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 8253429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 8263429f56bSmatthias.ringwald // odd: waiting for command completion 82706b35ec0Smatthias.ringwald return; 8283429f56bSmatthias.ringwald } 82990919203Smatthias.ringwald switch (hci_stack.substate >> 1){ 830*c7492964Smatthias.ringwald case 0: // RESET 83122909952Smatthias.ringwald hci_send_cmd(&hci_reset); 832*c7492964Smatthias.ringwald if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 833*c7492964Smatthias.ringwald // skip baud change 834*c7492964Smatthias.ringwald hci_stack.substate = 4; // >> 1 = 2 835*c7492964Smatthias.ringwald } 8363429f56bSmatthias.ringwald break; 837*c7492964Smatthias.ringwald case 1: // SEND BAUD CHANGE 838*c7492964Smatthias.ringwald hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_cmd_buffer); 839*c7492964Smatthias.ringwald hci_send_cmd_packet(hci_stack.hci_cmd_buffer, 3 + hci_stack.hci_cmd_buffer[2]); 840*c7492964Smatthias.ringwald break; 841*c7492964Smatthias.ringwald case 2: // LOCAL BAUD CHANGE 842*c7492964Smatthias.ringwald hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 843*c7492964Smatthias.ringwald hci_stack.substate += 2; 844*c7492964Smatthias.ringwald // break missing here for fall through 845*c7492964Smatthias.ringwald 846*c7492964Smatthias.ringwald case 3: 84790919203Smatthias.ringwald // custom initialization 84890919203Smatthias.ringwald if (hci_stack.control && hci_stack.control->next_command){ 84990919203Smatthias.ringwald uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config); 85090919203Smatthias.ringwald if (cmd) { 85190919203Smatthias.ringwald int size = 3 + cmd[2]; 852622d0de9Smatthias.ringwald hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size); 853*c7492964Smatthias.ringwald hci_stack.substate = 4; // more init commands 85490919203Smatthias.ringwald break; 85590919203Smatthias.ringwald } 856*c7492964Smatthias.ringwald printf("hci_run: init script done\n\r"); 85790919203Smatthias.ringwald } 8582f6c30e1Smatthias.ringwald // otherwise continue 859f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 860f432a6ddSmatthias.ringwald break; 861*c7492964Smatthias.ringwald case 4: 862e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 863e2edc0c3Smatthias.ringwald break; 864*c7492964Smatthias.ringwald case 5: 8653429f56bSmatthias.ringwald // ca. 15 sec 8663429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 8673429f56bSmatthias.ringwald break; 868*c7492964Smatthias.ringwald case 6: 8690a90cc40Smatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 2 | hci_stack.discoverable); // page scan 870f432a6ddSmatthias.ringwald break; 871*c7492964Smatthias.ringwald case 7: 8728a485f27Smatthias.ringwald #ifndef EMBEDDED 8738a485f27Smatthias.ringwald { 8748a485f27Smatthias.ringwald char hostname[30]; 8758a485f27Smatthias.ringwald gethostname(hostname, 30); 8768a485f27Smatthias.ringwald hostname[29] = '\0'; 8778a485f27Smatthias.ringwald hci_send_cmd(&hci_write_local_name, hostname); 8788a485f27Smatthias.ringwald break; 8798a485f27Smatthias.ringwald } 880*c7492964Smatthias.ringwald case 8: 8813c4d4b90Smatthias.ringwald #ifdef USE_BLUETOOL 8823c4d4b90Smatthias.ringwald hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 8833c4d4b90Smatthias.ringwald break; 8843c4d4b90Smatthias.ringwald 885*c7492964Smatthias.ringwald case 9: 8863c4d4b90Smatthias.ringwald #endif 8878a485f27Smatthias.ringwald #endif 8883429f56bSmatthias.ringwald // done. 8893429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 890b360b6adSmatthias.ringwald hci_emit_state(); 8913429f56bSmatthias.ringwald break; 8923429f56bSmatthias.ringwald default: 8933429f56bSmatthias.ringwald break; 894475c8125Smatthias.ringwald } 8953429f56bSmatthias.ringwald hci_stack.substate++; 8963429f56bSmatthias.ringwald break; 897c7e0c5f6Smatthias.ringwald 898c7e0c5f6Smatthias.ringwald case HCI_STATE_HALTING: 899c7e0c5f6Smatthias.ringwald 9003144bce4Smatthias.ringwald log_dbg("HCI_STATE_HALTING\n"); 901c7e0c5f6Smatthias.ringwald // close all open connections 902c7e0c5f6Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 903c7e0c5f6Smatthias.ringwald if (connection){ 904*c7492964Smatthias.ringwald log_dbg("HCI_STATE_HALTING, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle); 905c7e0c5f6Smatthias.ringwald // send disconnect 9066ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 907c7e0c5f6Smatthias.ringwald 908c7e0c5f6Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 909c7e0c5f6Smatthias.ringwald hci_shutdown_connection(connection); 910c7e0c5f6Smatthias.ringwald return; 911c7e0c5f6Smatthias.ringwald } 9123144bce4Smatthias.ringwald log_dbg("HCI_STATE_HALTING, calling off\n"); 913c7e0c5f6Smatthias.ringwald 91472ea5239Smatthias.ringwald // switch mode 915c7e0c5f6Smatthias.ringwald hci_power_control_off(); 9169418f9c9Smatthias.ringwald 9179418f9c9Smatthias.ringwald log_dbg("HCI_STATE_HALTING, emitting state\n"); 91872ea5239Smatthias.ringwald hci_emit_state(); 9193144bce4Smatthias.ringwald log_dbg("HCI_STATE_HALTING, done\n"); 92072ea5239Smatthias.ringwald break; 921c7e0c5f6Smatthias.ringwald 92272ea5239Smatthias.ringwald case HCI_STATE_FALLING_ASLEEP: 92389db417bSmatthias.ringwald switch(hci_stack.substate) { 92489db417bSmatthias.ringwald case 0: 92589db417bSmatthias.ringwald log_dbg("HCI_STATE_FALLING_ASLEEP\n"); 92672ea5239Smatthias.ringwald // close all open connections 92772ea5239Smatthias.ringwald connection = (hci_connection_t *) hci_stack.connections; 92872ea5239Smatthias.ringwald if (connection){ 929*c7492964Smatthias.ringwald log_dbg("HCI_STATE_FALLING_ASLEEP, connection %lu, handle %u\n", (uintptr_t) connection, connection->con_handle); 93072ea5239Smatthias.ringwald // send disconnect 9316ad890d3Smatthias.ringwald hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 93272ea5239Smatthias.ringwald 93372ea5239Smatthias.ringwald // send disconnected event right away - causes higher layer connections to get closed, too. 93472ea5239Smatthias.ringwald hci_shutdown_connection(connection); 93572ea5239Smatthias.ringwald return; 93672ea5239Smatthias.ringwald } 93772ea5239Smatthias.ringwald 93889db417bSmatthias.ringwald log_dbg("HCI_STATE_HALTING, disabling inq & page scans\n"); 93989db417bSmatthias.ringwald 94089db417bSmatthias.ringwald // disable page and inquiry scan 94189db417bSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 0); // none 94289db417bSmatthias.ringwald 94389db417bSmatthias.ringwald // continue in next sub state 94489db417bSmatthias.ringwald hci_stack.substate++; 94589db417bSmatthias.ringwald break; 94689db417bSmatthias.ringwald case 1: 94789db417bSmatthias.ringwald // wait for command complete "hci_write_scan_enable" in event_handler(); 94889db417bSmatthias.ringwald break; 94989db417bSmatthias.ringwald case 2: 95089db417bSmatthias.ringwald log_dbg("HCI_STATE_HALTING, calling sleep\n"); 95172ea5239Smatthias.ringwald // switch mode 95289db417bSmatthias.ringwald hci_power_control_sleep(); // changes hci_stack.state to SLEEP 953c7e0c5f6Smatthias.ringwald hci_emit_state(); 95489db417bSmatthias.ringwald default: 95589db417bSmatthias.ringwald break; 95689db417bSmatthias.ringwald } 957c7e0c5f6Smatthias.ringwald break; 958c7e0c5f6Smatthias.ringwald 9593429f56bSmatthias.ringwald default: 9603429f56bSmatthias.ringwald break; 9611f504dbdSmatthias.ringwald } 9623429f56bSmatthias.ringwald } 96316833f0aSmatthias.ringwald 96431452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 965c8e4258aSmatthias.ringwald bd_addr_t addr; 966c8e4258aSmatthias.ringwald hci_connection_t * conn; 967c8e4258aSmatthias.ringwald // house-keeping 968c8e4258aSmatthias.ringwald 969c8e4258aSmatthias.ringwald // create_connection? 970c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 971c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 972cf0b66f0Smatthias.ringwald log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n"); 973c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 974c8e4258aSmatthias.ringwald if (conn) { 975c8e4258aSmatthias.ringwald // if connection exists 976c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 977c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 978c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 979c8e4258aSmatthias.ringwald } 980c8e4258aSmatthias.ringwald // otherwise, just ignore 981c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 982c8e4258aSmatthias.ringwald 983c8e4258aSmatthias.ringwald } else{ 984c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 985c8e4258aSmatthias.ringwald if (conn){ 986c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 987c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 988c8e4258aSmatthias.ringwald } 989c8e4258aSmatthias.ringwald } 990c8e4258aSmatthias.ringwald } 991c8e4258aSmatthias.ringwald 9927fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_reply)){ 9937fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 9947fde4af9Smatthias.ringwald } 9957fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 9967fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 9977fde4af9Smatthias.ringwald } 9987fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 9997fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 10007fde4af9Smatthias.ringwald } 10017fde4af9Smatthias.ringwald if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 10027fde4af9Smatthias.ringwald hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 10037fde4af9Smatthias.ringwald } 10047fde4af9Smatthias.ringwald 10058ef73945Smatthias.ringwald if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 10068ef73945Smatthias.ringwald if (hci_stack.remote_device_db){ 10078ef73945Smatthias.ringwald bt_flip_addr(addr, &packet[3]); 10088ef73945Smatthias.ringwald hci_stack.remote_device_db->delete_link_key(&addr); 10098ef73945Smatthias.ringwald } 10108ef73945Smatthias.ringwald } 1011c8e4258aSmatthias.ringwald 101231452debSmatthias.ringwald hci_stack.num_cmd_packets--; 1013622d0de9Smatthias.ringwald return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 101431452debSmatthias.ringwald } 10158adf0ddaSmatthias.ringwald 10161cd208adSmatthias.ringwald /** 10171cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 10181cd208adSmatthias.ringwald */ 1019fe35119dSmatthias.ringwald int hci_send_cmd(const hci_cmd_t *cmd, ...){ 10201cd208adSmatthias.ringwald va_list argptr; 10211cd208adSmatthias.ringwald va_start(argptr, cmd); 10221cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 10231cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 10241cd208adSmatthias.ringwald va_end(argptr); 10251cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 102693b8dc03Smatthias.ringwald } 1027c8e4258aSmatthias.ringwald 1028ee091cf1Smatthias.ringwald // Create various non-HCI events. 1029ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 1030ee091cf1Smatthias.ringwald 1031c8e4258aSmatthias.ringwald void hci_emit_state(){ 1032c8e4258aSmatthias.ringwald uint8_t len = 3; 1033c8e4258aSmatthias.ringwald uint8_t event[len]; 103480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 10351b0e3922Smatthias.ringwald event[1] = len - 3; 1036c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 1037c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 10382718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 1039c8e4258aSmatthias.ringwald } 1040c8e4258aSmatthias.ringwald 1041c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 1042c8e4258aSmatthias.ringwald uint8_t len = 13; 1043c8e4258aSmatthias.ringwald uint8_t event[len]; 1044c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 10451b0e3922Smatthias.ringwald event[1] = len - 3; 1046c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 1047c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 1048c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 1049c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 1050c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 1051c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 10522718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 1053c8e4258aSmatthias.ringwald } 1054c8e4258aSmatthias.ringwald 10553c4d4b90Smatthias.ringwald void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 10563c4d4b90Smatthias.ringwald uint8_t len = 6; 10573c4d4b90Smatthias.ringwald uint8_t event[len]; 10583c4d4b90Smatthias.ringwald event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 10593c4d4b90Smatthias.ringwald event[1] = len - 3; 10603c4d4b90Smatthias.ringwald event[2] = 0; // status = OK 10613c4d4b90Smatthias.ringwald bt_store_16(event, 3, handle); 10623c4d4b90Smatthias.ringwald event[5] = reason; 10633c4d4b90Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 10643c4d4b90Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 10653c4d4b90Smatthias.ringwald } 10663c4d4b90Smatthias.ringwald 1067ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1068ee091cf1Smatthias.ringwald uint8_t len = 4; 1069ee091cf1Smatthias.ringwald uint8_t event[len]; 107080d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 10711b0e3922Smatthias.ringwald event[1] = len - 2; 1072ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 1073ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 10742718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 1075ee091cf1Smatthias.ringwald } 107643bfb1bdSmatthias.ringwald 107743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 107843bfb1bdSmatthias.ringwald uint8_t len = 3; 107943bfb1bdSmatthias.ringwald uint8_t event[len]; 108080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 10811b0e3922Smatthias.ringwald event[1] = len - 2; 108243bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 108343bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 10842718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 108543bfb1bdSmatthias.ringwald } 1086038bc64cSmatthias.ringwald 1087038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 10881b0e3922Smatthias.ringwald uint8_t len = 2; 1089038bc64cSmatthias.ringwald uint8_t event[len]; 109080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 10911b0e3922Smatthias.ringwald event[1] = len - 2; 1092038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 10932718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 1094038bc64cSmatthias.ringwald } 10951b0e3922Smatthias.ringwald 10961b0e3922Smatthias.ringwald 10971b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 10981b0e3922Smatthias.ringwald uint8_t len = 6; 10991b0e3922Smatthias.ringwald uint8_t event[len]; 11001b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 11011b0e3922Smatthias.ringwald event[1] = len - 2; 11021b0e3922Smatthias.ringwald event[len++] = BTSTACK_MAJOR; 11031b0e3922Smatthias.ringwald event[len++] = BTSTACK_MINOR; 11041b0e3922Smatthias.ringwald bt_store_16(event, len, BTSTACK_REVISION); 11051b0e3922Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 11062718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 11071b0e3922Smatthias.ringwald } 11081b0e3922Smatthias.ringwald 11092ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 11102ed6235cSmatthias.ringwald uint8_t len = 3; 11112ed6235cSmatthias.ringwald uint8_t event[len]; 11122ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1113ddce1d8dSmatthias.ringwald event[1] = len - 2; 11142ed6235cSmatthias.ringwald event[2] = enabled; 11152ed6235cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 11162718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 11172ed6235cSmatthias.ringwald } 1118627c2f45Smatthias.ringwald 1119627c2f45Smatthias.ringwald void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1120f653b6bdSmatthias.ringwald uint16_t len = 2+1+6+248; 1121627c2f45Smatthias.ringwald uint8_t event[len]; 1122627c2f45Smatthias.ringwald event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1123627c2f45Smatthias.ringwald event[1] = len - 2; 1124f653b6bdSmatthias.ringwald event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 11252f89d309Smatthias.ringwald bt_flip_addr(&event[3], *addr); 1126f653b6bdSmatthias.ringwald memcpy(&event[9], name, 248); 1127627c2f45Smatthias.ringwald hci_dump_packet(HCI_EVENT_PACKET, 0, event, len); 1128627c2f45Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 1129627c2f45Smatthias.ringwald } 1130381fbed8Smatthias.ringwald 1131381fbed8Smatthias.ringwald void hci_emit_discoverable_enabled(uint8_t enabled){ 1132381fbed8Smatthias.ringwald uint8_t len = 3; 1133381fbed8Smatthias.ringwald uint8_t event[len]; 1134381fbed8Smatthias.ringwald event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1135381fbed8Smatthias.ringwald event[1] = len - 2; 1136381fbed8Smatthias.ringwald event[2] = enabled; 1137381fbed8Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 1138381fbed8Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 1139381fbed8Smatthias.ringwald } 1140