Adds AIDL sensors HAL crash handling

Bug: 195593357
Test: Verify reconnect works by manually crashing sensors HAL
Change-Id: I2ea382e0d0ecadeb9aab7c427a2e9a0be71dd861
diff --git a/services/sensorservice/AidlSensorHalWrapper.cpp b/services/sensorservice/AidlSensorHalWrapper.cpp
index 10f52e0..74c47ba 100644
--- a/services/sensorservice/AidlSensorHalWrapper.cpp
+++ b/services/sensorservice/AidlSensorHalWrapper.cpp
@@ -376,6 +376,11 @@
     }
 }
 
+void serviceDied(void *cookie) {
+    ALOGW("Sensors HAL died, attempting to reconnect.");
+    ((AidlSensorHalWrapper *)cookie)->prepareForReconnect();
+}
+
 template <typename EnumType>
 constexpr typename std::underlying_type<EnumType>::type asBaseType(EnumType value) {
     return static_cast<typename std::underlying_type<EnumType>::type>(value);
@@ -415,6 +420,11 @@
     ISensorHalWrapper::SensorDeviceCallback *mSensorDeviceCallback;
 };
 
+AidlSensorHalWrapper::AidlSensorHalWrapper()
+      : mEventQueueFlag(nullptr),
+        mWakeLockQueueFlag(nullptr),
+        mDeathRecipient(AIBinder_DeathRecipient_new(serviceDied)) {}
+
 bool AidlSensorHalWrapper::supportsPolling() {
     return false;
 }
@@ -429,8 +439,13 @@
 
     auto aidlServiceName = std::string() + ISensors::descriptor + "/default";
     if (AServiceManager_isDeclared(aidlServiceName.c_str())) {
+        if (mSensors != nullptr) {
+            AIBinder_unlinkToDeath(mSensors->asBinder().get(), mDeathRecipient.get(), this);
+        }
+
         ndk::SpAIBinder binder(AServiceManager_waitForService(aidlServiceName.c_str()));
         if (binder.get() != nullptr) {
+
             mSensors = ISensors::fromBinder(binder);
             mEventQueue = std::make_unique<AidlMessageQueue<
                     Event, SynchronizedReadWrite>>(MAX_RECEIVE_BUFFER_EVENT_COUNT,
@@ -453,8 +468,9 @@
 
             mCallback = ndk::SharedRefBase::make<AidlSensorsCallback>(mSensorDeviceCallback);
             mSensors->initialize(mEventQueue->dupeDesc(), mWakeLockQueue->dupeDesc(), mCallback);
+
+            AIBinder_linkToDeath(mSensors->asBinder().get(), mDeathRecipient.get(), this);
         } else {
-            // TODO(b/195593357): Handle AIDL HAL crash
             ALOGE("Could not connect to declared sensors AIDL HAL");
         }
     }