Merge "API for libnfc_nci is added"
diff --git a/src/Android.bp b/src/Android.bp
index 795509f..788c364 100644
--- a/src/Android.bp
+++ b/src/Android.bp
@@ -18,7 +18,6 @@
         "libcutils",
         "liblog",
         "libdl",
-        "libhardware",
         "libz",
         "libchrome",
         "libbase",
@@ -30,6 +29,9 @@
         "android.hardware.nfc@1.0",
         "android.hardware.nfc@1.1",
         "android.hardware.nfc@1.2",
+        // Add for AIDL
+        "android.hardware.nfc-V1-ndk",
+        "libbinder_ndk",
     ],
     static_libs: [
         "libnfcutils",
diff --git a/src/adaptation/NfcAdaptation.cc b/src/adaptation/NfcAdaptation.cc
index 1567b0c..8524a17 100644
--- a/src/adaptation/NfcAdaptation.cc
+++ b/src/adaptation/NfcAdaptation.cc
@@ -15,6 +15,18 @@
  *  limitations under the License.
  *
  ******************************************************************************/
+#include <aidl/android/hardware/nfc/BnNfc.h>
+#include <aidl/android/hardware/nfc/BnNfcClientCallback.h>
+#include <aidl/android/hardware/nfc/INfc.h>
+#include <android/binder_ibinder.h>
+#include <android/binder_manager.h>
+#include <android/binder_process.h>
+// syslog.h and base/logging.h both try to #define LOG_INFO and LOG_WARNING.
+// We need to #undef these two before including base/logging.h.
+// libchrome => logging.h
+// aidl => syslog.h
+#undef LOG_INFO
+#undef LOG_WARNING
 #include <android-base/stringprintf.h>
 #include <android/hardware/nfc/1.1/INfc.h>
 #include <android/hardware/nfc/1.2/INfc.h>
@@ -50,6 +62,18 @@
 using NfcVendorConfigV1_2 = android::hardware::nfc::V1_2::NfcConfig;
 using android::hardware::nfc::V1_1::INfcClientCallback;
 using android::hardware::hidl_vec;
+using INfcAidl = ::aidl::android::hardware::nfc::INfc;
+using NfcAidlConfig = ::aidl::android::hardware::nfc::NfcConfig;
+using AidlPresenceCheckAlgorithm =
+    ::aidl::android::hardware::nfc::PresenceCheckAlgorithm;
+using INfcAidlClientCallback =
+    ::aidl::android::hardware::nfc::INfcClientCallback;
+using NfcAidlEvent = ::aidl::android::hardware::nfc::NfcEvent;
+using NfcAidlStatus = ::aidl::android::hardware::nfc::NfcStatus;
+using ::aidl::android::hardware::nfc::NfcCloseType;
+using Status = ::ndk::ScopedAStatus;
+
+std::string NFC_AIDL_HAL_SERVICE_NAME = "android.hardware.nfc.INfc/default";
 
 extern bool nfc_debug_enabled;
 
@@ -59,14 +83,15 @@
 
 NfcAdaptation* NfcAdaptation::mpInstance = nullptr;
 ThreadMutex NfcAdaptation::sLock;
-tHAL_NFC_CBACK* NfcAdaptation::mHalCallback = nullptr;
-tHAL_NFC_DATA_CBACK* NfcAdaptation::mHalDataCallback = nullptr;
 ThreadCondVar NfcAdaptation::mHalOpenCompletedEvent;
 ThreadCondVar NfcAdaptation::mHalCloseCompletedEvent;
 sp<INfc> NfcAdaptation::mHal;
 sp<INfcV1_1> NfcAdaptation::mHal_1_1;
 sp<INfcV1_2> NfcAdaptation::mHal_1_2;
 INfcClientCallback* NfcAdaptation::mCallback;
+std::shared_ptr<INfcAidlClientCallback> mAidlCallback;
+::ndk::ScopedAIBinder_DeathRecipient mDeathRecipient;
+std::shared_ptr<INfcAidl> mAidlHal;
 
 bool nfc_debug_enabled = false;
 std::string nfc_storage_path;
@@ -82,7 +107,7 @@
 // Whitelist for hosts allowed to create a pipe
 // See ADM_CREATE_PIPE command in the ETSI test specification
 // ETSI TS 102 622, section 6.1.3.1
-static std::vector<uint8_t> host_whitelist;
+static std::vector<uint8_t> host_allowlist;
 
 namespace {
 void initializeGlobalDebugEnabledFlag() {
@@ -162,6 +187,70 @@
   }
 };
 
+class NfcAidlClientCallback
+    : public ::aidl::android::hardware::nfc::BnNfcClientCallback {
+ public:
+  NfcAidlClientCallback(tHAL_NFC_CBACK* eventCallback,
+                        tHAL_NFC_DATA_CBACK dataCallback) {
+    mEventCallback = eventCallback;
+    mDataCallback = dataCallback;
+  };
+  virtual ~NfcAidlClientCallback() = default;
+
+  ::ndk::ScopedAStatus sendEvent(NfcAidlEvent event,
+                                 NfcAidlStatus event_status) override {
+    uint8_t e_num;
+    uint8_t s_num;
+    switch (event) {
+      case NfcAidlEvent::OPEN_CPLT:
+        e_num = HAL_NFC_OPEN_CPLT_EVT;
+        break;
+      case NfcAidlEvent::CLOSE_CPLT:
+        e_num = HAL_NFC_CLOSE_CPLT_EVT;
+        break;
+      case NfcAidlEvent::POST_INIT_CPLT:
+        e_num = HAL_NFC_POST_INIT_CPLT_EVT;
+        break;
+      case NfcAidlEvent::PRE_DISCOVER_CPLT:
+        e_num = HAL_NFC_PRE_DISCOVER_CPLT_EVT;
+        break;
+      case NfcAidlEvent::HCI_NETWORK_RESET:
+        e_num = HAL_HCI_NETWORK_RESET;
+        break;
+      case NfcAidlEvent::ERROR:
+      default:
+        e_num = HAL_NFC_ERROR_EVT;
+    }
+    switch (event_status) {
+      case NfcAidlStatus::OK:
+        s_num = HAL_NFC_STATUS_OK;
+        break;
+      case NfcAidlStatus::FAILED:
+        s_num = HAL_NFC_STATUS_FAILED;
+        break;
+      case NfcAidlStatus::ERR_TRANSPORT:
+        s_num = HAL_NFC_STATUS_ERR_TRANSPORT;
+        break;
+      case NfcAidlStatus::ERR_CMD_TIMEOUT:
+        s_num = HAL_NFC_STATUS_ERR_CMD_TIMEOUT;
+        break;
+      default:
+        s_num = HAL_NFC_STATUS_FAILED;
+    }
+    mEventCallback(e_num, (tHAL_NFC_STATUS)s_num);
+    return ::ndk::ScopedAStatus::ok();
+  };
+  ::ndk::ScopedAStatus sendData(const std::vector<uint8_t>& data) override {
+    std::vector<uint8_t> copy = data;
+    mDataCallback(copy.size(), &copy[0]);
+    return ::ndk::ScopedAStatus::ok();
+  };
+
+ private:
+  tHAL_NFC_CBACK* mEventCallback;
+  tHAL_NFC_DATA_CBACK* mDataCallback;
+};
+
 /*******************************************************************************
 **
 ** Function:    NfcAdaptation::NfcAdaptation()
@@ -173,6 +262,8 @@
 *******************************************************************************/
 NfcAdaptation::NfcAdaptation() {
   memset(&mHalEntryFuncs, 0, sizeof(mHalEntryFuncs));
+  mDeathRecipient = ::ndk::ScopedAIBinder_DeathRecipient(
+      AIBinder_DeathRecipient_new(NfcAdaptation::HalAidlBinderDied));
 }
 
 /*******************************************************************************
@@ -208,7 +299,10 @@
 void NfcAdaptation::GetVendorConfigs(
     std::map<std::string, ConfigValue>& configMap) {
   NfcVendorConfigV1_2 configValue;
-  if (mHal_1_2) {
+  NfcAidlConfig aidlConfigValue;
+  if (mAidlHal) {
+    mAidlHal->getConfig(&aidlConfigValue);
+  } else if (mHal_1_2) {
     mHal_1_2->getConfig_1_2(
         [&configValue](NfcVendorConfigV1_2 config) { configValue = config; });
   } else if (mHal_1_1) {
@@ -218,7 +312,61 @@
     });
   }
 
-  if (mHal_1_1 || mHal_1_2) {
+  if (mAidlHal) {
+    std::vector<int8_t> nfaPropCfg = {
+        aidlConfigValue.nfaProprietaryCfg.protocol18092Active,
+        aidlConfigValue.nfaProprietaryCfg.protocolBPrime,
+        aidlConfigValue.nfaProprietaryCfg.protocolDual,
+        aidlConfigValue.nfaProprietaryCfg.protocol15693,
+        aidlConfigValue.nfaProprietaryCfg.protocolKovio,
+        aidlConfigValue.nfaProprietaryCfg.protocolMifare,
+        aidlConfigValue.nfaProprietaryCfg.discoveryPollKovio,
+        aidlConfigValue.nfaProprietaryCfg.discoveryPollBPrime,
+        aidlConfigValue.nfaProprietaryCfg.discoveryListenBPrime};
+    configMap.emplace(NAME_NFA_PROPRIETARY_CFG, ConfigValue(nfaPropCfg));
+    configMap.emplace(NAME_NFA_POLL_BAIL_OUT_MODE,
+                      ConfigValue(aidlConfigValue.nfaPollBailOutMode ? 1 : 0));
+    configMap.emplace(NAME_DEFAULT_OFFHOST_ROUTE,
+                      ConfigValue(aidlConfigValue.defaultOffHostRoute));
+    if (configValue.offHostRouteUicc.size() != 0) {
+      configMap.emplace(NAME_OFFHOST_ROUTE_UICC,
+                        ConfigValue(aidlConfigValue.offHostRouteUicc));
+    }
+    if (configValue.offHostRouteEse.size() != 0) {
+      configMap.emplace(NAME_OFFHOST_ROUTE_ESE,
+                        ConfigValue(aidlConfigValue.offHostRouteEse));
+    }
+    configMap.emplace(NAME_DEFAULT_ROUTE,
+                      ConfigValue(aidlConfigValue.defaultRoute));
+    configMap.emplace(NAME_DEFAULT_NFCF_ROUTE,
+                      ConfigValue(aidlConfigValue.defaultOffHostRouteFelica));
+    configMap.emplace(NAME_DEFAULT_ISODEP_ROUTE,
+                      ConfigValue(aidlConfigValue.defaultIsoDepRoute));
+    configMap.emplace(NAME_DEFAULT_SYS_CODE_ROUTE,
+                      ConfigValue(aidlConfigValue.defaultSystemCodeRoute));
+    configMap.emplace(NAME_DEFAULT_SYS_CODE_PWR_STATE,
+                      ConfigValue(aidlConfigValue.defaultSystemCodePowerState));
+    configMap.emplace(NAME_OFF_HOST_SIM_PIPE_ID,
+                      ConfigValue(aidlConfigValue.offHostSIMPipeId));
+    configMap.emplace(NAME_OFF_HOST_ESE_PIPE_ID,
+                      ConfigValue(aidlConfigValue.offHostESEPipeId));
+    configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
+                      ConfigValue(aidlConfigValue.maxIsoDepTransceiveLength));
+    if (aidlConfigValue.hostAllowlist.size() != 0) {
+      configMap.emplace(NAME_DEVICE_HOST_ALLOW_LIST,
+                        ConfigValue(aidlConfigValue.hostAllowlist));
+    }
+    /* For Backwards compatibility */
+    if (aidlConfigValue.presenceCheckAlgorithm ==
+        AidlPresenceCheckAlgorithm::ISO_DEP_NAK) {
+      configMap.emplace(NAME_PRESENCE_CHECK_ALGORITHM,
+                        ConfigValue((uint32_t)NFA_RW_PRES_CHK_ISO_DEP_NAK));
+    } else {
+      configMap.emplace(
+          NAME_PRESENCE_CHECK_ALGORITHM,
+          ConfigValue((uint32_t)aidlConfigValue.presenceCheckAlgorithm));
+    }
+  } else if (mHal_1_1 || mHal_1_2) {
     std::vector<uint8_t> nfaPropCfg = {
         configValue.v1_1.nfaProprietaryCfg.protocol18092Active,
         configValue.v1_1.nfaProprietaryCfg.protocolBPrime,
@@ -260,7 +408,7 @@
     configMap.emplace(NAME_ISO_DEP_MAX_TRANSCEIVE,
                       ConfigValue(configValue.v1_1.maxIsoDepTransceiveLength));
     if (configValue.v1_1.hostWhitelist.size() != 0) {
-      configMap.emplace(NAME_DEVICE_HOST_WHITE_LIST,
+      configMap.emplace(NAME_DEVICE_HOST_ALLOW_LIST,
                         ConfigValue(configValue.v1_1.hostWhitelist));
     }
     /* For Backwards compatibility */
@@ -305,9 +453,9 @@
     if (dm_config.size() > 1) nfa_dm_cfg.auto_read_ndef = dm_config[1];
     if (dm_config.size() > 2) nfa_dm_cfg.auto_presence_check = dm_config[2];
     if (dm_config.size() > 3) nfa_dm_cfg.presence_check_option = dm_config[3];
-    // NOTE: The timeout value is not configurable here because the endianess
+    // NOTE: The timeout value is not configurable here because the endianness
     // of a byte array is ambiguous and needlessly difficult to configure.
-    // If this value needs to be configgurable, a numeric config option should
+    // If this value needs to be configurable, a numeric config option should
     // be used.
   }
 
@@ -348,12 +496,12 @@
       nfa_proprietary_cfg.pro_discovery_b_prime_listen = p_config[8];
   }
 
-  // Configure whitelist of HCI host ID's
+  // Configure allowlist of HCI host ID's
   // See specification: ETSI TS 102 622, section 6.1.3.1
-  if (NfcConfig::hasKey(NAME_DEVICE_HOST_WHITE_LIST)) {
-    host_whitelist = NfcConfig::getBytes(NAME_DEVICE_HOST_WHITE_LIST);
-    nfa_hci_cfg.num_whitelist_host = host_whitelist.size();
-    nfa_hci_cfg.p_whitelist = &host_whitelist[0];
+  if (NfcConfig::hasKey(NAME_DEVICE_HOST_ALLOW_LIST)) {
+    host_allowlist = NfcConfig::getBytes(NAME_DEVICE_HOST_ALLOW_LIST);
+    nfa_hci_cfg.num_allowlist_host = host_allowlist.size();
+    nfa_hci_cfg.p_allowlist = &host_allowlist[0];
   }
 
   verify_stack_non_volatile_store();
@@ -398,13 +546,17 @@
 
   NfcConfig::clear();
 
-  mNfcHalDeathRecipient->finalize();
+  if (mHal != nullptr) {
+    mNfcHalDeathRecipient->finalize();
+  }
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: exit", func);
   delete this;
 }
 
 void NfcAdaptation::FactoryReset() {
-  if (mHal_1_2 != nullptr) {
+  if (mAidlHal != nullptr) {
+    mAidlHal->factoryReset();
+  } else if (mHal_1_2 != nullptr) {
     mHal_1_2->factoryReset();
   } else if (mHal_1_1 != nullptr) {
     mHal_1_1->factoryReset();
@@ -412,13 +564,19 @@
 }
 
 void NfcAdaptation::DeviceShutdown() {
-  if (mHal_1_2 != nullptr) {
-    mHal_1_2->closeForPowerOffCase();
-  } else if (mHal_1_1 != nullptr) {
-    mHal_1_1->closeForPowerOffCase();
-  }
-  if (mHal) {
-    mHal->unlinkToDeath(mNfcHalDeathRecipient);
+  if (mAidlHal != nullptr) {
+    mAidlHal->close(NfcCloseType::HOST_SWITCHED_OFF);
+    AIBinder_unlinkToDeath(mAidlHal->asBinder().get(), mDeathRecipient.get(),
+                           this);
+  } else {
+    if (mHal_1_2 != nullptr) {
+      mHal_1_2->closeForPowerOffCase();
+    } else if (mHal_1_1 != nullptr) {
+      mHal_1_1->closeForPowerOffCase();
+    }
+    if (mHal != nullptr) {
+      mHal->unlinkToDeath(mNfcHalDeathRecipient);
+    }
   }
 }
 
@@ -504,7 +662,7 @@
 **
 ** Function:    NfcAdaptation::InitializeHalDeviceContext
 **
-** Description: Ask the generic Android HAL to find the Broadcom-specific HAL.
+** Description: Check validity of current handle to the nfc HAL service
 **
 ** Returns:     None.
 **
@@ -523,6 +681,7 @@
   mHalEntryFuncs.power_cycle = HalPowerCycle;
   mHalEntryFuncs.get_max_ee = HalGetMaxNfcee;
   LOG(INFO) << StringPrintf("%s: INfc::getService()", func);
+  // TODO: Try get the NFC HIDL first before vendor AIDL impl complete
   mHal = mHal_1_1 = mHal_1_2 = INfcV1_2::getService();
   if (mHal_1_2 == nullptr) {
     mHal = mHal_1_1 = INfcV1_1::getService();
@@ -530,11 +689,22 @@
       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"));
-  if (mHal) {
+  if (mHal == nullptr) {
+    // Try get AIDL
+    ::ndk::SpAIBinder binder(
+        AServiceManager_getService(NFC_AIDL_HAL_SERVICE_NAME.c_str()));
+    mAidlHal = INfcAidl::fromBinder(binder);
+    if (mAidlHal != nullptr) {
+      AIBinder_linkToDeath(mAidlHal->asBinder().get(), mDeathRecipient.get(),
+                           this /* cookie */);
+      mHal = mHal_1_1 = mHal_1_2 = nullptr;
+      LOG(INFO) << StringPrintf("%s: INfcAidl::fromBinder returned", func);
+    }
+    LOG_FATAL_IF(mAidlHal == nullptr, "Failed to retrieve the NFC AIDL!");
+  } else {
+    LOG(INFO) << StringPrintf("%s: INfc::getService() returned %p (%s)", func,
+                              mHal.get(),
+                              (mHal->isRemote() ? "remote" : "local"));
     mNfcHalDeathRecipient = new NfcHalDeathRecipient(mHal);
     mHal->linkToDeath(mNfcHalDeathRecipient, 0);
   }
@@ -583,10 +753,22 @@
                             tHAL_NFC_DATA_CBACK* p_data_cback) {
   const char* func = "NfcAdaptation::HalOpen";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
-  if (mHal_1_1 != nullptr) {
+
+  if (mAidlHal != nullptr) {
+    mAidlCallback = ::ndk::SharedRefBase::make<NfcAidlClientCallback>(
+        p_hal_cback, p_data_cback);
+    Status status = mAidlHal->open(mAidlCallback);
+    if (!status.isOk()) {
+      LOG(ERROR) << "Open Error: "
+                 << ::aidl::android::hardware::nfc::toString(
+                        static_cast<NfcAidlStatus>(
+                            status.getServiceSpecificError()));
+    }
+  } else if (mHal_1_1 != nullptr) {
+    mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
     mHal_1_1->open_1_1(mCallback);
   } else {
+    mCallback = new NfcClientCallback(p_hal_cback, p_data_cback);
     mHal->open(mCallback);
   }
 }
@@ -603,42 +785,11 @@
 void NfcAdaptation::HalClose() {
   const char* func = "NfcAdaptation::HalClose";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  mHal->close();
-}
-
-/*******************************************************************************
-**
-** Function:    NfcAdaptation::HalDeviceContextCallback
-**
-** Description: Translate generic Android HAL's callback into Broadcom-specific
-**              callback function.
-**
-** Returns:     None.
-**
-*******************************************************************************/
-void NfcAdaptation::HalDeviceContextCallback(nfc_event_t event,
-                                             nfc_status_t event_status) {
-  const char* func = "NfcAdaptation::HalDeviceContextCallback";
-  DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s: event=%u", func, event);
-  if (mHalCallback) mHalCallback(event, (tHAL_NFC_STATUS)event_status);
-}
-
-/*******************************************************************************
-**
-** Function:    NfcAdaptation::HalDeviceContextDataCallback
-**
-** Description: Translate generic Android HAL's callback into Broadcom-specific
-**              callback function.
-**
-** Returns:     None.
-**
-*******************************************************************************/
-void NfcAdaptation::HalDeviceContextDataCallback(uint16_t data_len,
-                                                 uint8_t* p_data) {
-  const char* func = "NfcAdaptation::HalDeviceContextDataCallback";
-  DLOG_IF(INFO, nfc_debug_enabled)
-      << StringPrintf("%s: len=%u", func, data_len);
-  if (mHalDataCallback) mHalDataCallback(data_len, p_data);
+  if (mAidlHal != nullptr) {
+    mAidlHal->close(NfcCloseType::DISABLE);
+  } else {
+    mHal->close();
+  }
 }
 
 /*******************************************************************************
@@ -653,9 +804,16 @@
 void NfcAdaptation::HalWrite(uint16_t data_len, uint8_t* p_data) {
   const char* func = "NfcAdaptation::HalWrite";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  ::android::hardware::nfc::V1_0::NfcData data;
-  data.setToExternal(p_data, data_len);
-  mHal->write(data);
+
+  if (mAidlHal != nullptr) {
+    int ret;
+    std::vector<uint8_t> aidl_data(p_data, p_data + data_len);
+    mAidlHal->write(aidl_data, &ret);
+  } else {
+    ::android::hardware::nfc::V1_0::NfcData data;
+    data.setToExternal(p_data, data_len);
+    mHal->write(data);
+  }
 }
 
 /*******************************************************************************
@@ -671,10 +829,14 @@
                                        uint8_t* p_core_init_rsp_params) {
   const char* func = "NfcAdaptation::HalCoreInitialized";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  hidl_vec<uint8_t> data;
-  data.setToExternal(p_core_init_rsp_params, data_len);
-
-  mHal->coreInitialized(data);
+  if (mAidlHal != nullptr) {
+    // AIDL coreInitialized doesn't send data to HAL.
+    mAidlHal->coreInitialized();
+  } else {
+    hidl_vec<uint8_t> data;
+    data.setToExternal(p_core_init_rsp_params, data_len);
+    mHal->coreInitialized(data);
+  }
 }
 
 /*******************************************************************************
@@ -694,9 +856,18 @@
 bool NfcAdaptation::HalPrediscover() {
   const char* func = "NfcAdaptation::HalPrediscover";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  bool retval = FALSE;
-  mHal->prediscover();
-  return retval;
+  if (mAidlHal != nullptr) {
+    Status status = mAidlHal->preDiscover();
+    if (status.isOk()) {
+      DLOG_IF(INFO, nfc_debug_enabled)
+          << StringPrintf("%s wait for NFC_PRE_DISCOVER_CPLT_EVT", func);
+      return true;
+    }
+  } else {
+    mHal->prediscover();
+  }
+
+  return false;
 }
 
 /*******************************************************************************
@@ -715,7 +886,11 @@
 void NfcAdaptation::HalControlGranted() {
   const char* func = "NfcAdaptation::HalControlGranted";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  mHal->controlGranted();
+  if (mAidlHal != nullptr) {
+    LOG(ERROR) << StringPrintf("Unsupported function %s", func);
+  } else {
+    mHal->controlGranted();
+  }
 }
 
 /*******************************************************************************
@@ -730,7 +905,11 @@
 void NfcAdaptation::HalPowerCycle() {
   const char* func = "NfcAdaptation::HalPowerCycle";
   DLOG_IF(INFO, nfc_debug_enabled) << StringPrintf("%s", func);
-  mHal->powerCycle();
+  if (mAidlHal != nullptr) {
+    mAidlHal->powerCycle();
+  } else {
+    mHal->powerCycle();
+  }
 }
 
 /*******************************************************************************
@@ -825,6 +1004,31 @@
 
 /*******************************************************************************
 **
+** Function:    NfcAdaptation::HalAidlBinderDiedImpl
+**
+** Description: Abort nfc service when AIDL process died.
+**
+** Returns:     None.
+**
+*******************************************************************************/
+void NfcAdaptation::HalAidlBinderDiedImpl() {
+  LOG(WARNING) << __func__ << "INfc aidl hal died, resetting the state";
+  if (mAidlHal != nullptr) {
+    AIBinder_unlinkToDeath(mAidlHal->asBinder().get(), mDeathRecipient.get(),
+                           this);
+    mAidlHal = nullptr;
+  }
+  abort();
+}
+
+// static
+void NfcAdaptation::HalAidlBinderDied(void* cookie) {
+  auto thiz = static_cast<NfcAdaptation*>(cookie);
+  thiz->HalAidlBinderDiedImpl();
+}
+
+/*******************************************************************************
+**
 ** Function:    ThreadMutex::ThreadMutex()
 **
 ** Description: class constructor
diff --git a/src/gki/ulinux/gki_ulinux.cc b/src/gki/ulinux/gki_ulinux.cc
index 64983fd..8a7eea1 100644
--- a/src/gki/ulinux/gki_ulinux.cc
+++ b/src/gki/ulinux/gki_ulinux.cc
@@ -125,7 +125,7 @@
   pthread_mutexattr_init(&attr);
 
 #ifndef __CYGWIN__
-  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
+  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
 #endif
   p_os = &gki_cb.os;
   pthread_mutex_init(&p_os->GKI_mutex, &attr);
diff --git a/src/include/NfcAdaptation.h b/src/include/NfcAdaptation.h
index df12a44..8bbb9c2 100644
--- a/src/include/NfcAdaptation.h
+++ b/src/include/NfcAdaptation.h
@@ -16,14 +16,14 @@
  *
  ******************************************************************************/
 #pragma once
+
 #include <pthread.h>
+#include <utils/RefBase.h>
 
 #include "config.h"
 #include "nfc_hal_api.h"
 #include "nfc_target.h"
 
-#include <utils/RefBase.h>
-
 using ::android::sp;
 
 namespace android {
@@ -138,4 +138,9 @@
                                           nfc_status_t event_status);
   static void HalDownloadFirmwareDataCallback(uint16_t data_len,
                                               uint8_t* p_data);
+
+  // Death recipient callback that is called when INfcAidl dies.
+  // The cookie is a pointer to a NfcAdaptation object.
+  static void HalAidlBinderDied(void* cookie);
+  void HalAidlBinderDiedImpl();
 };
diff --git a/src/include/hardware_nfc.h b/src/include/hardware_nfc.h
new file mode 100644
index 0000000..b266541
--- /dev/null
+++ b/src/include/hardware_nfc.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+typedef uint8_t nfc_event_t;
+typedef uint8_t nfc_status_t;
+
+/*
+ * The callback passed in from the NFC stack that the HAL
+ * can use to pass events back to the stack.
+ */
+typedef void(nfc_stack_callback_t)(nfc_event_t event,
+                                   nfc_status_t event_status);
+
+/*
+ * The callback passed in from the NFC stack that the HAL
+ * can use to pass incomming data to the stack.
+ */
+typedef void(nfc_stack_data_callback_t)(uint16_t data_len, uint8_t* p_data);
+
+enum {
+  HAL_NFC_OPEN_CPLT_EVT = 0u,
+  HAL_NFC_CLOSE_CPLT_EVT = 1u,
+  HAL_NFC_POST_INIT_CPLT_EVT = 2u,
+  HAL_NFC_PRE_DISCOVER_CPLT_EVT = 3u,
+  HAL_NFC_REQUEST_CONTROL_EVT = 4u,
+  HAL_NFC_RELEASE_CONTROL_EVT = 5u,
+  HAL_NFC_ERROR_EVT = 6u,
+  HAL_HCI_NETWORK_RESET = 7u,
+};
+
+enum {
+  HAL_NFC_STATUS_OK = 0u,
+  HAL_NFC_STATUS_FAILED = 1u,
+  HAL_NFC_STATUS_ERR_TRANSPORT = 2u,
+  HAL_NFC_STATUS_ERR_CMD_TIMEOUT = 3u,
+  HAL_NFC_STATUS_REFUSED = 4u,
+};
diff --git a/src/include/nfc_config.h b/src/include/nfc_config.h
index aa7ac66..4731e97 100644
--- a/src/include/nfc_config.h
+++ b/src/include/nfc_config.h
@@ -57,7 +57,7 @@
 #define NAME_OFF_HOST_ESE_PIPE_ID "OFF_HOST_ESE_PIPE_ID"
 #define NAME_OFF_HOST_SIM_PIPE_ID "OFF_HOST_SIM_PIPE_ID"
 #define NAME_ISO_DEP_MAX_TRANSCEIVE "ISO_DEP_MAX_TRANSCEIVE"
-#define NAME_DEVICE_HOST_WHITE_LIST "DEVICE_HOST_WHITE_LIST"
+#define NAME_DEVICE_HOST_ALLOW_LIST "DEVICE_HOST_ALLOW_LIST"
 #define NAME_DEFAULT_ISODEP_ROUTE "DEFAULT_ISODEP_ROUTE"
 
 class NfcConfig {
diff --git a/src/include/nfc_hal_api.h b/src/include/nfc_hal_api.h
index 205c6f7..8934a63 100644
--- a/src/include/nfc_hal_api.h
+++ b/src/include/nfc_hal_api.h
@@ -23,8 +23,8 @@
  ******************************************************************************/
 #ifndef NFC_HAL_API_H
 #define NFC_HAL_API_H
-#include <hardware/nfc.h>
 #include "data_types.h"
+#include "hardware_nfc.h"
 #include "nfc_hal_target.h"
 
 typedef uint8_t tHAL_NFC_STATUS;
diff --git a/src/nfa/hci/nfa_hci_act.cc b/src/nfa/hci/nfa_hci_act.cc
index 70c519c..ae9c3b1 100644
--- a/src/nfa/hci/nfa_hci_act.cc
+++ b/src/nfa/hci/nfa_hci_act.cc
@@ -1356,7 +1356,7 @@
           /* Set WHITELIST */
           nfa_hciu_send_set_param_cmd(
               NFA_HCI_ADMIN_PIPE, NFA_HCI_WHITELIST_INDEX,
-              p_nfa_hci_cfg->num_whitelist_host, p_nfa_hci_cfg->p_whitelist);
+              p_nfa_hci_cfg->num_allowlist_host, p_nfa_hci_cfg->p_allowlist);
         } else if (nfa_hci_cb.param_in_use == NFA_HCI_WHITELIST_INDEX) {
           if ((nfa_hci_cb.hci_state == NFA_HCI_STATE_STARTUP) ||
               (nfa_hci_cb.hci_state == NFA_HCI_STATE_RESTORE))
@@ -1408,7 +1408,7 @@
             /* Session has not changed, Set WHITELIST */
             nfa_hciu_send_set_param_cmd(
                 NFA_HCI_ADMIN_PIPE, NFA_HCI_WHITELIST_INDEX,
-                p_nfa_hci_cfg->num_whitelist_host, p_nfa_hci_cfg->p_whitelist);
+                p_nfa_hci_cfg->num_allowlist_host, p_nfa_hci_cfg->p_allowlist);
           } else {
             /* Something wrong, NVRAM data could be corrupt or first start with
              * default session id */
diff --git a/src/nfa/include/nfa_api.h b/src/nfa/include/nfa_api.h
index d89c6f3..b94f45e 100755
--- a/src/nfa/include/nfa_api.h
+++ b/src/nfa/include/nfa_api.h
@@ -573,10 +573,10 @@
   uint16_t hci_netwk_enable_timeout;
   /* Maximum time to wait for EE DISC REQ NTF(s) after HOT PLUG EVT(s) */
   uint16_t hcp_response_timeout;
-  /* Number of host in the whitelist of Terminal host */
-  uint8_t num_whitelist_host;
-  /* Whitelist of Terminal Host */
-  uint8_t* p_whitelist;
+  /* Number of host in the allowlist of Terminal host */
+  uint8_t num_allowlist_host;
+  /* Allowlist of Terminal Host */
+  uint8_t* p_allowlist;
 } tNFA_HCI_CFG;
 
 /*
diff --git a/src/nfc/nfc/nfc_main.cc b/src/nfc/nfc/nfc_main.cc
index a03fa7a..1b25eeb 100644
--- a/src/nfc/nfc/nfc_main.cc
+++ b/src/nfc/nfc/nfc_main.cc
@@ -148,7 +148,7 @@
       return "HAL_NFC_RELEASE_CONTROL_EVT";
     case HAL_NFC_ERROR_EVT:
       return "HAL_NFC_ERROR_EVT";
-    case (uint32_t)NfcEvent::HCI_NETWORK_RESET:
+    case HAL_HCI_NETWORK_RESET:
       return "HCI_NETWORK_RESET";
     default:
       return "???? UNKNOWN EVENT";
@@ -526,7 +526,7 @@
           }
           break;
 
-        case (uint32_t)NfcEvent::HCI_NETWORK_RESET:
+        case HAL_HCI_NETWORK_RESET:
           delete_stack_non_volatile_store(true);
           break;
 
@@ -630,7 +630,7 @@
     case HAL_NFC_REQUEST_CONTROL_EVT:
     case HAL_NFC_RELEASE_CONTROL_EVT:
     case HAL_NFC_ERROR_EVT:
-    case (uint32_t)NfcEvent::HCI_NETWORK_RESET:
+    case HAL_HCI_NETWORK_RESET:
       nfc_main_post_hal_evt(event, status);
       break;
 
diff --git a/utils/config.cc b/utils/config.cc
index 6354a2e..40be00f 100644
--- a/utils/config.cc
+++ b/utils/config.cc
@@ -59,6 +59,13 @@
   value_unsigned_ = value;
 }
 
+ConfigValue::ConfigValue(std::vector<int8_t> value) {
+  CHECK(!(value.empty()));
+  type_ = BYTES;
+  value_bytes_ = std::vector<uint8_t>(value.begin(), value.end());
+  value_unsigned_ = 0;
+}
+
 ConfigValue::ConfigValue(std::vector<uint8_t> value) {
   CHECK(!(value.empty()));
   type_ = BYTES;
diff --git a/utils/include/config.h b/utils/include/config.h
index f4bcdec..e48717b 100644
--- a/utils/include/config.h
+++ b/utils/include/config.h
@@ -27,6 +27,7 @@
   explicit ConfigValue(std::string);
   explicit ConfigValue(unsigned);
   explicit ConfigValue(std::vector<uint8_t>);
+  explicit ConfigValue(std::vector<int8_t>);
   Type getType() const;
   std::string getString() const;
   unsigned getUnsigned() const;
diff --git a/utils/include/ringbuffer.h b/utils/include/ringbuffer.h
index d2c310a..35cc023 100644
--- a/utils/include/ringbuffer.h
+++ b/utils/include/ringbuffer.h
@@ -19,6 +19,7 @@
 #pragma once
 
 #include <stdint.h>
+#include <sys/types.h>
 
 typedef struct ringbuffer_t ringbuffer_t;