Bluetooth: deal with framework going away.

Bug: 35624326
Test: Bluetooth HAL stays alive and works after com.android.bluetooth
is killed.

Change-Id: Id5b5a5b88cd35202e5737a0c99d28db9bebb4250
(cherry picked from commit 678af7f5f81148cf49c06eda676b8da15d4f6e70)
diff --git a/bluetooth/1.0/default/bluetooth_hci.cc b/bluetooth/1.0/default/bluetooth_hci.cc
index 6cea623..1d6e600 100644
--- a/bluetooth/1.0/default/bluetooth_hci.cc
+++ b/bluetooth/1.0/default/bluetooth_hci.cc
@@ -30,9 +30,13 @@
 static const uint8_t HCI_DATA_TYPE_ACL = 2;
 static const uint8_t HCI_DATA_TYPE_SCO = 3;
 
+BluetoothHci::BluetoothHci()
+    : deathRecipient(new BluetoothDeathRecipient(this)) {}
+
 Return<void> BluetoothHci::initialize(
     const ::android::sp<IBluetoothHciCallbacks>& cb) {
   ALOGW("BluetoothHci::initialize()");
+  cb->linkToDeath(deathRecipient, 0);
   event_cb_ = cb;
 
   bool rc = VendorInterface::Initialize(
@@ -62,6 +66,7 @@
 
 Return<void> BluetoothHci::close() {
   ALOGW("BluetoothHci::close()");
+  event_cb_->unlinkToDeath(deathRecipient);
   VendorInterface::Shutdown();
   return Void();
 }
diff --git a/bluetooth/1.0/default/bluetooth_hci.h b/bluetooth/1.0/default/bluetooth_hci.h
index da1b411..67d6c37 100644
--- a/bluetooth/1.0/default/bluetooth_hci.h
+++ b/bluetooth/1.0/default/bluetooth_hci.h
@@ -30,8 +30,20 @@
 using ::android::hardware::Return;
 using ::android::hardware::hidl_vec;
 
+struct BluetoothDeathRecipient : hidl_death_recipient {
+  BluetoothDeathRecipient(const sp<IBluetoothHci> hci) : mHci(hci) {}
+
+  virtual void serviceDied(
+      uint64_t /*cookie*/,
+      const wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
+    mHci->close();
+  }
+  sp<IBluetoothHci> mHci;
+};
+
 class BluetoothHci : public IBluetoothHci {
  public:
+  BluetoothHci();
   Return<void> initialize(
       const ::android::sp<IBluetoothHciCallbacks>& cb) override;
   Return<void> sendHciCommand(const hidl_vec<uint8_t>& packet) override;
@@ -42,6 +54,7 @@
  private:
   void sendDataToController(const uint8_t type, const hidl_vec<uint8_t>& data);
   ::android::sp<IBluetoothHciCallbacks> event_cb_;
+  ::android::sp<BluetoothDeathRecipient> deathRecipient;
 };
 
 extern "C" IBluetoothHci* HIDL_FETCH_IBluetoothHci(const char* name);