blob: ad83674272538f4bdd62b4707940a351495cfc75 [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
23#include "hal/hci_hal.h"
Zach Johnsone0e158c2019-04-26 11:57:05 -070024#include "os/thread.h"
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070025#include "os/handler.h"
26#include "os/log.h"
Zach Johnsone0e158c2019-04-26 11:57:05 -070027#include "module.h"
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070028
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070029using ::bluetooth::os::Handler;
30using ::bluetooth::os::Thread;
31
32namespace bluetooth {
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070033
Zach Johnson7ea20682019-04-29 14:48:42 -070034void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) {
Zach Johnsone0e158c2019-04-26 11:57:05 -070035 management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL);
36 handler_ = new Handler(management_thread_);
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070037
Zach Johnsone0e158c2019-04-26 11:57:05 -070038 std::promise<void>* promise = new std::promise<void>();
Zach Johnson7ea20682019-04-29 14:48:42 -070039 handler_->Post([this, promise, modules, stack_thread]() {
40 registry_.Start(modules, stack_thread);
Zach Johnsone0e158c2019-04-26 11:57:05 -070041 promise->set_value();
42 });
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070043
Zach Johnsone0e158c2019-04-26 11:57:05 -070044 auto future = promise->get_future();
45 auto init_status = future.wait_for(std::chrono::seconds(3));
46 ASSERT_LOG(init_status == std::future_status::ready, "Can't start stack");
47 delete promise;
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070048
49 LOG_INFO("init complete");
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070050}
51
52void StackManager::ShutDown() {
Zach Johnsone0e158c2019-04-26 11:57:05 -070053 std::promise<void>* promise = new std::promise<void>();
54 handler_->Post([this, promise]() {
55 registry_.StopAll();
56 promise->set_value();
57 });
58
59 auto future = promise->get_future();
60 auto stop_status = future.wait_for(std::chrono::seconds(3));
61 ASSERT_LOG(stop_status == std::future_status::ready, "Can't stop stack");
62
63 delete promise;
64 delete handler_;
65 delete management_thread_;
Hansong Zhang3fa05ab2019-04-09 08:55:27 -070066}
67} // namespace bluetooth