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
48 #define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8
50 static const hci_t *hci;
51 static const hci_packet_factory_t *packet_factory;
52 static const hci_packet_parser_t *packet_parser;
54 static bt_bdaddr_t address;
55 static bt_version_t bt_version;
57 static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
58 static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
59 static uint8_t last_features_classic_page_index;
61 static uint16_t acl_data_size_classic;
62 static uint16_t acl_data_size_ble;
63 static uint16_t acl_buffer_count_classic;
64 static uint8_t acl_buffer_count_ble;
66 static uint8_t ble_white_list_size;
67 static uint8_t ble_resolving_list_max_size;
68 static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
69 static bt_device_features_t features_ble;
70 static uint16_t ble_suggested_default_data_length;
71 static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE];
72 static uint8_t number_of_local_supported_codecs = 0;
75 static bool ble_supported;
76 static bool simple_pairing_supported;
77 static bool secure_connections_supported;
79 #define AWAIT_COMMAND(command) future_await(hci->transmit_command_futured(command))
81 // Module lifecycle functions
83 static future_t *start_up(void) {
86 // Send the initial reset command
87 response = AWAIT_COMMAND(packet_factory->make_reset());
88 packet_parser->parse_generic_command_complete(response);
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);
95 // Tell the controller about our buffer sizes and buffer counts next
96 // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just a hardcoded 10?
97 response = AWAIT_COMMAND(
98 packet_factory->make_host_buffer_size(
100 SCO_HOST_BUFFER_SIZE,
101 L2CAP_HOST_FC_ACL_BUFS,
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
118 response = AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
119 packet_parser->parse_read_local_supported_commands_response(
122 HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
125 // Read page 0 of the controller features next
126 uint8_t page_number = 0;
127 response = AWAIT_COMMAND(packet_factory->make_read_local_extended_features(page_number));
128 packet_parser->parse_read_local_extended_features_response(
131 &last_features_classic_page_index,
133 MAX_FEATURES_CLASSIC_PAGE_COUNT
136 assert(page_number == 0);
139 // Inform the controller what page 0 features we support, based on what
140 // it told us it supports. We need to do this first before we request the
141 // next page, because the controller's response for page 1 may be
142 // dependent on what we configure from page 0
143 simple_pairing_supported = HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
144 if (simple_pairing_supported) {
145 response = AWAIT_COMMAND(packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
146 packet_parser->parse_generic_command_complete(response);
149 #if (BLE_INCLUDED == TRUE)
150 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
151 uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
152 response = AWAIT_COMMAND(
153 packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host)
156 packet_parser->parse_generic_command_complete(response);
158 // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1
159 if (last_features_classic_page_index < 1)
160 last_features_classic_page_index = 1;
164 // Done telling the controller about what page 0 features we support
165 // Request the remaining feature pages
166 while (page_number <= last_features_classic_page_index &&
167 page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
168 response = AWAIT_COMMAND(packet_factory->make_read_local_extended_features(page_number));
169 packet_parser->parse_read_local_extended_features_response(
172 &last_features_classic_page_index,
174 MAX_FEATURES_CLASSIC_PAGE_COUNT
180 #if (SC_MODE_INCLUDED == TRUE)
181 secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
182 if (secure_connections_supported) {
183 response = AWAIT_COMMAND(packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
184 packet_parser->parse_generic_command_complete(response);
188 #if (BLE_INCLUDED == TRUE)
189 ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
191 // Request the ble white list size next
192 response = AWAIT_COMMAND(packet_factory->make_ble_read_white_list_size());
193 packet_parser->parse_ble_read_white_list_size_response(response, &ble_white_list_size);
195 // Request the ble buffer size next
196 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
197 packet_parser->parse_ble_read_buffer_size_response(
200 &acl_buffer_count_ble
203 // Response of 0 indicates ble has the same buffer size as classic
204 if (acl_data_size_ble == 0)
205 acl_data_size_ble = acl_data_size_classic;
207 // Request the ble supported states next
208 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
209 packet_parser->parse_ble_read_supported_states_response(
211 ble_supported_states,
212 sizeof(ble_supported_states)
215 // Request the ble supported features next
216 response = AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
217 packet_parser->parse_ble_read_local_supported_features_response(
222 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
223 response = AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
224 packet_parser->parse_ble_read_resolving_list_size_response(
226 &ble_resolving_list_max_size);
229 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
230 response = AWAIT_COMMAND(packet_factory->make_ble_read_suggested_default_data_length());
231 packet_parser->parse_ble_read_suggested_default_data_length_response(
233 &ble_suggested_default_data_length);
236 // Set the ble event mask next
237 response = AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
238 packet_parser->parse_generic_command_complete(response);
242 if (simple_pairing_supported) {
243 response = AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
244 packet_parser->parse_generic_command_complete(response);
247 // read local supported codecs
248 if(HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
249 response = AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs());
250 packet_parser->parse_read_local_supported_codecs_response(
252 &number_of_local_supported_codecs, local_supported_codecs);
256 return future_new_immediate(FUTURE_SUCCESS);
259 static future_t *shut_down(void) {
261 return future_new_immediate(FUTURE_SUCCESS);
264 EXPORT_SYMBOL const module_t controller_module = {
265 .name = CONTROLLER_MODULE,
267 .start_up = start_up,
268 .shut_down = shut_down,
276 // Interface functions
278 static bool get_is_ready(void) {
282 static const bt_bdaddr_t *get_address(void) {
287 static const bt_version_t *get_bt_version(void) {
292 // TODO(zachoverflow): hide inside, move decoder inside too
293 static const bt_device_features_t *get_features_classic(int index) {
295 assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
296 return &features_classic[index];
299 static uint8_t get_last_features_classic_index(void) {
301 return last_features_classic_page_index;
304 static uint8_t *get_local_supported_codecs(uint8_t *number_of_codecs) {
306 if(number_of_local_supported_codecs) {
307 *number_of_codecs = number_of_local_supported_codecs;
308 return local_supported_codecs;
313 static const bt_device_features_t *get_features_ble(void) {
315 assert(ble_supported);
316 return &features_ble;
319 static const uint8_t *get_ble_supported_states(void) {
321 assert(ble_supported);
322 return ble_supported_states;
325 static bool supports_simple_pairing(void) {
327 return simple_pairing_supported;
330 static bool supports_secure_connections(void) {
332 return secure_connections_supported;
335 static bool supports_simultaneous_le_bredr(void) {
337 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
340 static bool supports_reading_remote_extended_features(void) {
342 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
345 static bool supports_interlaced_inquiry_scan(void) {
347 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
350 static bool supports_rssi_with_inquiry_results(void) {
352 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
355 static bool supports_extended_inquiry_response(void) {
357 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
360 static bool supports_master_slave_role_switch(void) {
362 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
365 static bool supports_ble(void) {
367 return ble_supported;
370 static bool supports_ble_privacy(void) {
372 assert(ble_supported);
373 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
376 static bool supports_ble_packet_extension(void) {
378 assert(ble_supported);
379 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
382 static bool supports_ble_connection_parameters_request(void) {
384 assert(ble_supported);
385 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
388 static uint16_t get_acl_data_size_classic(void) {
390 return acl_data_size_classic;
393 static uint16_t get_acl_data_size_ble(void) {
395 assert(ble_supported);
396 return acl_data_size_ble;
399 static uint16_t get_acl_packet_size_classic(void) {
401 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
404 static uint16_t get_acl_packet_size_ble(void) {
406 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
409 static uint16_t get_ble_suggested_default_data_length(void) {
411 assert(ble_supported);
412 return ble_suggested_default_data_length;
415 static uint16_t get_acl_buffer_count_classic(void) {
417 return acl_buffer_count_classic;
420 static uint8_t get_acl_buffer_count_ble(void) {
422 assert(ble_supported);
423 return acl_buffer_count_ble;
426 static uint8_t get_ble_white_list_size(void) {
428 assert(ble_supported);
429 return ble_white_list_size;
432 static uint8_t get_ble_resolving_list_max_size(void) {
434 assert(ble_supported);
435 return ble_resolving_list_max_size;
438 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
440 assert(ble_supported);
441 ble_resolving_list_max_size = resolving_list_max_size;
444 static const controller_t interface = {
450 get_features_classic,
451 get_last_features_classic_index,
454 get_ble_supported_states,
456 supports_simple_pairing,
457 supports_secure_connections,
458 supports_simultaneous_le_bredr,
459 supports_reading_remote_extended_features,
460 supports_interlaced_inquiry_scan,
461 supports_rssi_with_inquiry_results,
462 supports_extended_inquiry_response,
463 supports_master_slave_role_switch,
466 supports_ble_packet_extension,
467 supports_ble_connection_parameters_request,
468 supports_ble_privacy,
470 get_acl_data_size_classic,
471 get_acl_data_size_ble,
473 get_acl_packet_size_classic,
474 get_acl_packet_size_ble,
475 get_ble_suggested_default_data_length,
477 get_acl_buffer_count_classic,
478 get_acl_buffer_count_ble,
480 get_ble_white_list_size,
482 get_ble_resolving_list_max_size,
483 set_ble_resolving_list_max_size,
484 get_local_supported_codecs
487 const controller_t *controller_get_interface() {
488 static bool loaded = false;
492 hci = hci_layer_get_interface();
493 packet_factory = hci_packet_factory_get_interface();
494 packet_parser = hci_packet_parser_get_interface();
500 const controller_t *controller_get_test_interface(
501 const hci_t *hci_interface,
502 const hci_packet_factory_t *packet_factory_interface,
503 const hci_packet_parser_t *packet_parser_interface) {
506 packet_factory = packet_factory_interface;
507 packet_parser = packet_parser_interface;