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 */ 1422718e2e7Smatthias.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 2112718e2e7Smatthias.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 2332718e2e7Smatthias.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 260*e2edc0c3Smatthias.ringwald // get num_cmd_packets 261*e2edc0c3Smatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 262*e2edc0c3Smatthias.ringwald // Get Num_HCI_Command_Packets 263*e2edc0c3Smatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 264*e2edc0c3Smatthias.ringwald } 265*e2edc0c3Smatthias.ringwald 2666772a24cSmatthias.ringwald switch (packet[0]) { 26722909952Smatthias.ringwald 2686772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 269*e2edc0c3Smatthias.ringwald if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 270*e2edc0c3Smatthias.ringwald // from offset 5 271*e2edc0c3Smatthias.ringwald // status 272*e2edc0c3Smatthias.ringwald hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 273*e2edc0c3Smatthias.ringwald // SCO data packet len (8) 274*e2edc0c3Smatthias.ringwald hci_stack.total_num_acl_packets = packet[9]; 275*e2edc0c3Smatthias.ringwald // total num SCO packets 276*e2edc0c3Smatthias.ringwald log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets); 277*e2edc0c3Smatthias.ringwald } 2786772a24cSmatthias.ringwald break; 2796772a24cSmatthias.ringwald 2801f7b95a1Smatthias.ringwald case HCI_EVENT_CONNECTION_REQUEST: 28137eaa4cfSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 28237eaa4cfSmatthias.ringwald // TODO: eval COD 8-10 28337eaa4cfSmatthias.ringwald uint8_t link_type = packet[11]; 284cf0b66f0Smatthias.ringwald log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type); 28537eaa4cfSmatthias.ringwald if (link_type == 1) { // ACL 2861f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 2871f7b95a1Smatthias.ringwald if (!conn) { 2881f7b95a1Smatthias.ringwald conn = create_connection_for_addr(addr); 2891f7b95a1Smatthias.ringwald } 2901f7b95a1Smatthias.ringwald // TODO: check for malloc failure 2911f7b95a1Smatthias.ringwald conn->state = ACCEPTED_CONNECTION_REQUEST; 29237eaa4cfSmatthias.ringwald hci_send_cmd(&hci_accept_connection_request, addr, 1); 29337eaa4cfSmatthias.ringwald } else { 29437eaa4cfSmatthias.ringwald // TODO: decline request 29537eaa4cfSmatthias.ringwald } 2961f7b95a1Smatthias.ringwald break; 2971f7b95a1Smatthias.ringwald 2986772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 299fe1ed1b8Smatthias.ringwald // Connection management 300fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 301cf0b66f0Smatthias.ringwald log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n"); 3021f7b95a1Smatthias.ringwald conn = connection_for_address(addr); 303fe1ed1b8Smatthias.ringwald if (conn) { 304b448a0e7Smatthias.ringwald if (!packet[2]){ 305c8e4258aSmatthias.ringwald conn->state = OPEN; 306fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 307fe1ed1b8Smatthias.ringwald conn->flags = 0; 308ee091cf1Smatthias.ringwald 309ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 310c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 311ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 312ee091cf1Smatthias.ringwald 313cf0b66f0Smatthias.ringwald log_dbg("New connection: handle %u, ", conn->con_handle); 314fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 315cf0b66f0Smatthias.ringwald log_dbg("\n"); 31643bfb1bdSmatthias.ringwald 31743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 318b448a0e7Smatthias.ringwald } else { 319b448a0e7Smatthias.ringwald // connection failed, remove entry 320b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 321b448a0e7Smatthias.ringwald free( conn ); 322fe1ed1b8Smatthias.ringwald } 323fe1ed1b8Smatthias.ringwald } 3246772a24cSmatthias.ringwald break; 325fe1ed1b8Smatthias.ringwald 3266772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 327fe1ed1b8Smatthias.ringwald if (!packet[2]){ 328fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 329fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 330fe1ed1b8Smatthias.ringwald if (conn) { 331cf0b66f0Smatthias.ringwald log_dbg("Connection closed: handle %u, ", conn->con_handle); 332fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 333cf0b66f0Smatthias.ringwald log_dbg("\n"); 334ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 335fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 336fe1ed1b8Smatthias.ringwald free( conn ); 33743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 338fe1ed1b8Smatthias.ringwald } 339fe1ed1b8Smatthias.ringwald } 3406772a24cSmatthias.ringwald break; 3416772a24cSmatthias.ringwald 3426772a24cSmatthias.ringwald default: 3436772a24cSmatthias.ringwald break; 344fe1ed1b8Smatthias.ringwald } 345fe1ed1b8Smatthias.ringwald 3463429f56bSmatthias.ringwald // handle BT initialization 3473429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 3487301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 3497301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 3507301ad89Smatthias.ringwald // hci_stack.substate = 0; 3517301ad89Smatthias.ringwald // } 3527301ad89Smatthias.ringwald // handle normal init sequence 3533429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 3543429f56bSmatthias.ringwald // odd: waiting for event 3553429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 3563429f56bSmatthias.ringwald hci_stack.substate++; 3573429f56bSmatthias.ringwald } 3583429f56bSmatthias.ringwald } 35922909952Smatthias.ringwald } 36022909952Smatthias.ringwald 3612718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 36294ab26f8Smatthias.ringwald 36394ab26f8Smatthias.ringwald // execute main loop 36494ab26f8Smatthias.ringwald hci_run(); 36516833f0aSmatthias.ringwald } 36616833f0aSmatthias.ringwald 36710e830c9Smatthias.ringwald void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 36810e830c9Smatthias.ringwald switch (packet_type) { 36910e830c9Smatthias.ringwald case HCI_EVENT_PACKET: 37010e830c9Smatthias.ringwald event_handler(packet, size); 37110e830c9Smatthias.ringwald break; 37210e830c9Smatthias.ringwald case HCI_ACL_DATA_PACKET: 37310e830c9Smatthias.ringwald acl_handler(packet, size); 37410e830c9Smatthias.ringwald break; 37510e830c9Smatthias.ringwald default: 37610e830c9Smatthias.ringwald break; 37710e830c9Smatthias.ringwald } 37810e830c9Smatthias.ringwald } 37910e830c9Smatthias.ringwald 380fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 3812718e2e7Smatthias.ringwald void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 3822718e2e7Smatthias.ringwald hci_stack.packet_handler = handler; 38316833f0aSmatthias.ringwald } 38416833f0aSmatthias.ringwald 38511e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 386475c8125Smatthias.ringwald 387475c8125Smatthias.ringwald // reference to use transport layer implementation 38816833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 389475c8125Smatthias.ringwald 39011e23e5fSmatthias.ringwald // references to used control implementation 3917301ad89Smatthias.ringwald if (control) { 39211e23e5fSmatthias.ringwald hci_stack.control = control; 3937301ad89Smatthias.ringwald } else { 3947301ad89Smatthias.ringwald hci_stack.control = &null_control; 3957301ad89Smatthias.ringwald } 39611e23e5fSmatthias.ringwald 39711e23e5fSmatthias.ringwald // reference to used config 39811e23e5fSmatthias.ringwald hci_stack.config = config; 39911e23e5fSmatthias.ringwald 400fe1ed1b8Smatthias.ringwald // no connections yet 401fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 402fe1ed1b8Smatthias.ringwald 40302ea9861Smatthias.ringwald // empty cmd buffer 40416833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 40516833f0aSmatthias.ringwald 40616833f0aSmatthias.ringwald // higher level handler 4072718e2e7Smatthias.ringwald hci_stack.packet_handler = dummy_handler; 40816833f0aSmatthias.ringwald 40916833f0aSmatthias.ringwald // register packet handlers with transport 41010e830c9Smatthias.ringwald transport->register_packet_handler(&packet_handler); 411475c8125Smatthias.ringwald } 412475c8125Smatthias.ringwald 413475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 414f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 4157301ad89Smatthias.ringwald 416038bc64cSmatthias.ringwald // power on 417038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 418038bc64cSmatthias.ringwald if (err){ 4197f2435e6Smatthias.ringwald log_err( "POWER_ON failed\n"); 420038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 421038bc64cSmatthias.ringwald return err; 422038bc64cSmatthias.ringwald } 423038bc64cSmatthias.ringwald 424038bc64cSmatthias.ringwald // open low-level device 425038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 426038bc64cSmatthias.ringwald if (err){ 4277f2435e6Smatthias.ringwald log_err( "HCI_INIT failed, turning Bluetooth off again\n"); 428038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 429038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 430038bc64cSmatthias.ringwald return err; 431038bc64cSmatthias.ringwald } 432038bc64cSmatthias.ringwald 4337301ad89Smatthias.ringwald // set up state machine 4347301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 4357301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 4367301ad89Smatthias.ringwald hci_stack.substate = 0; 4377301ad89Smatthias.ringwald 438f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 4397301ad89Smatthias.ringwald 4407301ad89Smatthias.ringwald // close low-level device 4417301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 4427301ad89Smatthias.ringwald 4437301ad89Smatthias.ringwald // power off 44411e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 44543bfb1bdSmatthias.ringwald 44643bfb1bdSmatthias.ringwald // we're off now 44743bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 44811e23e5fSmatthias.ringwald } 44968d92d03Smatthias.ringwald 450038bc64cSmatthias.ringwald // create internal event 451ee8bf225Smatthias.ringwald hci_emit_state(); 452ee8bf225Smatthias.ringwald 45368d92d03Smatthias.ringwald // trigger next/first action 45468d92d03Smatthias.ringwald hci_run(); 45568d92d03Smatthias.ringwald 456475c8125Smatthias.ringwald return 0; 457475c8125Smatthias.ringwald } 458475c8125Smatthias.ringwald 45906b35ec0Smatthias.ringwald void hci_run(){ 4603429f56bSmatthias.ringwald switch (hci_stack.state){ 4613429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 4623429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 4633429f56bSmatthias.ringwald // odd: waiting for command completion 46406b35ec0Smatthias.ringwald return; 4653429f56bSmatthias.ringwald } 4663429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 4673429f56bSmatthias.ringwald // cannot send command yet 46806b35ec0Smatthias.ringwald return; 4693429f56bSmatthias.ringwald } 4703429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 4713429f56bSmatthias.ringwald case 0: 47222909952Smatthias.ringwald hci_send_cmd(&hci_reset); 4733429f56bSmatthias.ringwald break; 4743429f56bSmatthias.ringwald case 1: 475f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 476f432a6ddSmatthias.ringwald break; 477f432a6ddSmatthias.ringwald case 2: 478*e2edc0c3Smatthias.ringwald hci_send_cmd(&hci_read_buffer_size); 479*e2edc0c3Smatthias.ringwald break; 480*e2edc0c3Smatthias.ringwald case 3: 4813429f56bSmatthias.ringwald // ca. 15 sec 4823429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 4833429f56bSmatthias.ringwald break; 484*e2edc0c3Smatthias.ringwald case 4: 485bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 486f432a6ddSmatthias.ringwald break; 487*e2edc0c3Smatthias.ringwald case 5: 4883429f56bSmatthias.ringwald // done. 4893429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 490b360b6adSmatthias.ringwald hci_emit_state(); 4913429f56bSmatthias.ringwald break; 4923429f56bSmatthias.ringwald default: 4933429f56bSmatthias.ringwald break; 494475c8125Smatthias.ringwald } 4953429f56bSmatthias.ringwald hci_stack.substate++; 4963429f56bSmatthias.ringwald break; 4973429f56bSmatthias.ringwald default: 4983429f56bSmatthias.ringwald break; 4991f504dbdSmatthias.ringwald } 5003429f56bSmatthias.ringwald } 50116833f0aSmatthias.ringwald 50231452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 503c8e4258aSmatthias.ringwald bd_addr_t addr; 504c8e4258aSmatthias.ringwald hci_connection_t * conn; 505c8e4258aSmatthias.ringwald // house-keeping 506c8e4258aSmatthias.ringwald 507c8e4258aSmatthias.ringwald // create_connection? 508c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 509c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 510cf0b66f0Smatthias.ringwald log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n"); 511c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 512c8e4258aSmatthias.ringwald if (conn) { 513c8e4258aSmatthias.ringwald // if connection exists 514c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 515c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 516c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 517c8e4258aSmatthias.ringwald } 518c8e4258aSmatthias.ringwald // otherwise, just ignore 519c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 520c8e4258aSmatthias.ringwald 521c8e4258aSmatthias.ringwald } else{ 522c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 523c8e4258aSmatthias.ringwald if (conn){ 524c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 525c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 526c8e4258aSmatthias.ringwald } 527c8e4258aSmatthias.ringwald } 528c8e4258aSmatthias.ringwald } 529c8e4258aSmatthias.ringwald 530c8e4258aSmatthias.ringwald // accept connection 531c8e4258aSmatthias.ringwald 532c8e4258aSmatthias.ringwald // reject connection 533c8e4258aSmatthias.ringwald 534c8e4258aSmatthias.ringwald // close_connection? 535c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 536c8e4258aSmatthias.ringwald 53731452debSmatthias.ringwald hci_stack.num_cmd_packets--; 53831452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 53931452debSmatthias.ringwald } 5408adf0ddaSmatthias.ringwald 5411cd208adSmatthias.ringwald /** 5421cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 5431cd208adSmatthias.ringwald */ 5441cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 5451cd208adSmatthias.ringwald va_list argptr; 5461cd208adSmatthias.ringwald va_start(argptr, cmd); 5471cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 5481cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 5491cd208adSmatthias.ringwald va_end(argptr); 5501cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 55193b8dc03Smatthias.ringwald } 552c8e4258aSmatthias.ringwald 553ee091cf1Smatthias.ringwald // Create various non-HCI events. 554ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 555ee091cf1Smatthias.ringwald 556c8e4258aSmatthias.ringwald void hci_emit_state(){ 557c8e4258aSmatthias.ringwald uint8_t len = 3; 558c8e4258aSmatthias.ringwald uint8_t event[len]; 55980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 5601b0e3922Smatthias.ringwald event[1] = len - 3; 561c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 562c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 5632718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 564c8e4258aSmatthias.ringwald } 565c8e4258aSmatthias.ringwald 566c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 567c8e4258aSmatthias.ringwald uint8_t len = 13; 568c8e4258aSmatthias.ringwald uint8_t event[len]; 569c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 5701b0e3922Smatthias.ringwald event[1] = len - 3; 571c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 572c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 573c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 574c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 575c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 576c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 5772718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 578c8e4258aSmatthias.ringwald } 579c8e4258aSmatthias.ringwald 580ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 581ee091cf1Smatthias.ringwald uint8_t len = 4; 582ee091cf1Smatthias.ringwald uint8_t event[len]; 58380d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 5841b0e3922Smatthias.ringwald event[1] = len - 2; 585ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 586ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 5872718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 588ee091cf1Smatthias.ringwald } 58943bfb1bdSmatthias.ringwald 59043bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 59143bfb1bdSmatthias.ringwald uint8_t len = 3; 59243bfb1bdSmatthias.ringwald uint8_t event[len]; 59380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 5941b0e3922Smatthias.ringwald event[1] = len - 2; 59543bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 59643bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 5972718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 59843bfb1bdSmatthias.ringwald } 599038bc64cSmatthias.ringwald 600038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 6011b0e3922Smatthias.ringwald uint8_t len = 2; 602038bc64cSmatthias.ringwald uint8_t event[len]; 60380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 6041b0e3922Smatthias.ringwald event[1] = len - 2; 605038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6062718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 607038bc64cSmatthias.ringwald } 6081b0e3922Smatthias.ringwald 6091b0e3922Smatthias.ringwald 6101b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 6111b0e3922Smatthias.ringwald uint8_t len = 6; 6121b0e3922Smatthias.ringwald uint8_t event[len]; 6131b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 6141b0e3922Smatthias.ringwald event[1] = len - 2; 6151b0e3922Smatthias.ringwald event[len++] = BTSTACK_MAJOR; 6161b0e3922Smatthias.ringwald event[len++] = BTSTACK_MINOR; 6171b0e3922Smatthias.ringwald bt_store_16(event, len, BTSTACK_REVISION); 6181b0e3922Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6192718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 6201b0e3922Smatthias.ringwald } 6211b0e3922Smatthias.ringwald 6222ed6235cSmatthias.ringwald void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 6232ed6235cSmatthias.ringwald uint8_t len = 3; 6242ed6235cSmatthias.ringwald uint8_t event[len]; 6252ed6235cSmatthias.ringwald event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 626ddce1d8dSmatthias.ringwald event[1] = len - 2; 6272ed6235cSmatthias.ringwald event[2] = enabled; 6282ed6235cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 6292718e2e7Smatthias.ringwald hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 6302ed6235cSmatthias.ringwald } 631