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 <assert.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 = { "\x00\x00\x00\x00\x00\x00\x06\x7f" };
34
35 #if (BLE_INCLUDED)
36 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
37 #else
38 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
39 #endif
40
41 // TODO(zachoverflow): factor out into common module
42 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
43
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
49
50 static const hci_t *hci;
51 static const hci_packet_factory_t *packet_factory;
52 static const hci_packet_parser_t *packet_parser;
53
54 static bt_bdaddr_t address;
55 static bt_version_t bt_version;
56
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;
60
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;
65
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;
73
74 static bool readable;
75 static bool ble_supported;
76 static bool simple_pairing_supported;
77 static bool secure_connections_supported;
78
79 #define AWAIT_COMMAND(command) future_await(hci->transmit_command_futured(command))
80
81 // Module lifecycle functions
82
start_up(void)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 a hardcoded 10?
97 response = AWAIT_COMMAND(
98 packet_factory->make_host_buffer_size(
99 L2CAP_MTU_SIZE,
100 SCO_HOST_BUFFER_SIZE,
101 L2CAP_HOST_FC_ACL_BUFS,
102 10
103 )
104 );
105
106 packet_parser->parse_generic_command_complete(response);
107
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);
112
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);
116
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(
120 response,
121 supported_commands,
122 HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
123 );
124
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(
129 response,
130 &page_number,
131 &last_features_classic_page_index,
132 features_classic,
133 MAX_FEATURES_CLASSIC_PAGE_COUNT
134 );
135
136 assert(page_number == 0);
137 page_number++;
138
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);
147 }
148
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)
154 );
155
156 packet_parser->parse_generic_command_complete(response);
157
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;
161 }
162 #endif
163
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(
170 response,
171 &page_number,
172 &last_features_classic_page_index,
173 features_classic,
174 MAX_FEATURES_CLASSIC_PAGE_COUNT
175 );
176
177 page_number++;
178 }
179
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);
185 }
186 #endif
187
188 #if (BLE_INCLUDED == TRUE)
189 ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
190 if (ble_supported) {
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);
194
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(
198 response,
199 &acl_data_size_ble,
200 &acl_buffer_count_ble
201 );
202
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;
206
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(
210 response,
211 ble_supported_states,
212 sizeof(ble_supported_states)
213 );
214
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(
218 response,
219 &features_ble
220 );
221
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(
225 response,
226 &ble_resolving_list_max_size);
227 }
228
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(
232 response,
233 &ble_suggested_default_data_length);
234 }
235
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);
239 }
240 #endif
241
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);
245 }
246
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(
251 response,
252 &number_of_local_supported_codecs, local_supported_codecs);
253 }
254
255 readable = true;
256 return future_new_immediate(FUTURE_SUCCESS);
257 }
258
shut_down(void)259 static future_t *shut_down(void) {
260 readable = false;
261 return future_new_immediate(FUTURE_SUCCESS);
262 }
263
264 EXPORT_SYMBOL const module_t controller_module = {
265 .name = CONTROLLER_MODULE,
266 .init = NULL,
267 .start_up = start_up,
268 .shut_down = shut_down,
269 .clean_up = NULL,
270 .dependencies = {
271 HCI_MODULE,
272 NULL
273 }
274 };
275
276 // Interface functions
277
get_is_ready(void)278 static bool get_is_ready(void) {
279 return readable;
280 }
281
get_address(void)282 static const bt_bdaddr_t *get_address(void) {
283 assert(readable);
284 return &address;
285 }
286
get_bt_version(void)287 static const bt_version_t *get_bt_version(void) {
288 assert(readable);
289 return &bt_version;
290 }
291
292 // TODO(zachoverflow): hide inside, move decoder inside too
get_features_classic(int index)293 static const bt_device_features_t *get_features_classic(int index) {
294 assert(readable);
295 assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
296 return &features_classic[index];
297 }
298
get_last_features_classic_index(void)299 static uint8_t get_last_features_classic_index(void) {
300 assert(readable);
301 return last_features_classic_page_index;
302 }
303
get_local_supported_codecs(uint8_t * number_of_codecs)304 static uint8_t *get_local_supported_codecs(uint8_t *number_of_codecs) {
305 assert(readable);
306 if(number_of_local_supported_codecs) {
307 *number_of_codecs = number_of_local_supported_codecs;
308 return local_supported_codecs;
309 }
310 return NULL;
311 }
312
get_features_ble(void)313 static const bt_device_features_t *get_features_ble(void) {
314 assert(readable);
315 assert(ble_supported);
316 return &features_ble;
317 }
318
get_ble_supported_states(void)319 static const uint8_t *get_ble_supported_states(void) {
320 assert(readable);
321 assert(ble_supported);
322 return ble_supported_states;
323 }
324
supports_simple_pairing(void)325 static bool supports_simple_pairing(void) {
326 assert(readable);
327 return simple_pairing_supported;
328 }
329
supports_secure_connections(void)330 static bool supports_secure_connections(void) {
331 assert(readable);
332 return secure_connections_supported;
333 }
334
supports_simultaneous_le_bredr(void)335 static bool supports_simultaneous_le_bredr(void) {
336 assert(readable);
337 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
338 }
339
supports_reading_remote_extended_features(void)340 static bool supports_reading_remote_extended_features(void) {
341 assert(readable);
342 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
343 }
344
supports_interlaced_inquiry_scan(void)345 static bool supports_interlaced_inquiry_scan(void) {
346 assert(readable);
347 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
348 }
349
supports_rssi_with_inquiry_results(void)350 static bool supports_rssi_with_inquiry_results(void) {
351 assert(readable);
352 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
353 }
354
supports_extended_inquiry_response(void)355 static bool supports_extended_inquiry_response(void) {
356 assert(readable);
357 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
358 }
359
supports_master_slave_role_switch(void)360 static bool supports_master_slave_role_switch(void) {
361 assert(readable);
362 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
363 }
364
supports_ble(void)365 static bool supports_ble(void) {
366 assert(readable);
367 return ble_supported;
368 }
369
supports_ble_privacy(void)370 static bool supports_ble_privacy(void) {
371 assert(readable);
372 assert(ble_supported);
373 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
374 }
375
supports_ble_packet_extension(void)376 static bool supports_ble_packet_extension(void) {
377 assert(readable);
378 assert(ble_supported);
379 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
380 }
381
supports_ble_connection_parameters_request(void)382 static bool supports_ble_connection_parameters_request(void) {
383 assert(readable);
384 assert(ble_supported);
385 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
386 }
387
get_acl_data_size_classic(void)388 static uint16_t get_acl_data_size_classic(void) {
389 assert(readable);
390 return acl_data_size_classic;
391 }
392
get_acl_data_size_ble(void)393 static uint16_t get_acl_data_size_ble(void) {
394 assert(readable);
395 assert(ble_supported);
396 return acl_data_size_ble;
397 }
398
get_acl_packet_size_classic(void)399 static uint16_t get_acl_packet_size_classic(void) {
400 assert(readable);
401 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
402 }
403
get_acl_packet_size_ble(void)404 static uint16_t get_acl_packet_size_ble(void) {
405 assert(readable);
406 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
407 }
408
get_ble_suggested_default_data_length(void)409 static uint16_t get_ble_suggested_default_data_length(void) {
410 assert(readable);
411 assert(ble_supported);
412 return ble_suggested_default_data_length;
413 }
414
get_acl_buffer_count_classic(void)415 static uint16_t get_acl_buffer_count_classic(void) {
416 assert(readable);
417 return acl_buffer_count_classic;
418 }
419
get_acl_buffer_count_ble(void)420 static uint8_t get_acl_buffer_count_ble(void) {
421 assert(readable);
422 assert(ble_supported);
423 return acl_buffer_count_ble;
424 }
425
get_ble_white_list_size(void)426 static uint8_t get_ble_white_list_size(void) {
427 assert(readable);
428 assert(ble_supported);
429 return ble_white_list_size;
430 }
431
get_ble_resolving_list_max_size(void)432 static uint8_t get_ble_resolving_list_max_size(void) {
433 assert(readable);
434 assert(ble_supported);
435 return ble_resolving_list_max_size;
436 }
437
set_ble_resolving_list_max_size(int resolving_list_max_size)438 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
439 // Setting "resolving_list_max_size" to 0 is done during cleanup,
440 // hence we ignore the "readable" flag already set to false during shutdown.
441 if (resolving_list_max_size != 0) {
442 assert(readable);
443 }
444 assert(ble_supported);
445 ble_resolving_list_max_size = resolving_list_max_size;
446 }
447
448 static const controller_t interface = {
449 get_is_ready,
450
451 get_address,
452 get_bt_version,
453
454 get_features_classic,
455 get_last_features_classic_index,
456
457 get_features_ble,
458 get_ble_supported_states,
459
460 supports_simple_pairing,
461 supports_secure_connections,
462 supports_simultaneous_le_bredr,
463 supports_reading_remote_extended_features,
464 supports_interlaced_inquiry_scan,
465 supports_rssi_with_inquiry_results,
466 supports_extended_inquiry_response,
467 supports_master_slave_role_switch,
468
469 supports_ble,
470 supports_ble_packet_extension,
471 supports_ble_connection_parameters_request,
472 supports_ble_privacy,
473
474 get_acl_data_size_classic,
475 get_acl_data_size_ble,
476
477 get_acl_packet_size_classic,
478 get_acl_packet_size_ble,
479 get_ble_suggested_default_data_length,
480
481 get_acl_buffer_count_classic,
482 get_acl_buffer_count_ble,
483
484 get_ble_white_list_size,
485
486 get_ble_resolving_list_max_size,
487 set_ble_resolving_list_max_size,
488 get_local_supported_codecs
489 };
490
controller_get_interface()491 const controller_t *controller_get_interface() {
492 static bool loaded = false;
493 if (!loaded) {
494 loaded = true;
495
496 hci = hci_layer_get_interface();
497 packet_factory = hci_packet_factory_get_interface();
498 packet_parser = hci_packet_parser_get_interface();
499 }
500
501 return &interface;
502 }
503
controller_get_test_interface(const hci_t * hci_interface,const hci_packet_factory_t * packet_factory_interface,const hci_packet_parser_t * packet_parser_interface)504 const controller_t *controller_get_test_interface(
505 const hci_t *hci_interface,
506 const hci_packet_factory_t *packet_factory_interface,
507 const hci_packet_parser_t *packet_parser_interface) {
508
509 hci = hci_interface;
510 packet_factory = packet_factory_interface;
511 packet_parser = packet_parser_interface;
512 return &interface;
513 }
514