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

Commit f89c914b authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Merge "Bluetooth: Protect pointers and callbacks" am: 54906b6c am: ce78f9fa

am: 4cfd84a0

Change-Id: I6325f5c93c1edd58c7813c5ab7bec9bb8b3f6aef
parents dc039363 4cfd84a0
Loading
Loading
Loading
Loading
+36 −17
Original line number Diff line number Diff line
@@ -15,9 +15,10 @@
//

#define LOG_TAG "android.hardware.bluetooth@1.0-impl"
#include "bluetooth_hci.h"

#include <utils/Log.h>

#include "bluetooth_hci.h"
#include "vendor_interface.h"

namespace android {
@@ -38,59 +39,77 @@ class BluetoothDeathRecipient : public hidl_death_recipient {
      uint64_t /*cookie*/,
      const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
    ALOGE("BluetoothDeathRecipient::serviceDied - Bluetooth service died");
    has_died_ = true;
    mHci->close();
  }
  sp<IBluetoothHci> mHci;
  bool getHasDied() const { return has_died_; }
  void setHasDied(bool has_died) { has_died_ = has_died; }

 private:
  bool has_died_;
};

BluetoothHci::BluetoothHci()
    : deathRecipient(new BluetoothDeathRecipient(this)) {}
    : death_recipient_(new BluetoothDeathRecipient(this)) {}

Return<void> BluetoothHci::initialize(
    const ::android::sp<IBluetoothHciCallbacks>& cb) {
  ALOGW("BluetoothHci::initialize()");
  cb->linkToDeath(deathRecipient, 0);
  event_cb_ = cb;
  ALOGI("BluetoothHci::initialize()");
  if (cb == nullptr) {
    ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
    return Void();
  }

  death_recipient_->setHasDied(false);
  cb->linkToDeath(death_recipient_, 0);

  bool rc = VendorInterface::Initialize(
      [this](bool status) {
        auto hidl_status = event_cb_->initializationComplete(
      [cb](bool status) {
        auto hidl_status = cb->initializationComplete(
            status ? Status::SUCCESS : Status::INITIALIZATION_ERROR);
        if (!hidl_status.isOk()) {
          ALOGE("VendorInterface -> Unable to call initializationComplete()");
        }
      },
      [this](const hidl_vec<uint8_t>& packet) {
        auto hidl_status = event_cb_->hciEventReceived(packet);
      [cb](const hidl_vec<uint8_t>& packet) {
        auto hidl_status = cb->hciEventReceived(packet);
        if (!hidl_status.isOk()) {
          ALOGE("VendorInterface -> Unable to call hciEventReceived()");
        }
      },
      [this](const hidl_vec<uint8_t>& packet) {
        auto hidl_status = event_cb_->aclDataReceived(packet);
      [cb](const hidl_vec<uint8_t>& packet) {
        auto hidl_status = cb->aclDataReceived(packet);
        if (!hidl_status.isOk()) {
          ALOGE("VendorInterface -> Unable to call aclDataReceived()");
        }
      },
      [this](const hidl_vec<uint8_t>& packet) {
        auto hidl_status = event_cb_->scoDataReceived(packet);
      [cb](const hidl_vec<uint8_t>& packet) {
        auto hidl_status = cb->scoDataReceived(packet);
        if (!hidl_status.isOk()) {
          ALOGE("VendorInterface -> Unable to call scoDataReceived()");
        }
      });
  if (!rc) {
    auto hidl_status =
        event_cb_->initializationComplete(Status::INITIALIZATION_ERROR);
    auto hidl_status = cb->initializationComplete(Status::INITIALIZATION_ERROR);
    if (!hidl_status.isOk()) {
      ALOGE("VendorInterface -> Unable to call initializationComplete(ERR)");
    }
  }

  unlink_cb_ = [cb](sp<BluetoothDeathRecipient>& death_recipient) {
    if (death_recipient->getHasDied())
      ALOGI("Skipping unlink call, service died.");
    else
      cb->unlinkToDeath(death_recipient);
  };

  return Void();
}

Return<void> BluetoothHci::close() {
  ALOGW("BluetoothHci::close()");
  event_cb_->unlinkToDeath(deathRecipient);
  ALOGI("BluetoothHci::close()");
  unlink_cb_(death_recipient_);
  VendorInterface::Shutdown();
  return Void();
}
+2 −2
Original line number Diff line number Diff line
@@ -44,8 +44,8 @@ class BluetoothHci : public IBluetoothHci {

 private:
  void sendDataToController(const uint8_t type, const hidl_vec<uint8_t>& data);
  ::android::sp<IBluetoothHciCallbacks> event_cb_;
  ::android::sp<BluetoothDeathRecipient> deathRecipient;
  ::android::sp<BluetoothDeathRecipient> death_recipient_;
  std::function<void(sp<BluetoothDeathRecipient>&)> unlink_cb_;
};

extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name);