• 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 #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   LOG_INFO("init_status == %d", init_status);
53 
54   ASSERT_LOG(
55       init_status == std::future_status::ready,
56       "Can't start stack, last instance: %s",
57       registry_.last_instance_.c_str());
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, std::promise<void> promise) {
63   registry_.Start(modules, stack_thread);
64   promise.set_value();
65 }
66 
ShutDown()67 void StackManager::ShutDown() {
68   WakelockManager::Get().Acquire();
69 
70   std::promise<void> promise;
71   auto future = promise.get_future();
72   handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), std::move(promise)));
73 
74   auto stop_status = future.wait_for(std::chrono::seconds(5));
75 
76   WakelockManager::Get().Release();
77   WakelockManager::Get().CleanUp();
78 
79   ASSERT_LOG(
80       stop_status == std::future_status::ready,
81       "Can't stop stack, last instance: %s",
82       registry_.last_instance_.c_str());
83 
84   handler_->Clear();
85   handler_->WaitUntilStopped(std::chrono::milliseconds(2000));
86   delete handler_;
87   delete management_thread_;
88 }
89 
handle_shut_down(std::promise<void> promise)90 void StackManager::handle_shut_down(std::promise<void> promise) {
91   registry_.StopAll();
92   promise.set_value();
93 }
94 
95 }  // namespace bluetooth
96