Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cb6c5d3f authored by Hansong Zhang's avatar Hansong Zhang
Browse files

Simple stack bring up

Just bring up the HAL transportation layer

Test: unit test
Change-Id: I94641d3008ce155d13df6ffe78f3710b3efca645
parent 45253cbf
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -98,6 +98,7 @@ cc_library {
        },
    },
    srcs: [
        "stack_manager.cc",
        ":BluetoothCommonSources",
        ":BluetoothPacketSources",
    ]
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "stack_manager.h"

#include <chrono>
#include <future>
#include <queue>

#include "hal/hci_hal.h"
#include "os/handler.h"
#include "os/log.h"

using ::bluetooth::hal::BluetoothHciHalCallbacks;
using ::bluetooth::hal::BluetoothInitializationCompleteCallback;
using ::bluetooth::hal::HciPacket;
using ::bluetooth::hal::Status;
using ::bluetooth::os::Handler;
using ::bluetooth::os::Thread;

namespace bluetooth {
namespace {
std::promise<void>* startup_promise;

class InitCallback : public BluetoothInitializationCompleteCallback {
 public:
  void initializationComplete(Status status) override {
    ASSERT(status == Status::SUCCESS);
    startup_promise->set_value();
  }
} init_callback;

Thread* main_thread_;

}  // namespace

void StackManager::StartUp() {
  startup_promise = new std::promise<void>;
  ::bluetooth::hal::GetBluetoothHciHal()->initialize(&init_callback);
  auto init_status = startup_promise->get_future().wait_for(std::chrono::seconds(3));
  ASSERT_LOG(init_status == std::future_status::ready, "Can't initialize HCI HAL");
  delete startup_promise;

  main_thread_ = new Thread("main_thread", Thread::Priority::NORMAL);

  LOG_INFO("init complete");
  // Bring up HCI layer
}

void StackManager::ShutDown() {
  // Delete HCI layer
  delete main_thread_;
  ::bluetooth::hal::GetBluetoothHciHal()->close();
}
}  // namespace bluetooth
+38 −0
Original line number Diff line number Diff line
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

namespace bluetooth {

class StackManager {
 public:
  static StackManager* Get() {
    static StackManager instance;
    return &instance;
  }

  // Start up the stack, init HCI HAL
  void StartUp();

  // Shut down the stack, close HCI HAL
  void ShutDown();

 private:
  StackManager() = default;
};

}  // namespace bluetooth