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

Commit 617c9ebc authored by Myles Watson's avatar Myles Watson Committed by Andre Eisenbach
Browse files

Finish conversion to Android.bp

Bug: 35651902, 35672576, 36810308
Test: build, Android Cloud Bluetooth net_test_bluetooth
Change-Id: Ie99966d610ea35ab8caf8a084d92c43618e90b94
parent c11b8428
Loading
Loading
Loading
Loading
+0 −61
Original line number Original line Diff line number Diff line
LOCAL_PATH := $(call my-dir)

# test-vendor shared library for target
# ========================================================
include $(CLEAR_VARS)

BT_DIR := $(TOP_DIR)packages/modules/Bluetooth/system

LOCAL_SRC_FILES := \
    src/async_manager.cc \
    src/bt_address.cc \
    src/bt_vendor.cc \
    src/command_packet.cc \
    src/dual_mode_controller.cc \
    src/event_packet.cc \
    src/hci_transport.cc \
    src/packet.cc \
    src/packet_stream.cc \
    src/test_channel_transport.cc \
    src/vendor_manager.cc

# We pull in gtest because base/files/file_util.h, which is used to read the
# controller properties file, needs gtest/gtest_prod.h.
LOCAL_C_INCLUDES := \
    $(LOCAL_PATH)/include \
    $(BT_DIR) \
    $(BT_DIR)/include \
    $(BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR) \
    $(BT_DIR)/utils/include \
    $(BT_DIR)/hci/include \
    $(BT_DIR)/stack/include \
    $(BT_DIR)/third_party/gtest/include

LOCAL_SHARED_LIBRARIES := \
    liblog \
    libchrome

LOCAL_CPP_EXTENSION := .cc
# On some devices this is the actual vendor library. On other devices build
# as a test library.
ifneq (,$(BOARD_BLUETOOTH_USE_TEST_AS_VENDOR))
LOCAL_MODULE := libbt-vendor
LOCAL_MODULE_OWNER := google
LOCAL_PROPRIETARY_MODULE := true
LOCAL_CFLAGS += -DBLUETOOTH_USE_TEST_AS_VENDOR
ifeq ($(TARGET_TRANSLATE_2ND_ARCH),true)
# If its vendor library and secondary arch is translated then only one library
# is provided
ifneq (1,$(words $(LOCAL_MODULE_TARGET_ARCH)))
LOCAL_MULTILIB := first
endif
endif
else
LOCAL_MODULE := test-vendor
endif
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := SHARED_LIBRARIES

LOCAL_CFLAGS += $(test-vendor_CFLAGS)

include $(BUILD_SHARED_LIBRARY)
+0 −63
Original line number Original line Diff line number Diff line
shared_library("test_vendor_lib") {
  sources = [
    "src/async_manager.cc",
    "src/bt_address.cc",
    "src/bt_vendor.cc",
    "src/command_packet.cc",
    "src/dual_mode_controller.cc",
    "src/event_packet.cc",
    "src/hci_transport.cc",
    "src/packet.cc",
    "src/packet_stream.cc",
    "src/test_channel_transport.cc",
    "src/vendor_manager.cc",
  ]

  include_dirs = [
    "include",
    "//",

    # TODO(dennischeng): Ideally we should need to have the lines below for
    # indirect includes.
    "//stack/include",
  ]

  deps = [
    "//third_party/libchrome:base"
  ]
}

executable("test_vendor_lib_test") {
  testonly = true
  sources = [
    "src/async_manager.cc",
    "src/command_packet.cc",
    "src/event_packet.cc",
    "src/hci_transport.cc",
    "src/packet.cc",
    "src/packet_stream.cc",
    "test/hci_transport_unittest.cc",
    "test/packet_stream_unittest.cc",
  ]

  include_dirs = [
    "include",
    "//",
    "//btcore/include",
    "//hci/include",
    "//stack/include",
  ]

  deps = [
    "//third_party/googletest:gtest_main",
    "//third_party/libchrome:base",
    "//vendor_libs/test_vendor_lib",
  ]

  libs = [
    "-lpthread",
    "-lrt",
    "-ldl",
    "-latomic",
  ]
}
+0 −143
Original line number Original line Diff line number Diff line
//
// Copyright 2015 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.
//

#define LOG_TAG "bt_vendor"

#include <unistd.h>
#include <memory>

#include <base/logging.h>
#include "osi/include/log.h"
#include "vendor_manager.h"

namespace test_vendor_lib {

class BtVendor {
 public:
  // Initializes vendor manager for test controller. |p_cb| are the callbacks to
  // be in TestVendorOp(). |local_bdaddr| points to the address of the Bluetooth
  // device. Returns 0 on success, -1 on error.
  static int Initialize(const bt_vendor_callbacks_t* p_cb,
                        unsigned char* /* local_bdaddr */) {
    LOG_INFO(LOG_TAG, "Initializing test controller.");
    CHECK(p_cb);

    vendor_callbacks_ = *p_cb;

    vendor_manager_.reset(new VendorManager());
    return vendor_manager_->Initialize() ? 0 : 1;
  }

  // Vendor specific operations. |opcode| is the opcode for Bluedroid's vendor
  // op definitions. |param| points to operation specific arguments. Return
  // value is dependent on the operation invoked, or -1 on error.
  static int Op(bt_vendor_opcode_t opcode, void* param) {
    LOG_INFO(LOG_TAG, "Opcode received in vendor library: %d", opcode);

    switch (opcode) {
      case BT_VND_OP_POWER_CTRL: {
        LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_POWER_CTRL");
        int* state = static_cast<int*>(param);
        if (*state == BT_VND_PWR_OFF)
          LOG_INFO(LOG_TAG, "Turning Bluetooth off.");
        else if (*state == BT_VND_PWR_ON)
          LOG_INFO(LOG_TAG, "Turning Bluetooth on.");
        return 0;
      }

      // Give the HCI its fd to communicate with the HciTransport.
      case BT_VND_OP_USERIAL_OPEN: {
        LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_USERIAL_OPEN");
        CHECK(vendor_manager_);
        int* fd_list = static_cast<int*>(param);
        fd_list[0] = vendor_manager_->GetHciFd();
        LOG_INFO(LOG_TAG, "Setting HCI's fd to: %d", fd_list[0]);
        return 1;
      }

      // Close the HCI's file descriptor.
      case BT_VND_OP_USERIAL_CLOSE:
        LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_USERIAL_CLOSE");
        CHECK(vendor_manager_);
        LOG_INFO(LOG_TAG, "Closing HCI's fd (fd: %d)",
                 vendor_manager_->GetHciFd());
        vendor_manager_->CloseHciFd();
        return 1;

      case BT_VND_OP_FW_CFG:
        LOG_INFO(LOG_TAG, "BT_VND_OP_FW_CFG (Does nothing)");
        vendor_callbacks_.fwcfg_cb(BT_VND_OP_RESULT_SUCCESS);
        return 0;

      case BT_VND_OP_SCO_CFG:
        LOG_INFO(LOG_TAG, "BT_VND_OP_SCO_CFG (Does nothing)");
        vendor_callbacks_.scocfg_cb(BT_VND_OP_RESULT_SUCCESS);
        return 0;

      case BT_VND_OP_GET_LPM_IDLE_TIMEOUT:
        LOG_INFO(LOG_TAG, "Doing op: BT_VND_OP_SCO_CFG");
        *((uint32_t*)param) = 1000;
        return 0;

      case BT_VND_OP_LPM_SET_MODE:
        LOG_INFO(LOG_TAG, "BT_VND_OP_LPM_SET_MODE (Does nothing)");
        vendor_callbacks_.lpm_cb(BT_VND_OP_RESULT_SUCCESS);
        return 0;

      case BT_VND_OP_LPM_WAKE_SET_STATE:
        LOG_INFO(LOG_TAG, "BT_VND_OP_LPM_WAKE_SET_STATE (Does nothing)");
        return 0;

      case BT_VND_OP_SET_AUDIO_STATE:
        LOG_INFO(LOG_TAG, "BT_VND_OP_SET_AUDIO_STATE (Does nothing)");
        return 0;

      case BT_VND_OP_EPILOG:
        LOG_INFO(LOG_TAG, "BT_VND_OP_EPILOG (Does nothing)");
        vendor_callbacks_.epilog_cb(BT_VND_OP_RESULT_SUCCESS);
        return 0;

      default:
        LOG_INFO(LOG_TAG, "Op not recognized.");
        return -1;
    }
    return 0;
  }

  // Closes the vendor interface and cleans up the global vendor manager object.
  static void CleanUp(void) {
    LOG_INFO(LOG_TAG, "Cleaning up vendor library.");
    CHECK(vendor_manager_);
    vendor_manager_->CleanUp();
    vendor_manager_.reset();
  }

 private:
  static std::unique_ptr<VendorManager> vendor_manager_;
  static bt_vendor_callbacks_t vendor_callbacks_;
};

// Definition of static class members
std::unique_ptr<VendorManager> BtVendor::vendor_manager_;
bt_vendor_callbacks_t BtVendor::vendor_callbacks_;

}  // namespace test_vendor_lib

// Entry point of DLib.
EXPORT_SYMBOL
const bt_vendor_interface_t BLUETOOTH_VENDOR_LIB_INTERFACE = {
    sizeof(bt_vendor_interface_t), test_vendor_lib::BtVendor::Initialize,
    test_vendor_lib::BtVendor::Op, test_vendor_lib::BtVendor::CleanUp};
+0 −120
Original line number Original line Diff line number Diff line
//
// Copyright 2015 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.
//

#define LOG_TAG "vendor_manager"

#include "vendor_manager.h"

#include <base/logging.h>

#include "osi/include/log.h"

#include <vector>

namespace test_vendor_lib {

VendorManager* g_manager = nullptr;

void VendorManager::CleanUp() { test_channel_transport_.CleanUp(); }

bool VendorManager::Initialize() {
  if (!transport_.SetUp()) {
    LOG_ERROR(LOG_TAG, "Error setting up transport object.");
    return false;
  }

  transport_.RegisterCommandHandler(
      [this](std::unique_ptr<CommandPacket> command) {
        CommandPacket& cmd =
            *command;  // only to be copied into the lambda, not a memory leak
        async_manager_.ExecAsync(std::chrono::milliseconds(0), [this, cmd]() {
          controller_.HandleCommand(std::unique_ptr<CommandPacket>(
              new CommandPacket(std::move(cmd))));
        });
      });

  test_channel_transport_.RegisterCommandHandler(
      [this](const std::string& name, const std::vector<std::string>& args) {
        async_manager_.ExecAsync(
            std::chrono::milliseconds(0), [this, name, args]() {
              controller_.HandleTestChannelCommand(name, args);
            });
      });

  controller_.RegisterEventChannel([this](std::unique_ptr<EventPacket> event) {
    transport_.SendEvent(std::move(event));
  });

  controller_.RegisterTaskScheduler(
      [this](std::chrono::milliseconds delay, const TaskCallback& task) {
        return async_manager_.ExecAsync(delay, task);
      });

  controller_.RegisterPeriodicTaskScheduler(
      [this](std::chrono::milliseconds delay, std::chrono::milliseconds period,
             const TaskCallback& task) {
        return async_manager_.ExecAsyncPeriodically(delay, period, task);
      });

  controller_.RegisterTaskCancel(
      [this](AsyncTaskId task) { async_manager_.CancelAsyncTask(task); });

  if (async_manager_.WatchFdForNonBlockingReads(
          transport_.GetVendorFd(),
          [this](int fd) { transport_.OnCommandReady(fd); }) != 0) {
    LOG_ERROR(LOG_TAG, "Error watching vendor fd.");
    return true;
  }

  SetUpTestChannel(6111);

  return true;
}

VendorManager::VendorManager() : test_channel_transport_() {}

void VendorManager::SetUpTestChannel(int port) {
  int socket_fd = test_channel_transport_.SetUp(port);

  if (socket_fd == -1) {
    LOG_ERROR(LOG_TAG, "Test channel SetUp(%d) failed.", port);
    return;
  }

  LOG_INFO(LOG_TAG, "Test channel SetUp() successful");
  async_manager_.WatchFdForNonBlockingReads(socket_fd, [this](int socket_fd) {
    int conn_fd = test_channel_transport_.Accept(socket_fd);
    if (conn_fd < 0) {
      LOG_ERROR(LOG_TAG, "Error watching test channel fd.");
      return;
    }
    LOG_INFO(LOG_TAG, "Test channel connection accepted.");
    async_manager_.WatchFdForNonBlockingReads(conn_fd, [this](int conn_fd) {
      test_channel_transport_.OnCommandReady(conn_fd, [this, conn_fd]() {
        async_manager_.StopWatchingFileDescriptor(conn_fd);
      });
    });
  });
}

void VendorManager::CloseHciFd() {
  async_manager_.StopWatchingFileDescriptor(transport_.GetHciFd());
  transport_.CloseHciFd();
}

int VendorManager::GetHciFd() const { return transport_.GetHciFd(); }

}  // namespace test_vendor_lib