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 "hci.h" 40 41 #include <unistd.h> 42 #include <stdarg.h> 43 #include <string.h> 44 #include <stdio.h> 45 46 #include "debug.h" 47 #include "hci_dump.h" 48 49 #include "../include/btstack/hci_cmds.h" 50 #include "../include/btstack/version.h" 51 52 // temp 53 #include "l2cap.h" 54 55 #define HCI_CONNECTION_TIMEOUT_MS 10000 56 57 // the STACK is here 58 static hci_stack_t hci_stack; 59 60 /** 61 * get connection for a given handle 62 * 63 * @return connection OR NULL, if not found 64 */ 65 hci_connection_t * connection_for_handle(hci_con_handle_t con_handle){ 66 linked_item_t *it; 67 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 68 if ( ((hci_connection_t *) it)->con_handle == con_handle){ 69 return (hci_connection_t *) it; 70 } 71 } 72 return NULL; 73 } 74 75 static void hci_connection_timeout_handler(timer_source_t *timer){ 76 hci_connection_t * connection = linked_item_get_user(&timer->item); 77 struct timeval tv; 78 gettimeofday(&tv, NULL); 79 if (tv.tv_sec >= connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000) { 80 // connections might be timed out 81 hci_emit_l2cap_check_timeout(connection); 82 run_loop_set_timer(timer, HCI_CONNECTION_TIMEOUT_MS); 83 } else { 84 // next timeout check at 85 timer->timeout.tv_sec = connection->timestamp.tv_sec + HCI_CONNECTION_TIMEOUT_MS/1000; 86 } 87 run_loop_add_timer(timer); 88 } 89 90 static void hci_connection_timestamp(hci_connection_t *connection){ 91 gettimeofday(&connection->timestamp, NULL); 92 } 93 94 /** 95 * create connection for given address 96 * 97 * @return connection OR NULL, if not found 98 */ 99 static hci_connection_t * create_connection_for_addr(bd_addr_t addr){ 100 hci_connection_t * conn = malloc( sizeof(hci_connection_t) ); 101 if (!conn) return NULL; 102 BD_ADDR_COPY(conn->address, addr); 103 conn->con_handle = 0xffff; 104 conn->authentication_flags = 0; 105 linked_item_set_user(&conn->timeout.item, conn); 106 conn->timeout.process = hci_connection_timeout_handler; 107 hci_connection_timestamp(conn); 108 conn->acl_recombination_length = 0; 109 conn->acl_recombination_pos = 0; 110 conn->num_acl_packets_sent = 0; 111 linked_list_add(&hci_stack.connections, (linked_item_t *) conn); 112 return conn; 113 } 114 115 /** 116 * get connection for given address 117 * 118 * @return connection OR NULL, if not found 119 */ 120 static hci_connection_t * connection_for_address(bd_addr_t address){ 121 linked_item_t *it; 122 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 123 if ( ! BD_ADDR_CMP( ((hci_connection_t *) it)->address, address) ){ 124 return (hci_connection_t *) it; 125 } 126 } 127 return NULL; 128 } 129 130 /** 131 * add authentication flags 132 */ 133 static void hci_add_connection_flags_for_flipped_bd_addr(uint8_t *bd_addr, hci_authentication_flags_t flags){ 134 bd_addr_t addr; 135 bt_flip_addr(addr, *(bd_addr_t *) bd_addr); 136 hci_connection_t * conn = connection_for_address(addr); 137 if (conn) { 138 conn->authentication_flags |= flags; 139 } 140 } 141 142 143 /** 144 * count connections 145 */ 146 static int nr_hci_connections(){ 147 int count = 0; 148 linked_item_t *it; 149 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next, count++); 150 return count; 151 } 152 153 /** 154 * Dummy handler called by HCI 155 */ 156 static void dummy_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 157 } 158 159 uint8_t hci_number_outgoing_packets(hci_con_handle_t handle){ 160 hci_connection_t * connection = connection_for_handle(handle); 161 if (!connection) { 162 log_err("hci_number_outgoing_packets connectino for handle %u does not exist!\n", handle); 163 return 0; 164 } 165 return connection->num_acl_packets_sent; 166 } 167 168 uint8_t hci_number_free_acl_slots(){ 169 uint8_t free_slots = hci_stack.total_num_acl_packets; 170 linked_item_t *it; 171 for (it = (linked_item_t *) hci_stack.connections; it ; it = it->next){ 172 hci_connection_t * connection = (hci_connection_t *) it; 173 if (free_slots < connection->num_acl_packets_sent) { 174 log_err("hci_number_free_acl_slots: sum of outgoing packets > total acl packets!\n"); 175 return 0; 176 } 177 free_slots -= connection->num_acl_packets_sent; 178 } 179 return free_slots; 180 } 181 182 int hci_ready_to_send(hci_con_handle_t handle){ 183 return hci_number_free_acl_slots() && hci_number_outgoing_packets(handle) < 2; 184 } 185 186 int hci_send_acl_packet(uint8_t *packet, int size){ 187 188 // check for free places on BT module 189 if (!hci_number_free_acl_slots()) return -1; 190 191 hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 192 hci_connection_t *connection = connection_for_handle( con_handle); 193 if (!connection) return 0; 194 hci_connection_timestamp(connection); 195 196 // count packet 197 connection->num_acl_packets_sent++; 198 // log_dbg("hci_send_acl_packet - handle %u, sent %u\n", connection->con_handle, connection->num_acl_packets_sent); 199 200 // send packet - ignore errors 201 hci_stack.hci_transport->send_packet(HCI_ACL_DATA_PACKET, packet, size); 202 203 return 0; 204 } 205 206 static void acl_handler(uint8_t *packet, int size){ 207 208 // get info 209 hci_con_handle_t con_handle = READ_ACL_CONNECTION_HANDLE(packet); 210 hci_connection_t *conn = connection_for_handle(con_handle); 211 uint8_t acl_flags = READ_ACL_FLAGS(packet); 212 uint16_t acl_length = READ_ACL_LENGTH(packet); 213 214 // ignore non-registered handle 215 if (!conn){ 216 log_err( "hci.c: acl_handler called with non-registered handle %u!\n" , con_handle); 217 return; 218 } 219 220 // update idle timestamp 221 hci_connection_timestamp(conn); 222 223 // handle different packet types 224 switch (acl_flags & 0x03) { 225 226 case 0x01: // continuation fragment 227 228 // sanity check 229 if (conn->acl_recombination_pos == 0) { 230 log_err( "ACL Cont Fragment but no first fragment for handle 0x%02x\n", con_handle); 231 return; 232 } 233 234 // append fragment payload (header already stored) 235 memcpy(&conn->acl_recombination_buffer[conn->acl_recombination_pos], &packet[4], acl_length ); 236 conn->acl_recombination_pos += acl_length; 237 238 // log_err( "ACL Cont Fragment: acl_len %u, combined_len %u, l2cap_len %u\n", 239 // acl_length, connection->acl_recombination_pos, connection->acl_recombination_length); 240 241 // forward complete L2CAP packet if complete. 242 if (conn->acl_recombination_pos >= conn->acl_recombination_length + 4 + 4){ // pos already incl. ACL header 243 244 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, conn->acl_recombination_buffer, conn->acl_recombination_pos); 245 // reset recombination buffer 246 conn->acl_recombination_length = 0; 247 conn->acl_recombination_pos = 0; 248 } 249 break; 250 251 case 0x02: { // first fragment 252 253 // sanity check 254 if (conn->acl_recombination_pos) { 255 log_err( "ACL First Fragment but data in buffer for handle 0x%02x\n", con_handle); 256 return; 257 } 258 259 // peek into L2CAP packet! 260 uint16_t l2cap_length = READ_L2CAP_LENGTH( packet ); 261 262 // compare fragment size to L2CAP packet size 263 if (acl_length >= l2cap_length + 4){ 264 265 // forward fragment as L2CAP packet 266 hci_stack.packet_handler(HCI_ACL_DATA_PACKET, packet, acl_length + 4); 267 268 } else { 269 // store first fragment and tweak acl length for complete package 270 memcpy(conn->acl_recombination_buffer, packet, acl_length + 4); 271 conn->acl_recombination_pos = acl_length + 4; 272 conn->acl_recombination_length = l2cap_length; 273 bt_store_16(conn->acl_recombination_buffer, 2, acl_length +4); 274 // log_err( "ACL First Fragment: acl_len %u, l2cap_len %u\n", acl_length, l2cap_length); 275 } 276 break; 277 278 } 279 default: 280 log_err( "hci.c: acl_handler called with invalid packet boundary flags %u\n", acl_flags & 0x03); 281 return; 282 } 283 284 // execute main loop 285 hci_run(); 286 } 287 288 static void event_handler(uint8_t *packet, int size){ 289 bd_addr_t addr; 290 hci_con_handle_t handle; 291 hci_connection_t * conn; 292 int i; 293 294 // get num_cmd_packets 295 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE || packet[0] == HCI_EVENT_COMMAND_STATUS){ 296 // Get Num_HCI_Command_Packets 297 hci_stack.num_cmd_packets = packet[2]; 298 } 299 300 switch (packet[0]) { 301 302 case HCI_EVENT_COMMAND_COMPLETE: 303 if (COMMAND_COMPLETE_EVENT(packet, hci_read_buffer_size)){ 304 // from offset 5 305 // status 306 hci_stack.acl_data_packet_length = READ_BT_16(packet, 6); 307 // ignore: SCO data packet len (8) 308 hci_stack.total_num_acl_packets = packet[9]; 309 // ignore: total num SCO packets 310 if (hci_stack.state == HCI_STATE_INITIALIZING){ 311 log_dbg("hci_read_buffer_size: size %u, count %u\n", hci_stack.acl_data_packet_length, hci_stack.total_num_acl_packets); 312 } 313 } 314 break; 315 316 case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS: 317 for (i=0; i<packet[2];i++){ 318 handle = READ_BT_16(packet, 3 + 2*i); 319 uint16_t num_packets = READ_BT_16(packet, 3 + packet[2]*2 + 2*i); 320 conn = connection_for_handle(handle); 321 if (!conn){ 322 log_err("hci_number_completed_packet lists unused con handle %u\n", handle); 323 continue; 324 } 325 conn->num_acl_packets_sent -= num_packets; 326 // log_dbg("hci_number_completed_packet %u processed for handle %u, outstanding %u\n", num_packets, handle, conn->num_acl_packets_sent); 327 } 328 break; 329 330 case HCI_EVENT_CONNECTION_REQUEST: 331 bt_flip_addr(addr, &packet[2]); 332 // TODO: eval COD 8-10 333 uint8_t link_type = packet[11]; 334 log_dbg("Connection_incoming: "); print_bd_addr(addr); log_dbg(", type %u\n", link_type); 335 if (link_type == 1) { // ACL 336 conn = connection_for_address(addr); 337 if (!conn) { 338 conn = create_connection_for_addr(addr); 339 } 340 // TODO: check for malloc failure 341 conn->state = ACCEPTED_CONNECTION_REQUEST; 342 hci_send_cmd(&hci_accept_connection_request, addr, 1); 343 } else { 344 // TODO: decline request 345 } 346 break; 347 348 case HCI_EVENT_CONNECTION_COMPLETE: 349 // Connection management 350 bt_flip_addr(addr, &packet[5]); 351 log_dbg("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); log_dbg("\n"); 352 conn = connection_for_address(addr); 353 if (conn) { 354 if (!packet[2]){ 355 conn->state = OPEN; 356 conn->con_handle = READ_BT_16(packet, 3); 357 358 gettimeofday(&conn->timestamp, NULL); 359 run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 360 run_loop_add_timer(&conn->timeout); 361 362 log_dbg("New connection: handle %u, ", conn->con_handle); 363 print_bd_addr( conn->address ); 364 log_dbg("\n"); 365 366 hci_emit_nr_connections_changed(); 367 } else { 368 // connection failed, remove entry 369 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 370 free( conn ); 371 } 372 } 373 break; 374 375 case HCI_EVENT_LINK_KEY_REQUEST: 376 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 377 break; 378 379 case HCI_EVENT_LINK_KEY_NOTIFICATION: 380 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_NOTIFICATION); 381 break; 382 383 case HCI_EVENT_PIN_CODE_REQUEST: 384 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_PIN_CODE_REQUEST); 385 break; 386 387 case HCI_EVENT_DISCONNECTION_COMPLETE: 388 hci_add_connection_flags_for_flipped_bd_addr(&packet[2], RECV_LINK_KEY_REQUEST); 389 if (!packet[2]){ 390 handle = READ_BT_16(packet, 3); 391 hci_connection_t * conn = connection_for_handle(handle); 392 if (conn) { 393 log_dbg("Connection closed: handle %u, ", conn->con_handle); 394 print_bd_addr( conn->address ); 395 log_dbg("\n"); 396 run_loop_remove_timer(&conn->timeout); 397 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 398 free( conn ); 399 hci_emit_nr_connections_changed(); 400 } 401 } 402 break; 403 404 default: 405 break; 406 } 407 408 // handle BT initialization 409 if (hci_stack.state == HCI_STATE_INITIALIZING){ 410 // handle H4 synchronization loss on restart 411 // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 412 // hci_stack.substate = 0; 413 // } 414 // handle normal init sequence 415 if (hci_stack.substate % 2){ 416 // odd: waiting for event 417 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 418 hci_stack.substate++; 419 } 420 } 421 } 422 423 hci_stack.packet_handler(HCI_EVENT_PACKET, packet, size); 424 425 // execute main loop 426 hci_run(); 427 } 428 429 void packet_handler(uint8_t packet_type, uint8_t *packet, uint16_t size){ 430 switch (packet_type) { 431 case HCI_EVENT_PACKET: 432 event_handler(packet, size); 433 break; 434 case HCI_ACL_DATA_PACKET: 435 acl_handler(packet, size); 436 break; 437 default: 438 break; 439 } 440 } 441 442 /** Register HCI packet handlers */ 443 void hci_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)){ 444 hci_stack.packet_handler = handler; 445 } 446 447 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 448 449 // reference to use transport layer implementation 450 hci_stack.hci_transport = transport; 451 452 // references to used control implementation 453 hci_stack.control = control; 454 455 // reference to used config 456 hci_stack.config = config; 457 458 // no connections yet 459 hci_stack.connections = NULL; 460 461 // empty cmd buffer 462 hci_stack.hci_cmd_buffer = malloc(3+255); 463 464 // higher level handler 465 hci_stack.packet_handler = dummy_handler; 466 467 // register packet handlers with transport 468 transport->register_packet_handler(&packet_handler); 469 } 470 471 int hci_power_control(HCI_POWER_MODE power_mode){ 472 if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 473 474 // power on 475 int err = 0; 476 if (hci_stack.control && hci_stack.control->on){ 477 err = (*hci_stack.control->on)(hci_stack.config); 478 } 479 if (err){ 480 log_err( "POWER_ON failed\n"); 481 hci_emit_hci_open_failed(); 482 return err; 483 } 484 485 // open low-level device 486 err = hci_stack.hci_transport->open(hci_stack.config); 487 if (err){ 488 log_err( "HCI_INIT failed, turning Bluetooth off again\n"); 489 if (hci_stack.control && hci_stack.control->off){ 490 (*hci_stack.control->off)(hci_stack.config); 491 } 492 hci_emit_hci_open_failed(); 493 return err; 494 } 495 496 // set up state machine 497 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 498 hci_stack.state = HCI_STATE_INITIALIZING; 499 hci_stack.substate = 0; 500 501 } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 502 503 // close low-level device 504 hci_stack.hci_transport->close(hci_stack.config); 505 506 // power off 507 if (hci_stack.control && hci_stack.control->off){ 508 (*hci_stack.control->off)(hci_stack.config); 509 } 510 511 // we're off now 512 hci_stack.state = HCI_STATE_OFF; 513 } 514 515 // create internal event 516 hci_emit_state(); 517 518 // trigger next/first action 519 hci_run(); 520 521 return 0; 522 } 523 524 void hci_run(){ 525 526 switch (hci_stack.state){ 527 case HCI_STATE_INITIALIZING: 528 if (hci_stack.substate % 2) { 529 // odd: waiting for command completion 530 return; 531 } 532 if (hci_stack.num_cmd_packets == 0) { 533 // cannot send command yet 534 return; 535 } 536 switch (hci_stack.substate >> 1){ 537 case 0: 538 hci_send_cmd(&hci_reset); 539 break; 540 case 1: 541 // custom initialization 542 if (hci_stack.control && hci_stack.control->next_command){ 543 uint8_t * cmd = (*hci_stack.control->next_command)(hci_stack.config); 544 if (cmd) { 545 int size = 3 + cmd[2]; 546 hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, cmd, size); 547 hci_stack.substate = 0; // more init commands 548 break; 549 } 550 } 551 // otherwise continue 552 hci_send_cmd(&hci_read_bd_addr); 553 break; 554 case 2: 555 hci_send_cmd(&hci_read_buffer_size); 556 break; 557 case 3: 558 // ca. 15 sec 559 hci_send_cmd(&hci_write_page_timeout, 0x6000); 560 break; 561 case 4: 562 hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 563 break; 564 case 5: 565 #ifndef EMBEDDED 566 { 567 char hostname[30]; 568 gethostname(hostname, 30); 569 hostname[29] = '\0'; 570 hci_send_cmd(&hci_write_local_name, hostname); 571 break; 572 } 573 case 6: 574 #endif 575 // done. 576 hci_stack.state = HCI_STATE_WORKING; 577 hci_emit_state(); 578 break; 579 default: 580 break; 581 } 582 hci_stack.substate++; 583 break; 584 default: 585 break; 586 } 587 } 588 589 int hci_send_cmd_packet(uint8_t *packet, int size){ 590 bd_addr_t addr; 591 hci_connection_t * conn; 592 // house-keeping 593 594 // create_connection? 595 if (IS_COMMAND(packet, hci_create_connection)){ 596 bt_flip_addr(addr, &packet[3]); 597 log_dbg("Create_connection to "); print_bd_addr(addr); log_dbg("\n"); 598 conn = connection_for_address(addr); 599 if (conn) { 600 // if connection exists 601 if (conn->state == OPEN) { 602 // if OPEN, emit connection complete command 603 hci_emit_connection_complete(conn); 604 } 605 // otherwise, just ignore 606 return 0; // don't sent packet to controller 607 608 } else{ 609 conn = create_connection_for_addr(addr); 610 if (conn){ 611 // create connection struct and register, state = SENT_CREATE_CONNECTION 612 conn->state = SENT_CREATE_CONNECTION; 613 } 614 } 615 } 616 617 if (IS_COMMAND(packet, hci_link_key_request_reply)){ 618 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_REPLY); 619 } 620 if (IS_COMMAND(packet, hci_link_key_request_negative_reply)){ 621 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_LINK_KEY_NEGATIVE_REQUEST); 622 } 623 if (IS_COMMAND(packet, hci_pin_code_request_reply)){ 624 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_REPLY); 625 } 626 if (IS_COMMAND(packet, hci_pin_code_request_negative_reply)){ 627 hci_add_connection_flags_for_flipped_bd_addr(&packet[3], SENT_PIN_CODE_NEGATIVE_REPLY); 628 } 629 630 // accept connection 631 632 // reject connection 633 634 // close_connection? 635 // set state = SENT_DISCONNECT 636 637 hci_stack.num_cmd_packets--; 638 return hci_stack.hci_transport->send_packet(HCI_COMMAND_DATA_PACKET, packet, size); 639 } 640 641 /** 642 * pre: numcmds >= 0 - it's allowed to send a command to the controller 643 */ 644 int hci_send_cmd(const hci_cmd_t *cmd, ...){ 645 va_list argptr; 646 va_start(argptr, cmd); 647 uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 648 uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 649 va_end(argptr); 650 return hci_send_cmd_packet(hci_cmd_buffer, size); 651 } 652 653 // Create various non-HCI events. 654 // TODO: generalize, use table similar to hci_create_command 655 656 void hci_emit_state(){ 657 uint8_t len = 3; 658 uint8_t event[len]; 659 event[0] = BTSTACK_EVENT_STATE; 660 event[1] = len - 3; 661 event[2] = hci_stack.state; 662 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 663 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 664 } 665 666 void hci_emit_connection_complete(hci_connection_t *conn){ 667 uint8_t len = 13; 668 uint8_t event[len]; 669 event[0] = HCI_EVENT_CONNECTION_COMPLETE; 670 event[1] = len - 3; 671 event[2] = 0; // status = OK 672 bt_store_16(event, 3, conn->con_handle); 673 bt_flip_addr(&event[5], conn->address); 674 event[11] = 1; // ACL connection 675 event[12] = 0; // encryption disabled 676 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 677 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 678 } 679 680 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 681 uint8_t len = 4; 682 uint8_t event[len]; 683 event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 684 event[1] = len - 2; 685 bt_store_16(event, 2, conn->con_handle); 686 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 687 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 688 } 689 690 void hci_emit_nr_connections_changed(){ 691 uint8_t len = 3; 692 uint8_t event[len]; 693 event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 694 event[1] = len - 2; 695 event[2] = nr_hci_connections(); 696 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 697 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 698 } 699 700 void hci_emit_hci_open_failed(){ 701 uint8_t len = 2; 702 uint8_t event[len]; 703 event[0] = BTSTACK_EVENT_POWERON_FAILED; 704 event[1] = len - 2; 705 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 706 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 707 } 708 709 710 void hci_emit_btstack_version() { 711 uint8_t len = 6; 712 uint8_t event[len]; 713 event[0] = BTSTACK_EVENT_VERSION; 714 event[1] = len - 2; 715 event[len++] = BTSTACK_MAJOR; 716 event[len++] = BTSTACK_MINOR; 717 bt_store_16(event, len, BTSTACK_REVISION); 718 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 719 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 720 } 721 722 void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 723 uint8_t len = 3; 724 uint8_t event[len]; 725 event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 726 event[1] = len - 2; 727 event[2] = enabled; 728 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 729 hci_stack.packet_handler(HCI_EVENT_PACKET, event, len); 730 } 731