xref: /btstack/src/hci.c (revision 94ab26f8d119a3bda5c373a8c9b3b6e636c2e919)
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 // the stack is here
59 static hci_stack_t       hci_stack;
60 
61 
62 void bt_store_16(uint8_t *buffer, uint16_t pos, uint16_t value){
63     buffer[pos++] = value;
64     buffer[pos++] = value >> 8;
65 }
66 
67 void bt_store_32(uint8_t *buffer, uint16_t pos, uint32_t value){
68     buffer[pos++] = value;
69     buffer[pos++] = value >> 8;
70     buffer[pos++] = value >> 16;
71     buffer[pos++] = value >> 24;
72 }
73 
74 void bt_flip_addr(bd_addr_t dest, bd_addr_t src){
75     dest[0] = src[5];
76     dest[1] = src[4];
77     dest[2] = src[3];
78     dest[3] = src[2];
79     dest[4] = src[1];
80     dest[5] = src[0];
81 }
82 
83 void hexdump(void *data, int size){
84     int i;
85     for (i=0; i<size;i++){
86         printf("%02X ", ((uint8_t *)data)[i]);
87     }
88     printf("\n");
89 }
90 
91 #if 0
92 static void *hci_daemon_thread(void *arg){
93     printf("HCI Daemon started\n");
94     hci_run(transport, &config);
95     return NULL;
96 }
97 #endif
98 
99 /**
100  * Linked link list
101  */
102 
103 /**
104  * get link for given address
105  *
106  * @return connection OR NULL, if not found
107  */
108 #if 0
109 static hci_connection_t *link_for_addr(bd_addr_t addr){
110     return NULL;
111 }
112 #endif
113 
114 /**
115  * Handler called by HCI transport
116  */
117 static void dummy_handler(uint8_t *packet, int size){
118 }
119 
120 static void acl_handler(uint8_t *packet, int size){
121     hci_stack.acl_packet_handler(packet, size);
122 
123     // execute main loop
124     hci_run();
125 }
126 
127 static void event_handler(uint8_t *packet, int size){
128     bd_addr_t addr;
129 
130     // Get Num_HCI_Command_Packets
131     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
132         packet[0] == HCI_EVENT_COMMAND_STATUS){
133         hci_stack.num_cmd_packets = packet[2];
134     }
135 
136     // handle BT initialization
137     if (hci_stack.state == HCI_STATE_INITIALIZING){
138         // handle H4 synchronization loss on restart
139         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
140         //    hci_stack.substate = 0;
141         // }
142         // handle normal init sequence
143         if (hci_stack.substate % 2){
144             // odd: waiting for event
145             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
146                 hci_stack.substate++;
147             }
148         }
149     }
150 
151     // link key request
152     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
153         bt_flip_addr(addr, &packet[2]);
154         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
155         return;
156     }
157 
158     // pin code request
159     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
160         bt_flip_addr(addr, &packet[2]);
161         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
162     }
163 
164     hci_stack.event_packet_handler(packet, size);
165 
166 	// execute main loop
167 	hci_run();
168 }
169 
170 /** Register L2CAP handlers */
171 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, int size)){
172     hci_stack.event_packet_handler = handler;
173 }
174 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, int size)){
175     hci_stack.acl_packet_handler = handler;
176 }
177 
178 static int null_control_function(void *config){
179     return 0;
180 }
181 static const char * null_control_name(void *config){
182     return "Hardware unknown";
183 }
184 
185 static bt_control_t null_control = {
186     null_control_function,
187     null_control_function,
188     null_control_function,
189     null_control_name
190 };
191 
192 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
193 
194     // reference to use transport layer implementation
195     hci_stack.hci_transport = transport;
196 
197     // references to used control implementation
198     if (control) {
199         hci_stack.control = control;
200     } else {
201         hci_stack.control = &null_control;
202     }
203 
204     // reference to used config
205     hci_stack.config = config;
206 
207     // empty cmd buffer
208     hci_stack.hci_cmd_buffer = malloc(3+255);
209 
210     // higher level handler
211     hci_stack.event_packet_handler = dummy_handler;
212     hci_stack.acl_packet_handler = dummy_handler;
213 
214     // register packet handlers with transport
215     transport->register_event_packet_handler( event_handler);
216     transport->register_acl_packet_handler( acl_handler);
217 
218     // turn on
219     hci_power_control(HCI_POWER_ON);
220 }
221 
222 int hci_power_control(HCI_POWER_MODE power_mode){
223     if (power_mode == HCI_POWER_ON) {
224 
225         // set up state machine
226         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
227         hci_stack.state = HCI_STATE_INITIALIZING;
228         hci_stack.substate = 0;
229 
230         // power on
231         hci_stack.control->on(hci_stack.config);
232 
233         // open low-level device
234         hci_stack.hci_transport->open(hci_stack.config);
235 
236     } else if (power_mode == HCI_POWER_OFF){
237 
238         // close low-level device
239         hci_stack.hci_transport->close(hci_stack.config);
240 
241         // power off
242         hci_stack.control->off(hci_stack.config);
243     }
244     return 0;
245 }
246 
247 uint32_t hci_run(){
248     uint8_t micro_packet;
249     switch (hci_stack.state){
250         case HCI_STATE_INITIALIZING:
251             if (hci_stack.substate % 2) {
252                 // odd: waiting for command completion
253                 return 0;
254             }
255             if (hci_stack.num_cmd_packets == 0) {
256                 // cannot send command yet
257                 return 0;
258             }
259             switch (hci_stack.substate/2){
260                 case 0:
261                     hci_send_cmd(&hci_reset);
262                     break;
263                 case 1:
264                     // ca. 15 sec
265                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
266                     break;
267                 case 2:
268                     // done.
269                     hci_stack.state = HCI_STATE_WORKING;
270                     micro_packet = BTSTACK_EVENT_HCI_WORKING;
271                     hci_stack.event_packet_handler(&micro_packet, 1);
272                     break;
273                 default:
274                     break;
275             }
276             hci_stack.substate++;
277             break;
278         default:
279             break;
280     }
281 
282     // don't check for timetous yet
283     return 0;
284 }
285 
286 
287 int hci_send_acl_packet(uint8_t *packet, int size){
288     return hci_stack.hci_transport->send_acl_packet(packet, size);
289 }
290 
291 
292 /**
293  * pre: numcmds >= 0 - it's allowed to send a command to the controller
294  */
295 int hci_send_cmd(hci_cmd_t *cmd, ...){
296     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
297     hci_cmd_buffer[0] = cmd->opcode & 0xff;
298     hci_cmd_buffer[1] = cmd->opcode >> 8;
299     int pos = 3;
300 
301     va_list argptr;
302     va_start(argptr, cmd);
303     const char *format = cmd->format;
304     uint16_t word;
305     uint32_t longword;
306     uint8_t * ptr;
307     while (*format) {
308         switch(*format) {
309             case '1': //  8 bit value
310             case '2': // 16 bit value
311             case 'H': // hci_handle
312                 word = va_arg(argptr, int);  // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
313                 hci_cmd_buffer[pos++] = word & 0xff;
314                 if (*format == '2') {
315                     hci_cmd_buffer[pos++] = word >> 8;
316                 } else if (*format == 'H') {
317                     // TODO
318                 }
319                 break;
320             case '3':
321             case '4':
322                 longword = va_arg(argptr, uint32_t);
323                 // longword = va_arg(argptr, int);
324                 hci_cmd_buffer[pos++] = longword;
325                 hci_cmd_buffer[pos++] = longword >> 8;
326                 hci_cmd_buffer[pos++] = longword >> 16;
327                 if (*format == '4'){
328                     hci_cmd_buffer[pos++] = longword >> 24;
329                 }
330                 break;
331             case 'B': // bt-addr
332                 ptr = va_arg(argptr, uint8_t *);
333                 hci_cmd_buffer[pos++] = ptr[5];
334                 hci_cmd_buffer[pos++] = ptr[4];
335                 hci_cmd_buffer[pos++] = ptr[3];
336                 hci_cmd_buffer[pos++] = ptr[2];
337                 hci_cmd_buffer[pos++] = ptr[1];
338                 hci_cmd_buffer[pos++] = ptr[0];
339                 break;
340             case 'P': // c string passed as pascal string with leading 1-byte len
341                 ptr = va_arg(argptr, uint8_t *);
342                 memcpy(&hci_cmd_buffer[pos], ptr, 16);
343                 pos += 16;
344                 break;
345             default:
346                 break;
347         }
348         format++;
349     };
350     va_end(argptr);
351     hci_cmd_buffer[2] = pos - 3;
352     // send packet
353     hci_stack.num_cmd_packets--;
354     return hci_stack.hci_transport->send_cmd_packet(hci_cmd_buffer, pos);
355 }