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"
24 #include "btcore/include/bdaddr.h"
26 #include "device/include/controller.h"
27 #include "btcore/include/event_mask.h"
28 #include "osi/include/future.h"
30 #include "hci/include/hci_layer.h"
31 #include "hci/include/hci_packet_factory.h"
32 #include "hci/include/hci_packet_parser.h"
33 #include "btcore/include/module.h"
34 #include "stack/include/btm_ble_api.h"
35 #include "btcore/include/version.h"
37 const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\x00\x04\x7f" };
40 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
42 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
45 // TODO(zachoverflow): factor out into common module
46 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
48 #define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
49 #define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
50 #define BLE_SUPPORTED_STATES_SIZE 8
51 #define BLE_SUPPORTED_FEATURES_SIZE 8
53 static const hci_t *hci;
54 static const hci_packet_factory_t *packet_factory;
55 static const hci_packet_parser_t *packet_parser;
57 static bt_bdaddr_t address;
58 static bt_version_t bt_version;
60 static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
61 static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
62 static uint8_t last_features_classic_page_index;
64 static uint16_t acl_data_size_classic;
65 static uint16_t acl_data_size_ble;
66 static uint16_t acl_buffer_count_classic;
67 static uint8_t acl_buffer_count_ble;
69 static uint8_t ble_white_list_size;
70 static uint8_t ble_resolving_list_max_size;
71 static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
72 static bt_device_features_t features_ble;
73 static uint16_t ble_suggested_default_data_length;
76 static bool ble_supported;
77 static bool simple_pairing_supported;
78 static bool secure_connections_supported;
80 #define AWAIT_COMMAND(command) future_await(hci->transmit_command_futured(command))
82 // Module lifecycle functions
84 static future_t *start_up(void) {
87 // Send the initial reset command
88 response = AWAIT_COMMAND(packet_factory->make_reset());
89 packet_parser->parse_generic_command_complete(response);
91 // Request the classic buffer size next
92 response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
93 packet_parser->parse_read_buffer_size_response(
94 response, &acl_data_size_classic, &acl_buffer_count_classic);
96 // Tell the controller about our buffer sizes and buffer counts next
97 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just a hardcoded 10?
98 response = AWAIT_COMMAND(
99 packet_factory->make_host_buffer_size(
101 SCO_HOST_BUFFER_SIZE,
102 L2CAP_HOST_FC_ACL_BUFS,
107 packet_parser->parse_generic_command_complete(response);
109 // Read the local version info off the controller next, including
110 // information such as manufacturer and supported HCI version
111 response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
112 packet_parser->parse_read_local_version_info_response(response, &bt_version);
114 // Read the bluetooth address off the controller next
115 response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
116 packet_parser->parse_read_bd_addr_response(response, &address);
118 // Request the controller's supported commands next
119 response = AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
120 packet_parser->parse_read_local_supported_commands_response(
123 HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
126 // Read page 0 of the controller features next
127 uint8_t page_number = 0;
128 response = AWAIT_COMMAND(packet_factory->make_read_local_extended_features(page_number));
129 packet_parser->parse_read_local_extended_features_response(
132 &last_features_classic_page_index,
134 MAX_FEATURES_CLASSIC_PAGE_COUNT
137 assert(page_number == 0);
140 // Inform the controller what page 0 features we support, based on what
141 // it told us it supports. We need to do this first before we request the
142 // next page, because the controller's response for page 1 may be
143 // dependent on what we configure from page 0
144 simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
145 if (simple_pairing_supported) {
146 response = AWAIT_COMMAND(packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
147 packet_parser->parse_generic_command_complete(response);
150 secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
151 if (secure_connections_supported) {
152 response = AWAIT_COMMAND(packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
153 packet_parser->parse_generic_command_complete(response);
156 #if (BLE_INCLUDED == TRUE)
157 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
158 uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
159 response = AWAIT_COMMAND(
160 packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host)
163 packet_parser->parse_generic_command_complete(response);
167 // Done telling the controller about what page 0 features we support
168 // Request the remaining feature pages
169 while (page_number <= last_features_classic_page_index &&
170 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
171 response = AWAIT_COMMAND(packet_factory->make_read_local_extended_features(page_number));
172 packet_parser->parse_read_local_extended_features_response(
175 &last_features_classic_page_index,
177 MAX_FEATURES_CLASSIC_PAGE_COUNT
183 secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
184 if (secure_connections_supported) {
185 response = AWAIT_COMMAND(packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
186 packet_parser->parse_generic_command_complete(response);
189 #if (BLE_INCLUDED == TRUE)
190 ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
192 // Request the ble white list size next
193 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
194 packet_parser->parse_ble_read_white_list_size_response(response, &ble_white_list_size);
196 // Request the ble buffer size next
197 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
198 packet_parser->parse_ble_read_buffer_size_response(
201 &acl_buffer_count_ble
204 // Response of 0 indicates ble has the same buffer size as classic
205 if (acl_data_size_ble == 0)
206 acl_data_size_ble = acl_data_size_classic;
208 // Request the ble supported states next
209 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
210 packet_parser->parse_ble_read_supported_states_response(
212 ble_supported_states,
213 sizeof(ble_supported_states)
216 // Request the ble supported features next
217 response = AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
218 packet_parser->parse_ble_read_local_supported_features_response(
223 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
224 response = AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
225 packet_parser->parse_ble_read_resolving_list_size_response(
227 &ble_resolving_list_max_size);
230 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
231 response = AWAIT_COMMAND(packet_factory->make_ble_read_suggested_default_data_length());
232 packet_parser->parse_ble_read_suggested_default_data_length_response(
234 &ble_suggested_default_data_length);
237 // Set the ble event mask next
238 response = AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
239 packet_parser->parse_generic_command_complete(response);
243 if (simple_pairing_supported) {
244 response = AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
245 packet_parser->parse_generic_command_complete(response);
249 return future_new_immediate(FUTURE_SUCCESS);
252 static future_t *shut_down(void) {
254 return future_new_immediate(FUTURE_SUCCESS);
257 const module_t controller_module = {
258 .name = CONTROLLER_MODULE,
260 .start_up = start_up,
261 .shut_down = shut_down,
269 // Interface functions
271 static bool get_is_ready(void) {
275 static const bt_bdaddr_t *get_address(void) {
280 static const bt_version_t *get_bt_version(void) {
285 // TODO(zachoverflow): hide inside, move decoder inside too
286 static const bt_device_features_t *get_features_classic(int index) {
288 assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
289 return &features_classic[index];
292 static uint8_t get_last_features_classic_index(void) {
294 return last_features_classic_page_index;
297 static const bt_device_features_t *get_features_ble(void) {
299 assert(ble_supported);
300 return &features_ble;
303 static const uint8_t *get_ble_supported_states(void) {
305 assert(ble_supported);
306 return ble_supported_states;
309 static bool supports_simple_pairing(void) {
311 return simple_pairing_supported;
314 static bool supports_secure_connections(void) {
316 return secure_connections_supported;
319 static bool supports_simultaneous_le_bredr(void) {
321 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
324 static bool supports_reading_remote_extended_features(void) {
326 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
329 static bool supports_interlaced_inquiry_scan(void) {
331 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
334 static bool supports_rssi_with_inquiry_results(void) {
336 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
339 static bool supports_extended_inquiry_response(void) {
341 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
344 static bool supports_master_slave_role_switch(void) {
346 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
349 static bool supports_ble(void) {
351 return ble_supported;
354 static bool supports_ble_privacy(void) {
356 assert(ble_supported);
357 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
360 static bool supports_ble_packet_extension(void) {
362 assert(ble_supported);
363 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
366 static bool supports_ble_connection_parameters_request(void) {
368 assert(ble_supported);
369 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
372 static uint16_t get_acl_data_size_classic(void) {
374 return acl_data_size_classic;
377 static uint16_t get_acl_data_size_ble(void) {
379 assert(ble_supported);
380 return acl_data_size_ble;
383 static uint16_t get_acl_packet_size_classic(void) {
385 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
388 static uint16_t get_acl_packet_size_ble(void) {
390 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
393 static uint16_t get_ble_suggested_default_data_length(void) {
395 assert(ble_supported);
396 return ble_suggested_default_data_length;
399 static uint16_t get_acl_buffer_count_classic(void) {
401 return acl_buffer_count_classic;
404 static uint8_t get_acl_buffer_count_ble(void) {
406 assert(ble_supported);
407 return acl_buffer_count_ble;
410 static uint8_t get_ble_white_list_size(void) {
412 assert(ble_supported);
413 return ble_white_list_size;
416 static uint8_t get_ble_resolving_list_max_size(void) {
418 assert(ble_supported);
419 return ble_resolving_list_max_size;
422 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
424 assert(ble_supported);
425 ble_resolving_list_max_size = resolving_list_max_size;
428 static const controller_t interface = {
434 get_features_classic,
435 get_last_features_classic_index,
438 get_ble_supported_states,
440 supports_simple_pairing,
441 supports_secure_connections,
442 supports_simultaneous_le_bredr,
443 supports_reading_remote_extended_features,
444 supports_interlaced_inquiry_scan,
445 supports_rssi_with_inquiry_results,
446 supports_extended_inquiry_response,
447 supports_master_slave_role_switch,
450 supports_ble_packet_extension,
451 supports_ble_connection_parameters_request,
452 supports_ble_privacy,
454 get_acl_data_size_classic,
455 get_acl_data_size_ble,
457 get_acl_packet_size_classic,
458 get_acl_packet_size_ble,
459 get_ble_suggested_default_data_length,
461 get_acl_buffer_count_classic,
462 get_acl_buffer_count_ble,
464 get_ble_white_list_size,
466 get_ble_resolving_list_max_size,
467 set_ble_resolving_list_max_size
470 const controller_t *controller_get_interface() {
471 static bool loaded = false;
475 hci = hci_layer_get_interface();
476 packet_factory = hci_packet_factory_get_interface();
477 packet_parser = hci_packet_parser_get_interface();
483 const controller_t *controller_get_test_interface(
484 const hci_t *hci_interface,
485 const hci_packet_factory_t *packet_factory_interface,
486 const hci_packet_parser_t *packet_parser_interface) {
489 packet_factory = packet_factory_interface;
490 packet_parser = packet_parser_interface;