11f504dbdSmatthias.ringwald /* 21f504dbdSmatthias.ringwald * hci.c 31f504dbdSmatthias.ringwald * 41f504dbdSmatthias.ringwald * Created by Matthias Ringwald on 4/29/09. 51f504dbdSmatthias.ringwald * 61f504dbdSmatthias.ringwald */ 71f504dbdSmatthias.ringwald 8475c8125Smatthias.ringwald #include <unistd.h> 993b8dc03Smatthias.ringwald #include <stdarg.h> 1093b8dc03Smatthias.ringwald #include <string.h> 1156fe0872Smatthias.ringwald #include <stdio.h> 121f504dbdSmatthias.ringwald #include "hci.h" 13d8905019Smatthias.ringwald #include "hci_dump.h" 1493b8dc03Smatthias.ringwald 151e6aba47Smatthias.ringwald // temp 161e6aba47Smatthias.ringwald #include "l2cap.h" 171e6aba47Smatthias.ringwald 18ee091cf1Smatthias.ringwald #define HCI_CONNECTION_TIMEOUT 10 19ee091cf1Smatthias.ringwald 2006b35ec0Smatthias.ringwald // the STACK is here 2116833f0aSmatthias.ringwald static hci_stack_t hci_stack; 2216833f0aSmatthias.ringwald 2397addcc5Smatthias.ringwald /** 24ee091cf1Smatthias.ringwald * get connection for a given handle 25ee091cf1Smatthias.ringwald * 26ee091cf1Smatthias.ringwald * @return connection OR NULL, if not found 27ee091cf1Smatthias.ringwald */ 28ee091cf1Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 29ee091cf1Smatthias.ringwald linked_item_t *it; 30ee091cf1Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 31ee091cf1Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 32ee091cf1Smatthias.ringwald return (hci_connection_t *) it; 33ee091cf1Smatthias.ringwald } 34ee091cf1Smatthias.ringwald } 35ee091cf1Smatthias.ringwald return NULL; 36ee091cf1Smatthias.ringwald } 37ee091cf1Smatthias.ringwald 38ee091cf1Smatthias.ringwald static void hci_connection_timeout_handler(timer_t *timer){ 39ee091cf1Smatthias.ringwald hci_connection_t * connection = linked_item_get_user(&timer->item); 40ee091cf1Smatthias.ringwald struct timeval tv; 41ee091cf1Smatthias.ringwald gettimeofday(&tv, NULL); 42ee091cf1Smatthias.ringwald if (tv.tv_sec > connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT) { 43ee091cf1Smatthias.ringwald // connections might be timed out 44ee091cf1Smatthias.ringwald hci_emit_l2cap_check_timeout(connection); 45ee091cf1Smatthias.ringwald run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT); 46ee091cf1Smatthias.ringwald } else { 47ee091cf1Smatthias.ringwald // next timeout check at 48ee091cf1Smatthias.ringwald timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT; 49ee091cf1Smatthias.ringwald } 50ee091cf1Smatthias.ringwald run_loop_add_timer(timer); 51ee091cf1Smatthias.ringwald } 52ee091cf1Smatthias.ringwald 53ee091cf1Smatthias.ringwald static void hci_connection_timestamp(hci_connection_t *connection){ 54ee091cf1Smatthias.ringwald gettimeofday(&connection->timestamp, NULL); 55ee091cf1Smatthias.ringwald } 56ee091cf1Smatthias.ringwald 57ee091cf1Smatthias.ringwald static void hci_connection_update_timestamp_for_acl(uint8_t *packet) { 58ee091cf1Smatthias.ringwald // update timestamp 59ee091cf1Smatthias.ringwald hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 60ee091cf1Smatthias.ringwald hci_connection_t *connection = connection_for_handle( con_handle); 61ee091cf1Smatthias.ringwald if (connection) hci_connection_timestamp(connection); 62ee091cf1Smatthias.ringwald } 63ee091cf1Smatthias.ringwald 64ee091cf1Smatthias.ringwald /** 65c8e4258aSmatthias.ringwald * create connection for given address 66c8e4258aSmatthias.ringwald * 67c8e4258aSmatthias.ringwald * @return connection OR NULL, if not found 68c8e4258aSmatthias.ringwald */ 69c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 70c8e4258aSmatthias.ringwald hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 71c8e4258aSmatthias.ringwald if (!conn) return NULL; 72c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 73c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 74c8e4258aSmatthias.ringwald conn->flags = 0; 75ee091cf1Smatthias.ringwald linked_item_set_user(&conn->timeout.item, conn); 76ee091cf1Smatthias.ringwald conn->timeout.process = hci_connection_timeout_handler; 77ee091cf1Smatthias.ringwald hci_connection_timestamp(conn); 78c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 79c8e4258aSmatthias.ringwald return conn; 80c8e4258aSmatthias.ringwald } 81c8e4258aSmatthias.ringwald 82c8e4258aSmatthias.ringwald /** 8306b35ec0Smatthias.ringwald * get connection for given address 8497addcc5Smatthias.ringwald * 8597addcc5Smatthias.ringwald * @return connection OR NULL, if not found 8697addcc5Smatthias.ringwald */ 87fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 8806b35ec0Smatthias.ringwald linked_item_t *it; 8906b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 9006b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 9106b35ec0Smatthias.ringwald return (hci_connection_t *) it; 9206b35ec0Smatthias.ringwald } 9306b35ec0Smatthias.ringwald } 9406b35ec0Smatthias.ringwald return NULL; 9506b35ec0Smatthias.ringwald } 9606b35ec0Smatthias.ringwald 9743bfb1bdSmatthias.ringwald /** 9843bfb1bdSmatthias.ringwald * count connections 9943bfb1bdSmatthias.ringwald */ 10043bfb1bdSmatthias.ringwald static int nr_hci_connections(){ 10156c253c9Smatthias.ringwald int count = 0; 10243bfb1bdSmatthias.ringwald linked_item_t *it; 10343bfb1bdSmatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 10443bfb1bdSmatthias.ringwald return count; 10543bfb1bdSmatthias.ringwald } 106c8e4258aSmatthias.ringwald 10797addcc5Smatthias.ringwald /** 108ba681a6cSmatthias.ringwald * Dummy handler called by HCI 10916833f0aSmatthias.ringwald */ 1101cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){ 11116833f0aSmatthias.ringwald } 11216833f0aSmatthias.ringwald 113ba681a6cSmatthias.ringwald /** 114ba681a6cSmatthias.ringwald * Dummy control handler 115ba681a6cSmatthias.ringwald */ 116ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 117ba681a6cSmatthias.ringwald return 0; 118ba681a6cSmatthias.ringwald } 119ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 120ba681a6cSmatthias.ringwald return "Hardware unknown"; 121ba681a6cSmatthias.ringwald } 122ba681a6cSmatthias.ringwald static bt_control_t null_control = { 123ba681a6cSmatthias.ringwald null_control_function, 124ba681a6cSmatthias.ringwald null_control_function, 125ba681a6cSmatthias.ringwald null_control_function, 126ba681a6cSmatthias.ringwald null_control_name 127ba681a6cSmatthias.ringwald }; 128ba681a6cSmatthias.ringwald 129c8e4258aSmatthias.ringwald 130ee091cf1Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 131ee091cf1Smatthias.ringwald hci_connection_update_timestamp_for_acl(packet); 132ee091cf1Smatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 133ee091cf1Smatthias.ringwald } 134ee091cf1Smatthias.ringwald 13516833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 136ee091cf1Smatthias.ringwald hci_connection_update_timestamp_for_acl(packet); 13716833f0aSmatthias.ringwald hci_stack.acl_packet_handler(packet, size); 13894ab26f8Smatthias.ringwald 13994ab26f8Smatthias.ringwald // execute main loop 14094ab26f8Smatthias.ringwald hci_run(); 14116833f0aSmatthias.ringwald } 14222909952Smatthias.ringwald 14316833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1441281a47eSmatthias.ringwald bd_addr_t addr; 145fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 14622909952Smatthias.ringwald 1473429f56bSmatthias.ringwald // Get Num_HCI_Command_Packets 1483429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || 1493429f56bSmatthias.ringwald packet[0] == HCI_EVENT_COMMAND_STATUS){ 1503429f56bSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 15122909952Smatthias.ringwald } 15222909952Smatthias.ringwald 153fe1ed1b8Smatthias.ringwald // Connection management 154fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { 155fe1ed1b8Smatthias.ringwald if (!packet[2]){ 156fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 157c8e4258aSmatthias.ringwald printf("Connection_complete "); print_bd_addr(addr); printf("\n"); 158fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 159fe1ed1b8Smatthias.ringwald if (conn) { 160c8e4258aSmatthias.ringwald conn->state = OPEN; 161fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 162fe1ed1b8Smatthias.ringwald conn->flags = 0; 163ee091cf1Smatthias.ringwald 164ee091cf1Smatthias.ringwald gettimeofday(&conn->timestamp, NULL); 165ee091cf1Smatthias.ringwald run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT); 166ee091cf1Smatthias.ringwald run_loop_add_timer(&conn->timeout); 167ee091cf1Smatthias.ringwald 168fe1ed1b8Smatthias.ringwald printf("New connection: handle %u, ", conn->con_handle); 169fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 170fe1ed1b8Smatthias.ringwald printf("\n"); 17143bfb1bdSmatthias.ringwald 17243bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 173fe1ed1b8Smatthias.ringwald } 174fe1ed1b8Smatthias.ringwald } 175fe1ed1b8Smatthias.ringwald } 176fe1ed1b8Smatthias.ringwald 177fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { 178fe1ed1b8Smatthias.ringwald if (!packet[2]){ 179fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 180fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 181fe1ed1b8Smatthias.ringwald if (conn) { 182fe1ed1b8Smatthias.ringwald printf("Connection closed: handle %u, ", conn->con_handle); 183fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 184fe1ed1b8Smatthias.ringwald printf("\n"); 185ee091cf1Smatthias.ringwald run_loop_remove_timer(&conn->timeout); 186fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 187fe1ed1b8Smatthias.ringwald free( conn ); 18843bfb1bdSmatthias.ringwald hci_emit_nr_connections_changed(); 189fe1ed1b8Smatthias.ringwald } 190fe1ed1b8Smatthias.ringwald } 191fe1ed1b8Smatthias.ringwald } 192fe1ed1b8Smatthias.ringwald 1933429f56bSmatthias.ringwald // handle BT initialization 1943429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 1957301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 1967301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 1977301ad89Smatthias.ringwald // hci_stack.substate = 0; 1987301ad89Smatthias.ringwald // } 1997301ad89Smatthias.ringwald // handle normal init sequence 2003429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 2013429f56bSmatthias.ringwald // odd: waiting for event 2023429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 2033429f56bSmatthias.ringwald hci_stack.substate++; 2043429f56bSmatthias.ringwald } 2053429f56bSmatthias.ringwald } 20622909952Smatthias.ringwald } 20722909952Smatthias.ringwald 2081281a47eSmatthias.ringwald // link key request 2093429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){ 2101281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 2111281a47eSmatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 2121281a47eSmatthias.ringwald return; 2131281a47eSmatthias.ringwald } 2141281a47eSmatthias.ringwald 21516833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 21694ab26f8Smatthias.ringwald 21794ab26f8Smatthias.ringwald // execute main loop 21894ab26f8Smatthias.ringwald hci_run(); 21916833f0aSmatthias.ringwald } 22016833f0aSmatthias.ringwald 221fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 2221cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 22316833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 22416833f0aSmatthias.ringwald } 2251cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 22616833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 22716833f0aSmatthias.ringwald } 22816833f0aSmatthias.ringwald 22911e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 230475c8125Smatthias.ringwald 231475c8125Smatthias.ringwald // reference to use transport layer implementation 23216833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 233475c8125Smatthias.ringwald 23411e23e5fSmatthias.ringwald // references to used control implementation 2357301ad89Smatthias.ringwald if (control) { 23611e23e5fSmatthias.ringwald hci_stack.control = control; 2377301ad89Smatthias.ringwald } else { 2387301ad89Smatthias.ringwald hci_stack.control = &null_control; 2397301ad89Smatthias.ringwald } 24011e23e5fSmatthias.ringwald 24111e23e5fSmatthias.ringwald // reference to used config 24211e23e5fSmatthias.ringwald hci_stack.config = config; 24311e23e5fSmatthias.ringwald 244fe1ed1b8Smatthias.ringwald // no connections yet 245fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 246fe1ed1b8Smatthias.ringwald 24702ea9861Smatthias.ringwald // empty cmd buffer 24816833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 24916833f0aSmatthias.ringwald 25016833f0aSmatthias.ringwald // higher level handler 25116833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 25216833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 25316833f0aSmatthias.ringwald 25416833f0aSmatthias.ringwald // register packet handlers with transport 25516833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 25616833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 257475c8125Smatthias.ringwald } 258475c8125Smatthias.ringwald 259475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 260f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 2617301ad89Smatthias.ringwald 262038bc64cSmatthias.ringwald // power on 263038bc64cSmatthias.ringwald int err = hci_stack.control->on(hci_stack.config); 264038bc64cSmatthias.ringwald if (err){ 265ac2e07f8Smatthias.ringwald fprintf(stderr, "POWER_ON failed\n"); 266038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 267038bc64cSmatthias.ringwald return err; 268038bc64cSmatthias.ringwald } 269038bc64cSmatthias.ringwald 270038bc64cSmatthias.ringwald // open low-level device 271038bc64cSmatthias.ringwald err = hci_stack.hci_transport->open(hci_stack.config); 272038bc64cSmatthias.ringwald if (err){ 273ac2e07f8Smatthias.ringwald fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 274038bc64cSmatthias.ringwald hci_stack.control->off(hci_stack.config); 275038bc64cSmatthias.ringwald hci_emit_hci_open_failed(); 276038bc64cSmatthias.ringwald return err; 277038bc64cSmatthias.ringwald } 278038bc64cSmatthias.ringwald 2797301ad89Smatthias.ringwald // set up state machine 2807301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 2817301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 2827301ad89Smatthias.ringwald hci_stack.substate = 0; 2837301ad89Smatthias.ringwald 284f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 2857301ad89Smatthias.ringwald 2867301ad89Smatthias.ringwald // close low-level device 2877301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 2887301ad89Smatthias.ringwald 2897301ad89Smatthias.ringwald // power off 29011e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 29143bfb1bdSmatthias.ringwald 29243bfb1bdSmatthias.ringwald // we're off now 29343bfb1bdSmatthias.ringwald hci_stack.state = HCI_STATE_OFF; 29411e23e5fSmatthias.ringwald } 29568d92d03Smatthias.ringwald 296038bc64cSmatthias.ringwald // create internal event 297ee8bf225Smatthias.ringwald hci_emit_state(); 298ee8bf225Smatthias.ringwald 29968d92d03Smatthias.ringwald // trigger next/first action 30068d92d03Smatthias.ringwald hci_run(); 30168d92d03Smatthias.ringwald 302475c8125Smatthias.ringwald return 0; 303475c8125Smatthias.ringwald } 304475c8125Smatthias.ringwald 30506b35ec0Smatthias.ringwald void hci_run(){ 3063429f56bSmatthias.ringwald uint8_t micro_packet; 3073429f56bSmatthias.ringwald switch (hci_stack.state){ 3083429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 3093429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 3103429f56bSmatthias.ringwald // odd: waiting for command completion 31106b35ec0Smatthias.ringwald return; 3123429f56bSmatthias.ringwald } 3133429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 3143429f56bSmatthias.ringwald // cannot send command yet 31506b35ec0Smatthias.ringwald return; 3163429f56bSmatthias.ringwald } 3173429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 3183429f56bSmatthias.ringwald case 0: 31922909952Smatthias.ringwald hci_send_cmd(&hci_reset); 3203429f56bSmatthias.ringwald break; 3213429f56bSmatthias.ringwald case 1: 322f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 323f432a6ddSmatthias.ringwald break; 324f432a6ddSmatthias.ringwald case 2: 3253429f56bSmatthias.ringwald // ca. 15 sec 3263429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 3273429f56bSmatthias.ringwald break; 328f432a6ddSmatthias.ringwald case 3: 329bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 330f432a6ddSmatthias.ringwald break; 331f432a6ddSmatthias.ringwald case 4: 3323429f56bSmatthias.ringwald // done. 3333429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 334*b360b6adSmatthias.ringwald hci_emit_state(); 3353429f56bSmatthias.ringwald break; 3363429f56bSmatthias.ringwald default: 3373429f56bSmatthias.ringwald break; 338475c8125Smatthias.ringwald } 3393429f56bSmatthias.ringwald hci_stack.substate++; 3403429f56bSmatthias.ringwald break; 3413429f56bSmatthias.ringwald default: 3423429f56bSmatthias.ringwald break; 3431f504dbdSmatthias.ringwald } 3443429f56bSmatthias.ringwald } 34516833f0aSmatthias.ringwald 34631452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 347c8e4258aSmatthias.ringwald bd_addr_t addr; 348c8e4258aSmatthias.ringwald hci_connection_t * conn; 349c8e4258aSmatthias.ringwald // house-keeping 350c8e4258aSmatthias.ringwald 351c8e4258aSmatthias.ringwald // create_connection? 352c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 353c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 354c8e4258aSmatthias.ringwald printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 355c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 356c8e4258aSmatthias.ringwald if (conn) { 357c8e4258aSmatthias.ringwald // if connection exists 358c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 359c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 360c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 361c8e4258aSmatthias.ringwald } 362c8e4258aSmatthias.ringwald // otherwise, just ignore 363c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 364c8e4258aSmatthias.ringwald 365c8e4258aSmatthias.ringwald } else{ 366c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 367c8e4258aSmatthias.ringwald if (conn){ 368c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 369c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 370c8e4258aSmatthias.ringwald } 371c8e4258aSmatthias.ringwald } 372c8e4258aSmatthias.ringwald } 373c8e4258aSmatthias.ringwald 374c8e4258aSmatthias.ringwald // accept connection 375c8e4258aSmatthias.ringwald 376c8e4258aSmatthias.ringwald // reject connection 377c8e4258aSmatthias.ringwald 378c8e4258aSmatthias.ringwald // close_connection? 379c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 380c8e4258aSmatthias.ringwald 38131452debSmatthias.ringwald hci_stack.num_cmd_packets--; 38231452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 38331452debSmatthias.ringwald } 3848adf0ddaSmatthias.ringwald 3851cd208adSmatthias.ringwald /** 3861cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 3871cd208adSmatthias.ringwald */ 3881cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 3891cd208adSmatthias.ringwald va_list argptr; 3901cd208adSmatthias.ringwald va_start(argptr, cmd); 3911cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 3921cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 3931cd208adSmatthias.ringwald va_end(argptr); 3941cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 39593b8dc03Smatthias.ringwald } 396c8e4258aSmatthias.ringwald 397ee091cf1Smatthias.ringwald // Create various non-HCI events. 398ee091cf1Smatthias.ringwald // TODO: generalize, use table similar to hci_create_command 399ee091cf1Smatthias.ringwald 400c8e4258aSmatthias.ringwald void hci_emit_state(){ 401c8e4258aSmatthias.ringwald uint8_t len = 3; 402c8e4258aSmatthias.ringwald uint8_t event[len]; 40380d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_STATE; 404c8e4258aSmatthias.ringwald event[1] = 1; 405c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 406c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 407c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 408c8e4258aSmatthias.ringwald } 409c8e4258aSmatthias.ringwald 410c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 411c8e4258aSmatthias.ringwald uint8_t len = 13; 412c8e4258aSmatthias.ringwald uint8_t event[len]; 413c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 414c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 415c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 416c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 417c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 418c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 419c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 420c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 421c8e4258aSmatthias.ringwald } 422c8e4258aSmatthias.ringwald 423ee091cf1Smatthias.ringwald void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 424ee091cf1Smatthias.ringwald uint8_t len = 4; 425ee091cf1Smatthias.ringwald uint8_t event[len]; 42680d52d6bSmatthias.ringwald event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 427ee091cf1Smatthias.ringwald event[1] = 2; 428ee091cf1Smatthias.ringwald bt_store_16(event, 2, conn->con_handle); 429ee091cf1Smatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 430ee091cf1Smatthias.ringwald hci_stack.event_packet_handler(event, len); 431ee091cf1Smatthias.ringwald } 43243bfb1bdSmatthias.ringwald 43343bfb1bdSmatthias.ringwald void hci_emit_nr_connections_changed(){ 43443bfb1bdSmatthias.ringwald uint8_t len = 3; 43543bfb1bdSmatthias.ringwald uint8_t event[len]; 43680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 43743bfb1bdSmatthias.ringwald event[1] = 1; 43843bfb1bdSmatthias.ringwald event[2] = nr_hci_connections(); 43943bfb1bdSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 44043bfb1bdSmatthias.ringwald hci_stack.event_packet_handler(event, len); 44143bfb1bdSmatthias.ringwald } 442038bc64cSmatthias.ringwald 443038bc64cSmatthias.ringwald void hci_emit_hci_open_failed(){ 444038bc64cSmatthias.ringwald uint8_t len = 1; 445038bc64cSmatthias.ringwald uint8_t event[len]; 44680d52d6bSmatthias.ringwald event[0] = BTSTACK_EVENT_POWERON_FAILED; 447038bc64cSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 448038bc64cSmatthias.ringwald hci_stack.event_packet_handler(event, len); 449038bc64cSmatthias.ringwald } 450