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