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