Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 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 <chrono> |
| 20 | #include <future> |
| 21 | #include <queue> |
| 22 | |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 23 | #include "common/bind.h" |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 24 | #include "hal/hci_hal.h" |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 25 | #include "module.h" |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 26 | #include "os/handler.h" |
| 27 | #include "os/log.h" |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 28 | #include "os/thread.h" |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 29 | |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 30 | using ::bluetooth::os::Handler; |
| 31 | using ::bluetooth::os::Thread; |
| 32 | |
| 33 | namespace bluetooth { |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 34 | |
Zach Johnson | 7ea2068 | 2019-04-29 14:48:42 -0700 | [diff] [blame] | 35 | void StackManager::StartUp(ModuleList* modules, Thread* stack_thread) { |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 36 | management_thread_ = new Thread("management_thread", Thread::Priority::NORMAL); |
| 37 | handler_ = new Handler(management_thread_); |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 38 | |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 39 | 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 Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 43 | |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 44 | auto init_status = future.wait_for(std::chrono::seconds(3)); |
| 45 | ASSERT_LOG(init_status == std::future_status::ready, "Can't start stack"); |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 46 | |
| 47 | LOG_INFO("init complete"); |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 50 | void StackManager::handle_start_up(ModuleList* modules, Thread* stack_thread, std::promise<void> promise) { |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 51 | registry_.Start(modules, stack_thread); |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 52 | promise.set_value(); |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 55 | void StackManager::ShutDown() { |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 56 | 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 Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 59 | |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 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 | |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 63 | handler_->Clear(); |
| 64 | handler_->WaitUntilStopped(std::chrono::milliseconds(20)); |
Zach Johnson | e0e158c | 2019-04-26 11:57:05 -0700 | [diff] [blame] | 65 | delete handler_; |
| 66 | delete management_thread_; |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 67 | } |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 68 | |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 69 | void StackManager::handle_shut_down(std::promise<void> promise) { |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 70 | registry_.StopAll(); |
Hansong Zhang | 0bce6f7 | 2019-06-13 18:31:40 -0700 | [diff] [blame] | 71 | promise.set_value(); |
Hansong Zhang | aa0875c | 2019-06-06 21:44:55 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Hansong Zhang | 3fa05ab | 2019-04-09 08:55:27 -0700 | [diff] [blame] | 74 | } // namespace bluetooth |