Merge Android U (ab/10368041)

Bug: 291102124
Merged-In: I685a21fe69e84db3a8fc0605a121f5f48c2ab5cd
Change-Id: Ibcd7657cb1cfece301a26e634c819f43d5ad1897
diff --git a/1.0/hal/halcore.c b/1.0/hal/halcore.c
index 1493cac..2912b05 100644
--- a/1.0/hal/halcore.c
+++ b/1.0/hal/halcore.c
@@ -27,6 +27,8 @@
 #include <pthread.h>
 #include <semaphore.h>
 
+pthread_mutex_t debugOutputSem = PTHREAD_MUTEX_INITIALIZER;
+bool halTraceMask = true;
 extern int I2cWriteCmd(const uint8_t* x, size_t len);
 extern void DispHal(const char* title, const void* data, size_t length);
 
diff --git a/1.0/include/halcore.h b/1.0/include/halcore.h
index 1df331b..4b646b6 100644
--- a/1.0/include/halcore.h
+++ b/1.0/include/halcore.h
@@ -42,8 +42,8 @@
 #define HAL_FLAG_NO_DEBUG 0 /* disable debug output */
 #define HAL_FLAG_DEBUG 1    /* enable debug output */
 
-bool halTraceMask;
-pthread_mutex_t debugOutputSem;
+extern bool halTraceMask;
+extern pthread_mutex_t debugOutputSem;
 
 #ifdef ANDROID
 #include <android/log.h>
diff --git a/aidl/hal_st21nfc.cc b/aidl/hal_st21nfc.cc
index d3903c7..5aa5f5e 100644
--- a/aidl/hal_st21nfc.cc
+++ b/aidl/hal_st21nfc.cc
@@ -528,6 +528,7 @@
 
 void StNfc_hal_setLogging(bool enable) {
   dbg_logging = enable;
+  hal_wrapper_setFwLogging(enable);
   if (dbg_logging && hal_conf_trace_level < STNFC_TRACE_LEVEL_VERBOSE) {
     hal_trace_level = STNFC_TRACE_LEVEL_VERBOSE;
   } else {
diff --git a/st21nfc/hal_wrapper.cc b/st21nfc/hal_wrapper.cc
index 626f4e1..571991f 100644
--- a/st21nfc/hal_wrapper.cc
+++ b/st21nfc/hal_wrapper.cc
@@ -70,7 +70,11 @@
 bool mfactoryReset = false;
 bool ready_flag = 0;
 bool mTimerStarted = false;
+bool mFieldInfoTimerStarted = false;
 bool forceRecover = false;
+unsigned long hal_field_timer = 0;
+
+static bool sEnableFwLog = false;
 
 void wait_ready() {
   pthread_mutex_lock(&mutex);
@@ -396,9 +400,12 @@
 
             // Check if FW DBG shall be set
             if (GetNumValue(NAME_STNFC_FW_DEBUG_ENABLED, &num, sizeof(num)) ||
-                isDebuggable) {
-              if (firmware_debug_enabled) num = 1;
-
+                isDebuggable || sEnableFwLog) {
+              if (firmware_debug_enabled || sEnableFwLog) {
+                num = 1;
+                swp_log = 30;
+                rf_log = 15;
+              }
               if (num == 1) {
                 GetNumValue(NAME_STNFC_FW_SWP_LOG_SIZE, &swp_log,
                             sizeof(swp_log));
@@ -464,13 +471,15 @@
                                        nciPropEnableFwDbgTraces_size)) {
                   STLOG_HAL_E("%s - SendDownstream failed", __func__);
                 }
-
+                mHalWrapperState = HAL_WRAPPER_STATE_APPLY_PROP_CONFIG;
                 break;
               }
             }
           }
         }
-
+        GetNumValue(NAME_STNFC_REMOTE_FIELD_TIMER, &hal_field_timer,
+                    sizeof(hal_field_timer));
+        STLOG_HAL_D("%s - hal_field_timer = %lu", __func__, hal_field_timer);
         // Exit state, all processing done
         mHalWrapperCallback(HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_OK);
         mHalWrapperState = HAL_WRAPPER_STATE_READY;
@@ -494,6 +503,20 @@
               }
             }
           }
+        } else if ((p_data[0] == 0x61) && (p_data[1] == 0x07)) {
+          // RF_FIELD_INFO_NTF
+          if (p_data[3] == 0x01) {  // field on
+            // start timer
+            if (hal_field_timer) {
+              mFieldInfoTimerStarted = true;
+              HalSendDownstreamTimer(mHalHandle, 20000);
+            }
+          } else if (p_data[3] == 0x00) {
+            if (mFieldInfoTimerStarted) {
+              HalSendDownstreamStopTimer(mHalHandle);
+              mFieldInfoTimerStarted = false;
+            }
+          }
         } else if ((p_data[0] == 0x6f) && (p_data[1] == 0x05)) {
           (void)pthread_mutex_lock(&mutex_activerw);
           // start timer
@@ -525,6 +548,10 @@
                    ((p_data[0] == 0x61) && (p_data[1] == 0x03))) {
           mError_count = 0;
           // stop timer
+          if (mFieldInfoTimerStarted) {
+            HalSendDownstreamStopTimer(mHalHandle);
+            mFieldInfoTimerStarted = false;
+          }
           if (mTimerStarted) {
             HalSendDownstreamStopTimer(mHalHandle);
             mTimerStarted = false;
@@ -555,6 +582,11 @@
               p_data[5] = 0x00;
               data_len = 0x6;
             }
+          } else if (p_data[3] == 0xA1) {
+            if (mFieldInfoTimerStarted) {
+              HalSendDownstreamStopTimer(mHalHandle);
+              mFieldInfoTimerStarted = false;
+            }
           }
         }
         mHalWrapperDataCallback(data_len, p_data);
@@ -614,11 +646,36 @@
       mHalWrapperState = HAL_WRAPPER_STATE_READY;
       mHalWrapperDataCallback(data_len, p_data);
       break;
+
+    case HAL_WRAPPER_STATE_APPLY_PROP_CONFIG:
+      STLOG_HAL_V("%s - mHalWrapperState = HAL_WRAPPER_STATE_APPLY_PROP_CONFIG",
+                  __func__);
+      if (p_data[0] == 0x4f) {
+        I2cResetPulse();
+      } else if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
+        // Send CORE_INIT_CMD
+        STLOG_HAL_D("%s - Sending CORE_INIT_CMD", __func__);
+        if (!HalSendDownstream(mHalHandle, coreInitCmd, sizeof(coreInitCmd))) {
+          STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
+        }
+      }
+      // CORE_INIT_RSP
+      else if ((p_data[0] == 0x40) && (p_data[1] == 0x01)) {
+        // Exit state, all processing done
+        mHalWrapperCallback(HAL_NFC_POST_INIT_CPLT_EVT, HAL_NFC_STATUS_OK);
+        mHalWrapperState = HAL_WRAPPER_STATE_READY;
+      }
+      break;
+    case HAL_WRAPPER_STATE_RECOVERY:
+      break;
   }
 }
 
 static void halWrapperCallback(uint8_t event, __attribute__((unused))uint8_t event_status) {
   uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
+  uint8_t rfDeactivateCmd[] = {0x21, 0x06, 0x01, 0x00};
+  uint8_t p_data[6];
+  uint16_t data_len;
 
   switch (mHalWrapperState) {
     case HAL_WRAPPER_STATE_CLOSING:
@@ -636,6 +693,7 @@
         STLOG_HAL_D("NFC-NCI HAL: %s  Timeout accessing the CLF.", __func__);
         HalSendDownstreamStopTimer(mHalHandle);
         I2cRecovery();
+        abort(); // TODO: fix it when we have a better recovery method.
         return;
       }
       break;
@@ -652,6 +710,7 @@
       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
         STLOG_HAL_E("%s - Timer for FW update procedure timeout, retry",
                     __func__);
+        abort(); // TODO: fix it when we have a better recovery method.
         HalSendDownstreamStopTimer(mHalHandle);
         resetHandlerState();
         I2cResetPulse();
@@ -673,6 +732,7 @@
     case HAL_WRAPPER_STATE_PROP_CONFIG:
       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
         STLOG_HAL_E("%s - Timer when sending conf parameters, retry", __func__);
+        abort(); // TODO: fix it when we have a better recovery method.
         HalSendDownstreamStopTimer(mHalHandle);
         resetHandlerState();
         I2cResetPulse();
@@ -682,16 +742,30 @@
 
     case HAL_WRAPPER_STATE_READY:
       if (event == HAL_WRAPPER_TIMEOUT_EVT) {
-        if (mTimerStarted) {
-          STLOG_HAL_E("NFC-NCI HAL: %s  Timeout.. Recover", __func__);
+        if (mTimerStarted || mFieldInfoTimerStarted) {
+          STLOG_HAL_E("NFC-NCI HAL: %s  Timeout.. Recover!", __func__);
           STLOG_HAL_E("%s mIsActiveRW = %d", __func__, mIsActiveRW);
           HalSendDownstreamStopTimer(mHalHandle);
           mTimerStarted = false;
-          forceRecover = true;
+          mFieldInfoTimerStarted = false;
+          // forceRecover = true;
           resetHandlerState();
-          I2cResetPulse();
-          mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
+          if (!HalSendDownstream(mHalHandle, rfDeactivateCmd,
+                                 sizeof(rfDeactivateCmd))) {
+            STLOG_HAL_E("%s - SendDownstream failed", __func__);
+          }
+          usleep(10000);
+          // Core Generic Error
 
+          p_data[0] = 0x60;
+          p_data[1] = 0x00;
+          p_data[2] = 0x03;
+          p_data[3] = 0xAA;
+          p_data[4] = 0x00;
+          p_data[5] = 0x00;
+          data_len = 0x6;
+          mHalWrapperDataCallback(data_len, p_data);
+          mHalWrapperState = HAL_WRAPPER_STATE_RECOVERY;
         }
         return;
       }
@@ -718,3 +792,18 @@
 
   mHalWrapperState = new_wrapper_state;
 }
+
+/*******************************************************************************
+ **
+ ** Function         hal_wrapper_setFwLogging
+ **
+ ** Description      Enable the FW log by hte GUI if needed.
+ **
+ ** Returns          void
+ **
+ *******************************************************************************/
+void hal_wrapper_setFwLogging(bool enable) {
+  ALOGD("%s : enable = %d", __func__, enable);
+
+  sEnableFwLog = enable;
+}
diff --git a/st21nfc/include/android_logmsg.h b/st21nfc/include/android_logmsg.h
index f8a49cf..666c2b9 100644
--- a/st21nfc/include/android_logmsg.h
+++ b/st21nfc/include/android_logmsg.h
@@ -55,6 +55,7 @@
 #define NAME_STNFC_ACTIVERW_TIMER "STNFC_ACTIVERW_TIMER"
 #define NAME_STNFC_FW_SWP_LOG_SIZE "STNFC_FW_SWP_LOG_SIZE"
 #define NAME_STNFC_FW_RF_LOG_SIZE "STNFC_FW_RF_LOG_SIZE"
+#define NAME_STNFC_REMOTE_FIELD_TIMER "STNFC_REMOTE_FIELD_TIMER"
 
 /* #######################
  * Set the logging level
diff --git a/st21nfc/include/halcore.h b/st21nfc/include/halcore.h
index 96ba779..ddae93e 100644
--- a/st21nfc/include/halcore.h
+++ b/st21nfc/include/halcore.h
@@ -56,6 +56,8 @@
   HAL_WRAPPER_STATE_APPLY_CUSTOM_PARAM,
   HAL_WRAPPER_STATE_APPLY_UWB_PARAM,
   HAL_WRAPPER_STATE_SET_ACTIVERW_TIMER,
+  HAL_WRAPPER_STATE_APPLY_PROP_CONFIG,
+  HAL_WRAPPER_STATE_RECOVERY,
 } hal_wrapper_state_e;
 
 /* callback function to communicate from HAL Core with the outside world */
@@ -83,5 +85,6 @@
 bool HalSendUpstream(HALHANDLE hHAL, const uint8_t* data, size_t size);
 
 void hal_wrapper_set_state(hal_wrapper_state_e new_wrapper_state);
+void hal_wrapper_setFwLogging(bool enable);
 void I2cResetPulse();
 #endif