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 #include "../include/btstack/hci_cmds.h" 47 #include "../include/btstack/version.h" 48 49 // temp 50 #include "l2cap.h" 51 52 #define HCI_CONNECTION_TIMEOUT_MS 5000 53 54 // the STACK is here 55 static hci_stack_t hci_stack; 56 57 /** 58 * get connection for a given handle 59 * 60 * @return connection OR NULL, if not found 61 */ 62 static hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 63 linked_item_t *it; 64 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 65 if ( ((hci_connection_t *) it)->con_handle == con_handle){ 66 return (hci_connection_t *) it; 67 } 68 } 69 return NULL; 70 } 71 72 static void hci_connection_timeout_handler(timer_source_t *timer){ 73 hci_connection_t * connection = linked_item_get_user(&timer->item); 74 struct timeval tv; 75 gettimeofday(&tv, NULL); 76 if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 77 // connections might be timed out 78 hci_emit_l2cap_check_timeout(connection); 79 run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 80 } else { 81 // next timeout check at 82 timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000; 83 } 84 run_loop_add_timer(timer); 85 } 86 87 static void hci_connection_timestamp(hci_connection_t *connection){ 88 gettimeofday(&connection->timestamp, NULL); 89 } 90 91 static void hci_connection_update_timestamp_for_acl(uint8_t *packet) { 92 // update timestamp 93 hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 94 hci_connection_t *connection = connection_for_handle( con_handle); 95 if (connection) hci_connection_timestamp(connection); 96 } 97 98 /** 99 * create connection for given address 100 * 101 * @return connection OR NULL, if not found 102 */ 103 static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 104 hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 105 if (!conn) return NULL; 106 BD_ADDR_COPY(conn->address, addr); 107 conn->con_handle = 0xffff; 108 conn->flags = 0; 109 linked_item_set_user(&conn->timeout.item, conn); 110 conn->timeout.process = hci_connection_timeout_handler; 111 hci_connection_timestamp(conn); 112 linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 113 return conn; 114 } 115 116 /** 117 * get connection for given address 118 * 119 * @return connection OR NULL, if not found 120 */ 121 static hci_connection_t * connection_for_address(bd_addr_t address){ 122 linked_item_t *it; 123 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 124 if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 125 return (hci_connection_t *) it; 126 } 127 } 128 return NULL; 129 } 130 131 /** 132 * count connections 133 */ 134 static int nr_hci_connections(){ 135 int count = 0; 136 linked_item_t *it; 137 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 138 return count; 139 } 140 141 /** 142 * Dummy handler called by HCI 143 */ 144 static void dummy_handler(uint8_t *packet, uint16_t size){ 145 } 146 147 /** 148 * Dummy control handler 149 */ 150 static int null_control_function(void *config){ 151 return 0; 152 } 153 static const char * null_control_name(void *config){ 154 return "Hardware unknown"; 155 } 156 static bt_control_t null_control = { 157 null_control_function, 158 null_control_function, 159 null_control_function, 160 null_control_name 161 }; 162 163 164 int hci_send_acl_packet(uint8_t *packet, int size){ 165 hci_connection_update_timestamp_for_acl(packet); 166 return hci_stack.hci_transport->send_acl_packet(packet, size); 167 } 168 169 static void acl_handler(uint8_t *packet, int size){ 170 hci_connection_update_timestamp_for_acl(packet); 171 hci_stack.acl_packet_handler(packet, size); 172 173 // execute main loop 174 hci_run(); 175 } 176 177 static void event_handler(uint8_t *packet, int size){ 178 bd_addr_t addr; 179 hci_con_handle_t handle; 180 hci_connection_t * conn; 181 182 switch (packet[0]) { 183 184 case HCI_EVENT_COMMAND_COMPLETE: 185 case HCI_EVENT_COMMAND_STATUS: 186 // Get Num_HCI_Command_Packets 187 hci_stack.num_cmd_packets = packet[2]; 188 break; 189 190 case HCI_EVENT_CONNECTION_REQUEST: 191 bt_flip_addr(addr, &packet[5]); 192 printf("Connection_incoming: "); print_bd_addr(addr); printf("\n"); 193 conn = connection_for_address(addr); 194 if (!conn) { 195 conn = create_connection_for_addr(addr); 196 } 197 // TODO: check for malloc failure 198 conn->state = ACCEPTED_CONNECTION_REQUEST; 199 hci_send_cmd(&hci_accept_connection_request, 1); 200 break; 201 202 case HCI_EVENT_CONNECTION_COMPLETE: 203 // Connection management 204 bt_flip_addr(addr, &packet[5]); 205 printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); 206 conn = connection_for_address(addr); 207 if (conn) { 208 if (!packet[2]){ 209 conn->state = OPEN; 210 conn->con_handle = READ_BT_16(packet, 3); 211 conn->flags = 0; 212 213 gettimeofday(&conn->timestamp, NULL); 214 run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 215 run_loop_add_timer(&conn->timeout); 216 217 printf("New connection: handle %u, ", conn->con_handle); 218 print_bd_addr( conn->address ); 219 printf("\n"); 220 221 hci_emit_nr_connections_changed(); 222 } else { 223 // connection failed, remove entry 224 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 225 free( conn ); 226 } 227 } 228 break; 229 230 case HCI_EVENT_DISCONNECTION_COMPLETE: 231 if (!packet[2]){ 232 handle = READ_BT_16(packet, 3); 233 hci_connection_t * conn = connection_for_handle(handle); 234 if (conn) { 235 printf("Connection closed: handle %u, ", conn->con_handle); 236 print_bd_addr( conn->address ); 237 printf("\n"); 238 run_loop_remove_timer(&conn->timeout); 239 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 240 free( conn ); 241 hci_emit_nr_connections_changed(); 242 } 243 } 244 break; 245 246 default: 247 break; 248 } 249 250 // handle BT initialization 251 if (hci_stack.state == HCI_STATE_INITIALIZING){ 252 // handle H4 synchronization loss on restart 253 // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 254 // hci_stack.substate = 0; 255 // } 256 // handle normal init sequence 257 if (hci_stack.substate % 2){ 258 // odd: waiting for event 259 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 260 hci_stack.substate++; 261 } 262 } 263 } 264 265 hci_stack.event_packet_handler(packet, size); 266 267 // execute main loop 268 hci_run(); 269 } 270 271 /** Register HCI packet handlers */ 272 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 273 hci_stack.event_packet_handler = handler; 274 } 275 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 276 hci_stack.acl_packet_handler = handler; 277 } 278 279 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 280 281 // reference to use transport layer implementation 282 hci_stack.hci_transport = transport; 283 284 // references to used control implementation 285 if (control) { 286 hci_stack.control = control; 287 } else { 288 hci_stack.control = &null_control; 289 } 290 291 // reference to used config 292 hci_stack.config = config; 293 294 // no connections yet 295 hci_stack.connections = NULL; 296 297 // empty cmd buffer 298 hci_stack.hci_cmd_buffer = malloc(3+255); 299 300 // higher level handler 301 hci_stack.event_packet_handler = dummy_handler; 302 hci_stack.acl_packet_handler = dummy_handler; 303 304 // register packet handlers with transport 305 transport->register_event_packet_handler( event_handler); 306 transport->register_acl_packet_handler( acl_handler); 307 } 308 309 int hci_power_control(HCI_POWER_MODE power_mode){ 310 if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 311 312 // power on 313 int err = hci_stack.control->on(hci_stack.config); 314 if (err){ 315 fprintf(stderr, "POWER_ON failed\n"); 316 hci_emit_hci_open_failed(); 317 return err; 318 } 319 320 // open low-level device 321 err = hci_stack.hci_transport->open(hci_stack.config); 322 if (err){ 323 fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 324 hci_stack.control->off(hci_stack.config); 325 hci_emit_hci_open_failed(); 326 return err; 327 } 328 329 // set up state machine 330 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 331 hci_stack.state = HCI_STATE_INITIALIZING; 332 hci_stack.substate = 0; 333 334 } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 335 336 // close low-level device 337 hci_stack.hci_transport->close(hci_stack.config); 338 339 // power off 340 hci_stack.control->off(hci_stack.config); 341 342 // we're off now 343 hci_stack.state = HCI_STATE_OFF; 344 } 345 346 // create internal event 347 hci_emit_state(); 348 349 // trigger next/first action 350 hci_run(); 351 352 return 0; 353 } 354 355 void hci_run(){ 356 switch (hci_stack.state){ 357 case HCI_STATE_INITIALIZING: 358 if (hci_stack.substate % 2) { 359 // odd: waiting for command completion 360 return; 361 } 362 if (hci_stack.num_cmd_packets == 0) { 363 // cannot send command yet 364 return; 365 } 366 switch (hci_stack.substate/2){ 367 case 0: 368 hci_send_cmd(&hci_reset); 369 break; 370 case 1: 371 hci_send_cmd(&hci_read_bd_addr); 372 break; 373 case 2: 374 // ca. 15 sec 375 hci_send_cmd(&hci_write_page_timeout, 0x6000); 376 break; 377 case 3: 378 hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 379 break; 380 case 4: 381 // done. 382 hci_stack.state = HCI_STATE_WORKING; 383 hci_emit_state(); 384 break; 385 default: 386 break; 387 } 388 hci_stack.substate++; 389 break; 390 default: 391 break; 392 } 393 } 394 395 int hci_send_cmd_packet(uint8_t *packet, int size){ 396 bd_addr_t addr; 397 hci_connection_t * conn; 398 // house-keeping 399 400 // create_connection? 401 if (IS_COMMAND(packet, hci_create_connection)){ 402 bt_flip_addr(addr, &packet[3]); 403 printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 404 conn = connection_for_address(addr); 405 if (conn) { 406 // if connection exists 407 if (conn->state == OPEN) { 408 // if OPEN, emit connection complete command 409 hci_emit_connection_complete(conn); 410 } 411 // otherwise, just ignore 412 return 0; // don't sent packet to controller 413 414 } else{ 415 conn = create_connection_for_addr(addr); 416 if (conn){ 417 // create connection struct and register, state = SENT_CREATE_CONNECTION 418 conn->state = SENT_CREATE_CONNECTION; 419 } 420 } 421 } 422 423 // accept connection 424 425 // reject connection 426 427 // close_connection? 428 // set state = SENT_DISCONNECT 429 430 hci_stack.num_cmd_packets--; 431 return hci_stack.hci_transport->send_cmd_packet(packet, size); 432 } 433 434 /** 435 * pre: numcmds >= 0 - it's allowed to send a command to the controller 436 */ 437 int hci_send_cmd(hci_cmd_t *cmd, ...){ 438 va_list argptr; 439 va_start(argptr, cmd); 440 uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 441 uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 442 va_end(argptr); 443 return hci_send_cmd_packet(hci_cmd_buffer, size); 444 } 445 446 // Create various non-HCI events. 447 // TODO: generalize, use table similar to hci_create_command 448 449 void hci_emit_state(){ 450 uint8_t len = 3; 451 uint8_t event[len]; 452 event[0] = BTSTACK_EVENT_STATE; 453 event[1] = len - 3; 454 event[2] = hci_stack.state; 455 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 456 hci_stack.event_packet_handler(event, len); 457 } 458 459 void hci_emit_connection_complete(hci_connection_t *conn){ 460 uint8_t len = 13; 461 uint8_t event[len]; 462 event[0] = HCI_EVENT_CONNECTION_COMPLETE; 463 event[1] = len - 3; 464 event[2] = 0; // status = OK 465 bt_store_16(event, 3, conn->con_handle); 466 bt_flip_addr(&event[5], conn->address); 467 event[11] = 1; // ACL connection 468 event[12] = 0; // encryption disabled 469 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 470 hci_stack.event_packet_handler(event, len); 471 } 472 473 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 474 uint8_t len = 4; 475 uint8_t event[len]; 476 event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 477 event[1] = len - 2; 478 bt_store_16(event, 2, conn->con_handle); 479 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 480 hci_stack.event_packet_handler(event, len); 481 } 482 483 void hci_emit_nr_connections_changed(){ 484 uint8_t len = 3; 485 uint8_t event[len]; 486 event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 487 event[1] = len - 2; 488 event[2] = nr_hci_connections(); 489 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 490 hci_stack.event_packet_handler(event, len); 491 } 492 493 void hci_emit_hci_open_failed(){ 494 uint8_t len = 2; 495 uint8_t event[len]; 496 event[0] = BTSTACK_EVENT_POWERON_FAILED; 497 event[1] = len - 2; 498 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 499 hci_stack.event_packet_handler(event, len); 500 } 501 502 503 void hci_emit_btstack_version() { 504 uint8_t len = 6; 505 uint8_t event[len]; 506 event[0] = BTSTACK_EVENT_VERSION; 507 event[1] = len - 2; 508 event[len++] = BTSTACK_MAJOR; 509 event[len++] = BTSTACK_MINOR; 510 bt_store_16(event, len, BTSTACK_REVISION); 511 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 512 hci_stack.event_packet_handler(event, len); 513 } 514 515 void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 516 uint8_t len = 3; 517 uint8_t event[len]; 518 event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 519 event[1] = len - 3; 520 event[2] = enabled; 521 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 522 hci_stack.event_packet_handler(event, len); 523 } 524