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 15 hci_cmd_t hci_inquiry = { 16 0x01, 0x01, "311" // LAP, Inquiry length, Num_responses 17 }; 18 19 hci_cmd_t hci_reset = { 20 0x03, 0x03, "" 21 }; 22 23 24 static hci_transport_t *hci_transport; 25 26 void hexdump(uint8_t *data, int size){ 27 int i; 28 for (i=0; i<size;i++){ 29 printf("%02X ", data[i]); 30 } 31 printf("\n"); 32 } 33 34 #if 0 35 static void *hci_daemon_thread(void *arg){ 36 printf("HCI Daemon started\n"); 37 hci_run(transport, &config); 38 return NULL; 39 } 40 #endif 41 42 void hci_init(hci_transport_t *transport, void *config){ 43 44 // reference to use transport layer implementation 45 hci_transport = transport; 46 47 // open unix socket 48 49 // wait for connections 50 51 // enter loop 52 53 // handle events 54 } 55 56 int hci_power_control(HCI_POWER_MODE power_mode){ 57 return 0; 58 } 59 60 int hci_send_cmd_packet(uint8_t *buffer, int size){ 61 return hci_transport->send_cmd_packet(buffer, size); 62 } 63 64 void hci_run(){ 65 while (1) { 66 // construct file descriptor set to wait for 67 // select 68 69 // for each ready file in FD - call handle_data 70 sleep(1); 71 } 72 } 73 74 75 void hci_create_cmd_packet(uint8_t *buffer, uint8_t *cmd_len, hci_cmd_t *cmd, ...){ 76 buffer[0] = cmd->ocf; 77 buffer[1] = cmd->ocf >> 8 | cmd->ogf << 2; 78 int pos = 3; 79 80 va_list argptr; 81 va_start(argptr, cmd); 82 const char *format = cmd->format; 83 uint16_t word; 84 uint32_t longword; 85 uint8_t * bt_addr; 86 while (*format) { 87 switch(*format) { 88 case '1': // 8 bit value 89 case '2': // 16 bit value 90 case 'H': // hci_handle 91 word = va_arg(argptr, int); // minimum c parameter width is int 92 buffer[pos++] = word & 0xff; 93 if (*format == '2') { 94 buffer[pos++] = word >> 8; 95 } else if (*format == 'H') { 96 97 } 98 break; 99 case '3': 100 case '4': 101 longword = va_arg(argptr, uint32_t); 102 // longword = va_arg(argptr, int); 103 buffer[pos++] = longword; 104 buffer[pos++] = longword >> 8; 105 buffer[pos++] = longword >> 16; 106 if (*format == '4'){ 107 buffer[pos++] = longword >> 24; 108 } 109 break; 110 case 'B': // bt-addr 111 bt_addr = va_arg(argptr, uint8_t *); 112 memcpy( &buffer[pos], bt_addr, 6); 113 pos += 6; 114 break; 115 default: 116 break; 117 } 118 format++; 119 }; 120 va_end(argptr); 121 buffer[2] = pos - 3; 122 *cmd_len = pos; 123 }