xref: /btstack/src/hci.c (revision 02ea9861adb72eb7d0cba8d04d752df236f0e067)
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" // LAP, Inquiry length, Num_responses
21 };
22 
23 hci_cmd_t hci_reset = {
24     OPCODE(OGF_CONTROLLER_BASEBAND, 0x03), ""
25 };
26 
27 hci_cmd_t hci_create_connection = {
28     OPCODE(OGF_LINK_CONTROL, 0x05), "B21121"
29     // BD_ADDR, Packet_Type, Page_Scan_Repetition_Mode, Reserved, Clock_Offset, Allow_Role_Switch
30 };
31 
32 hci_cmd_t hci_write_page_timeout = {
33     OPCODE(OGF_CONTROLLER_BASEBAND, 0x18), "2"
34     // Page_Timeout * 0.625 ms
35 };
36 
37 hci_cmd_t hci_host_buffer_size = {
38     OPCODE(OGF_CONTROLLER_BASEBAND, 0x33), "2122"
39     // Host_ACL_Data_Packet_Length:, Host_Synchronous_Data_Packet_Length:, Host_Total_Num_ACL_Data_Packets:, Host_Total_Num_Synchronous_Data_Packets:
40 };
41 
42 
43 static hci_transport_t * hci_transport;
44 static uint8_t         * hci_cmd_buffer;
45 
46 void hexdump(uint8_t *data, int size){
47     int i;
48     for (i=0; i<size;i++){
49         printf("%02X ", data[i]);
50     }
51     printf("\n");
52 }
53 
54 #if 0
55 static void *hci_daemon_thread(void *arg){
56     printf("HCI Daemon started\n");
57     hci_run(transport, &config);
58     return NULL;
59 }
60 #endif
61 
62 void hci_init(hci_transport_t *transport, void *config){
63 
64     // reference to use transport layer implementation
65     hci_transport = transport;
66 
67     // empty cmd buffer
68     hci_cmd_buffer = malloc(3+255);
69 
70     // open unix socket
71 
72     // wait for connections
73 
74     // enter loop
75 
76     // handle events
77 }
78 
79 int hci_power_control(HCI_POWER_MODE power_mode){
80     return 0;
81 }
82 
83 void hci_run(){
84     while (1) {
85         //  construct file descriptor set to wait for
86         //  select
87 
88         // for each ready file in FD - call handle_data
89         sleep(1);
90     }
91 }
92 
93 int hci_send_cmd(hci_cmd_t *cmd, ...){
94     hci_cmd_buffer[0] = cmd->opcode & 0xff;
95     hci_cmd_buffer[1] = cmd->opcode >> 8;
96     int pos = 3;
97 
98     va_list argptr;
99     va_start(argptr, cmd);
100     const char *format = cmd->format;
101     uint16_t word;
102     uint32_t longword;
103     uint8_t * bt_addr;
104     while (*format) {
105         switch(*format) {
106             case '1': //  8 bit value
107             case '2': // 16 bit value
108             case 'H': // hci_handle
109                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
110                 hci_cmd_buffer[pos++] = word & 0xff;
111                 if (*format == '2') {
112                     hci_cmd_buffer[pos++] = word >> 8;
113                 } else if (*format == 'H') {
114                     // TODO
115                 }
116                 break;
117             case '3':
118             case '4':
119                 longword = va_arg(argptr, uint32_t);
120                 // longword = va_arg(argptr, int);
121                 hci_cmd_buffer[pos++] = longword;
122                 hci_cmd_buffer[pos++] = longword >> 8;
123                 hci_cmd_buffer[pos++] = longword >> 16;
124                 if (*format == '4'){
125                     hci_cmd_buffer[pos++] = longword >> 24;
126                 }
127                 break;
128             case 'B': // bt-addr
129                 bt_addr = va_arg(argptr, uint8_t *);
130                 hci_cmd_buffer[pos++] = bt_addr[5];
131                 hci_cmd_buffer[pos++] = bt_addr[4];
132                 hci_cmd_buffer[pos++] = bt_addr[3];
133                 hci_cmd_buffer[pos++] = bt_addr[2];
134                 hci_cmd_buffer[pos++] = bt_addr[1];
135                 hci_cmd_buffer[pos++] = bt_addr[0];
136                 break;
137             default:
138                 break;
139         }
140         format++;
141     };
142     va_end(argptr);
143     hci_cmd_buffer[2] = pos - 3;
144     // send packet
145     return hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
146 }