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 46*1b0e3922Smatthias.ringwald #include <btstack/version.h> 47*1b0e3922Smatthias.ringwald 481e6aba47Smatthias.ringwald // temp 491e6aba47Smatthias.ringwald #include "l2cap.h" 501e6aba47Smatthias.ringwald 51c21e6239Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT_MS 5000 52ee091cf1Smatthias.ringwald 5306b35ec0Smatthias.ringwald // the STACK is here 5416833f0aSmatthias.ringwald static hci_stack_t hci_stack; 5516833f0aSmatthias.ringwald 5697addcc5Smatthias.ringwald /** 57ee091cf1Smatthias.ringwald * get connection for a given handle 58ee091cf1Smatthias.ringwald * 59ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 60ee091cf1Smatthias.ringwald */ 61ee091cf1Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 62ee091cf1Smatthias.ringwald linked_item_t *it; 63ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 64ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 65ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 66ee091cf1Smatthias.ringwald } 67ee091cf1Smatthias.ringwald } 68ee091cf1Smatthias.ringwald return NULL; 69ee091cf1Smatthias.ringwald } 70ee091cf1Smatthias.ringwald 71981eb02eSmatthias.ringwald static void hci_connection_timeout_handler(timer_source_t *timer){ 72ee091cf1Smatthias.ringwald hci_connection_t * connection = linked_item_get_user(&timer->item); 73ee091cf1Smatthias.ringwald struct timeval tv; 74ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 75c21e6239Smatthias.ringwald if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 76ee091cf1Smatthias.ringwald // connections might be timed out 77ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 78c21e6239Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 79ee091cf1Smatthias.ringwald } else { 80ee091cf1Smatthias.ringwald // next timeout check at 81c21e6239Smatthias.ringwald timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000; 82ee091cf1Smatthias.ringwald } 83ee091cf1Smatthias.ringwald run_loop_add_timer(timer); 84ee091cf1Smatthias.ringwald } 85ee091cf1Smatthias.ringwald 86ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 87ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 88ee091cf1Smatthias.ringwald } 89ee091cf1Smatthias.ringwald 90ee091cf1Smatthias.ringwald static void hci_connection_update_timestamp_for_acl(uint8_t *packet) { 91ee091cf1Smatthias.ringwald // update timestamp 92ee091cf1Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 93ee091cf1Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 94ee091cf1Smatthias.ringwald if (connection) hci_connection_timestamp(connection); 95ee091cf1Smatthias.ringwald } 96ee091cf1Smatthias.ringwald 97ee091cf1Smatthias.ringwald /** 98c8e4258aSmatthias.ringwald * create connection for given address 99c8e4258aSmatthias.ringwald * 100c8e4258aSmatthias.ringwald * @return connection OR NULL, if not found 101c8e4258aSmatthias.ringwald */ 102c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 103c8e4258aSmatthias.ringwald hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 104c8e4258aSmatthias.ringwald if (!conn) return NULL; 105c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 106c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 107c8e4258aSmatthias.ringwald conn->flags = 0; 108ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 109ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 110ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 111c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 112c8e4258aSmatthias.ringwald return conn; 113c8e4258aSmatthias.ringwald } 114c8e4258aSmatthias.ringwald 115c8e4258aSmatthias.ringwald /** 11606b35ec0Smatthias.ringwald * get connection for given address 11797addcc5Smatthias.ringwald * 11897addcc5Smatthias.ringwald * @return connection OR NULL, if not found 11997addcc5Smatthias.ringwald */ 120fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 12106b35ec0Smatthias.ringwald linked_item_t *it; 12206b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 12306b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 12406b35ec0Smatthias.ringwald return (hci_connection_t *) it; 12506b35ec0Smatthias.ringwald } 12606b35ec0Smatthias.ringwald } 12706b35ec0Smatthias.ringwald return NULL; 12806b35ec0Smatthias.ringwald } 12906b35ec0Smatthias.ringwald 13043bfb1bdSmatthias.ringwald /** 13143bfb1bdSmatthias.ringwald * count connections 13243bfb1bdSmatthias.ringwald */ 13343bfb1bdSmatthias.ringwald static int nr_hci_connections(){ 13456c253c9Smatthias.ringwald int count = 0; 13543bfb1bdSmatthias.ringwald linked_item_t *it; 13643bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 13743bfb1bdSmatthias.ringwald return count; 13843bfb1bdSmatthias.ringwald } 139c8e4258aSmatthias.ringwald 14097addcc5Smatthias.ringwald /** 141ba681a6cSmatthias.ringwald * Dummy handler called by HCI 14216833f0aSmatthias.ringwald */ 1431cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){ 14416833f0aSmatthias.ringwald } 14516833f0aSmatthias.ringwald 146ba681a6cSmatthias.ringwald /** 147ba681a6cSmatthias.ringwald * Dummy control handler 148ba681a6cSmatthias.ringwald */ 149ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 150ba681a6cSmatthias.ringwald return 0; 151ba681a6cSmatthias.ringwald } 152ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 153ba681a6cSmatthias.ringwald return "Hardware unknown"; 154ba681a6cSmatthias.ringwald } 155ba681a6cSmatthias.ringwald static bt_control_t null_control = { 156ba681a6cSmatthias.ringwald null_control_function, 157ba681a6cSmatthias.ringwald null_control_function, 158ba681a6cSmatthias.ringwald null_control_function, 159ba681a6cSmatthias.ringwald null_control_name 160ba681a6cSmatthias.ringwald }; 161ba681a6cSmatthias.ringwald 162c8e4258aSmatthias.ringwald 163ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 164ee091cf1Smatthias.ringwald hci_connection_update_timestamp_for_acl(packet); 165ee091cf1Smatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 166ee091cf1Smatthias.ringwald } 167ee091cf1Smatthias.ringwald 16816833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 169ee091cf1Smatthias.ringwald hci_connection_update_timestamp_for_acl(packet); 17016833f0aSmatthias.ringwald hci_stack.acl_packet_handler(packet, size); 17194ab26f8Smatthias.ringwald 17294ab26f8Smatthias.ringwald // execute main loop 17394ab26f8Smatthias.ringwald hci_run(); 17416833f0aSmatthias.ringwald } 17522909952Smatthias.ringwald 17616833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1771281a47eSmatthias.ringwald bd_addr_t addr; 178fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 17922909952Smatthias.ringwald 1806772a24cSmatthias.ringwald switch (packet[0]) { 18122909952Smatthias.ringwald 1826772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 1836772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 1846772a24cSmatthias.ringwald // Get Num_HCI_Command_Packets 1856772a24cSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 1866772a24cSmatthias.ringwald break; 1876772a24cSmatthias.ringwald 1886772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 189fe1ed1b8Smatthias.ringwald // Connection management 190fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 191b448a0e7Smatthias.ringwald printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); 192fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 193fe1ed1b8Smatthias.ringwald if (conn) { 194b448a0e7Smatthias.ringwald if (!packet[2]){ 195c8e4258aSmatthias.ringwald conn->state = OPEN; 196fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 197fe1ed1b8Smatthias.ringwald conn->flags = 0; 198ee091cf1Smatthias.ringwald 199ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 200c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 201ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 202ee091cf1Smatthias.ringwald 203fe1ed1b8Smatthias.ringwald printf("New connection: handle %u, ", conn->con_handle); 204fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 205fe1ed1b8Smatthias.ringwald printf("\n"); 20643bfb1bdSmatthias.ringwald 20743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 208b448a0e7Smatthias.ringwald } else { 209b448a0e7Smatthias.ringwald // connection failed, remove entry 210b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 211b448a0e7Smatthias.ringwald free( conn ); 212fe1ed1b8Smatthias.ringwald } 213fe1ed1b8Smatthias.ringwald } 2146772a24cSmatthias.ringwald break; 215fe1ed1b8Smatthias.ringwald 2166772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 217fe1ed1b8Smatthias.ringwald if (!packet[2]){ 218fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 219fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 220fe1ed1b8Smatthias.ringwald if (conn) { 221fe1ed1b8Smatthias.ringwald printf("Connection closed: handle %u, ", conn->con_handle); 222fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 223fe1ed1b8Smatthias.ringwald printf("\n"); 224ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 225fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 226fe1ed1b8Smatthias.ringwald free( conn ); 22743bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 228fe1ed1b8Smatthias.ringwald } 229fe1ed1b8Smatthias.ringwald } 2306772a24cSmatthias.ringwald break; 2316772a24cSmatthias.ringwald 2326772a24cSmatthias.ringwald default: 2336772a24cSmatthias.ringwald break; 234fe1ed1b8Smatthias.ringwald } 235fe1ed1b8Smatthias.ringwald 2363429f56bSmatthias.ringwald // handle BT initialization 2373429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 2387301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 2397301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 2407301ad89Smatthias.ringwald // hci_stack.substate = 0; 2417301ad89Smatthias.ringwald // } 2427301ad89Smatthias.ringwald // handle normal init sequence 2433429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 2443429f56bSmatthias.ringwald // odd: waiting for event 2453429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 2463429f56bSmatthias.ringwald hci_stack.substate++; 2473429f56bSmatthias.ringwald } 2483429f56bSmatthias.ringwald } 24922909952Smatthias.ringwald } 25022909952Smatthias.ringwald 25116833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 25294ab26f8Smatthias.ringwald 25394ab26f8Smatthias.ringwald // execute main loop 25494ab26f8Smatthias.ringwald hci_run(); 25516833f0aSmatthias.ringwald } 25616833f0aSmatthias.ringwald 257fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 2581cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 25916833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 26016833f0aSmatthias.ringwald } 2611cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 26216833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 26316833f0aSmatthias.ringwald } 26416833f0aSmatthias.ringwald 26511e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 266475c8125Smatthias.ringwald 267475c8125Smatthias.ringwald // reference to use transport layer implementation 26816833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 269475c8125Smatthias.ringwald 27011e23e5fSmatthias.ringwald // references to used control implementation 2717301ad89Smatthias.ringwald if (control) { 27211e23e5fSmatthias.ringwald hci_stack.control = control; 2737301ad89Smatthias.ringwald } else { 2747301ad89Smatthias.ringwald hci_stack.control = &null_control; 2757301ad89Smatthias.ringwald } 27611e23e5fSmatthias.ringwald 27711e23e5fSmatthias.ringwald // reference to used config 27811e23e5fSmatthias.ringwald hci_stack.config = config; 27911e23e5fSmatthias.ringwald 280fe1ed1b8Smatthias.ringwald // no connections yet 281fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 282fe1ed1b8Smatthias.ringwald 28302ea9861Smatthias.ringwald // empty cmd buffer 28416833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 28516833f0aSmatthias.ringwald 28616833f0aSmatthias.ringwald // higher level handler 28716833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 28816833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 28916833f0aSmatthias.ringwald 29016833f0aSmatthias.ringwald // register packet handlers with transport 29116833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 29216833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 293475c8125Smatthias.ringwald } 294475c8125Smatthias.ringwald 295475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 296f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 2977301ad89Smatthias.ringwald 298038bc64cSmatthias.ringwald // power on 299038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 300038bc64cSmatthias.ringwald if (err){ 301ac2e07f8Smatthias.ringwald fprintf(stderr, "POWER_ON failed\n"); 302038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 303038bc64cSmatthias.ringwald return err; 304038bc64cSmatthias.ringwald } 305038bc64cSmatthias.ringwald 306038bc64cSmatthias.ringwald // open low-level device 307038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 308038bc64cSmatthias.ringwald if (err){ 309ac2e07f8Smatthias.ringwald fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 310038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 311038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 312038bc64cSmatthias.ringwald return err; 313038bc64cSmatthias.ringwald } 314038bc64cSmatthias.ringwald 3157301ad89Smatthias.ringwald // set up state machine 3167301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 3177301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 3187301ad89Smatthias.ringwald hci_stack.substate = 0; 3197301ad89Smatthias.ringwald 320f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 3217301ad89Smatthias.ringwald 3227301ad89Smatthias.ringwald // close low-level device 3237301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 3247301ad89Smatthias.ringwald 3257301ad89Smatthias.ringwald // power off 32611e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 32743bfb1bdSmatthias.ringwald 32843bfb1bdSmatthias.ringwald // we're off now 32943bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 33011e23e5fSmatthias.ringwald } 33168d92d03Smatthias.ringwald 332038bc64cSmatthias.ringwald // create internal event 333ee8bf225Smatthias.ringwald hci_emit_state(); 334ee8bf225Smatthias.ringwald 33568d92d03Smatthias.ringwald // trigger next/first action 33668d92d03Smatthias.ringwald hci_run(); 33768d92d03Smatthias.ringwald 338475c8125Smatthias.ringwald return 0; 339475c8125Smatthias.ringwald } 340475c8125Smatthias.ringwald 34106b35ec0Smatthias.ringwald void hci_run(){ 3423429f56bSmatthias.ringwald switch (hci_stack.state){ 3433429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 3443429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 3453429f56bSmatthias.ringwald // odd: waiting for command completion 34606b35ec0Smatthias.ringwald return; 3473429f56bSmatthias.ringwald } 3483429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 3493429f56bSmatthias.ringwald // cannot send command yet 35006b35ec0Smatthias.ringwald return; 3513429f56bSmatthias.ringwald } 3523429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 3533429f56bSmatthias.ringwald case 0: 35422909952Smatthias.ringwald hci_send_cmd(&hci_reset); 3553429f56bSmatthias.ringwald break; 3563429f56bSmatthias.ringwald case 1: 357f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 358f432a6ddSmatthias.ringwald break; 359f432a6ddSmatthias.ringwald case 2: 3603429f56bSmatthias.ringwald // ca. 15 sec 3613429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 3623429f56bSmatthias.ringwald break; 363f432a6ddSmatthias.ringwald case 3: 364bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 365f432a6ddSmatthias.ringwald break; 366f432a6ddSmatthias.ringwald case 4: 3673429f56bSmatthias.ringwald // done. 3683429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 369b360b6adSmatthias.ringwald hci_emit_state(); 3703429f56bSmatthias.ringwald break; 3713429f56bSmatthias.ringwald default: 3723429f56bSmatthias.ringwald break; 373475c8125Smatthias.ringwald } 3743429f56bSmatthias.ringwald hci_stack.substate++; 3753429f56bSmatthias.ringwald break; 3763429f56bSmatthias.ringwald default: 3773429f56bSmatthias.ringwald break; 3781f504dbdSmatthias.ringwald } 3793429f56bSmatthias.ringwald } 38016833f0aSmatthias.ringwald 38131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 382c8e4258aSmatthias.ringwald bd_addr_t addr; 383c8e4258aSmatthias.ringwald hci_connection_t * conn; 384c8e4258aSmatthias.ringwald // house-keeping 385c8e4258aSmatthias.ringwald 386c8e4258aSmatthias.ringwald // create_connection? 387c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 388c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 389c8e4258aSmatthias.ringwald printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 390c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 391c8e4258aSmatthias.ringwald if (conn) { 392c8e4258aSmatthias.ringwald // if connection exists 393c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 394c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 395c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 396c8e4258aSmatthias.ringwald } 397c8e4258aSmatthias.ringwald // otherwise, just ignore 398c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 399c8e4258aSmatthias.ringwald 400c8e4258aSmatthias.ringwald } else{ 401c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 402c8e4258aSmatthias.ringwald if (conn){ 403c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 404c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 405c8e4258aSmatthias.ringwald } 406c8e4258aSmatthias.ringwald } 407c8e4258aSmatthias.ringwald } 408c8e4258aSmatthias.ringwald 409c8e4258aSmatthias.ringwald // accept connection 410c8e4258aSmatthias.ringwald 411c8e4258aSmatthias.ringwald // reject connection 412c8e4258aSmatthias.ringwald 413c8e4258aSmatthias.ringwald // close_connection? 414c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 415c8e4258aSmatthias.ringwald 41631452debSmatthias.ringwald hci_stack.num_cmd_packets--; 41731452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 41831452debSmatthias.ringwald } 4198adf0ddaSmatthias.ringwald 4201cd208adSmatthias.ringwald /** 4211cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 4221cd208adSmatthias.ringwald */ 4231cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 4241cd208adSmatthias.ringwald va_list argptr; 4251cd208adSmatthias.ringwald va_start(argptr, cmd); 4261cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 4271cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 4281cd208adSmatthias.ringwald va_end(argptr); 4291cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 43093b8dc03Smatthias.ringwald } 431c8e4258aSmatthias.ringwald 432ee091cf1Smatthias.ringwald // Create various non-HCI events. 433ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 434ee091cf1Smatthias.ringwald 435c8e4258aSmatthias.ringwald void hci_emit_state(){ 436c8e4258aSmatthias.ringwald uint8_t len = 3; 437c8e4258aSmatthias.ringwald uint8_t event[len]; 43880d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 439*1b0e3922Smatthias.ringwald event[1] = len - 3; 440c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 441c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 442c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 443c8e4258aSmatthias.ringwald } 444c8e4258aSmatthias.ringwald 445c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 446c8e4258aSmatthias.ringwald uint8_t len = 13; 447c8e4258aSmatthias.ringwald uint8_t event[len]; 448c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 449*1b0e3922Smatthias.ringwald event[1] = len - 3; 450c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 451c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 452c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 453c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 454c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 455c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 456c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 457c8e4258aSmatthias.ringwald } 458c8e4258aSmatthias.ringwald 459ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 460ee091cf1Smatthias.ringwald uint8_t len = 4; 461ee091cf1Smatthias.ringwald uint8_t event[len]; 46280d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 463*1b0e3922Smatthias.ringwald event[1] = len - 2; 464ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 465ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 466ee091cf1Smatthias.ringwald hci_stack.event_packet_handler(event, len); 467ee091cf1Smatthias.ringwald } 46843bfb1bdSmatthias.ringwald 46943bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 47043bfb1bdSmatthias.ringwald uint8_t len = 3; 47143bfb1bdSmatthias.ringwald uint8_t event[len]; 47280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 473*1b0e3922Smatthias.ringwald event[1] = len - 2; 47443bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 47543bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 47643bfb1bdSmatthias.ringwald hci_stack.event_packet_handler(event, len); 47743bfb1bdSmatthias.ringwald } 478038bc64cSmatthias.ringwald 479038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 480*1b0e3922Smatthias.ringwald uint8_t len = 2; 481038bc64cSmatthias.ringwald uint8_t event[len]; 48280d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 483*1b0e3922Smatthias.ringwald event[1] = len - 2; 484038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 485038bc64cSmatthias.ringwald hci_stack.event_packet_handler(event, len); 486038bc64cSmatthias.ringwald } 487*1b0e3922Smatthias.ringwald 488*1b0e3922Smatthias.ringwald 489*1b0e3922Smatthias.ringwald void hci_emit_btstack_version() { 490*1b0e3922Smatthias.ringwald uint8_t len = 6; 491*1b0e3922Smatthias.ringwald uint8_t event[len]; 492*1b0e3922Smatthias.ringwald event[0] = BTSTACK_EVENT_VERSION; 493*1b0e3922Smatthias.ringwald event[1] = len - 2; 494*1b0e3922Smatthias.ringwald event[len++] = BTSTACK_MAJOR; 495*1b0e3922Smatthias.ringwald event[len++] = BTSTACK_MINOR; 496*1b0e3922Smatthias.ringwald bt_store_16(event, len, BTSTACK_REVISION); 497*1b0e3922Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 498*1b0e3922Smatthias.ringwald hci_stack.event_packet_handler(event, len); 499*1b0e3922Smatthias.ringwald } 500*1b0e3922Smatthias.ringwald 501