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 39475c8125Smatthias.ringwald #include <unistd.h> 4093b8dc03Smatthias.ringwald #include <stdarg.h> 4193b8dc03Smatthias.ringwald #include <string.h> 4256fe0872Smatthias.ringwald #include <stdio.h> 431f504dbdSmatthias.ringwald #include "hci.h" 44d8905019Smatthias.ringwald #include "hci_dump.h" 4593b8dc03Smatthias.ringwald 461e6aba47Smatthias.ringwald // temp 471e6aba47Smatthias.ringwald #include "l2cap.h" 481e6aba47Smatthias.ringwald 49c21e6239Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 5000 50ee091cf1Smatthias.ringwald 5106b35ec0Smatthias.ringwald // the STACK is here 5216833f0aSmatthias.ringwald static hci_stack_t hci_stack; 5316833f0aSmatthias.ringwald 5497addcc5Smatthias.ringwald /** 55ee091cf1Smatthias.ringwald * get connection for a given handle 56ee091cf1Smatthias.ringwald * 57ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 58ee091cf1Smatthias.ringwald */ 59ee091cf1Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 60ee091cf1Smatthias.ringwald linked_item_t *it; 61ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 62ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 63ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 64ee091cf1Smatthias.ringwald } 65ee091cf1Smatthias.ringwald } 66ee091cf1Smatthias.ringwald return NULL; 67ee091cf1Smatthias.ringwald } 68ee091cf1Smatthias.ringwald 69981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 70ee091cf1Smatthias.ringwald hci_connection_t * connection = linked_item_get_user(&timer->item); 71ee091cf1Smatthias.ringwald struct timeval tv; 72ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 73c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 74ee091cf1Smatthias.ringwald // connections might be timed out 75ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 76c21e6239Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 77ee091cf1Smatthias.ringwald } else { 78ee091cf1Smatthias.ringwald // next timeout check at 79c21e6239Smatthias.ringwald timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000; 80ee091cf1Smatthias.ringwald } 81ee091cf1Smatthias.ringwald run_loop_add_timer(timer); 82ee091cf1Smatthias.ringwald } 83ee091cf1Smatthias.ringwald 84ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 85ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 86ee091cf1Smatthias.ringwald } 87ee091cf1Smatthias.ringwald 88ee091cf1Smatthias.ringwald static void hci_connection_update_timestamp_for_acl(uint8_t *packet) { 89ee091cf1Smatthias.ringwald // update timestamp 90ee091cf1Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 91ee091cf1Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 92ee091cf1Smatthias.ringwald if (connection) hci_connection_timestamp(connection); 93ee091cf1Smatthias.ringwald } 94ee091cf1Smatthias.ringwald 95ee091cf1Smatthias.ringwald /** 96c8e4258aSmatthias.ringwald * create connection for given address 97c8e4258aSmatthias.ringwald * 98c8e4258aSmatthias.ringwald * @return connection OR NULL, if not found 99c8e4258aSmatthias.ringwald */ 100c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 101c8e4258aSmatthias.ringwald hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 102c8e4258aSmatthias.ringwald if (!conn) return NULL; 103c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 104c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 105c8e4258aSmatthias.ringwald conn->flags = 0; 106ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 107ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 108ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 109c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 110c8e4258aSmatthias.ringwald return conn; 111c8e4258aSmatthias.ringwald } 112c8e4258aSmatthias.ringwald 113c8e4258aSmatthias.ringwald /** 11406b35ec0Smatthias.ringwald * get connection for given address 11597addcc5Smatthias.ringwald * 11697addcc5Smatthias.ringwald * @return connection OR NULL, if not found 11797addcc5Smatthias.ringwald */ 118fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 11906b35ec0Smatthias.ringwald linked_item_t *it; 12006b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 12106b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 12206b35ec0Smatthias.ringwald return (hci_connection_t *) it; 12306b35ec0Smatthias.ringwald } 12406b35ec0Smatthias.ringwald } 12506b35ec0Smatthias.ringwald return NULL; 12606b35ec0Smatthias.ringwald } 12706b35ec0Smatthias.ringwald 12843bfb1bdSmatthias.ringwald /** 12943bfb1bdSmatthias.ringwald * count connections 13043bfb1bdSmatthias.ringwald */ 13143bfb1bdSmatthias.ringwald static int nr_hci_connections(){ 13256c253c9Smatthias.ringwald int count = 0; 13343bfb1bdSmatthias.ringwald linked_item_t *it; 13443bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 13543bfb1bdSmatthias.ringwald return count; 13643bfb1bdSmatthias.ringwald } 137c8e4258aSmatthias.ringwald 13897addcc5Smatthias.ringwald /** 139ba681a6cSmatthias.ringwald * Dummy handler called by HCI 14016833f0aSmatthias.ringwald */ 1411cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){ 14216833f0aSmatthias.ringwald } 14316833f0aSmatthias.ringwald 144ba681a6cSmatthias.ringwald /** 145ba681a6cSmatthias.ringwald * Dummy control handler 146ba681a6cSmatthias.ringwald */ 147ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 148ba681a6cSmatthias.ringwald return 0; 149ba681a6cSmatthias.ringwald } 150ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 151ba681a6cSmatthias.ringwald return "Hardware unknown"; 152ba681a6cSmatthias.ringwald } 153ba681a6cSmatthias.ringwald static bt_control_t null_control = { 154ba681a6cSmatthias.ringwald null_control_function, 155ba681a6cSmatthias.ringwald null_control_function, 156ba681a6cSmatthias.ringwald null_control_function, 157ba681a6cSmatthias.ringwald null_control_name 158ba681a6cSmatthias.ringwald }; 159ba681a6cSmatthias.ringwald 160c8e4258aSmatthias.ringwald 161ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 162ee091cf1Smatthias.ringwald hci_connection_update_timestamp_for_acl(packet); 163ee091cf1Smatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 164ee091cf1Smatthias.ringwald } 165ee091cf1Smatthias.ringwald 16616833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 167ee091cf1Smatthias.ringwald hci_connection_update_timestamp_for_acl(packet); 16816833f0aSmatthias.ringwald hci_stack.acl_packet_handler(packet, size); 16994ab26f8Smatthias.ringwald 17094ab26f8Smatthias.ringwald // execute main loop 17194ab26f8Smatthias.ringwald hci_run(); 17216833f0aSmatthias.ringwald } 17322909952Smatthias.ringwald 17416833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1751281a47eSmatthias.ringwald bd_addr_t addr; 176fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 17722909952Smatthias.ringwald 1783429f56bSmatthias.ringwald // Get Num_HCI_Command_Packets 1793429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || 1803429f56bSmatthias.ringwald packet[0] == HCI_EVENT_COMMAND_STATUS){ 1813429f56bSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 18222909952Smatthias.ringwald } 18322909952Smatthias.ringwald 184fe1ed1b8Smatthias.ringwald // Connection management 185fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { 186fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 187*b448a0e7Smatthias.ringwald printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); 188fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 189fe1ed1b8Smatthias.ringwald if (conn) { 190*b448a0e7Smatthias.ringwald if (!packet[2]){ 191c8e4258aSmatthias.ringwald conn->state = OPEN; 192fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 193fe1ed1b8Smatthias.ringwald conn->flags = 0; 194ee091cf1Smatthias.ringwald 195ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 196c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 197ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 198ee091cf1Smatthias.ringwald 199fe1ed1b8Smatthias.ringwald printf("New connection: handle %u, ", conn->con_handle); 200fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 201fe1ed1b8Smatthias.ringwald printf("\n"); 20243bfb1bdSmatthias.ringwald 20343bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 204*b448a0e7Smatthias.ringwald } else { 205*b448a0e7Smatthias.ringwald // connection failed, remove entry 206*b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 207*b448a0e7Smatthias.ringwald free( conn ); 208fe1ed1b8Smatthias.ringwald } 209fe1ed1b8Smatthias.ringwald } 210fe1ed1b8Smatthias.ringwald } 211fe1ed1b8Smatthias.ringwald 212fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { 213fe1ed1b8Smatthias.ringwald if (!packet[2]){ 214fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 215fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 216fe1ed1b8Smatthias.ringwald if (conn) { 217fe1ed1b8Smatthias.ringwald printf("Connection closed: handle %u, ", conn->con_handle); 218fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 219fe1ed1b8Smatthias.ringwald printf("\n"); 220ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 221fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 222fe1ed1b8Smatthias.ringwald free( conn ); 22343bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 224fe1ed1b8Smatthias.ringwald } 225fe1ed1b8Smatthias.ringwald } 226fe1ed1b8Smatthias.ringwald } 227fe1ed1b8Smatthias.ringwald 2283429f56bSmatthias.ringwald // handle BT initialization 2293429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 2307301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 2317301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 2327301ad89Smatthias.ringwald // hci_stack.substate = 0; 2337301ad89Smatthias.ringwald // } 2347301ad89Smatthias.ringwald // handle normal init sequence 2353429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 2363429f56bSmatthias.ringwald // odd: waiting for event 2373429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 2383429f56bSmatthias.ringwald hci_stack.substate++; 2393429f56bSmatthias.ringwald } 2403429f56bSmatthias.ringwald } 24122909952Smatthias.ringwald } 24222909952Smatthias.ringwald 2431281a47eSmatthias.ringwald // link key request 2443429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){ 2451281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 2461281a47eSmatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 2471281a47eSmatthias.ringwald return; 2481281a47eSmatthias.ringwald } 2491281a47eSmatthias.ringwald 25016833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 25194ab26f8Smatthias.ringwald 25294ab26f8Smatthias.ringwald // execute main loop 25394ab26f8Smatthias.ringwald hci_run(); 25416833f0aSmatthias.ringwald } 25516833f0aSmatthias.ringwald 256fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 2571cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 25816833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 25916833f0aSmatthias.ringwald } 2601cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 26116833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 26216833f0aSmatthias.ringwald } 26316833f0aSmatthias.ringwald 26411e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 265475c8125Smatthias.ringwald 266475c8125Smatthias.ringwald // reference to use transport layer implementation 26716833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 268475c8125Smatthias.ringwald 26911e23e5fSmatthias.ringwald // references to used control implementation 2707301ad89Smatthias.ringwald if (control) { 27111e23e5fSmatthias.ringwald hci_stack.control = control; 2727301ad89Smatthias.ringwald } else { 2737301ad89Smatthias.ringwald hci_stack.control = &null_control; 2747301ad89Smatthias.ringwald } 27511e23e5fSmatthias.ringwald 27611e23e5fSmatthias.ringwald // reference to used config 27711e23e5fSmatthias.ringwald hci_stack.config = config; 27811e23e5fSmatthias.ringwald 279fe1ed1b8Smatthias.ringwald // no connections yet 280fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 281fe1ed1b8Smatthias.ringwald 28202ea9861Smatthias.ringwald // empty cmd buffer 28316833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 28416833f0aSmatthias.ringwald 28516833f0aSmatthias.ringwald // higher level handler 28616833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 28716833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 28816833f0aSmatthias.ringwald 28916833f0aSmatthias.ringwald // register packet handlers with transport 29016833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 29116833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 292475c8125Smatthias.ringwald } 293475c8125Smatthias.ringwald 294475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 295f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 2967301ad89Smatthias.ringwald 297038bc64cSmatthias.ringwald // power on 298038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 299038bc64cSmatthias.ringwald if (err){ 300ac2e07f8Smatthias.ringwald fprintf(stderr, "POWER_ON failed\n"); 301038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 302038bc64cSmatthias.ringwald return err; 303038bc64cSmatthias.ringwald } 304038bc64cSmatthias.ringwald 305038bc64cSmatthias.ringwald // open low-level device 306038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 307038bc64cSmatthias.ringwald if (err){ 308ac2e07f8Smatthias.ringwald fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 309038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 310038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 311038bc64cSmatthias.ringwald return err; 312038bc64cSmatthias.ringwald } 313038bc64cSmatthias.ringwald 3147301ad89Smatthias.ringwald // set up state machine 3157301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 3167301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 3177301ad89Smatthias.ringwald hci_stack.substate = 0; 3187301ad89Smatthias.ringwald 319f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 3207301ad89Smatthias.ringwald 3217301ad89Smatthias.ringwald // close low-level device 3227301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 3237301ad89Smatthias.ringwald 3247301ad89Smatthias.ringwald // power off 32511e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 32643bfb1bdSmatthias.ringwald 32743bfb1bdSmatthias.ringwald // we're off now 32843bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 32911e23e5fSmatthias.ringwald } 33068d92d03Smatthias.ringwald 331038bc64cSmatthias.ringwald // create internal event 332ee8bf225Smatthias.ringwald hci_emit_state(); 333ee8bf225Smatthias.ringwald 33468d92d03Smatthias.ringwald // trigger next/first action 33568d92d03Smatthias.ringwald hci_run(); 33668d92d03Smatthias.ringwald 337475c8125Smatthias.ringwald return 0; 338475c8125Smatthias.ringwald } 339475c8125Smatthias.ringwald 34006b35ec0Smatthias.ringwald void hci_run(){ 3413429f56bSmatthias.ringwald switch (hci_stack.state){ 3423429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 3433429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 3443429f56bSmatthias.ringwald // odd: waiting for command completion 34506b35ec0Smatthias.ringwald return; 3463429f56bSmatthias.ringwald } 3473429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 3483429f56bSmatthias.ringwald // cannot send command yet 34906b35ec0Smatthias.ringwald return; 3503429f56bSmatthias.ringwald } 3513429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 3523429f56bSmatthias.ringwald case 0: 35322909952Smatthias.ringwald hci_send_cmd(&hci_reset); 3543429f56bSmatthias.ringwald break; 3553429f56bSmatthias.ringwald case 1: 356f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 357f432a6ddSmatthias.ringwald break; 358f432a6ddSmatthias.ringwald case 2: 3593429f56bSmatthias.ringwald // ca. 15 sec 3603429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 3613429f56bSmatthias.ringwald break; 362f432a6ddSmatthias.ringwald case 3: 363bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 364f432a6ddSmatthias.ringwald break; 365f432a6ddSmatthias.ringwald case 4: 3663429f56bSmatthias.ringwald // done. 3673429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 368b360b6adSmatthias.ringwald hci_emit_state(); 3693429f56bSmatthias.ringwald break; 3703429f56bSmatthias.ringwald default: 3713429f56bSmatthias.ringwald break; 372475c8125Smatthias.ringwald } 3733429f56bSmatthias.ringwald hci_stack.substate++; 3743429f56bSmatthias.ringwald break; 3753429f56bSmatthias.ringwald default: 3763429f56bSmatthias.ringwald break; 3771f504dbdSmatthias.ringwald } 3783429f56bSmatthias.ringwald } 37916833f0aSmatthias.ringwald 38031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 381c8e4258aSmatthias.ringwald bd_addr_t addr; 382c8e4258aSmatthias.ringwald hci_connection_t * conn; 383c8e4258aSmatthias.ringwald // house-keeping 384c8e4258aSmatthias.ringwald 385c8e4258aSmatthias.ringwald // create_connection? 386c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 387c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 388c8e4258aSmatthias.ringwald printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 389c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 390c8e4258aSmatthias.ringwald if (conn) { 391c8e4258aSmatthias.ringwald // if connection exists 392c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 393c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 394c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 395c8e4258aSmatthias.ringwald } 396c8e4258aSmatthias.ringwald // otherwise, just ignore 397c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 398c8e4258aSmatthias.ringwald 399c8e4258aSmatthias.ringwald } else{ 400c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 401c8e4258aSmatthias.ringwald if (conn){ 402c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 403c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 404c8e4258aSmatthias.ringwald } 405c8e4258aSmatthias.ringwald } 406c8e4258aSmatthias.ringwald } 407c8e4258aSmatthias.ringwald 408c8e4258aSmatthias.ringwald // accept connection 409c8e4258aSmatthias.ringwald 410c8e4258aSmatthias.ringwald // reject connection 411c8e4258aSmatthias.ringwald 412c8e4258aSmatthias.ringwald // close_connection? 413c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 414c8e4258aSmatthias.ringwald 41531452debSmatthias.ringwald hci_stack.num_cmd_packets--; 41631452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 41731452debSmatthias.ringwald } 4188adf0ddaSmatthias.ringwald 4191cd208adSmatthias.ringwald /** 4201cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 4211cd208adSmatthias.ringwald */ 4221cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 4231cd208adSmatthias.ringwald va_list argptr; 4241cd208adSmatthias.ringwald va_start(argptr, cmd); 4251cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 4261cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 4271cd208adSmatthias.ringwald va_end(argptr); 4281cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 42993b8dc03Smatthias.ringwald } 430c8e4258aSmatthias.ringwald 431ee091cf1Smatthias.ringwald // Create various non-HCI events. 432ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 433ee091cf1Smatthias.ringwald 434c8e4258aSmatthias.ringwald void hci_emit_state(){ 435c8e4258aSmatthias.ringwald uint8_t len = 3; 436c8e4258aSmatthias.ringwald uint8_t event[len]; 43780d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 438c8e4258aSmatthias.ringwald event[1] = 1; 439c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 440c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 441c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 442c8e4258aSmatthias.ringwald } 443c8e4258aSmatthias.ringwald 444c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 445c8e4258aSmatthias.ringwald uint8_t len = 13; 446c8e4258aSmatthias.ringwald uint8_t event[len]; 447c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 448c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 449c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 450c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 451c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 452c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 453c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 454c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 455c8e4258aSmatthias.ringwald } 456c8e4258aSmatthias.ringwald 457ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 458ee091cf1Smatthias.ringwald uint8_t len = 4; 459ee091cf1Smatthias.ringwald uint8_t event[len]; 46080d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 461ee091cf1Smatthias.ringwald event[1] = 2; 462ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 463ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 464ee091cf1Smatthias.ringwald hci_stack.event_packet_handler(event, len); 465ee091cf1Smatthias.ringwald } 46643bfb1bdSmatthias.ringwald 46743bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 46843bfb1bdSmatthias.ringwald uint8_t len = 3; 46943bfb1bdSmatthias.ringwald uint8_t event[len]; 47080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 47143bfb1bdSmatthias.ringwald event[1] = 1; 47243bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 47343bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 47443bfb1bdSmatthias.ringwald hci_stack.event_packet_handler(event, len); 47543bfb1bdSmatthias.ringwald } 476038bc64cSmatthias.ringwald 477038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 478038bc64cSmatthias.ringwald uint8_t len = 1; 479038bc64cSmatthias.ringwald uint8_t event[len]; 48080d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 481038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 482038bc64cSmatthias.ringwald hci_stack.event_packet_handler(event, len); 483038bc64cSmatthias.ringwald } 484