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 <stdio.h>
20 #include <chrono>
21 #include <future>
22 #include <queue>
23
24 #include "common/bind.h"
25 #include "module.h"
26 #include "os/handler.h"
27 #include "os/log.h"
28 #include "os/thread.h"
29 #include "os/wakelock_manager.h"
30
31 using ::bluetooth::os::Handler;
32 using ::bluetooth::os::Thread;
33 using ::bluetooth::os::WakelockManager;
34
35 namespace bluetooth {
36
StartUp(ModuleList * modules,Thread * stack_thread)37 void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
38 management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
39 handler_ = new Handler(management_thread_);
40
41 WakelockManager::Get().Acquire();
42
43 std::promise<void> promise;
44 auto future = promise.get_future();
45 handler_->Post(common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread,
46 std::move(promise)));
47
48 auto init_status = future.wait_for(std::chrono::seconds(3));
49
50 WakelockManager::Get().Release();
51
52 ASSERT_LOG(
53 init_status == std::future_status::ready,
54 "Can't start stack, last instance: %s",
55 registry_.last_instance_.c_str());
56
57 LOG_INFO("init complete");
58 }
59
handle_start_up(ModuleList * modules,Thread * stack_thread,std::promise<void> promise)60 void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void> promise) {
61 registry_.Start(modules, stack_thread);
62 promise.set_value();
63 }
64
ShutDown()65 void StackManager::ShutDown() {
66 WakelockManager::Get().Acquire();
67
68 std::promise<void> promise;
69 auto future = promise.get_future();
70 handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), std::move(promise)));
71
72 auto stop_status = future.wait_for(std::chrono::seconds(5));
73
74 WakelockManager::Get().Release();
75 WakelockManager::Get().CleanUp();
76
77 ASSERT_LOG(
78 stop_status == std::future_status::ready,
79 "Can't stop stack, last instance: %s",
80 registry_.last_instance_.c_str());
81
82 handler_->Clear();
83 handler_->WaitUntilStopped(std::chrono::milliseconds(2000));
84 delete handler_;
85 delete management_thread_;
86 }
87
handle_shut_down(std::promise<void> promise)88 void StackManager::handle_shut_down(std::promise<void> promise) {
89 registry_.StopAll();
90 promise.set_value();
91 }
92
93 } // namespace bluetooth
94