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