blob: f06bf87ccb0832ab77f5d07a57236f9ef5f3febf [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"
24#include "os/handler.h"
25#include "os/log.h"
26
27using ::bluetooth::hal::BluetoothHciHalCallbacks;
28using ::bluetooth::hal::BluetoothInitializationCompleteCallback;
29using ::bluetooth::hal::HciPacket;
30using ::bluetooth::hal::Status;
31using ::bluetooth::os::Handler;
32using ::bluetooth::os::Thread;
33
34namespace bluetooth {
35namespace {
36std::promise<void>* startup_promise;
37
38class 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
46Thread* main_thread_;
47
48} // namespace
49
50void 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
63void StackManager::ShutDown() {
64 // Delete HCI layer
65 delete main_thread_;
66 ::bluetooth::hal::GetBluetoothHciHal()->close();
67}
68} // namespace bluetooth