blob: 7cfaaded34ea44252edb695fbf57f81c92acbd87 [file] [log] [blame]
Hansong Zhang3fa05ab2019-04-09 08:55:27 -07001/*
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 <chrono>
20#include <future>
21#include <queue>
22
Hansong Zhangaa0875c2019-06-06 21:44:55 -070023#include "common/bind.h"
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070024#include "hal/hci_hal.h"
Hansong Zhangaa0875c2019-06-06 21:44:55 -070025#include "module.h"
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070026#include "os/handler.h"
27#include "os/log.h"
Hansong Zhangaa0875c2019-06-06 21:44:55 -070028#include "os/thread.h"
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070029
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070030using ::bluetooth::os::Handler;
31using ::bluetooth::os::Thread;
32
33namespace bluetooth {
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070034
Zach Johnson7ea20682019-04-29 14:48:42 -070035void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
Zach Johnsone0e158c2019-04-26 11:57:05 -070036 management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
37 handler_ = new Handler(management_thread_);
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070038
Zach Johnsone0e158c2019-04-26 11:57:05 -070039 std::promise<void>* promise = new std::promise<void>();
Hansong Zhangaa0875c2019-06-06 21:44:55 -070040 handler_->Post(
41 common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread, promise));
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070042
Zach Johnsone0e158c2019-04-26 11:57:05 -070043 auto future = promise->get_future();
44 auto init_status = future.wait_for(std::chrono::seconds(3));
45 ASSERT_LOG(init_status == std::future_status::ready, "Can't start stack");
46 delete promise;
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070047
48 LOG_INFO("init complete");
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070049}
50
Hansong Zhangaa0875c2019-06-06 21:44:55 -070051void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void>* promise) {
52 registry_.Start(modules, stack_thread);
53 promise->set_value();
54}
55
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070056void StackManager::ShutDown() {
Zach Johnsone0e158c2019-04-26 11:57:05 -070057 std::promise<void>* promise = new std::promise<void>();
Hansong Zhangaa0875c2019-06-06 21:44:55 -070058 handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), promise));
Zach Johnsone0e158c2019-04-26 11:57:05 -070059
60 auto future = promise->get_future();
61 auto stop_status = future.wait_for(std::chrono::seconds(3));
62 ASSERT_LOG(stop_status == std::future_status::ready, "Can't stop stack");
63
64 delete promise;
65 delete handler_;
66 delete management_thread_;
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070067}
Hansong Zhangaa0875c2019-06-06 21:44:55 -070068
69void StackManager::handle_shut_down(std::promise<void>* promise) {
70 registry_.StopAll();
71 promise->set_value();
72}
73
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070074} // namespace bluetooth