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" 131f504dbdSmatthias.ringwald 140a974e0cSmatthias.ringwald // calculate combined ogf/ocf value 150a974e0cSmatthias.ringwald #define OPCODE(ogf, ocf) (ocf | ogf << 10) 1602ea9861Smatthias.ringwald #define OGF_LINK_CONTROL 0x01 1702ea9861Smatthias.ringwald #define OGF_CONTROLLER_BASEBAND 0x03 1893b8dc03Smatthias.ringwald 1993b8dc03Smatthias.ringwald hci_cmd_t hci_inquiry = { 203091b266Smatthias.ringwald OPCODE(OGF_LINK_CONTROL, 0x01), "311" 213091b266Smatthias.ringwald // LAP, Inquiry length, Num_responses 223091b266Smatthias.ringwald }; 233091b266Smatthias.ringwald 243091b266Smatthias.ringwald hci_cmd_t hci_link_key_request_negative_reply = { 253091b266Smatthias.ringwald OPCODE(OGF_LINK_CONTROL, 0x0c), "B" 263091b266Smatthias.ringwald }; 273091b266Smatthias.ringwald 283091b266Smatthias.ringwald hci_cmd_t hci_pin_code_request_reply = { 293091b266Smatthias.ringwald OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P" 303091b266Smatthias.ringwald // BD_ADDR, pin length, PIN: c-string 3193b8dc03Smatthias.ringwald }; 3293b8dc03Smatthias.ringwald 3393b8dc03Smatthias.ringwald hci_cmd_t hci_reset = { 3402ea9861Smatthias.ringwald OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), "" 3502ea9861Smatthias.ringwald }; 3602ea9861Smatthias.ringwald 3702ea9861Smatthias.ringwald hci_cmd_t hci_create_connection = { 3802ea9861Smatthias.ringwald OPCODE(OGF_LINK_CONTROL, 0x05), "B21121" 3902ea9861Smatthias.ringwald // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 4002ea9861Smatthias.ringwald }; 4102ea9861Smatthias.ringwald 4202ea9861Smatthias.ringwald hci_cmd_t hci_write_page_timeout = { 4302ea9861Smatthias.ringwald OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2" 4402ea9861Smatthias.ringwald // Page_Timeout * 0.625 ms 4502ea9861Smatthias.ringwald }; 4602ea9861Smatthias.ringwald 473091b266Smatthias.ringwald hci_cmd_t hci_write_authentication_enable = { 483091b266Smatthias.ringwald OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1" 493091b266Smatthias.ringwald // Authentication_Enable 503091b266Smatthias.ringwald }; 513091b266Smatthias.ringwald 5202ea9861Smatthias.ringwald hci_cmd_t hci_host_buffer_size = { 5302ea9861Smatthias.ringwald OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122" 5402ea9861Smatthias.ringwald // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets: 5593b8dc03Smatthias.ringwald }; 5693b8dc03Smatthias.ringwald 5793b8dc03Smatthias.ringwald 5816833f0aSmatthias.ringwald // the stack is here 5916833f0aSmatthias.ringwald static hci_stack_t hci_stack; 6016833f0aSmatthias.ringwald 61475c8125Smatthias.ringwald 6243625864Smatthias.ringwald void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 6343625864Smatthias.ringwald buffer[pos] = value & 0xff; 6443625864Smatthias.ringwald buffer[pos+1] = value >> 8; 6543625864Smatthias.ringwald } 6643625864Smatthias.ringwald 671281a47eSmatthias.ringwald void bt_flip_addr(bd_addr_t dest, bd_addr_t src){ 681281a47eSmatthias.ringwald dest[0] = src[5]; 691281a47eSmatthias.ringwald dest[1] = src[4]; 701281a47eSmatthias.ringwald dest[2] = src[3]; 711281a47eSmatthias.ringwald dest[3] = src[2]; 721281a47eSmatthias.ringwald dest[4] = src[1]; 731281a47eSmatthias.ringwald dest[5] = src[0]; 741281a47eSmatthias.ringwald } 751281a47eSmatthias.ringwald 76*02582713Smatthias.ringwald void hexdump(void *data, int size){ 7756fe0872Smatthias.ringwald int i; 7856fe0872Smatthias.ringwald for (i=0; i<size;i++){ 79*02582713Smatthias.ringwald printf("%02X ", ((uint8_t *)data)[i]); 8056fe0872Smatthias.ringwald } 8156fe0872Smatthias.ringwald printf("\n"); 8256fe0872Smatthias.ringwald } 8356fe0872Smatthias.ringwald 8456fe0872Smatthias.ringwald #if 0 8556fe0872Smatthias.ringwald static void *hci_daemon_thread(void *arg){ 8656fe0872Smatthias.ringwald printf("HCI Daemon started\n"); 8756fe0872Smatthias.ringwald hci_run(transport, &config); 8856fe0872Smatthias.ringwald return NULL; 8956fe0872Smatthias.ringwald } 9056fe0872Smatthias.ringwald #endif 9156fe0872Smatthias.ringwald 9216833f0aSmatthias.ringwald /** 9397addcc5Smatthias.ringwald * Linked link list 9497addcc5Smatthias.ringwald */ 9597addcc5Smatthias.ringwald 9697addcc5Smatthias.ringwald /** 9797addcc5Smatthias.ringwald * get link for given address 9897addcc5Smatthias.ringwald * 9997addcc5Smatthias.ringwald * @return connection OR NULL, if not found 10097addcc5Smatthias.ringwald */ 101145be03fSmatthias.ringwald #if 0 10297addcc5Smatthias.ringwald static hci_connection_t *link_for_addr(bd_addr_t addr){ 10397addcc5Smatthias.ringwald return NULL; 10497addcc5Smatthias.ringwald } 105145be03fSmatthias.ringwald #endif 10697addcc5Smatthias.ringwald 10797addcc5Smatthias.ringwald /** 10816833f0aSmatthias.ringwald * Handler called by HCI transport 10916833f0aSmatthias.ringwald */ 11016833f0aSmatthias.ringwald static void dummy_handler(uint8_t *packet, int size){ 11116833f0aSmatthias.ringwald } 11216833f0aSmatthias.ringwald 11316833f0aSmatthias.ringwald static void acl_handler(uint8_t *packet, int size){ 11416833f0aSmatthias.ringwald hci_stack.acl_packet_handler(packet, size); 11516833f0aSmatthias.ringwald } 11622909952Smatthias.ringwald 11716833f0aSmatthias.ringwald static void event_handler(uint8_t *packet, int size){ 1181281a47eSmatthias.ringwald bd_addr_t addr; 11922909952Smatthias.ringwald 1203429f56bSmatthias.ringwald // Get Num_HCI_Command_Packets 1213429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || 1223429f56bSmatthias.ringwald packet[0] == HCI_EVENT_COMMAND_STATUS){ 1233429f56bSmatthias.ringwald hci_stack.num_cmd_packets = packet[2]; 12422909952Smatthias.ringwald } 12522909952Smatthias.ringwald 1263429f56bSmatthias.ringwald // handle BT initialization 1273429f56bSmatthias.ringwald if (hci_stack.state == HCI_STATE_INITIALIZING){ 1283429f56bSmatthias.ringwald if (hci_stack.substate % 2){ 1293429f56bSmatthias.ringwald // odd: waiting for event 1303429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 1313429f56bSmatthias.ringwald hci_stack.substate++; 1323429f56bSmatthias.ringwald } 1333429f56bSmatthias.ringwald } 13422909952Smatthias.ringwald } 13522909952Smatthias.ringwald 1361281a47eSmatthias.ringwald // link key request 1373429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){ 1381281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 1391281a47eSmatthias.ringwald hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 1401281a47eSmatthias.ringwald return; 1411281a47eSmatthias.ringwald } 1421281a47eSmatthias.ringwald 1431281a47eSmatthias.ringwald // pin code request 1443429f56bSmatthias.ringwald if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){ 1451281a47eSmatthias.ringwald bt_flip_addr(addr, &packet[2]); 1461281a47eSmatthias.ringwald hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234"); 1471281a47eSmatthias.ringwald } 1481281a47eSmatthias.ringwald 14916833f0aSmatthias.ringwald hci_stack.event_packet_handler(packet, size); 15016833f0aSmatthias.ringwald } 15116833f0aSmatthias.ringwald 15216833f0aSmatthias.ringwald /** Register L2CAP handlers */ 15316833f0aSmatthias.ringwald void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){ 15416833f0aSmatthias.ringwald hci_stack.event_packet_handler = handler; 15516833f0aSmatthias.ringwald } 15616833f0aSmatthias.ringwald void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, int size)){ 15716833f0aSmatthias.ringwald hci_stack.acl_packet_handler = handler; 15816833f0aSmatthias.ringwald } 15916833f0aSmatthias.ringwald 16011e23e5fSmatthias.ringwald void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 161475c8125Smatthias.ringwald 162475c8125Smatthias.ringwald // reference to use transport layer implementation 16316833f0aSmatthias.ringwald hci_stack.hci_transport = transport; 164475c8125Smatthias.ringwald 16511e23e5fSmatthias.ringwald // references to used control implementation 16611e23e5fSmatthias.ringwald hci_stack.control = control; 16711e23e5fSmatthias.ringwald 16811e23e5fSmatthias.ringwald // reference to used config 16911e23e5fSmatthias.ringwald hci_stack.config = config; 17011e23e5fSmatthias.ringwald 17102ea9861Smatthias.ringwald // empty cmd buffer 17216833f0aSmatthias.ringwald hci_stack.hci_cmd_buffer = malloc(3+255); 17316833f0aSmatthias.ringwald 1743429f56bSmatthias.ringwald // set up state 1753429f56bSmatthias.ringwald hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 1763429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_INITIALIZING; 1773429f56bSmatthias.ringwald hci_stack.substate = 0; 1783429f56bSmatthias.ringwald 17916833f0aSmatthias.ringwald // higher level handler 18016833f0aSmatthias.ringwald hci_stack.event_packet_handler = dummy_handler; 18116833f0aSmatthias.ringwald hci_stack.acl_packet_handler = dummy_handler; 18216833f0aSmatthias.ringwald 18316833f0aSmatthias.ringwald // register packet handlers with transport 18416833f0aSmatthias.ringwald transport->register_event_packet_handler( event_handler); 18516833f0aSmatthias.ringwald transport->register_acl_packet_handler( acl_handler); 18616833f0aSmatthias.ringwald 1878cbb57e6Smatthias.ringwald // turn on 1888cbb57e6Smatthias.ringwald hci_power_control(HCI_POWER_ON); 1898cbb57e6Smatthias.ringwald 19016833f0aSmatthias.ringwald // open low-level device 19122909952Smatthias.ringwald transport->open(config); 192475c8125Smatthias.ringwald } 193475c8125Smatthias.ringwald 194475c8125Smatthias.ringwald int hci_power_control(HCI_POWER_MODE power_mode){ 19511e23e5fSmatthias.ringwald if (hci_stack.control) { 19611e23e5fSmatthias.ringwald if (power_mode == HCI_POWER_ON) { 19711e23e5fSmatthias.ringwald hci_stack.control->on(hci_stack.config); 19811e23e5fSmatthias.ringwald } else if (power_mode == HCI_POWER_OFF){ 19911e23e5fSmatthias.ringwald hci_stack.control->off(hci_stack.config); 20011e23e5fSmatthias.ringwald } 20111e23e5fSmatthias.ringwald } 202475c8125Smatthias.ringwald return 0; 203475c8125Smatthias.ringwald } 204475c8125Smatthias.ringwald 2053429f56bSmatthias.ringwald uint32_t hci_run(){ 2063429f56bSmatthias.ringwald uint8_t micro_packet; 2073429f56bSmatthias.ringwald switch (hci_stack.state){ 2083429f56bSmatthias.ringwald case HCI_STATE_INITIALIZING: 2093429f56bSmatthias.ringwald if (hci_stack.substate % 2) { 2103429f56bSmatthias.ringwald // odd: waiting for command completion 2113429f56bSmatthias.ringwald return 0; 2123429f56bSmatthias.ringwald } 2133429f56bSmatthias.ringwald if (hci_stack.num_cmd_packets == 0) { 2143429f56bSmatthias.ringwald // cannot send command yet 2153429f56bSmatthias.ringwald return 0; 2163429f56bSmatthias.ringwald } 2173429f56bSmatthias.ringwald switch (hci_stack.substate/2){ 2183429f56bSmatthias.ringwald case 0: 21922909952Smatthias.ringwald hci_send_cmd(&hci_reset); 2203429f56bSmatthias.ringwald break; 2213429f56bSmatthias.ringwald case 1: 2223429f56bSmatthias.ringwald // ca. 15 sec 2233429f56bSmatthias.ringwald hci_send_cmd(&hci_write_page_timeout, 0x6000); 2243429f56bSmatthias.ringwald break; 2253429f56bSmatthias.ringwald case 2: 2263429f56bSmatthias.ringwald // done. 2273429f56bSmatthias.ringwald hci_stack.state = HCI_STATE_WORKING; 2283429f56bSmatthias.ringwald micro_packet = BTSTACK_EVENT_HCI_WORKING; 2293429f56bSmatthias.ringwald hci_stack.event_packet_handler(µ_packet, 1); 2303429f56bSmatthias.ringwald break; 2313429f56bSmatthias.ringwald default: 2323429f56bSmatthias.ringwald break; 233475c8125Smatthias.ringwald } 2343429f56bSmatthias.ringwald hci_stack.substate++; 2353429f56bSmatthias.ringwald break; 2363429f56bSmatthias.ringwald default: 2373429f56bSmatthias.ringwald break; 2381f504dbdSmatthias.ringwald } 23993b8dc03Smatthias.ringwald 2403429f56bSmatthias.ringwald // don't check for timetous yet 2413429f56bSmatthias.ringwald return 0; 2423429f56bSmatthias.ringwald } 24316833f0aSmatthias.ringwald 24416833f0aSmatthias.ringwald 24543625864Smatthias.ringwald int hci_send_acl_packet(uint8_t *packet, int size){ 24616833f0aSmatthias.ringwald return hci_stack.hci_transport->send_acl_packet(packet, size); 24743625864Smatthias.ringwald } 24843625864Smatthias.ringwald 2493429f56bSmatthias.ringwald 2503429f56bSmatthias.ringwald /** 2513429f56bSmatthias.ringwald * pre: numcmds >= 0 - it's allowed to send a command to the controller 2523429f56bSmatthias.ringwald */ 25302ea9861Smatthias.ringwald int hci_send_cmd(hci_cmd_t *cmd, ...){ 25416833f0aSmatthias.ringwald uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 25502ea9861Smatthias.ringwald hci_cmd_buffer[0] = cmd->opcode & 0xff; 25602ea9861Smatthias.ringwald hci_cmd_buffer[1] = cmd->opcode >> 8; 25793b8dc03Smatthias.ringwald int pos = 3; 25893b8dc03Smatthias.ringwald 25993b8dc03Smatthias.ringwald va_list argptr; 26093b8dc03Smatthias.ringwald va_start(argptr, cmd); 26193b8dc03Smatthias.ringwald const char *format = cmd->format; 26293b8dc03Smatthias.ringwald uint16_t word; 26393b8dc03Smatthias.ringwald uint32_t longword; 2643091b266Smatthias.ringwald uint8_t * ptr; 26593b8dc03Smatthias.ringwald while (*format) { 26693b8dc03Smatthias.ringwald switch(*format) { 26793b8dc03Smatthias.ringwald case '1': // 8 bit value 26893b8dc03Smatthias.ringwald case '2': // 16 bit value 26993b8dc03Smatthias.ringwald case 'H': // hci_handle 270554588a5Smatthias.ringwald word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 27102ea9861Smatthias.ringwald hci_cmd_buffer[pos++] = word & 0xff; 27293b8dc03Smatthias.ringwald if (*format == '2') { 27302ea9861Smatthias.ringwald hci_cmd_buffer[pos++] = word >> 8; 27493b8dc03Smatthias.ringwald } else if (*format == 'H') { 275554588a5Smatthias.ringwald // TODO 27693b8dc03Smatthias.ringwald } 27793b8dc03Smatthias.ringwald break; 27893b8dc03Smatthias.ringwald case '3': 27993b8dc03Smatthias.ringwald case '4': 28093b8dc03Smatthias.ringwald longword = va_arg(argptr, uint32_t); 28193b8dc03Smatthias.ringwald // longword = va_arg(argptr, int); 28202ea9861Smatthias.ringwald hci_cmd_buffer[pos++] = longword; 28302ea9861Smatthias.ringwald hci_cmd_buffer[pos++] = longword >> 8; 28402ea9861Smatthias.ringwald hci_cmd_buffer[pos++] = longword >> 16; 28593b8dc03Smatthias.ringwald if (*format == '4'){ 28602ea9861Smatthias.ringwald hci_cmd_buffer[pos++] = longword >> 24; 28793b8dc03Smatthias.ringwald } 28893b8dc03Smatthias.ringwald break; 28993b8dc03Smatthias.ringwald case 'B': // bt-addr 2903091b266Smatthias.ringwald ptr = va_arg(argptr, uint8_t *); 2913091b266Smatthias.ringwald hci_cmd_buffer[pos++] = ptr[5]; 2923091b266Smatthias.ringwald hci_cmd_buffer[pos++] = ptr[4]; 2933091b266Smatthias.ringwald hci_cmd_buffer[pos++] = ptr[3]; 2943091b266Smatthias.ringwald hci_cmd_buffer[pos++] = ptr[2]; 2953091b266Smatthias.ringwald hci_cmd_buffer[pos++] = ptr[1]; 2963091b266Smatthias.ringwald hci_cmd_buffer[pos++] = ptr[0]; 2973091b266Smatthias.ringwald break; 2983091b266Smatthias.ringwald case 'P': // c string passed as pascal string with leading 1-byte len 2993091b266Smatthias.ringwald ptr = va_arg(argptr, uint8_t *); 3003091b266Smatthias.ringwald memcpy(&hci_cmd_buffer[pos], ptr, 16); 3013091b266Smatthias.ringwald pos += 16; 30293b8dc03Smatthias.ringwald break; 30393b8dc03Smatthias.ringwald default: 30493b8dc03Smatthias.ringwald break; 30593b8dc03Smatthias.ringwald } 30693b8dc03Smatthias.ringwald format++; 30793b8dc03Smatthias.ringwald }; 30893b8dc03Smatthias.ringwald va_end(argptr); 30902ea9861Smatthias.ringwald hci_cmd_buffer[2] = pos - 3; 31002ea9861Smatthias.ringwald // send packet 3113429f56bSmatthias.ringwald hci_stack.num_cmd_packets--; 31216833f0aSmatthias.ringwald return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos); 31393b8dc03Smatthias.ringwald }