1 /* 2 * Copyright (C) 2009 by Matthias Ringwald 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 21 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 24 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 27 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * SUCH DAMAGE. 29 * 30 */ 31 32 /* 33 * hci.c 34 * 35 * Created by Matthias Ringwald on 4/29/09. 36 * 37 */ 38 39 #include <unistd.h> 40 #include <stdarg.h> 41 #include <string.h> 42 #include <stdio.h> 43 #include "hci.h" 44 #include "hci_dump.h" 45 46 // temp 47 #include "l2cap.h" 48 49 #define HCI_CONNECTION_TIMEOUT_MS 5000 50 51 // the STACK is here 52 static hci_stack_t hci_stack; 53 54 /** 55 * get connection for a given handle 56 * 57 * @return connection OR NULL, if not found 58 */ 59 static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 60 linked_item_t *it; 61 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 62 if ( ((hci_connection_t *) it)->con_handle == con_handle){ 63 return (hci_connection_t *) it; 64 } 65 } 66 return NULL; 67 } 68 69 static void hci_connection_timeout_handler(timer_source_t *timer){ 70 hci_connection_t * connection = linked_item_get_user(&timer->item); 71 struct timeval tv; 72 gettimeofday(&tv, NULL); 73 if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 74 // connections might be timed out 75 hci_emit_l2cap_check_timeout(connection); 76 run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 77 } else { 78 // next timeout check at 79 timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000; 80 } 81 run_loop_add_timer(timer); 82 } 83 84 static void hci_connection_timestamp(hci_connection_t *connection){ 85 gettimeofday(&connection->timestamp, NULL); 86 } 87 88 static void hci_connection_update_timestamp_for_acl(uint8_t *packet) { 89 // update timestamp 90 hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 91 hci_connection_t *connection = connection_for_handle( con_handle); 92 if (connection) hci_connection_timestamp(connection); 93 } 94 95 /** 96 * create connection for given address 97 * 98 * @return connection OR NULL, if not found 99 */ 100 static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 101 hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 102 if (!conn) return NULL; 103 BD_ADDR_COPY(conn->address, addr); 104 conn->con_handle = 0xffff; 105 conn->flags = 0; 106 linked_item_set_user(&conn->timeout.item, conn); 107 conn->timeout.process = hci_connection_timeout_handler; 108 hci_connection_timestamp(conn); 109 linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 110 return conn; 111 } 112 113 /** 114 * get connection for given address 115 * 116 * @return connection OR NULL, if not found 117 */ 118 static hci_connection_t * connection_for_address(bd_addr_t address){ 119 linked_item_t *it; 120 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 121 if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 122 return (hci_connection_t *) it; 123 } 124 } 125 return NULL; 126 } 127 128 /** 129 * count connections 130 */ 131 static int nr_hci_connections(){ 132 int count = 0; 133 linked_item_t *it; 134 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 135 return count; 136 } 137 138 /** 139 * Dummy handler called by HCI 140 */ 141 static void dummy_handler(uint8_t *packet, uint16_t size){ 142 } 143 144 /** 145 * Dummy control handler 146 */ 147 static int null_control_function(void *config){ 148 return 0; 149 } 150 static const char * null_control_name(void *config){ 151 return "Hardware unknown"; 152 } 153 static bt_control_t null_control = { 154 null_control_function, 155 null_control_function, 156 null_control_function, 157 null_control_name 158 }; 159 160 161 int hci_send_acl_packet(uint8_t *packet, int size){ 162 hci_connection_update_timestamp_for_acl(packet); 163 return hci_stack.hci_transport->send_acl_packet(packet, size); 164 } 165 166 static void acl_handler(uint8_t *packet, int size){ 167 hci_connection_update_timestamp_for_acl(packet); 168 hci_stack.acl_packet_handler(packet, size); 169 170 // execute main loop 171 hci_run(); 172 } 173 174 static void event_handler(uint8_t *packet, int size){ 175 bd_addr_t addr; 176 hci_con_handle_t handle; 177 178 // Get Num_HCI_Command_Packets 179 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || 180 packet[0] == HCI_EVENT_COMMAND_STATUS){ 181 hci_stack.num_cmd_packets = packet[2]; 182 } 183 184 // Connection management 185 if (packet[0] == HCI_EVENT_CONNECTION_COMPLETE) { 186 bt_flip_addr(addr, &packet[5]); 187 printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); 188 hci_connection_t * conn = connection_for_address(addr); 189 if (conn) { 190 if (!packet[2]){ 191 conn->state = OPEN; 192 conn->con_handle = READ_BT_16(packet, 3); 193 conn->flags = 0; 194 195 gettimeofday(&conn->timestamp, NULL); 196 run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 197 run_loop_add_timer(&conn->timeout); 198 199 printf("New connection: handle %u, ", conn->con_handle); 200 print_bd_addr( conn->address ); 201 printf("\n"); 202 203 hci_emit_nr_connections_changed(); 204 } else { 205 // connection failed, remove entry 206 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 207 free( conn ); 208 } 209 } 210 } 211 212 if (packet[0] == HCI_EVENT_DISCONNECTION_COMPLETE) { 213 if (!packet[2]){ 214 handle = READ_BT_16(packet, 3); 215 hci_connection_t * conn = connection_for_handle(handle); 216 if (conn) { 217 printf("Connection closed: handle %u, ", conn->con_handle); 218 print_bd_addr( conn->address ); 219 printf("\n"); 220 run_loop_remove_timer(&conn->timeout); 221 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 222 free( conn ); 223 hci_emit_nr_connections_changed(); 224 } 225 } 226 } 227 228 // handle BT initialization 229 if (hci_stack.state == HCI_STATE_INITIALIZING){ 230 // handle H4 synchronization loss on restart 231 // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 232 // hci_stack.substate = 0; 233 // } 234 // handle normal init sequence 235 if (hci_stack.substate % 2){ 236 // odd: waiting for event 237 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 238 hci_stack.substate++; 239 } 240 } 241 } 242 243 // link key request 244 if (packet[0] == HCI_EVENT_LINK_KEY_REQUEST){ 245 bt_flip_addr(addr, &packet[2]); 246 hci_send_cmd(&hci_link_key_request_negative_reply, &addr); 247 return; 248 } 249 250 hci_stack.event_packet_handler(packet, size); 251 252 // execute main loop 253 hci_run(); 254 } 255 256 /** Register HCI packet handlers */ 257 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 258 hci_stack.event_packet_handler = handler; 259 } 260 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 261 hci_stack.acl_packet_handler = handler; 262 } 263 264 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 265 266 // reference to use transport layer implementation 267 hci_stack.hci_transport = transport; 268 269 // references to used control implementation 270 if (control) { 271 hci_stack.control = control; 272 } else { 273 hci_stack.control = &null_control; 274 } 275 276 // reference to used config 277 hci_stack.config = config; 278 279 // no connections yet 280 hci_stack.connections = NULL; 281 282 // empty cmd buffer 283 hci_stack.hci_cmd_buffer = malloc(3+255); 284 285 // higher level handler 286 hci_stack.event_packet_handler = dummy_handler; 287 hci_stack.acl_packet_handler = dummy_handler; 288 289 // register packet handlers with transport 290 transport->register_event_packet_handler( event_handler); 291 transport->register_acl_packet_handler( acl_handler); 292 } 293 294 int hci_power_control(HCI_POWER_MODE power_mode){ 295 if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 296 297 // power on 298 int err = hci_stack.control->on(hci_stack.config); 299 if (err){ 300 fprintf(stderr, "POWER_ON failed\n"); 301 hci_emit_hci_open_failed(); 302 return err; 303 } 304 305 // open low-level device 306 err = hci_stack.hci_transport->open(hci_stack.config); 307 if (err){ 308 fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 309 hci_stack.control->off(hci_stack.config); 310 hci_emit_hci_open_failed(); 311 return err; 312 } 313 314 // set up state machine 315 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 316 hci_stack.state = HCI_STATE_INITIALIZING; 317 hci_stack.substate = 0; 318 319 } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 320 321 // close low-level device 322 hci_stack.hci_transport->close(hci_stack.config); 323 324 // power off 325 hci_stack.control->off(hci_stack.config); 326 327 // we're off now 328 hci_stack.state = HCI_STATE_OFF; 329 } 330 331 // create internal event 332 hci_emit_state(); 333 334 // trigger next/first action 335 hci_run(); 336 337 return 0; 338 } 339 340 void hci_run(){ 341 switch (hci_stack.state){ 342 case HCI_STATE_INITIALIZING: 343 if (hci_stack.substate % 2) { 344 // odd: waiting for command completion 345 return; 346 } 347 if (hci_stack.num_cmd_packets == 0) { 348 // cannot send command yet 349 return; 350 } 351 switch (hci_stack.substate/2){ 352 case 0: 353 hci_send_cmd(&hci_reset); 354 break; 355 case 1: 356 hci_send_cmd(&hci_read_bd_addr); 357 break; 358 case 2: 359 // ca. 15 sec 360 hci_send_cmd(&hci_write_page_timeout, 0x6000); 361 break; 362 case 3: 363 hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 364 break; 365 case 4: 366 // done. 367 hci_stack.state = HCI_STATE_WORKING; 368 hci_emit_state(); 369 break; 370 default: 371 break; 372 } 373 hci_stack.substate++; 374 break; 375 default: 376 break; 377 } 378 } 379 380 int hci_send_cmd_packet(uint8_t *packet, int size){ 381 bd_addr_t addr; 382 hci_connection_t * conn; 383 // house-keeping 384 385 // create_connection? 386 if (IS_COMMAND(packet, hci_create_connection)){ 387 bt_flip_addr(addr, &packet[3]); 388 printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 389 conn = connection_for_address(addr); 390 if (conn) { 391 // if connection exists 392 if (conn->state == OPEN) { 393 // if OPEN, emit connection complete command 394 hci_emit_connection_complete(conn); 395 } 396 // otherwise, just ignore 397 return 0; // don't sent packet to controller 398 399 } else{ 400 conn = create_connection_for_addr(addr); 401 if (conn){ 402 // create connection struct and register, state = SENT_CREATE_CONNECTION 403 conn->state = SENT_CREATE_CONNECTION; 404 } 405 } 406 } 407 408 // accept connection 409 410 // reject connection 411 412 // close_connection? 413 // set state = SENT_DISCONNECT 414 415 hci_stack.num_cmd_packets--; 416 return hci_stack.hci_transport->send_cmd_packet(packet, size); 417 } 418 419 /** 420 * pre: numcmds >= 0 - it's allowed to send a command to the controller 421 */ 422 int hci_send_cmd(hci_cmd_t *cmd, ...){ 423 va_list argptr; 424 va_start(argptr, cmd); 425 uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 426 uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 427 va_end(argptr); 428 return hci_send_cmd_packet(hci_cmd_buffer, size); 429 } 430 431 // Create various non-HCI events. 432 // TODO: generalize, use table similar to hci_create_command 433 434 void hci_emit_state(){ 435 uint8_t len = 3; 436 uint8_t event[len]; 437 event[0] = BTSTACK_EVENT_STATE; 438 event[1] = 1; 439 event[2] = hci_stack.state; 440 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 441 hci_stack.event_packet_handler(event, len); 442 } 443 444 void hci_emit_connection_complete(hci_connection_t *conn){ 445 uint8_t len = 13; 446 uint8_t event[len]; 447 event[0] = HCI_EVENT_CONNECTION_COMPLETE; 448 event[2] = 0; // status = OK 449 bt_store_16(event, 3, conn->con_handle); 450 bt_flip_addr(&event[5], conn->address); 451 event[11] = 1; // ACL connection 452 event[12] = 0; // encryption disabled 453 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 454 hci_stack.event_packet_handler(event, len); 455 } 456 457 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 458 uint8_t len = 4; 459 uint8_t event[len]; 460 event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 461 event[1] = 2; 462 bt_store_16(event, 2, conn->con_handle); 463 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 464 hci_stack.event_packet_handler(event, len); 465 } 466 467 void hci_emit_nr_connections_changed(){ 468 uint8_t len = 3; 469 uint8_t event[len]; 470 event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 471 event[1] = 1; 472 event[2] = nr_hci_connections(); 473 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 474 hci_stack.event_packet_handler(event, len); 475 } 476 477 void hci_emit_hci_open_failed(){ 478 uint8_t len = 1; 479 uint8_t event[len]; 480 event[0] = BTSTACK_EVENT_POWERON_FAILED; 481 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 482 hci_stack.event_packet_handler(event, len); 483 } 484