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 <assert.h>
22 #include <stdbool.h>
23
24 #include "btcore/include/bdaddr.h"
25 #include "bt_types.h"
26 #include "device/include/controller.h"
27 #include "btcore/include/event_mask.h"
28 #include "osi/include/future.h"
29 #include "hcimsgs.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"
36
37 const bt_event_mask_t BLE_EVENT_MASK = { "\x00\x00\x00\x00\x00\x00\x06\x7f" };
38
39 #if (BLE_INCLUDED)
40 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_DUMO_EVENT_MASK_EXT };
41 #else
42 const bt_event_mask_t CLASSIC_EVENT_MASK = { HCI_LISBON_EVENT_MASK_EXT };
43 #endif
44
45 // TODO(zachoverflow): factor out into common module
46 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
47
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
52
53 static const hci_t *hci;
54 static const hci_packet_factory_t *packet_factory;
55 static const hci_packet_parser_t *packet_parser;
56
57 static bt_bdaddr_t address;
58 static bt_version_t bt_version;
59
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;
63
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;
68
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;
74
75 static bool readable;
76 static bool ble_supported;
77 static bool simple_pairing_supported;
78 static bool secure_connections_supported;
79
80 #define AWAIT_COMMAND(command) future_await(hci->transmit_command_futured(command))
81
82 // Module lifecycle functions
83
start_up(void)84 static future_t *start_up(void) {
85 BT_HDR *response;
86
87 // Send the initial reset command
88 response = AWAIT_COMMAND(packet_factory->make_reset());
89 packet_parser->parse_generic_command_complete(response);
90
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);
95
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(
100 L2CAP_MTU_SIZE,
101 SCO_HOST_BUFFER_SIZE,
102 L2CAP_HOST_FC_ACL_BUFS,
103 10
104 )
105 );
106
107 packet_parser->parse_generic_command_complete(response);
108
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);
113
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);
117
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(
121 response,
122 supported_commands,
123 HCI_SUPPORTED_COMMANDS_ARRAY_SIZE
124 );
125
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(
130 response,
131 &page_number,
132 &last_features_classic_page_index,
133 features_classic,
134 MAX_FEATURES_CLASSIC_PAGE_COUNT
135 );
136
137 assert(page_number == 0);
138 page_number++;
139
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);
148 }
149
150 #if (BLE_INCLUDED == TRUE)
151 if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
152 uint8_t simultaneous_le_host = HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array) ? BTM_BLE_SIMULTANEOUS_HOST : 0;
153 response = AWAIT_COMMAND(
154 packet_factory->make_ble_write_host_support(BTM_BLE_HOST_SUPPORT, simultaneous_le_host)
155 );
156
157 packet_parser->parse_generic_command_complete(response);
158 }
159 #endif
160
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(packet_factory->make_read_local_extended_features(page_number));
166 packet_parser->parse_read_local_extended_features_response(
167 response,
168 &page_number,
169 &last_features_classic_page_index,
170 features_classic,
171 MAX_FEATURES_CLASSIC_PAGE_COUNT
172 );
173
174 page_number++;
175 }
176
177 #if (SC_MODE_INCLUDED == TRUE)
178 secure_connections_supported = HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
179 if (secure_connections_supported) {
180 response = AWAIT_COMMAND(packet_factory->make_write_secure_connections_host_support(HCI_SC_MODE_ENABLED));
181 packet_parser->parse_generic_command_complete(response);
182 }
183 #endif
184
185 #if (BLE_INCLUDED == TRUE)
186 ble_supported = last_features_classic_page_index >= 1 && HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
187 if (ble_supported) {
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(response, &ble_white_list_size);
191
192 // Request the ble buffer size next
193 response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
194 packet_parser->parse_ble_read_buffer_size_response(
195 response,
196 &acl_data_size_ble,
197 &acl_buffer_count_ble
198 );
199
200 // Response of 0 indicates ble has the same buffer size as classic
201 if (acl_data_size_ble == 0)
202 acl_data_size_ble = acl_data_size_classic;
203
204 // Request the ble supported states next
205 response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
206 packet_parser->parse_ble_read_supported_states_response(
207 response,
208 ble_supported_states,
209 sizeof(ble_supported_states)
210 );
211
212 // Request the ble supported features next
213 response = AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
214 packet_parser->parse_ble_read_local_supported_features_response(
215 response,
216 &features_ble
217 );
218
219 if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
220 response = AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
221 packet_parser->parse_ble_read_resolving_list_size_response(
222 response,
223 &ble_resolving_list_max_size);
224 }
225
226 if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
227 response = AWAIT_COMMAND(packet_factory->make_ble_read_suggested_default_data_length());
228 packet_parser->parse_ble_read_suggested_default_data_length_response(
229 response,
230 &ble_suggested_default_data_length);
231 }
232
233 // Set the ble event mask next
234 response = AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
235 packet_parser->parse_generic_command_complete(response);
236 }
237 #endif
238
239 if (simple_pairing_supported) {
240 response = AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
241 packet_parser->parse_generic_command_complete(response);
242 }
243
244 readable = true;
245 return future_new_immediate(FUTURE_SUCCESS);
246 }
247
shut_down(void)248 static future_t *shut_down(void) {
249 readable = false;
250 return future_new_immediate(FUTURE_SUCCESS);
251 }
252
253 const module_t controller_module = {
254 .name = CONTROLLER_MODULE,
255 .init = NULL,
256 .start_up = start_up,
257 .shut_down = shut_down,
258 .clean_up = NULL,
259 .dependencies = {
260 HCI_MODULE,
261 NULL
262 }
263 };
264
265 // Interface functions
266
get_is_ready(void)267 static bool get_is_ready(void) {
268 return readable;
269 }
270
get_address(void)271 static const bt_bdaddr_t *get_address(void) {
272 assert(readable);
273 return &address;
274 }
275
get_bt_version(void)276 static const bt_version_t *get_bt_version(void) {
277 assert(readable);
278 return &bt_version;
279 }
280
281 // TODO(zachoverflow): hide inside, move decoder inside too
get_features_classic(int index)282 static const bt_device_features_t *get_features_classic(int index) {
283 assert(readable);
284 assert(index < MAX_FEATURES_CLASSIC_PAGE_COUNT);
285 return &features_classic[index];
286 }
287
get_last_features_classic_index(void)288 static uint8_t get_last_features_classic_index(void) {
289 assert(readable);
290 return last_features_classic_page_index;
291 }
292
get_features_ble(void)293 static const bt_device_features_t *get_features_ble(void) {
294 assert(readable);
295 assert(ble_supported);
296 return &features_ble;
297 }
298
get_ble_supported_states(void)299 static const uint8_t *get_ble_supported_states(void) {
300 assert(readable);
301 assert(ble_supported);
302 return ble_supported_states;
303 }
304
supports_simple_pairing(void)305 static bool supports_simple_pairing(void) {
306 assert(readable);
307 return simple_pairing_supported;
308 }
309
supports_secure_connections(void)310 static bool supports_secure_connections(void) {
311 assert(readable);
312 return secure_connections_supported;
313 }
314
supports_simultaneous_le_bredr(void)315 static bool supports_simultaneous_le_bredr(void) {
316 assert(readable);
317 return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
318 }
319
supports_reading_remote_extended_features(void)320 static bool supports_reading_remote_extended_features(void) {
321 assert(readable);
322 return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
323 }
324
supports_interlaced_inquiry_scan(void)325 static bool supports_interlaced_inquiry_scan(void) {
326 assert(readable);
327 return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
328 }
329
supports_rssi_with_inquiry_results(void)330 static bool supports_rssi_with_inquiry_results(void) {
331 assert(readable);
332 return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
333 }
334
supports_extended_inquiry_response(void)335 static bool supports_extended_inquiry_response(void) {
336 assert(readable);
337 return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
338 }
339
supports_master_slave_role_switch(void)340 static bool supports_master_slave_role_switch(void) {
341 assert(readable);
342 return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
343 }
344
supports_ble(void)345 static bool supports_ble(void) {
346 assert(readable);
347 return ble_supported;
348 }
349
supports_ble_privacy(void)350 static bool supports_ble_privacy(void) {
351 assert(readable);
352 assert(ble_supported);
353 return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
354 }
355
supports_ble_packet_extension(void)356 static bool supports_ble_packet_extension(void) {
357 assert(readable);
358 assert(ble_supported);
359 return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
360 }
361
supports_ble_connection_parameters_request(void)362 static bool supports_ble_connection_parameters_request(void) {
363 assert(readable);
364 assert(ble_supported);
365 return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
366 }
367
get_acl_data_size_classic(void)368 static uint16_t get_acl_data_size_classic(void) {
369 assert(readable);
370 return acl_data_size_classic;
371 }
372
get_acl_data_size_ble(void)373 static uint16_t get_acl_data_size_ble(void) {
374 assert(readable);
375 assert(ble_supported);
376 return acl_data_size_ble;
377 }
378
get_acl_packet_size_classic(void)379 static uint16_t get_acl_packet_size_classic(void) {
380 assert(readable);
381 return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
382 }
383
get_acl_packet_size_ble(void)384 static uint16_t get_acl_packet_size_ble(void) {
385 assert(readable);
386 return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
387 }
388
get_ble_suggested_default_data_length(void)389 static uint16_t get_ble_suggested_default_data_length(void) {
390 assert(readable);
391 assert(ble_supported);
392 return ble_suggested_default_data_length;
393 }
394
get_acl_buffer_count_classic(void)395 static uint16_t get_acl_buffer_count_classic(void) {
396 assert(readable);
397 return acl_buffer_count_classic;
398 }
399
get_acl_buffer_count_ble(void)400 static uint8_t get_acl_buffer_count_ble(void) {
401 assert(readable);
402 assert(ble_supported);
403 return acl_buffer_count_ble;
404 }
405
get_ble_white_list_size(void)406 static uint8_t get_ble_white_list_size(void) {
407 assert(readable);
408 assert(ble_supported);
409 return ble_white_list_size;
410 }
411
get_ble_resolving_list_max_size(void)412 static uint8_t get_ble_resolving_list_max_size(void) {
413 assert(readable);
414 assert(ble_supported);
415 return ble_resolving_list_max_size;
416 }
417
set_ble_resolving_list_max_size(int resolving_list_max_size)418 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
419 assert(readable);
420 assert(ble_supported);
421 ble_resolving_list_max_size = resolving_list_max_size;
422 }
423
424 static const controller_t interface = {
425 get_is_ready,
426
427 get_address,
428 get_bt_version,
429
430 get_features_classic,
431 get_last_features_classic_index,
432
433 get_features_ble,
434 get_ble_supported_states,
435
436 supports_simple_pairing,
437 supports_secure_connections,
438 supports_simultaneous_le_bredr,
439 supports_reading_remote_extended_features,
440 supports_interlaced_inquiry_scan,
441 supports_rssi_with_inquiry_results,
442 supports_extended_inquiry_response,
443 supports_master_slave_role_switch,
444
445 supports_ble,
446 supports_ble_packet_extension,
447 supports_ble_connection_parameters_request,
448 supports_ble_privacy,
449
450 get_acl_data_size_classic,
451 get_acl_data_size_ble,
452
453 get_acl_packet_size_classic,
454 get_acl_packet_size_ble,
455 get_ble_suggested_default_data_length,
456
457 get_acl_buffer_count_classic,
458 get_acl_buffer_count_ble,
459
460 get_ble_white_list_size,
461
462 get_ble_resolving_list_max_size,
463 set_ble_resolving_list_max_size
464 };
465
controller_get_interface()466 const controller_t *controller_get_interface() {
467 static bool loaded = false;
468 if (!loaded) {
469 loaded = true;
470
471 hci = hci_layer_get_interface();
472 packet_factory = hci_packet_factory_get_interface();
473 packet_parser = hci_packet_parser_get_interface();
474 }
475
476 return &interface;
477 }
478
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)479 const controller_t *controller_get_test_interface(
480 const hci_t *hci_interface,
481 const hci_packet_factory_t *packet_factory_interface,
482 const hci_packet_parser_t *packet_parser_interface) {
483
484 hci = hci_interface;
485 packet_factory = packet_factory_interface;
486 packet_parser = packet_parser_interface;
487 return &interface;
488 }
489