1 /*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "bt_shim_controller"
18
19 #include "main/shim/controller.h"
20
21 #include "btcore/include/module.h"
22 #include "gd/common/contextual_callback.h"
23 #include "gd/common/init_flags.h"
24 #include "gd/hci/controller.h"
25 #include "hci/controller.h"
26 #include "main/shim/entry.h"
27 #include "main/shim/helpers.h"
28 #include "main/shim/shim.h"
29 #include "main/shim/stack.h"
30 #include "osi/include/future.h"
31 #include "osi/include/log.h"
32 #include "types/raw_address.h"
33
34 using ::bluetooth::shim::GetController;
35
36 constexpr int kMaxSupportedCodecs = 8; // MAX_LOCAL_SUPPORTED_CODECS_SIZE
37
38 constexpr uint8_t kPhyLe1M = 0x01;
39
40 constexpr int kHciDataPreambleSize = 4; // #define HCI_DATA_PREAMBLE_SIZE 4
41
42 // Module lifecycle functions
43 static future_t* start_up(void);
44 static future_t* shut_down(void);
45
46 EXPORT_SYMBOL extern const module_t gd_controller_module = {
47 .name = GD_CONTROLLER_MODULE,
48 .init = nullptr,
49 .start_up = start_up,
50 .shut_down = shut_down,
51 .clean_up = nullptr,
52 .dependencies = {GD_SHIM_MODULE, nullptr}};
53
54 struct {
55 bool ready;
56 RawAddress raw_address;
57 bt_version_t bt_version;
58 uint8_t local_supported_codecs[kMaxSupportedCodecs];
59 uint8_t number_of_local_supported_codecs;
60 uint64_t le_supported_states;
61 uint8_t phy;
62 } data_;
63
start_up(void)64 static future_t* start_up(void) {
65 LOG_INFO("%s Starting up", __func__);
66 data_.ready = true;
67
68 std::string string_address = GetController()->GetMacAddress().ToString();
69 RawAddress::FromString(string_address, data_.raw_address);
70
71 data_.le_supported_states =
72 bluetooth::shim::GetController()->GetLeSupportedStates();
73
74 auto local_version_info =
75 bluetooth::shim::GetController()->GetLocalVersionInformation();
76 data_.bt_version.hci_version =
77 static_cast<uint8_t>(local_version_info.hci_version_);
78 data_.bt_version.hci_revision = local_version_info.hci_revision_;
79 data_.bt_version.lmp_version =
80 static_cast<uint8_t>(local_version_info.lmp_version_);
81 data_.bt_version.lmp_subversion = local_version_info.lmp_subversion_;
82 data_.bt_version.manufacturer = local_version_info.manufacturer_name_;
83
84 LOG_INFO("Mac address:%s", ADDRESS_TO_LOGGABLE_CSTR(data_.raw_address));
85
86 data_.phy = kPhyLe1M;
87
88 return future_new_immediate(FUTURE_SUCCESS);
89 }
90
shut_down(void)91 static future_t* shut_down(void) {
92 data_.ready = false;
93 return future_new_immediate(FUTURE_SUCCESS);
94 }
95
96 /**
97 * Module methods
98 */
99
get_is_ready(void)100 static bool get_is_ready(void) { return data_.ready; }
101
get_address(void)102 static const RawAddress* get_address(void) { return &data_.raw_address; }
103
get_bt_version(void)104 static const bt_version_t* get_bt_version(void) { return &data_.bt_version; }
105
get_local_supported_codecs(uint8_t * number_of_codecs)106 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
107 CHECK(number_of_codecs != nullptr);
108 if (data_.number_of_local_supported_codecs != 0) {
109 *number_of_codecs = data_.number_of_local_supported_codecs;
110 return data_.local_supported_codecs;
111 }
112 return (uint8_t*)nullptr;
113 }
114
get_ble_supported_states(void)115 static const uint8_t* get_ble_supported_states(void) {
116 return (const uint8_t*)&data_.le_supported_states;
117 }
118
119 #define MAP_TO_GD(legacy, gd) \
120 static bool legacy(void) { return GetController()->gd(); }
121
MAP_TO_GD(supports_simple_pairing,SupportsSimplePairing)122 MAP_TO_GD(supports_simple_pairing, SupportsSimplePairing)
123 MAP_TO_GD(supports_secure_connections, SupportsSecureConnections)
124 MAP_TO_GD(supports_simultaneous_le_bredr, SupportsSimultaneousLeBrEdr)
125 MAP_TO_GD(supports_interlaced_inquiry_scan, SupportsInterlacedInquiryScan)
126 MAP_TO_GD(supports_rssi_with_inquiry_results, SupportsRssiWithInquiryResults)
127 MAP_TO_GD(supports_extended_inquiry_response, SupportsExtendedInquiryResponse)
128 MAP_TO_GD(supports_three_slot_packets, Supports3SlotPackets)
129 MAP_TO_GD(supports_five_slot_packets, Supports5SlotPackets)
130 MAP_TO_GD(supports_classic_2m_phy, SupportsClassic2mPhy)
131 MAP_TO_GD(supports_classic_3m_phy, SupportsClassic3mPhy)
132 MAP_TO_GD(supports_three_slot_edr_packets, Supports3SlotEdrPackets)
133 MAP_TO_GD(supports_five_slot_edr_packets, Supports5SlotEdrPackets)
134 MAP_TO_GD(supports_sco, SupportsSco)
135 MAP_TO_GD(supports_hv2_packets, SupportsHv2Packets)
136 MAP_TO_GD(supports_hv3_packets, SupportsHv3Packets)
137 MAP_TO_GD(supports_ev3_packets, SupportsEv3Packets)
138 MAP_TO_GD(supports_ev4_packets, SupportsEv4Packets)
139 MAP_TO_GD(supports_ev5_packets, SupportsEv5Packets)
140 MAP_TO_GD(supports_esco_2m_phy, SupportsEsco2mPhy)
141 MAP_TO_GD(supports_esco_3m_phy, SupportsEsco3mPhy)
142 MAP_TO_GD(supports_three_slot_esco_edr_packets, Supports3SlotEscoEdrPackets)
143 MAP_TO_GD(supports_role_switch, SupportsRoleSwitch)
144 MAP_TO_GD(supports_hold_mode, SupportsHoldMode)
145 MAP_TO_GD(supports_sniff_mode, SupportsSniffMode)
146 MAP_TO_GD(supports_park_mode, SupportsParkMode)
147 MAP_TO_GD(supports_non_flushable_pb, SupportsNonFlushablePb)
148 MAP_TO_GD(supports_sniff_subrating, SupportsSniffSubrating)
149 MAP_TO_GD(supports_encryption_pause, SupportsEncryptionPause)
150
151 MAP_TO_GD(supports_ble, SupportsBle)
152 MAP_TO_GD(supports_privacy, SupportsBlePrivacy)
153 MAP_TO_GD(supports_packet_extension, SupportsBleDataPacketLengthExtension)
154 MAP_TO_GD(supports_connection_parameters_request,
155 SupportsBleConnectionParametersRequest)
156 MAP_TO_GD(supports_ble_2m_phy, SupportsBle2mPhy)
157 MAP_TO_GD(supports_ble_coded_phy, SupportsBleCodedPhy)
158 MAP_TO_GD(supports_extended_advertising, SupportsBleExtendedAdvertising)
159 MAP_TO_GD(supports_periodic_advertising, SupportsBlePeriodicAdvertising)
160 MAP_TO_GD(supports_peripheral_initiated_feature_exchange,
161 SupportsBlePeripheralInitiatedFeaturesExchange)
162 MAP_TO_GD(supports_connection_parameter_request,
163 SupportsBleConnectionParametersRequest)
164
165 MAP_TO_GD(supports_periodic_advertising_sync_transfer_sender,
166 SupportsBlePeriodicAdvertisingSyncTransferSender)
167 MAP_TO_GD(supports_periodic_advertising_sync_transfer_recipient,
168 SupportsBlePeriodicAdvertisingSyncTransferRecipient)
169 MAP_TO_GD(supports_connected_iso_stream_central,
170 SupportsBleConnectedIsochronousStreamCentral)
171 MAP_TO_GD(supports_connected_iso_stream_peripheral,
172 SupportsBleConnectedIsochronousStreamPeripheral)
173 MAP_TO_GD(supports_iso_broadcaster, SupportsBleIsochronousBroadcaster)
174 MAP_TO_GD(supports_synchronized_receiver, SupportsBleSynchronizedReceiver)
175 MAP_TO_GD(supports_ble_connection_subrating, SupportsBleConnectionSubrating)
176 MAP_TO_GD(supports_ble_connection_subrating_host,
177 SupportsBleConnectionSubratingHost)
178
179 #define FORWARD(legacy, gd) \
180 static bool legacy(void) { return gd; }
181
182 FORWARD(
183 supports_configure_data_path,
184 GetController()->IsSupported(bluetooth::hci::OpCode::CONFIGURE_DATA_PATH))
185
186 FORWARD(supports_set_min_encryption_key_size,
187 GetController()->IsSupported(
188 bluetooth::hci::OpCode::SET_MIN_ENCRYPTION_KEY_SIZE))
189
190 FORWARD(supports_read_encryption_key_size,
191 GetController()->IsSupported(
192 bluetooth::hci::OpCode::READ_ENCRYPTION_KEY_SIZE))
193
194 FORWARD(supports_reading_remote_extended_features,
195 GetController()->IsSupported(
196 bluetooth::hci::OpCode::READ_REMOTE_EXTENDED_FEATURES))
197
198 FORWARD(supports_enhanced_setup_synchronous_connection,
199 GetController()->IsSupported(
200 bluetooth::hci::OpCode::ENHANCED_SETUP_SYNCHRONOUS_CONNECTION))
201
202 FORWARD(supports_enhanced_accept_synchronous_connection,
203 GetController()->IsSupported(
204 bluetooth::hci::OpCode::ENHANCED_ACCEPT_SYNCHRONOUS_CONNECTION))
205
206 FORWARD(
207 supports_ble_set_privacy_mode,
208 GetController()->IsSupported(bluetooth::hci::OpCode::LE_SET_PRIVACY_MODE))
209
210 #define FORWARD_GETTER(type, legacy, gd) \
211 static type legacy(void) { return gd; }
212
213 FORWARD_GETTER(uint16_t, get_acl_buffer_length,
214 GetController()->GetAclPacketLength())
215 FORWARD_GETTER(uint16_t, get_le_buffer_length,
216 GetController()->GetLeBufferSize().le_data_packet_length_)
217 FORWARD_GETTER(
218 uint16_t, get_iso_buffer_length,
219 GetController()->GetControllerIsoBufferSize().le_data_packet_length_)
220
221 static uint16_t get_acl_packet_size_classic(void) {
222 return get_acl_buffer_length() + kHciDataPreambleSize;
223 }
224
get_acl_packet_size_ble(void)225 static uint16_t get_acl_packet_size_ble(void) {
226 return get_le_buffer_length() + kHciDataPreambleSize;
227 }
228
get_iso_packet_size(void)229 static uint16_t get_iso_packet_size(void) {
230 return get_iso_buffer_length() + kHciDataPreambleSize;
231 }
232
233 FORWARD_GETTER(uint16_t, get_le_suggested_default_data_length,
234 GetController()->GetLeSuggestedDefaultDataLength())
235
get_le_maximum_tx_data_length(void)236 static uint16_t get_le_maximum_tx_data_length(void) {
237 ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
238 GetController()->GetLeMaximumDataLength();
239 return le_maximum_data_length.supported_max_tx_octets_;
240 }
241
get_le_maximum_tx_time(void)242 static uint16_t get_le_maximum_tx_time(void) {
243 ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
244 GetController()->GetLeMaximumDataLength();
245 return le_maximum_data_length.supported_max_tx_time_;
246 }
247
248 FORWARD_GETTER(uint16_t, get_le_max_advertising_data_length,
249 GetController()->GetLeMaximumAdvertisingDataLength())
250 FORWARD_GETTER(uint8_t, get_le_supported_advertising_sets,
251 GetController()->GetLeNumberOfSupportedAdverisingSets())
252 FORWARD_GETTER(uint8_t, get_le_periodic_advertiser_list_size,
253 GetController()->GetLePeriodicAdvertiserListSize())
254 FORWARD_GETTER(uint16_t, get_acl_buffers,
255 GetController()->GetNumAclPacketBuffers())
256 FORWARD_GETTER(uint8_t, get_le_buffers,
257 GetController()->GetLeBufferSize().total_num_le_packets_)
258 FORWARD_GETTER(
259 uint8_t, get_iso_buffers,
260 GetController()->GetControllerIsoBufferSize().total_num_le_packets_)
261 FORWARD_GETTER(uint8_t, get_le_connect_list_size,
262 GetController()->GetLeFilterAcceptListSize())
263
set_ble_resolving_list_max_size(int resolving_list_max_size)264 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
265 LOG_DEBUG("UNSUPPORTED");
266 }
267
get_le_resolving_list_size(void)268 static uint8_t get_le_resolving_list_size(void) {
269 return bluetooth::shim::GetController()->GetLeResolvingListSize();
270 }
271
get_le_all_initiating_phys()272 static uint8_t get_le_all_initiating_phys() { return data_.phy; }
273
controller_clear_event_filter()274 static uint8_t controller_clear_event_filter() {
275 LOG_VERBOSE("Called!");
276 bluetooth::shim::GetController()->SetEventFilterClearAll();
277 return BTM_SUCCESS;
278 }
279
controller_clear_event_mask()280 static uint8_t controller_clear_event_mask() {
281 LOG_VERBOSE("Called!");
282 bluetooth::shim::GetController()->SetEventMask(0);
283 bluetooth::shim::GetController()->LeSetEventMask(0);
284 return BTM_SUCCESS;
285 }
286
controller_le_rand(LeRandCallback cb)287 static uint8_t controller_le_rand(LeRandCallback cb) {
288 LOG_VERBOSE("Called!");
289 bluetooth::shim::GetController()->LeRand(cb);
290 return BTM_SUCCESS;
291 }
292
controller_set_event_filter_connection_setup_all_devices()293 static uint8_t controller_set_event_filter_connection_setup_all_devices() {
294 bluetooth::shim::GetController()->SetEventFilterConnectionSetupAllDevices(
295 bluetooth::hci::AutoAcceptFlag::AUTO_ACCEPT_ON_ROLE_SWITCH_ENABLED);
296 return BTM_SUCCESS;
297 }
298
controller_set_event_filter_allow_device_connection(std::vector<RawAddress> devices)299 static uint8_t controller_set_event_filter_allow_device_connection(
300 std::vector<RawAddress> devices) {
301 for (const RawAddress& address : devices) {
302 bluetooth::shim::GetController()->SetEventFilterConnectionSetupAddress(
303 bluetooth::ToGdAddress(address),
304 bluetooth::hci::AutoAcceptFlag::AUTO_ACCEPT_OFF);
305 }
306 return BTM_SUCCESS;
307 }
308
controller_set_default_event_mask_except(uint64_t mask,uint64_t le_mask)309 static uint8_t controller_set_default_event_mask_except(uint64_t mask,
310 uint64_t le_mask) {
311 uint64_t applied_mask =
312 bluetooth::hci::Controller::kDefaultEventMask & ~(mask);
313 uint64_t applied_le_mask =
314 bluetooth::hci::Controller::kDefaultLeEventMask & ~(le_mask);
315
316 bluetooth::shim::GetController()->SetEventMask(applied_mask);
317 bluetooth::shim::GetController()->LeSetEventMask(applied_le_mask);
318 return BTM_SUCCESS;
319 }
320
controller_set_event_filter_inquiry_result_all_devices()321 static uint8_t controller_set_event_filter_inquiry_result_all_devices() {
322 bluetooth::shim::GetController()->SetEventFilterInquiryResultAllDevices();
323 return BTM_SUCCESS;
324 }
325
326 static const controller_t interface = {
327 .get_is_ready = get_is_ready,
328
329 .get_address = get_address,
330 .get_bt_version = get_bt_version,
331
332 .get_ble_supported_states = get_ble_supported_states,
333
334 .supports_simple_pairing = supports_simple_pairing,
335 .supports_secure_connections = supports_secure_connections,
336 .supports_simultaneous_le_bredr = supports_simultaneous_le_bredr,
337 .supports_reading_remote_extended_features =
338 supports_reading_remote_extended_features,
339 .supports_interlaced_inquiry_scan = supports_interlaced_inquiry_scan,
340 .supports_rssi_with_inquiry_results = supports_rssi_with_inquiry_results,
341 .supports_extended_inquiry_response = supports_extended_inquiry_response,
342 .supports_central_peripheral_role_switch = supports_role_switch,
343 .supports_enhanced_setup_synchronous_connection =
344 supports_enhanced_setup_synchronous_connection,
345 .supports_enhanced_accept_synchronous_connection =
346 supports_enhanced_accept_synchronous_connection,
347 .supports_3_slot_packets = supports_three_slot_packets,
348 .supports_5_slot_packets = supports_five_slot_packets,
349 .supports_classic_2m_phy = supports_classic_2m_phy,
350 .supports_classic_3m_phy = supports_classic_3m_phy,
351 .supports_3_slot_edr_packets = supports_three_slot_edr_packets,
352 .supports_5_slot_edr_packets = supports_five_slot_edr_packets,
353 .supports_sco = supports_sco,
354 .supports_hv2_packets = supports_hv2_packets,
355 .supports_hv3_packets = supports_hv3_packets,
356 .supports_ev3_packets = supports_ev3_packets,
357 .supports_ev4_packets = supports_ev4_packets,
358 .supports_ev5_packets = supports_ev5_packets,
359 .supports_esco_2m_phy = supports_esco_2m_phy,
360 .supports_esco_3m_phy = supports_esco_3m_phy,
361 .supports_3_slot_esco_edr_packets = supports_three_slot_esco_edr_packets,
362 .supports_role_switch = supports_role_switch,
363 .supports_hold_mode = supports_hold_mode,
364 .supports_sniff_mode = supports_sniff_mode,
365 .supports_park_mode = supports_park_mode,
366 .supports_non_flushable_pb = supports_non_flushable_pb,
367 .supports_sniff_subrating = supports_sniff_subrating,
368 .supports_encryption_pause = supports_encryption_pause,
369 .supports_configure_data_path = supports_configure_data_path,
370 .supports_set_min_encryption_key_size =
371 supports_set_min_encryption_key_size,
372 .supports_read_encryption_key_size = supports_read_encryption_key_size,
373
374 .supports_ble = supports_ble,
375 .supports_ble_packet_extension = supports_packet_extension,
376 .supports_ble_connection_parameters_request =
377 supports_connection_parameters_request,
378 .supports_ble_privacy = supports_privacy,
379 .supports_ble_set_privacy_mode = supports_ble_set_privacy_mode,
380 .supports_ble_2m_phy = supports_ble_2m_phy,
381 .supports_ble_coded_phy = supports_ble_coded_phy,
382 .supports_ble_extended_advertising = supports_extended_advertising,
383 .supports_ble_periodic_advertising = supports_periodic_advertising,
384 .supports_ble_peripheral_initiated_feature_exchange =
385 supports_peripheral_initiated_feature_exchange,
386 .supports_ble_connection_parameter_request =
387 supports_connection_parameter_request,
388 .supports_ble_periodic_advertising_sync_transfer_sender =
389 supports_periodic_advertising_sync_transfer_sender,
390 .supports_ble_periodic_advertising_sync_transfer_recipient =
391 supports_periodic_advertising_sync_transfer_recipient,
392 .supports_ble_connected_isochronous_stream_central =
393 supports_connected_iso_stream_central,
394 .supports_ble_connected_isochronous_stream_peripheral =
395 supports_connected_iso_stream_peripheral,
396 .supports_ble_isochronous_broadcaster = supports_iso_broadcaster,
397 .supports_ble_synchronized_receiver = supports_synchronized_receiver,
398 .supports_ble_connection_subrating = supports_ble_connection_subrating,
399 .supports_ble_connection_subrating_host =
400 supports_ble_connection_subrating_host,
401
402 .get_acl_data_size_classic = get_acl_buffer_length,
403 .get_acl_data_size_ble = get_le_buffer_length,
404 .get_iso_data_size = get_iso_buffer_length,
405
406 .get_acl_packet_size_classic = get_acl_packet_size_classic,
407 .get_acl_packet_size_ble = get_acl_packet_size_ble,
408 .get_iso_packet_size = get_iso_packet_size,
409
410 .get_ble_default_data_packet_length = get_le_suggested_default_data_length,
411 .get_ble_maximum_tx_data_length = get_le_maximum_tx_data_length,
412 .get_ble_maximum_tx_time = get_le_maximum_tx_time,
413 .get_ble_maximum_advertising_data_length =
414 get_le_max_advertising_data_length,
415 .get_ble_number_of_supported_advertising_sets =
416 get_le_supported_advertising_sets,
417 .get_ble_periodic_advertiser_list_size =
418 get_le_periodic_advertiser_list_size,
419
420 .get_acl_buffer_count_classic = get_acl_buffers,
421 .get_acl_buffer_count_ble = get_le_buffers,
422 .get_iso_buffer_count = get_iso_buffers,
423
424 .get_ble_acceptlist_size = get_le_connect_list_size,
425
426 .get_ble_resolving_list_max_size = get_le_resolving_list_size,
427 .set_ble_resolving_list_max_size = set_ble_resolving_list_max_size,
428 .get_local_supported_codecs = get_local_supported_codecs,
429 .get_le_all_initiating_phys = get_le_all_initiating_phys,
430 .clear_event_filter = controller_clear_event_filter,
431 .clear_event_mask = controller_clear_event_mask,
432 .le_rand = controller_le_rand,
433 .set_event_filter_connection_setup_all_devices =
434 controller_set_event_filter_connection_setup_all_devices,
435 .set_event_filter_allow_device_connection =
436 controller_set_event_filter_allow_device_connection,
437 .set_default_event_mask_except = controller_set_default_event_mask_except,
438 .set_event_filter_inquiry_result_all_devices =
439 controller_set_event_filter_inquiry_result_all_devices};
440
controller_get_interface()441 const controller_t* bluetooth::shim::controller_get_interface() {
442 static bool loaded = false;
443 if (!loaded) {
444 loaded = true;
445 }
446 return &interface;
447 }
448
controller_is_write_link_supervision_timeout_supported()449 bool bluetooth::shim::controller_is_write_link_supervision_timeout_supported() {
450 return bluetooth::shim::GetController()->IsSupported(
451 bluetooth::hci::OpCode::WRITE_LINK_SUPERVISION_TIMEOUT);
452 }
453