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

Commit 0a4ad643 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Revert "Fix VTS Fuzz issue"" am: e959d092 am: cfb439b4 am:...

Merge "Revert "Fix VTS Fuzz issue"" am: e959d092 am: cfb439b4 am: b6f1abab am: b2a55b46 am: a0980601

Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/2646737



Change-Id: Iaaa88a2d7ef427a6bd493462da898f0707da6646
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents a9f62f2f a0980601
Loading
Loading
Loading
Loading
+2 −25
Original line number Original line Diff line number Diff line
@@ -33,8 +33,7 @@ static const uint8_t HCI_DATA_TYPE_SCO = 3;


class BluetoothDeathRecipient : public hidl_death_recipient {
class BluetoothDeathRecipient : public hidl_death_recipient {
 public:
 public:
  BluetoothDeathRecipient(const sp<IBluetoothHci> hci)
  BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {}
    : mHci(hci), has_died_(false) {}


  virtual void serviceDied(
  virtual void serviceDied(
      uint64_t /*cookie*/,
      uint64_t /*cookie*/,
@@ -52,7 +51,7 @@ class BluetoothDeathRecipient : public hidl_death_recipient {
};
};


BluetoothHci::BluetoothHci()
BluetoothHci::BluetoothHci()
    : death_recipient_(new BluetoothDeathRecipient(this)) {bt_enabled = 0;}
    : death_recipient_(new BluetoothDeathRecipient(this)) {}


Return<void> BluetoothHci::initialize(
Return<void> BluetoothHci::initialize(
    const ::android::sp<IBluetoothHciCallbacks>& cb) {
    const ::android::sp<IBluetoothHciCallbacks>& cb) {
@@ -62,19 +61,8 @@ Return<void> BluetoothHci::initialize(
    return Void();
    return Void();
  }
  }


  if (bt_enabled == 1) {
    ALOGE("initialize was called!");
    return Void();
  }
  bt_enabled = 1;
  death_recipient_->setHasDied(false);
  death_recipient_->setHasDied(false);
  cb->linkToDeath(death_recipient_, 0);
  cb->linkToDeath(death_recipient_, 0);
  unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
    if (death_recipient->getHasDied())
      ALOGI("Skipping unlink call, service died.");
    else
      cb->unlinkToDeath(death_recipient);
  };


  bool rc = VendorInterface::Initialize(
  bool rc = VendorInterface::Initialize(
      [cb](bool status) {
      [cb](bool status) {
@@ -124,12 +112,6 @@ Return<void> BluetoothHci::initialize(


Return<void> BluetoothHci::close() {
Return<void> BluetoothHci::close() {
  ALOGI("BluetoothHci::close()");
  ALOGI("BluetoothHci::close()");

  if (bt_enabled != 1) {
    ALOGE("should initialize first!");
    return Void();
  }
  bt_enabled = 0;
  unlink_cb_(death_recipient_);
  unlink_cb_(death_recipient_);
  VendorInterface::Shutdown();
  VendorInterface::Shutdown();
  return Void();
  return Void();
@@ -152,11 +134,6 @@ Return<void> BluetoothHci::sendScoData(const hidl_vec<uint8_t>& data) {


void BluetoothHci::sendDataToController(const uint8_t type,
void BluetoothHci::sendDataToController(const uint8_t type,
                                        const hidl_vec<uint8_t>& data) {
                                        const hidl_vec<uint8_t>& data) {
  if (bt_enabled != 1) {
    ALOGE("should initialize first!");
    return;
  }

  VendorInterface::get()->Send(type, data.data(), data.size());
  VendorInterface::get()->Send(type, data.data(), data.size());
}
}


+0 −1
Original line number Original line Diff line number Diff line
@@ -48,7 +48,6 @@ class BluetoothHci : public IBluetoothHci {
  void sendDataToController(const uint8_t type, const hidl_vec<uint8_t>& data);
  void sendDataToController(const uint8_t type, const hidl_vec<uint8_t>& data);
  ::android::sp<BluetoothDeathRecipient> death_recipient_;
  ::android::sp<BluetoothDeathRecipient> death_recipient_;
  std::function<void(sp<BluetoothDeathRecipient>&)> unlink_cb_;
  std::function<void(sp<BluetoothDeathRecipient>&)> unlink_cb_;
  int bt_enabled;
};
};


extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name);
extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name);
+113 −179
Original line number Original line Diff line number Diff line
@@ -36,8 +36,6 @@ static const char* VENDOR_LIBRARY_SYMBOL_NAME =
    "BLUETOOTH_VENDOR_LIB_INTERFACE";
    "BLUETOOTH_VENDOR_LIB_INTERFACE";


static const int INVALID_FD = -1;
static const int INVALID_FD = -1;
std::mutex vendor_mutex_;
std::mutex initcb_mutex_;


namespace {
namespace {


@@ -49,25 +47,13 @@ struct {
  uint16_t opcode;
  uint16_t opcode;
} internal_command;
} internal_command;


enum {
  VENDOR_STATE_INIT = 1,
  VENDOR_STATE_OPENING,	/* during opening */
  VENDOR_STATE_OPENED,	/* open in fops_open */
  VENDOR_STATE_CLOSING,	/* during closing */
  VENDOR_STATE_CLOSED,	/* closed */

  VENDOR_STATE_MSG_NUM
} ;

uint8_t vstate = VENDOR_STATE_INIT;

// True when LPM is not enabled yet or wake is not asserted.
// True when LPM is not enabled yet or wake is not asserted.
bool lpm_wake_deasserted;
bool lpm_wake_deasserted;
uint32_t lpm_timeout_ms;
uint32_t lpm_timeout_ms;
bool recent_activity_flag;
bool recent_activity_flag;


VendorInterface* g_vendor_interface = nullptr;
VendorInterface* g_vendor_interface = nullptr;
static VendorInterface vendor_interface;
std::mutex wakeup_mutex_;


HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
HC_BT_HDR* WrapPacketAndCopy(uint16_t event, const hidl_vec<uint8_t>& data) {
  size_t packet_size = data.size() + sizeof(HC_BT_HDR);
  size_t packet_size = data.size() + sizeof(HC_BT_HDR);
@@ -181,8 +167,11 @@ bool VendorInterface::Initialize(
    InitializeCompleteCallback initialize_complete_cb,
    InitializeCompleteCallback initialize_complete_cb,
    PacketReadCallback event_cb, PacketReadCallback acl_cb,
    PacketReadCallback event_cb, PacketReadCallback acl_cb,
    PacketReadCallback sco_cb, PacketReadCallback iso_cb) {
    PacketReadCallback sco_cb, PacketReadCallback iso_cb) {
  ALOGI("%s: VendorInterface::Initialize", __func__);
  if (g_vendor_interface) {
  g_vendor_interface = &vendor_interface;
    ALOGE("%s: No previous Shutdown()?", __func__);
    return false;
  }
  g_vendor_interface = new VendorInterface();
  return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
  return g_vendor_interface->Open(initialize_complete_cb, event_cb, acl_cb,
                                  sco_cb, iso_cb);
                                  sco_cb, iso_cb);
}
}
@@ -190,8 +179,9 @@ bool VendorInterface::Initialize(
void VendorInterface::Shutdown() {
void VendorInterface::Shutdown() {
  LOG_ALWAYS_FATAL_IF(!g_vendor_interface, "%s: No Vendor interface!",
  LOG_ALWAYS_FATAL_IF(!g_vendor_interface, "%s: No Vendor interface!",
                      __func__);
                      __func__);
  ALOGI("%s: VendorInterface::Shutdown", __func__);
  g_vendor_interface->Close();
  g_vendor_interface->Close();
  delete g_vendor_interface;
  g_vendor_interface = nullptr;
}
}


VendorInterface* VendorInterface::get() { return g_vendor_interface; }
VendorInterface* VendorInterface::get() { return g_vendor_interface; }
@@ -201,23 +191,8 @@ bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
                           PacketReadCallback acl_cb,
                           PacketReadCallback acl_cb,
                           PacketReadCallback sco_cb,
                           PacketReadCallback sco_cb,
                           PacketReadCallback iso_cb) {
                           PacketReadCallback iso_cb) {
  {
    std::unique_lock<std::mutex> guard(vendor_mutex_);
    if (vstate == VENDOR_STATE_OPENED) {
      ALOGW("VendorInterface opened!");
      return true;
    }

    if ((vstate == VENDOR_STATE_CLOSING) ||
        (vstate == VENDOR_STATE_OPENING)) {
      ALOGW("VendorInterface open/close is on-going !");
      return true;
    }

    vstate = VENDOR_STATE_OPENING;
    ALOGI("%s: VendorInterface::Open", __func__);

  initialize_complete_cb_ = initialize_complete_cb;
  initialize_complete_cb_ = initialize_complete_cb;

  // Initialize vendor interface
  // Initialize vendor interface


  lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
  lib_handle_ = dlopen(VENDOR_LIBRARY_NAME, RTLD_NOW);
@@ -237,10 +212,9 @@ bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,


  // Get the local BD address
  // Get the local BD address


    uint8_t local_bda[BluetoothAddress::kBytes] = {0, 0, 0, 0, 0, 0};
  uint8_t local_bda[BluetoothAddress::kBytes];
  if (!BluetoothAddress::get_local_address(local_bda)) {
  if (!BluetoothAddress::get_local_address(local_bda)) {
      // BT driver will get BD address from NVRAM for MTK solution
    LOG_ALWAYS_FATAL("%s: No Bluetooth Address!", __func__);
      ALOGW("%s: No pre-set Bluetooth Address!", __func__);
  }
  }
  int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
  int status = lib_interface_->init(&lib_callbacks, (unsigned char*)local_bda);
  if (status) {
  if (status) {
@@ -289,8 +263,7 @@ bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
    fd_watcher_.WatchFdForNonBlockingReads(
    fd_watcher_.WatchFdForNonBlockingReads(
        fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
        fd_list[CH_EVT], [mct_hci](int fd) { mct_hci->OnEventDataReady(fd); });
    fd_watcher_.WatchFdForNonBlockingReads(
    fd_watcher_.WatchFdForNonBlockingReads(
          fd_list[CH_ACL_IN],
        fd_list[CH_ACL_IN], [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
          [mct_hci](int fd) { mct_hci->OnAclDataReady(fd); });
    hci_ = mct_hci;
    hci_ = mct_hci;
  }
  }


@@ -301,33 +274,19 @@ bool VendorInterface::Open(InitializeCompleteCallback initialize_complete_cb,
  firmware_startup_timer_ = new FirmwareStartupTimer();
  firmware_startup_timer_ = new FirmwareStartupTimer();
  lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);
  lib_interface_->op(BT_VND_OP_FW_CFG, nullptr);


    vstate = VENDOR_STATE_OPENED;
    ALOGI("%s: VendorInterface::Open done!!!", __func__);
  }  // vendor_mutex_ done
  return true;
  return true;
}
}


void VendorInterface::Close() {
void VendorInterface::Close() {
  // These callbacks may send HCI events (vendor-dependent), so make sure to
  // These callbacks may send HCI events (vendor-dependent), so make sure to
  // StopWatching the file descriptor after this.
  // StopWatching the file descriptor after this.

  if (vstate != VENDOR_STATE_OPENED) {
    ALOGW("VendorInterface is not allow close(%d)", vstate);
    return;
  }
  vstate = VENDOR_STATE_CLOSING;
  ALOGI("%s: VendorInterface::Close", __func__);

  if (lib_interface_ != nullptr) {
  if (lib_interface_ != nullptr) {
    lib_interface_->cleanup();
    bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
    bt_vendor_lpm_mode_t mode = BT_VND_LPM_DISABLE;
    lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
    lib_interface_->op(BT_VND_OP_LPM_SET_MODE, &mode);
  }
  }


  {
    std::unique_lock<std::mutex> guard(vendor_mutex_);

  fd_watcher_.StopWatchingFileDescriptors();
  fd_watcher_.StopWatchingFileDescriptors();

  if (hci_ != nullptr) {
  if (hci_ != nullptr) {
    delete hci_;
    delete hci_;
    hci_ = nullptr;
    hci_ = nullptr;
@@ -339,6 +298,7 @@ void VendorInterface::Close() {
    int power_state = BT_VND_PWR_OFF;
    int power_state = BT_VND_PWR_OFF;
    lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);
    lib_interface_->op(BT_VND_OP_POWER_CTRL, &power_state);


    lib_interface_->cleanup();
    lib_interface_ = nullptr;
    lib_interface_ = nullptr;
  }
  }


@@ -351,26 +311,12 @@ void VendorInterface::Close() {
    delete firmware_startup_timer_;
    delete firmware_startup_timer_;
    firmware_startup_timer_ = nullptr;
    firmware_startup_timer_ = nullptr;
  }
  }
    vstate = VENDOR_STATE_CLOSED;
  }  // vendor_mutex_ done
  ALOGI("%s: VendorInterface::Close done!!!", __func__);
}
}


size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
  {
  std::unique_lock<std::mutex> lock(wakeup_mutex_);
    std::unique_lock<std::mutex> guard(vendor_mutex_);

    if (vstate != VENDOR_STATE_OPENED) {
      ALOGW("VendorInterface is not open yet(%d)!", vstate);
      return 0;
    }
    ALOGI("%s: VendorInterface::Send", __func__);

    if (lib_interface_ == nullptr) {
      ALOGE("lib_interface_ is null");
      return 0;
    }
  recent_activity_flag = true;
  recent_activity_flag = true;

  if (lpm_wake_deasserted == true) {
  if (lpm_wake_deasserted == true) {
    // Restart the timer.
    // Restart the timer.
    fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
    fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
@@ -382,8 +328,7 @@ size_t VendorInterface::Send(uint8_t type, const uint8_t* data, size_t length) {
    ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
    ALOGV("%s: Sent wake before (%02x)", __func__, data[0] | (data[1] << 8));
  }
  }


    return hci_ ? hci_->Send(type, data, length) : 0;
  return hci_->Send(type, data, length);
  }  // vendor_mutex_ done
}
}


void VendorInterface::OnFirmwareConfigured(uint8_t result) {
void VendorInterface::OnFirmwareConfigured(uint8_t result) {
@@ -394,17 +339,11 @@ void VendorInterface::OnFirmwareConfigured(uint8_t result) {
    firmware_startup_timer_ = nullptr;
    firmware_startup_timer_ = nullptr;
  }
  }


  {
    std::unique_lock<std::mutex> guard(initcb_mutex_);
    ALOGD("%s OnFirmwareConfigured get lock", __func__);
  if (initialize_complete_cb_ != nullptr) {
  if (initialize_complete_cb_ != nullptr) {
      LOG_ALWAYS_FATAL_IF((result != 0),
          "%s: Failed to init firmware!", __func__);
    initialize_complete_cb_(result == 0);
    initialize_complete_cb_(result == 0);
    initialize_complete_cb_ = nullptr;
  }
  }
  }  // initcb_mutex_ done


  if (lib_interface_ != nullptr) {
  lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
  lib_interface_->op(BT_VND_OP_GET_LPM_IDLE_TIMEOUT, &lpm_timeout_ms);
  ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);
  ALOGI("%s: lpm_timeout_ms %d", __func__, lpm_timeout_ms);


@@ -415,15 +354,10 @@ void VendorInterface::OnFirmwareConfigured(uint8_t result) {
  fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
  fd_watcher_.ConfigureTimeout(std::chrono::milliseconds(lpm_timeout_ms),
                               [this]() { OnTimeout(); });
                               [this]() { OnTimeout(); });
}
}
  else {
    ALOGE("lib_interface_ is null");
  }

  initialize_complete_cb_ = nullptr;
}


void VendorInterface::OnTimeout() {
void VendorInterface::OnTimeout() {
  ALOGV("%s", __func__);
  ALOGV("%s", __func__);
  std::unique_lock<std::mutex> lock(wakeup_mutex_);
  if (recent_activity_flag == false) {
  if (recent_activity_flag == false) {
    lpm_wake_deasserted = true;
    lpm_wake_deasserted = true;
    bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
    bt_vendor_lpm_wake_state_t wakeState = BT_VND_LPM_WAKE_DEASSERT;
+2 −3
Original line number Original line Diff line number Diff line
@@ -22,8 +22,6 @@
#include "bt_vendor_lib.h"
#include "bt_vendor_lib.h"
#include "hci_protocol.h"
#include "hci_protocol.h"


extern std::mutex initcb_mutex_;

namespace android {
namespace android {
namespace hardware {
namespace hardware {
namespace bluetooth {
namespace bluetooth {
@@ -47,9 +45,10 @@ class VendorInterface {
  size_t Send(uint8_t type, const uint8_t* data, size_t length);
  size_t Send(uint8_t type, const uint8_t* data, size_t length);


  void OnFirmwareConfigured(uint8_t result);
  void OnFirmwareConfigured(uint8_t result);
  virtual ~VendorInterface() = default;


 private:
 private:
  virtual ~VendorInterface() = default;

  bool Open(InitializeCompleteCallback initialize_complete_cb,
  bool Open(InitializeCompleteCallback initialize_complete_cb,
            PacketReadCallback event_cb, PacketReadCallback acl_cb,
            PacketReadCallback event_cb, PacketReadCallback acl_cb,
            PacketReadCallback sco_cb, PacketReadCallback iso_cb);
            PacketReadCallback sco_cb, PacketReadCallback iso_cb);