xref: /btstack/src/hci.c (revision fe1ed1b8b680d23b4d9fe8dd47c59beebcb72020)
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 int hci_power_control(HCI_POWER_MODE power_mode){
196     if (power_mode == HCI_POWER_ON) {
197 
198         // set up state machine
199         hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent
200         hci_stack.state = HCI_STATE_INITIALIZING;
201         hci_stack.substate = 0;
202 
203         // power on
204         hci_stack.control->on(hci_stack.config);
205 
206         // open low-level device
207         hci_stack.hci_transport->open(hci_stack.config);
208 
209     } else if (power_mode == HCI_POWER_OFF){
210 
211         // close low-level device
212         hci_stack.hci_transport->close(hci_stack.config);
213 
214         // power off
215         hci_stack.control->off(hci_stack.config);
216     }
217 
218 	// trigger next/first action
219 	hci_run();
220 
221     return 0;
222 }
223 
224 void hci_run(){
225     uint8_t micro_packet;
226     switch (hci_stack.state){
227         case HCI_STATE_INITIALIZING:
228             if (hci_stack.substate % 2) {
229                 // odd: waiting for command completion
230                 return;
231             }
232             if (hci_stack.num_cmd_packets == 0) {
233                 // cannot send command yet
234                 return;
235             }
236             switch (hci_stack.substate/2){
237                 case 0:
238                     hci_send_cmd(&hci_reset);
239                     break;
240 				case 1:
241 					hci_send_cmd(&hci_read_bd_addr);
242 					break;
243                 case 2:
244                     // ca. 15 sec
245                     hci_send_cmd(&hci_write_page_timeout, 0x6000);
246                     break;
247 				case 3:
248 					hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan
249 					break;
250                 case 4:
251                     // done.
252                     hci_stack.state = HCI_STATE_WORKING;
253                     micro_packet = HCI_EVENT_BTSTACK_WORKING;
254                     hci_stack.event_packet_handler(&micro_packet, 1);
255                     break;
256                 default:
257                     break;
258             }
259             hci_stack.substate++;
260             break;
261         default:
262             break;
263     }
264 }
265 
266 
267 int hci_send_acl_packet(uint8_t *packet, int size){
268     return hci_stack.hci_transport->send_acl_packet(packet, size);
269 }
270 
271 int hci_send_cmd_packet(uint8_t *packet, int size){
272     if (READ_CMD_OGF(packet) != OGF_BTSTACK) {
273         hci_stack.num_cmd_packets--;
274         return hci_stack.hci_transport->send_cmd_packet(packet, size);
275     }
276 
277     hci_dump_packet( HCI_COMMAND_DATA_PACKET, 1, packet, size);
278 
279     // BTstack internal commands
280     uint8_t event[3];
281     switch (READ_CMD_OCF(packet)){
282         case HCI_BTSTACK_GET_STATE:
283             event[0] = HCI_EVENT_BTSTACK_STATE;
284             event[1] = 1;
285             event[2] = hci_stack.state;
286             hci_dump_packet( HCI_EVENT_PACKET, 0, event, 3);
287             hci_stack.event_packet_handler(event, 3);
288             break;
289         default:
290             // TODO log into hci dump as vendor specific "event"
291             printf("Error: command %u not implemented\n:", READ_CMD_OCF(packet));
292             break;
293     }
294     return 0;
295 }
296 
297 /**
298  * pre: numcmds >= 0 - it's allowed to send a command to the controller
299  */
300 int hci_send_cmd(hci_cmd_t *cmd, ...){
301     va_list argptr;
302     va_start(argptr, cmd);
303     uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer;
304     uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr);
305     va_end(argptr);
306     return hci_send_cmd_packet(hci_cmd_buffer, size);
307 }