Fix syncronization issue if LoaderService script perform failed.

Bug: 69876039
Test: VTS, perform LoaderService script
Change-Id: I8e626e6bbe2cd1fe82d2a73b225b3f39109ee05d
diff --git a/1.0/SecureElement.cpp b/1.0/SecureElement.cpp
index f02a454..bcc282b 100755
--- a/1.0/SecureElement.cpp
+++ b/1.0/SecureElement.cpp
@@ -34,7 +34,6 @@
 
 SecureElement::SecureElement()
     : mOpenedchannelCount(0),
-      mIsEseInitialized(false),
       mOpenedChannels{false, false, false, false} {}
 
 Return<void> SecureElement::init(
@@ -51,7 +50,7 @@
       ALOGE("%s: Failed to register death notification", __func__);
     }
   }
-  if (mIsEseInitialized) {
+  if (isSeInitialized()) {
     clientCallback->onStateChange(true);
     return Void();
   }
@@ -124,7 +123,7 @@
   resApduBuff.channelNumber = 0xff;
   memset(&resApduBuff, 0x00, sizeof(resApduBuff));
 
-  if (!mIsEseInitialized) {
+  if (!isSeInitialized()) {
     ESESTATUS status = seHalInit();
     if (status != ESESTATUS_SUCCESS) {
       ALOGE("%s: seHalInit Failed!!!", __func__);
@@ -245,7 +244,7 @@
                                              openBasicChannel_cb _hidl_cb) {
   hidl_vec<uint8_t> result;
 
-  if (!mIsEseInitialized) {
+  if (!isSeInitialized()) {
     ESESTATUS status = seHalInit();
     if (status != ESESTATUS_SUCCESS) {
       ALOGE("%s: seHalInit Failed!!!", __func__);
@@ -384,6 +383,8 @@
   }
 }
 
+bool SecureElement::isSeInitialized() { return phNxpEse_isOpen(); }
+
 ESESTATUS SecureElement::seHalInit() {
   ESESTATUS status = ESESTATUS_SUCCESS;
   phNxpEse_initParams initParams;
@@ -397,8 +398,6 @@
     status = phNxpEse_init(initParams);
     if (status != ESESTATUS_SUCCESS) {
       ALOGE("%s: SecureElement init failed!!!", __func__);
-    } else {
-      mIsEseInitialized = true;
     }
   }
   return status;
@@ -416,7 +415,6 @@
     if (status != ESESTATUS_SUCCESS) {
       sestatus = SecureElementStatus::FAILED;
     } else {
-      mIsEseInitialized = false;
       sestatus = SecureElementStatus::SUCCESS;
 
       for (uint8_t xx = 0; xx < MAX_LOGICAL_CHANNELS; xx++) {
diff --git a/1.0/SecureElement.h b/1.0/SecureElement.h
index d287880..6e5c4a7 100755
--- a/1.0/SecureElement.h
+++ b/1.0/SecureElement.h
@@ -64,12 +64,12 @@
 
  private:
   uint8_t mOpenedchannelCount = 0;
-  bool mIsEseInitialized = false;
   bool mOpenedChannels[MAX_LOGICAL_CHANNELS];
   static sp<V1_0::ISecureElementHalCallback> mCallbackV1_0;
   Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
   seHalDeInit();
   ESESTATUS seHalInit();
+  bool isSeInitialized();
 };
 
 }  // namespace implementation
diff --git a/libese-spi/p73/inc/phNxpEse_Api.h b/libese-spi/p73/inc/phNxpEse_Api.h
index c93367b..fb31af1 100755
--- a/libese-spi/p73/inc/phNxpEse_Api.h
+++ b/libese-spi/p73/inc/phNxpEse_Api.h
@@ -84,6 +84,16 @@
 /******************************************************************************
  * \ingroup spi_libese
  *
+ * \brief  Check if libese has opened
+ *
+ * \retval return false if it is close, otherwise true.
+ *
+ ******************************************************************************/
+bool phNxpEse_isOpen();
+
+/******************************************************************************
+ * \ingroup spi_libese
+ *
  * \brief  This function is used to communicate from nfc-hal to ese-hal
  *
  * \retval This function return ESESTATUS_SUCCES (0) in case of success
@@ -91,6 +101,7 @@
  *
  ******************************************************************************/
 ESESTATUS phNxpEse_spiIoctl(uint64_t ioctlType, void* p_data);
+
 /**
  * \ingroup spi_libese
  * \brief This function is called by Jni during the
diff --git a/libese-spi/p73/lib/phNxpEse_Api.cpp b/libese-spi/p73/lib/phNxpEse_Api.cpp
index 73472ed..6366350 100755
--- a/libese-spi/p73/lib/phNxpEse_Api.cpp
+++ b/libese-spi/p73/lib/phNxpEse_Api.cpp
@@ -322,6 +322,16 @@
 }
 
 /******************************************************************************
+ * \ingroup spi_libese
+ *
+ * \brief  Check if libese has opened
+ *
+ * \retval return false if it is close, otherwise true.
+ *
+ ******************************************************************************/
+bool phNxpEse_isOpen() { return nxpese_ctxt.EseLibStatus != ESE_STATUS_CLOSE; }
+
+/******************************************************************************
  * Function         phNxpEse_openPrioSession
  *
  * Description      This function is called by Jni during the
diff --git a/libese-spi/src/adaptation/NfcAdaptation.cpp b/libese-spi/src/adaptation/NfcAdaptation.cpp
index 35fb2d9..76ca268 100755
--- a/libese-spi/src/adaptation/NfcAdaptation.cpp
+++ b/libese-spi/src/adaptation/NfcAdaptation.cpp
@@ -29,7 +29,7 @@
 using android::hardware::hidl_vec;
 using vendor::nxp::nxpnfc::V1_0::INxpNfc;
 
-sp<INxpNfc> NfcAdaptation::mHalNxpNfc;
+sp<INxpNfc> NfcAdaptation::mHalNxpNfc = nullptr;
 ThreadMutex NfcAdaptation::sIoctlLock;
 NfcAdaptation* NfcAdaptation::mpInstance = NULL;
 ThreadMutex NfcAdaptation::sLock;
@@ -39,6 +39,7 @@
 void NfcAdaptation::Initialize() {
   const char* func = "NfcAdaptation::Initialize";
   ALOGD_IF(ese_debug_enabled, "%s", func);
+  if (mHalNxpNfc != nullptr) return;
   mHalNxpNfc = INxpNfc::tryGetService();
   LOG_FATAL_IF(mHalNxpNfc == nullptr, "Failed to retrieve the NXP NFC HAL!");
   if (mHalNxpNfc != nullptr) {