• 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 "main/shim/entry.h"
22 #include "main/shim/shim.h"
23 #include "osi/include/future.h"
24 #include "osi/include/log.h"
25 
26 #include "hci/controller.h"
27 
28 using ::bluetooth::shim::GetController;
29 
30 constexpr uint8_t kPageZero = 0;
31 constexpr uint8_t kPageOne = 1;
32 constexpr uint8_t kPageTwo = 2;
33 constexpr uint8_t kMaxFeaturePage = 3;
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 
47 constexpr int kHciDataPreambleSize = 4;  // #define HCI_DATA_PREAMBLE_SIZE 4
48 
49 // Module lifecycle functions
50 static future_t* start_up(void);
51 static future_t* shut_down(void);
52 
53 EXPORT_SYMBOL extern const module_t gd_controller_module = {
54     .name = GD_CONTROLLER_MODULE,
55     .init = nullptr,
56     .start_up = start_up,
57     .shut_down = shut_down,
58     .clean_up = nullptr,
59     .dependencies = {GD_SHIM_MODULE, nullptr}};
60 
61 struct {
62   bool ready;
63   uint64_t feature[kMaxFeaturePage];
64   uint64_t le_feature[kMaxFeaturePage];
65   RawAddress raw_address;
66   bt_version_t bt_version;
67   uint8_t local_supported_codecs[kMaxSupportedCodecs];
68   uint8_t number_of_local_supported_codecs;
69   uint64_t le_supported_states;
70   uint8_t phy;
71 } data_;
72 
start_up(void)73 static future_t* start_up(void) {
74   LOG_INFO(LOG_TAG, "%s Starting up", __func__);
75   data_.ready = true;
76 
77   std::string string_address =
78       GetController()->GetControllerMacAddress().ToString();
79   RawAddress::FromString(string_address, data_.raw_address);
80 
81   data_.le_supported_states =
82       bluetooth::shim::GetController()->GetControllerLeSupportedStates();
83 
84   LOG_INFO(LOG_TAG, "Mac address:%s", string_address.c_str());
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 #define BIT(x) (0x1ULL << (x))
100 
get_is_ready(void)101 static bool get_is_ready(void) { return data_.ready; }
102 
get_address(void)103 static const RawAddress* get_address(void) { return &data_.raw_address; }
104 
get_bt_version(void)105 static const bt_version_t* get_bt_version(void) { return &data_.bt_version; }
106 
get_features_classic(int index)107 static const bt_device_features_t* get_features_classic(int index) {
108   CHECK(index >= 0 && index < kMaxFeaturePage);
109   data_.feature[index] =
110       bluetooth::shim::GetController()->GetControllerLocalExtendedFeatures(
111           index);
112   return (const bt_device_features_t*)&data_.feature[index];
113 }
114 
get_last_features_classic_index(void)115 static uint8_t get_last_features_classic_index(void) {
116   return bluetooth::shim::GetController()
117       ->GetControllerLocalExtendedFeaturesMaxPageNumber();
118 }
119 
get_local_supported_codecs(uint8_t * number_of_codecs)120 static uint8_t* get_local_supported_codecs(uint8_t* number_of_codecs) {
121   CHECK(number_of_codecs != nullptr);
122   if (data_.number_of_local_supported_codecs != 0) {
123     *number_of_codecs = data_.number_of_local_supported_codecs;
124     return data_.local_supported_codecs;
125   }
126   return (uint8_t*)nullptr;
127 }
128 
get_features_ble(void)129 static const bt_device_features_t* get_features_ble(void) {
130   return (const bt_device_features_t*)&data_.le_feature[0];
131 }
132 
get_ble_supported_states(void)133 static const uint8_t* get_ble_supported_states(void) {
134   return (const uint8_t*)&data_.le_supported_states;
135 }
136 
supports_simple_pairing(void)137 static bool supports_simple_pairing(void) {
138   return GetController()->GetControllerLocalExtendedFeatures(kPageOne) &
139          BIT(51);
140 }
141 
supports_secure_connections(void)142 static bool supports_secure_connections(void) {
143   return GetController()->GetControllerLocalExtendedFeatures(kPageTwo) & BIT(8);
144 }
145 
supports_simultaneous_le_bredr(void)146 static bool supports_simultaneous_le_bredr(void) {
147   return GetController()->GetControllerLocalExtendedFeatures(kPageZero) &
148          BIT(49);
149 }
150 
supports_reading_remote_extended_features(void)151 static bool supports_reading_remote_extended_features(void) {
152   return GetController()->IsSupported(
153       (bluetooth::hci::OpCode)kReadRemoteExtendedFeatures);
154 }
155 
supports_interlaced_inquiry_scan(void)156 static bool supports_interlaced_inquiry_scan(void) {
157   return GetController()->GetControllerLocalExtendedFeatures(kPageZero) &
158          BIT(28);
159 }
160 
supports_rssi_with_inquiry_results(void)161 static bool supports_rssi_with_inquiry_results(void) {
162   return GetController()->GetControllerLocalExtendedFeatures(kPageZero) &
163          BIT(28);
164 }
165 
supports_extended_inquiry_response(void)166 static bool supports_extended_inquiry_response(void) {
167   return GetController()->GetControllerLocalExtendedFeatures(kPageZero) &
168          BIT(48);
169 }
170 
supports_master_slave_role_switch(void)171 static bool supports_master_slave_role_switch(void) {
172   return GetController()->GetControllerLocalExtendedFeatures(kPageZero) &
173          BIT(5);
174 }
175 
supports_enhanced_setup_synchronous_connection(void)176 static bool supports_enhanced_setup_synchronous_connection(void) {
177   return GetController()->IsSupported(
178       (bluetooth::hci::OpCode)kEnhancedSetupSynchronousConnection);
179 }
180 
supports_enhanced_accept_synchronous_connection(void)181 static bool supports_enhanced_accept_synchronous_connection(void) {
182   return GetController()->IsSupported(
183       (bluetooth::hci::OpCode)kEnhancedAcceptSynchronousConnection);
184 }
185 
supports_ble(void)186 static bool supports_ble(void) {
187   return GetController()->GetControllerLocalExtendedFeatures(kPageOne) & BIT(1);
188 }
189 
supports_ble_privacy(void)190 static bool supports_ble_privacy(void) {
191   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(6);
192 }
193 
supports_ble_set_privacy_mode()194 static bool supports_ble_set_privacy_mode() {
195   return GetController()->IsSupported(
196       (bluetooth::hci::OpCode)kLeSetPrivacyMode);
197 }
198 
supports_ble_packet_extension(void)199 static bool supports_ble_packet_extension(void) {
200   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(5);
201 }
202 
supports_ble_connection_parameters_request(void)203 static bool supports_ble_connection_parameters_request(void) {
204   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(2);
205 }
206 
supports_ble_2m_phy(void)207 static bool supports_ble_2m_phy(void) {
208   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(8);
209 }
210 
supports_ble_coded_phy(void)211 static bool supports_ble_coded_phy(void) {
212   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(11);
213 }
214 
supports_ble_extended_advertising(void)215 static bool supports_ble_extended_advertising(void) {
216   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(12);
217 }
218 
supports_ble_periodic_advertising(void)219 static bool supports_ble_periodic_advertising(void) {
220   return GetController()->GetControllerLeLocalSupportedFeatures() & BIT(13);
221 }
222 
get_acl_data_size_classic(void)223 static uint16_t get_acl_data_size_classic(void) {
224   return GetController()->GetControllerAclPacketLength();
225 }
226 
get_acl_data_size_ble(void)227 static uint16_t get_acl_data_size_ble(void) {
228   ::bluetooth::hci::LeBufferSize le_buffer_size =
229       GetController()->GetControllerLeBufferSize();
230   return le_buffer_size.le_data_packet_length_;
231 }
232 
get_acl_packet_size_classic(void)233 static uint16_t get_acl_packet_size_classic(void) {
234   return get_acl_data_size_classic() + kHciDataPreambleSize;
235 }
236 
get_acl_packet_size_ble(void)237 static uint16_t get_acl_packet_size_ble(void) {
238   return get_acl_data_size_ble() + kHciDataPreambleSize;
239 }
240 
get_ble_suggested_default_data_length(void)241 static uint16_t get_ble_suggested_default_data_length(void) {
242   LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
243   return 0;
244 }
245 
get_ble_maximum_tx_data_length(void)246 static uint16_t get_ble_maximum_tx_data_length(void) {
247   ::bluetooth::hci::LeMaximumDataLength le_maximum_data_length =
248       GetController()->GetControllerLeMaximumDataLength();
249   return le_maximum_data_length.supported_max_tx_octets_;
250 }
251 
get_ble_maxium_advertising_data_length(void)252 static uint16_t get_ble_maxium_advertising_data_length(void) {
253   LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
254   return 0;
255 }
256 
get_ble_number_of_supported_advertising_sets(void)257 static uint8_t get_ble_number_of_supported_advertising_sets(void) {
258   return GetController()->GetControllerLeNumberOfSupportedAdverisingSets();
259 }
260 
get_acl_buffer_count_classic(void)261 static uint16_t get_acl_buffer_count_classic(void) {
262   return GetController()->GetControllerNumAclPacketBuffers();
263 }
264 
get_acl_buffer_count_ble(void)265 static uint8_t get_acl_buffer_count_ble(void) {
266   LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
267   return 0;
268 }
269 
get_ble_white_list_size(void)270 static uint8_t get_ble_white_list_size(void) {
271   LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
272   return 0;
273 }
274 
get_ble_resolving_list_max_size(void)275 static uint8_t get_ble_resolving_list_max_size(void) {
276   LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
277   return 0;
278 }
279 
set_ble_resolving_list_max_size(int resolving_list_max_size)280 static void set_ble_resolving_list_max_size(int resolving_list_max_size) {
281   LOG_WARN(LOG_TAG, "%s TODO Unimplemented", __func__);
282 }
283 
get_le_all_initiating_phys()284 static uint8_t get_le_all_initiating_phys() { return data_.phy; }
285 
286 static const controller_t interface = {
287     get_is_ready,
288 
289     get_address,
290     get_bt_version,
291 
292     get_features_classic,
293     get_last_features_classic_index,
294 
295     get_features_ble,
296     get_ble_supported_states,
297 
298     supports_simple_pairing,
299     supports_secure_connections,
300     supports_simultaneous_le_bredr,
301     supports_reading_remote_extended_features,
302     supports_interlaced_inquiry_scan,
303     supports_rssi_with_inquiry_results,
304     supports_extended_inquiry_response,
305     supports_master_slave_role_switch,
306     supports_enhanced_setup_synchronous_connection,
307     supports_enhanced_accept_synchronous_connection,
308 
309     supports_ble,
310     supports_ble_packet_extension,
311     supports_ble_connection_parameters_request,
312     supports_ble_privacy,
313     supports_ble_set_privacy_mode,
314     supports_ble_2m_phy,
315     supports_ble_coded_phy,
316     supports_ble_extended_advertising,
317     supports_ble_periodic_advertising,
318 
319     get_acl_data_size_classic,
320     get_acl_data_size_ble,
321 
322     get_acl_packet_size_classic,
323     get_acl_packet_size_ble,
324     get_ble_suggested_default_data_length,
325     get_ble_maximum_tx_data_length,
326     get_ble_maxium_advertising_data_length,
327     get_ble_number_of_supported_advertising_sets,
328 
329     get_acl_buffer_count_classic,
330     get_acl_buffer_count_ble,
331 
332     get_ble_white_list_size,
333 
334     get_ble_resolving_list_max_size,
335     set_ble_resolving_list_max_size,
336     get_local_supported_codecs,
337     get_le_all_initiating_phys};
338 
controller_get_interface()339 const controller_t* bluetooth::shim::controller_get_interface() {
340   static bool loaded = false;
341   if (!loaded) {
342     loaded = true;
343   }
344   return &interface;
345 }
346