Perform a FW update at factory reset

Bug: 132048307
Test: Manually test after factory reset
Change-Id: Ic4565d811b5764691714b864b6902d7f1a55704b
diff --git a/1.1/hal_st21nfc.cc b/1.1/hal_st21nfc.cc
index e97d0bf..927e9a1 100644
--- a/1.1/hal_st21nfc.cc
+++ b/1.1/hal_st21nfc.cc
@@ -43,7 +43,7 @@
   nfc_stack_callback_t* p_cback_unwrap;
 } st21nfc_dev_t;
 
-const char* halVersion = "ST21NFC HAL1.1 Version 3.1.14";
+const char* halVersion = "ST21NFC HAL1.1 Version 3.1.15";
 
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
 uint8_t hal_is_closed = 1;
@@ -66,6 +66,7 @@
 extern int hal_wrapper_close(int call_cb, int nfc_mode);
 
 extern void hal_wrapper_send_config();
+extern void hal_wrapper_factoryReset();
 
 /* Make sure to always post nfc_stack_callback_t in a separate thread.
 This prevents a possible deadlock in upper layer on some sequences.
@@ -407,6 +408,7 @@
 
 void StNfc_hal_factoryReset() {
   STLOG_HAL_D("HAL st21nfc: %s", __func__);
+  hal_wrapper_factoryReset();
   // Nothing needed for factory reset in st21nfc case.
 }
 
diff --git a/1.2/hal_st21nfc.cc b/1.2/hal_st21nfc.cc
index 8ab2076..dfac627 100644
--- a/1.2/hal_st21nfc.cc
+++ b/1.2/hal_st21nfc.cc
@@ -43,7 +43,7 @@
   nfc_stack_callback_t* p_cback_unwrap;
 } st21nfc_dev_t;
 
-const char* halVersion = "ST21NFC HAL1.2 Version 3.2.3";
+const char* halVersion = "ST21NFC HAL1.2 Version 3.2.4";
 
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
 uint8_t hal_is_closed = 1;
@@ -67,6 +67,7 @@
 extern int hal_wrapper_close(int call_cb, int nfc_mode);
 
 extern void hal_wrapper_send_config();
+extern void hal_wrapper_factoryReset();
 
 /* Make sure to always post nfc_stack_callback_t in a separate thread.
 This prevents a possible deadlock in upper layer on some sequences.
@@ -408,6 +409,7 @@
 
 void StNfc_hal_factoryReset() {
   STLOG_HAL_D("HAL st21nfc: %s", __func__);
+  hal_wrapper_factoryReset();
   // Nothing needed for factory reset in st21nfc case.
 }
 
diff --git a/st21nfc/hal_wrapper.cc b/st21nfc/hal_wrapper.cc
index 0fbdf8d..7558521 100644
--- a/st21nfc/hal_wrapper.cc
+++ b/st21nfc/hal_wrapper.cc
@@ -66,7 +66,7 @@
 bool mReadFwConfigDone = false;
 
 bool mHciCreditLent = false;
-
+bool mfactoryReset = false;
 bool ready_flag = 0;
 
 void wait_ready() {
@@ -179,6 +179,10 @@
   hal_wrapper_send_vs_config();
 }
 
+void hal_wrapper_factoryReset() {
+  mfactoryReset = true;
+  STLOG_HAL_V("%s - mfactoryReset = %d", __func__, mfactoryReset);
+}
 void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data) {
   uint8_t propNfcModeSetCmdOn[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
   uint8_t coreInitCmd[] = {0x20, 0x01, 0x02, 0x00, 0x00};
@@ -195,10 +199,20 @@
 
       if ((p_data[0] == 0x60) && (p_data[1] == 0x00)) {
         mFwUpdateTaskMask = ft_cmd_HwReset(p_data, &mClfMode);
+
+        if (mfactoryReset == true) {
+          STLOG_HAL_V(
+              "%s - first boot after factory reset detected - start FW update",
+              __func__);
+          if ((mFwUpdateResMask & FW_PATCH_AVAILABLE) &&
+              (mFwUpdateResMask & FW_CUSTOM_PARAM_AVAILABLE)) {
+            mFwUpdateTaskMask = FW_UPDATE_NEEDED | CONF_UPDATE_NEEDED;
+            mfactoryReset = false;
+          }
+        }
         STLOG_HAL_V(
             "%s - mFwUpdateTaskMask = %d,  mClfMode = %d,  mRetryFwDwl = %d",
             __func__, mFwUpdateTaskMask, mClfMode, mRetryFwDwl);
-
         // CLF in MODE LOADER & Update needed.
         if (mClfMode == FT_CLF_MODE_LOADER) {
           HalSendDownstreamStopTimer(mHalHandle);