• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 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 <base/logging.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 "main/shim/controller.h"
31 #include "main/shim/shim.h"
32 #include "osi/include/future.h"
33 #include "osi/include/properties.h"
34 #include "stack/include/btm_ble_api.h"
35 
36 const bt_event_mask_t BLE_EVENT_MASK = {{0x00, 0x00, 0x00, 0x00, 0x7F, 0x02,
37 #if (BLE_PRIVACY_SPT == TRUE)
38                                          0xFE,
39 #else
40                                          /* Disable "LE Enhanced Connection
41                                             Complete" when privacy is off */
42                                          0xFC,
43 #endif
44                                          0x7f}};
45 
46 const bt_event_mask_t CLASSIC_EVENT_MASK = {HCI_DUMO_EVENT_MASK_EXT};
47 
48 // TODO(zachoverflow): factor out into common module
49 const uint8_t SCO_HOST_BUFFER_SIZE = 0xff;
50 
51 #define HCI_SUPPORTED_COMMANDS_ARRAY_SIZE 64
52 #define MAX_FEATURES_CLASSIC_PAGE_COUNT 3
53 #define BLE_SUPPORTED_STATES_SIZE 8
54 #define BLE_SUPPORTED_FEATURES_SIZE 8
55 #define MAX_LOCAL_SUPPORTED_CODECS_SIZE 8
56 #define LL_FEATURE_BIT_ISO_HOST_SUPPORT 32
57 
58 static const hci_t* local_hci;
59 static const hci_packet_factory_t* packet_factory;
60 static const hci_packet_parser_t* packet_parser;
61 
62 static RawAddress address;
63 static bt_version_t bt_version;
64 
65 static uint8_t supported_commands[HCI_SUPPORTED_COMMANDS_ARRAY_SIZE];
66 static bt_device_features_t features_classic[MAX_FEATURES_CLASSIC_PAGE_COUNT];
67 static uint8_t last_features_classic_page_index;
68 
69 static uint16_t acl_data_size_classic;
70 static uint16_t acl_data_size_ble;
71 static uint16_t iso_data_size;
72 
73 static uint16_t acl_buffer_count_classic;
74 static uint8_t acl_buffer_count_ble;
75 static uint8_t iso_buffer_count;
76 
77 static uint8_t ble_acceptlist_size;
78 static uint8_t ble_resolving_list_max_size;
79 static uint8_t ble_supported_states[BLE_SUPPORTED_STATES_SIZE];
80 static bt_device_features_t features_ble;
81 static uint16_t ble_suggested_default_data_length;
82 static uint16_t ble_supported_max_tx_octets;
83 static uint16_t ble_supported_max_tx_time;
84 static uint16_t ble_supported_max_rx_octets;
85 static uint16_t ble_supported_max_rx_time;
86 
87 static uint16_t ble_maxium_advertising_data_length;
88 static uint8_t ble_number_of_supported_advertising_sets;
89 static uint8_t ble_periodic_advertiser_list_size;
90 static uint8_t local_supported_codecs[MAX_LOCAL_SUPPORTED_CODECS_SIZE];
91 static uint8_t number_of_local_supported_codecs = 0;
92 
93 static bool readable;
94 static bool ble_supported;
95 static bool iso_supported;
96 static bool simple_pairing_supported;
97 static bool secure_connections_supported;
98 
99 #define AWAIT_COMMAND(command) \
100   static_cast<BT_HDR*>(        \
101       future_await(local_hci->transmit_command_futured(command)))
102 
103 // Module lifecycle functions
104 
start_up(void)105 static future_t* start_up(void) {
106   BT_HDR* response;
107 
108   // Send the initial reset command
109   response = AWAIT_COMMAND(packet_factory->make_reset());
110   packet_parser->parse_generic_command_complete(response);
111 
112   // Request the classic buffer size next
113   response = AWAIT_COMMAND(packet_factory->make_read_buffer_size());
114   packet_parser->parse_read_buffer_size_response(
115       response, &acl_data_size_classic, &acl_buffer_count_classic);
116 
117   // Tell the controller about our buffer sizes and buffer counts next
118   // TODO(zachoverflow): factor this out. eww l2cap contamination. And why just
119   // a hardcoded 10?
120   response = AWAIT_COMMAND(packet_factory->make_host_buffer_size(
121       L2CAP_MTU_SIZE, SCO_HOST_BUFFER_SIZE, L2CAP_HOST_FC_ACL_BUFS, 10));
122 
123   packet_parser->parse_generic_command_complete(response);
124 
125   // Read the local version info off the controller next, including
126   // information such as manufacturer and supported HCI version
127   response = AWAIT_COMMAND(packet_factory->make_read_local_version_info());
128   packet_parser->parse_read_local_version_info_response(response, &bt_version);
129 
130   // Read the bluetooth address off the controller next
131   response = AWAIT_COMMAND(packet_factory->make_read_bd_addr());
132   packet_parser->parse_read_bd_addr_response(response, &address);
133 
134   // Request the controller's supported commands next
135   response =
136       AWAIT_COMMAND(packet_factory->make_read_local_supported_commands());
137   packet_parser->parse_read_local_supported_commands_response(
138       response, supported_commands, HCI_SUPPORTED_COMMANDS_ARRAY_SIZE);
139 
140   // Read page 0 of the controller features next
141   uint8_t page_number = 0;
142   response = AWAIT_COMMAND(
143       packet_factory->make_read_local_extended_features(page_number));
144   packet_parser->parse_read_local_extended_features_response(
145       response, &page_number, &last_features_classic_page_index,
146       features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
147 
148   CHECK(page_number == 0);
149   page_number++;
150 
151   // Inform the controller what page 0 features we support, based on what
152   // it told us it supports. We need to do this first before we request the
153   // next page, because the controller's response for page 1 may be
154   // dependent on what we configure from page 0
155   simple_pairing_supported =
156       HCI_SIMPLE_PAIRING_SUPPORTED(features_classic[0].as_array);
157   if (simple_pairing_supported) {
158     response = AWAIT_COMMAND(
159         packet_factory->make_write_simple_pairing_mode(HCI_SP_MODE_ENABLED));
160     packet_parser->parse_generic_command_complete(response);
161   }
162 
163   if (HCI_LE_SPT_SUPPORTED(features_classic[0].as_array)) {
164     uint8_t simultaneous_le_host =
165         HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array)
166             ? BTM_BLE_SIMULTANEOUS_HOST
167             : 0;
168     response = AWAIT_COMMAND(packet_factory->make_ble_write_host_support(
169         BTM_BLE_HOST_SUPPORT, simultaneous_le_host));
170 
171     packet_parser->parse_generic_command_complete(response);
172 
173     // If we modified the BT_HOST_SUPPORT, we will need ext. feat. page 1
174     if (last_features_classic_page_index < 1)
175       last_features_classic_page_index = 1;
176   }
177 
178   // Done telling the controller about what page 0 features we support
179   // Request the remaining feature pages
180   while (page_number <= last_features_classic_page_index &&
181          page_number < MAX_FEATURES_CLASSIC_PAGE_COUNT) {
182     response = AWAIT_COMMAND(
183         packet_factory->make_read_local_extended_features(page_number));
184     packet_parser->parse_read_local_extended_features_response(
185         response, &page_number, &last_features_classic_page_index,
186         features_classic, MAX_FEATURES_CLASSIC_PAGE_COUNT);
187 
188     page_number++;
189   }
190 
191 #if (SC_MODE_INCLUDED == TRUE)
192   secure_connections_supported =
193       HCI_SC_CTRLR_SUPPORTED(features_classic[2].as_array);
194   if (secure_connections_supported) {
195     response = AWAIT_COMMAND(
196         packet_factory->make_write_secure_connections_host_support(
197             HCI_SC_MODE_ENABLED));
198     packet_parser->parse_generic_command_complete(response);
199   }
200 #endif
201 
202   ble_supported = last_features_classic_page_index >= 1 &&
203                   HCI_LE_HOST_SUPPORTED(features_classic[1].as_array);
204   if (ble_supported) {
205     // Request the ble acceptlist size next
206     response = AWAIT_COMMAND(packet_factory->make_ble_read_acceptlist_size());
207     packet_parser->parse_ble_read_acceptlist_size_response(
208         response, &ble_acceptlist_size);
209 
210     // Request the ble supported features next
211     response =
212         AWAIT_COMMAND(packet_factory->make_ble_read_local_supported_features());
213     packet_parser->parse_ble_read_local_supported_features_response(
214         response, &features_ble);
215 
216     iso_supported = HCI_LE_CIS_CENTRAL(features_ble.as_array) ||
217                     HCI_LE_CIS_PERIPHERAL(features_ble.as_array) ||
218                     HCI_LE_ISO_BROADCASTER(features_ble.as_array);
219 
220     if (iso_supported) {
221       // Request the ble buffer size next
222       response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size_v2());
223       packet_parser->parse_ble_read_buffer_size_v2_response(
224           response, &acl_data_size_ble, &acl_buffer_count_ble, &iso_data_size,
225           &iso_buffer_count);
226 
227     } else {
228       // Request the ble buffer size next
229       response = AWAIT_COMMAND(packet_factory->make_ble_read_buffer_size());
230       packet_parser->parse_ble_read_buffer_size_response(
231           response, &acl_data_size_ble, &acl_buffer_count_ble);
232     }
233 
234     // Response of 0 indicates ble has the same buffer size as classic
235     if (acl_data_size_ble == 0) acl_data_size_ble = acl_data_size_classic;
236 
237     // Request the ble supported states next
238     response = AWAIT_COMMAND(packet_factory->make_ble_read_supported_states());
239     packet_parser->parse_ble_read_supported_states_response(
240         response, ble_supported_states, sizeof(ble_supported_states));
241 
242     if (HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array)) {
243       response =
244           AWAIT_COMMAND(packet_factory->make_ble_read_resolving_list_size());
245       packet_parser->parse_ble_read_resolving_list_size_response(
246           response, &ble_resolving_list_max_size);
247     }
248 
249     if (HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array)) {
250       response =
251           AWAIT_COMMAND(packet_factory->make_ble_read_maximum_data_length());
252       packet_parser->parse_ble_read_maximum_data_length_response(
253           response, &ble_supported_max_tx_octets, &ble_supported_max_tx_time,
254           &ble_supported_max_rx_octets, &ble_supported_max_rx_time);
255 
256       response = AWAIT_COMMAND(
257           packet_factory->make_ble_read_suggested_default_data_length());
258       packet_parser->parse_ble_read_suggested_default_data_length_response(
259           response, &ble_suggested_default_data_length);
260     }
261 
262     if (HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array)) {
263       response = AWAIT_COMMAND(
264           packet_factory->make_ble_read_maximum_advertising_data_length());
265       packet_parser->parse_ble_read_maximum_advertising_data_length(
266           response, &ble_maxium_advertising_data_length);
267 
268       response = AWAIT_COMMAND(
269           packet_factory->make_ble_read_number_of_supported_advertising_sets());
270       packet_parser->parse_ble_read_number_of_supported_advertising_sets(
271           response, &ble_number_of_supported_advertising_sets);
272     } else {
273       /* If LE Excended Advertising is not supported, use the default value */
274       ble_maxium_advertising_data_length = 31;
275     }
276 
277     if (HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array)) {
278       response = AWAIT_COMMAND(
279           packet_factory->make_ble_read_periodic_advertiser_list_size());
280 
281       packet_parser->parse_ble_read_size_of_advertiser_list(
282           response, &ble_periodic_advertiser_list_size);
283     }
284 
285     // Set the ble event mask next
286     response =
287         AWAIT_COMMAND(packet_factory->make_ble_set_event_mask(&BLE_EVENT_MASK));
288     packet_parser->parse_generic_command_complete(response);
289 
290     if (HCI_LE_SET_HOST_FEATURE_SUPPORTED(supported_commands)) {
291       response = AWAIT_COMMAND(packet_factory->make_ble_set_host_features(
292           LL_FEATURE_BIT_ISO_HOST_SUPPORT, 0x01));
293       packet_parser->parse_generic_command_complete(response);
294     }
295   }
296 
297   if (simple_pairing_supported) {
298     response =
299         AWAIT_COMMAND(packet_factory->make_set_event_mask(&CLASSIC_EVENT_MASK));
300     packet_parser->parse_generic_command_complete(response);
301   }
302 
303   // read local supported codecs
304   if (HCI_READ_LOCAL_CODECS_SUPPORTED(supported_commands)) {
305     response =
306         AWAIT_COMMAND(packet_factory->make_read_local_supported_codecs());
307     packet_parser->parse_read_local_supported_codecs_response(
308         response, &number_of_local_supported_codecs, local_supported_codecs);
309   }
310 
311   if (!HCI_READ_ENCR_KEY_SIZE_SUPPORTED(supported_commands)) {
312     LOG(FATAL) << " Controller must support Read Encryption Key Size command";
313   }
314 
315   readable = true;
316   return future_new_immediate(FUTURE_SUCCESS);
317 }
318 
shut_down(void)319 static future_t* shut_down(void) {
320   readable = false;
321   return future_new_immediate(FUTURE_SUCCESS);
322 }
323 
324 EXPORT_SYMBOL extern const module_t controller_module = {
325     .name = CONTROLLER_MODULE,
326     .init = NULL,
327     .start_up = start_up,
328     .shut_down = shut_down,
329     .clean_up = NULL,
330     .dependencies = {HCI_MODULE, NULL}};
331 
332 // Interface functions
333 
get_is_ready(void)334 static bool get_is_ready(void) { return readable; }
335 
get_address(void)336 static const RawAddress* get_address(void) {
337   CHECK(readable);
338   return &address;
339 }
340 
get_bt_version(void)341 static const bt_version_t* get_bt_version(void) {
342   CHECK(readable);
343   return &bt_version;
344 }
345 
get_local_supported_codecs(uint8_t * number_of_codecs)346 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
347   CHECK(readable);
348   if (number_of_local_supported_codecs) {
349     *number_of_codecs = number_of_local_supported_codecs;
350     return local_supported_codecs;
351   }
352   return NULL;
353 }
354 
get_ble_supported_states(void)355 static const uint8_t* get_ble_supported_states(void) {
356   CHECK(readable);
357   CHECK(ble_supported);
358   return ble_supported_states;
359 }
360 
supports_simple_pairing(void)361 static bool supports_simple_pairing(void) {
362   CHECK(readable);
363   return simple_pairing_supported;
364 }
365 
supports_secure_connections(void)366 static bool supports_secure_connections(void) {
367   CHECK(readable);
368   return secure_connections_supported;
369 }
370 
supports_simultaneous_le_bredr(void)371 static bool supports_simultaneous_le_bredr(void) {
372   CHECK(readable);
373   return HCI_SIMUL_LE_BREDR_SUPPORTED(features_classic[0].as_array);
374 }
375 
supports_reading_remote_extended_features(void)376 static bool supports_reading_remote_extended_features(void) {
377   CHECK(readable);
378   return HCI_READ_REMOTE_EXT_FEATURES_SUPPORTED(supported_commands);
379 }
380 
supports_interlaced_inquiry_scan(void)381 static bool supports_interlaced_inquiry_scan(void) {
382   CHECK(readable);
383   return HCI_LMP_INTERLACED_INQ_SCAN_SUPPORTED(features_classic[0].as_array);
384 }
385 
supports_rssi_with_inquiry_results(void)386 static bool supports_rssi_with_inquiry_results(void) {
387   CHECK(readable);
388   return HCI_LMP_INQ_RSSI_SUPPORTED(features_classic[0].as_array);
389 }
390 
supports_extended_inquiry_response(void)391 static bool supports_extended_inquiry_response(void) {
392   CHECK(readable);
393   return HCI_EXT_INQ_RSP_SUPPORTED(features_classic[0].as_array);
394 }
395 
supports_central_peripheral_role_switch(void)396 static bool supports_central_peripheral_role_switch(void) {
397   CHECK(readable);
398   return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
399 }
400 
supports_enhanced_setup_synchronous_connection(void)401 static bool supports_enhanced_setup_synchronous_connection(void) {
402   assert(readable);
403   return HCI_ENH_SETUP_SYNCH_CONN_SUPPORTED(supported_commands);
404 }
405 
supports_enhanced_accept_synchronous_connection(void)406 static bool supports_enhanced_accept_synchronous_connection(void) {
407   assert(readable);
408   return HCI_ENH_ACCEPT_SYNCH_CONN_SUPPORTED(supported_commands);
409 }
410 
supports_3_slot_packets(void)411 static bool supports_3_slot_packets(void) {
412   CHECK(readable);
413   return HCI_3_SLOT_PACKETS_SUPPORTED(features_classic[0].as_array);
414 }
415 
supports_5_slot_packets(void)416 static bool supports_5_slot_packets(void) {
417   CHECK(readable);
418   return HCI_5_SLOT_PACKETS_SUPPORTED(features_classic[0].as_array);
419 }
420 
supports_classic_2m_phy(void)421 static bool supports_classic_2m_phy(void) {
422   CHECK(readable);
423   return HCI_EDR_ACL_2MPS_SUPPORTED(features_classic[0].as_array);
424 }
425 
supports_classic_3m_phy(void)426 static bool supports_classic_3m_phy(void) {
427   CHECK(readable);
428   return HCI_EDR_ACL_3MPS_SUPPORTED(features_classic[0].as_array);
429 }
430 
supports_3_slot_edr_packets(void)431 static bool supports_3_slot_edr_packets(void) {
432   CHECK(readable);
433   return HCI_3_SLOT_EDR_ACL_SUPPORTED(features_classic[0].as_array);
434 }
435 
supports_5_slot_edr_packets(void)436 static bool supports_5_slot_edr_packets(void) {
437   CHECK(readable);
438   return HCI_5_SLOT_EDR_ACL_SUPPORTED(features_classic[0].as_array);
439 }
440 
supports_sco(void)441 static bool supports_sco(void) {
442   CHECK(readable);
443   return HCI_SCO_LINK_SUPPORTED(features_classic[0].as_array);
444 }
445 
supports_hv2_packets(void)446 static bool supports_hv2_packets(void) {
447   CHECK(readable);
448   return HCI_HV2_PACKETS_SUPPORTED(features_classic[0].as_array);
449 }
450 
supports_hv3_packets(void)451 static bool supports_hv3_packets(void) {
452   CHECK(readable);
453   return HCI_HV3_PACKETS_SUPPORTED(features_classic[0].as_array);
454 }
455 
supports_ev3_packets(void)456 static bool supports_ev3_packets(void) {
457   CHECK(readable);
458   return HCI_ESCO_EV3_SUPPORTED(features_classic[0].as_array);
459 }
460 
supports_ev4_packets(void)461 static bool supports_ev4_packets(void) {
462   CHECK(readable);
463   return HCI_ESCO_EV4_SUPPORTED(features_classic[0].as_array);
464 }
465 
supports_ev5_packets(void)466 static bool supports_ev5_packets(void) {
467   CHECK(readable);
468   return HCI_ESCO_EV5_SUPPORTED(features_classic[0].as_array);
469 }
470 
supports_esco_2m_phy(void)471 static bool supports_esco_2m_phy(void) {
472   CHECK(readable);
473   return HCI_EDR_ESCO_2MPS_SUPPORTED(features_classic[0].as_array);
474 }
475 
supports_esco_3m_phy(void)476 static bool supports_esco_3m_phy(void) {
477   CHECK(readable);
478   return HCI_EDR_ESCO_3MPS_SUPPORTED(features_classic[0].as_array);
479 }
480 
supports_3_slot_esco_edr_packets(void)481 static bool supports_3_slot_esco_edr_packets(void) {
482   CHECK(readable);
483   return HCI_3_SLOT_EDR_ESCO_SUPPORTED(features_classic[0].as_array);
484 }
485 
supports_role_switch(void)486 static bool supports_role_switch(void) {
487   CHECK(readable);
488   return HCI_SWITCH_SUPPORTED(features_classic[0].as_array);
489 }
490 
supports_hold_mode(void)491 static bool supports_hold_mode(void) {
492   CHECK(readable);
493   return HCI_HOLD_MODE_SUPPORTED(features_classic[0].as_array);
494 }
495 
supports_sniff_mode(void)496 static bool supports_sniff_mode(void) {
497   CHECK(readable);
498   return HCI_SNIFF_MODE_SUPPORTED(features_classic[0].as_array);
499 }
500 
supports_park_mode(void)501 static bool supports_park_mode(void) {
502   CHECK(readable);
503   return HCI_PARK_MODE_SUPPORTED(features_classic[0].as_array);
504 }
505 
supports_non_flushable_pb(void)506 static bool supports_non_flushable_pb(void) {
507   CHECK(readable);
508   return HCI_NON_FLUSHABLE_PB_SUPPORTED(features_classic[0].as_array);
509 }
510 
supports_sniff_subrating(void)511 static bool supports_sniff_subrating(void) {
512   CHECK(readable);
513   return HCI_SNIFF_SUB_RATE_SUPPORTED(features_classic[0].as_array);
514 }
515 
supports_encryption_pause(void)516 static bool supports_encryption_pause(void) {
517   CHECK(readable);
518   return HCI_ATOMIC_ENCRYPT_SUPPORTED(features_classic[0].as_array);
519 }
520 
supports_ble(void)521 static bool supports_ble(void) {
522   CHECK(readable);
523   return ble_supported;
524 }
525 
supports_ble_privacy(void)526 static bool supports_ble_privacy(void) {
527   CHECK(readable);
528   CHECK(ble_supported);
529   return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array);
530 }
531 
supports_ble_set_privacy_mode()532 static bool supports_ble_set_privacy_mode() {
533   CHECK(readable);
534   CHECK(ble_supported);
535   return HCI_LE_ENHANCED_PRIVACY_SUPPORTED(features_ble.as_array) &&
536          HCI_LE_SET_PRIVACY_MODE_SUPPORTED(supported_commands);
537 }
538 
supports_ble_packet_extension(void)539 static bool supports_ble_packet_extension(void) {
540   CHECK(readable);
541   CHECK(ble_supported);
542   return HCI_LE_DATA_LEN_EXT_SUPPORTED(features_ble.as_array);
543 }
544 
supports_ble_connection_parameters_request(void)545 static bool supports_ble_connection_parameters_request(void) {
546   CHECK(readable);
547   CHECK(ble_supported);
548   return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
549 }
550 
supports_ble_2m_phy(void)551 static bool supports_ble_2m_phy(void) {
552   CHECK(readable);
553   CHECK(ble_supported);
554   return HCI_LE_2M_PHY_SUPPORTED(features_ble.as_array);
555 }
556 
supports_ble_coded_phy(void)557 static bool supports_ble_coded_phy(void) {
558   CHECK(readable);
559   CHECK(ble_supported);
560   return HCI_LE_CODED_PHY_SUPPORTED(features_ble.as_array);
561 }
562 
supports_ble_extended_advertising(void)563 static bool supports_ble_extended_advertising(void) {
564   CHECK(readable);
565   CHECK(ble_supported);
566   return HCI_LE_EXTENDED_ADVERTISING_SUPPORTED(features_ble.as_array);
567 }
568 
supports_ble_periodic_advertising(void)569 static bool supports_ble_periodic_advertising(void) {
570   CHECK(readable);
571   CHECK(ble_supported);
572   return HCI_LE_PERIODIC_ADVERTISING_SUPPORTED(features_ble.as_array);
573 }
574 
supports_ble_peripheral_initiated_feature_exchange(void)575 static bool supports_ble_peripheral_initiated_feature_exchange(void) {
576   CHECK(readable);
577   CHECK(ble_supported);
578   return HCI_LE_PERIPHERAL_INIT_FEAT_EXC_SUPPORTED(features_ble.as_array);
579 }
580 
supports_ble_connection_parameter_request(void)581 static bool supports_ble_connection_parameter_request(void) {
582   CHECK(readable);
583   CHECK(ble_supported);
584   return HCI_LE_CONN_PARAM_REQ_SUPPORTED(features_ble.as_array);
585 }
586 
supports_ble_periodic_advertising_sync_transfer_sender(void)587 static bool supports_ble_periodic_advertising_sync_transfer_sender(void) {
588   CHECK(readable);
589   CHECK(ble_supported);
590   return HCI_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_SENDER(
591       features_ble.as_array);
592 }
593 
supports_ble_periodic_advertising_sync_transfer_recipient(void)594 static bool supports_ble_periodic_advertising_sync_transfer_recipient(void) {
595   CHECK(readable);
596   CHECK(ble_supported);
597   return HCI_LE_PERIODIC_ADVERTISING_SYNC_TRANSFER_RECIPIENT(
598       features_ble.as_array);
599 }
600 
supports_ble_connected_isochronous_stream_central(void)601 static bool supports_ble_connected_isochronous_stream_central(void) {
602   CHECK(readable);
603   CHECK(ble_supported);
604   return HCI_LE_CIS_CENTRAL(features_ble.as_array);
605 }
606 
supports_ble_connected_isochronous_stream_peripheral(void)607 static bool supports_ble_connected_isochronous_stream_peripheral(void) {
608   CHECK(readable);
609   CHECK(ble_supported);
610   return HCI_LE_CIS_PERIPHERAL(features_ble.as_array);
611 }
612 
supports_ble_isochronous_broadcaster(void)613 static bool supports_ble_isochronous_broadcaster(void) {
614   CHECK(readable);
615   CHECK(ble_supported);
616   return HCI_LE_ISO_BROADCASTER(features_ble.as_array);
617 }
618 
supports_ble_synchronized_receiver(void)619 static bool supports_ble_synchronized_receiver(void) {
620   CHECK(readable);
621   CHECK(ble_supported);
622   return HCI_LE_SYNCHRONIZED_RECEIVER(features_ble.as_array);
623 }
624 
get_acl_data_size_classic(void)625 static uint16_t get_acl_data_size_classic(void) {
626   CHECK(readable);
627   return acl_data_size_classic;
628 }
629 
get_acl_data_size_ble(void)630 static uint16_t get_acl_data_size_ble(void) {
631   CHECK(readable);
632   CHECK(ble_supported);
633   return acl_data_size_ble;
634 }
635 
get_iso_data_size(void)636 static uint16_t get_iso_data_size(void) {
637   CHECK(readable);
638   return iso_data_size;
639 }
640 
get_acl_packet_size_classic(void)641 static uint16_t get_acl_packet_size_classic(void) {
642   CHECK(readable);
643   return acl_data_size_classic + HCI_DATA_PREAMBLE_SIZE;
644 }
645 
get_acl_packet_size_ble(void)646 static uint16_t get_acl_packet_size_ble(void) {
647   CHECK(readable);
648   return acl_data_size_ble + HCI_DATA_PREAMBLE_SIZE;
649 }
650 
get_iso_packet_size(void)651 static uint16_t get_iso_packet_size(void) {
652   CHECK(readable);
653   return iso_data_size + HCI_DATA_PREAMBLE_SIZE;
654 }
655 
get_ble_suggested_default_data_length(void)656 static uint16_t get_ble_suggested_default_data_length(void) {
657   CHECK(readable);
658   CHECK(ble_supported);
659   return ble_suggested_default_data_length;
660 }
661 
get_ble_maximum_tx_data_length(void)662 static uint16_t get_ble_maximum_tx_data_length(void) {
663   CHECK(readable);
664   CHECK(ble_supported);
665   return ble_supported_max_tx_octets;
666 }
667 
get_ble_maximum_tx_time(void)668 static uint16_t get_ble_maximum_tx_time(void) {
669   CHECK(readable);
670   CHECK(ble_supported);
671   return ble_supported_max_tx_time;
672 }
673 
get_ble_maxium_advertising_data_length(void)674 static uint16_t get_ble_maxium_advertising_data_length(void) {
675   CHECK(readable);
676   CHECK(ble_supported);
677   return ble_maxium_advertising_data_length;
678 }
679 
get_ble_number_of_supported_advertising_sets(void)680 static uint8_t get_ble_number_of_supported_advertising_sets(void) {
681   CHECK(readable);
682   CHECK(ble_supported);
683   return ble_number_of_supported_advertising_sets;
684 }
685 
get_ble_periodic_advertiser_list_size(void)686 static uint8_t get_ble_periodic_advertiser_list_size(void) {
687   CHECK(readable);
688   CHECK(ble_supported);
689   return ble_periodic_advertiser_list_size;
690 }
691 
get_acl_buffer_count_classic(void)692 static uint16_t get_acl_buffer_count_classic(void) {
693   CHECK(readable);
694   return acl_buffer_count_classic;
695 }
696 
get_acl_buffer_count_ble(void)697 static uint8_t get_acl_buffer_count_ble(void) {
698   CHECK(readable);
699   CHECK(ble_supported);
700   return acl_buffer_count_ble;
701 }
702 
get_iso_buffer_count(void)703 static uint8_t get_iso_buffer_count(void) {
704   CHECK(readable);
705   CHECK(ble_supported);
706   return iso_buffer_count;
707 }
708 
get_ble_acceptlist_size(void)709 static uint8_t get_ble_acceptlist_size(void) {
710   CHECK(readable);
711   CHECK(ble_supported);
712   return ble_acceptlist_size;
713 }
714 
get_ble_resolving_list_max_size(void)715 static uint8_t get_ble_resolving_list_max_size(void) {
716   CHECK(readable);
717   CHECK(ble_supported);
718   return ble_resolving_list_max_size;
719 }
720 
set_ble_resolving_list_max_size(int resolving_list_max_size)721 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
722   // Setting "resolving_list_max_size" to 0 is done during cleanup,
723   // hence we ignore the "readable" flag already set to false during shutdown.
724   if (resolving_list_max_size != 0) {
725     CHECK(readable);
726   }
727   CHECK(ble_supported);
728   ble_resolving_list_max_size = resolving_list_max_size;
729 }
730 
get_le_all_initiating_phys()731 static uint8_t get_le_all_initiating_phys() {
732   uint8_t phy = PHY_LE_1M;
733   // TODO(jpawlowski): uncomment after next FW udpate
734   // if (supports_ble_2m_phy()) phy |= PHY_LE_2M;
735   // if (supports_ble_coded_phy()) phy |= PHY_LE_CODED;
736   return phy;
737 }
738 
739 static const controller_t interface = {
740     get_is_ready,
741 
742     get_address,
743     get_bt_version,
744 
745     get_ble_supported_states,
746 
747     supports_simple_pairing,
748     supports_secure_connections,
749     supports_simultaneous_le_bredr,
750     supports_reading_remote_extended_features,
751     supports_interlaced_inquiry_scan,
752     supports_rssi_with_inquiry_results,
753     supports_extended_inquiry_response,
754     supports_central_peripheral_role_switch,
755     supports_enhanced_setup_synchronous_connection,
756     supports_enhanced_accept_synchronous_connection,
757     supports_3_slot_packets,
758     supports_5_slot_packets,
759     supports_classic_2m_phy,
760     supports_classic_3m_phy,
761     supports_3_slot_edr_packets,
762     supports_5_slot_edr_packets,
763     supports_sco,
764     supports_hv2_packets,
765     supports_hv3_packets,
766     supports_ev3_packets,
767     supports_ev4_packets,
768     supports_ev5_packets,
769     supports_esco_2m_phy,
770     supports_esco_3m_phy,
771     supports_3_slot_esco_edr_packets,
772     supports_role_switch,
773     supports_hold_mode,
774     supports_sniff_mode,
775     supports_park_mode,
776     supports_non_flushable_pb,
777     supports_sniff_subrating,
778     supports_encryption_pause,
779 
780     supports_ble,
781     supports_ble_packet_extension,
782     supports_ble_connection_parameters_request,
783     supports_ble_privacy,
784     supports_ble_set_privacy_mode,
785     supports_ble_2m_phy,
786     supports_ble_coded_phy,
787     supports_ble_extended_advertising,
788     supports_ble_periodic_advertising,
789     supports_ble_peripheral_initiated_feature_exchange,
790     supports_ble_connection_parameter_request,
791     supports_ble_periodic_advertising_sync_transfer_sender,
792     supports_ble_periodic_advertising_sync_transfer_recipient,
793     supports_ble_connected_isochronous_stream_central,
794     supports_ble_connected_isochronous_stream_peripheral,
795     supports_ble_isochronous_broadcaster,
796     supports_ble_synchronized_receiver,
797 
798     get_acl_data_size_classic,
799     get_acl_data_size_ble,
800     get_iso_data_size,
801 
802     get_acl_packet_size_classic,
803     get_acl_packet_size_ble,
804     get_iso_packet_size,
805 
806     get_ble_suggested_default_data_length,
807     get_ble_maximum_tx_data_length,
808     get_ble_maximum_tx_time,
809     get_ble_maxium_advertising_data_length,
810     get_ble_number_of_supported_advertising_sets,
811     get_ble_periodic_advertiser_list_size,
812 
813     get_acl_buffer_count_classic,
814     get_acl_buffer_count_ble,
815     get_iso_buffer_count,
816 
817     get_ble_acceptlist_size,
818 
819     get_ble_resolving_list_max_size,
820     set_ble_resolving_list_max_size,
821     get_local_supported_codecs,
822     get_le_all_initiating_phys};
823 
controller_get_interface_legacy()824 static const controller_t* controller_get_interface_legacy() {
825   static bool loaded = false;
826   if (!loaded) {
827     loaded = true;
828 
829     local_hci = hci_layer_get_interface();
830     packet_factory = hci_packet_factory_get_interface();
831     packet_parser = hci_packet_parser_get_interface();
832   }
833 
834   return &interface;
835 }
836 
controller_get_interface()837 const controller_t* controller_get_interface() {
838   if (bluetooth::shim::is_gd_controller_enabled()) {
839     return bluetooth::shim::controller_get_interface();
840   } else {
841     return controller_get_interface_legacy();
842   }
843 }
844 
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)845 const controller_t* controller_get_test_interface(
846     const hci_t* hci_interface,
847     const hci_packet_factory_t* packet_factory_interface,
848     const hci_packet_parser_t* packet_parser_interface) {
849   local_hci = hci_interface;
850   packet_factory = packet_factory_interface;
851   packet_parser = packet_parser_interface;
852   return &interface;
853 }
854