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 41475c8125Smatthias.ringwald #include <unistd.h> 4293b8dc03Smatthias.ringwald #include <stdarg.h> 4393b8dc03Smatthias.ringwald #include <string.h> 4456fe0872Smatthias.ringwald #include <stdio.h> 457f2435e6Smatthias.ringwald 467f2435e6Smatthias.ringwald #include "debug.h" 47d8905019Smatthias.ringwald #include "hci_dump.h" 4893b8dc03Smatthias.ringwald 492ed6235cSmatthias.ringwald #include "../include/btstack/hci_cmds.h" 502ed6235cSmatthias.ringwald #include "../include/btstack/version.h" 511b0e3922Smatthias.ringwald 521e6aba47Smatthias.ringwald // temp 531e6aba47Smatthias.ringwald #include "l2cap.h" 541e6aba47Smatthias.ringwald 55169f8b28Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 10000 56ee091cf1Smatthias.ringwald 5706b35ec0Smatthias.ringwald // the STACK is here 5816833f0aSmatthias.ringwald static hci_stack_t hci_stack; 5916833f0aSmatthias.ringwald 6097addcc5Smatthias.ringwald /** 61ee091cf1Smatthias.ringwald * get connection for a given handle 62ee091cf1Smatthias.ringwald * 63ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 64ee091cf1Smatthias.ringwald */ 65645658c9Smatthias.ringwald hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 66ee091cf1Smatthias.ringwald linked_item_t *it; 67ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 68ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 69ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 70ee091cf1Smatthias.ringwald } 71ee091cf1Smatthias.ringwald } 72ee091cf1Smatthias.ringwald return NULL; 73ee091cf1Smatthias.ringwald } 74ee091cf1Smatthias.ringwald 75981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 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); 88ee091cf1Smatthias.ringwald } 89ee091cf1Smatthias.ringwald 90ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 91ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 92ee091cf1Smatthias.ringwald } 93ee091cf1Smatthias.ringwald 94ee091cf1Smatthias.ringwald /** 95c8e4258aSmatthias.ringwald * create connection for given address 96c8e4258aSmatthias.ringwald * 97c8e4258aSmatthias.ringwald * @return connection OR NULL, if not found 98c8e4258aSmatthias.ringwald */ 99c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 100c8e4258aSmatthias.ringwald hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 101c8e4258aSmatthias.ringwald if (!conn) return NULL; 102c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 103c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 104c8e4258aSmatthias.ringwald conn->flags = 0; 105ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 106ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 107ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 108d55db49eSmatthias.ringwald conn->acl_recombination_length = 0; 1097856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 11056cf178bSmatthias.ringwald conn->num_acl_packets_sent = 0; 111c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 112c8e4258aSmatthias.ringwald return conn; 113c8e4258aSmatthias.ringwald } 114c8e4258aSmatthias.ringwald 115c8e4258aSmatthias.ringwald /** 11606b35ec0Smatthias.ringwald * get connection for given address 11797addcc5Smatthias.ringwald * 11897addcc5Smatthias.ringwald * @return connection OR NULL, if not found 11997addcc5Smatthias.ringwald */ 120fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 12106b35ec0Smatthias.ringwald linked_item_t *it; 12206b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 12306b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 12406b35ec0Smatthias.ringwald return (hci_connection_t *) it; 12506b35ec0Smatthias.ringwald } 12606b35ec0Smatthias.ringwald } 12706b35ec0Smatthias.ringwald return NULL; 12806b35ec0Smatthias.ringwald } 12906b35ec0Smatthias.ringwald 13043bfb1bdSmatthias.ringwald /** 13143bfb1bdSmatthias.ringwald * count connections 13243bfb1bdSmatthias.ringwald */ 13343bfb1bdSmatthias.ringwald static int nr_hci_connections(){ 13456c253c9Smatthias.ringwald int count = 0; 13543bfb1bdSmatthias.ringwald linked_item_t *it; 13643bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 13743bfb1bdSmatthias.ringwald return count; 13843bfb1bdSmatthias.ringwald } 139c8e4258aSmatthias.ringwald 14097addcc5Smatthias.ringwald /** 141ba681a6cSmatthias.ringwald * Dummy handler called by HCI 14216833f0aSmatthias.ringwald */ 1432718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14416833f0aSmatthias.ringwald } 14516833f0aSmatthias.ringwald 146ba681a6cSmatthias.ringwald /** 147ba681a6cSmatthias.ringwald * Dummy control handler 148ba681a6cSmatthias.ringwald */ 149ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 150ba681a6cSmatthias.ringwald return 0; 151ba681a6cSmatthias.ringwald } 152ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 153ba681a6cSmatthias.ringwald return "Hardware unknown"; 154ba681a6cSmatthias.ringwald } 155ba681a6cSmatthias.ringwald static bt_control_t null_control = { 156ba681a6cSmatthias.ringwald null_control_function, 157ba681a6cSmatthias.ringwald null_control_function, 158ba681a6cSmatthias.ringwald null_control_function, 159ba681a6cSmatthias.ringwald null_control_name 160ba681a6cSmatthias.ringwald }; 161ba681a6cSmatthias.ringwald 162*998906cdSmatthias.ringwald uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 163*998906cdSmatthias.ringwald hci_connection_t * connection = connection_for_handle(handle); 164*998906cdSmatthias.ringwald if (!connection) { 165*998906cdSmatthias.ringwald log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 166*998906cdSmatthias.ringwald return 0; 167*998906cdSmatthias.ringwald } 168*998906cdSmatthias.ringwald return connection->num_acl_packets_sent; 169*998906cdSmatthias.ringwald } 170*998906cdSmatthias.ringwald 171*998906cdSmatthias.ringwald uint8_t hci_number_free_acl_slots(){ 172*998906cdSmatthias.ringwald uint8_t free_slots = hci_stack.total_num_acl_packets; 173*998906cdSmatthias.ringwald linked_item_t *it; 174*998906cdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 175*998906cdSmatthias.ringwald hci_connection_t * connection = (hci_connection_t *) it; 176*998906cdSmatthias.ringwald if (free_slots < connection->num_acl_packets_sent) { 177*998906cdSmatthias.ringwald log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 178*998906cdSmatthias.ringwald return 0; 179*998906cdSmatthias.ringwald } 180*998906cdSmatthias.ringwald free_slots -= connection->num_acl_packets_sent; 181*998906cdSmatthias.ringwald } 182*998906cdSmatthias.ringwald return free_slots; 183*998906cdSmatthias.ringwald } 184*998906cdSmatthias.ringwald 185*998906cdSmatthias.ringwald int hci_ready_to_send(hci_con_handle_t handle){ 186*998906cdSmatthias.ringwald return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2; 187*998906cdSmatthias.ringwald } 188c8e4258aSmatthias.ringwald 189ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 1907856c818Smatthias.ringwald 1917856c818Smatthias.ringwald // update idle timestamp 1927856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 1937856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 19456cf178bSmatthias.ringwald if (!connection) return 0; 19556cf178bSmatthias.ringwald hci_connection_timestamp(connection); 19656cf178bSmatthias.ringwald 19756cf178bSmatthias.ringwald // count packet 19856cf178bSmatthias.ringwald connection->num_acl_packets_sent++; 199*998906cdSmatthias.ringwald log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 2007856c818Smatthias.ringwald 2017856c818Smatthias.ringwald // send packet 202ee091cf1Smatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 203ee091cf1Smatthias.ringwald } 204ee091cf1Smatthias.ringwald 20516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 2067856c818Smatthias.ringwald 2077856c818Smatthias.ringwald // get info 2087856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 2097856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 2107856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 2117856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 2127856c818Smatthias.ringwald 2137856c818Smatthias.ringwald // ignore non-registered handle 2147856c818Smatthias.ringwald if (!conn){ 2157f2435e6Smatthias.ringwald log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 2167856c818Smatthias.ringwald return; 2177856c818Smatthias.ringwald } 2187856c818Smatthias.ringwald 2197856c818Smatthias.ringwald // update idle timestamp 2207856c818Smatthias.ringwald hci_connection_timestamp(conn); 2217856c818Smatthias.ringwald 2227856c818Smatthias.ringwald // handle different packet types 2237856c818Smatthias.ringwald switch (acl_flags & 0x03) { 2247856c818Smatthias.ringwald 2257856c818Smatthias.ringwald case 0x01: // continuation fragment 2267856c818Smatthias.ringwald 2277856c818Smatthias.ringwald // sanity check 2287856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 2297f2435e6Smatthias.ringwald log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 2307856c818Smatthias.ringwald return; 2317856c818Smatthias.ringwald } 2327856c818Smatthias.ringwald 2337856c818Smatthias.ringwald // append fragment payload (header already stored) 2347856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 2357856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 2367856c818Smatthias.ringwald 2377f2435e6Smatthias.ringwald // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", 2387856c818Smatthias.ringwald // acl_length, connection->acl_recombination_pos, connection->acl_recombination_length); 2397856c818Smatthias.ringwald 2407856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 2417856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 2427856c818Smatthias.ringwald 2432718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 2447856c818Smatthias.ringwald // reset recombination buffer 2457856c818Smatthias.ringwald conn->acl_recombination_length = 0; 2467856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 2477856c818Smatthias.ringwald } 2487856c818Smatthias.ringwald break; 2497856c818Smatthias.ringwald 2507856c818Smatthias.ringwald case 0x02: { // first fragment 2517856c818Smatthias.ringwald 2527856c818Smatthias.ringwald // sanity check 2537856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 2547f2435e6Smatthias.ringwald log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 2557856c818Smatthias.ringwald return; 2567856c818Smatthias.ringwald } 2577856c818Smatthias.ringwald 2587856c818Smatthias.ringwald // peek into L2CAP packet! 2597856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 2607856c818Smatthias.ringwald 2617856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 2627856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 2637856c818Smatthias.ringwald 2647856c818Smatthias.ringwald // forward fragment as L2CAP packet 2652718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 2667856c818Smatthias.ringwald 2677856c818Smatthias.ringwald } else { 2687856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 2697856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 2707856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 2717856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 2727856c818Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4); 2737f2435e6Smatthias.ringwald // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 2747856c818Smatthias.ringwald } 2757856c818Smatthias.ringwald break; 2767856c818Smatthias.ringwald 2777856c818Smatthias.ringwald } 2787856c818Smatthias.ringwald default: 2797f2435e6Smatthias.ringwald log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 2807856c818Smatthias.ringwald return; 2817856c818Smatthias.ringwald } 28294ab26f8Smatthias.ringwald 28394ab26f8Smatthias.ringwald // execute main loop 28494ab26f8Smatthias.ringwald hci_run(); 28516833f0aSmatthias.ringwald } 28622909952Smatthias.ringwald 28716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 2881281a47eSmatthias.ringwald bd_addr_t addr; 289fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 2901f7b95a1Smatthias.ringwald hci_connection_t * conn; 29156cf178bSmatthias.ringwald int i; 29222909952Smatthias.ringwald 293e2edc0c3Smatthias.ringwald // get num_cmd_packets 294e2edc0c3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 295e2edc0c3Smatthias.ringwald // Get Num_HCI_Command_Packets 296e2edc0c3Smatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 297e2edc0c3Smatthias.ringwald } 298e2edc0c3Smatthias.ringwald 2996772a24cSmatthias.ringwald switch (packet[0]) { 30022909952Smatthias.ringwald 3016772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 302e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 303e2edc0c3Smatthias.ringwald // from offset 5 304e2edc0c3Smatthias.ringwald // status 305e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 30656cf178bSmatthias.ringwald // ignore: SCO data packet len (8) 307e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 30856cf178bSmatthias.ringwald // ignore: total num SCO packets 30956cf178bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 310e2edc0c3Smatthias.ringwald log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets); 311e2edc0c3Smatthias.ringwald } 31256cf178bSmatthias.ringwald } 31356cf178bSmatthias.ringwald break; 31456cf178bSmatthias.ringwald 31556cf178bSmatthias.ringwald case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 31656cf178bSmatthias.ringwald for (i=0; i<packet[2];i++){ 31756cf178bSmatthias.ringwald handle = READ_BT_16(packet, 3 + 2*i); 31856cf178bSmatthias.ringwald uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 31956cf178bSmatthias.ringwald conn = connection_for_handle(handle); 32056cf178bSmatthias.ringwald if (!conn){ 32156cf178bSmatthias.ringwald log_err("hci_number_completed_packet lists unused con handle %u\n", handle); 32256cf178bSmatthias.ringwald continue; 32356cf178bSmatthias.ringwald } 32456cf178bSmatthias.ringwald conn->num_acl_packets_sent -= num_packets; 325*998906cdSmatthias.ringwald log_dbg("hci_number_completed_packet (hci) %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 32656cf178bSmatthias.ringwald } 3276772a24cSmatthias.ringwald break; 3286772a24cSmatthias.ringwald 3291f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 33037eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 33137eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 33237eaa4cfSmatthias.ringwald uint8_t link_type = packet[11]; 333cf0b66f0Smatthias.ringwald log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type); 33437eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 3351f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 3361f7b95a1Smatthias.ringwald if (!conn) { 3371f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 3381f7b95a1Smatthias.ringwald } 3391f7b95a1Smatthias.ringwald // TODO: check for malloc failure 3401f7b95a1Smatthias.ringwald conn->state = ACCEPTED_CONNECTION_REQUEST; 34137eaa4cfSmatthias.ringwald hci_send_cmd(&hci_accept_connection_request, addr, 1); 34237eaa4cfSmatthias.ringwald } else { 34337eaa4cfSmatthias.ringwald // TODO: decline request 34437eaa4cfSmatthias.ringwald } 3451f7b95a1Smatthias.ringwald break; 3461f7b95a1Smatthias.ringwald 3476772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 348fe1ed1b8Smatthias.ringwald // Connection management 349fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 350cf0b66f0Smatthias.ringwald log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n"); 3511f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 352fe1ed1b8Smatthias.ringwald if (conn) { 353b448a0e7Smatthias.ringwald if (!packet[2]){ 354c8e4258aSmatthias.ringwald conn->state = OPEN; 355fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 356fe1ed1b8Smatthias.ringwald conn->flags = 0; 357ee091cf1Smatthias.ringwald 358ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 359c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 360ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 361ee091cf1Smatthias.ringwald 362cf0b66f0Smatthias.ringwald log_dbg("New connection: handle %u, ", conn->con_handle); 363fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 364cf0b66f0Smatthias.ringwald log_dbg("\n"); 36543bfb1bdSmatthias.ringwald 36643bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 367b448a0e7Smatthias.ringwald } else { 368b448a0e7Smatthias.ringwald // connection failed, remove entry 369b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 370b448a0e7Smatthias.ringwald free( conn ); 371fe1ed1b8Smatthias.ringwald } 372fe1ed1b8Smatthias.ringwald } 3736772a24cSmatthias.ringwald break; 374fe1ed1b8Smatthias.ringwald 3756772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 376fe1ed1b8Smatthias.ringwald if (!packet[2]){ 377fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 378fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 379fe1ed1b8Smatthias.ringwald if (conn) { 380cf0b66f0Smatthias.ringwald log_dbg("Connection closed: handle %u, ", conn->con_handle); 381fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 382cf0b66f0Smatthias.ringwald log_dbg("\n"); 383ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 384fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 385fe1ed1b8Smatthias.ringwald free( conn ); 38643bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 387fe1ed1b8Smatthias.ringwald } 388fe1ed1b8Smatthias.ringwald } 3896772a24cSmatthias.ringwald break; 3906772a24cSmatthias.ringwald 3916772a24cSmatthias.ringwald default: 3926772a24cSmatthias.ringwald break; 393fe1ed1b8Smatthias.ringwald } 394fe1ed1b8Smatthias.ringwald 3953429f56bSmatthias.ringwald // handle BT initialization 3963429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 3977301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 3987301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 3997301ad89Smatthias.ringwald // hci_stack.substate = 0; 4007301ad89Smatthias.ringwald // } 4017301ad89Smatthias.ringwald // handle normal init sequence 4023429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 4033429f56bSmatthias.ringwald // odd: waiting for event 4043429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 4053429f56bSmatthias.ringwald hci_stack.substate++; 4063429f56bSmatthias.ringwald } 4073429f56bSmatthias.ringwald } 40822909952Smatthias.ringwald } 40922909952Smatthias.ringwald 4102718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 41194ab26f8Smatthias.ringwald 41294ab26f8Smatthias.ringwald // execute main loop 41394ab26f8Smatthias.ringwald hci_run(); 41416833f0aSmatthias.ringwald } 41516833f0aSmatthias.ringwald 41610e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 41710e830c9Smatthias.ringwald switch (packet_type) { 41810e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 41910e830c9Smatthias.ringwald event_handler(packet, size); 42010e830c9Smatthias.ringwald break; 42110e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 42210e830c9Smatthias.ringwald acl_handler(packet, size); 42310e830c9Smatthias.ringwald break; 42410e830c9Smatthias.ringwald default: 42510e830c9Smatthias.ringwald break; 42610e830c9Smatthias.ringwald } 42710e830c9Smatthias.ringwald } 42810e830c9Smatthias.ringwald 429fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 4302718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 4312718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 43216833f0aSmatthias.ringwald } 43316833f0aSmatthias.ringwald 43411e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 435475c8125Smatthias.ringwald 436475c8125Smatthias.ringwald // reference to use transport layer implementation 43716833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 438475c8125Smatthias.ringwald 43911e23e5fSmatthias.ringwald // references to used control implementation 4407301ad89Smatthias.ringwald if (control) { 44111e23e5fSmatthias.ringwald hci_stack.control = control; 4427301ad89Smatthias.ringwald } else { 4437301ad89Smatthias.ringwald hci_stack.control = &null_control; 4447301ad89Smatthias.ringwald } 44511e23e5fSmatthias.ringwald 44611e23e5fSmatthias.ringwald // reference to used config 44711e23e5fSmatthias.ringwald hci_stack.config = config; 44811e23e5fSmatthias.ringwald 449fe1ed1b8Smatthias.ringwald // no connections yet 450fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 451fe1ed1b8Smatthias.ringwald 45202ea9861Smatthias.ringwald // empty cmd buffer 45316833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 45416833f0aSmatthias.ringwald 45516833f0aSmatthias.ringwald // higher level handler 4562718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 45716833f0aSmatthias.ringwald 45816833f0aSmatthias.ringwald // register packet handlers with transport 45910e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 460475c8125Smatthias.ringwald } 461475c8125Smatthias.ringwald 462475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 463f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 4647301ad89Smatthias.ringwald 465038bc64cSmatthias.ringwald // power on 466038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 467038bc64cSmatthias.ringwald if (err){ 4687f2435e6Smatthias.ringwald log_err( "POWER_ON failed\n"); 469038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 470038bc64cSmatthias.ringwald return err; 471038bc64cSmatthias.ringwald } 472038bc64cSmatthias.ringwald 473038bc64cSmatthias.ringwald // open low-level device 474038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 475038bc64cSmatthias.ringwald if (err){ 4767f2435e6Smatthias.ringwald log_err( "HCI_INIT failed, turning Bluetooth off again\n"); 477038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 478038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 479038bc64cSmatthias.ringwald return err; 480038bc64cSmatthias.ringwald } 481038bc64cSmatthias.ringwald 4827301ad89Smatthias.ringwald // set up state machine 4837301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 4847301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 4857301ad89Smatthias.ringwald hci_stack.substate = 0; 4867301ad89Smatthias.ringwald 487f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 4887301ad89Smatthias.ringwald 4897301ad89Smatthias.ringwald // close low-level device 4907301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 4917301ad89Smatthias.ringwald 4927301ad89Smatthias.ringwald // power off 49311e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 49443bfb1bdSmatthias.ringwald 49543bfb1bdSmatthias.ringwald // we're off now 49643bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 49711e23e5fSmatthias.ringwald } 49868d92d03Smatthias.ringwald 499038bc64cSmatthias.ringwald // create internal event 500ee8bf225Smatthias.ringwald hci_emit_state(); 501ee8bf225Smatthias.ringwald 50268d92d03Smatthias.ringwald // trigger next/first action 50368d92d03Smatthias.ringwald hci_run(); 50468d92d03Smatthias.ringwald 505475c8125Smatthias.ringwald return 0; 506475c8125Smatthias.ringwald } 507475c8125Smatthias.ringwald 50806b35ec0Smatthias.ringwald void hci_run(){ 5093429f56bSmatthias.ringwald switch (hci_stack.state){ 5103429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 5113429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 5123429f56bSmatthias.ringwald // odd: waiting for command completion 51306b35ec0Smatthias.ringwald return; 5143429f56bSmatthias.ringwald } 5153429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 5163429f56bSmatthias.ringwald // cannot send command yet 51706b35ec0Smatthias.ringwald return; 5183429f56bSmatthias.ringwald } 5193429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 5203429f56bSmatthias.ringwald case 0: 52122909952Smatthias.ringwald hci_send_cmd(&hci_reset); 5223429f56bSmatthias.ringwald break; 5233429f56bSmatthias.ringwald case 1: 524f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 525f432a6ddSmatthias.ringwald break; 526f432a6ddSmatthias.ringwald case 2: 527e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 528e2edc0c3Smatthias.ringwald break; 529e2edc0c3Smatthias.ringwald case 3: 5303429f56bSmatthias.ringwald // ca. 15 sec 5313429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 5323429f56bSmatthias.ringwald break; 533e2edc0c3Smatthias.ringwald case 4: 534bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 535f432a6ddSmatthias.ringwald break; 536e2edc0c3Smatthias.ringwald case 5: 5373429f56bSmatthias.ringwald // done. 5383429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 539b360b6adSmatthias.ringwald hci_emit_state(); 5403429f56bSmatthias.ringwald break; 5413429f56bSmatthias.ringwald default: 5423429f56bSmatthias.ringwald break; 543475c8125Smatthias.ringwald } 5443429f56bSmatthias.ringwald hci_stack.substate++; 5453429f56bSmatthias.ringwald break; 5463429f56bSmatthias.ringwald default: 5473429f56bSmatthias.ringwald break; 5481f504dbdSmatthias.ringwald } 5493429f56bSmatthias.ringwald } 55016833f0aSmatthias.ringwald 55131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 552c8e4258aSmatthias.ringwald bd_addr_t addr; 553c8e4258aSmatthias.ringwald hci_connection_t * conn; 554c8e4258aSmatthias.ringwald // house-keeping 555c8e4258aSmatthias.ringwald 556c8e4258aSmatthias.ringwald // create_connection? 557c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 558c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 559cf0b66f0Smatthias.ringwald log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n"); 560c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 561c8e4258aSmatthias.ringwald if (conn) { 562c8e4258aSmatthias.ringwald // if connection exists 563c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 564c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 565c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 566c8e4258aSmatthias.ringwald } 567c8e4258aSmatthias.ringwald // otherwise, just ignore 568c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 569c8e4258aSmatthias.ringwald 570c8e4258aSmatthias.ringwald } else{ 571c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 572c8e4258aSmatthias.ringwald if (conn){ 573c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 574c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 575c8e4258aSmatthias.ringwald } 576c8e4258aSmatthias.ringwald } 577c8e4258aSmatthias.ringwald } 578c8e4258aSmatthias.ringwald 579c8e4258aSmatthias.ringwald // accept connection 580c8e4258aSmatthias.ringwald 581c8e4258aSmatthias.ringwald // reject connection 582c8e4258aSmatthias.ringwald 583c8e4258aSmatthias.ringwald // close_connection? 584c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 585c8e4258aSmatthias.ringwald 58631452debSmatthias.ringwald hci_stack.num_cmd_packets--; 58731452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 58831452debSmatthias.ringwald } 5898adf0ddaSmatthias.ringwald 5901cd208adSmatthias.ringwald /** 5911cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 5921cd208adSmatthias.ringwald */ 5931cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 5941cd208adSmatthias.ringwald va_list argptr; 5951cd208adSmatthias.ringwald va_start(argptr, cmd); 5961cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 5971cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 5981cd208adSmatthias.ringwald va_end(argptr); 5991cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 60093b8dc03Smatthias.ringwald } 601c8e4258aSmatthias.ringwald 602ee091cf1Smatthias.ringwald // Create various non-HCI events. 603ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 604ee091cf1Smatthias.ringwald 605c8e4258aSmatthias.ringwald void hci_emit_state(){ 606c8e4258aSmatthias.ringwald uint8_t len = 3; 607c8e4258aSmatthias.ringwald uint8_t event[len]; 60880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 6091b0e3922Smatthias.ringwald event[1] = len - 3; 610c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 611c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6122718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 613c8e4258aSmatthias.ringwald } 614c8e4258aSmatthias.ringwald 615c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 616c8e4258aSmatthias.ringwald uint8_t len = 13; 617c8e4258aSmatthias.ringwald uint8_t event[len]; 618c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 6191b0e3922Smatthias.ringwald event[1] = len - 3; 620c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 621c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 622c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 623c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 624c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 625c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6262718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 627c8e4258aSmatthias.ringwald } 628c8e4258aSmatthias.ringwald 629ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 630ee091cf1Smatthias.ringwald uint8_t len = 4; 631ee091cf1Smatthias.ringwald uint8_t event[len]; 63280d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 6331b0e3922Smatthias.ringwald event[1] = len - 2; 634ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 635ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6362718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 637ee091cf1Smatthias.ringwald } 63843bfb1bdSmatthias.ringwald 63943bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 64043bfb1bdSmatthias.ringwald uint8_t len = 3; 64143bfb1bdSmatthias.ringwald uint8_t event[len]; 64280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 6431b0e3922Smatthias.ringwald event[1] = len - 2; 64443bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 64543bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6462718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 64743bfb1bdSmatthias.ringwald } 648038bc64cSmatthias.ringwald 649038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 6501b0e3922Smatthias.ringwald uint8_t len = 2; 651038bc64cSmatthias.ringwald uint8_t event[len]; 65280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 6531b0e3922Smatthias.ringwald event[1] = len - 2; 654038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6552718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 656038bc64cSmatthias.ringwald } 6571b0e3922Smatthias.ringwald 6581b0e3922Smatthias.ringwald 6591b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 6601b0e3922Smatthias.ringwald uint8_t len = 6; 6611b0e3922Smatthias.ringwald uint8_t event[len]; 6621b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 6631b0e3922Smatthias.ringwald event[1] = len - 2; 6641b0e3922Smatthias.ringwald event[len++] = BTSTACK_MAJOR; 6651b0e3922Smatthias.ringwald event[len++] = BTSTACK_MINOR; 6661b0e3922Smatthias.ringwald bt_store_16(event, len, BTSTACK_REVISION); 6671b0e3922Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6682718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 6691b0e3922Smatthias.ringwald } 6701b0e3922Smatthias.ringwald 6712ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 6722ed6235cSmatthias.ringwald uint8_t len = 3; 6732ed6235cSmatthias.ringwald uint8_t event[len]; 6742ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 675ddce1d8dSmatthias.ringwald event[1] = len - 2; 6762ed6235cSmatthias.ringwald event[2] = enabled; 6772ed6235cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6782718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 6792ed6235cSmatthias.ringwald } 680