Update eSE reset am: a3cb96627c am: 29bfbc5be3 am: ab45c38b84

Original change: https://android-review.googlesource.com/c/platform/hardware/st/secure_element/+/1488397

Change-Id: I6fe5b3ba8ed8855dc6a339dcc8471110311b1982
diff --git a/1.2/SecureElement.cpp b/1.2/SecureElement.cpp
index 0276ae6..0ec90ae 100644
--- a/1.2/SecureElement.cpp
+++ b/1.2/SecureElement.cpp
@@ -17,11 +17,16 @@
  *
  ******************************************************************************/
 #define LOG_TAG "StEse-SecureElement"
+#include "SecureElement.h"
+#include <android-base/properties.h>
 #include <android_logmsg.h>
-
+#include <dlfcn.h>
 #include <stdlib.h>
 #include <string.h>
-#include "SecureElement.h"
+
+#define VENDOR_LIB_PATH "/vendor/lib64/"
+#define VENDOR_LIB_EXT ".so"
+typedef int (*STEseReset)(void);
 
 extern bool ese_debug_enabled;
 static bool OpenLogicalChannelProcessing = false;
@@ -517,14 +522,35 @@
 
 Return<::android::hardware::secure_element::V1_0::SecureElementStatus>
 SecureElement::reset() {
+  int ret = 0;
   ESESTATUS status = ESESTATUS_SUCCESS;
   SecureElementStatus sestatus = SecureElementStatus::FAILED;
+  std::string valueStr =
+      android::base::GetProperty("persist.vendor.se.streset", "");
 
   STLOG_HAL_D("%s: Enter", __func__);
   if (!isSeInitialized()) {
     ESESTATUS status = seHalInit();
     if (status != ESESTATUS_SUCCESS) {
       STLOG_HAL_E("%s: seHalInit Failed!!!", __func__);
+      if (valueStr.length() > 0) {
+        valueStr = VENDOR_LIB_PATH + valueStr + VENDOR_LIB_EXT;
+        void* stdll = dlopen(valueStr.c_str(), RTLD_NOW);
+        if (stdll) {
+          STEseReset fn = (STEseReset)dlsym(stdll, "direct_reset");
+          if (fn) {
+            STLOG_HAL_E("STReset direct reset");
+            ret = fn();
+            STLOG_HAL_E("STReset result=%d", ret);
+            if (ret == 0) {
+              STLOG_HAL_E("STReset pass, retry seHalInit()");
+              status = seHalInit();
+            }
+          }
+        } else {
+          STLOG_HAL_D("%s not found, do nothing.", valueStr.c_str());
+        }
+      }
     }
   }
 
diff --git a/1.2/android.hardware.secure_element@1.2-service.st.rc b/1.2/android.hardware.secure_element@1.2-service.st.rc
index 2e40811..3f94a31 100644
--- a/1.2/android.hardware.secure_element@1.2-service.st.rc
+++ b/1.2/android.hardware.secure_element@1.2-service.st.rc
@@ -1,4 +1,4 @@
 service secure_element_hal_service /vendor/bin/hw/android.hardware.secure_element@1.2-service.st
     class hal
     user secure_element
-    group secure_element
+    group secure_element nfc
diff --git a/ese-spi-driver/SpiLayerInterface.cc b/ese-spi-driver/SpiLayerInterface.cc
index 11a7065..e2c4fd5 100644
--- a/ese-spi-driver/SpiLayerInterface.cc
+++ b/ese-spi-driver/SpiLayerInterface.cc
@@ -156,10 +156,13 @@
 **
 *******************************************************************************/
 int SpiLayerInterface_setup() {
+  const char ese_reset_property[] = "persist.vendor.se.reset";
   // First of all, read the ATP from the slave
   if (SpiLayerComm_readAtp() != 0) {
     // Error reading the ATP
     STLOG_HAL_E("Error reading the ATP.");
+    // eSE needs cold reset.
+    property_set(ese_reset_property, "needed");
     return -1;
   }
   T1protocol_resetSequenceNumbers();
@@ -167,5 +170,7 @@
   if (T1protocol_doRequestIFS() != 0) {
     return -1;
   }
+  // Set noneed if SPI worked normally.
+  property_set(ese_reset_property, "noneed");
   return 0;
 }