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