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 | |
| 23 | #include "hal/hci_hal.h" |
| 24 | #include "os/handler.h" |
| 25 | #include "os/log.h" |
| 26 | |
| 27 | using ::bluetooth::hal::BluetoothHciHalCallbacks; |
| 28 | using ::bluetooth::hal::BluetoothInitializationCompleteCallback; |
| 29 | using ::bluetooth::hal::HciPacket; |
| 30 | using ::bluetooth::hal::Status; |
| 31 | using ::bluetooth::os::Handler; |
| 32 | using ::bluetooth::os::Thread; |
| 33 | |
| 34 | namespace bluetooth { |
| 35 | namespace { |
| 36 | std::promise<void>* startup_promise; |
| 37 | |
| 38 | class InitCallback : public BluetoothInitializationCompleteCallback { |
| 39 | public: |
| 40 | void initializationComplete(Status status) override { |
| 41 | ASSERT(status == Status::SUCCESS); |
| 42 | startup_promise->set_value(); |
| 43 | } |
| 44 | } init_callback; |
| 45 | |
| 46 | Thread* main_thread_; |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | void StackManager::StartUp() { |
| 51 | startup_promise = new std::promise<void>; |
| 52 | ::bluetooth::hal::GetBluetoothHciHal()->initialize(&init_callback); |
| 53 | auto init_status = startup_promise->get_future().wait_for(std::chrono::seconds(3)); |
| 54 | ASSERT_LOG(init_status == std::future_status::ready, "Can't initialize HCI HAL"); |
| 55 | delete startup_promise; |
| 56 | |
| 57 | main_thread_ = new Thread("main_thread", Thread::Priority::NORMAL); |
| 58 | |
| 59 | LOG_INFO("init complete"); |
| 60 | // Bring up HCI layer |
| 61 | } |
| 62 | |
| 63 | void StackManager::ShutDown() { |
| 64 | // Delete HCI layer |
| 65 | delete main_thread_; |
| 66 | ::bluetooth::hal::GetBluetoothHciHal()->close(); |
| 67 | } |
| 68 | } // namespace bluetooth |