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; 110c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 111c8e4258aSmatthias.ringwald return conn; 112c8e4258aSmatthias.ringwald } 113c8e4258aSmatthias.ringwald 114c8e4258aSmatthias.ringwald /** 11506b35ec0Smatthias.ringwald * get connection for given address 11697addcc5Smatthias.ringwald * 11797addcc5Smatthias.ringwald * @return connection OR NULL, if not found 11897addcc5Smatthias.ringwald */ 119fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 12006b35ec0Smatthias.ringwald linked_item_t *it; 12106b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 12206b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 12306b35ec0Smatthias.ringwald return (hci_connection_t *) it; 12406b35ec0Smatthias.ringwald } 12506b35ec0Smatthias.ringwald } 12606b35ec0Smatthias.ringwald return NULL; 12706b35ec0Smatthias.ringwald } 12806b35ec0Smatthias.ringwald 12943bfb1bdSmatthias.ringwald /** 13043bfb1bdSmatthias.ringwald * count connections 13143bfb1bdSmatthias.ringwald */ 13243bfb1bdSmatthias.ringwald static int nr_hci_connections(){ 13356c253c9Smatthias.ringwald int count = 0; 13443bfb1bdSmatthias.ringwald linked_item_t *it; 13543bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 13643bfb1bdSmatthias.ringwald return count; 13743bfb1bdSmatthias.ringwald } 138c8e4258aSmatthias.ringwald 13997addcc5Smatthias.ringwald /** 140ba681a6cSmatthias.ringwald * Dummy handler called by HCI 14116833f0aSmatthias.ringwald */ 142*2718e2e7Smatthias.ringwald static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 14316833f0aSmatthias.ringwald } 14416833f0aSmatthias.ringwald 145ba681a6cSmatthias.ringwald /** 146ba681a6cSmatthias.ringwald * Dummy control handler 147ba681a6cSmatthias.ringwald */ 148ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 149ba681a6cSmatthias.ringwald return 0; 150ba681a6cSmatthias.ringwald } 151ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 152ba681a6cSmatthias.ringwald return "Hardware unknown"; 153ba681a6cSmatthias.ringwald } 154ba681a6cSmatthias.ringwald static bt_control_t null_control = { 155ba681a6cSmatthias.ringwald null_control_function, 156ba681a6cSmatthias.ringwald null_control_function, 157ba681a6cSmatthias.ringwald null_control_function, 158ba681a6cSmatthias.ringwald null_control_name 159ba681a6cSmatthias.ringwald }; 160ba681a6cSmatthias.ringwald 161c8e4258aSmatthias.ringwald 162ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 1637856c818Smatthias.ringwald 1647856c818Smatthias.ringwald // update idle timestamp 1657856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 1667856c818Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 1677856c818Smatthias.ringwald if (connection) hci_connection_timestamp(connection); 1687856c818Smatthias.ringwald 1697856c818Smatthias.ringwald // send packet 170ee091cf1Smatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 171ee091cf1Smatthias.ringwald } 172ee091cf1Smatthias.ringwald 17316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 1747856c818Smatthias.ringwald 1757856c818Smatthias.ringwald // get info 1767856c818Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 1777856c818Smatthias.ringwald hci_connection_t *conn = connection_for_handle(con_handle); 1787856c818Smatthias.ringwald uint8_t acl_flags = READ_ACL_FLAGS(packet); 1797856c818Smatthias.ringwald uint16_t acl_length = READ_ACL_LENGTH(packet); 1807856c818Smatthias.ringwald 1817856c818Smatthias.ringwald // ignore non-registered handle 1827856c818Smatthias.ringwald if (!conn){ 1837f2435e6Smatthias.ringwald log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 1847856c818Smatthias.ringwald return; 1857856c818Smatthias.ringwald } 1867856c818Smatthias.ringwald 1877856c818Smatthias.ringwald // update idle timestamp 1887856c818Smatthias.ringwald hci_connection_timestamp(conn); 1897856c818Smatthias.ringwald 1907856c818Smatthias.ringwald // handle different packet types 1917856c818Smatthias.ringwald switch (acl_flags & 0x03) { 1927856c818Smatthias.ringwald 1937856c818Smatthias.ringwald case 0x01: // continuation fragment 1947856c818Smatthias.ringwald 1957856c818Smatthias.ringwald // sanity check 1967856c818Smatthias.ringwald if (conn->acl_recombination_pos == 0) { 1977f2435e6Smatthias.ringwald log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 1987856c818Smatthias.ringwald return; 1997856c818Smatthias.ringwald } 2007856c818Smatthias.ringwald 2017856c818Smatthias.ringwald // append fragment payload (header already stored) 2027856c818Smatthias.ringwald memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 2037856c818Smatthias.ringwald conn->acl_recombination_pos += acl_length; 2047856c818Smatthias.ringwald 2057f2435e6Smatthias.ringwald // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", 2067856c818Smatthias.ringwald // acl_length, connection->acl_recombination_pos, connection->acl_recombination_length); 2077856c818Smatthias.ringwald 2087856c818Smatthias.ringwald // forward complete L2CAP packet if complete. 2097856c818Smatthias.ringwald if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 2107856c818Smatthias.ringwald 211*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 2127856c818Smatthias.ringwald // reset recombination buffer 2137856c818Smatthias.ringwald conn->acl_recombination_length = 0; 2147856c818Smatthias.ringwald conn->acl_recombination_pos = 0; 2157856c818Smatthias.ringwald } 2167856c818Smatthias.ringwald break; 2177856c818Smatthias.ringwald 2187856c818Smatthias.ringwald case 0x02: { // first fragment 2197856c818Smatthias.ringwald 2207856c818Smatthias.ringwald // sanity check 2217856c818Smatthias.ringwald if (conn->acl_recombination_pos) { 2227f2435e6Smatthias.ringwald log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 2237856c818Smatthias.ringwald return; 2247856c818Smatthias.ringwald } 2257856c818Smatthias.ringwald 2267856c818Smatthias.ringwald // peek into L2CAP packet! 2277856c818Smatthias.ringwald uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 2287856c818Smatthias.ringwald 2297856c818Smatthias.ringwald // compare fragment size to L2CAP packet size 2307856c818Smatthias.ringwald if (acl_length >= l2cap_length + 4){ 2317856c818Smatthias.ringwald 2327856c818Smatthias.ringwald // forward fragment as L2CAP packet 233*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 2347856c818Smatthias.ringwald 2357856c818Smatthias.ringwald } else { 2367856c818Smatthias.ringwald // store first fragment and tweak acl length for complete package 2377856c818Smatthias.ringwald memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 2387856c818Smatthias.ringwald conn->acl_recombination_pos = acl_length + 4; 2397856c818Smatthias.ringwald conn->acl_recombination_length = l2cap_length; 2407856c818Smatthias.ringwald bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4); 2417f2435e6Smatthias.ringwald // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 2427856c818Smatthias.ringwald } 2437856c818Smatthias.ringwald break; 2447856c818Smatthias.ringwald 2457856c818Smatthias.ringwald } 2467856c818Smatthias.ringwald default: 2477f2435e6Smatthias.ringwald log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 2487856c818Smatthias.ringwald return; 2497856c818Smatthias.ringwald } 25094ab26f8Smatthias.ringwald 25194ab26f8Smatthias.ringwald // execute main loop 25294ab26f8Smatthias.ringwald hci_run(); 25316833f0aSmatthias.ringwald } 25422909952Smatthias.ringwald 25516833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 2561281a47eSmatthias.ringwald bd_addr_t addr; 257fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 2581f7b95a1Smatthias.ringwald hci_connection_t * conn; 25922909952Smatthias.ringwald 2606772a24cSmatthias.ringwald switch (packet[0]) { 26122909952Smatthias.ringwald 2626772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 2636772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 2646772a24cSmatthias.ringwald // Get Num_HCI_Command_Packets 2656772a24cSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 2666772a24cSmatthias.ringwald break; 2676772a24cSmatthias.ringwald 2681f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 26937eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 27037eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 27137eaa4cfSmatthias.ringwald uint8_t link_type = packet[11]; 272cf0b66f0Smatthias.ringwald log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type); 27337eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 2741f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 2751f7b95a1Smatthias.ringwald if (!conn) { 2761f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 2771f7b95a1Smatthias.ringwald } 2781f7b95a1Smatthias.ringwald // TODO: check for malloc failure 2791f7b95a1Smatthias.ringwald conn->state = ACCEPTED_CONNECTION_REQUEST; 28037eaa4cfSmatthias.ringwald hci_send_cmd(&hci_accept_connection_request, addr, 1); 28137eaa4cfSmatthias.ringwald } else { 28237eaa4cfSmatthias.ringwald // TODO: decline request 28337eaa4cfSmatthias.ringwald } 2841f7b95a1Smatthias.ringwald break; 2851f7b95a1Smatthias.ringwald 2866772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 287fe1ed1b8Smatthias.ringwald // Connection management 288fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 289cf0b66f0Smatthias.ringwald log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n"); 2901f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 291fe1ed1b8Smatthias.ringwald if (conn) { 292b448a0e7Smatthias.ringwald if (!packet[2]){ 293c8e4258aSmatthias.ringwald conn->state = OPEN; 294fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 295fe1ed1b8Smatthias.ringwald conn->flags = 0; 296ee091cf1Smatthias.ringwald 297ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 298c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 299ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 300ee091cf1Smatthias.ringwald 301cf0b66f0Smatthias.ringwald log_dbg("New connection: handle %u, ", conn->con_handle); 302fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 303cf0b66f0Smatthias.ringwald log_dbg("\n"); 30443bfb1bdSmatthias.ringwald 30543bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 306b448a0e7Smatthias.ringwald } else { 307b448a0e7Smatthias.ringwald // connection failed, remove entry 308b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 309b448a0e7Smatthias.ringwald free( conn ); 310fe1ed1b8Smatthias.ringwald } 311fe1ed1b8Smatthias.ringwald } 3126772a24cSmatthias.ringwald break; 313fe1ed1b8Smatthias.ringwald 3146772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 315fe1ed1b8Smatthias.ringwald if (!packet[2]){ 316fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 317fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 318fe1ed1b8Smatthias.ringwald if (conn) { 319cf0b66f0Smatthias.ringwald log_dbg("Connection closed: handle %u, ", conn->con_handle); 320fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 321cf0b66f0Smatthias.ringwald log_dbg("\n"); 322ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 323fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 324fe1ed1b8Smatthias.ringwald free( conn ); 32543bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 326fe1ed1b8Smatthias.ringwald } 327fe1ed1b8Smatthias.ringwald } 3286772a24cSmatthias.ringwald break; 3296772a24cSmatthias.ringwald 3306772a24cSmatthias.ringwald default: 3316772a24cSmatthias.ringwald break; 332fe1ed1b8Smatthias.ringwald } 333fe1ed1b8Smatthias.ringwald 3343429f56bSmatthias.ringwald // handle BT initialization 3353429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 3367301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 3377301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 3387301ad89Smatthias.ringwald // hci_stack.substate = 0; 3397301ad89Smatthias.ringwald // } 3407301ad89Smatthias.ringwald // handle normal init sequence 3413429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 3423429f56bSmatthias.ringwald // odd: waiting for event 3433429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 3443429f56bSmatthias.ringwald hci_stack.substate++; 3453429f56bSmatthias.ringwald } 3463429f56bSmatthias.ringwald } 34722909952Smatthias.ringwald } 34822909952Smatthias.ringwald 349*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 35094ab26f8Smatthias.ringwald 35194ab26f8Smatthias.ringwald // execute main loop 35294ab26f8Smatthias.ringwald hci_run(); 35316833f0aSmatthias.ringwald } 35416833f0aSmatthias.ringwald 35510e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 35610e830c9Smatthias.ringwald switch (packet_type) { 35710e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 35810e830c9Smatthias.ringwald event_handler(packet, size); 35910e830c9Smatthias.ringwald break; 36010e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 36110e830c9Smatthias.ringwald acl_handler(packet, size); 36210e830c9Smatthias.ringwald break; 36310e830c9Smatthias.ringwald default: 36410e830c9Smatthias.ringwald break; 36510e830c9Smatthias.ringwald } 36610e830c9Smatthias.ringwald } 36710e830c9Smatthias.ringwald 368fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 369*2718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 370*2718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 37116833f0aSmatthias.ringwald } 37216833f0aSmatthias.ringwald 37311e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 374475c8125Smatthias.ringwald 375475c8125Smatthias.ringwald // reference to use transport layer implementation 37616833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 377475c8125Smatthias.ringwald 37811e23e5fSmatthias.ringwald // references to used control implementation 3797301ad89Smatthias.ringwald if (control) { 38011e23e5fSmatthias.ringwald hci_stack.control = control; 3817301ad89Smatthias.ringwald } else { 3827301ad89Smatthias.ringwald hci_stack.control = &null_control; 3837301ad89Smatthias.ringwald } 38411e23e5fSmatthias.ringwald 38511e23e5fSmatthias.ringwald // reference to used config 38611e23e5fSmatthias.ringwald hci_stack.config = config; 38711e23e5fSmatthias.ringwald 388fe1ed1b8Smatthias.ringwald // no connections yet 389fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 390fe1ed1b8Smatthias.ringwald 39102ea9861Smatthias.ringwald // empty cmd buffer 39216833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 39316833f0aSmatthias.ringwald 39416833f0aSmatthias.ringwald // higher level handler 395*2718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 39616833f0aSmatthias.ringwald 39716833f0aSmatthias.ringwald // register packet handlers with transport 39810e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 399475c8125Smatthias.ringwald } 400475c8125Smatthias.ringwald 401475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 402f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 4037301ad89Smatthias.ringwald 404038bc64cSmatthias.ringwald // power on 405038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 406038bc64cSmatthias.ringwald if (err){ 4077f2435e6Smatthias.ringwald log_err( "POWER_ON failed\n"); 408038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 409038bc64cSmatthias.ringwald return err; 410038bc64cSmatthias.ringwald } 411038bc64cSmatthias.ringwald 412038bc64cSmatthias.ringwald // open low-level device 413038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 414038bc64cSmatthias.ringwald if (err){ 4157f2435e6Smatthias.ringwald log_err( "HCI_INIT failed, turning Bluetooth off again\n"); 416038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 417038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 418038bc64cSmatthias.ringwald return err; 419038bc64cSmatthias.ringwald } 420038bc64cSmatthias.ringwald 4217301ad89Smatthias.ringwald // set up state machine 4227301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 4237301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 4247301ad89Smatthias.ringwald hci_stack.substate = 0; 4257301ad89Smatthias.ringwald 426f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 4277301ad89Smatthias.ringwald 4287301ad89Smatthias.ringwald // close low-level device 4297301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 4307301ad89Smatthias.ringwald 4317301ad89Smatthias.ringwald // power off 43211e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 43343bfb1bdSmatthias.ringwald 43443bfb1bdSmatthias.ringwald // we're off now 43543bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 43611e23e5fSmatthias.ringwald } 43768d92d03Smatthias.ringwald 438038bc64cSmatthias.ringwald // create internal event 439ee8bf225Smatthias.ringwald hci_emit_state(); 440ee8bf225Smatthias.ringwald 44168d92d03Smatthias.ringwald // trigger next/first action 44268d92d03Smatthias.ringwald hci_run(); 44368d92d03Smatthias.ringwald 444475c8125Smatthias.ringwald return 0; 445475c8125Smatthias.ringwald } 446475c8125Smatthias.ringwald 44706b35ec0Smatthias.ringwald void hci_run(){ 4483429f56bSmatthias.ringwald switch (hci_stack.state){ 4493429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 4503429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 4513429f56bSmatthias.ringwald // odd: waiting for command completion 45206b35ec0Smatthias.ringwald return; 4533429f56bSmatthias.ringwald } 4543429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 4553429f56bSmatthias.ringwald // cannot send command yet 45606b35ec0Smatthias.ringwald return; 4573429f56bSmatthias.ringwald } 4583429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 4593429f56bSmatthias.ringwald case 0: 46022909952Smatthias.ringwald hci_send_cmd(&hci_reset); 4613429f56bSmatthias.ringwald break; 4623429f56bSmatthias.ringwald case 1: 463f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 464f432a6ddSmatthias.ringwald break; 465f432a6ddSmatthias.ringwald case 2: 4663429f56bSmatthias.ringwald // ca. 15 sec 4673429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 4683429f56bSmatthias.ringwald break; 469f432a6ddSmatthias.ringwald case 3: 470bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 471f432a6ddSmatthias.ringwald break; 472f432a6ddSmatthias.ringwald case 4: 4733429f56bSmatthias.ringwald // done. 4743429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 475b360b6adSmatthias.ringwald hci_emit_state(); 4763429f56bSmatthias.ringwald break; 4773429f56bSmatthias.ringwald default: 4783429f56bSmatthias.ringwald break; 479475c8125Smatthias.ringwald } 4803429f56bSmatthias.ringwald hci_stack.substate++; 4813429f56bSmatthias.ringwald break; 4823429f56bSmatthias.ringwald default: 4833429f56bSmatthias.ringwald break; 4841f504dbdSmatthias.ringwald } 4853429f56bSmatthias.ringwald } 48616833f0aSmatthias.ringwald 48731452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 488c8e4258aSmatthias.ringwald bd_addr_t addr; 489c8e4258aSmatthias.ringwald hci_connection_t * conn; 490c8e4258aSmatthias.ringwald // house-keeping 491c8e4258aSmatthias.ringwald 492c8e4258aSmatthias.ringwald // create_connection? 493c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 494c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 495cf0b66f0Smatthias.ringwald log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n"); 496c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 497c8e4258aSmatthias.ringwald if (conn) { 498c8e4258aSmatthias.ringwald // if connection exists 499c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 500c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 501c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 502c8e4258aSmatthias.ringwald } 503c8e4258aSmatthias.ringwald // otherwise, just ignore 504c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 505c8e4258aSmatthias.ringwald 506c8e4258aSmatthias.ringwald } else{ 507c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 508c8e4258aSmatthias.ringwald if (conn){ 509c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 510c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 511c8e4258aSmatthias.ringwald } 512c8e4258aSmatthias.ringwald } 513c8e4258aSmatthias.ringwald } 514c8e4258aSmatthias.ringwald 515c8e4258aSmatthias.ringwald // accept connection 516c8e4258aSmatthias.ringwald 517c8e4258aSmatthias.ringwald // reject connection 518c8e4258aSmatthias.ringwald 519c8e4258aSmatthias.ringwald // close_connection? 520c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 521c8e4258aSmatthias.ringwald 52231452debSmatthias.ringwald hci_stack.num_cmd_packets--; 52331452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 52431452debSmatthias.ringwald } 5258adf0ddaSmatthias.ringwald 5261cd208adSmatthias.ringwald /** 5271cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 5281cd208adSmatthias.ringwald */ 5291cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 5301cd208adSmatthias.ringwald va_list argptr; 5311cd208adSmatthias.ringwald va_start(argptr, cmd); 5321cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 5331cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 5341cd208adSmatthias.ringwald va_end(argptr); 5351cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 53693b8dc03Smatthias.ringwald } 537c8e4258aSmatthias.ringwald 538ee091cf1Smatthias.ringwald // Create various non-HCI events. 539ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 540ee091cf1Smatthias.ringwald 541c8e4258aSmatthias.ringwald void hci_emit_state(){ 542c8e4258aSmatthias.ringwald uint8_t len = 3; 543c8e4258aSmatthias.ringwald uint8_t event[len]; 54480d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 5451b0e3922Smatthias.ringwald event[1] = len - 3; 546c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 547c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 548*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 549c8e4258aSmatthias.ringwald } 550c8e4258aSmatthias.ringwald 551c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 552c8e4258aSmatthias.ringwald uint8_t len = 13; 553c8e4258aSmatthias.ringwald uint8_t event[len]; 554c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5551b0e3922Smatthias.ringwald event[1] = len - 3; 556c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 557c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 558c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 559c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 560c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 561c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 562*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 563c8e4258aSmatthias.ringwald } 564c8e4258aSmatthias.ringwald 565ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 566ee091cf1Smatthias.ringwald uint8_t len = 4; 567ee091cf1Smatthias.ringwald uint8_t event[len]; 56880d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 5691b0e3922Smatthias.ringwald event[1] = len - 2; 570ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 571ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 572*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 573ee091cf1Smatthias.ringwald } 57443bfb1bdSmatthias.ringwald 57543bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 57643bfb1bdSmatthias.ringwald uint8_t len = 3; 57743bfb1bdSmatthias.ringwald uint8_t event[len]; 57880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 5791b0e3922Smatthias.ringwald event[1] = len - 2; 58043bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 58143bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 582*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 58343bfb1bdSmatthias.ringwald } 584038bc64cSmatthias.ringwald 585038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 5861b0e3922Smatthias.ringwald uint8_t len = 2; 587038bc64cSmatthias.ringwald uint8_t event[len]; 58880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 5891b0e3922Smatthias.ringwald event[1] = len - 2; 590038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 591*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 592038bc64cSmatthias.ringwald } 5931b0e3922Smatthias.ringwald 5941b0e3922Smatthias.ringwald 5951b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 5961b0e3922Smatthias.ringwald uint8_t len = 6; 5971b0e3922Smatthias.ringwald uint8_t event[len]; 5981b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 5991b0e3922Smatthias.ringwald event[1] = len - 2; 6001b0e3922Smatthias.ringwald event[len++] = BTSTACK_MAJOR; 6011b0e3922Smatthias.ringwald event[len++] = BTSTACK_MINOR; 6021b0e3922Smatthias.ringwald bt_store_16(event, len, BTSTACK_REVISION); 6031b0e3922Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 604*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 6051b0e3922Smatthias.ringwald } 6061b0e3922Smatthias.ringwald 6072ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 6082ed6235cSmatthias.ringwald uint8_t len = 3; 6092ed6235cSmatthias.ringwald uint8_t event[len]; 6102ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 611ddce1d8dSmatthias.ringwald event[1] = len - 2; 6122ed6235cSmatthias.ringwald event[2] = enabled; 6132ed6235cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 614*2718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 6152ed6235cSmatthias.ringwald } 616