Add factory reset functionality for NFC HAL 1.1 (1/2)

Factory reset will be used to inform the HAL implementation that a
factory reset has taken place recently and it will have to do a full
initialization during the next open().

Test: manual
Bug: 70294869
Change-Id: I2786c07634ea561e29cc24c02f76649c3c5e9d79
diff --git a/src/Android.bp b/src/Android.bp
index a5f613a..0db231d 100644
--- a/src/Android.bp
+++ b/src/Android.bp
@@ -22,6 +22,7 @@
         "libhwbinder",
         "libutils",
         "android.hardware.nfc@1.0",
+        "android.hardware.nfc@1.1",
     ],
     static_libs: [
         "libnfcutils",
diff --git a/src/adaptation/NfcAdaptation.cc b/src/adaptation/NfcAdaptation.cc
index 292dc5c..254f947 100644
--- a/src/adaptation/NfcAdaptation.cc
+++ b/src/adaptation/NfcAdaptation.cc
@@ -16,7 +16,7 @@
  *
  ******************************************************************************/
 #include <android-base/stringprintf.h>
-#include <android/hardware/nfc/1.0/INfc.h>
+#include <android/hardware/nfc/1.1/INfc.h>
 #include <base/command_line.h>
 #include <base/logging.h>
 #include <cutils/properties.h>
@@ -40,6 +40,7 @@
 using android::hardware::Return;
 using android::hardware::Void;
 using android::hardware::nfc::V1_0::INfc;
+using INfcV1_1 = android::hardware::nfc::V1_1::INfc;
 using android::hardware::nfc::V1_0::INfcClientCallback;
 using android::hardware::hidl_vec;
 
@@ -56,6 +57,7 @@
 ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
 ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
 sp<INfc> NfcAdaptation::mHal;
+sp<INfcV1_1> NfcAdaptation::mHal_1_1;
 INfcClientCallback* NfcAdaptation::mCallback;
 
 bool nfc_debug_enabled = false;
@@ -157,7 +159,10 @@
 NfcAdaptation& NfcAdaptation::GetInstance() {
   AutoThreadMutex a(sLock);
 
-  if (!mpInstance) mpInstance = new NfcAdaptation;
+  if (!mpInstance) {
+    mpInstance = new NfcAdaptation;
+    mpInstance->InitializeHalDeviceContext();
+  }
   return *mpInstance;
 }
 
@@ -264,9 +269,6 @@
     mCondVar.wait();
   }
 
-  mHalCallback = NULL;
-  memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
-  InitializeHalDeviceContext();
   debug_nfcsnoop_init();
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
 }
@@ -289,13 +291,16 @@
 
   NfcConfig::clear();
 
-  mCallback = NULL;
-  memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
-
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
   delete this;
 }
 
+void NfcAdaptation::FactoryReset() {
+  if (mHal_1_1 != nullptr) {
+    mHal_1_1->factoryReset();
+  }
+}
+
 /*******************************************************************************
 **
 ** Function:    NfcAdaptation::Dump
@@ -385,7 +390,6 @@
 *******************************************************************************/
 void NfcAdaptation::InitializeHalDeviceContext() {
   const char* func = "NfcAdaptation::InitializeHalDeviceContext";
-  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: enter", func);
 
   mHalEntryFuncs.initialize = HalInitialize;
   mHalEntryFuncs.terminate = HalTerminate;
@@ -398,12 +402,14 @@
   mHalEntryFuncs.power_cycle = HalPowerCycle;
   mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
   LOG(INFO) << StringPrintf("%s: INfc::getService()", func);
-  mHal = INfc::getService();
+  mHal = mHal_1_1 = INfcV1_1::getService();
+  if (mHal_1_1 == nullptr) {
+    mHal = INfc::getService();
+  }
   LOG_FATAL_IF(mHal == nullptr, "Failed to retrieve the NFC HAL!");
   LOG(INFO) << StringPrintf("%s: INfc::getService() returned %p (%s)", func,
                             mHal.get(),
                             (mHal->isRemote() ? "remote" : "local"));
-  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
 }
 
 /*******************************************************************************
diff --git a/src/include/NfcAdaptation.h b/src/include/NfcAdaptation.h
index 49138b6..9382fce 100644
--- a/src/include/NfcAdaptation.h
+++ b/src/include/NfcAdaptation.h
@@ -30,6 +30,9 @@
 struct INfc;
 struct INfcClientCallback;
 }
+namespace V1_1 {
+struct INfc;
+}
 }
 }
 }
@@ -77,6 +80,7 @@
   virtual ~NfcAdaptation();
   void Initialize();
   void Finalize();
+  void FactoryReset();
   static NfcAdaptation& GetInstance();
   tHAL_NFC_ENTRY* GetHalEntryFuncs();
   void DownloadFirmware();
@@ -90,6 +94,7 @@
   ThreadCondVar mCondVar;
   tHAL_NFC_ENTRY mHalEntryFuncs;  // function pointers for HAL entry points
   static android::sp<android::hardware::nfc::V1_0::INfc> mHal;
+  static android::sp<android::hardware::nfc::V1_1::INfc> mHal_1_1;
   static android::hardware::nfc::V1_0::INfcClientCallback* mCallback;
   static tHAL_NFC_CBACK* mHalCallback;
   static tHAL_NFC_DATA_CBACK* mHalDataCallback;