OSDN Git Service

Handle Bluetooth HAL service died
authorUgo Yu <ugoyu@google.com>
Tue, 2 Apr 2019 13:55:14 +0000 (21:55 +0800)
committerUgo Yu <ugoyu@google.com>
Wed, 3 Apr 2019 16:43:23 +0000 (00:43 +0800)
Bug: 129320921
Test: kill Bluetooth HAL
Change-Id: I83c897f5a9def52b2050a3e73d97f99b1b71aa3b

hci/src/hci_layer_android.cc

index be0a7eb..92be0df 100644 (file)
 #define LOG_PATH "/data/misc/bluetooth/logs/firmware_events.log"
 #define LAST_LOG_PATH "/data/misc/bluetooth/logs/firmware_events.log.last"
 
-using android::hardware::bluetooth::V1_0::IBluetoothHci;
-using android::hardware::bluetooth::V1_0::IBluetoothHciCallbacks;
-using android::hardware::bluetooth::V1_0::HciPacket;
-using android::hardware::bluetooth::V1_0::Status;
-using android::hardware::ProcessState;
+using ::android::hardware::hidl_death_recipient;
+using ::android::hardware::hidl_vec;
+using ::android::hardware::ProcessState;
 using ::android::hardware::Return;
 using ::android::hardware::Void;
-using ::android::hardware::hidl_vec;
+using ::android::hardware::bluetooth::V1_0::HciPacket;
+using ::android::hardware::bluetooth::V1_0::IBluetoothHci;
+using ::android::hardware::bluetooth::V1_0::IBluetoothHciCallbacks;
+using ::android::hardware::bluetooth::V1_0::Status;
 
 extern void initialization_complete();
 extern void hci_event_received(const base::Location& from_here, BT_HDR* packet);
@@ -53,6 +54,15 @@ extern void sco_data_received(BT_HDR* packet);
 
 android::sp<IBluetoothHci> btHci;
 
+class BluetoothHciDeathRecipient : public hidl_death_recipient {
+ public:
+  virtual void serviceDied(uint64_t /*cookie*/, const android::wp<::android::hidl::base::V1_0::IBase>& /*who*/) {
+    LOG_ERROR(LOG_TAG, "Bluetooth HAL service died!");
+    abort();
+  }
+};
+android::sp<BluetoothHciDeathRecipient> bluetoothHciDeathRecipient = new BluetoothHciDeathRecipient();
+
 class BluetoothHciCallbacks : public IBluetoothHciCallbacks {
  public:
   BluetoothHciCallbacks() {
@@ -106,6 +116,11 @@ void hci_initialize() {
   btHci = IBluetoothHci::getService();
   // If android.hardware.bluetooth* is not found, Bluetooth can not continue.
   CHECK(btHci != nullptr);
+  auto death_link = btHci->linkToDeath(bluetoothHciDeathRecipient, 0);
+  if (!death_link.isOk()) {
+    LOG_ERROR(LOG_TAG, "%s: Unable to set the death recipient for the Bluetooth HAL", __func__);
+    abort();
+  }
   LOG_INFO(LOG_TAG, "%s: IBluetoothHci::getService() returned %p (%s)",
            __func__, btHci.get(), (btHci->isRemote() ? "remote" : "local"));
 
@@ -117,6 +132,12 @@ void hci_initialize() {
 }
 
 void hci_close() {
+  if (btHci != nullptr) {
+    auto death_unlink = btHci->unlinkToDeath(bluetoothHciDeathRecipient);
+    if (!death_unlink.isOk()) {
+      LOG_ERROR(LOG_TAG, "%s: Error unlinking death recipient from the Bluetooth HAL", __func__);
+    }
+  }
   btHci->close();
   btHci = nullptr;
 }