• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/init_flags.h"
23 #include "hci/controller.h"
24 #include "main/shim/entry.h"
25 #include "main/shim/shim.h"
26 #include "main/shim/stack.h"
27 #include "osi/include/future.h"
28 #include "osi/include/log.h"
29 #include "src/bridge.rs.h"
30 #include "types/raw_address.h"
31 
32 using ::bluetooth::common::init_flags::gd_rust_is_enabled;
33 using ::bluetooth::shim::GetController;
34 
35 constexpr int kMaxSupportedCodecs = 8;  // MAX_LOCAL_SUPPORTED_CODECS_SIZE
36 
37 constexpr uint8_t kPhyLe1M = 0x01;
38 
39 /**
40  * Interesting commands supported by controller
41  */
42 constexpr int kReadRemoteExtendedFeatures = 0x41c;
43 constexpr int kEnhancedSetupSynchronousConnection = 0x428;
44 constexpr int kEnhancedAcceptSynchronousConnection = 0x429;
45 constexpr int kLeSetPrivacyMode = 0x204e;
46 constexpr int kConfigureDataPath = 0x0c83;
47 
48 constexpr int kHciDataPreambleSize = 4;  // #define HCI_DATA_PREAMBLE_SIZE 4
49 
50 // Module lifecycle functions
51 static future_t* start_up(void);
52 static future_t* shut_down(void);
53 
54 EXPORT_SYMBOL extern const module_t gd_controller_module = {
55     .name = GD_CONTROLLER_MODULE,
56     .init = nullptr,
57     .start_up = start_up,
58     .shut_down = shut_down,
59     .clean_up = nullptr,
60     .dependencies = {GD_SHIM_MODULE, nullptr}};
61 
62 struct {
63   bool ready;
64   RawAddress raw_address;
65   bt_version_t bt_version;
66   uint8_t local_supported_codecs[kMaxSupportedCodecs];
67   uint8_t number_of_local_supported_codecs;
68   uint64_t le_supported_states;
69   uint8_t phy;
70 } data_;
71 
start_up(void)72 static future_t* start_up(void) {
73   LOG_INFO("%s Starting up", __func__);
74   data_.ready = true;
75 
76   if (gd_rust_is_enabled()) {
77     auto controller =
78         bluetooth::shim::Stack::GetInstance()->GetRustController();
79     auto rust_string_address =
80         bluetooth::shim::rust::controller_get_address(**controller);
81     auto string_address =
82         std::string(rust_string_address.data(), rust_string_address.length());
83     RawAddress::FromString(string_address, data_.raw_address);
84 
85     data_.le_supported_states =
86         bluetooth::shim::rust::controller_get_le_supported_states(**controller);
87 
88     LOG_INFO("Mac address:%s", string_address.c_str());
89   } else {
90     std::string string_address = GetController()->GetMacAddress().ToString();
91     RawAddress::FromString(string_address, data_.raw_address);
92 
93     data_.le_supported_states =
94         bluetooth::shim::GetController()->GetLeSupportedStates();
95 
96     auto local_version_info =
97         bluetooth::shim::GetController()->GetLocalVersionInformation();
98     data_.bt_version.hci_version =
99         static_cast<uint8_t>(local_version_info.hci_version_);
100     data_.bt_version.hci_revision = local_version_info.hci_revision_;
101     data_.bt_version.lmp_version =
102         static_cast<uint8_t>(local_version_info.lmp_version_);
103     data_.bt_version.lmp_subversion = local_version_info.lmp_subversion_;
104     data_.bt_version.manufacturer = local_version_info.manufacturer_name_;
105 
106     LOG_INFO("Mac address:%s", string_address.c_str());
107   }
108 
109   data_.phy = kPhyLe1M;
110 
111   return future_new_immediate(FUTURE_SUCCESS);
112 }
113 
shut_down(void)114 static future_t* shut_down(void) {
115   data_.ready = false;
116   return future_new_immediate(FUTURE_SUCCESS);
117 }
118 
119 /**
120  * Module methods
121  */
122 
get_is_ready(void)123 static bool get_is_ready(void) { return data_.ready; }
124 
get_address(void)125 static const RawAddress* get_address(void) { return &data_.raw_address; }
126 
get_bt_version(void)127 static const bt_version_t* get_bt_version(void) { return &data_.bt_version; }
128 
get_local_supported_codecs(uint8_t * number_of_codecs)129 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
130   CHECK(number_of_codecs != nullptr);
131   if (data_.number_of_local_supported_codecs != 0) {
132     *number_of_codecs = data_.number_of_local_supported_codecs;
133     return data_.local_supported_codecs;
134   }
135   return (uint8_t*)nullptr;
136 }
137 
get_ble_supported_states(void)138 static const uint8_t* get_ble_supported_states(void) {
139   return (const uint8_t*)&data_.le_supported_states;
140 }
141 
142 #define MAP_TO_GD(legacy, gd)                                            \
143   static bool legacy(void) {                                             \
144     if (gd_rust_is_enabled()) {                                          \
145       return bluetooth::shim::rust::controller_##legacy(                 \
146           **bluetooth::shim::Stack::GetInstance()->GetRustController()); \
147     } else {                                                             \
148       return GetController()->gd();                                      \
149     }                                                                    \
150   }
151 
MAP_TO_GD(supports_simple_pairing,SupportsSimplePairing)152 MAP_TO_GD(supports_simple_pairing, SupportsSimplePairing)
153 MAP_TO_GD(supports_secure_connections, SupportsSecureConnections)
154 MAP_TO_GD(supports_simultaneous_le_bredr, SupportsSimultaneousLeBrEdr)
155 MAP_TO_GD(supports_interlaced_inquiry_scan, SupportsInterlacedInquiryScan)
156 MAP_TO_GD(supports_rssi_with_inquiry_results, SupportsRssiWithInquiryResults)
157 MAP_TO_GD(supports_extended_inquiry_response, SupportsExtendedInquiryResponse)
158 MAP_TO_GD(supports_three_slot_packets, Supports3SlotPackets)
159 MAP_TO_GD(supports_five_slot_packets, Supports5SlotPackets)
160 MAP_TO_GD(supports_classic_2m_phy, SupportsClassic2mPhy)
161 MAP_TO_GD(supports_classic_3m_phy, SupportsClassic3mPhy)
162 MAP_TO_GD(supports_three_slot_edr_packets, Supports3SlotEdrPackets)
163 MAP_TO_GD(supports_five_slot_edr_packets, Supports5SlotEdrPackets)
164 MAP_TO_GD(supports_sco, SupportsSco)
165 MAP_TO_GD(supports_hv2_packets, SupportsHv2Packets)
166 MAP_TO_GD(supports_hv3_packets, SupportsHv3Packets)
167 MAP_TO_GD(supports_ev3_packets, SupportsEv3Packets)
168 MAP_TO_GD(supports_ev4_packets, SupportsEv4Packets)
169 MAP_TO_GD(supports_ev5_packets, SupportsEv5Packets)
170 MAP_TO_GD(supports_esco_2m_phy, SupportsEsco2mPhy)
171 MAP_TO_GD(supports_esco_3m_phy, SupportsEsco3mPhy)
172 MAP_TO_GD(supports_three_slot_esco_edr_packets, Supports3SlotEscoEdrPackets)
173 MAP_TO_GD(supports_role_switch, SupportsRoleSwitch)
174 MAP_TO_GD(supports_hold_mode, SupportsHoldMode)
175 MAP_TO_GD(supports_sniff_mode, SupportsSniffMode)
176 MAP_TO_GD(supports_park_mode, SupportsParkMode)
177 MAP_TO_GD(supports_non_flushable_pb, SupportsNonFlushablePb)
178 MAP_TO_GD(supports_sniff_subrating, SupportsSniffSubrating)
179 MAP_TO_GD(supports_encryption_pause, SupportsEncryptionPause)
180 
181 MAP_TO_GD(supports_ble, SupportsBle)
182 MAP_TO_GD(supports_privacy, SupportsBlePrivacy)
183 MAP_TO_GD(supports_packet_extension, SupportsBleDataPacketLengthExtension)
184 MAP_TO_GD(supports_connection_parameters_request,
185           SupportsBleConnectionParametersRequest)
186 MAP_TO_GD(supports_ble_2m_phy, SupportsBle2mPhy)
187 MAP_TO_GD(supports_ble_coded_phy, SupportsBleCodedPhy)
188 MAP_TO_GD(supports_extended_advertising, SupportsBleExtendedAdvertising)
189 MAP_TO_GD(supports_periodic_advertising, SupportsBlePeriodicAdvertising)
190 MAP_TO_GD(supports_peripheral_initiated_feature_exchange,
191           SupportsBlePeripheralInitiatedFeaturesExchange)
192 MAP_TO_GD(supports_connection_parameter_request,
193           SupportsBleConnectionParametersRequest)
194 
195 MAP_TO_GD(supports_periodic_advertising_sync_transfer_sender,
196           SupportsBlePeriodicAdvertisingSyncTransferSender)
197 MAP_TO_GD(supports_periodic_advertising_sync_transfer_recipient,
198           SupportsBlePeriodicAdvertisingSyncTransferRecipient)
199 MAP_TO_GD(supports_connected_iso_stream_central,
200           SupportsBleConnectedIsochronousStreamCentral)
201 MAP_TO_GD(supports_connected_iso_stream_peripheral,
202           SupportsBleConnectedIsochronousStreamPeripheral)
203 MAP_TO_GD(supports_iso_broadcaster, SupportsBleIsochronousBroadcaster)
204 MAP_TO_GD(supports_synchronized_receiver, SupportsBleSynchronizedReceiver)
205 
206 #define FORWARD_IF_RUST(legacy, gd)                                      \
207   static bool legacy(void) {                                             \
208     if (gd_rust_is_enabled()) {                                          \
209       return bluetooth::shim::rust::controller_##legacy(                 \
210           **bluetooth::shim::Stack::GetInstance()->GetRustController()); \
211     } else {                                                             \
212       return gd;                                                         \
213     }                                                                    \
214   }
215 
216 FORWARD_IF_RUST(
217     supports_configure_data_path,
218     GetController()->IsSupported((bluetooth::hci::OpCode)kConfigureDataPath))
219 
220 FORWARD_IF_RUST(supports_reading_remote_extended_features,
221                 GetController()->IsSupported((bluetooth::hci::OpCode)
222                                                  kReadRemoteExtendedFeatures))
223 FORWARD_IF_RUST(supports_enhanced_setup_synchronous_connection,
224                 GetController()->IsSupported((
225                     bluetooth::hci::OpCode)kEnhancedSetupSynchronousConnection))
226 FORWARD_IF_RUST(
227     supports_enhanced_accept_synchronous_connection,
228     GetController()->IsSupported((bluetooth::hci::OpCode)
229                                      kEnhancedAcceptSynchronousConnection))
230 FORWARD_IF_RUST(
231     supports_ble_set_privacy_mode,
232     GetController()->IsSupported((bluetooth::hci::OpCode)kLeSetPrivacyMode))
233 
234 #define FORWARD_GETTER_IF_RUST(type, legacy, gd)                         \
235   static type legacy(void) {                                             \
236     if (gd_rust_is_enabled()) {                                          \
237       return bluetooth::shim::rust::controller_##legacy(                 \
238           **bluetooth::shim::Stack::GetInstance()->GetRustController()); \
239     } else {                                                             \
240       return gd;                                                         \
241     }                                                                    \
242   }
243 
244 FORWARD_GETTER_IF_RUST(uint16_t, get_acl_buffer_length,
245                        GetController()->GetAclPacketLength())
246 FORWARD_GETTER_IF_RUST(
247     uint16_t, get_le_buffer_length,
248     GetController()->GetLeBufferSize().le_data_packet_length_)
249 FORWARD_GETTER_IF_RUST(
250     uint16_t, get_iso_buffer_length,
251     GetController()->GetControllerIsoBufferSize().le_data_packet_length_)
252 
253 static uint16_t get_acl_packet_size_classic(void) {
254   return get_acl_buffer_length() + kHciDataPreambleSize;
255 }
256 
get_acl_packet_size_ble(void)257 static uint16_t get_acl_packet_size_ble(void) {
258   return get_le_buffer_length() + kHciDataPreambleSize;
259 }
260 
get_iso_packet_size(void)261 static uint16_t get_iso_packet_size(void) {
262   return get_iso_buffer_length() + kHciDataPreambleSize;
263 }
264 
265 FORWARD_GETTER_IF_RUST(uint16_t, get_le_suggested_default_data_length,
266                        GetController()->GetLeSuggestedDefaultDataLength())
267 
get_le_maximum_tx_data_length(void)268 static uint16_t get_le_maximum_tx_data_length(void) {
269   if (gd_rust_is_enabled()) {
270     return bluetooth::shim::rust::controller_get_le_maximum_tx_data_length(
271         **bluetooth::shim::Stack::GetInstance()->GetRustController());
272   } else {
273     ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
274         GetController()->GetLeMaximumDataLength();
275     return le_maximum_data_length.supported_max_tx_octets_;
276   }
277 }
278 
get_le_maximum_tx_time(void)279 static uint16_t get_le_maximum_tx_time(void) {
280   if (gd_rust_is_enabled()) {
281     return bluetooth::shim::rust::controller_get_le_maximum_tx_time(
282         **bluetooth::shim::Stack::GetInstance()->GetRustController());
283   } else {
284     ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
285         GetController()->GetLeMaximumDataLength();
286     return le_maximum_data_length.supported_max_tx_time_;
287   }
288 }
289 
290 FORWARD_GETTER_IF_RUST(uint16_t, get_le_max_advertising_data_length,
291                        GetController()->GetLeMaximumAdvertisingDataLength())
292 FORWARD_GETTER_IF_RUST(uint8_t, get_le_supported_advertising_sets,
293                        GetController()->GetLeNumberOfSupportedAdverisingSets())
294 FORWARD_GETTER_IF_RUST(uint8_t, get_le_periodic_advertiser_list_size,
295                        GetController()->GetLePeriodicAdvertiserListSize())
296 FORWARD_GETTER_IF_RUST(uint16_t, get_acl_buffers,
297                        GetController()->GetNumAclPacketBuffers())
298 FORWARD_GETTER_IF_RUST(uint8_t, get_le_buffers,
299                        GetController()->GetLeBufferSize().total_num_le_packets_)
300 FORWARD_GETTER_IF_RUST(
301     uint8_t, get_iso_buffers,
302     GetController()->GetControllerIsoBufferSize().total_num_le_packets_)
303 FORWARD_GETTER_IF_RUST(uint8_t, get_le_connect_list_size,
304                        GetController()->GetLeFilterAcceptListSize())
305 
set_ble_resolving_list_max_size(int resolving_list_max_size)306 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
307   LOG_DEBUG("UNSUPPORTED");
308 }
309 
get_le_resolving_list_size(void)310 static uint8_t get_le_resolving_list_size(void) {
311   return bluetooth::shim::GetController()->GetLeResolvingListSize();
312 }
313 
get_le_all_initiating_phys()314 static uint8_t get_le_all_initiating_phys() { return data_.phy; }
315 
controller_clear_event_filter()316 static uint8_t controller_clear_event_filter() {
317   LOG_VERBOSE("Called!");
318   bluetooth::shim::GetController()->SetEventFilterClearAll();
319   return BTM_SUCCESS;
320 }
321 
322 static const controller_t interface = {
323     .get_is_ready = get_is_ready,
324 
325     .get_address = get_address,
326     .get_bt_version = get_bt_version,
327 
328     .get_ble_supported_states = get_ble_supported_states,
329 
330     .supports_simple_pairing = supports_simple_pairing,
331     .supports_secure_connections = supports_secure_connections,
332     .supports_simultaneous_le_bredr = supports_simultaneous_le_bredr,
333     .supports_reading_remote_extended_features =
334         supports_reading_remote_extended_features,
335     .supports_interlaced_inquiry_scan = supports_interlaced_inquiry_scan,
336     .supports_rssi_with_inquiry_results = supports_rssi_with_inquiry_results,
337     .supports_extended_inquiry_response = supports_extended_inquiry_response,
338     .supports_central_peripheral_role_switch = supports_role_switch,
339     .supports_enhanced_setup_synchronous_connection =
340         supports_enhanced_setup_synchronous_connection,
341     .supports_enhanced_accept_synchronous_connection =
342         supports_enhanced_accept_synchronous_connection,
343     .supports_3_slot_packets = supports_three_slot_packets,
344     .supports_5_slot_packets = supports_five_slot_packets,
345     .supports_classic_2m_phy = supports_classic_2m_phy,
346     .supports_classic_3m_phy = supports_classic_3m_phy,
347     .supports_3_slot_edr_packets = supports_three_slot_edr_packets,
348     .supports_5_slot_edr_packets = supports_five_slot_edr_packets,
349     .supports_sco = supports_sco,
350     .supports_hv2_packets = supports_hv2_packets,
351     .supports_hv3_packets = supports_hv3_packets,
352     .supports_ev3_packets = supports_ev3_packets,
353     .supports_ev4_packets = supports_ev4_packets,
354     .supports_ev5_packets = supports_ev5_packets,
355     .supports_esco_2m_phy = supports_esco_2m_phy,
356     .supports_esco_3m_phy = supports_esco_3m_phy,
357     .supports_3_slot_esco_edr_packets = supports_three_slot_esco_edr_packets,
358     .supports_role_switch = supports_role_switch,
359     .supports_hold_mode = supports_hold_mode,
360     .supports_sniff_mode = supports_sniff_mode,
361     .supports_park_mode = supports_park_mode,
362     .supports_non_flushable_pb = supports_non_flushable_pb,
363     .supports_sniff_subrating = supports_sniff_subrating,
364     .supports_encryption_pause = supports_encryption_pause,
365     .supports_configure_data_path = supports_configure_data_path,
366 
367     .supports_ble = supports_ble,
368     .supports_ble_packet_extension = supports_packet_extension,
369     .supports_ble_connection_parameters_request =
370         supports_connection_parameters_request,
371     .supports_ble_privacy = supports_privacy,
372     .supports_ble_set_privacy_mode = supports_ble_set_privacy_mode,
373     .supports_ble_2m_phy = supports_ble_2m_phy,
374     .supports_ble_coded_phy = supports_ble_coded_phy,
375     .supports_ble_extended_advertising = supports_extended_advertising,
376     .supports_ble_periodic_advertising = supports_periodic_advertising,
377     .supports_ble_peripheral_initiated_feature_exchange =
378         supports_peripheral_initiated_feature_exchange,
379     .supports_ble_connection_parameter_request =
380         supports_connection_parameter_request,
381     .supports_ble_periodic_advertising_sync_transfer_sender =
382         supports_periodic_advertising_sync_transfer_sender,
383     .supports_ble_periodic_advertising_sync_transfer_recipient =
384         supports_periodic_advertising_sync_transfer_recipient,
385     .supports_ble_connected_isochronous_stream_central =
386         supports_connected_iso_stream_central,
387     .supports_ble_connected_isochronous_stream_peripheral =
388         supports_connected_iso_stream_peripheral,
389     .supports_ble_isochronous_broadcaster = supports_iso_broadcaster,
390     .supports_ble_synchronized_receiver = supports_synchronized_receiver,
391 
392     .get_acl_data_size_classic = get_acl_buffer_length,
393     .get_acl_data_size_ble = get_le_buffer_length,
394     .get_iso_data_size = get_iso_buffer_length,
395 
396     .get_acl_packet_size_classic = get_acl_packet_size_classic,
397     .get_acl_packet_size_ble = get_acl_packet_size_ble,
398     .get_iso_packet_size = get_iso_packet_size,
399 
400     .get_ble_default_data_packet_length = get_le_suggested_default_data_length,
401     .get_ble_maximum_tx_data_length = get_le_maximum_tx_data_length,
402     .get_ble_maximum_tx_time = get_le_maximum_tx_time,
403     .get_ble_maximum_advertising_data_length =
404         get_le_max_advertising_data_length,
405     .get_ble_number_of_supported_advertising_sets =
406         get_le_supported_advertising_sets,
407     .get_ble_periodic_advertiser_list_size =
408         get_le_periodic_advertiser_list_size,
409 
410     .get_acl_buffer_count_classic = get_acl_buffers,
411     .get_acl_buffer_count_ble = get_le_buffers,
412     .get_iso_buffer_count = get_iso_buffers,
413 
414     .get_ble_acceptlist_size = get_le_connect_list_size,
415 
416     .get_ble_resolving_list_max_size = get_le_resolving_list_size,
417     .set_ble_resolving_list_max_size = set_ble_resolving_list_max_size,
418     .get_local_supported_codecs = get_local_supported_codecs,
419     .get_le_all_initiating_phys = get_le_all_initiating_phys,
420     .clear_event_filter = controller_clear_event_filter};
421 
controller_get_interface()422 const controller_t* bluetooth::shim::controller_get_interface() {
423   static bool loaded = false;
424   if (!loaded) {
425     loaded = true;
426   }
427   return &interface;
428 }
429 
controller_clear_event_mask()430 void bluetooth::shim::controller_clear_event_mask() {
431   bluetooth::shim::GetController()->SetEventMask(0);
432 }
433 
controller_is_write_link_supervision_timeout_supported()434 bool bluetooth::shim::controller_is_write_link_supervision_timeout_supported() {
435   return bluetooth::shim::GetController()->IsSupported(
436       bluetooth::hci::OpCode::WRITE_LINK_SUPERVISION_TIMEOUT);
437 }
438