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 1506b35ec0Smatthias.ringwald // the STACK is here 1616833f0aSmatthias.ringwald static hci_stack_t hci_stack; 1716833f0aSmatthias.ringwald 1897addcc5Smatthias.ringwald /** 1906b35ec0Smatthias.ringwald * get connection for given address 2097addcc5Smatthias.ringwald * 2197addcc5Smatthias.ringwald * @return connection OR NULL, if not found 2297addcc5Smatthias.ringwald */ 23*fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_address(bd_addr_t address){ 2406b35ec0Smatthias.ringwald linked_item_t *it; 2506b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 2606b35ec0Smatthias.ringwald if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 2706b35ec0Smatthias.ringwald return (hci_connection_t *) it; 2806b35ec0Smatthias.ringwald } 2906b35ec0Smatthias.ringwald } 3006b35ec0Smatthias.ringwald return NULL; 3106b35ec0Smatthias.ringwald } 3206b35ec0Smatthias.ringwald 3306b35ec0Smatthias.ringwald /** 3406b35ec0Smatthias.ringwald * get connection for a given handle 3506b35ec0Smatthias.ringwald * 3606b35ec0Smatthias.ringwald * @return connection OR NULL, if not found 3706b35ec0Smatthias.ringwald */ 38*fe1ed1b8Smatthias.ringwald static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 3906b35ec0Smatthias.ringwald linked_item_t *it; 4006b35ec0Smatthias.ringwald for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 4106b35ec0Smatthias.ringwald if ( ((hci_connection_t *) it)->con_handle == con_handle){ 4206b35ec0Smatthias.ringwald return (hci_connection_t *) it; 4306b35ec0Smatthias.ringwald } 4406b35ec0Smatthias.ringwald } 4597addcc5Smatthias.ringwald return NULL; 4697addcc5Smatthias.ringwald } 4797addcc5Smatthias.ringwald 4897addcc5Smatthias.ringwald /** 49ba681a6cSmatthias.ringwald * Dummy handler called by HCI 5016833f0aSmatthias.ringwald */ 511cd208adSmatthias.ringwald static void dummy_handler(uint8_t *packet, uint16_t size){ 5216833f0aSmatthias.ringwald } 5316833f0aSmatthias.ringwald 54ba681a6cSmatthias.ringwald /** 55ba681a6cSmatthias.ringwald * Dummy control handler 56ba681a6cSmatthias.ringwald */ 57ba681a6cSmatthias.ringwald static int null_control_function(void *config){ 58ba681a6cSmatthias.ringwald return 0; 59ba681a6cSmatthias.ringwald } 60ba681a6cSmatthias.ringwald static const char * null_control_name(void *config){ 61ba681a6cSmatthias.ringwald return "Hardware unknown"; 62ba681a6cSmatthias.ringwald } 63ba681a6cSmatthias.ringwald static bt_control_t null_control = { 64ba681a6cSmatthias.ringwald null_control_function, 65ba681a6cSmatthias.ringwald null_control_function, 66ba681a6cSmatthias.ringwald null_control_function, 67ba681a6cSmatthias.ringwald null_control_name 68ba681a6cSmatthias.ringwald }; 69ba681a6cSmatthias.ringwald 7016833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 7116833f0aSmatthias.ringwald hci_stack.acl_packet_handler(packet, size); 7294ab26f8Smatthias.ringwald 7394ab26f8Smatthias.ringwald // execute main loop 7494ab26f8Smatthias.ringwald hci_run(); 7516833f0aSmatthias.ringwald } 7622909952Smatthias.ringwald 7716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 781281a47eSmatthias.ringwald bd_addr_t addr; 79*fe1ed1b8Smatthias.ringwald hci_con_handle_t handle; 8022909952Smatthias.ringwald 813429f56bSmatthias.ringwald // Get Num_HCI_Command_Packets 823429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || 833429f56bSmatthias.ringwald packet[0] == HCI_EVENT_COMMAND_STATUS){ 843429f56bSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 8522909952Smatthias.ringwald } 8622909952Smatthias.ringwald 87*fe1ed1b8Smatthias.ringwald // Connection management 88*fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { 89*fe1ed1b8Smatthias.ringwald if (!packet[2]){ 90*fe1ed1b8Smatthias.ringwald bt_flip_addr(addr, &packet[5]); 91*fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_address(addr); 92*fe1ed1b8Smatthias.ringwald if (!conn) { 93*fe1ed1b8Smatthias.ringwald conn = malloc( sizeof(hci_connection_t) ); 94*fe1ed1b8Smatthias.ringwald if (conn) { 95*fe1ed1b8Smatthias.ringwald linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 96*fe1ed1b8Smatthias.ringwald } 97*fe1ed1b8Smatthias.ringwald } 98*fe1ed1b8Smatthias.ringwald if (conn) { 99*fe1ed1b8Smatthias.ringwald BD_ADDR_COPY(conn->address, addr); 100*fe1ed1b8Smatthias.ringwald conn->con_handle = READ_BT_16(packet, 3); 101*fe1ed1b8Smatthias.ringwald conn->flags = 0; 102*fe1ed1b8Smatthias.ringwald printf("New connection: handle %u, ", conn->con_handle); 103*fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 104*fe1ed1b8Smatthias.ringwald printf("\n"); 105*fe1ed1b8Smatthias.ringwald } 106*fe1ed1b8Smatthias.ringwald } 107*fe1ed1b8Smatthias.ringwald } 108*fe1ed1b8Smatthias.ringwald 109*fe1ed1b8Smatthias.ringwald if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { 110*fe1ed1b8Smatthias.ringwald if (!packet[2]){ 111*fe1ed1b8Smatthias.ringwald handle = READ_BT_16(packet, 3); 112*fe1ed1b8Smatthias.ringwald hci_connection_t * conn = connection_for_handle(handle); 113*fe1ed1b8Smatthias.ringwald if (conn) { 114*fe1ed1b8Smatthias.ringwald printf("Connection closed: handle %u, ", conn->con_handle); 115*fe1ed1b8Smatthias.ringwald print_bd_addr( conn->address ); 116*fe1ed1b8Smatthias.ringwald printf("\n"); 117*fe1ed1b8Smatthias.ringwald linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 118*fe1ed1b8Smatthias.ringwald free( conn ); 119*fe1ed1b8Smatthias.ringwald } 120*fe1ed1b8Smatthias.ringwald } 121*fe1ed1b8Smatthias.ringwald } 122*fe1ed1b8Smatthias.ringwald 1233429f56bSmatthias.ringwald // handle BT initialization 1243429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 1257301ad89Smatthias.ringwald // handle H4 synchronization loss on restart 1267301ad89Smatthias.ringwald // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 1277301ad89Smatthias.ringwald // hci_stack.substate = 0; 1287301ad89Smatthias.ringwald // } 1297301ad89Smatthias.ringwald // handle normal init sequence 1303429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 1313429f56bSmatthias.ringwald // odd: waiting for event 1323429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 1333429f56bSmatthias.ringwald hci_stack.substate++; 1343429f56bSmatthias.ringwald } 1353429f56bSmatthias.ringwald } 13622909952Smatthias.ringwald } 13722909952Smatthias.ringwald 1381281a47eSmatthias.ringwald // link key request 1393429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){ 1401281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 1411281a47eSmatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 1421281a47eSmatthias.ringwald return; 1431281a47eSmatthias.ringwald } 1441281a47eSmatthias.ringwald 1451281a47eSmatthias.ringwald // pin code request 1463429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){ 1471281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 1481281a47eSmatthias.ringwald hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234"); 1491281a47eSmatthias.ringwald } 1501281a47eSmatthias.ringwald 15116833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 15294ab26f8Smatthias.ringwald 15394ab26f8Smatthias.ringwald // execute main loop 15494ab26f8Smatthias.ringwald hci_run(); 15516833f0aSmatthias.ringwald } 15616833f0aSmatthias.ringwald 15716833f0aSmatthias.ringwald /** Register L2CAP handlers */ 1581cd208adSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 15916833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 16016833f0aSmatthias.ringwald } 1611cd208adSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 16216833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 16316833f0aSmatthias.ringwald } 16416833f0aSmatthias.ringwald 16511e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 166475c8125Smatthias.ringwald 167475c8125Smatthias.ringwald // reference to use transport layer implementation 16816833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 169475c8125Smatthias.ringwald 17011e23e5fSmatthias.ringwald // references to used control implementation 1717301ad89Smatthias.ringwald if (control) { 17211e23e5fSmatthias.ringwald hci_stack.control = control; 1737301ad89Smatthias.ringwald } else { 1747301ad89Smatthias.ringwald hci_stack.control = &null_control; 1757301ad89Smatthias.ringwald } 17611e23e5fSmatthias.ringwald 17711e23e5fSmatthias.ringwald // reference to used config 17811e23e5fSmatthias.ringwald hci_stack.config = config; 17911e23e5fSmatthias.ringwald 180*fe1ed1b8Smatthias.ringwald // no connections yet 181*fe1ed1b8Smatthias.ringwald hci_stack.connections = NULL; 182*fe1ed1b8Smatthias.ringwald 18302ea9861Smatthias.ringwald // empty cmd buffer 18416833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 18516833f0aSmatthias.ringwald 18616833f0aSmatthias.ringwald // higher level handler 18716833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 18816833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 18916833f0aSmatthias.ringwald 19016833f0aSmatthias.ringwald // register packet handlers with transport 19116833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 19216833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 193475c8125Smatthias.ringwald } 194475c8125Smatthias.ringwald 195475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 19611e23e5fSmatthias.ringwald if (power_mode == HCI_POWER_ON) { 1977301ad89Smatthias.ringwald 1987301ad89Smatthias.ringwald // set up state machine 1997301ad89Smatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 2007301ad89Smatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 2017301ad89Smatthias.ringwald hci_stack.substate = 0; 2027301ad89Smatthias.ringwald 2037301ad89Smatthias.ringwald // power on 20411e23e5fSmatthias.ringwald hci_stack.control->on(hci_stack.config); 2057301ad89Smatthias.ringwald 2067301ad89Smatthias.ringwald // open low-level device 2077301ad89Smatthias.ringwald hci_stack.hci_transport->open(hci_stack.config); 2087301ad89Smatthias.ringwald 20911e23e5fSmatthias.ringwald } else if (power_mode == HCI_POWER_OFF){ 2107301ad89Smatthias.ringwald 2117301ad89Smatthias.ringwald // close low-level device 2127301ad89Smatthias.ringwald hci_stack.hci_transport->close(hci_stack.config); 2137301ad89Smatthias.ringwald 2147301ad89Smatthias.ringwald // power off 21511e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 21611e23e5fSmatthias.ringwald } 21768d92d03Smatthias.ringwald 21868d92d03Smatthias.ringwald // trigger next/first action 21968d92d03Smatthias.ringwald hci_run(); 22068d92d03Smatthias.ringwald 221475c8125Smatthias.ringwald return 0; 222475c8125Smatthias.ringwald } 223475c8125Smatthias.ringwald 22406b35ec0Smatthias.ringwald void hci_run(){ 2253429f56bSmatthias.ringwald uint8_t micro_packet; 2263429f56bSmatthias.ringwald switch (hci_stack.state){ 2273429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 2283429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 2293429f56bSmatthias.ringwald // odd: waiting for command completion 23006b35ec0Smatthias.ringwald return; 2313429f56bSmatthias.ringwald } 2323429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 2333429f56bSmatthias.ringwald // cannot send command yet 23406b35ec0Smatthias.ringwald return; 2353429f56bSmatthias.ringwald } 2363429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 2373429f56bSmatthias.ringwald case 0: 23822909952Smatthias.ringwald hci_send_cmd(&hci_reset); 2393429f56bSmatthias.ringwald break; 2403429f56bSmatthias.ringwald case 1: 241f432a6ddSmatthias.ringwald hci_send_cmd(&hci_read_bd_addr); 242f432a6ddSmatthias.ringwald break; 243f432a6ddSmatthias.ringwald case 2: 2443429f56bSmatthias.ringwald // ca. 15 sec 2453429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 2463429f56bSmatthias.ringwald break; 247f432a6ddSmatthias.ringwald case 3: 248bd67ef2fSmatthias.ringwald hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 249f432a6ddSmatthias.ringwald break; 250f432a6ddSmatthias.ringwald case 4: 2513429f56bSmatthias.ringwald // done. 2523429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 2538adf0ddaSmatthias.ringwald micro_packet = HCI_EVENT_BTSTACK_WORKING; 2543429f56bSmatthias.ringwald hci_stack.event_packet_handler(µ_packet, 1); 2553429f56bSmatthias.ringwald break; 2563429f56bSmatthias.ringwald default: 2573429f56bSmatthias.ringwald break; 258475c8125Smatthias.ringwald } 2593429f56bSmatthias.ringwald hci_stack.substate++; 2603429f56bSmatthias.ringwald break; 2613429f56bSmatthias.ringwald default: 2623429f56bSmatthias.ringwald break; 2631f504dbdSmatthias.ringwald } 2643429f56bSmatthias.ringwald } 26516833f0aSmatthias.ringwald 26616833f0aSmatthias.ringwald 26743625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 26816833f0aSmatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 26943625864Smatthias.ringwald } 27043625864Smatthias.ringwald 27131452debSmatthias.ringwald int hci_send_cmd_packet(uint8_t *packet, int size){ 27231452debSmatthias.ringwald if (READ_CMD_OGF(packet) != OGF_BTSTACK) { 27331452debSmatthias.ringwald hci_stack.num_cmd_packets--; 27431452debSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(packet, size); 27531452debSmatthias.ringwald } 27631452debSmatthias.ringwald 27731452debSmatthias.ringwald hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size); 27831452debSmatthias.ringwald 2798adf0ddaSmatthias.ringwald // BTstack internal commands 2808adf0ddaSmatthias.ringwald uint8_t event[3]; 2818adf0ddaSmatthias.ringwald switch (READ_CMD_OCF(packet)){ 2828adf0ddaSmatthias.ringwald case HCI_BTSTACK_GET_STATE: 2838adf0ddaSmatthias.ringwald event[0] = HCI_EVENT_BTSTACK_STATE; 2848adf0ddaSmatthias.ringwald event[1] = 1; 2858adf0ddaSmatthias.ringwald event[2] = hci_stack.state; 28631452debSmatthias.ringwald hci_dump_packet( HCI_EVENT_PACKET, 0, event, 3); 2878adf0ddaSmatthias.ringwald hci_stack.event_packet_handler(event, 3); 2888adf0ddaSmatthias.ringwald break; 2898adf0ddaSmatthias.ringwald default: 2908adf0ddaSmatthias.ringwald // TODO log into hci dump as vendor specific "event" 2918adf0ddaSmatthias.ringwald printf("Error: command %u not implemented\n:", READ_CMD_OCF(packet)); 2928adf0ddaSmatthias.ringwald break; 2938adf0ddaSmatthias.ringwald } 2948adf0ddaSmatthias.ringwald return 0; 2958adf0ddaSmatthias.ringwald } 2968adf0ddaSmatthias.ringwald 2971cd208adSmatthias.ringwald /** 2981cd208adSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 2991cd208adSmatthias.ringwald */ 3001cd208adSmatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 3011cd208adSmatthias.ringwald va_list argptr; 3021cd208adSmatthias.ringwald va_start(argptr, cmd); 3031cd208adSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 3041cd208adSmatthias.ringwald uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 3051cd208adSmatthias.ringwald va_end(argptr); 3061cd208adSmatthias.ringwald return hci_send_cmd_packet(hci_cmd_buffer, size); 30793b8dc03Smatthias.ringwald }