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 178*6772a24cSmatthias.ringwald switch (packet[0]) { 17922909952Smatthias.ringwald 180*6772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_COMPLETE: 181*6772a24cSmatthias.ringwald case HCI_EVENT_COMMAND_STATUS: 182*6772a24cSmatthias.ringwald // Get Num_HCI_Command_Packets 183*6772a24cSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 184*6772a24cSmatthias.ringwald break; 185*6772a24cSmatthias.ringwald 186*6772a24cSmatthias.ringwald case HCI_EVENT_CONNECTION_COMPLETE: 187fe1ed1b8Smatthias.ringwald // Connection management 188fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 189b448a0e7Smatthias.ringwald printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); 190fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 191fe1ed1b8Smatthias.ringwald if (conn) { 192b448a0e7Smatthias.ringwald if (!packet[2]){ 193c8e4258aSmatthias.ringwald conn->state = OPEN; 194fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 195fe1ed1b8Smatthias.ringwald conn->flags = 0; 196ee091cf1Smatthias.ringwald 197ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 198c21e6239Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 199ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 200ee091cf1Smatthias.ringwald 201fe1ed1b8Smatthias.ringwald printf("New connection: handle %u, ", conn->con_handle); 202fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 203fe1ed1b8Smatthias.ringwald printf("\n"); 20443bfb1bdSmatthias.ringwald 20543bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 206b448a0e7Smatthias.ringwald } else { 207b448a0e7Smatthias.ringwald // connection failed, remove entry 208b448a0e7Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 209b448a0e7Smatthias.ringwald free( conn ); 210fe1ed1b8Smatthias.ringwald } 211fe1ed1b8Smatthias.ringwald } 212*6772a24cSmatthias.ringwald break; 213fe1ed1b8Smatthias.ringwald 214*6772a24cSmatthias.ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 215fe1ed1b8Smatthias.ringwald if (!packet[2]){ 216fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 217fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 218fe1ed1b8Smatthias.ringwald if (conn) { 219fe1ed1b8Smatthias.ringwald printf("Connection closed: handle %u, ", conn->con_handle); 220fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 221fe1ed1b8Smatthias.ringwald printf("\n"); 222ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 223fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 224fe1ed1b8Smatthias.ringwald free( conn ); 22543bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 226fe1ed1b8Smatthias.ringwald } 227fe1ed1b8Smatthias.ringwald } 228*6772a24cSmatthias.ringwald break; 229*6772a24cSmatthias.ringwald 230*6772a24cSmatthias.ringwald default: 231*6772a24cSmatthias.ringwald break; 232fe1ed1b8Smatthias.ringwald } 233fe1ed1b8Smatthias.ringwald 2343429f56bSmatthias.ringwald // handle BT initialization 2353429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 2367301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 2377301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 2387301ad89Smatthias.ringwald // hci_stack.substate = 0; 2397301ad89Smatthias.ringwald // } 2407301ad89Smatthias.ringwald // handle normal init sequence 2413429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 2423429f56bSmatthias.ringwald // odd: waiting for event 2433429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 2443429f56bSmatthias.ringwald hci_stack.substate++; 2453429f56bSmatthias.ringwald } 2463429f56bSmatthias.ringwald } 24722909952Smatthias.ringwald } 24822909952Smatthias.ringwald 24916833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 25094ab26f8Smatthias.ringwald 25194ab26f8Smatthias.ringwald // execute main loop 25294ab26f8Smatthias.ringwald hci_run(); 25316833f0aSmatthias.ringwald } 25416833f0aSmatthias.ringwald 255fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 2561cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 25716833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 25816833f0aSmatthias.ringwald } 2591cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 26016833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 26116833f0aSmatthias.ringwald } 26216833f0aSmatthias.ringwald 26311e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 264475c8125Smatthias.ringwald 265475c8125Smatthias.ringwald // reference to use transport layer implementation 26616833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 267475c8125Smatthias.ringwald 26811e23e5fSmatthias.ringwald // references to used control implementation 2697301ad89Smatthias.ringwald if (control) { 27011e23e5fSmatthias.ringwald hci_stack.control = control; 2717301ad89Smatthias.ringwald } else { 2727301ad89Smatthias.ringwald hci_stack.control = &null_control; 2737301ad89Smatthias.ringwald } 27411e23e5fSmatthias.ringwald 27511e23e5fSmatthias.ringwald // reference to used config 27611e23e5fSmatthias.ringwald hci_stack.config = config; 27711e23e5fSmatthias.ringwald 278fe1ed1b8Smatthias.ringwald // no connections yet 279fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 280fe1ed1b8Smatthias.ringwald 28102ea9861Smatthias.ringwald // empty cmd buffer 28216833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 28316833f0aSmatthias.ringwald 28416833f0aSmatthias.ringwald // higher level handler 28516833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 28616833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 28716833f0aSmatthias.ringwald 28816833f0aSmatthias.ringwald // register packet handlers with transport 28916833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 29016833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 291475c8125Smatthias.ringwald } 292475c8125Smatthias.ringwald 293475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 294f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 2957301ad89Smatthias.ringwald 296038bc64cSmatthias.ringwald // power on 297038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 298038bc64cSmatthias.ringwald if (err){ 299ac2e07f8Smatthias.ringwald fprintf(stderr, "POWER_ON failed\n"); 300038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 301038bc64cSmatthias.ringwald return err; 302038bc64cSmatthias.ringwald } 303038bc64cSmatthias.ringwald 304038bc64cSmatthias.ringwald // open low-level device 305038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 306038bc64cSmatthias.ringwald if (err){ 307ac2e07f8Smatthias.ringwald fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 308038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 309038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 310038bc64cSmatthias.ringwald return err; 311038bc64cSmatthias.ringwald } 312038bc64cSmatthias.ringwald 3137301ad89Smatthias.ringwald // set up state machine 3147301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 3157301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 3167301ad89Smatthias.ringwald hci_stack.substate = 0; 3177301ad89Smatthias.ringwald 318f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 3197301ad89Smatthias.ringwald 3207301ad89Smatthias.ringwald // close low-level device 3217301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 3227301ad89Smatthias.ringwald 3237301ad89Smatthias.ringwald // power off 32411e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 32543bfb1bdSmatthias.ringwald 32643bfb1bdSmatthias.ringwald // we're off now 32743bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 32811e23e5fSmatthias.ringwald } 32968d92d03Smatthias.ringwald 330038bc64cSmatthias.ringwald // create internal event 331ee8bf225Smatthias.ringwald hci_emit_state(); 332ee8bf225Smatthias.ringwald 33368d92d03Smatthias.ringwald // trigger next/first action 33468d92d03Smatthias.ringwald hci_run(); 33568d92d03Smatthias.ringwald 336475c8125Smatthias.ringwald return 0; 337475c8125Smatthias.ringwald } 338475c8125Smatthias.ringwald 33906b35ec0Smatthias.ringwald void hci_run(){ 3403429f56bSmatthias.ringwald switch (hci_stack.state){ 3413429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 3423429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 3433429f56bSmatthias.ringwald // odd: waiting for command completion 34406b35ec0Smatthias.ringwald return; 3453429f56bSmatthias.ringwald } 3463429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 3473429f56bSmatthias.ringwald // cannot send command yet 34806b35ec0Smatthias.ringwald return; 3493429f56bSmatthias.ringwald } 3503429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 3513429f56bSmatthias.ringwald case 0: 35222909952Smatthias.ringwald hci_send_cmd(&hci_reset); 3533429f56bSmatthias.ringwald break; 3543429f56bSmatthias.ringwald case 1: 355f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 356f432a6ddSmatthias.ringwald break; 357f432a6ddSmatthias.ringwald case 2: 3583429f56bSmatthias.ringwald // ca. 15 sec 3593429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 3603429f56bSmatthias.ringwald break; 361f432a6ddSmatthias.ringwald case 3: 362bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 363f432a6ddSmatthias.ringwald break; 364f432a6ddSmatthias.ringwald case 4: 3653429f56bSmatthias.ringwald // done. 3663429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 367b360b6adSmatthias.ringwald hci_emit_state(); 3683429f56bSmatthias.ringwald break; 3693429f56bSmatthias.ringwald default: 3703429f56bSmatthias.ringwald break; 371475c8125Smatthias.ringwald } 3723429f56bSmatthias.ringwald hci_stack.substate++; 3733429f56bSmatthias.ringwald break; 3743429f56bSmatthias.ringwald default: 3753429f56bSmatthias.ringwald break; 3761f504dbdSmatthias.ringwald } 3773429f56bSmatthias.ringwald } 37816833f0aSmatthias.ringwald 37931452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 380c8e4258aSmatthias.ringwald bd_addr_t addr; 381c8e4258aSmatthias.ringwald hci_connection_t * conn; 382c8e4258aSmatthias.ringwald // house-keeping 383c8e4258aSmatthias.ringwald 384c8e4258aSmatthias.ringwald // create_connection? 385c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 386c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 387c8e4258aSmatthias.ringwald printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 388c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 389c8e4258aSmatthias.ringwald if (conn) { 390c8e4258aSmatthias.ringwald // if connection exists 391c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 392c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 393c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 394c8e4258aSmatthias.ringwald } 395c8e4258aSmatthias.ringwald // otherwise, just ignore 396c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 397c8e4258aSmatthias.ringwald 398c8e4258aSmatthias.ringwald } else{ 399c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 400c8e4258aSmatthias.ringwald if (conn){ 401c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 402c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 403c8e4258aSmatthias.ringwald } 404c8e4258aSmatthias.ringwald } 405c8e4258aSmatthias.ringwald } 406c8e4258aSmatthias.ringwald 407c8e4258aSmatthias.ringwald // accept connection 408c8e4258aSmatthias.ringwald 409c8e4258aSmatthias.ringwald // reject connection 410c8e4258aSmatthias.ringwald 411c8e4258aSmatthias.ringwald // close_connection? 412c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 413c8e4258aSmatthias.ringwald 41431452debSmatthias.ringwald hci_stack.num_cmd_packets--; 41531452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 41631452debSmatthias.ringwald } 4178adf0ddaSmatthias.ringwald 4181cd208adSmatthias.ringwald /** 4191cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 4201cd208adSmatthias.ringwald */ 4211cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 4221cd208adSmatthias.ringwald va_list argptr; 4231cd208adSmatthias.ringwald va_start(argptr, cmd); 4241cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 4251cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 4261cd208adSmatthias.ringwald va_end(argptr); 4271cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 42893b8dc03Smatthias.ringwald } 429c8e4258aSmatthias.ringwald 430ee091cf1Smatthias.ringwald // Create various non-HCI events. 431ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 432ee091cf1Smatthias.ringwald 433c8e4258aSmatthias.ringwald void hci_emit_state(){ 434c8e4258aSmatthias.ringwald uint8_t len = 3; 435c8e4258aSmatthias.ringwald uint8_t event[len]; 43680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 437c8e4258aSmatthias.ringwald event[1] = 1; 438c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 439c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 440c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 441c8e4258aSmatthias.ringwald } 442c8e4258aSmatthias.ringwald 443c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 444c8e4258aSmatthias.ringwald uint8_t len = 13; 445c8e4258aSmatthias.ringwald uint8_t event[len]; 446c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 447c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 448c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 449c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 450c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 451c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 452c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 453c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 454c8e4258aSmatthias.ringwald } 455c8e4258aSmatthias.ringwald 456ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 457ee091cf1Smatthias.ringwald uint8_t len = 4; 458ee091cf1Smatthias.ringwald uint8_t event[len]; 45980d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 460ee091cf1Smatthias.ringwald event[1] = 2; 461ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 462ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 463ee091cf1Smatthias.ringwald hci_stack.event_packet_handler(event, len); 464ee091cf1Smatthias.ringwald } 46543bfb1bdSmatthias.ringwald 46643bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 46743bfb1bdSmatthias.ringwald uint8_t len = 3; 46843bfb1bdSmatthias.ringwald uint8_t event[len]; 46980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 47043bfb1bdSmatthias.ringwald event[1] = 1; 47143bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 47243bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 47343bfb1bdSmatthias.ringwald hci_stack.event_packet_handler(event, len); 47443bfb1bdSmatthias.ringwald } 475038bc64cSmatthias.ringwald 476038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 477038bc64cSmatthias.ringwald uint8_t len = 1; 478038bc64cSmatthias.ringwald uint8_t event[len]; 47980d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 480038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 481038bc64cSmatthias.ringwald hci_stack.event_packet_handler(event, len); 482038bc64cSmatthias.ringwald } 483