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 1806b35ec0Smatthias.ringwald // the STACK is here 1916833f0aSmatthias.ringwald static hci_stack_t hci_stack; 2016833f0aSmatthias.ringwald 2197addcc5Smatthias.ringwald /** 22c8e4258aSmatthias.ringwald * create connection for given address 23c8e4258aSmatthias.ringwald * 24c8e4258aSmatthias.ringwald * @return connection OR NULL, if not found 25c8e4258aSmatthias.ringwald */ 26c8e4258aSmatthias.ringwald static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 27c8e4258aSmatthias.ringwald hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 28c8e4258aSmatthias.ringwald if (!conn) return NULL; 29c8e4258aSmatthias.ringwald BD_ADDR_COPY(conn->address, addr); 30c8e4258aSmatthias.ringwald conn->con_handle = 0xffff; 31c8e4258aSmatthias.ringwald conn->flags = 0; 32c8e4258aSmatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 33c8e4258aSmatthias.ringwald return conn; 34c8e4258aSmatthias.ringwald } 35c8e4258aSmatthias.ringwald 36c8e4258aSmatthias.ringwald /** 3706b35ec0Smatthias.ringwald * get connection for given address 3897addcc5Smatthias.ringwald * 3997addcc5Smatthias.ringwald * @return connection OR NULL, if not found 4097addcc5Smatthias.ringwald */ 41fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 4206b35ec0Smatthias.ringwald linked_item_t *it; 4306b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 4406b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 4506b35ec0Smatthias.ringwald return (hci_connection_t *) it; 4606b35ec0Smatthias.ringwald } 4706b35ec0Smatthias.ringwald } 4806b35ec0Smatthias.ringwald return NULL; 4906b35ec0Smatthias.ringwald } 5006b35ec0Smatthias.ringwald 5106b35ec0Smatthias.ringwald /** 5206b35ec0Smatthias.ringwald * get connection for a given handle 5306b35ec0Smatthias.ringwald * 5406b35ec0Smatthias.ringwald * @return connection OR NULL, if not found 5506b35ec0Smatthias.ringwald */ 56fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 5706b35ec0Smatthias.ringwald linked_item_t *it; 5806b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 5906b35ec0Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 6006b35ec0Smatthias.ringwald return (hci_connection_t *) it; 6106b35ec0Smatthias.ringwald } 6206b35ec0Smatthias.ringwald } 6397addcc5Smatthias.ringwald return NULL; 6497addcc5Smatthias.ringwald } 6597addcc5Smatthias.ringwald 66c8e4258aSmatthias.ringwald 6797addcc5Smatthias.ringwald /** 68ba681a6cSmatthias.ringwald * Dummy handler called by HCI 6916833f0aSmatthias.ringwald */ 701cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){ 7116833f0aSmatthias.ringwald } 7216833f0aSmatthias.ringwald 73ba681a6cSmatthias.ringwald /** 74ba681a6cSmatthias.ringwald * Dummy control handler 75ba681a6cSmatthias.ringwald */ 76ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 77ba681a6cSmatthias.ringwald return 0; 78ba681a6cSmatthias.ringwald } 79ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 80ba681a6cSmatthias.ringwald return "Hardware unknown"; 81ba681a6cSmatthias.ringwald } 82ba681a6cSmatthias.ringwald static bt_control_t null_control = { 83ba681a6cSmatthias.ringwald null_control_function, 84ba681a6cSmatthias.ringwald null_control_function, 85ba681a6cSmatthias.ringwald null_control_function, 86ba681a6cSmatthias.ringwald null_control_name 87ba681a6cSmatthias.ringwald }; 88ba681a6cSmatthias.ringwald 89c8e4258aSmatthias.ringwald 9016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 9116833f0aSmatthias.ringwald hci_stack.acl_packet_handler(packet, size); 9294ab26f8Smatthias.ringwald 9394ab26f8Smatthias.ringwald // execute main loop 9494ab26f8Smatthias.ringwald hci_run(); 9516833f0aSmatthias.ringwald } 9622909952Smatthias.ringwald 9716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 981281a47eSmatthias.ringwald bd_addr_t addr; 99fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 10022909952Smatthias.ringwald 1013429f56bSmatthias.ringwald // Get Num_HCI_Command_Packets 1023429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || 1033429f56bSmatthias.ringwald packet[0] == HCI_EVENT_COMMAND_STATUS){ 1043429f56bSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 10522909952Smatthias.ringwald } 10622909952Smatthias.ringwald 107fe1ed1b8Smatthias.ringwald // Connection management 108fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { 109fe1ed1b8Smatthias.ringwald if (!packet[2]){ 110fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 111c8e4258aSmatthias.ringwald printf("Connection_complete "); print_bd_addr(addr); printf("\n"); 112fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 113fe1ed1b8Smatthias.ringwald if (conn) { 114c8e4258aSmatthias.ringwald conn->state = OPEN; 115fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 116fe1ed1b8Smatthias.ringwald conn->flags = 0; 117fe1ed1b8Smatthias.ringwald printf("New connection: handle %u, ", conn->con_handle); 118fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 119fe1ed1b8Smatthias.ringwald printf("\n"); 120fe1ed1b8Smatthias.ringwald } 121fe1ed1b8Smatthias.ringwald } 122fe1ed1b8Smatthias.ringwald } 123fe1ed1b8Smatthias.ringwald 124fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { 125fe1ed1b8Smatthias.ringwald if (!packet[2]){ 126fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 127fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 128fe1ed1b8Smatthias.ringwald if (conn) { 129fe1ed1b8Smatthias.ringwald printf("Connection closed: handle %u, ", conn->con_handle); 130fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 131fe1ed1b8Smatthias.ringwald printf("\n"); 132fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 133fe1ed1b8Smatthias.ringwald free( conn ); 134fe1ed1b8Smatthias.ringwald } 135fe1ed1b8Smatthias.ringwald } 136fe1ed1b8Smatthias.ringwald } 137fe1ed1b8Smatthias.ringwald 1383429f56bSmatthias.ringwald // handle BT initialization 1393429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 1407301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 1417301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 1427301ad89Smatthias.ringwald // hci_stack.substate = 0; 1437301ad89Smatthias.ringwald // } 1447301ad89Smatthias.ringwald // handle normal init sequence 1453429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 1463429f56bSmatthias.ringwald // odd: waiting for event 1473429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 1483429f56bSmatthias.ringwald hci_stack.substate++; 1493429f56bSmatthias.ringwald } 1503429f56bSmatthias.ringwald } 15122909952Smatthias.ringwald } 15222909952Smatthias.ringwald 1531281a47eSmatthias.ringwald // link key request 1543429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){ 1551281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 1561281a47eSmatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 1571281a47eSmatthias.ringwald return; 1581281a47eSmatthias.ringwald } 1591281a47eSmatthias.ringwald 1601281a47eSmatthias.ringwald // pin code request 1613429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){ 1621281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 1631281a47eSmatthias.ringwald hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234"); 1641281a47eSmatthias.ringwald } 1651281a47eSmatthias.ringwald 16616833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 16794ab26f8Smatthias.ringwald 16894ab26f8Smatthias.ringwald // execute main loop 16994ab26f8Smatthias.ringwald hci_run(); 17016833f0aSmatthias.ringwald } 17116833f0aSmatthias.ringwald 172fcadd0caSmatthias.ringwald /** Register HCI packet handlers */ 1731cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 17416833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 17516833f0aSmatthias.ringwald } 1761cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 17716833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 17816833f0aSmatthias.ringwald } 17916833f0aSmatthias.ringwald 18011e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 181475c8125Smatthias.ringwald 182475c8125Smatthias.ringwald // reference to use transport layer implementation 18316833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 184475c8125Smatthias.ringwald 18511e23e5fSmatthias.ringwald // references to used control implementation 1867301ad89Smatthias.ringwald if (control) { 18711e23e5fSmatthias.ringwald hci_stack.control = control; 1887301ad89Smatthias.ringwald } else { 1897301ad89Smatthias.ringwald hci_stack.control = &null_control; 1907301ad89Smatthias.ringwald } 19111e23e5fSmatthias.ringwald 19211e23e5fSmatthias.ringwald // reference to used config 19311e23e5fSmatthias.ringwald hci_stack.config = config; 19411e23e5fSmatthias.ringwald 195fe1ed1b8Smatthias.ringwald // no connections yet 196fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 197fe1ed1b8Smatthias.ringwald 19802ea9861Smatthias.ringwald // empty cmd buffer 19916833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 20016833f0aSmatthias.ringwald 20116833f0aSmatthias.ringwald // higher level handler 20216833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 20316833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 20416833f0aSmatthias.ringwald 20516833f0aSmatthias.ringwald // register packet handlers with transport 20616833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 20716833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 208475c8125Smatthias.ringwald } 209475c8125Smatthias.ringwald 210475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 211*f12adbd6Smatthias.ringwald if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 2127301ad89Smatthias.ringwald 2137301ad89Smatthias.ringwald // set up state machine 2147301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 2157301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 2167301ad89Smatthias.ringwald hci_stack.substate = 0; 2177301ad89Smatthias.ringwald 2187301ad89Smatthias.ringwald // power on 21911e23e5fSmatthias.ringwald hci_stack.control->on(hci_stack.config); 2207301ad89Smatthias.ringwald 2217301ad89Smatthias.ringwald // open low-level device 2227301ad89Smatthias.ringwald hci_stack.hci_transport->open(hci_stack.config); 2237301ad89Smatthias.ringwald 224ee8bf225Smatthias.ringwald // create internal event 225ee8bf225Smatthias.ringwald 226*f12adbd6Smatthias.ringwald } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 2277301ad89Smatthias.ringwald 2287301ad89Smatthias.ringwald // close low-level device 2297301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 2307301ad89Smatthias.ringwald 2317301ad89Smatthias.ringwald // power off 23211e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 23311e23e5fSmatthias.ringwald } 23468d92d03Smatthias.ringwald 235ee8bf225Smatthias.ringwald hci_emit_state(); 236ee8bf225Smatthias.ringwald 23768d92d03Smatthias.ringwald // trigger next/first action 23868d92d03Smatthias.ringwald hci_run(); 23968d92d03Smatthias.ringwald 240475c8125Smatthias.ringwald return 0; 241475c8125Smatthias.ringwald } 242475c8125Smatthias.ringwald 24306b35ec0Smatthias.ringwald void hci_run(){ 2443429f56bSmatthias.ringwald uint8_t micro_packet; 2453429f56bSmatthias.ringwald switch (hci_stack.state){ 2463429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 2473429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 2483429f56bSmatthias.ringwald // odd: waiting for command completion 24906b35ec0Smatthias.ringwald return; 2503429f56bSmatthias.ringwald } 2513429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 2523429f56bSmatthias.ringwald // cannot send command yet 25306b35ec0Smatthias.ringwald return; 2543429f56bSmatthias.ringwald } 2553429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 2563429f56bSmatthias.ringwald case 0: 25722909952Smatthias.ringwald hci_send_cmd(&hci_reset); 2583429f56bSmatthias.ringwald break; 2593429f56bSmatthias.ringwald case 1: 260f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 261f432a6ddSmatthias.ringwald break; 262f432a6ddSmatthias.ringwald case 2: 2633429f56bSmatthias.ringwald // ca. 15 sec 2643429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 2653429f56bSmatthias.ringwald break; 266f432a6ddSmatthias.ringwald case 3: 267bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 268f432a6ddSmatthias.ringwald break; 269f432a6ddSmatthias.ringwald case 4: 2703429f56bSmatthias.ringwald // done. 2713429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 2728adf0ddaSmatthias.ringwald micro_packet = HCI_EVENT_BTSTACK_WORKING; 2733429f56bSmatthias.ringwald hci_stack.event_packet_handler(µ_packet, 1); 2743429f56bSmatthias.ringwald break; 2753429f56bSmatthias.ringwald default: 2763429f56bSmatthias.ringwald break; 277475c8125Smatthias.ringwald } 2783429f56bSmatthias.ringwald hci_stack.substate++; 2793429f56bSmatthias.ringwald break; 2803429f56bSmatthias.ringwald default: 2813429f56bSmatthias.ringwald break; 2821f504dbdSmatthias.ringwald } 2833429f56bSmatthias.ringwald } 28416833f0aSmatthias.ringwald 28516833f0aSmatthias.ringwald 28643625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 28716833f0aSmatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 28843625864Smatthias.ringwald } 28943625864Smatthias.ringwald 29031452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 291c8e4258aSmatthias.ringwald bd_addr_t addr; 292c8e4258aSmatthias.ringwald hci_connection_t * conn; 293c8e4258aSmatthias.ringwald // house-keeping 294c8e4258aSmatthias.ringwald 295c8e4258aSmatthias.ringwald // create_connection? 296c8e4258aSmatthias.ringwald if (IS_COMMAND(packet, hci_create_connection)){ 297c8e4258aSmatthias.ringwald bt_flip_addr(addr, &packet[3]); 298c8e4258aSmatthias.ringwald printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 299c8e4258aSmatthias.ringwald conn = connection_for_address(addr); 300c8e4258aSmatthias.ringwald if (conn) { 301c8e4258aSmatthias.ringwald // if connection exists 302c8e4258aSmatthias.ringwald if (conn->state == OPEN) { 303c8e4258aSmatthias.ringwald // if OPEN, emit connection complete command 304c8e4258aSmatthias.ringwald hci_emit_connection_complete(conn); 305c8e4258aSmatthias.ringwald } 306c8e4258aSmatthias.ringwald // otherwise, just ignore 307c8e4258aSmatthias.ringwald return 0; // don't sent packet to controller 308c8e4258aSmatthias.ringwald 309c8e4258aSmatthias.ringwald } else{ 310c8e4258aSmatthias.ringwald conn = create_connection_for_addr(addr); 311c8e4258aSmatthias.ringwald if (conn){ 312c8e4258aSmatthias.ringwald // create connection struct and register, state = SENT_CREATE_CONNECTION 313c8e4258aSmatthias.ringwald conn->state = SENT_CREATE_CONNECTION; 314c8e4258aSmatthias.ringwald } 315c8e4258aSmatthias.ringwald } 316c8e4258aSmatthias.ringwald } 317c8e4258aSmatthias.ringwald 318c8e4258aSmatthias.ringwald // accept connection 319c8e4258aSmatthias.ringwald 320c8e4258aSmatthias.ringwald // reject connection 321c8e4258aSmatthias.ringwald 322c8e4258aSmatthias.ringwald // close_connection? 323c8e4258aSmatthias.ringwald // set state = SENT_DISCONNECT 324c8e4258aSmatthias.ringwald 32531452debSmatthias.ringwald hci_stack.num_cmd_packets--; 32631452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 32731452debSmatthias.ringwald } 3288adf0ddaSmatthias.ringwald 3291cd208adSmatthias.ringwald /** 3301cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 3311cd208adSmatthias.ringwald */ 3321cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 3331cd208adSmatthias.ringwald va_list argptr; 3341cd208adSmatthias.ringwald va_start(argptr, cmd); 3351cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 3361cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 3371cd208adSmatthias.ringwald va_end(argptr); 3381cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 33993b8dc03Smatthias.ringwald } 340c8e4258aSmatthias.ringwald 341c8e4258aSmatthias.ringwald void hci_emit_state(){ 342c8e4258aSmatthias.ringwald uint8_t len = 3; 343c8e4258aSmatthias.ringwald uint8_t event[len]; 344c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_BTSTACK_STATE; 345c8e4258aSmatthias.ringwald event[1] = 1; 346c8e4258aSmatthias.ringwald event[2] = hci_stack.state; 347c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 348c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 349c8e4258aSmatthias.ringwald } 350c8e4258aSmatthias.ringwald 351c8e4258aSmatthias.ringwald void hci_emit_connection_complete(hci_connection_t *conn){ 352c8e4258aSmatthias.ringwald uint8_t len = 13; 353c8e4258aSmatthias.ringwald uint8_t event[len]; 354c8e4258aSmatthias.ringwald event[0] = HCI_EVENT_CONNECTION_COMPLETE; 355c8e4258aSmatthias.ringwald event[2] = 0; // status = OK 356c8e4258aSmatthias.ringwald bt_store_16(event, 3, conn->con_handle); 357c8e4258aSmatthias.ringwald bt_flip_addr(&event[5], conn->address); 358c8e4258aSmatthias.ringwald event[11] = 1; // ACL connection 359c8e4258aSmatthias.ringwald event[12] = 0; // encryption disabled 360c8e4258aSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 361c8e4258aSmatthias.ringwald hci_stack.event_packet_handler(event, len); 362c8e4258aSmatthias.ringwald } 363c8e4258aSmatthias.ringwald 364