1 /****************************************************************************** 2 * 3 * Copyright 2014 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #define LOG_TAG "bt_controller" 20 21 #include "device/include/controller.h" 22 23 #include <base/logging.h> 24 25 #include "bt_types.h" 26 #include "btcore/include/event_mask.h" 27 #include "btcore/include/module.h" 28 #include "btcore/include/version.h" 29 #include "hcimsgs.h" 30 #include "osi/include/future.h" 31 #include "stack/include/btm_ble_api.h" 32 33 const bt_event_mask_t BLE_EVENT_MASK = { 34 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1E, 0x7f}}; 35 36 const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT}; 37 38 // TODO(zachoverflow): factor out into common module 39 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff; 40 41 #define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64 42 #define MAX_FEATURES_CLASSIC_PAGE_COUNT 3 43 #define BLE_SUPPORTED_STATES_SIZE 8 44 #define BLE_SUPPORTED_FEATURES_SIZE 8 45 #define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8 46 47 static const hci_t* hci; 48 static const hci_packet_factory_t* packet_factory; 49 static const hci_packet_parser_t* packet_parser; 50 51 static RawAddress address; 52 static bt_version_t bt_version; 53 54 static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE]; 55 static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT]; 56 static uint8_t last_features_classic_page_index; 57 58 static uint16_t acl_data_size_classic; 59 static uint16_t acl_data_size_ble; 60 static uint16_t acl_buffer_count_classic; 61 static uint8_t acl_buffer_count_ble; 62 63 static uint8_t ble_white_list_size; 64 static uint8_t ble_resolving_list_max_size; 65 static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE]; 66 static bt_device_features_t features_ble; 67 static uint16_t ble_suggested_default_data_length; 68 static uint16_t ble_supported_max_tx_octets; 69 static uint16_t ble_supported_max_tx_time; 70 static uint16_t ble_supported_max_rx_octets; 71 static uint16_t ble_supported_max_rx_time; 72 73 static uint16_t ble_maxium_advertising_data_length; 74 static uint8_t ble_number_of_supported_advertising_sets; 75 static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE]; 76 static uint8_t number_of_local_supported_codecs = 0; 77 78 static bool readable; 79 static bool ble_supported; 80 static bool simple_pairing_supported; 81 static bool secure_connections_supported; 82 83 #define AWAIT_COMMAND(command) \ 84 static_cast<BT_HDR*>(future_await(hci->transmit_command_futured(command))) 85 86 // Module lifecycle functions 87 88 static future_t* start_up(void) { 89 BT_HDR* response; 90 91 // Send the initial reset command 92 response = AWAIT_COMMAND(packet_factory->make_reset()); 93 packet_parser->parse_generic_command_complete(response); 94 95 // Request the classic buffer size next 96 response = AWAIT_COMMAND(packet_factory->make_read_buffer_size()); 97 packet_parser->parse_read_buffer_size_response( 98 response, &acl_data_size_classic, &acl_buffer_count_classic); 99 100 // Tell the controller about our buffer sizes and buffer counts next 101 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just 102 // a hardcoded 10? 103 response = AWAIT_COMMAND(packet_factory->make_host_buffer_size( 104 L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10)); 105 106 packet_parser->parse_generic_command_complete(response); 107 108 // Read the local version info off the controller next, including 109 // information such as manufacturer and supported HCI version 110 response = AWAIT_COMMAND(packet_factory->make_read_local_version_info()); 111 packet_parser->parse_read_local_version_info_response(response, &bt_version); 112 113 // Read the bluetooth address off the controller next 114 response = AWAIT_COMMAND(packet_factory->make_read_bd_addr()); 115 packet_parser->parse_read_bd_addr_response(response, &address); 116 117 // Request the controller's supported commands next 118 response = 119 AWAIT_COMMAND(packet_factory->make_read_local_supported_commands()); 120 packet_parser->parse_read_local_supported_commands_response( 121 response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE); 122 123 // Read page 0 of the controller features next 124 uint8_t page_number = 0; 125 response = AWAIT_COMMAND( 126 packet_factory->make_read_local_extended_features(page_number)); 127 packet_parser->parse_read_local_extended_features_response( 128 response, &page_number, &last_features_classic_page_index, 129 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT); 130 131 CHECK(page_number == 0); 132 page_number++; 133 134 // Inform the controller what page 0 features we support, based on what 135 // it told us it supports. We need to do this first before we request the 136 // next page, because the controller's response for page 1 may be 137 // dependent on what we configure from page 0 138 simple_pairing_supported = 139 HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array); 140 if (simple_pairing_supported) { 141 response = AWAIT_COMMAND( 142 packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED)); 143 packet_parser->parse_generic_command_complete(response); 144 } 145 146 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) { 147 uint8_t simultaneous_le_host = 148 HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) 149 ? BTM_BLE_SIMULTANEOUS_HOST 150 : 0; 151 response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support( 152 BTM_BLE_HOST_SUPPORT, simultaneous_le_host)); 153 154 packet_parser->parse_generic_command_complete(response); 155 156 // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1 157 if (last_features_classic_page_index < 1) 158 last_features_classic_page_index = 1; 159 } 160 161 // Done telling the controller about what page 0 features we support 162 // Request the remaining feature pages 163 while (page_number <= last_features_classic_page_index && 164 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) { 165 response = AWAIT_COMMAND( 166 packet_factory->make_read_local_extended_features(page_number)); 167 packet_parser->parse_read_local_extended_features_response( 168 response, &page_number, &last_features_classic_page_index, 169 features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT); 170 171 page_number++; 172 } 173 174 #if (SC_MODE_INCLUDED == TRUE) 175 secure_connections_supported = 176 HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array); 177 if (secure_connections_supported) { 178 response = AWAIT_COMMAND( 179 packet_factory->make_write_secure_connections_host_support( 180 HCI_SC_MODE_ENABLED)); 181 packet_parser->parse_generic_command_complete(response); 182 } 183 #endif 184 185 ble_supported = last_features_classic_page_index >= 1 && 186 HCI_LE_HOST_SUPPORTED(features_classic[1].as_array); 187 if (ble_supported) { 188 // Request the ble white list size next 189 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size()); 190 packet_parser->parse_ble_read_white_list_size_response( 191 response, &ble_white_list_size); 192 193 // Request the ble buffer size next 194 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size()); 195 packet_parser->parse_ble_read_buffer_size_response( 196 response, &acl_data_size_ble, &acl_buffer_count_ble); 197 198 // Response of 0 indicates ble has the same buffer size as classic 199 if (acl_data_size_ble == 0) acl_data_size_ble = acl_data_size_classic; 200 201 // Request the ble supported states next 202 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states()); 203 packet_parser->parse_ble_read_supported_states_response( 204 response, ble_supported_states, sizeof(ble_supported_states)); 205 206 // Request the ble supported features next 207 response = 208 AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features()); 209 packet_parser->parse_ble_read_local_supported_features_response( 210 response, &features_ble); 211 212 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) { 213 response = 214 AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size()); 215 packet_parser->parse_ble_read_resolving_list_size_response( 216 response, &ble_resolving_list_max_size); 217 } 218 219 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) { 220 response = 221 AWAIT_COMMAND(packet_factory->make_ble_read_maximum_data_length()); 222 packet_parser->parse_ble_read_maximum_data_length_response( 223 response, &ble_supported_max_tx_octets, &ble_supported_max_tx_time, 224 &ble_supported_max_rx_octets, &ble_supported_max_rx_time); 225 226 response = AWAIT_COMMAND( 227 packet_factory->make_ble_read_suggested_default_data_length()); 228 packet_parser->parse_ble_read_suggested_default_data_length_response( 229 response, &ble_suggested_default_data_length); 230 } 231 232 if (HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array)) { 233 response = AWAIT_COMMAND( 234 packet_factory->make_ble_read_maximum_advertising_data_length()); 235 packet_parser->parse_ble_read_maximum_advertising_data_length( 236 response, &ble_maxium_advertising_data_length); 237 238 response = AWAIT_COMMAND( 239 packet_factory->make_ble_read_number_of_supported_advertising_sets()); 240 packet_parser->parse_ble_read_number_of_supported_advertising_sets( 241 response, &ble_number_of_supported_advertising_sets); 242 } else { 243 /* If LE Excended Advertising is not supported, use the default value */ 244 ble_maxium_advertising_data_length = 31; 245 } 246 247 // Set the ble event mask next 248 response = 249 AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK)); 250 packet_parser->parse_generic_command_complete(response); 251 } 252 253 if (simple_pairing_supported) { 254 response = 255 AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK)); 256 packet_parser->parse_generic_command_complete(response); 257 } 258 259 // read local supported codecs 260 if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) { 261 response = 262 AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs()); 263 packet_parser->parse_read_local_supported_codecs_response( 264 response, &number_of_local_supported_codecs, local_supported_codecs); 265 } 266 267 readable = true; 268 return future_new_immediate(FUTURE_SUCCESS); 269 } 270 271 static future_t* shut_down(void) { 272 readable = false; 273 return future_new_immediate(FUTURE_SUCCESS); 274 } 275 276 EXPORT_SYMBOL extern const module_t controller_module = { 277 .name = CONTROLLER_MODULE, 278 .init = NULL, 279 .start_up = start_up, 280 .shut_down = shut_down, 281 .clean_up = NULL, 282 .dependencies = {HCI_MODULE, NULL}}; 283 284 // Interface functions 285 286 static bool get_is_ready(void) { return readable; } 287 288 static const RawAddress* get_address(void) { 289 CHECK(readable); 290 return &address; 291 } 292 293 static const bt_version_t* get_bt_version(void) { 294 CHECK(readable); 295 return &bt_version; 296 } 297 298 // TODO(zachoverflow): hide inside, move decoder inside too 299 static const bt_device_features_t* get_features_classic(int index) { 300 CHECK(readable); 301 CHECK(index < MAX_FEATURES_CLASSIC_PAGE_COUNT); 302 return &features_classic[index]; 303 } 304 305 static uint8_t get_last_features_classic_index(void) { 306 CHECK(readable); 307 return last_features_classic_page_index; 308 } 309 310 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) { 311 CHECK(readable); 312 if (number_of_local_supported_codecs) { 313 *number_of_codecs = number_of_local_supported_codecs; 314 return local_supported_codecs; 315 } 316 return NULL; 317 } 318 319 static const bt_device_features_t* get_features_ble(void) { 320 CHECK(readable); 321 CHECK(ble_supported); 322 return &features_ble; 323 } 324 325 static const uint8_t* get_ble_supported_states(void) { 326 CHECK(readable); 327 CHECK(ble_supported); 328 return ble_supported_states; 329 } 330 331 static bool supports_simple_pairing(void) { 332 CHECK(readable); 333 return simple_pairing_supported; 334 } 335 336 static bool supports_secure_connections(void) { 337 CHECK(readable); 338 return secure_connections_supported; 339 } 340 341 static bool supports_simultaneous_le_bredr(void) { 342 CHECK(readable); 343 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array); 344 } 345 346 static bool supports_reading_remote_extended_features(void) { 347 CHECK(readable); 348 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands); 349 } 350 351 static bool supports_interlaced_inquiry_scan(void) { 352 CHECK(readable); 353 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array); 354 } 355 356 static bool supports_rssi_with_inquiry_results(void) { 357 CHECK(readable); 358 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array); 359 } 360 361 static bool supports_extended_inquiry_response(void) { 362 CHECK(readable); 363 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array); 364 } 365 366 static bool supports_master_slave_role_switch(void) { 367 CHECK(readable); 368 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array); 369 } 370 371 static bool supports_enhanced_setup_synchronous_connection(void) { 372 assert(readable); 373 return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands); 374 } 375 376 static bool supports_enhanced_accept_synchronous_connection(void) { 377 assert(readable); 378 return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands); 379 } 380 381 static bool supports_ble(void) { 382 CHECK(readable); 383 return ble_supported; 384 } 385 386 static bool supports_ble_privacy(void) { 387 CHECK(readable); 388 CHECK(ble_supported); 389 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array); 390 } 391 392 static bool supports_ble_set_privacy_mode() { 393 CHECK(readable); 394 CHECK(ble_supported); 395 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array) && 396 HCI_LE_SET_PRIVACY_MODE_SUPPORTED(supported_commands); 397 } 398 399 static bool supports_ble_packet_extension(void) { 400 CHECK(readable); 401 CHECK(ble_supported); 402 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array); 403 } 404 405 static bool supports_ble_connection_parameters_request(void) { 406 CHECK(readable); 407 CHECK(ble_supported); 408 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array); 409 } 410 411 static bool supports_ble_2m_phy(void) { 412 CHECK(readable); 413 CHECK(ble_supported); 414 return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array); 415 } 416 417 static bool supports_ble_coded_phy(void) { 418 CHECK(readable); 419 CHECK(ble_supported); 420 return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array); 421 } 422 423 static bool supports_ble_extended_advertising(void) { 424 CHECK(readable); 425 CHECK(ble_supported); 426 return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array); 427 } 428 429 static bool supports_ble_periodic_advertising(void) { 430 CHECK(readable); 431 CHECK(ble_supported); 432 return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array); 433 } 434 435 static uint16_t get_acl_data_size_classic(void) { 436 CHECK(readable); 437 return acl_data_size_classic; 438 } 439 440 static uint16_t get_acl_data_size_ble(void) { 441 CHECK(readable); 442 CHECK(ble_supported); 443 return acl_data_size_ble; 444 } 445 446 static uint16_t get_acl_packet_size_classic(void) { 447 CHECK(readable); 448 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE; 449 } 450 451 static uint16_t get_acl_packet_size_ble(void) { 452 CHECK(readable); 453 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE; 454 } 455 456 static uint16_t get_ble_suggested_default_data_length(void) { 457 CHECK(readable); 458 CHECK(ble_supported); 459 return ble_suggested_default_data_length; 460 } 461 462 static uint16_t get_ble_maximum_tx_data_length(void) { 463 CHECK(readable); 464 CHECK(ble_supported); 465 return ble_supported_max_tx_octets; 466 } 467 468 static uint16_t get_ble_maxium_advertising_data_length(void) { 469 CHECK(readable); 470 CHECK(ble_supported); 471 return ble_maxium_advertising_data_length; 472 } 473 474 static uint8_t get_ble_number_of_supported_advertising_sets(void) { 475 CHECK(readable); 476 CHECK(ble_supported); 477 return ble_number_of_supported_advertising_sets; 478 } 479 480 static uint16_t get_acl_buffer_count_classic(void) { 481 CHECK(readable); 482 return acl_buffer_count_classic; 483 } 484 485 static uint8_t get_acl_buffer_count_ble(void) { 486 CHECK(readable); 487 CHECK(ble_supported); 488 return acl_buffer_count_ble; 489 } 490 491 static uint8_t get_ble_white_list_size(void) { 492 CHECK(readable); 493 CHECK(ble_supported); 494 return ble_white_list_size; 495 } 496 497 static uint8_t get_ble_resolving_list_max_size(void) { 498 CHECK(readable); 499 CHECK(ble_supported); 500 return ble_resolving_list_max_size; 501 } 502 503 static void set_ble_resolving_list_max_size(int resolving_list_max_size) { 504 // Setting "resolving_list_max_size" to 0 is done during cleanup, 505 // hence we ignore the "readable" flag already set to false during shutdown. 506 if (resolving_list_max_size != 0) { 507 CHECK(readable); 508 } 509 CHECK(ble_supported); 510 ble_resolving_list_max_size = resolving_list_max_size; 511 } 512 513 static uint8_t get_le_all_initiating_phys() { 514 uint8_t phy = PHY_LE_1M; 515 // TODO(jpawlowski): uncomment after next FW udpate 516 // if (supports_ble_2m_phy()) phy |= PHY_LE_2M; 517 // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED; 518 return phy; 519 } 520 521 static const controller_t interface = { 522 get_is_ready, 523 524 get_address, 525 get_bt_version, 526 527 get_features_classic, 528 get_last_features_classic_index, 529 530 get_features_ble, 531 get_ble_supported_states, 532 533 supports_simple_pairing, 534 supports_secure_connections, 535 supports_simultaneous_le_bredr, 536 supports_reading_remote_extended_features, 537 supports_interlaced_inquiry_scan, 538 supports_rssi_with_inquiry_results, 539 supports_extended_inquiry_response, 540 supports_master_slave_role_switch, 541 supports_enhanced_setup_synchronous_connection, 542 supports_enhanced_accept_synchronous_connection, 543 544 supports_ble, 545 supports_ble_packet_extension, 546 supports_ble_connection_parameters_request, 547 supports_ble_privacy, 548 supports_ble_set_privacy_mode, 549 supports_ble_2m_phy, 550 supports_ble_coded_phy, 551 supports_ble_extended_advertising, 552 supports_ble_periodic_advertising, 553 554 get_acl_data_size_classic, 555 get_acl_data_size_ble, 556 557 get_acl_packet_size_classic, 558 get_acl_packet_size_ble, 559 get_ble_suggested_default_data_length, 560 get_ble_maximum_tx_data_length, 561 get_ble_maxium_advertising_data_length, 562 get_ble_number_of_supported_advertising_sets, 563 564 get_acl_buffer_count_classic, 565 get_acl_buffer_count_ble, 566 567 get_ble_white_list_size, 568 569 get_ble_resolving_list_max_size, 570 set_ble_resolving_list_max_size, 571 get_local_supported_codecs, 572 get_le_all_initiating_phys}; 573 574 const controller_t* controller_get_interface() { 575 static bool loaded = false; 576 if (!loaded) { 577 loaded = true; 578 579 hci = hci_layer_get_interface(); 580 packet_factory = hci_packet_factory_get_interface(); 581 packet_parser = hci_packet_parser_get_interface(); 582 } 583 584 return &interface; 585 } 586 587 const controller_t* controller_get_test_interface( 588 const hci_t* hci_interface, 589 const hci_packet_factory_t* packet_factory_interface, 590 const hci_packet_parser_t* packet_parser_interface) { 591 hci = hci_interface; 592 packet_factory = packet_factory_interface; 593 packet_parser = packet_parser_interface; 594 return &interface; 595 } 596