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 #include "stack_manager.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include <chrono>
22 #include <future>
23 #include <queue>
24 
25 #include "common/bind.h"
26 #include "module.h"
27 #include "os/handler.h"
28 #include "os/log.h"
29 #include "os/system_properties.h"
30 #include "os/thread.h"
31 #include "os/wakelock_manager.h"
32 
33 using ::bluetooth::os::Handler;
34 using ::bluetooth::os::Thread;
35 using ::bluetooth::os::WakelockManager;
36 
37 namespace bluetooth {
38 
StartUp(ModuleList * modules,Thread * stack_thread)39 void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
40   management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
41   handler_ = new Handler(management_thread_);
42 
43   WakelockManager::Get().Acquire();
44 
45   std::promise<void> promise;
46   auto future = promise.get_future();
47   handler_->Post(common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread,
48                                   std::move(promise)));
49 
50   auto init_status = future.wait_for(std::chrono::milliseconds(
51       get_gd_stack_timeout_ms(/* is_start = */ true)));
52 
53   WakelockManager::Get().Release();
54 
55   log::info("init_status == {}", int(init_status));
56 
57   log::assert_that(
58       init_status == std::future_status::ready,
59       "Can't start stack, last instance: {}",
60       registry_.last_instance_);
61 
62   log::info("init complete");
63 }
64 
handle_start_up(ModuleList * modules,Thread * stack_thread,std::promise<void> promise)65 void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void> promise) {
66   registry_.Start(modules, stack_thread);
67   promise.set_value();
68 }
69 
ShutDown()70 void StackManager::ShutDown() {
71   WakelockManager::Get().Acquire();
72 
73   std::promise<void> promise;
74   auto future = promise.get_future();
75   handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), std::move(promise)));
76 
77   auto stop_status = future.wait_for(std::chrono::milliseconds(
78       get_gd_stack_timeout_ms(/* is_start = */ false)));
79 
80   WakelockManager::Get().Release();
81   WakelockManager::Get().CleanUp();
82 
83   log::assert_that(
84       stop_status == std::future_status::ready,
85       "Can't stop stack, last instance: {}",
86       registry_.last_instance_);
87 
88   handler_->Clear();
89   handler_->WaitUntilStopped(std::chrono::milliseconds(2000));
90   delete handler_;
91   delete management_thread_;
92 }
93 
handle_shut_down(std::promise<void> promise)94 void StackManager::handle_shut_down(std::promise<void> promise) {
95   registry_.StopAll();
96   promise.set_value();
97 }
98 
get_gd_stack_timeout_ms(bool is_start)99 std::chrono::milliseconds StackManager::get_gd_stack_timeout_ms(bool is_start) {
100   auto gd_timeout = os::GetSystemPropertyUint32(
101         is_start ? "bluetooth.gd.start_timeout" : "bluetooth.gd.stop_timeout",
102         /* default_value = */ is_start ? 3000 : 5000);
103   return std::chrono::milliseconds(
104       gd_timeout * os::GetSystemPropertyUint32("ro.hw_timeout_multiplier",
105                                                /* default_value = */ 1));
106 }
107 
108 }  // namespace bluetooth
109