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