1 /* 2 * hci.c 3 * 4 * Created by Matthias Ringwald on 4/29/09. 5 * 6 */ 7 8 #include <unistd.h> 9 #include <stdarg.h> 10 #include <string.h> 11 #include <stdio.h> 12 #include "hci.h" 13 14 // calculate combined ogf/ocf value 15 #define OPCODE(ogf, ocf) (ocf | ogf << 10) 16 #define OGF_LINK_CONTROL 0x01 17 #define OGF_CONTROLLER_BASEBAND 0x03 18 19 hci_cmd_t hci_inquiry = { 20 OPCODE(OGF_LINK_CONTROL, 0x01), "311" 21 // LAP, Inquiry length, Num_responses 22 }; 23 24 hci_cmd_t hci_link_key_request_negative_reply = { 25 OPCODE(OGF_LINK_CONTROL, 0x0c), "B" 26 }; 27 28 hci_cmd_t hci_pin_code_request_reply = { 29 OPCODE(OGF_LINK_CONTROL, 0x0d), "B1P" 30 // BD_ADDR, pin length, PIN: c-string 31 }; 32 33 hci_cmd_t hci_reset = { 34 OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), "" 35 }; 36 37 hci_cmd_t hci_create_connection = { 38 OPCODE(OGF_LINK_CONTROL, 0x05), "B21121" 39 // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch 40 }; 41 42 hci_cmd_t hci_write_page_timeout = { 43 OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2" 44 // Page_Timeout * 0.625 ms 45 }; 46 47 hci_cmd_t hci_write_authentication_enable = { 48 OPCODE(OGF_CONTROLLER_BASEBAND, 0x20), "1" 49 // Authentication_Enable 50 }; 51 52 hci_cmd_t hci_host_buffer_size = { 53 OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122" 54 // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets: 55 }; 56 57 58 static hci_transport_t * hci_transport; 59 static uint8_t * hci_cmd_buffer; 60 61 void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){ 62 buffer[pos] = value & 0xff; 63 buffer[pos+1] = value >> 8; 64 } 65 66 void hexdump(uint8_t *data, int size){ 67 int i; 68 for (i=0; i<size;i++){ 69 printf("%02X ", data[i]); 70 } 71 printf("\n"); 72 } 73 74 #if 0 75 static void *hci_daemon_thread(void *arg){ 76 printf("HCI Daemon started\n"); 77 hci_run(transport, &config); 78 return NULL; 79 } 80 #endif 81 82 void hci_init(hci_transport_t *transport, void *config){ 83 84 // reference to use transport layer implementation 85 hci_transport = transport; 86 87 // empty cmd buffer 88 hci_cmd_buffer = malloc(3+255); 89 90 // open unix socket 91 92 // wait for connections 93 94 // enter loop 95 96 // handle events 97 } 98 99 int hci_power_control(HCI_POWER_MODE power_mode){ 100 return 0; 101 } 102 103 void hci_run(){ 104 while (1) { 105 // construct file descriptor set to wait for 106 // select 107 108 // for each ready file in FD - call handle_data 109 sleep(1); 110 } 111 } 112 113 int hci_send_acl_packet(uint8_t *packet, int size){ 114 return hci_transport->send_acl_packet(packet, size); 115 } 116 117 int hci_send_cmd(hci_cmd_t *cmd, ...){ 118 hci_cmd_buffer[0] = cmd->opcode & 0xff; 119 hci_cmd_buffer[1] = cmd->opcode >> 8; 120 int pos = 3; 121 122 va_list argptr; 123 va_start(argptr, cmd); 124 const char *format = cmd->format; 125 uint16_t word; 126 uint32_t longword; 127 uint8_t * ptr; 128 while (*format) { 129 switch(*format) { 130 case '1': // 8 bit value 131 case '2': // 16 bit value 132 case 'H': // hci_handle 133 word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs 134 hci_cmd_buffer[pos++] = word & 0xff; 135 if (*format == '2') { 136 hci_cmd_buffer[pos++] = word >> 8; 137 } else if (*format == 'H') { 138 // TODO 139 } 140 break; 141 case '3': 142 case '4': 143 longword = va_arg(argptr, uint32_t); 144 // longword = va_arg(argptr, int); 145 hci_cmd_buffer[pos++] = longword; 146 hci_cmd_buffer[pos++] = longword >> 8; 147 hci_cmd_buffer[pos++] = longword >> 16; 148 if (*format == '4'){ 149 hci_cmd_buffer[pos++] = longword >> 24; 150 } 151 break; 152 case 'B': // bt-addr 153 ptr = va_arg(argptr, uint8_t *); 154 hci_cmd_buffer[pos++] = ptr[5]; 155 hci_cmd_buffer[pos++] = ptr[4]; 156 hci_cmd_buffer[pos++] = ptr[3]; 157 hci_cmd_buffer[pos++] = ptr[2]; 158 hci_cmd_buffer[pos++] = ptr[1]; 159 hci_cmd_buffer[pos++] = ptr[0]; 160 break; 161 case 'P': // c string passed as pascal string with leading 1-byte len 162 ptr = va_arg(argptr, uint8_t *); 163 memcpy(&hci_cmd_buffer[pos], ptr, 16); 164 pos += 16; 165 break; 166 default: 167 break; 168 } 169 format++; 170 }; 171 va_end(argptr); 172 hci_cmd_buffer[2] = pos - 3; 173 // send packet 174 return hci_transport->send_cmd_packet(hci_cmd_buffer, pos); 175 }