1 /* 2 * Copyright (C) 2009-2012 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 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at [email protected] 34 * 35 */ 36 37 /* 38 * hci.c 39 * 40 * Created by Matthias Ringwald on 4/29/09. 41 * 42 */ 43 44 #include "config.h" 45 46 #include "hci.h" 47 48 #include <stdarg.h> 49 #include <string.h> 50 #include <stdio.h> 51 52 #ifndef EMBEDDED 53 #include <unistd.h> // gethostbyname 54 #include <btstack/version.h> 55 #endif 56 57 #include "btstack_memory.h" 58 #include "debug.h" 59 #include "hci_dump.h" 60 61 #include <btstack/hci_cmds.h> 62 63 #define HCI_CONNECTION_TIMEOUT_MS 10000 64 65 #ifdef USE_BLUETOOL 66 #include "bt_control_iphone.h" 67 #endif 68 69 static void hci_update_scan_enable(void); 70 71 // the STACK is here 72 static hci_stack_t hci_stack; 73 74 /** 75 * get connection for a given handle 76 * 77 * @return connection OR NULL, if not found 78 */ 79 hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 80 linked_item_t *it; 81 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 82 if ( ((hci_connection_t *) it)->con_handle == con_handle){ 83 return (hci_connection_t *) it; 84 } 85 } 86 return NULL; 87 } 88 89 static void hci_connection_timeout_handler(timer_source_t *timer){ 90 hci_connection_t * connection = (hci_connection_t *) linked_item_get_user(&timer->item); 91 #ifdef HAVE_TIME 92 struct timeval tv; 93 gettimeofday(&tv, NULL); 94 if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 95 // connections might be timed out 96 hci_emit_l2cap_check_timeout(connection); 97 } 98 #endif 99 #ifdef HAVE_TICK 100 if (embedded_get_ticks() > connection->timestamp + embedded_ticks_for_ms(HCI_CONNECTION_TIMEOUT_MS)){ 101 // connections might be timed out 102 hci_emit_l2cap_check_timeout(connection); 103 } 104 #endif 105 run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 106 run_loop_add_timer(timer); 107 } 108 109 static void hci_connection_timestamp(hci_connection_t *connection){ 110 #ifdef HAVE_TIME 111 gettimeofday(&connection->timestamp, NULL); 112 #endif 113 #ifdef HAVE_TICK 114 connection->timestamp = embedded_get_ticks(); 115 #endif 116 } 117 118 /** 119 * create connection for given address 120 * 121 * @return connection OR NULL, if no memory left 122 */ 123 static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 124 hci_connection_t * conn = (hci_connection_t *) btstack_memory_hci_connection_get(); 125 if (!conn) return NULL; 126 BD_ADDR_COPY(conn->address, addr); 127 conn->con_handle = 0xffff; 128 conn->authentication_flags = AUTH_FLAGS_NONE; 129 linked_item_set_user(&conn->timeout.item, conn); 130 conn->timeout.process = hci_connection_timeout_handler; 131 hci_connection_timestamp(conn); 132 conn->acl_recombination_length = 0; 133 conn->acl_recombination_pos = 0; 134 conn->num_acl_packets_sent = 0; 135 linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 136 return conn; 137 } 138 139 /** 140 * get connection for given address 141 * 142 * @return connection OR NULL, if not found 143 */ 144 static hci_connection_t * connection_for_address(bd_addr_t address){ 145 linked_item_t *it; 146 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 147 if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 148 return (hci_connection_t *) it; 149 } 150 } 151 return NULL; 152 } 153 154 inline static void connectionSetAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 155 conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags | flags); 156 } 157 158 inline static void connectionClearAuthenticationFlags(hci_connection_t * conn, hci_authentication_flags_t flags){ 159 conn->authentication_flags = (hci_authentication_flags_t)(conn->authentication_flags & ~flags); 160 } 161 162 163 /** 164 * add authentication flags and reset timer 165 */ 166 static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 167 bd_addr_t addr; 168 bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 169 hci_connection_t * conn = connection_for_address(addr); 170 if (conn) { 171 connectionSetAuthenticationFlags(conn, flags); 172 hci_connection_timestamp(conn); 173 } 174 } 175 176 int hci_authentication_active_for_handle(hci_con_handle_t handle){ 177 hci_connection_t * conn = connection_for_handle(handle); 178 if (!conn) return 0; 179 if (!conn->authentication_flags) return 0; 180 if (conn->authentication_flags & SENT_LINK_KEY_REPLY) return 0; 181 if (conn->authentication_flags & RECV_LINK_KEY_NOTIFICATION) return 0; 182 return 1; 183 } 184 185 void hci_drop_link_key_for_bd_addr(bd_addr_t *addr){ 186 if (hci_stack.remote_device_db) { 187 hci_stack.remote_device_db->delete_link_key(addr); 188 } 189 } 190 191 192 /** 193 * count connections 194 */ 195 static int nr_hci_connections(void){ 196 int count = 0; 197 linked_item_t *it; 198 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 199 return count; 200 } 201 202 /** 203 * Dummy handler called by HCI 204 */ 205 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 206 } 207 208 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 209 hci_connection_t * connection = connection_for_handle(handle); 210 if (!connection) { 211 log_error("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 212 return 0; 213 } 214 return connection->num_acl_packets_sent; 215 } 216 217 uint8_t hci_number_free_acl_slots(){ 218 uint8_t free_slots = hci_stack.total_num_acl_packets; 219 linked_item_t *it; 220 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 221 hci_connection_t * connection = (hci_connection_t *) it; 222 if (free_slots < connection->num_acl_packets_sent) { 223 log_error("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 224 return 0; 225 } 226 free_slots -= connection->num_acl_packets_sent; 227 } 228 return free_slots; 229 } 230 231 int hci_can_send_packet_now(uint8_t packet_type){ 232 233 // check for async hci transport implementations 234 if (hci_stack.hci_transport->can_send_packet_now){ 235 if (!hci_stack.hci_transport->can_send_packet_now(packet_type)){ 236 return 0; 237 } 238 } 239 240 // check regular Bluetooth flow control 241 switch (packet_type) { 242 case HCI_ACL_DATA_PACKET: 243 return hci_number_free_acl_slots(); 244 case HCI_COMMAND_DATA_PACKET: 245 return hci_stack.num_cmd_packets; 246 default: 247 return 0; 248 } 249 } 250 251 int hci_send_acl_packet(uint8_t *packet, int size){ 252 253 // check for free places on BT module 254 if (!hci_number_free_acl_slots()) return BTSTACK_ACL_BUFFERS_FULL; 255 256 hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 257 hci_connection_t *connection = connection_for_handle( con_handle); 258 if (!connection) return 0; 259 hci_connection_timestamp(connection); 260 261 // count packet 262 connection->num_acl_packets_sent++; 263 // log_info("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 264 265 // send packet 266 int err = hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 267 268 return err; 269 } 270 271 static void acl_handler(uint8_t *packet, int size){ 272 273 // get info 274 hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 275 hci_connection_t *conn = connection_for_handle(con_handle); 276 uint8_t acl_flags = READ_ACL_FLAGS(packet); 277 uint16_t acl_length = READ_ACL_LENGTH(packet); 278 279 // ignore non-registered handle 280 if (!conn){ 281 log_error( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 282 return; 283 } 284 285 // update idle timestamp 286 hci_connection_timestamp(conn); 287 288 // handle different packet types 289 switch (acl_flags & 0x03) { 290 291 case 0x01: // continuation fragment 292 293 // sanity check 294 if (conn->acl_recombination_pos == 0) { 295 log_error( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 296 return; 297 } 298 299 // append fragment payload (header already stored) 300 memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 301 conn->acl_recombination_pos += acl_length; 302 303 // log_error( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", acl_length, 304 // conn->acl_recombination_pos, conn->acl_recombination_length); 305 306 // forward complete L2CAP packet if complete. 307 if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 308 309 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 310 // reset recombination buffer 311 conn->acl_recombination_length = 0; 312 conn->acl_recombination_pos = 0; 313 } 314 break; 315 316 case 0x02: { // first fragment 317 318 // sanity check 319 if (conn->acl_recombination_pos) { 320 log_error( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 321 return; 322 } 323 324 // peek into L2CAP packet! 325 uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 326 327 // log_error( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 328 329 // compare fragment size to L2CAP packet size 330 if (acl_length >= l2cap_length + 4){ 331 332 // forward fragment as L2CAP packet 333 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 334 335 } else { 336 // store first fragment and tweak acl length for complete package 337 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 338 conn->acl_recombination_pos = acl_length + 4; 339 conn->acl_recombination_length = l2cap_length; 340 bt_store_16(conn->acl_recombination_buffer, 2, l2cap_length +4); 341 } 342 break; 343 344 } 345 default: 346 log_error( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 347 return; 348 } 349 350 // execute main loop 351 hci_run(); 352 } 353 354 static void hci_shutdown_connection(hci_connection_t *conn){ 355 log_info("Connection closed: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 356 357 // cancel all l2cap connections 358 hci_emit_disconnection_complete(conn->con_handle, 0x16); // terminated by local host 359 360 run_loop_remove_timer(&conn->timeout); 361 362 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 363 btstack_memory_hci_connection_free( conn ); 364 365 // now it's gone 366 hci_emit_nr_connections_changed(); 367 } 368 369 static const uint16_t packet_type_sizes[] = { 370 0, HCI_ACL_2DH1_SIZE, HCI_ACL_3DH1_SIZE, HCI_ACL_DM1_SIZE, 371 HCI_ACL_DH1_SIZE, 0, 0, 0, 372 HCI_ACL_2DH3_SIZE, HCI_ACL_3DH3_SIZE, HCI_ACL_DM3_SIZE, HCI_ACL_DH3_SIZE, 373 HCI_ACL_2DH5_SIZE, HCI_ACL_3DH5_SIZE, HCI_ACL_DM5_SIZE, HCI_ACL_DH5_SIZE 374 }; 375 376 static uint16_t hci_acl_packet_types_for_buffer_size(uint16_t buffer_size){ 377 uint16_t packet_types = 0; 378 int i; 379 for (i=0;i<16;i++){ 380 if (packet_type_sizes[i] == 0) continue; 381 if (packet_type_sizes[i] <= buffer_size){ 382 packet_types |= 1 << i; 383 } 384 } 385 // flip bits for "may not be used" 386 packet_types ^= 0x3306; 387 return packet_types; 388 } 389 390 uint16_t hci_usable_acl_packet_types(void){ 391 return hci_stack.packet_types; 392 } 393 394 uint8_t* hci_get_outgoing_acl_packet_buffer(void){ 395 // hci packet buffer is >= acl data packet length 396 return hci_stack.hci_packet_buffer; 397 } 398 399 uint16_t hci_max_acl_data_packet_length(){ 400 return hci_stack.acl_data_packet_length; 401 } 402 403 // avoid huge local variables 404 #ifndef EMBEDDED 405 static device_name_t device_name; 406 #endif 407 static void event_handler(uint8_t *packet, int size){ 408 bd_addr_t addr; 409 uint8_t link_type; 410 hci_con_handle_t handle; 411 hci_connection_t * conn; 412 int i; 413 414 // printf("HCI:EVENT:%02x\n", packet[0]); 415 416 switch (packet[0]) { 417 418 case HCI_EVENT_COMMAND_COMPLETE: 419 // get num cmd packets 420 // log_info("HCI_EVENT_COMMAND_COMPLETE cmds old %u - new %u\n", hci_stack.num_cmd_packets, packet[2]); 421 hci_stack.num_cmd_packets = packet[2]; 422 423 if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 424 // from offset 5 425 // status 426 // "The HC_ACL_Data_Packet_Length return parameter will be used to determine the size of the L2CAP segments contained in ACL Data Packets" 427 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 428 // ignore: SCO data packet len (8) 429 hci_stack.total_num_acl_packets = packet[9]; 430 // ignore: total num SCO packets 431 if (hci_stack.state == HCI_STATE_INITIALIZING){ 432 // determine usable ACL payload size 433 if (HCI_ACL_PAYLOAD_SIZE < hci_stack.acl_data_packet_length){ 434 hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 435 } 436 // determine usable ACL packet types 437 hci_stack.packet_types = hci_acl_packet_types_for_buffer_size(hci_stack.acl_data_packet_length); 438 439 log_error("hci_read_buffer_size: used size %u, count %u, packet types %04x\n", 440 hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets, hci_stack.packet_types); 441 } 442 } 443 // Dump local address 444 if (COMMAND_COMPLETE_EVENT(packet, hci_read_bd_addr)) { 445 bd_addr_t addr; 446 bt_flip_addr(addr, &packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1]); 447 log_info("Local Address, Status: 0x%02x: Addr: %s\n", 448 packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(addr)); 449 } 450 if (COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 451 hci_emit_discoverable_enabled(hci_stack.discoverable); 452 } 453 break; 454 455 case HCI_EVENT_COMMAND_STATUS: 456 // get num cmd packets 457 // log_info("HCI_EVENT_COMMAND_STATUS cmds - old %u - new %u\n", hci_stack.num_cmd_packets, packet[3]); 458 hci_stack.num_cmd_packets = packet[3]; 459 break; 460 461 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 462 for (i=0; i<packet[2];i++){ 463 handle = READ_BT_16(packet, 3 + 2*i); 464 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 465 conn = connection_for_handle(handle); 466 if (!conn){ 467 log_error("hci_number_completed_packet lists unused con handle %u\n", handle); 468 continue; 469 } 470 conn->num_acl_packets_sent -= num_packets; 471 // log_info("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 472 } 473 break; 474 475 case HCI_EVENT_CONNECTION_REQUEST: 476 bt_flip_addr(addr, &packet[2]); 477 // TODO: eval COD 8-10 478 link_type = packet[11]; 479 log_info("Connection_incoming: %s, type %u\n", bd_addr_to_str(addr), link_type); 480 if (link_type == 1) { // ACL 481 conn = connection_for_address(addr); 482 if (!conn) { 483 conn = create_connection_for_addr(addr); 484 } 485 if (!conn) { 486 // CONNECTION REJECTED DUE TO LIMITED RESOURCES (0X0D) 487 hci_stack.decline_reason = 0x0d; 488 BD_ADDR_COPY(hci_stack.decline_addr, addr); 489 break; 490 } 491 conn->state = RECEIVED_CONNECTION_REQUEST; 492 hci_run(); 493 } else { 494 // SYNCHRONOUS CONNECTION LIMIT TO A DEVICE EXCEEDED (0X0A) 495 hci_stack.decline_reason = 0x0a; 496 BD_ADDR_COPY(hci_stack.decline_addr, addr); 497 } 498 break; 499 500 case HCI_EVENT_CONNECTION_COMPLETE: 501 // Connection management 502 bt_flip_addr(addr, &packet[5]); 503 log_info("Connection_complete (status=%u) %s\n", packet[2], bd_addr_to_str(addr)); 504 conn = connection_for_address(addr); 505 if (conn) { 506 if (!packet[2]){ 507 conn->state = OPEN; 508 conn->con_handle = READ_BT_16(packet, 3); 509 510 // restart timer 511 run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 512 run_loop_add_timer(&conn->timeout); 513 514 log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 515 516 hci_emit_nr_connections_changed(); 517 } else { 518 // connection failed, remove entry 519 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 520 btstack_memory_hci_connection_free( conn ); 521 522 // if authentication error, also delete link key 523 if (packet[2] == 0x05) { 524 hci_drop_link_key_for_bd_addr(&addr); 525 } 526 } 527 } 528 break; 529 530 case HCI_EVENT_LINK_KEY_REQUEST: 531 log_info("HCI_EVENT_LINK_KEY_REQUEST\n"); 532 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 533 if (!hci_stack.remote_device_db) break; 534 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], HANDLE_LINK_KEY_REQUEST); 535 hci_run(); 536 // request handled by hci_run() as HANDLE_LINK_KEY_REQUEST gets set 537 return; 538 539 case HCI_EVENT_LINK_KEY_NOTIFICATION: 540 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 541 if (!hci_stack.remote_device_db) break; 542 bt_flip_addr(addr, &packet[2]); 543 hci_stack.remote_device_db->put_link_key(&addr, (link_key_t *) &packet[8]); 544 // still forward event to allow dismiss of pairing dialog 545 break; 546 547 case HCI_EVENT_PIN_CODE_REQUEST: 548 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 549 // PIN CODE REQUEST means the link key request didn't succee -> delete stored link key 550 if (!hci_stack.remote_device_db) break; 551 bt_flip_addr(addr, &packet[2]); 552 hci_stack.remote_device_db->delete_link_key(&addr); 553 break; 554 555 #ifndef EMBEDDED 556 case HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE: 557 if (!hci_stack.remote_device_db) break; 558 if (packet[2]) break; // status not ok 559 bt_flip_addr(addr, &packet[3]); 560 // fix for invalid remote names - terminate on 0xff 561 for (i=0; i<248;i++){ 562 if (packet[9+i] == 0xff){ 563 packet[9+i] = 0; 564 break; 565 } 566 } 567 memset(&device_name, 0, sizeof(device_name_t)); 568 strncpy((char*) device_name, (char*) &packet[9], 248); 569 hci_stack.remote_device_db->put_name(&addr, &device_name); 570 break; 571 572 case HCI_EVENT_INQUIRY_RESULT: 573 case HCI_EVENT_INQUIRY_RESULT_WITH_RSSI: 574 if (!hci_stack.remote_device_db) break; 575 // first send inq result packet 576 hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 577 // then send cached remote names 578 for (i=0; i<packet[2];i++){ 579 bt_flip_addr(addr, &packet[3+i*6]); 580 if (hci_stack.remote_device_db->get_name(&addr, &device_name)){ 581 hci_emit_remote_name_cached(&addr, &device_name); 582 } 583 } 584 return; 585 #endif 586 587 case HCI_EVENT_DISCONNECTION_COMPLETE: 588 if (!packet[2]){ 589 handle = READ_BT_16(packet, 3); 590 hci_connection_t * conn = connection_for_handle(handle); 591 if (conn) { 592 hci_shutdown_connection(conn); 593 } 594 } 595 break; 596 597 case HCI_EVENT_HARDWARE_ERROR: 598 if(hci_stack.control->hw_error){ 599 (*hci_stack.control->hw_error)(); 600 } 601 break; 602 603 #ifdef HAVE_BLE 604 case HCI_EVENT_LE_META: 605 switch (packet[2]) { 606 case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 607 // Connection management 608 bt_flip_addr(addr, &packet[8]); 609 log_info("LE Connection_complete (status=%u) %s\n", packet[3], bd_addr_to_str(addr)); 610 // LE connections are auto-accepted, so just create a connection if there isn't one already 611 conn = connection_for_address(addr); 612 if (packet[3]){ 613 if (conn){ 614 // outgoing connection failed, remove entry 615 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 616 btstack_memory_hci_connection_free( conn ); 617 618 } 619 // if authentication error, also delete link key 620 if (packet[3] == 0x05) { 621 hci_drop_link_key_for_bd_addr(&addr); 622 } 623 break; 624 } 625 if (!conn){ 626 conn = create_connection_for_addr(addr); 627 } 628 if (!conn){ 629 // no memory 630 break; 631 } 632 633 conn->state = OPEN; 634 conn->con_handle = READ_BT_16(packet, 4); 635 636 // TODO: store - role, peer address type, conn_interval, conn_latency, supervision timeout, master clock 637 638 // restart timer 639 // run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 640 // run_loop_add_timer(&conn->timeout); 641 642 log_info("New connection: handle %u, %s\n", conn->con_handle, bd_addr_to_str(conn->address)); 643 644 hci_emit_nr_connections_changed(); 645 break; 646 647 default: 648 break; 649 } 650 break; 651 #endif 652 653 default: 654 break; 655 } 656 657 // handle BT initialization 658 if (hci_stack.state == HCI_STATE_INITIALIZING){ 659 // handle H4 synchronization loss on restart 660 // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 661 // hci_stack.substate = 0; 662 // } 663 // handle normal init sequence 664 if (hci_stack.substate % 2){ 665 // odd: waiting for event 666 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 667 hci_stack.substate++; 668 } 669 } 670 } 671 672 // help with BT sleep 673 if (hci_stack.state == HCI_STATE_FALLING_ASLEEP 674 && hci_stack.substate == 1 675 && COMMAND_COMPLETE_EVENT(packet, hci_write_scan_enable)){ 676 hci_stack.substate++; 677 } 678 679 hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 680 681 // execute main loop 682 hci_run(); 683 } 684 685 void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 686 switch (packet_type) { 687 case HCI_EVENT_PACKET: 688 event_handler(packet, size); 689 break; 690 case HCI_ACL_DATA_PACKET: 691 acl_handler(packet, size); 692 break; 693 default: 694 break; 695 } 696 } 697 698 /** Register HCI packet handlers */ 699 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 700 hci_stack.packet_handler = handler; 701 } 702 703 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control, remote_device_db_t const* remote_device_db){ 704 705 // reference to use transport layer implementation 706 hci_stack.hci_transport = transport; 707 708 // references to used control implementation 709 hci_stack.control = control; 710 711 // reference to used config 712 hci_stack.config = config; 713 714 // no connections yet 715 hci_stack.connections = NULL; 716 hci_stack.discoverable = 0; 717 hci_stack.connectable = 0; 718 719 // no pending cmds 720 hci_stack.decline_reason = 0; 721 hci_stack.new_scan_enable_value = 0xff; 722 723 // higher level handler 724 hci_stack.packet_handler = dummy_handler; 725 726 // store and open remote device db 727 hci_stack.remote_device_db = remote_device_db; 728 if (hci_stack.remote_device_db) { 729 hci_stack.remote_device_db->open(); 730 } 731 732 // max acl payload size defined in config.h 733 hci_stack.acl_data_packet_length = HCI_ACL_PAYLOAD_SIZE; 734 735 // register packet handlers with transport 736 transport->register_packet_handler(&packet_handler); 737 738 hci_stack.state = HCI_STATE_OFF; 739 } 740 741 void hci_close(){ 742 // close remote device db 743 if (hci_stack.remote_device_db) { 744 hci_stack.remote_device_db->close(); 745 } 746 while (hci_stack.connections) { 747 hci_shutdown_connection((hci_connection_t *) hci_stack.connections); 748 } 749 hci_power_control(HCI_POWER_OFF); 750 } 751 752 // State-Module-Driver overview 753 // state module low-level 754 // HCI_STATE_OFF off close 755 // HCI_STATE_INITIALIZING, on open 756 // HCI_STATE_WORKING, on open 757 // HCI_STATE_HALTING, on open 758 // HCI_STATE_SLEEPING, off/sleep close 759 // HCI_STATE_FALLING_ASLEEP on open 760 761 static int hci_power_control_on(void){ 762 763 // power on 764 int err = 0; 765 if (hci_stack.control && hci_stack.control->on){ 766 err = (*hci_stack.control->on)(hci_stack.config); 767 } 768 if (err){ 769 log_error( "POWER_ON failed\n"); 770 hci_emit_hci_open_failed(); 771 return err; 772 } 773 774 // open low-level device 775 err = hci_stack.hci_transport->open(hci_stack.config); 776 if (err){ 777 log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 778 if (hci_stack.control && hci_stack.control->off){ 779 (*hci_stack.control->off)(hci_stack.config); 780 } 781 hci_emit_hci_open_failed(); 782 return err; 783 } 784 return 0; 785 } 786 787 static void hci_power_control_off(void){ 788 789 log_info("hci_power_control_off\n"); 790 791 // close low-level device 792 hci_stack.hci_transport->close(hci_stack.config); 793 794 log_info("hci_power_control_off - hci_transport closed\n"); 795 796 // power off 797 if (hci_stack.control && hci_stack.control->off){ 798 (*hci_stack.control->off)(hci_stack.config); 799 } 800 801 log_info("hci_power_control_off - control closed\n"); 802 803 hci_stack.state = HCI_STATE_OFF; 804 } 805 806 static void hci_power_control_sleep(void){ 807 808 log_info("hci_power_control_sleep\n"); 809 810 #if 0 811 // don't close serial port during sleep 812 813 // close low-level device 814 hci_stack.hci_transport->close(hci_stack.config); 815 #endif 816 817 // sleep mode 818 if (hci_stack.control && hci_stack.control->sleep){ 819 (*hci_stack.control->sleep)(hci_stack.config); 820 } 821 822 hci_stack.state = HCI_STATE_SLEEPING; 823 } 824 825 static int hci_power_control_wake(void){ 826 827 log_info("hci_power_control_wake\n"); 828 829 // wake on 830 if (hci_stack.control && hci_stack.control->wake){ 831 (*hci_stack.control->wake)(hci_stack.config); 832 } 833 834 #if 0 835 // open low-level device 836 int err = hci_stack.hci_transport->open(hci_stack.config); 837 if (err){ 838 log_error( "HCI_INIT failed, turning Bluetooth off again\n"); 839 if (hci_stack.control && hci_stack.control->off){ 840 (*hci_stack.control->off)(hci_stack.config); 841 } 842 hci_emit_hci_open_failed(); 843 return err; 844 } 845 #endif 846 847 return 0; 848 } 849 850 851 int hci_power_control(HCI_POWER_MODE power_mode){ 852 853 log_info("hci_power_control: %u, current mode %u\n", power_mode, hci_stack.state); 854 855 int err = 0; 856 switch (hci_stack.state){ 857 858 case HCI_STATE_OFF: 859 switch (power_mode){ 860 case HCI_POWER_ON: 861 err = hci_power_control_on(); 862 if (err) return err; 863 // set up state machine 864 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 865 hci_stack.state = HCI_STATE_INITIALIZING; 866 hci_stack.substate = 0; 867 break; 868 case HCI_POWER_OFF: 869 // do nothing 870 break; 871 case HCI_POWER_SLEEP: 872 // do nothing (with SLEEP == OFF) 873 break; 874 } 875 break; 876 877 case HCI_STATE_INITIALIZING: 878 switch (power_mode){ 879 case HCI_POWER_ON: 880 // do nothing 881 break; 882 case HCI_POWER_OFF: 883 // no connections yet, just turn it off 884 hci_power_control_off(); 885 break; 886 case HCI_POWER_SLEEP: 887 // no connections yet, just turn it off 888 hci_power_control_sleep(); 889 break; 890 } 891 break; 892 893 case HCI_STATE_WORKING: 894 switch (power_mode){ 895 case HCI_POWER_ON: 896 // do nothing 897 break; 898 case HCI_POWER_OFF: 899 // see hci_run 900 hci_stack.state = HCI_STATE_HALTING; 901 break; 902 case HCI_POWER_SLEEP: 903 // see hci_run 904 hci_stack.state = HCI_STATE_FALLING_ASLEEP; 905 hci_stack.substate = 0; 906 break; 907 } 908 break; 909 910 case HCI_STATE_HALTING: 911 switch (power_mode){ 912 case HCI_POWER_ON: 913 // set up state machine 914 hci_stack.state = HCI_STATE_INITIALIZING; 915 hci_stack.substate = 0; 916 break; 917 case HCI_POWER_OFF: 918 // do nothing 919 break; 920 case HCI_POWER_SLEEP: 921 // see hci_run 922 hci_stack.state = HCI_STATE_FALLING_ASLEEP; 923 hci_stack.substate = 0; 924 break; 925 } 926 break; 927 928 case HCI_STATE_FALLING_ASLEEP: 929 switch (power_mode){ 930 case HCI_POWER_ON: 931 932 #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 933 // nothing to do, if H4 supports power management 934 if (bt_control_iphone_power_management_enabled()){ 935 hci_stack.state = HCI_STATE_INITIALIZING; 936 hci_stack.substate = 6; 937 break; 938 } 939 #endif 940 // set up state machine 941 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 942 hci_stack.state = HCI_STATE_INITIALIZING; 943 hci_stack.substate = 0; 944 break; 945 case HCI_POWER_OFF: 946 // see hci_run 947 hci_stack.state = HCI_STATE_HALTING; 948 break; 949 case HCI_POWER_SLEEP: 950 // do nothing 951 break; 952 } 953 break; 954 955 case HCI_STATE_SLEEPING: 956 switch (power_mode){ 957 case HCI_POWER_ON: 958 959 #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 960 // nothing to do, if H4 supports power management 961 if (bt_control_iphone_power_management_enabled()){ 962 hci_stack.state = HCI_STATE_INITIALIZING; 963 hci_stack.substate = 6; 964 hci_update_scan_enable(); 965 break; 966 } 967 #endif 968 err = hci_power_control_wake(); 969 if (err) return err; 970 // set up state machine 971 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 972 hci_stack.state = HCI_STATE_INITIALIZING; 973 hci_stack.substate = 0; 974 break; 975 case HCI_POWER_OFF: 976 hci_stack.state = HCI_STATE_HALTING; 977 break; 978 case HCI_POWER_SLEEP: 979 // do nothing 980 break; 981 } 982 break; 983 } 984 985 // create internal event 986 hci_emit_state(); 987 988 // trigger next/first action 989 hci_run(); 990 991 return 0; 992 } 993 994 static void hci_update_scan_enable(void){ 995 // 2 = page scan, 1 = inq scan 996 hci_stack.new_scan_enable_value = hci_stack.connectable << 1 | hci_stack.discoverable; 997 hci_run(); 998 } 999 1000 void hci_discoverable_control(uint8_t enable){ 1001 if (enable) enable = 1; // normalize argument 1002 1003 if (hci_stack.discoverable == enable){ 1004 hci_emit_discoverable_enabled(hci_stack.discoverable); 1005 return; 1006 } 1007 1008 hci_stack.discoverable = enable; 1009 hci_update_scan_enable(); 1010 } 1011 1012 void hci_connectable_control(uint8_t enable){ 1013 if (enable) enable = 1; // normalize argument 1014 1015 // don't emit event 1016 if (hci_stack.connectable == enable) return; 1017 1018 hci_stack.connectable = enable; 1019 hci_update_scan_enable(); 1020 } 1021 1022 void hci_run(){ 1023 1024 hci_connection_t * connection; 1025 linked_item_t * it; 1026 1027 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1028 1029 // global/non-connection oriented commands 1030 1031 // decline incoming connections 1032 if (hci_stack.decline_reason){ 1033 uint8_t reason = hci_stack.decline_reason; 1034 hci_stack.decline_reason = 0; 1035 hci_send_cmd(&hci_reject_connection_request, hci_stack.decline_addr, reason); 1036 } 1037 1038 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1039 1040 // send scan enable 1041 if (hci_stack.new_scan_enable_value != 0xff){ 1042 hci_send_cmd(&hci_write_scan_enable, hci_stack.new_scan_enable_value); 1043 hci_stack.new_scan_enable_value = 0xff; 1044 } 1045 1046 // send pending HCI commands 1047 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 1048 1049 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1050 1051 connection = (hci_connection_t *) it; 1052 1053 if (connection->state == RECEIVED_CONNECTION_REQUEST){ 1054 log_info("sending hci_accept_connection_request\n"); 1055 hci_send_cmd(&hci_accept_connection_request, connection->address, 1); 1056 connection->state = ACCEPTED_CONNECTION_REQUEST; 1057 } 1058 1059 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1060 1061 if (connection->authentication_flags & HANDLE_LINK_KEY_REQUEST){ 1062 link_key_t link_key; 1063 log_info("responding to link key request\n"); 1064 if ( hci_stack.remote_device_db->get_link_key( &connection->address, &link_key)){ 1065 hci_send_cmd(&hci_link_key_request_reply, connection->address, &link_key); 1066 } else { 1067 hci_send_cmd(&hci_link_key_request_negative_reply, connection->address); 1068 } 1069 connectionClearAuthenticationFlags(connection, HANDLE_LINK_KEY_REQUEST); 1070 } 1071 } 1072 1073 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1074 1075 switch (hci_stack.state){ 1076 case HCI_STATE_INITIALIZING: 1077 // log_info("hci_init: substate %u\n", hci_stack.substate); 1078 if (hci_stack.substate % 2) { 1079 // odd: waiting for command completion 1080 return; 1081 } 1082 switch (hci_stack.substate >> 1){ 1083 case 0: // RESET 1084 hci_send_cmd(&hci_reset); 1085 if (hci_stack.config == 0 || ((hci_uart_config_t *)hci_stack.config)->baudrate_main == 0){ 1086 // skip baud change 1087 hci_stack.substate = 4; // >> 1 = 2 1088 } 1089 break; 1090 case 1: // SEND BAUD CHANGE 1091 hci_stack.control->baudrate_cmd(hci_stack.config, ((hci_uart_config_t *)hci_stack.config)->baudrate_main, hci_stack.hci_packet_buffer); 1092 hci_send_cmd_packet(hci_stack.hci_packet_buffer, 3 + hci_stack.hci_packet_buffer[2]); 1093 break; 1094 case 2: // LOCAL BAUD CHANGE 1095 hci_stack.hci_transport->set_baudrate(((hci_uart_config_t *)hci_stack.config)->baudrate_main); 1096 hci_stack.substate += 2; 1097 // break missing here for fall through 1098 1099 case 3: 1100 // custom initialization 1101 if (hci_stack.control && hci_stack.control->next_cmd){ 1102 int valid_cmd = (*hci_stack.control->next_cmd)(hci_stack.config, hci_stack.hci_packet_buffer); 1103 if (valid_cmd){ 1104 int size = 3 + hci_stack.hci_packet_buffer[2]; 1105 hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, hci_stack.hci_packet_buffer, size); 1106 hci_stack.substate = 4; // more init commands 1107 break; 1108 } 1109 log_info("hci_run: init script done\n\r"); 1110 } 1111 // otherwise continue 1112 hci_send_cmd(&hci_read_bd_addr); 1113 break; 1114 case 4: 1115 hci_send_cmd(&hci_read_buffer_size); 1116 break; 1117 case 5: 1118 // ca. 15 sec 1119 hci_send_cmd(&hci_write_page_timeout, 0x6000); 1120 break; 1121 case 6: 1122 hci_send_cmd(&hci_write_scan_enable, (hci_stack.connectable << 1) | hci_stack.discoverable); // page scan 1123 break; 1124 case 7: 1125 #ifndef EMBEDDED 1126 { 1127 char hostname[30]; 1128 gethostname(hostname, 30); 1129 hostname[29] = '\0'; 1130 hci_send_cmd(&hci_write_local_name, hostname); 1131 break; 1132 } 1133 case 8: 1134 #ifdef USE_BLUETOOL 1135 hci_send_cmd(&hci_write_class_of_device, 0x007a020c); // Smartphone 1136 break; 1137 1138 case 9: 1139 #endif 1140 #endif 1141 // done. 1142 hci_stack.state = HCI_STATE_WORKING; 1143 hci_emit_state(); 1144 break; 1145 default: 1146 break; 1147 } 1148 hci_stack.substate++; 1149 break; 1150 1151 case HCI_STATE_HALTING: 1152 1153 log_info("HCI_STATE_HALTING\n"); 1154 // close all open connections 1155 connection = (hci_connection_t *) hci_stack.connections; 1156 if (connection){ 1157 1158 // send disconnect 1159 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1160 1161 log_info("HCI_STATE_HALTING, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 1162 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1163 1164 // send disconnected event right away - causes higher layer connections to get closed, too. 1165 hci_shutdown_connection(connection); 1166 return; 1167 } 1168 log_info("HCI_STATE_HALTING, calling off\n"); 1169 1170 // switch mode 1171 hci_power_control_off(); 1172 1173 log_info("HCI_STATE_HALTING, emitting state\n"); 1174 hci_emit_state(); 1175 log_info("HCI_STATE_HALTING, done\n"); 1176 break; 1177 1178 case HCI_STATE_FALLING_ASLEEP: 1179 switch(hci_stack.substate) { 1180 case 0: 1181 log_info("HCI_STATE_FALLING_ASLEEP\n"); 1182 // close all open connections 1183 connection = (hci_connection_t *) hci_stack.connections; 1184 1185 #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 1186 // don't close connections, if H4 supports power management 1187 if (bt_control_iphone_power_management_enabled()){ 1188 connection = NULL; 1189 } 1190 #endif 1191 if (connection){ 1192 1193 // send disconnect 1194 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1195 1196 log_info("HCI_STATE_FALLING_ASLEEP, connection %p, handle %u\n", connection, (uint16_t)connection->con_handle); 1197 hci_send_cmd(&hci_disconnect, connection->con_handle, 0x13); // remote closed connection 1198 1199 // send disconnected event right away - causes higher layer connections to get closed, too. 1200 hci_shutdown_connection(connection); 1201 return; 1202 } 1203 1204 // disable page and inquiry scan 1205 if (!hci_can_send_packet_now(HCI_COMMAND_DATA_PACKET)) return; 1206 1207 log_info("HCI_STATE_HALTING, disabling inq cans\n"); 1208 hci_send_cmd(&hci_write_scan_enable, hci_stack.connectable << 1); // drop inquiry scan but keep page scan 1209 1210 // continue in next sub state 1211 hci_stack.substate++; 1212 break; 1213 case 1: 1214 // wait for command complete "hci_write_scan_enable" in event_handler(); 1215 break; 1216 case 2: 1217 log_info("HCI_STATE_HALTING, calling sleep\n"); 1218 #if defined(USE_POWERMANAGEMENT) && defined(USE_BLUETOOL) 1219 // don't actually go to sleep, if H4 supports power management 1220 if (bt_control_iphone_power_management_enabled()){ 1221 // SLEEP MODE reached 1222 hci_stack.state = HCI_STATE_SLEEPING; 1223 hci_emit_state(); 1224 break; 1225 } 1226 #endif 1227 // switch mode 1228 hci_power_control_sleep(); // changes hci_stack.state to SLEEP 1229 hci_emit_state(); 1230 break; 1231 1232 default: 1233 break; 1234 } 1235 break; 1236 1237 default: 1238 break; 1239 } 1240 } 1241 1242 int hci_send_cmd_packet(uint8_t *packet, int size){ 1243 bd_addr_t addr; 1244 hci_connection_t * conn; 1245 // house-keeping 1246 1247 // create_connection? 1248 if (IS_COMMAND(packet, hci_create_connection)){ 1249 bt_flip_addr(addr, &packet[3]); 1250 log_info("Create_connection to %s\n", bd_addr_to_str(addr)); 1251 conn = connection_for_address(addr); 1252 if (conn) { 1253 // if connection exists 1254 if (conn->state == OPEN) { 1255 // and OPEN, emit connection complete command 1256 hci_emit_connection_complete(conn, 0); 1257 } 1258 // otherwise, just ignore as it is already in the open process 1259 return 0; // don't sent packet to controller 1260 1261 } 1262 // create connection struct and register, state = SENT_CREATE_CONNECTION 1263 conn = create_connection_for_addr(addr); 1264 if (!conn){ 1265 // notify client that alloc failed 1266 hci_emit_connection_complete(conn, BTSTACK_MEMORY_ALLOC_FAILED); 1267 return 0; // don't sent packet to controller 1268 } 1269 conn->state = SENT_CREATE_CONNECTION; 1270 } 1271 1272 if (IS_COMMAND(packet, hci_link_key_request_reply)){ 1273 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 1274 } 1275 if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 1276 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 1277 } 1278 if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 1279 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 1280 } 1281 if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 1282 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 1283 } 1284 1285 if (IS_COMMAND(packet, hci_delete_stored_link_key)){ 1286 if (hci_stack.remote_device_db){ 1287 bt_flip_addr(addr, &packet[3]); 1288 hci_stack.remote_device_db->delete_link_key(&addr); 1289 } 1290 } 1291 1292 hci_stack.num_cmd_packets--; 1293 return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 1294 } 1295 1296 /** 1297 * pre: numcmds >= 0 - it's allowed to send a command to the controller 1298 */ 1299 int hci_send_cmd(const hci_cmd_t *cmd, ...){ 1300 va_list argptr; 1301 va_start(argptr, cmd); 1302 uint16_t size = hci_create_cmd_internal(hci_stack.hci_packet_buffer, cmd, argptr); 1303 va_end(argptr); 1304 return hci_send_cmd_packet(hci_stack.hci_packet_buffer, size); 1305 } 1306 1307 // Create various non-HCI events. 1308 // TODO: generalize, use table similar to hci_create_command 1309 1310 void hci_emit_state(){ 1311 uint8_t event[3]; 1312 event[0] = BTSTACK_EVENT_STATE; 1313 event[1] = sizeof(event) - 2; 1314 event[2] = hci_stack.state; 1315 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1316 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1317 } 1318 1319 void hci_emit_connection_complete(hci_connection_t *conn, uint8_t status){ 1320 uint8_t event[13]; 1321 event[0] = HCI_EVENT_CONNECTION_COMPLETE; 1322 event[1] = sizeof(event) - 2; 1323 event[2] = status; 1324 bt_store_16(event, 3, conn->con_handle); 1325 bt_flip_addr(&event[5], conn->address); 1326 event[11] = 1; // ACL connection 1327 event[12] = 0; // encryption disabled 1328 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1329 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1330 } 1331 1332 void hci_emit_disconnection_complete(uint16_t handle, uint8_t reason){ 1333 uint8_t event[6]; 1334 event[0] = HCI_EVENT_DISCONNECTION_COMPLETE; 1335 event[1] = sizeof(event) - 2; 1336 event[2] = 0; // status = OK 1337 bt_store_16(event, 3, handle); 1338 event[5] = reason; 1339 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1340 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1341 } 1342 1343 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 1344 uint8_t event[4]; 1345 event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 1346 event[1] = sizeof(event) - 2; 1347 bt_store_16(event, 2, conn->con_handle); 1348 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1349 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1350 } 1351 1352 void hci_emit_nr_connections_changed(){ 1353 uint8_t event[3]; 1354 event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 1355 event[1] = sizeof(event) - 2; 1356 event[2] = nr_hci_connections(); 1357 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1358 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1359 } 1360 1361 void hci_emit_hci_open_failed(){ 1362 uint8_t event[2]; 1363 event[0] = BTSTACK_EVENT_POWERON_FAILED; 1364 event[1] = sizeof(event) - 2; 1365 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1366 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1367 } 1368 1369 #ifndef EMBEDDED 1370 void hci_emit_btstack_version() { 1371 uint8_t event[6]; 1372 event[0] = BTSTACK_EVENT_VERSION; 1373 event[1] = sizeof(event) - 2; 1374 event[2] = BTSTACK_MAJOR; 1375 event[3] = BTSTACK_MINOR; 1376 bt_store_16(event, 4, BTSTACK_REVISION); 1377 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1378 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1379 } 1380 #endif 1381 1382 void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 1383 uint8_t event[3]; 1384 event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 1385 event[1] = sizeof(event) - 2; 1386 event[2] = enabled; 1387 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1388 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1389 } 1390 1391 void hci_emit_remote_name_cached(bd_addr_t *addr, device_name_t *name){ 1392 uint8_t event[2+1+6+248]; 1393 event[0] = BTSTACK_EVENT_REMOTE_NAME_CACHED; 1394 event[1] = sizeof(event) - 2; 1395 event[2] = 0; // just to be compatible with HCI_EVENT_REMOTE_NAME_REQUEST_COMPLETE 1396 bt_flip_addr(&event[3], *addr); 1397 memcpy(&event[9], name, 248); 1398 hci_dump_packet(HCI_EVENT_PACKET, 0, event, sizeof(event)); 1399 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1400 } 1401 1402 void hci_emit_discoverable_enabled(uint8_t enabled){ 1403 uint8_t event[3]; 1404 event[0] = BTSTACK_EVENT_DISCOVERABLE_ENABLED; 1405 event[1] = sizeof(event) - 2; 1406 event[2] = enabled; 1407 hci_dump_packet( HCI_EVENT_PACKET, 0, event, sizeof(event)); 1408 hci_stack.packet_handler(HCI_EVENT_PACKET, event, sizeof(event)); 1409 } 1410