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_stack_manager"
20
21 #include "btif/include/stack_manager.h"
22
23 #include <hardware/bluetooth.h>
24 #include <cstdlib>
25 #include <cstring>
26
27 #include "btcore/include/module.h"
28 #include "btcore/include/osi_module.h"
29 #include "btif_api.h"
30 #include "btif_common.h"
31 #include "common/message_loop_thread.h"
32 #include "main/shim/shim.h"
33 #include "osi/include/log.h"
34 #include "osi/include/osi.h"
35 #include "osi/include/semaphore.h"
36 #include "stack/include/acl_api.h"
37 #include "stack/include/btm_client_interface.h"
38 #include "stack/include/btu.h"
39
40 // Temp includes
41 #include "bt_utils.h"
42 #include "bta/sys/bta_sys.h"
43 #include "btif_config.h"
44 #include "btif_profile_queue.h"
45 #include "internal_include/bt_target.h"
46 #include "stack/include/gatt_api.h"
47 #include "stack/include/l2c_api.h"
48 #include "stack/include/port_api.h"
49 #include "stack/sdp/sdpint.h"
50 #if (BNEP_INCLUDED == TRUE)
51 #include "stack/include/bnep_api.h"
52 #endif
53 #include "stack/include/gap_api.h"
54 #if (PAN_INCLUDED == TRUE)
55 #include "stack/include/pan_api.h"
56 #endif
57 #include "stack/include/a2dp_api.h"
58 #include "stack/include/avrc_api.h"
59 #if (HID_HOST_INCLUDED == TRUE)
60 #include "stack/include/hidh_api.h"
61 #endif
62 #include "bta/sys/bta_sys_int.h"
63 #include "bta_ar_api.h"
64 #include "bta_dm_int.h"
65 #include "btif/include/btif_pan.h"
66 #include "btif/include/btif_sock.h"
67 #include "btm_ble_int.h"
68 #include "device/include/interop.h"
69 #include "internal_include/stack_config.h"
70 #include "main/shim/controller.h"
71 #include "stack/include/smp_api.h"
72
73 #ifndef BT_STACK_CLEANUP_WAIT_MS
74 #define BT_STACK_CLEANUP_WAIT_MS 1000
75 #endif
76
77 // Validate or respond to various conditional compilation flags
78
79 #if SDP_RAW_DATA_INCLUDED != TRUE
80 // Once SDP_RAW_DATA_INCLUDED is no longer exposed via bt_target.h
81 // this check and error statement may be removed.
82 #warning \
83 "#define SDP_RAW_DATA_INCLUDED preprocessor compilation flag is unsupported"
84 #error "*** Conditional Compilation Directive error"
85 #endif
86
87 // Once BTA_PAN_INCLUDED is no longer exposed via bt_target.h
88 // this check and error statement may be removed.
89 static_assert(
90 BTA_PAN_INCLUDED,
91 "#define BTA_PAN_INCLUDED preprocessor compilation flag is unsupported"
92 " Pan profile is always included in the bluetooth stack"
93 "*** Conditional Compilation Directive error");
94
95 // Once PAN_SUPPORTS_ROLE_NAP is no longer exposed via bt_target.h
96 // this check and error statement may be removed.
97 static_assert(
98 PAN_SUPPORTS_ROLE_NAP,
99 "#define PAN_SUPPORTS_ROLE_NAP preprocessor compilation flag is unsupported"
100 " Pan profile always supports network access point in the bluetooth stack"
101 "*** Conditional Compilation Directive error");
102
103 // Once PAN_SUPPORTS_ROLE_PANU is no longer exposed via bt_target.h
104 // this check and error statement may be removed.
105 static_assert(
106 PAN_SUPPORTS_ROLE_PANU,
107 "#define PAN_SUPPORTS_ROLE_PANU preprocessor compilation flag is "
108 "unsupported"
109 " Pan profile always supports user as a client in the bluetooth stack"
110 "*** Conditional Compilation Directive error");
111
112 // Once BTA_HH_INCLUDED is no longer exposed via bt_target.h
113 // this check and error statement may be removed.
114 static_assert(
115 BTA_HH_INCLUDED,
116 "#define BTA_HH_INCLUDED preprocessor compilation flag is "
117 "unsupported"
118 " Host interface device profile is always enabled in the bluetooth stack"
119 "*** Conditional Compilation Directive error");
120
121 void main_thread_shut_down();
122 void main_thread_start_up();
123 void BTA_dm_on_hw_on();
124 void BTA_dm_on_hw_off();
125
126 using bluetooth::common::MessageLoopThread;
127
128 static MessageLoopThread management_thread("bt_stack_manager_thread");
129
130 // If initialized, any of the bluetooth API functions can be called.
131 // (e.g. turning logging on and off, enabling/disabling the stack, etc)
132 static bool stack_is_initialized;
133 // If running, the stack is fully up and able to bluetooth.
134 static bool stack_is_running;
135
136 static void event_init_stack(void* context);
137 static void event_start_up_stack(void* context);
138 static void event_shut_down_stack(void* context);
139 static void event_clean_up_stack(std::promise<void> promise);
140
141 static void event_signal_stack_up(void* context);
142 static void event_signal_stack_down(void* context);
143
144 // Unvetted includes/imports, etc which should be removed or vetted in the
145 // future
146 static future_t* hack_future;
147 // End unvetted section
148
149 // Interface functions
150
init_stack()151 static void init_stack() {
152 // This is a synchronous process. Post it to the thread though, so
153 // state modification only happens there. Using the thread to perform
154 // all stack operations ensures that the operations are done serially
155 // and do not overlap.
156 semaphore_t* semaphore = semaphore_new(0);
157 management_thread.DoInThread(FROM_HERE,
158 base::Bind(event_init_stack, semaphore));
159 semaphore_wait(semaphore);
160 semaphore_free(semaphore);
161 }
162
start_up_stack_async()163 static void start_up_stack_async() {
164 management_thread.DoInThread(FROM_HERE,
165 base::Bind(event_start_up_stack, nullptr));
166 }
167
shut_down_stack_async()168 static void shut_down_stack_async() {
169 management_thread.DoInThread(FROM_HERE,
170 base::Bind(event_shut_down_stack, nullptr));
171 }
172
clean_up_stack()173 static void clean_up_stack() {
174 // This is a synchronous process. Post it to the thread though, so
175 // state modification only happens there.
176 std::promise<void> promise;
177 auto future = promise.get_future();
178 management_thread.DoInThread(
179 FROM_HERE, base::BindOnce(event_clean_up_stack, std::move(promise)));
180
181 auto status =
182 future.wait_for(std::chrono::milliseconds(BT_STACK_CLEANUP_WAIT_MS));
183 if (status == std::future_status::ready) {
184 management_thread.ShutDown();
185 } else {
186 LOG_ERROR("cleanup could not be completed in time, abandon it");
187 }
188 }
189
get_stack_is_running()190 static bool get_stack_is_running() { return stack_is_running; }
191
192 // Internal functions
193 extern const module_t bt_utils_module;
194 extern const module_t bte_logmsg_module;
195 extern const module_t btif_config_module;
196 extern const module_t bt_utils_module;
197 extern const module_t gd_controller_module;
198 extern const module_t gd_idle_module;
199 extern const module_t gd_shim_module;
200 extern const module_t interop_module;
201 extern const module_t osi_module;
202 extern const module_t stack_config_module;
203
204 struct module_lookup {
205 const char* name;
206 const module_t* module;
207 };
208
209 const struct module_lookup module_table[] = {
210 {BTE_LOGMSG_MODULE, &bte_logmsg_module},
211 {BTIF_CONFIG_MODULE, &btif_config_module},
212 {BT_UTILS_MODULE, &bt_utils_module},
213 {GD_CONTROLLER_MODULE, &gd_controller_module},
214 {GD_IDLE_MODULE, &gd_idle_module},
215 {GD_SHIM_MODULE, &gd_shim_module},
216 {INTEROP_MODULE, &interop_module},
217 {OSI_MODULE, &osi_module},
218 {STACK_CONFIG_MODULE, &stack_config_module},
219 {NULL, NULL},
220 };
221
get_local_module(const char * name)222 inline const module_t* get_local_module(const char* name) {
223 size_t len = strlen(name);
224
225 for (const struct module_lookup* l = module_table; l->module; l++) {
226 if (strncmp(l->name, name, len) == 0) {
227 return l->module;
228 }
229 }
230
231 LOG_ALWAYS_FATAL("Cannot find module %s, aborting", name);
232 return nullptr;
233 }
234
235 // Synchronous function to initialize the stack
event_init_stack(void * context)236 static void event_init_stack(void* context) {
237 semaphore_t* semaphore = (semaphore_t*)context;
238
239 LOG_INFO("is initializing the stack");
240
241 if (stack_is_initialized) {
242 LOG_INFO("found the stack already in initialized state");
243 } else {
244 module_management_start();
245
246 module_init(get_local_module(OSI_MODULE));
247 module_init(get_local_module(BT_UTILS_MODULE));
248 module_start_up(get_local_module(GD_IDLE_MODULE));
249 module_init(get_local_module(BTIF_CONFIG_MODULE));
250 btif_init_bluetooth();
251
252 module_init(get_local_module(INTEROP_MODULE));
253 bte_main_init();
254 module_init(get_local_module(STACK_CONFIG_MODULE));
255
256 // stack init is synchronous, so no waiting necessary here
257 stack_is_initialized = true;
258 }
259
260 LOG_INFO("finished");
261
262 if (semaphore) semaphore_post(semaphore);
263 }
264
ensure_stack_is_initialized()265 static void ensure_stack_is_initialized() {
266 if (!stack_is_initialized) {
267 LOG_WARN("%s found the stack was uninitialized. Initializing now.",
268 __func__);
269 // No semaphore needed since we are calling it directly
270 event_init_stack(nullptr);
271 }
272 }
273
274 // Synchronous function to start up the stack
event_start_up_stack(UNUSED_ATTR void * context)275 static void event_start_up_stack(UNUSED_ATTR void* context) {
276 if (stack_is_running) {
277 LOG_INFO("%s stack already brought up", __func__);
278 return;
279 }
280
281 ensure_stack_is_initialized();
282
283 LOG_INFO("%s is bringing up the stack", __func__);
284 future_t* local_hack_future = future_new();
285 hack_future = local_hack_future;
286
287 LOG_INFO("%s Gd shim module enabled", __func__);
288 module_shut_down(get_local_module(GD_IDLE_MODULE));
289 get_btm_client_interface().lifecycle.btm_init();
290 module_start_up(get_local_module(GD_SHIM_MODULE));
291 module_start_up(get_local_module(BTIF_CONFIG_MODULE));
292
293 l2c_init();
294 sdp_init();
295 gatt_init();
296 SMP_Init();
297 get_btm_client_interface().lifecycle.btm_ble_init();
298
299 RFCOMM_Init();
300 #if (BNEP_INCLUDED == TRUE)
301 BNEP_Init();
302 #if (PAN_INCLUDED == TRUE)
303 PAN_Init();
304 #endif /* PAN */
305 #endif /* BNEP Included */
306 A2DP_Init();
307 AVRC_Init();
308 GAP_Init();
309 #if (HID_HOST_INCLUDED == TRUE)
310 HID_HostInit();
311 #endif
312
313 bta_sys_init();
314 bta_ar_init();
315 module_init(get_local_module(BTE_LOGMSG_MODULE));
316
317 main_thread_start_up();
318
319 btif_init_ok();
320 BTA_dm_init();
321 bta_dm_enable(bte_dm_evt);
322
323 bta_set_forward_hw_failures(true);
324 btm_acl_device_down();
325 CHECK(module_start_up(get_local_module(GD_CONTROLLER_MODULE)));
326 BTM_reset_complete();
327
328 BTA_dm_on_hw_on();
329
330 if (future_await(local_hack_future) != FUTURE_SUCCESS) {
331 LOG_ERROR("%s failed to start up the stack", __func__);
332 stack_is_running = true; // So stack shutdown actually happens
333 event_shut_down_stack(nullptr);
334 return;
335 }
336
337 stack_is_running = true;
338 LOG_INFO("%s finished", __func__);
339 do_in_jni_thread(FROM_HERE, base::Bind(event_signal_stack_up, nullptr));
340 }
341
342 // Synchronous function to shut down the stack
event_shut_down_stack(UNUSED_ATTR void * context)343 static void event_shut_down_stack(UNUSED_ATTR void* context) {
344 if (!stack_is_running) {
345 LOG_INFO("%s stack is already brought down", __func__);
346 return;
347 }
348
349 LOG_INFO("%s is bringing down the stack", __func__);
350 future_t* local_hack_future = future_new();
351 hack_future = local_hack_future;
352 stack_is_running = false;
353
354 do_in_main_thread(FROM_HERE, base::Bind(&btm_ble_multi_adv_cleanup));
355
356 do_in_main_thread(FROM_HERE, base::Bind(&btm_ble_scanner_cleanup));
357
358 btif_dm_on_disable();
359 btif_sock_cleanup();
360 btif_pan_cleanup();
361
362 do_in_main_thread(FROM_HERE, base::Bind(bta_dm_disable));
363
364 future_await(local_hack_future);
365 local_hack_future = future_new();
366 hack_future = local_hack_future;
367
368 bta_sys_disable();
369 bta_set_forward_hw_failures(false);
370 BTA_dm_on_hw_off();
371
372 module_shut_down(get_local_module(BTIF_CONFIG_MODULE));
373
374 future_await(local_hack_future);
375
376 main_thread_shut_down();
377
378 module_clean_up(get_local_module(BTE_LOGMSG_MODULE));
379
380 gatt_free();
381 l2c_free();
382 sdp_free();
383 get_btm_client_interface().lifecycle.btm_ble_free();
384
385 LOG_INFO("%s Gd shim module disabled", __func__);
386 module_shut_down(get_local_module(GD_SHIM_MODULE));
387 get_btm_client_interface().lifecycle.btm_free();
388 module_start_up(get_local_module(GD_IDLE_MODULE));
389
390 hack_future = future_new();
391 do_in_jni_thread(FROM_HERE, base::Bind(event_signal_stack_down, nullptr));
392 future_await(hack_future);
393 LOG_INFO("%s finished", __func__);
394 }
395
ensure_stack_is_not_running()396 static void ensure_stack_is_not_running() {
397 if (stack_is_running) {
398 LOG_WARN("%s found the stack was still running. Bringing it down now.",
399 __func__);
400 event_shut_down_stack(nullptr);
401 }
402 }
403
404 // Synchronous function to clean up the stack
event_clean_up_stack(std::promise<void> promise)405 static void event_clean_up_stack(std::promise<void> promise) {
406 if (!stack_is_initialized) {
407 LOG_INFO("%s found the stack already in a clean state", __func__);
408 goto cleanup;
409 }
410
411 ensure_stack_is_not_running();
412
413 LOG_INFO("%s is cleaning up the stack", __func__);
414 stack_is_initialized = false;
415
416 btif_cleanup_bluetooth();
417
418 module_clean_up(get_local_module(STACK_CONFIG_MODULE));
419 module_clean_up(get_local_module(INTEROP_MODULE));
420
421 module_clean_up(get_local_module(BTIF_CONFIG_MODULE));
422 module_clean_up(get_local_module(BT_UTILS_MODULE));
423 module_clean_up(get_local_module(OSI_MODULE));
424 module_shut_down(get_local_module(GD_IDLE_MODULE));
425 module_management_stop();
426 LOG_INFO("%s finished", __func__);
427
428 cleanup:;
429 promise.set_value();
430 }
431
event_signal_stack_up(UNUSED_ATTR void * context)432 static void event_signal_stack_up(UNUSED_ATTR void* context) {
433 // Notify BTIF connect queue that we've brought up the stack. It's
434 // now time to dispatch all the pending profile connect requests.
435 btif_queue_connect_next();
436 invoke_adapter_state_changed_cb(BT_STATE_ON);
437 }
438
event_signal_stack_down(UNUSED_ATTR void * context)439 static void event_signal_stack_down(UNUSED_ATTR void* context) {
440 invoke_adapter_state_changed_cb(BT_STATE_OFF);
441 future_ready(stack_manager_get_hack_future(), FUTURE_SUCCESS);
442 }
443
ensure_manager_initialized()444 static void ensure_manager_initialized() {
445 if (management_thread.IsRunning()) return;
446
447 management_thread.StartUp();
448 if (!management_thread.IsRunning()) {
449 LOG_ERROR("%s unable to start stack management thread", __func__);
450 return;
451 }
452 }
453
454 static const stack_manager_t interface = {init_stack, start_up_stack_async,
455 shut_down_stack_async, clean_up_stack,
456 get_stack_is_running};
457
stack_manager_get_interface()458 const stack_manager_t* stack_manager_get_interface() {
459 ensure_manager_initialized();
460 return &interface;
461 }
462
stack_manager_get_hack_future()463 future_t* stack_manager_get_hack_future() { return hack_future; }
464