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