xref: /btstack/src/hci.c (revision c8e4258af80ac01c2b8c90df5fb94a264ad985db)
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 #include "hci_dump.h"
14 
15 // temp
16 #include "l2cap.h"
17 
18 // the STACK is here
19 static hci_stack_t       hci_stack;
20 
21 /**
22  * create connection for given address
23  *
24  * @return connection OR NULL, if not found
25  */
26 static hci_connection_t * create_connection_for_addr(bd_addr_t addr){
27     hci_connection_t * conn = malloc( sizeof(hci_connection_t) );
28     if (!conn) return NULL;
29     BD_ADDR_COPY(conn->address, addr);
30     conn->con_handle = 0xffff;
31     conn->flags = 0;
32     linked_list_add(&hci_stack.connections, (linked_item_t *) conn);
33     return conn;
34 }
35 
36 /**
37  * get connection for given address
38  *
39  * @return connection OR NULL, if not found
40  */
41 static hci_connection_t * connection_for_address(bd_addr_t address){
42     linked_item_t *it;
43     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
44         if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){
45             return (hci_connection_t *) it;
46         }
47     }
48     return NULL;
49 }
50 
51 /**
52  * get connection for a given handle
53  *
54  * @return connection OR NULL, if not found
55  */
56 static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){
57     linked_item_t *it;
58     for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){
59         if ( ((hci_connection_t *) it)->con_handle == con_handle){
60             return (hci_connection_t *) it;
61         }
62     }
63     return NULL;
64 }
65 
66 
67 /**
68  * Dummy handler called by HCI
69  */
70 static void dummy_handler(uint8_t *packet, uint16_t size){
71 }
72 
73 /**
74  * Dummy control handler
75  */
76 static int null_control_function(void *config){
77     return 0;
78 }
79 static const char * null_control_name(void *config){
80     return "Hardware unknown";
81 }
82 static bt_control_t null_control = {
83     null_control_function,
84     null_control_function,
85     null_control_function,
86     null_control_name
87 };
88 
89 
90 static void acl_handler(uint8_t *packet, int size){
91     hci_stack.acl_packet_handler(packet, size);
92 
93     // execute main loop
94     hci_run();
95 }
96 
97 static void event_handler(uint8_t *packet, int size){
98     bd_addr_t addr;
99     hci_con_handle_t handle;
100 
101     // Get Num_HCI_Command_Packets
102     if (packet[0] == HCI_EVENT_COMMAND_COMPLETE ||
103         packet[0] == HCI_EVENT_COMMAND_STATUS){
104         hci_stack.num_cmd_packets = packet[2];
105     }
106 
107     // Connection management
108     if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) {
109         if (!packet[2]){
110             bt_flip_addr(addr, &packet[5]);
111             printf("Connection_complete "); print_bd_addr(addr); printf("\n");
112             hci_connection_t * conn = connection_for_address(addr);
113             if (conn) {
114                 conn->state = OPEN;
115                 conn->con_handle = READ_BT_16(packet, 3);
116                 conn->flags = 0;
117                 printf("New connection: handle %u, ", conn->con_handle);
118                 print_bd_addr( conn->address );
119                 printf("\n");
120             }
121         }
122     }
123 
124     if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) {
125         if (!packet[2]){
126             handle = READ_BT_16(packet, 3);
127             hci_connection_t * conn = connection_for_handle(handle);
128             if (conn) {
129                 printf("Connection closed: handle %u, ", conn->con_handle);
130                 print_bd_addr( conn->address );
131                 printf("\n");
132                 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn);
133                 free( conn );
134             }
135         }
136     }
137 
138     // handle BT initialization
139     if (hci_stack.state == HCI_STATE_INITIALIZING){
140         // handle H4 synchronization loss on restart
141         // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){
142         //    hci_stack.substate = 0;
143         // }
144         // handle normal init sequence
145         if (hci_stack.substate % 2){
146             // odd: waiting for event
147             if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){
148                 hci_stack.substate++;
149             }
150         }
151     }
152 
153     // link key request
154     if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){
155         bt_flip_addr(addr, &packet[2]);
156         hci_send_cmd(&hci_link_key_request_negative_reply, &addr);
157         return;
158     }
159 
160     // pin code request
161     if (packet[0] == HCI_EVENT_PIN_CODE_REQUEST){
162         bt_flip_addr(addr, &packet[2]);
163         hci_send_cmd(&hci_pin_code_request_reply, &addr, 4, "1234");
164     }
165 
166     hci_stack.event_packet_handler(packet, size);
167 
168 	// execute main loop
169 	hci_run();
170 }
171 
172 /** Register HCI packet handlers */
173 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){
174     hci_stack.event_packet_handler = handler;
175 }
176 void hci_register_acl_packet_handler  (void (*handler)(uint8_t *packet, uint16_t size)){
177     hci_stack.acl_packet_handler = handler;
178 }
179 
180 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){
181 
182     // reference to use transport layer implementation
183     hci_stack.hci_transport = transport;
184 
185     // references to used control implementation
186     if (control) {
187         hci_stack.control = control;
188     } else {
189         hci_stack.control = &null_control;
190     }
191 
192     // reference to used config
193     hci_stack.config = config;
194 
195     // no connections yet
196     hci_stack.connections = NULL;
197 
198     // empty cmd buffer
199     hci_stack.hci_cmd_buffer = malloc(3+255);
200 
201     // higher level handler
202     hci_stack.event_packet_handler = dummy_handler;
203     hci_stack.acl_packet_handler = dummy_handler;
204 
205     // register packet handlers with transport
206     transport->register_event_packet_handler( event_handler);
207     transport->register_acl_packet_handler( acl_handler);
208 }
209 
210 int hci_power_control(HCI_POWER_MODE power_mode){
211     if (power_mode == HCI_POWER_ON) {
212 
213         // set up state machine
214         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
215         hci_stack.state = HCI_STATE_INITIALIZING;
216         hci_stack.substate = 0;
217 
218         // power on
219         hci_stack.control->on(hci_stack.config);
220 
221         // open low-level device
222         hci_stack.hci_transport->open(hci_stack.config);
223 
224         // create internal event
225 
226     } else if (power_mode == HCI_POWER_OFF){
227 
228         // close low-level device
229         hci_stack.hci_transport->close(hci_stack.config);
230 
231         // power off
232         hci_stack.control->off(hci_stack.config);
233     }
234 
235 	hci_emit_state();
236 
237 	// trigger next/first action
238 	hci_run();
239 
240     return 0;
241 }
242 
243 void hci_run(){
244     uint8_t micro_packet;
245     switch (hci_stack.state){
246         case HCI_STATE_INITIALIZING:
247             if (hci_stack.substate % 2) {
248                 // odd: waiting for command completion
249                 return;
250             }
251             if (hci_stack.num_cmd_packets == 0) {
252                 // cannot send command yet
253                 return;
254             }
255             switch (hci_stack.substate/2){
256                 case 0:
257                     hci_send_cmd(&hci_reset);
258                     break;
259 				case 1:
260 					hci_send_cmd(&hci_read_bd_addr);
261 					break;
262                 case 2:
263                     // ca. 15 sec
264                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
265                     break;
266 				case 3:
267 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
268 					break;
269                 case 4:
270                     // done.
271                     hci_stack.state = HCI_STATE_WORKING;
272                     micro_packet = HCI_EVENT_BTSTACK_WORKING;
273                     hci_stack.event_packet_handler(&micro_packet, 1);
274                     break;
275                 default:
276                     break;
277             }
278             hci_stack.substate++;
279             break;
280         default:
281             break;
282     }
283 }
284 
285 
286 int hci_send_acl_packet(uint8_t *packet, int size){
287     return hci_stack.hci_transport->send_acl_packet(packet, size);
288 }
289 
290 int hci_send_cmd_packet(uint8_t *packet, int size){
291     bd_addr_t addr;
292     hci_connection_t * conn;
293     // house-keeping
294 
295     // create_connection?
296     if (IS_COMMAND(packet, hci_create_connection)){
297         bt_flip_addr(addr, &packet[3]);
298         printf("Create_connection to "); print_bd_addr(addr); printf("\n");
299         conn = connection_for_address(addr);
300         if (conn) {
301             // if connection exists
302             if (conn->state == OPEN) {
303                 // if OPEN, emit connection complete command
304                 hci_emit_connection_complete(conn);
305             }
306             //    otherwise, just ignore
307             return 0; // don't sent packet to controller
308 
309         } else{
310             conn = create_connection_for_addr(addr);
311             if (conn){
312                 //    create connection struct and register, state = SENT_CREATE_CONNECTION
313                 conn->state = SENT_CREATE_CONNECTION;
314             }
315         }
316     }
317 
318     // accept connection
319 
320     // reject connection
321 
322     // close_connection?
323       // set state = SENT_DISCONNECT
324 
325     hci_stack.num_cmd_packets--;
326     return hci_stack.hci_transport->send_cmd_packet(packet, size);
327 }
328 
329 /**
330  * pre: numcmds >= 0 - it's allowed to send a command to the controller
331  */
332 int hci_send_cmd(hci_cmd_t *cmd, ...){
333     va_list argptr;
334     va_start(argptr, cmd);
335     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
336     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
337     va_end(argptr);
338     return hci_send_cmd_packet(hci_cmd_buffer, size);
339 }
340 
341 void hci_emit_state(){
342     uint8_t len = 3;
343     uint8_t event[len];
344     event[0] = HCI_EVENT_BTSTACK_STATE;
345     event[1] = 1;
346     event[2] = hci_stack.state;
347     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
348     hci_stack.event_packet_handler(event, len);
349 }
350 
351 void hci_emit_connection_complete(hci_connection_t *conn){
352     uint8_t len = 13;
353     uint8_t event[len];
354     event[0] = HCI_EVENT_CONNECTION_COMPLETE;
355     event[2] = 0; // status = OK
356     bt_store_16(event, 3, conn->con_handle);
357     bt_flip_addr(&event[5], conn->address);
358     event[11] = 1; // ACL connection
359     event[12] = 0; // encryption disabled
360     hci_dump_packet( HCI_EVENT_PACKET, 0, event, len);
361     hci_stack.event_packet_handler(event, len);
362 }
363 
364