1 /******************************************************************************
3 * Copyright (C) 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"
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 = { "\x00\x00\x00\x00\x00\x00\x06\x7f" };
36 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
38 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
41 // TODO(zachoverflow): factor out into common module
42 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
44 #define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
45 #define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
46 #define BLE_SUPPORTED_STATES_SIZE 8
47 #define BLE_SUPPORTED_FEATURES_SIZE 8
49 static const hci_t *hci;
50 static const hci_packet_factory_t *packet_factory;
51 static const hci_packet_parser_t *packet_parser;
53 static bt_bdaddr_t address;
54 static bt_version_t bt_version;
56 static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
57 static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
58 static uint8_t last_features_classic_page_index;
60 static uint16_t acl_data_size_classic;
61 static uint16_t acl_data_size_ble;
62 static uint16_t acl_buffer_count_classic;
63 static uint8_t acl_buffer_count_ble;
65 static uint8_t ble_white_list_size;
66 static uint8_t ble_resolving_list_max_size;
67 static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
68 static bt_device_features_t features_ble;
69 static uint16_t ble_suggested_default_data_length;
72 static bool ble_supported;
73 static bool simple_pairing_supported;
74 static bool secure_connections_supported;
76 #define AWAIT_COMMAND(command) future_await(hci->transmit_command_futured(command))
78 // Module lifecycle functions
80 static future_t *start_up(void) {
83 // Send the initial reset command
84 response = AWAIT_COMMAND(packet_factory->make_reset());
85 packet_parser->parse_generic_command_complete(response);
87 // Request the classic buffer size next
88 response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
89 packet_parser->parse_read_buffer_size_response(
90 response, &acl_data_size_classic, &acl_buffer_count_classic);
92 // Tell the controller about our buffer sizes and buffer counts next
93 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just a hardcoded 10?
94 response = AWAIT_COMMAND(
95 packet_factory->make_host_buffer_size(
98 L2CAP_HOST_FC_ACL_BUFS,
103 packet_parser->parse_generic_command_complete(response);
105 // Read the local version info off the controller next, including
106 // information such as manufacturer and supported HCI version
107 response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
108 packet_parser->parse_read_local_version_info_response(response, &bt_version);
110 // Read the bluetooth address off the controller next
111 response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
112 packet_parser->parse_read_bd_addr_response(response, &address);
114 // Request the controller's supported commands next
115 response = AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
116 packet_parser->parse_read_local_supported_commands_response(
119 HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
122 // Read page 0 of the controller features next
123 uint8_t page_number = 0;
124 response = AWAIT_COMMAND(packet_factory->make_read_local_extended_features(page_number));
125 packet_parser->parse_read_local_extended_features_response(
128 &last_features_classic_page_index,
130 MAX_FEATURES_CLASSIC_PAGE_COUNT
133 assert(page_number == 0);
136 // Inform the controller what page 0 features we support, based on what
137 // it told us it supports. We need to do this first before we request the
138 // next page, because the controller's response for page 1 may be
139 // dependent on what we configure from page 0
140 simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
141 if (simple_pairing_supported) {
142 response = AWAIT_COMMAND(packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
143 packet_parser->parse_generic_command_complete(response);
146 #if (BLE_INCLUDED == TRUE)
147 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
148 uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
149 response = AWAIT_COMMAND(
150 packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host)
153 packet_parser->parse_generic_command_complete(response);
157 // Done telling the controller about what page 0 features we support
158 // Request the remaining feature pages
159 while (page_number <= last_features_classic_page_index &&
160 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
161 response = AWAIT_COMMAND(packet_factory->make_read_local_extended_features(page_number));
162 packet_parser->parse_read_local_extended_features_response(
165 &last_features_classic_page_index,
167 MAX_FEATURES_CLASSIC_PAGE_COUNT
173 #if (SC_MODE_INCLUDED == TRUE)
174 secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
175 if (secure_connections_supported) {
176 response = AWAIT_COMMAND(packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
177 packet_parser->parse_generic_command_complete(response);
181 #if (BLE_INCLUDED == TRUE)
182 ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
184 // Request the ble white list size next
185 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
186 packet_parser->parse_ble_read_white_list_size_response(response, &ble_white_list_size);
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(
193 &acl_buffer_count_ble
196 // Response of 0 indicates ble has the same buffer size as classic
197 if (acl_data_size_ble == 0)
198 acl_data_size_ble = acl_data_size_classic;
200 // Request the ble supported states next
201 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
202 packet_parser->parse_ble_read_supported_states_response(
204 ble_supported_states,
205 sizeof(ble_supported_states)
208 // Request the ble supported features next
209 response = AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
210 packet_parser->parse_ble_read_local_supported_features_response(
215 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
216 response = AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
217 packet_parser->parse_ble_read_resolving_list_size_response(
219 &ble_resolving_list_max_size);
222 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
223 response = AWAIT_COMMAND(packet_factory->make_ble_read_suggested_default_data_length());
224 packet_parser->parse_ble_read_suggested_default_data_length_response(
226 &ble_suggested_default_data_length);
229 // Set the ble event mask next
230 response = AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
231 packet_parser->parse_generic_command_complete(response);
235 if (simple_pairing_supported) {
236 response = AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
237 packet_parser->parse_generic_command_complete(response);
241 return future_new_immediate(FUTURE_SUCCESS);
244 static future_t *shut_down(void) {
246 return future_new_immediate(FUTURE_SUCCESS);
249 EXPORT_SYMBOL const module_t controller_module = {
250 .name = CONTROLLER_MODULE,
252 .start_up = start_up,
253 .shut_down = shut_down,
261 // Interface functions
263 static bool get_is_ready(void) {
267 static const bt_bdaddr_t *get_address(void) {
272 static const bt_version_t *get_bt_version(void) {
277 // TODO(zachoverflow): hide inside, move decoder inside too
278 static const bt_device_features_t *get_features_classic(int index) {
280 assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
281 return &features_classic[index];
284 static uint8_t get_last_features_classic_index(void) {
286 return last_features_classic_page_index;
289 static const bt_device_features_t *get_features_ble(void) {
291 assert(ble_supported);
292 return &features_ble;
295 static const uint8_t *get_ble_supported_states(void) {
297 assert(ble_supported);
298 return ble_supported_states;
301 static bool supports_simple_pairing(void) {
303 return simple_pairing_supported;
306 static bool supports_secure_connections(void) {
308 return secure_connections_supported;
311 static bool supports_simultaneous_le_bredr(void) {
313 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
316 static bool supports_reading_remote_extended_features(void) {
318 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
321 static bool supports_interlaced_inquiry_scan(void) {
323 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
326 static bool supports_rssi_with_inquiry_results(void) {
328 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
331 static bool supports_extended_inquiry_response(void) {
333 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
336 static bool supports_master_slave_role_switch(void) {
338 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
341 static bool supports_ble(void) {
343 return ble_supported;
346 static bool supports_ble_privacy(void) {
348 assert(ble_supported);
349 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
352 static bool supports_ble_packet_extension(void) {
354 assert(ble_supported);
355 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
358 static bool supports_ble_connection_parameters_request(void) {
360 assert(ble_supported);
361 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
364 static uint16_t get_acl_data_size_classic(void) {
366 return acl_data_size_classic;
369 static uint16_t get_acl_data_size_ble(void) {
371 assert(ble_supported);
372 return acl_data_size_ble;
375 static uint16_t get_acl_packet_size_classic(void) {
377 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
380 static uint16_t get_acl_packet_size_ble(void) {
382 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
385 static uint16_t get_ble_suggested_default_data_length(void) {
387 assert(ble_supported);
388 return ble_suggested_default_data_length;
391 static uint16_t get_acl_buffer_count_classic(void) {
393 return acl_buffer_count_classic;
396 static uint8_t get_acl_buffer_count_ble(void) {
398 assert(ble_supported);
399 return acl_buffer_count_ble;
402 static uint8_t get_ble_white_list_size(void) {
404 assert(ble_supported);
405 return ble_white_list_size;
408 static uint8_t get_ble_resolving_list_max_size(void) {
410 assert(ble_supported);
411 return ble_resolving_list_max_size;
414 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
416 assert(ble_supported);
417 ble_resolving_list_max_size = resolving_list_max_size;
420 static const controller_t interface = {
426 get_features_classic,
427 get_last_features_classic_index,
430 get_ble_supported_states,
432 supports_simple_pairing,
433 supports_secure_connections,
434 supports_simultaneous_le_bredr,
435 supports_reading_remote_extended_features,
436 supports_interlaced_inquiry_scan,
437 supports_rssi_with_inquiry_results,
438 supports_extended_inquiry_response,
439 supports_master_slave_role_switch,
442 supports_ble_packet_extension,
443 supports_ble_connection_parameters_request,
444 supports_ble_privacy,
446 get_acl_data_size_classic,
447 get_acl_data_size_ble,
449 get_acl_packet_size_classic,
450 get_acl_packet_size_ble,
451 get_ble_suggested_default_data_length,
453 get_acl_buffer_count_classic,
454 get_acl_buffer_count_ble,
456 get_ble_white_list_size,
458 get_ble_resolving_list_max_size,
459 set_ble_resolving_list_max_size
462 const controller_t *controller_get_interface() {
463 static bool loaded = false;
467 hci = hci_layer_get_interface();
468 packet_factory = hci_packet_factory_get_interface();
469 packet_parser = hci_packet_parser_get_interface();
475 const controller_t *controller_get_test_interface(
476 const hci_t *hci_interface,
477 const hci_packet_factory_t *packet_factory_interface,
478 const hci_packet_parser_t *packet_parser_interface) {
481 packet_factory = packet_factory_interface;
482 packet_parser = packet_parser_interface;