blob: 9c01d5641eea0e37bc8a9cee978c7f11ac85f721 [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
Hansong Zhang0bce6f72019-06-13 18:31:40 -070039 std::promise<void> promise;
40 auto future = promise.get_future();
41 handler_->Post(common::BindOnce(&StackManager::handle_start_up, common::Unretained(this), modules, stack_thread,
42 std::move(promise)));
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070043
Zach Johnsone0e158c2019-04-26 11:57:05 -070044 auto init_status = future.wait_for(std::chrono::seconds(3));
45 ASSERT_LOG(init_status == std::future_status::ready, "Can't start stack");
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070046
47 LOG_INFO("init complete");
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070048}
49
Hansong Zhang0bce6f72019-06-13 18:31:40 -070050void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void> promise) {
Hansong Zhangaa0875c2019-06-06 21:44:55 -070051 registry_.Start(modules, stack_thread);
Hansong Zhang0bce6f72019-06-13 18:31:40 -070052 promise.set_value();
Hansong Zhangaa0875c2019-06-06 21:44:55 -070053}
54
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070055void StackManager::ShutDown() {
Hansong Zhang0bce6f72019-06-13 18:31:40 -070056 std::promise<void> promise;
57 auto future = promise.get_future();
58 handler_->Post(common::BindOnce(&StackManager::handle_shut_down, common::Unretained(this), std::move(promise)));
Zach Johnsone0e158c2019-04-26 11:57:05 -070059
Zach Johnsone0e158c2019-04-26 11:57:05 -070060 auto stop_status = future.wait_for(std::chrono::seconds(3));
61 ASSERT_LOG(stop_status == std::future_status::ready, "Can't stop stack");
62
Hansong Zhang0bce6f72019-06-13 18:31:40 -070063 handler_->Clear();
64 handler_->WaitUntilStopped(std::chrono::milliseconds(20));
Zach Johnsone0e158c2019-04-26 11:57:05 -070065 delete handler_;
66 delete management_thread_;
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070067}
Hansong Zhangaa0875c2019-06-06 21:44:55 -070068
Hansong Zhang0bce6f72019-06-13 18:31:40 -070069void StackManager::handle_shut_down(std::promise<void> promise) {
Hansong Zhangaa0875c2019-06-06 21:44:55 -070070 registry_.StopAll();
Hansong Zhang0bce6f72019-06-13 18:31:40 -070071 promise.set_value();
Hansong Zhangaa0875c2019-06-06 21:44:55 -070072}
73
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070074} // namespace bluetooth