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

Commit a9ede391 authored by Henri Chataing's avatar Henri Chataing
Browse files

Remove the cc_binary bluetooth_stack_with_facade

Bug: 384782957
Test: TreeHugger
Flag: EXEMPT, dead code removal
Change-Id: I1a65e35f130a8fd53a5d43041f573c740723d7c7
parent 7f6912c0
Loading
Loading
Loading
Loading
+0 −102
Original line number Diff line number Diff line
@@ -10,26 +10,6 @@ package {
    default_applicable_licenses: ["system_bt_license"],
}

filegroup {
    name: "BlueberryFacadeProto",
    srcs: [
        "blueberry/facade/common.proto",
        "blueberry/facade/hal/hal_facade.proto",
        "blueberry/facade/hci/acl_manager_facade.proto",
        "blueberry/facade/hci/controller_facade.proto",
        "blueberry/facade/hci/hci_facade.proto",
        "blueberry/facade/hci/le_acl_manager_facade.proto",
        "blueberry/facade/hci/le_advertising_manager_facade.proto",
        "blueberry/facade/hci/le_initiator_address_facade.proto",
        "blueberry/facade/hci/le_scanning_manager_facade.proto",
        "blueberry/facade/l2cap/classic/facade.proto",
        "blueberry/facade/l2cap/le/facade.proto",
        "blueberry/facade/neighbor/facade.proto",
        "blueberry/facade/rootservice.proto",
        "blueberry/facade/topshim/facade.proto",
    ],
}

filegroup {
    name: "TestMockCodecManager",
    srcs: [
@@ -37,88 +17,6 @@ filegroup {
    ],
}

genrule {
    name: "BlueberryFacadeGeneratedStub_h",
    tools: [
        "aprotoc",
        "protoc-gen-grpc-cpp-plugin",
    ],
    cmd: "$(location aprotoc) -Ipackages/modules/Bluetooth/system -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(locations :BlueberryFacadeProto) --grpc_out=$(genDir) --cpp_out=$(genDir)",
    srcs: [
        ":BlueberryFacadeProto",
        ":libprotobuf-internal-protos",
    ],
    out: [
        "blueberry/facade/common.grpc.pb.h",
        "blueberry/facade/common.pb.h",
        "blueberry/facade/hal/hal_facade.grpc.pb.h",
        "blueberry/facade/hal/hal_facade.pb.h",
        "blueberry/facade/hci/acl_manager_facade.grpc.pb.h",
        "blueberry/facade/hci/acl_manager_facade.pb.h",
        "blueberry/facade/hci/controller_facade.grpc.pb.h",
        "blueberry/facade/hci/controller_facade.pb.h",
        "blueberry/facade/hci/hci_facade.grpc.pb.h",
        "blueberry/facade/hci/hci_facade.pb.h",
        "blueberry/facade/hci/le_acl_manager_facade.grpc.pb.h",
        "blueberry/facade/hci/le_acl_manager_facade.pb.h",
        "blueberry/facade/hci/le_advertising_manager_facade.grpc.pb.h",
        "blueberry/facade/hci/le_advertising_manager_facade.pb.h",
        "blueberry/facade/hci/le_initiator_address_facade.grpc.pb.h",
        "blueberry/facade/hci/le_initiator_address_facade.pb.h",
        "blueberry/facade/hci/le_scanning_manager_facade.grpc.pb.h",
        "blueberry/facade/hci/le_scanning_manager_facade.pb.h",
        "blueberry/facade/l2cap/classic/facade.grpc.pb.h",
        "blueberry/facade/l2cap/classic/facade.pb.h",
        "blueberry/facade/l2cap/le/facade.grpc.pb.h",
        "blueberry/facade/l2cap/le/facade.pb.h",
        "blueberry/facade/neighbor/facade.grpc.pb.h",
        "blueberry/facade/neighbor/facade.pb.h",
        "blueberry/facade/rootservice.grpc.pb.h",
        "blueberry/facade/rootservice.pb.h",
    ],
}

genrule {
    name: "BlueberryFacadeGeneratedStub_cc",
    tools: [
        "aprotoc",
        "protoc-gen-grpc-cpp-plugin",
    ],
    cmd: "$(location aprotoc) -Ipackages/modules/Bluetooth/system -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(locations :BlueberryFacadeProto) --grpc_out=$(genDir) --cpp_out=$(genDir)",
    srcs: [
        ":BlueberryFacadeProto",
        ":libprotobuf-internal-protos",
    ],
    out: [
        "blueberry/facade/common.grpc.pb.cc",
        "blueberry/facade/common.pb.cc",
        "blueberry/facade/hal/hal_facade.grpc.pb.cc",
        "blueberry/facade/hal/hal_facade.pb.cc",
        "blueberry/facade/hci/acl_manager_facade.grpc.pb.cc",
        "blueberry/facade/hci/acl_manager_facade.pb.cc",
        "blueberry/facade/hci/controller_facade.grpc.pb.cc",
        "blueberry/facade/hci/controller_facade.pb.cc",
        "blueberry/facade/hci/hci_facade.grpc.pb.cc",
        "blueberry/facade/hci/hci_facade.pb.cc",
        "blueberry/facade/hci/le_acl_manager_facade.grpc.pb.cc",
        "blueberry/facade/hci/le_acl_manager_facade.pb.cc",
        "blueberry/facade/hci/le_advertising_manager_facade.grpc.pb.cc",
        "blueberry/facade/hci/le_advertising_manager_facade.pb.cc",
        "blueberry/facade/hci/le_initiator_address_facade.grpc.pb.cc",
        "blueberry/facade/hci/le_initiator_address_facade.pb.cc",
        "blueberry/facade/hci/le_scanning_manager_facade.grpc.pb.cc",
        "blueberry/facade/hci/le_scanning_manager_facade.pb.cc",
        "blueberry/facade/l2cap/classic/facade.grpc.pb.cc",
        "blueberry/facade/l2cap/classic/facade.pb.cc",
        "blueberry/facade/l2cap/le/facade.grpc.pb.cc",
        "blueberry/facade/l2cap/le/facade.pb.cc",
        "blueberry/facade/neighbor/facade.grpc.pb.cc",
        "blueberry/facade/neighbor/facade.pb.cc",
        "blueberry/facade/rootservice.grpc.pb.cc",
        "blueberry/facade/rootservice.pb.cc",
    ],
}

rust_protobuf {
    name: "libbt_topshim_facade_protobuf",
    crate_name: "bt_topshim_facade_protobuf",
+0 −92
Original line number Diff line number Diff line
@@ -235,98 +235,6 @@ cc_library_static {
    ],
}

cc_binary {
    name: "bluetooth_stack_with_facade",
    defaults: [
        "gd_defaults",
    ],
    cflags: [
        // The generated gRPC code triggers these warnings.
        "-Wno-missing-prototypes",
    ],
    include_dirs: [
        "packages/modules/Bluetooth/system",
        "packages/modules/Bluetooth/system/include",
    ],
    host_supported: true,
    srcs: [
        ":BluetoothFacade_hci_hal",
        ":BluetoothFacade_hci_layer",
        ":BluetoothFacade_neighbor",
        ":TestCommonMockFunctions",
        ":TestMockMainShimStack",
        ":TestMockStackMetrics",
        "facade/facade_main.cc",
        "facade/grpc_root_server.cc",
        "facade/read_only_property_server.cc",
        "grpc/grpc_module.cc",
    ],
    generated_headers: [
        "BlueberryFacadeGeneratedStub_h",
    ],
    generated_sources: [
        "BlueberryFacadeGeneratedStub_cc",
    ],
    static_libs: [
        "bluetooth_flags_c_lib",
        "breakpad_client",
        "libPlatformProperties",
        "libbluetooth-protos",
        "libbluetooth-types",
        "libbluetooth_crypto_toolbox",
        "libbluetooth_gd",
        "libbluetooth_hci_pdl",
        "libbluetooth_l2cap_pdl",
        "libbluetooth_log",
        "libbluetooth_ras_pdl",
        "libbluetooth_smp_pdl",
        "libbt-common",
        "libbt-platform-protos-lite",
        "libchrome",
        "libcom.android.sysprop.bluetooth.wrapped",
        "libflatbuffers-cpp",
        "libosi",
    ],
    shared_libs: [
        "libaconfig_storage_read_api_cc",
        "libbase",
        "libcrypto",
        "libgrpc++",
        "libgrpc_wrap",
        "liblog",
        "libprotobuf-cpp-full",
        "libunwindstack",
        "server_configurable_flags",
    ],
    target: {
        android: {
            shared_libs: [
                "android.hardware.bluetooth@1.0",
                "android.hardware.bluetooth@1.1",
                "libbinder_ndk",
                "libcutils",
                "libhidlbase",
                "libstatssocket",
                "libutils",
            ],
            static_libs: [
                "android.system.suspend-V1-ndk",
                "android.system.suspend.control-V1-ndk",
                "libstatslog_bt",
            ],
        },
        host: {
            required: [
                "root-canal",
            ],
        },
    },
    sanitize: {
        address: true,
        cfi: true,
    },
}

cc_test {
    name: "bluetooth_test_with_timerfd",
    test_suites: ["general-tests"],

system/gd/facade/facade_main.cc

deleted100644 → 0
+0 −174
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 <bluetooth/log.h>
#include <sys/types.h>
#include <unistd.h>

#include <csignal>
#include <cstring>
#include <optional>
#include <string>
#include <thread>

#include "stack_manager.h"

// clang-format off
#include <client/linux/handler/exception_handler.h>
#include <unwindstack/AndroidUnwinder.h>
// clang-format on

#include "facade/grpc_root_server.h"
#include "hal/hci_hal_host.h"
#include "hal/snoop_logger.h"
#include "os/parameter_provider.h"
#include "os/system_properties.h"

using ::bluetooth::ModuleList;
using ::bluetooth::StackManager;
using ::bluetooth::hal::HciHalHostRootcanalConfig;
using ::bluetooth::os::Thread;
using namespace bluetooth;

extern "C" const char* __asan_default_options() { return "detect_container_overflow=0"; }

namespace {
::bluetooth::facade::GrpcRootServer grpc_root_server;

std::promise<void> interrupt_promise;
std::future<void> interrupt_future;
bool interrupted = false;
struct sigaction old_act = {};
void interrupt_handler(int signal_number) {
  if (!interrupted) {
    interrupted = true;
    log::info("Stopping gRPC root server due to signal: {}[{}]", strsignal(signal_number),
              signal_number);
    interrupt_promise.set_value();
  } else {
    log::warn("Already interrupted by signal: {}[{}]", strsignal(signal_number), signal_number);
  }
  if (old_act.sa_handler != nullptr && old_act.sa_handler != SIG_IGN &&
      old_act.sa_handler != SIG_DFL) {
    log::info("Calling saved signal handler");
    old_act.sa_handler(signal_number);
  }
}
struct sigaction new_act = {.sa_handler = interrupt_handler};

bool crash_callback(const void* crash_context, size_t crash_context_size, void* /* context */) {
  std::optional<pid_t> tid;
  if (crash_context_size >= sizeof(google_breakpad::ExceptionHandler::CrashContext)) {
    auto* ctx = static_cast<const google_breakpad::ExceptionHandler::CrashContext*>(crash_context);
    tid = ctx->tid;
    int signal_number = ctx->siginfo.si_signo;
    log::error("Process crashed, signal: {}[{}], tid: {}", strsignal(signal_number), signal_number,
               ctx->tid);
  } else {
    log::error("Process crashed, signal: unknown, tid: unknown");
  }
  unwindstack::AndroidLocalUnwinder unwinder;
  unwindstack::AndroidUnwinderData data;
  if (!unwinder.Unwind(tid, data)) {
    log::error("Unwind failed");
    return false;
  }
  log::error("Backtrace:");
  for (const auto& frame : data.frames) {
    log::error("{}", unwinder.FormatFrame(frame));
  }
  return true;
}

// Need to stop server on a thread that is not part of a signal handler due to an issue with gRPC
// See: https://github.com/grpc/grpc/issues/24884
void thread_check_shutdown() {
  log::info("shutdown thread waiting for interruption");
  interrupt_future.wait();
  log::info("interrupted, stopping server");
  grpc_root_server.StopServer();
}

}  // namespace

// The entry point for the binary with libbluetooth + facades
int main(int argc, const char** argv) {
  google_breakpad::MinidumpDescriptor descriptor(
          google_breakpad::MinidumpDescriptor::kMicrodumpOnConsole);
  google_breakpad::ExceptionHandler eh(descriptor, nullptr, nullptr, nullptr, true, -1);
  eh.set_crash_handler(crash_callback);

  int root_server_port = 8897;
  int grpc_port = 8899;

  const std::string arg_grpc_root_server_port = "--root-server-port=";
  const std::string arg_grpc_server_port = "--grpc-port=";
  const std::string arg_rootcanal_port = "--rootcanal-port=";
  const std::string arg_btsnoop_path = "--btsnoop=";
  const std::string arg_btsnooz_path = "--btsnooz=";
  const std::string arg_btconfig_path = "--btconfig=";
  for (int i = 1; i < argc; i++) {
    std::string arg = argv[i];
    if (arg.find(arg_grpc_root_server_port) == 0) {
      auto port_number = arg.substr(arg_grpc_root_server_port.size());
      root_server_port = std::stoi(port_number);
    }
    if (arg.find(arg_grpc_server_port) == 0) {
      auto port_number = arg.substr(arg_grpc_server_port.size());
      grpc_port = std::stoi(port_number);
    }
    if (arg.find(arg_rootcanal_port) == 0) {
      auto port_number = arg.substr(arg_rootcanal_port.size());
      HciHalHostRootcanalConfig::Get()->SetPort(std::stoi(port_number));
    }
    if (arg.find(arg_btsnoop_path) == 0) {
      auto btsnoop_path = arg.substr(arg_btsnoop_path.size());
      ::bluetooth::os::ParameterProvider::OverrideSnoopLogFilePath(btsnoop_path);
      log::assert_that(::bluetooth::os::SetSystemProperty(
                               ::bluetooth::hal::SnoopLogger::kBtSnoopLogModeProperty,
                               ::bluetooth::hal::SnoopLogger::kBtSnoopLogModeFull),
                       "assert failed: ::bluetooth::os::SetSystemProperty( "
                       "::bluetooth::hal::SnoopLogger::kBtSnoopLogModeProperty, "
                       "::bluetooth::hal::SnoopLogger::kBtSnoopLogModeFull)");
    }
    if (arg.find(arg_btsnooz_path) == 0) {
      auto btsnooz_path = arg.substr(arg_btsnooz_path.size());
      ::bluetooth::os::ParameterProvider::OverrideSnoozLogFilePath(btsnooz_path);
    }
    if (arg.find(arg_btconfig_path) == 0) {
      auto btconfig_path = arg.substr(arg_btconfig_path.size());
      ::bluetooth::os::ParameterProvider::OverrideConfigFilePath(btconfig_path);
    }
  }

  int ret = sigaction(SIGINT, &new_act, &old_act);
  if (ret < 0) {
    log::error("sigaction error: {}", strerror(errno));
  }

  log::info("Starting Server");
  grpc_root_server.StartServer("0.0.0.0", root_server_port, grpc_port);
  log::info("Server started");
  auto wait_thread = std::thread([] { grpc_root_server.RunGrpcLoop(); });
  interrupt_future = interrupt_promise.get_future();
  auto shutdown_thread = std::thread{thread_check_shutdown};
  wait_thread.join();
  log::info("Server terminated");
  shutdown_thread.join();
  log::info("Shutdown thread terminated");

  return 0;
}
+0 −157
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 "facade/grpc_root_server.h"

#include <string>

#include "blueberry/facade/rootservice.grpc.pb.h"
#include "facade/read_only_property_server.h"
#include "grpc/grpc_module.h"
#include "hal/facade.h"
#include "hci/facade/acl_manager_facade.h"
#include "hci/facade/controller_facade.h"
#include "hci/facade/facade.h"
#include "hci/facade/le_acl_manager_facade.h"
#include "hci/facade/le_advertising_manager_facade.h"
#include "hci/facade/le_initiator_address_facade.h"
#include "hci/facade/le_scanning_manager_facade.h"
#include "neighbor/facade/facade.h"
#include "os/thread.h"
#include "stack_manager.h"

namespace bluetooth {
namespace facade {

using ::blueberry::facade::BluetoothModule;
using ::bluetooth::grpc::GrpcModule;
using ::bluetooth::os::Thread;

class RootFacadeService : public ::blueberry::facade::RootFacade::Service {
public:
  explicit RootFacadeService(int grpc_port) : grpc_port_(grpc_port) {}

  ::grpc::Status StartStack(::grpc::ServerContext* /* context */,
                            const ::blueberry::facade::StartStackRequest* request,
                            ::blueberry::facade::StartStackResponse* /* response */) override {
    if (is_running_) {
      return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "stack is running");
    }

    ModuleList modules;
    modules.add<::bluetooth::grpc::GrpcModule>();

    BluetoothModule module_under_test = request->module_under_test();
    switch (module_under_test) {
      case BluetoothModule::HAL:
        modules.add<::bluetooth::hal::HciHalFacadeModule>();
        break;
      case BluetoothModule::HCI:
        modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
        modules.add<::bluetooth::hci::facade::HciFacadeModule>();
        break;
      case BluetoothModule::HCI_INTERFACES:
        modules.add<::bluetooth::facade::ReadOnlyPropertyServerModule>();
        modules.add<::bluetooth::hci::facade::HciFacadeModule>();
        modules.add<::bluetooth::hci::facade::AclManagerFacadeModule>();
        modules.add<::bluetooth::hci::facade::ControllerFacadeModule>();
        modules.add<::bluetooth::hci::facade::LeAclManagerFacadeModule>();
        modules.add<::bluetooth::hci::facade::LeAdvertisingManagerFacadeModule>();
        modules.add<::bluetooth::hci::facade::LeInitiatorAddressFacadeModule>();
        modules.add<::bluetooth::hci::facade::LeScanningManagerFacadeModule>();
        modules.add<::bluetooth::neighbor::facade::NeighborFacadeModule>();
        break;
      default:
        return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "invalid module under test");
    }

    stack_thread_ = std::make_unique<Thread>("stack_thread", Thread::Priority::NORMAL);
    stack_manager_.StartUp(&modules, stack_thread_.get());

    GrpcModule* grpc_module = stack_manager_.GetInstance<GrpcModule>();
    grpc_module->StartServer("0.0.0.0", grpc_port_);

    grpc_loop_thread_ =
            std::make_unique<std::thread>([grpc_module] { grpc_module->RunGrpcLoop(); });
    is_running_ = true;

    return ::grpc::Status::OK;
  }

  ::grpc::Status StopStack(::grpc::ServerContext* /* context */,
                           const ::blueberry::facade::StopStackRequest* /* request */,
                           ::blueberry::facade::StopStackResponse* /* response */) override {
    if (!is_running_) {
      return ::grpc::Status(::grpc::StatusCode::INVALID_ARGUMENT, "stack is not running");
    }

    stack_manager_.GetInstance<GrpcModule>()->StopServer();
    grpc_loop_thread_->join();
    grpc_loop_thread_.reset();

    stack_manager_.ShutDown();
    stack_thread_.reset();
    is_running_ = false;
    return ::grpc::Status::OK;
  }

private:
  std::unique_ptr<Thread> stack_thread_ = nullptr;
  bool is_running_ = false;
  std::unique_ptr<std::thread> grpc_loop_thread_ = nullptr;
  StackManager stack_manager_;
  int grpc_port_ = 8898;
};

struct GrpcRootServer::impl {
  bool started_ = false;
  std::unique_ptr<RootFacadeService> root_facade_service_ = nullptr;
  std::unique_ptr<::grpc::Server> server_ = nullptr;
};

GrpcRootServer::GrpcRootServer() : pimpl_(new impl()) {}

GrpcRootServer::~GrpcRootServer() = default;

void GrpcRootServer::StartServer(const std::string& address, int grpc_root_server_port,
                                 int grpc_port) {
  log::assert_that(!pimpl_->started_, "assert failed: !pimpl_->started_");
  pimpl_->started_ = true;

  std::string listening_port = address + ":" + std::to_string(grpc_root_server_port);
  ::grpc::ServerBuilder builder;

  pimpl_->root_facade_service_ = std::make_unique<RootFacadeService>(grpc_port);
  builder.RegisterService(pimpl_->root_facade_service_.get());
  builder.AddListeningPort(listening_port, ::grpc::InsecureServerCredentials());
  pimpl_->server_ = builder.BuildAndStart();

  log::assert_that(pimpl_->server_ != nullptr, "assert failed: pimpl_->server_ != nullptr");
}

void GrpcRootServer::StopServer() {
  log::assert_that(pimpl_->started_, "assert failed: pimpl_->started_");
  pimpl_->server_->Shutdown();
  pimpl_->started_ = false;
}

void GrpcRootServer::RunGrpcLoop() {
  log::assert_that(pimpl_->started_, "assert failed: pimpl_->started_");
  pimpl_->server_->Wait();
}

}  // namespace facade
}  // namespace bluetooth
+0 −44
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

#include <grpc++/grpc++.h>

#include <memory>
#include <string>

namespace bluetooth {
namespace facade {

class GrpcRootServer {
public:
  GrpcRootServer();
  ~GrpcRootServer();

  void StartServer(const std::string& address, int grpc_root_server_port, int grpc_port);

  void StopServer();

  void RunGrpcLoop();

private:
  struct impl;
  std::unique_ptr<impl> pimpl_;
};

}  // namespace facade
}  // namespace bluetooth
Loading