Revert "Avoid calling back to dead service"

This reverts commit 0057907c36df6b0bb45e146281cec410a968340b.

Reason for revert: internal build break
Bug: 215090722
Change-Id: Ib0726aa384c5d9b4059e5509ed0fadc9fa091b6f
diff --git a/1.2/Nfc.cpp b/1.2/Nfc.cpp
index f7d0ec2..3273294 100644
--- a/1.2/Nfc.cpp
+++ b/1.2/Nfc.cpp
@@ -44,11 +44,8 @@
     ALOGD_IF(nfc_debug_enabled,"Nfc::open null callback");
     return V1_0::NfcStatus::FAILED;
   } else {
-    pthread_mutex_lock(&mLockOpenClose);
-    mOpenCount++;
     mCallbackV1_1 = clientCallback;
-    mCallbackV1_1->linkToDeath(this, mOpenCount /*cookie*/);
-    pthread_mutex_unlock(&mLockOpenClose);
+    mCallbackV1_1->linkToDeath(this, 0 /*cookie*/);
   }
   return open(clientCallback);
 }
@@ -57,20 +54,16 @@
 Return<V1_0::NfcStatus> Nfc::open(
     const sp<V1_0::INfcClientCallback>& clientCallback) {
   ALOGD_IF(nfc_debug_enabled, "Nfc::open Enter");
-  pthread_mutex_lock(&mLockOpenClose);
-  if (mCallbackV1_1 == nullptr) mOpenCount++;
   if (clientCallback == nullptr) {
     ALOGD_IF(nfc_debug_enabled, "Nfc::open null callback");
-    pthread_mutex_unlock(&mLockOpenClose);
     return V1_0::NfcStatus::FAILED;
   } else {
     mCallbackV1_0 = clientCallback;
-    mCallbackV1_0->linkToDeath(this, mOpenCount /*cookie*/);
+    mCallbackV1_0->linkToDeath(this, 0 /*cookie*/);
   }
 
   int ret = StNfc_hal_open(eventCallback, dataCallback);
-  ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit (count:%ld)", mOpenCount);
-  pthread_mutex_unlock(&mLockOpenClose);
+  ALOGD_IF(nfc_debug_enabled, "Nfc::open Exit");
   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
 }
 
@@ -97,7 +90,9 @@
 }
 
 Return<V1_0::NfcStatus> Nfc::close() {
-  pthread_mutex_lock(&mLockOpenClose);
+  if (mCallbackV1_1 == nullptr && mCallbackV1_0 == nullptr) {
+    return V1_0::NfcStatus::FAILED;
+  }
   int ret = StNfc_hal_close(NFC_MODE_OFF);
 
   if (mCallbackV1_1 != nullptr) {
@@ -108,7 +103,6 @@
     mCallbackV1_0->unlinkToDeath(this);
     mCallbackV1_0 = nullptr;
   }
-  pthread_mutex_unlock(&mLockOpenClose);
   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
 }
 
@@ -133,7 +127,6 @@
     return V1_0::NfcStatus::FAILED;
   }
 
-  pthread_mutex_lock(&mLockOpenClose);
   int ret = StNfc_hal_closeForPowerOffCase();
 
   if (mCallbackV1_1 != nullptr) {
@@ -144,7 +137,6 @@
     mCallbackV1_0->unlinkToDeath(this);
     mCallbackV1_0 = nullptr;
   }
-  pthread_mutex_unlock(&mLockOpenClose);
   return ret == 0 ? V1_0::NfcStatus::OK : V1_0::NfcStatus::FAILED;
 }
 
diff --git a/1.2/Nfc.h b/1.2/Nfc.h
index 36ed706..77657d8 100644
--- a/1.2/Nfc.h
+++ b/1.2/Nfc.h
@@ -24,7 +24,6 @@
 #include <hidl/MQDescriptor.h>
 #include <hidl/Status.h>
 #include <log/log.h>
-#include <pthread.h>
 
 namespace android {
 namespace hardware {
@@ -97,32 +96,13 @@
     }
   }
 
-  virtual void serviceDied(uint64_t cookie, const wp<IBase>& /*who*/) {
-    pthread_mutex_lock(&mLockOpenClose);
-    ALOGE("serviceDied!!! %ld, %ld, %s, %s", cookie, mOpenCount,
-          (mCallbackV1_0 == nullptr ? "null" : "defined"),
-          (mCallbackV1_1 == nullptr ? "null" : "defined"));
-    if (cookie == mOpenCount) {
-      if (mCallbackV1_1 != nullptr) {
-        mCallbackV1_1->unlinkToDeath(this);
-        mCallbackV1_1 = nullptr;
-      }
-      if (mCallbackV1_0 != nullptr) {
-        mCallbackV1_0->unlinkToDeath(this);
-        mCallbackV1_0 = nullptr;
-      }
-      pthread_mutex_unlock(&mLockOpenClose);
-      close();
-    } else {
-      pthread_mutex_unlock(&mLockOpenClose);
-    }
+  virtual void serviceDied(uint64_t /*cookie*/, const wp<IBase>& /*who*/) {
+    close();
   }
 
  private:
   static sp<V1_1::INfcClientCallback> mCallbackV1_1;
   static sp<V1_0::INfcClientCallback> mCallbackV1_0;
-  pthread_mutex_t mLockOpenClose = PTHREAD_MUTEX_INITIALIZER;
-  uint64_t mOpenCount = 0;
 };
 
 }  // namespace implementation
diff --git a/1.2/hal_st21nfc.cc b/1.2/hal_st21nfc.cc
index 5efc6b1..74e8c2c 100644
--- a/1.2/hal_st21nfc.cc
+++ b/1.2/hal_st21nfc.cc
@@ -367,7 +367,7 @@
     (void)pthread_mutex_unlock(&hal_mtx);
     return 1;
   }
-  if (hal_wrapper_close(1, nfc_mode_value) == -1) {
+  if (hal_wrapper_close(1, nfc_mode_value) == 0) {
     hal_is_closed = 1;
     (void)pthread_mutex_unlock(&hal_mtx);
     return 1;