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