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

Commit 5b21503a authored by Rahul Arya's avatar Rahul Arya
Browse files

[Invisalign] Pass in an interface into core sending events to profiles

Will populate in future CLs.

Bug: 254063018
Test: adapter tests

Change-Id: I53e36990db65403592162c5e6a3b5ec98526698f
parent 773640a5
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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 {
namespace core {

// This class defines the overall interface expected by bluetooth::core.
struct CoreInterface {
  CoreInterface(){};

  CoreInterface(const CoreInterface&) = delete;
  CoreInterface& operator=(const CoreInterface&) = delete;
  virtual ~CoreInterface() = default;
};

}  // namespace core
}  // namespace bluetooth
+6 −2
Original line number Diff line number Diff line
@@ -20,14 +20,16 @@

#include <stdbool.h>

#include "core_callbacks.h"
#include "osi/include/future.h"

using ProfileStartCallback = void();
using ProfileStopCallback = void();

typedef struct {
  void (*init_stack)(void);
  void (*start_up_stack_async)(ProfileStartCallback, ProfileStopCallback);
  void (*init_stack)(bluetooth::core::CoreInterface*);
  void (*start_up_stack_async)(bluetooth::core::CoreInterface*,
                               ProfileStartCallback, ProfileStopCallback);
  void (*shut_down_stack_async)(ProfileStopCallback);
  void (*clean_up_stack)(void);

@@ -39,3 +41,5 @@ const stack_manager_t* stack_manager_get_interface();
// TODO(zachoverflow): remove this terrible hack once the startup sequence is
// more sane
future_t* stack_manager_get_hack_future();

bluetooth::core::CoreInterface* GetInterfaceToProfiles();
+16 −3
Original line number Diff line number Diff line
@@ -163,6 +163,19 @@ extern CsisClientInterface* btif_csis_client_get_interface();
/* Volume Control client */
extern VolumeControlInterface* btif_volume_control_get_interface();

/*******************************************************************************
 *  Callbacks from bluetooth::core (see go/invisalign-bt)
 ******************************************************************************/

struct CoreInterfaceImpl : bluetooth::core::CoreInterface {
  using bluetooth::core::CoreInterface::CoreInterface;
};

static bluetooth::core::CoreInterface* CreateInterfaceToProfiles() {
  static auto interfaceForCore = CoreInterfaceImpl();
  return &interfaceForCore;
}

/*******************************************************************************
 *  Functions
 ******************************************************************************/
@@ -231,7 +244,7 @@ static int init(bt_callbacks_t* callbacks, bool start_restricted,

  is_local_device_atv = is_atv;

  stack_manager_get_interface()->init_stack();
  stack_manager_get_interface()->init_stack(CreateInterfaceToProfiles());
  return BT_STATUS_SUCCESS;
}

@@ -258,8 +271,8 @@ static void stop_profiles() {
static int enable() {
  if (!interface_ready()) return BT_STATUS_NOT_READY;

  stack_manager_get_interface()->start_up_stack_async(&start_profiles,
                                                      &stop_profiles);
  stack_manager_get_interface()->start_up_stack_async(
      CreateInterfaceToProfiles(), &start_profiles, &stop_profiles);
  return BT_STATUS_SUCCESS;
}

+31 −14
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "btif/include/stack_manager.h"

#include <hardware/bluetooth.h>

#include <cstdlib>
#include <cstring>

@@ -29,6 +30,7 @@
#include "btif_api.h"
#include "btif_common.h"
#include "common/message_loop_thread.h"
#include "core_callbacks.h"
#include "main/shim/shim.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
@@ -133,8 +135,10 @@ static bool stack_is_initialized;
// If running, the stack is fully up and able to bluetooth.
static bool stack_is_running;

static void event_init_stack(void* context);
static void event_start_up_stack(ProfileStartCallback startProfiles,
static void event_init_stack(semaphore_t* semaphore,
                             bluetooth::core::CoreInterface* interface);
static void event_start_up_stack(bluetooth::core::CoreInterface* interface,
                                 ProfileStartCallback startProfiles,
                                 ProfileStopCallback stopProfiles);
static void event_shut_down_stack(ProfileStopCallback stopProfiles);
static void event_clean_up_stack(std::promise<void> promise);
@@ -142,6 +146,12 @@ static void event_clean_up_stack(std::promise<void> promise);
static void event_signal_stack_up(void* context);
static void event_signal_stack_down(void* context);

static bluetooth::core::CoreInterface* interfaceToProfiles;

bluetooth::core::CoreInterface* GetInterfaceToProfiles() {
  return interfaceToProfiles;
}

// Unvetted includes/imports, etc which should be removed or vetted in the
// future
static future_t* hack_future;
@@ -149,22 +159,25 @@ static future_t* hack_future;

// Interface functions

static void init_stack() {
static void init_stack(bluetooth::core::CoreInterface* interface) {
  // This is a synchronous process. Post it to the thread though, so
  // state modification only happens there. Using the thread to perform
  // all stack operations ensures that the operations are done serially
  // and do not overlap.
  semaphore_t* semaphore = semaphore_new(0);
  management_thread.DoInThread(FROM_HERE,
                               base::Bind(event_init_stack, semaphore));
  management_thread.DoInThread(
      FROM_HERE,
      base::Bind(event_init_stack, semaphore, base::Unretained(interface)));
  semaphore_wait(semaphore);
  semaphore_free(semaphore);
}

static void start_up_stack_async(ProfileStartCallback startProfiles,
static void start_up_stack_async(bluetooth::core::CoreInterface* interface,
                                 ProfileStartCallback startProfiles,
                                 ProfileStopCallback stopProfiles) {
  management_thread.DoInThread(
      FROM_HERE, base::Bind(event_start_up_stack, startProfiles, stopProfiles));
      FROM_HERE,
      base::Bind(event_start_up_stack, interface, startProfiles, stopProfiles));
}

static void shut_down_stack_async(ProfileStopCallback stopProfiles) {
@@ -235,14 +248,16 @@ inline const module_t* get_local_module(const char* name) {
}

// Synchronous function to initialize the stack
static void event_init_stack(void* context) {
  semaphore_t* semaphore = (semaphore_t*)context;

static void event_init_stack(semaphore_t* semaphore,
                             bluetooth::core::CoreInterface* interface) {
  LOG_INFO("is initializing the stack");

  if (stack_is_initialized) {
    LOG_INFO("found the stack already in initialized state");
  } else {
    // all callbacks out of libbluetooth-core happen via this interface
    interfaceToProfiles = interface;

    module_management_start();

    module_init(get_local_module(OSI_MODULE));
@@ -264,24 +279,26 @@ static void event_init_stack(void* context) {
  if (semaphore) semaphore_post(semaphore);
}

static void ensure_stack_is_initialized() {
static void ensure_stack_is_initialized(
    bluetooth::core::CoreInterface* interface) {
  if (!stack_is_initialized) {
    LOG_WARN("%s found the stack was uninitialized. Initializing now.",
             __func__);
    // No semaphore needed since we are calling it directly
    event_init_stack(nullptr);
    event_init_stack(nullptr, interface);
  }
}

// Synchronous function to start up the stack
static void event_start_up_stack(ProfileStartCallback startProfiles,
static void event_start_up_stack(bluetooth::core::CoreInterface* interface,
                                 ProfileStartCallback startProfiles,
                                 ProfileStopCallback stopProfiles) {
  if (stack_is_running) {
    LOG_INFO("%s stack already brought up", __func__);
    return;
  }

  ensure_stack_is_initialized();
  ensure_stack_is_initialized(interface);

  LOG_INFO("%s is bringing up the stack", __func__);
  future_t* local_hack_future = future_new();