1 /******************************************************************************
3 * Copyright 2014 Google, Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 #define LOG_TAG "bt_controller"
21 #include "device/include/controller.h"
23 #include <base/logging.h>
26 #include "btcore/include/event_mask.h"
27 #include "btcore/include/module.h"
28 #include "btcore/include/version.h"
30 #include "osi/include/future.h"
31 #include "stack/include/btm_ble_api.h"
33 const bt_event_mask_t BLE_EVENT_MASK = {
34 {0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x1E, 0x7f}};
36 const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT};
38 // TODO(zachoverflow): factor out into common module
39 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
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
47 static const hci_t* hci;
48 static const hci_packet_factory_t* packet_factory;
49 static const hci_packet_parser_t* packet_parser;
51 static RawAddress address;
52 static bt_version_t bt_version;
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;
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;
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;
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;
79 static bool ble_supported;
80 static bool simple_pairing_supported;
81 static bool secure_connections_supported;
83 #define AWAIT_COMMAND(command) \
84 static_cast<BT_HDR*>(future_await(hci->transmit_command_futured(command)))
86 // Module lifecycle functions
88 static future_t* start_up(void) {
91 // Send the initial reset command
92 response = AWAIT_COMMAND(packet_factory->make_reset());
93 packet_parser->parse_generic_command_complete(response);
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);
100 // Tell the controller about our buffer sizes and buffer counts next
101 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just
103 response = AWAIT_COMMAND(packet_factory->make_host_buffer_size(
104 L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10));
106 packet_parser->parse_generic_command_complete(response);
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);
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);
117 // Request the controller's supported commands next
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);
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);
131 CHECK(page_number == 0);
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);
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
151 response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support(
152 BTM_BLE_HOST_SUPPORT, simultaneous_le_host));
154 packet_parser->parse_generic_command_complete(response);
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;
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);
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);
185 ble_supported = last_features_classic_page_index >= 1 &&
186 HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
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);
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);
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;
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));
206 // Request the ble supported features next
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);
212 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
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);
219 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
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);
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);
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);
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);
243 /* If LE Excended Advertising is not supported, use the default value */
244 ble_maxium_advertising_data_length = 31;
247 // Set the ble event mask next
249 AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
250 packet_parser->parse_generic_command_complete(response);
253 if (simple_pairing_supported) {
255 AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
256 packet_parser->parse_generic_command_complete(response);
259 // read local supported codecs
260 if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
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);
267 if (!HCI_READ_ENCR_KEY_SIZE_SUPPORTED(supported_commands)) {
268 LOG(FATAL) << " Controller must support Read Encryption Key Size command";
272 return future_new_immediate(FUTURE_SUCCESS);
275 static future_t* shut_down(void) {
277 return future_new_immediate(FUTURE_SUCCESS);
280 EXPORT_SYMBOL extern const module_t controller_module = {
281 .name = CONTROLLER_MODULE,
283 .start_up = start_up,
284 .shut_down = shut_down,
286 .dependencies = {HCI_MODULE, NULL}};
288 // Interface functions
290 static bool get_is_ready(void) { return readable; }
292 static const RawAddress* get_address(void) {
297 static const bt_version_t* get_bt_version(void) {
302 // TODO(zachoverflow): hide inside, move decoder inside too
303 static const bt_device_features_t* get_features_classic(int index) {
305 CHECK(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
306 return &features_classic[index];
309 static uint8_t get_last_features_classic_index(void) {
311 return last_features_classic_page_index;
314 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
316 if (number_of_local_supported_codecs) {
317 *number_of_codecs = number_of_local_supported_codecs;
318 return local_supported_codecs;
323 static const bt_device_features_t* get_features_ble(void) {
325 CHECK(ble_supported);
326 return &features_ble;
329 static const uint8_t* get_ble_supported_states(void) {
331 CHECK(ble_supported);
332 return ble_supported_states;
335 static bool supports_simple_pairing(void) {
337 return simple_pairing_supported;
340 static bool supports_secure_connections(void) {
342 return secure_connections_supported;
345 static bool supports_simultaneous_le_bredr(void) {
347 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
350 static bool supports_reading_remote_extended_features(void) {
352 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
355 static bool supports_interlaced_inquiry_scan(void) {
357 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
360 static bool supports_rssi_with_inquiry_results(void) {
362 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
365 static bool supports_extended_inquiry_response(void) {
367 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
370 static bool supports_master_slave_role_switch(void) {
372 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
375 static bool supports_enhanced_setup_synchronous_connection(void) {
377 return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands);
380 static bool supports_enhanced_accept_synchronous_connection(void) {
382 return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands);
385 static bool supports_ble(void) {
387 return ble_supported;
390 static bool supports_ble_privacy(void) {
392 CHECK(ble_supported);
393 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
396 static bool supports_ble_set_privacy_mode() {
398 CHECK(ble_supported);
399 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array) &&
400 HCI_LE_SET_PRIVACY_MODE_SUPPORTED(supported_commands);
403 static bool supports_ble_packet_extension(void) {
405 CHECK(ble_supported);
406 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
409 static bool supports_ble_connection_parameters_request(void) {
411 CHECK(ble_supported);
412 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
415 static bool supports_ble_2m_phy(void) {
417 CHECK(ble_supported);
418 return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array);
421 static bool supports_ble_coded_phy(void) {
423 CHECK(ble_supported);
424 return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array);
427 static bool supports_ble_extended_advertising(void) {
429 CHECK(ble_supported);
430 return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array);
433 static bool supports_ble_periodic_advertising(void) {
435 CHECK(ble_supported);
436 return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array);
439 static uint16_t get_acl_data_size_classic(void) {
441 return acl_data_size_classic;
444 static uint16_t get_acl_data_size_ble(void) {
446 CHECK(ble_supported);
447 return acl_data_size_ble;
450 static uint16_t get_acl_packet_size_classic(void) {
452 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
455 static uint16_t get_acl_packet_size_ble(void) {
457 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
460 static uint16_t get_ble_suggested_default_data_length(void) {
462 CHECK(ble_supported);
463 return ble_suggested_default_data_length;
466 static uint16_t get_ble_maximum_tx_data_length(void) {
468 CHECK(ble_supported);
469 return ble_supported_max_tx_octets;
472 static uint16_t get_ble_maxium_advertising_data_length(void) {
474 CHECK(ble_supported);
475 return ble_maxium_advertising_data_length;
478 static uint8_t get_ble_number_of_supported_advertising_sets(void) {
480 CHECK(ble_supported);
481 return ble_number_of_supported_advertising_sets;
484 static uint16_t get_acl_buffer_count_classic(void) {
486 return acl_buffer_count_classic;
489 static uint8_t get_acl_buffer_count_ble(void) {
491 CHECK(ble_supported);
492 return acl_buffer_count_ble;
495 static uint8_t get_ble_white_list_size(void) {
497 CHECK(ble_supported);
498 return ble_white_list_size;
501 static uint8_t get_ble_resolving_list_max_size(void) {
503 CHECK(ble_supported);
504 return ble_resolving_list_max_size;
507 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
508 // Setting "resolving_list_max_size" to 0 is done during cleanup,
509 // hence we ignore the "readable" flag already set to false during shutdown.
510 if (resolving_list_max_size != 0) {
513 CHECK(ble_supported);
514 ble_resolving_list_max_size = resolving_list_max_size;
517 static uint8_t get_le_all_initiating_phys() {
518 uint8_t phy = PHY_LE_1M;
519 // TODO(jpawlowski): uncomment after next FW udpate
520 // if (supports_ble_2m_phy()) phy |= PHY_LE_2M;
521 // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED;
525 static const controller_t interface = {
531 get_features_classic,
532 get_last_features_classic_index,
535 get_ble_supported_states,
537 supports_simple_pairing,
538 supports_secure_connections,
539 supports_simultaneous_le_bredr,
540 supports_reading_remote_extended_features,
541 supports_interlaced_inquiry_scan,
542 supports_rssi_with_inquiry_results,
543 supports_extended_inquiry_response,
544 supports_master_slave_role_switch,
545 supports_enhanced_setup_synchronous_connection,
546 supports_enhanced_accept_synchronous_connection,
549 supports_ble_packet_extension,
550 supports_ble_connection_parameters_request,
551 supports_ble_privacy,
552 supports_ble_set_privacy_mode,
554 supports_ble_coded_phy,
555 supports_ble_extended_advertising,
556 supports_ble_periodic_advertising,
558 get_acl_data_size_classic,
559 get_acl_data_size_ble,
561 get_acl_packet_size_classic,
562 get_acl_packet_size_ble,
563 get_ble_suggested_default_data_length,
564 get_ble_maximum_tx_data_length,
565 get_ble_maxium_advertising_data_length,
566 get_ble_number_of_supported_advertising_sets,
568 get_acl_buffer_count_classic,
569 get_acl_buffer_count_ble,
571 get_ble_white_list_size,
573 get_ble_resolving_list_max_size,
574 set_ble_resolving_list_max_size,
575 get_local_supported_codecs,
576 get_le_all_initiating_phys};
578 const controller_t* controller_get_interface() {
579 static bool loaded = false;
583 hci = hci_layer_get_interface();
584 packet_factory = hci_packet_factory_get_interface();
585 packet_parser = hci_packet_parser_get_interface();
591 const controller_t* controller_get_test_interface(
592 const hci_t* hci_interface,
593 const hci_packet_factory_t* packet_factory_interface,
594 const hci_packet_parser_t* packet_parser_interface) {
596 packet_factory = packet_factory_interface;
597 packet_parser = packet_parser_interface;