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 181 switch (packet[0]) { 182 183 case HCI_EVENT_COMMAND_COMPLETE: 184 case HCI_EVENT_COMMAND_STATUS: 185 // Get Num_HCI_Command_Packets 186 hci_stack.num_cmd_packets = packet[2]; 187 break; 188 189 case HCI_EVENT_CONNECTION_COMPLETE: 190 // Connection management 191 bt_flip_addr(addr, &packet[5]); 192 printf("Connection_complete (status=%u)", packet[2]); print_bd_addr(addr); printf("\n"); 193 hci_connection_t * conn = connection_for_address(addr); 194 if (conn) { 195 if (!packet[2]){ 196 conn->state = OPEN; 197 conn->con_handle = READ_BT_16(packet, 3); 198 conn->flags = 0; 199 200 gettimeofday(&conn->timestamp, NULL); 201 run_loop_set_timer(&conn->timeout, HCI_CONNECTION_TIMEOUT_MS); 202 run_loop_add_timer(&conn->timeout); 203 204 printf("New connection: handle %u, ", conn->con_handle); 205 print_bd_addr( conn->address ); 206 printf("\n"); 207 208 hci_emit_nr_connections_changed(); 209 } else { 210 // connection failed, remove entry 211 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 212 free( conn ); 213 } 214 } 215 break; 216 217 case HCI_EVENT_DISCONNECTION_COMPLETE: 218 if (!packet[2]){ 219 handle = READ_BT_16(packet, 3); 220 hci_connection_t * conn = connection_for_handle(handle); 221 if (conn) { 222 printf("Connection closed: handle %u, ", conn->con_handle); 223 print_bd_addr( conn->address ); 224 printf("\n"); 225 run_loop_remove_timer(&conn->timeout); 226 linked_list_remove(&hci_stack.connections, (linked_item_t *) conn); 227 free( conn ); 228 hci_emit_nr_connections_changed(); 229 } 230 } 231 break; 232 233 default: 234 break; 235 } 236 237 // handle BT initialization 238 if (hci_stack.state == HCI_STATE_INITIALIZING){ 239 // handle H4 synchronization loss on restart 240 // if (hci_stack.substate == 1 && packet[0] == HCI_EVENT_HARDWARE_ERROR){ 241 // hci_stack.substate = 0; 242 // } 243 // handle normal init sequence 244 if (hci_stack.substate % 2){ 245 // odd: waiting for event 246 if (packet[0] == HCI_EVENT_COMMAND_COMPLETE){ 247 hci_stack.substate++; 248 } 249 } 250 } 251 252 hci_stack.event_packet_handler(packet, size); 253 254 // execute main loop 255 hci_run(); 256 } 257 258 /** Register HCI packet handlers */ 259 void hci_register_event_packet_handler(void (*handler)(uint8_t *packet, uint16_t size)){ 260 hci_stack.event_packet_handler = handler; 261 } 262 void hci_register_acl_packet_handler (void (*handler)(uint8_t *packet, uint16_t size)){ 263 hci_stack.acl_packet_handler = handler; 264 } 265 266 void hci_init(hci_transport_t *transport, void *config, bt_control_t *control){ 267 268 // reference to use transport layer implementation 269 hci_stack.hci_transport = transport; 270 271 // references to used control implementation 272 if (control) { 273 hci_stack.control = control; 274 } else { 275 hci_stack.control = &null_control; 276 } 277 278 // reference to used config 279 hci_stack.config = config; 280 281 // no connections yet 282 hci_stack.connections = NULL; 283 284 // empty cmd buffer 285 hci_stack.hci_cmd_buffer = malloc(3+255); 286 287 // higher level handler 288 hci_stack.event_packet_handler = dummy_handler; 289 hci_stack.acl_packet_handler = dummy_handler; 290 291 // register packet handlers with transport 292 transport->register_event_packet_handler( event_handler); 293 transport->register_acl_packet_handler( acl_handler); 294 } 295 296 int hci_power_control(HCI_POWER_MODE power_mode){ 297 if (power_mode == HCI_POWER_ON && hci_stack.state == HCI_STATE_OFF) { 298 299 // power on 300 int err = hci_stack.control->on(hci_stack.config); 301 if (err){ 302 fprintf(stderr, "POWER_ON failed\n"); 303 hci_emit_hci_open_failed(); 304 return err; 305 } 306 307 // open low-level device 308 err = hci_stack.hci_transport->open(hci_stack.config); 309 if (err){ 310 fprintf(stderr, "HCI_INIT failed, turning Bluetooth off again\n"); 311 hci_stack.control->off(hci_stack.config); 312 hci_emit_hci_open_failed(); 313 return err; 314 } 315 316 // set up state machine 317 hci_stack.num_cmd_packets = 1; // assume that one cmd can be sent 318 hci_stack.state = HCI_STATE_INITIALIZING; 319 hci_stack.substate = 0; 320 321 } else if (power_mode == HCI_POWER_OFF && hci_stack.state == HCI_STATE_WORKING){ 322 323 // close low-level device 324 hci_stack.hci_transport->close(hci_stack.config); 325 326 // power off 327 hci_stack.control->off(hci_stack.config); 328 329 // we're off now 330 hci_stack.state = HCI_STATE_OFF; 331 } 332 333 // create internal event 334 hci_emit_state(); 335 336 // trigger next/first action 337 hci_run(); 338 339 return 0; 340 } 341 342 void hci_run(){ 343 switch (hci_stack.state){ 344 case HCI_STATE_INITIALIZING: 345 if (hci_stack.substate % 2) { 346 // odd: waiting for command completion 347 return; 348 } 349 if (hci_stack.num_cmd_packets == 0) { 350 // cannot send command yet 351 return; 352 } 353 switch (hci_stack.substate/2){ 354 case 0: 355 hci_send_cmd(&hci_reset); 356 break; 357 case 1: 358 hci_send_cmd(&hci_read_bd_addr); 359 break; 360 case 2: 361 // ca. 15 sec 362 hci_send_cmd(&hci_write_page_timeout, 0x6000); 363 break; 364 case 3: 365 hci_send_cmd(&hci_write_scan_enable, 3); // 3 inq scan + page scan 366 break; 367 case 4: 368 // done. 369 hci_stack.state = HCI_STATE_WORKING; 370 hci_emit_state(); 371 break; 372 default: 373 break; 374 } 375 hci_stack.substate++; 376 break; 377 default: 378 break; 379 } 380 } 381 382 int hci_send_cmd_packet(uint8_t *packet, int size){ 383 bd_addr_t addr; 384 hci_connection_t * conn; 385 // house-keeping 386 387 // create_connection? 388 if (IS_COMMAND(packet, hci_create_connection)){ 389 bt_flip_addr(addr, &packet[3]); 390 printf("Create_connection to "); print_bd_addr(addr); printf("\n"); 391 conn = connection_for_address(addr); 392 if (conn) { 393 // if connection exists 394 if (conn->state == OPEN) { 395 // if OPEN, emit connection complete command 396 hci_emit_connection_complete(conn); 397 } 398 // otherwise, just ignore 399 return 0; // don't sent packet to controller 400 401 } else{ 402 conn = create_connection_for_addr(addr); 403 if (conn){ 404 // create connection struct and register, state = SENT_CREATE_CONNECTION 405 conn->state = SENT_CREATE_CONNECTION; 406 } 407 } 408 } 409 410 // accept connection 411 412 // reject connection 413 414 // close_connection? 415 // set state = SENT_DISCONNECT 416 417 hci_stack.num_cmd_packets--; 418 return hci_stack.hci_transport->send_cmd_packet(packet, size); 419 } 420 421 /** 422 * pre: numcmds >= 0 - it's allowed to send a command to the controller 423 */ 424 int hci_send_cmd(hci_cmd_t *cmd, ...){ 425 va_list argptr; 426 va_start(argptr, cmd); 427 uint8_t * hci_cmd_buffer = hci_stack.hci_cmd_buffer; 428 uint16_t size = hci_create_cmd_internal(hci_stack.hci_cmd_buffer, cmd, argptr); 429 va_end(argptr); 430 return hci_send_cmd_packet(hci_cmd_buffer, size); 431 } 432 433 // Create various non-HCI events. 434 // TODO: generalize, use table similar to hci_create_command 435 436 void hci_emit_state(){ 437 uint8_t len = 3; 438 uint8_t event[len]; 439 event[0] = BTSTACK_EVENT_STATE; 440 event[1] = len - 3; 441 event[2] = hci_stack.state; 442 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 443 hci_stack.event_packet_handler(event, len); 444 } 445 446 void hci_emit_connection_complete(hci_connection_t *conn){ 447 uint8_t len = 13; 448 uint8_t event[len]; 449 event[0] = HCI_EVENT_CONNECTION_COMPLETE; 450 event[1] = len - 3; 451 event[2] = 0; // status = OK 452 bt_store_16(event, 3, conn->con_handle); 453 bt_flip_addr(&event[5], conn->address); 454 event[11] = 1; // ACL connection 455 event[12] = 0; // encryption disabled 456 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 457 hci_stack.event_packet_handler(event, len); 458 } 459 460 void hci_emit_l2cap_check_timeout(hci_connection_t *conn){ 461 uint8_t len = 4; 462 uint8_t event[len]; 463 event[0] = L2CAP_EVENT_TIMEOUT_CHECK; 464 event[1] = len - 2; 465 bt_store_16(event, 2, conn->con_handle); 466 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 467 hci_stack.event_packet_handler(event, len); 468 } 469 470 void hci_emit_nr_connections_changed(){ 471 uint8_t len = 3; 472 uint8_t event[len]; 473 event[0] = BTSTACK_EVENT_NR_CONNECTIONS_CHANGED; 474 event[1] = len - 2; 475 event[2] = nr_hci_connections(); 476 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 477 hci_stack.event_packet_handler(event, len); 478 } 479 480 void hci_emit_hci_open_failed(){ 481 uint8_t len = 2; 482 uint8_t event[len]; 483 event[0] = BTSTACK_EVENT_POWERON_FAILED; 484 event[1] = len - 2; 485 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 486 hci_stack.event_packet_handler(event, len); 487 } 488 489 490 void hci_emit_btstack_version() { 491 uint8_t len = 6; 492 uint8_t event[len]; 493 event[0] = BTSTACK_EVENT_VERSION; 494 event[1] = len - 2; 495 event[len++] = BTSTACK_MAJOR; 496 event[len++] = BTSTACK_MINOR; 497 bt_store_16(event, len, BTSTACK_REVISION); 498 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 499 hci_stack.event_packet_handler(event, len); 500 } 501 502 void hci_emit_system_bluetooth_enabled(uint8_t enabled){ 503 uint8_t len = 3; 504 uint8_t event[len]; 505 event[0] = BTSTACK_EVENT_SYSTEM_BLUETOOTH_ENABLED; 506 event[1] = len - 3; 507 event[2] = enabled; 508 hci_dump_packet( HCI_EVENT_PACKET, 0, event, len); 509 hci_stack.event_packet_handler(event, len); 510 } 511