Add device dependent settings to be updated during NFC enable

A section can now be added in libnfc-hal-st.conf to add a device dependent
NCI settings (either generic or proprietary one).
Use name CORE_CONF_PROP in the conf file.

Bug: 132155154
Test: Manual - NFC on/off, send NCI setting according to config
Change-Id: Ied0a87d331f8ddec2cd8836aa7ada9d7cf049933
diff --git a/1.1/hal_st21nfc.cc b/1.1/hal_st21nfc.cc
index 56bdc4b..e97d0bf 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.13";
+const char* halVersion = "ST21NFC HAL1.1 Version 3.1.14";
 
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
 uint8_t hal_is_closed = 1;
@@ -65,7 +65,7 @@
 
 extern int hal_wrapper_close(int call_cb, int nfc_mode);
 
-extern void hal_wrapper_send_prop_config();
+extern void hal_wrapper_send_config();
 
 /* Make sure to always post nfc_stack_callback_t in a separate thread.
 This prevents a possible deadlock in upper layer on some sequences.
@@ -338,7 +338,7 @@
   (void)pthread_mutex_lock(&hal_mtx);
   hal_dta_state = *p_core_init_rsp_params;
 
-  hal_wrapper_send_prop_config();
+  hal_wrapper_send_config();
   (void) pthread_mutex_unlock(&hal_mtx);
 
   return 0;  // return != 0 to signal ready immediate
diff --git a/1.2/hal_st21nfc.cc b/1.2/hal_st21nfc.cc
index 3337995..8ab2076 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.2";
+const char* halVersion = "ST21NFC HAL1.2 Version 3.2.3";
 
 uint8_t cmd_set_nfc_mode_enable[] = {0x2f, 0x02, 0x02, 0x02, 0x01};
 uint8_t hal_is_closed = 1;
@@ -66,7 +66,7 @@
 
 extern int hal_wrapper_close(int call_cb, int nfc_mode);
 
-extern void hal_wrapper_send_prop_config();
+extern void hal_wrapper_send_config();
 
 /* Make sure to always post nfc_stack_callback_t in a separate thread.
 This prevents a possible deadlock in upper layer on some sequences.
@@ -339,7 +339,7 @@
   (void)pthread_mutex_lock(&hal_mtx);
   hal_dta_state = *p_core_init_rsp_params;
 
-  hal_wrapper_send_prop_config();
+  hal_wrapper_send_config();
   (void)pthread_mutex_unlock(&hal_mtx);
 
   return 0;  // return != 0 to signal ready immediate
diff --git a/st21nfc/adaptation/config.cpp b/st21nfc/adaptation/config.cpp
index 224b9a9..99c18eb 100644
--- a/st21nfc/adaptation/config.cpp
+++ b/st21nfc/adaptation/config.cpp
@@ -277,7 +277,7 @@
                    (c == ' ' || c == '\r' || c == '\n' || c == '\t')) {
           break;
         } else if (base == 16 &&
-                   (c == ':' || c == '-' || c == ' ' || c == '}')) {
+                   (c == ',' || c == ':' || c == '-' || c == ' ' || c == '}')) {
           if (c == '}') {
             bflag = 0;
           }
diff --git a/st21nfc/hal_wrapper.cc b/st21nfc/hal_wrapper.cc
index 67ff20f..0fbdf8d 100644
--- a/st21nfc/hal_wrapper.cc
+++ b/st21nfc/hal_wrapper.cc
@@ -51,6 +51,10 @@
 uint8_t mFwUpdateTaskMask;
 int mRetryFwDwl;
 uint8_t mFwUpdateResMask = 0;
+uint8_t* ConfigBuffer = NULL;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+pthread_cond_t ready_cond = PTHREAD_COND_INITIALIZER;
+
 static const uint8_t ApduGetAtr[] = {0x2F, 0x04, 0x05, 0x80,
                                      0x8A, 0x00, 0x00, 0x04};
 
@@ -63,6 +67,23 @@
 
 bool mHciCreditLent = false;
 
+bool ready_flag = 0;
+
+void wait_ready() {
+  pthread_mutex_lock(&mutex);
+  while (!ready_flag) {
+    pthread_cond_wait(&ready_cond, &mutex);
+  }
+  pthread_mutex_unlock(&mutex);
+}
+
+void set_ready(bool ready) {
+  pthread_mutex_lock(&mutex);
+  ready_flag = ready;
+  pthread_cond_signal(&ready_cond);
+  pthread_mutex_unlock(&mutex);
+}
+
 bool hal_wrapper_open(st21nfc_dev_t* dev, nfc_stack_callback_t* p_cback,
                       nfc_stack_data_callback_t* p_data_cback,
                       HALHANDLE* pHandle) {
@@ -115,17 +136,47 @@
   return 1;
 }
 
-void hal_wrapper_send_prop_config() {
-  uint8_t coreSetConfig[] = {0x20, 0x02, 0x07, 0x02, 0xa1,
-                             0x01, 0x19, 0xa2, 0x01, 0x14};
+void hal_wrapper_send_core_config_prop() {
+  long retlen = 0;
+  int isfound = 0;
 
-  // Send prop config and merge mode
-  STLOG_HAL_V("%s - Sending CORE_SET_CONFIG(LPS+Merge)", __func__);
-  if (!HalSendDownstream(mHalHandle, coreSetConfig, sizeof(coreSetConfig))) {
-    STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
+  // allocate buffer for setting parameters
+  ConfigBuffer = (uint8_t*)malloc(256 * sizeof(uint8_t));
+  if (ConfigBuffer != NULL) {
+    isfound = GetByteArrayValue(NAME_CORE_CONF_PROP, (char*)ConfigBuffer, 256,
+                                &retlen);
+
+    if (isfound > 0) {
+      STLOG_HAL_V("%s - Enter", __func__);
+      set_ready(0);
+
+      if (!HalSendDownstreamTimer(mHalHandle, ConfigBuffer, retlen, 500)) {
+        STLOG_HAL_E("NFC-NCI HAL: %s  SendDownstream failed", __func__);
+      }
+      mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
+      wait_ready();
+    }
+    free(ConfigBuffer);
+    ConfigBuffer = NULL;
   }
+}
 
+void hal_wrapper_send_vs_config() {
+  STLOG_HAL_V("%s - Enter", __func__);
+  set_ready(0);
+
+  if (!HalSendDownstreamTimer(mHalHandle, nciPropGetFwDbgTracesConfig,
+                              sizeof(nciPropGetFwDbgTracesConfig), 500)) {
+    STLOG_HAL_E("%s - SendDownstream failed", __func__);
+  }
+  mReadFwConfigDone = true;
+  wait_ready();
+}
+
+void hal_wrapper_send_config() {
+  hal_wrapper_send_core_config_prop();
   mHalWrapperState = HAL_WRAPPER_STATE_PROP_CONFIG;
+  hal_wrapper_send_vs_config();
 }
 
 void halWrapperDataCallback(uint16_t data_len, uint8_t* p_data) {
@@ -263,12 +314,10 @@
                   __func__);
       // CORE_SET_CONFIG_RSP
       if ((p_data[0] == 0x40) && (p_data[1] == 0x02)) {
+        HalSendDownstreamStopTimer(mHalHandle);
+        set_ready(1);
+
         STLOG_HAL_V("%s - Received config RSP, read FW dDBG config", __func__);
-        if (!HalSendDownstream(mHalHandle, nciPropGetFwDbgTracesConfig,
-                               sizeof(nciPropGetFwDbgTracesConfig))) {
-          STLOG_HAL_E("%s - SendDownstream failed", __func__);
-        }
-        mReadFwConfigDone = true;
       } else if (mHciCreditLent && (p_data[0] == 0x60) && (p_data[1] == 0x06)) {
         // CORE_CONN_CREDITS_NTF
         if (p_data[4] == 0x01) {  // HCI connection
@@ -289,7 +338,8 @@
         // PROP_RSP
         if (mReadFwConfigDone == true) {
           mReadFwConfigDone = false;
-
+          HalSendDownstreamStopTimer(mHalHandle);
+          set_ready(1);
           // NFC_STATUS_OK
           if (p_data[3] == 0x00) {
             bool confNeeded = false;
@@ -422,6 +472,15 @@
         return;
       }
       break;
+    case HAL_WRAPPER_STATE_PROP_CONFIG:
+      if (event == HAL_WRAPPER_TIMEOUT_EVT) {
+        STLOG_HAL_E("%s - Timer when sending conf parameters, retry", __func__);
+        HalSendDownstreamStopTimer(mHalHandle);
+        resetHandlerState();
+        I2cResetPulse();
+        mHalWrapperState = HAL_WRAPPER_STATE_OPEN;
+      }
+      break;
 
     default:
       break;
diff --git a/st21nfc/include/android_logmsg.h b/st21nfc/include/android_logmsg.h
index 01ba59c..1392d4c 100644
--- a/st21nfc/include/android_logmsg.h
+++ b/st21nfc/include/android_logmsg.h
@@ -48,6 +48,7 @@
 #define NAME_STNFC_FW_CONF_NAME "STNFC_FW_CONF_NAME"
 #define NAME_STNFC_FW_BIN_NAME "STNFC_FW_BIN_NAME"
 #define NAME_STNFC_FW_DEBUG_ENABLED "STNFC_FW_DEBUG_ENABLED"
+#define NAME_CORE_CONF_PROP "CORE_CONF_PROP"
 
 /* #######################
  * Set the logging level
diff --git a/st21nfc/include/hal_config.h b/st21nfc/include/hal_config.h
index f053563..8bc2932 100644
--- a/st21nfc/include/hal_config.h
+++ b/st21nfc/include/hal_config.h
@@ -41,5 +41,6 @@
 #define NAME_OFFHOST_ROUTE_UICC "OFFHOST_ROUTE_UICC"
 #define NAME_DEFAULT_ISODEP_ROUTE "DEFAULT_ISODEP_ROUTE"
 #define NAME_STNFC_USB_CHARGING_MODE "STNFC_USB_CHARGING_MODE"
+#define NAME_CORE_CONF_PROP "CORE_CONF_PROP"
 
 #endif
diff --git a/st21nfc/libnfc-hal-st-example.conf b/st21nfc/libnfc-hal-st-example.conf
new file mode 100644
index 0000000..cfc13d9
--- /dev/null
+++ b/st21nfc/libnfc-hal-st-example.conf
@@ -0,0 +1,147 @@
+########################### Start of libnf-hal-st_aosp.conf ###########################
+
+###############################################################################
+###############################################################################
+# ST HAL trace log level
+STNFC_HAL_LOGLEVEL=4
+NFC_DEBUG_ENABLED=1
+
+###############################################################################
+# Vendor specific mode to enable FW (RF & SWP) traces.
+STNFC_FW_DEBUG_ENABLED=0
+
+###############################################################################
+# File used for NFA storage
+NFA_STORAGE="/data/nfc"
+
+###############################################################################
+# Keep the nfa storage file.
+PRESERVE_STORAGE=1
+
+###############################################################################
+# In Switch OFF mode (phone switched-off), specify the desired CE mode to
+# the controller.
+# 0: No card-emulation; DEFAULT
+# 1: Switch-off card-emulation enabled
+CE_ON_SWITCH_OFF_STATE=1
+
+###############################################################################
+# Vendor specific mode to support the USB charging mode if VPSIO=1 in switch off.
+STNFC_USB_CHARGING_MODE=1
+
+###############################################################################
+# Vendor Specific Proprietary Protocol & Discovery Configuration
+# Set to 0xFF if unsupported
+#  byte[0] NCI_PROTOCOL_18092_ACTIVE
+#  byte[1] NCI_PROTOCOL_B_PRIME
+#  byte[2] NCI_PROTOCOL_DUAL
+#  byte[3] NCI_PROTOCOL_15693
+#  byte[4] NCI_PROTOCOL_KOVIO
+#  byte[5] NCI_PROTOCOL_MIFARE
+#  byte[6] NCI_DISCOVERY_TYPE_POLL_KOVIO
+#  byte[7] NCI_DISCOVERY_TYPE_POLL_B_PRIME
+#  byte[8] NCI_DISCOVERY_TYPE_LISTEN_B_PRIME
+NFA_PROPRIETARY_CFG={05:FF:FF:06:8A:90:77:FF:FF}
+
+###############################################################################
+# Choose the presence-check algorithm for type-4 tag.  If not defined,
+# the default value is 1.
+# 0  NFA_RW_PRES_CHK_DEFAULT; Let stack selects an algorithm
+# 1  NFA_RW_PRES_CHK_I_BLOCK; ISO-DEP protocol's empty I-block
+# 2  NFA_RW_PRES_CHK_RESET; Deactivate to Sleep, then re-activate
+# 3  NFA_RW_PRES_CHK_RB_CH0; Type-4 tag protocol's ReadBinary command on channel 0
+# 4  NFA_RW_PRES_CHK_RB_CH3; Type-4 tag protocol's ReadBinary command on channel 3
+# 5  NFA_RW_PRES_CHK_ISO_DEP_NAK; presence check command ISO-DEP NAK as per NCI2.0
+PRESENCE_CHECK_ALGORITHM=5
+
+###############################################################################
+# Name of the NCI HAL module to use
+# If unset, falls back to nfc_nci.bcm2079x
+NCI_HAL_MODULE="nfc_nci.st21nfc"
+
+###############################################################################
+# White list to be set at startup.
+DEVICE_HOST_WHITE_LIST={02:C0}
+
+###############################################################################
+# BAIL OUT value for P2P
+# Implements algorithm for NFC-DEP protocol priority over ISO-DEP protocol.
+POLL_BAIL_OUT_MODE=1
+
+###############################################################################
+# Extended APDU length for ISO_DEP
+ISO_DEP_MAX_TRANSCEIVE=0xFEFF
+
+###############################################################################
+# Configure the NFC Extras to open and use a static pipe.  If the value is
+# not set or set to 0, then the default is use a dynamic pipe based on a
+# destination gate (see NFA_HCI_DEFAULT_DEST_GATE).  Note there is a value
+# for each EE (ESE/SIM)
+OFF_HOST_ESE_PIPE_ID=0x5E
+OFF_HOST_SIM_PIPE_ID=0x3E
+
+###############################################################################
+#Set the default Felica T3T System Code OffHost route Location :
+#This settings will be used when application does not set this parameter
+# host  0x00
+# eSE   0x82 (eSE),    0x86 (eUICC/SPI-SE)
+# UICC  0x81 (UICC_1), 0x85 (UICC_2)
+DEFAULT_SYS_CODE_ROUTE=0x86
+
+###############################################################################
+#Set the Felica T3T System Code supported power state:
+DEFAULT_SYS_CODE_PWR_STATE=0x3B
+
+###############################################################################
+# Path and Files used for FW update binaries storage
+STNFC_FW_PATH_STORAGE="/vendor/firmware"
+STNFC_FW_BIN_NAME="/st54j_fw.bin"
+STNFC_FW_CONF_NAME="/st54j_conf.bin"
+
+###############################################################################
+# Default off-host route for Felica.
+# This settings will be used when application does not set this parameter
+# host  0x00
+# eSE   0x82 (eSE),    0x86 (eUICC/SPI-SE)
+# UICC  0x81 (UICC_1), 0x85 (UICC_2)
+DEFAULT_NFCF_ROUTE=0x86
+
+###############################################################################
+# Configure the default off-host route.
+# used for technology A and B routing 
+# eSE   0x82 (eSE),    0x86 (eUICC/SPI-SE)
+# UICC  0x81 (UICC_1), 0x85 (UICC_2)
+DEFAULT_OFFHOST_ROUTE=0x81
+
+###############################################################################
+# Configure the default AID route.
+# host  0x00
+# eSE   0x82 (eSE),    0x86 (eUICC/SPI-SE)
+# UICC  0x81 (UICC_1), 0x85 (UICC_2)
+DEFAULT_ROUTE=0x00
+
+###############################################################################
+# Configure the NFCEEIDs of offhost UICC.
+# UICC  0x81 (UICC_1), 0x85 (UICC_2)
+OFFHOST_ROUTE_UICC={81}
+
+###############################################################################
+# Configure the NFCEEIDs of offhost eSEs.
+# eSE   0x82 (eSE),    0x86 (eUICC/SPI-SE)
+OFFHOST_ROUTE_ESE={86}
+
+###############################################################################
+# Configure the list of NFCEE for the ISO-DEP routing.
+# host  0x00
+# eSE   0x82 (eSE),    0x86 (eUICC/SPI-SE)
+# UICC  0x81 (UICC_1), 0x85 (UICC_2)
+DEFAULT_ISODEP_ROUTE=0x81
+
+###############################################################################
+# Core configuration settings
+CORE_CONF_PROP={ 20, 02, 0a, 03,
+        a1, 01, 19,
+        a2, 01, 15,
+        80, 01, 01       
+}
+
diff --git a/st21nfc/libnfc-st-example.conf b/st21nfc/libnfc-st-example.conf
deleted file mode 100644
index fde1034..0000000
--- a/st21nfc/libnfc-st-example.conf
+++ /dev/null
@@ -1,66 +0,0 @@
-########################### Start of libnfc-st.conf ###########################
-
-###############################################################################
-###############################################################################
-# ST HAL trace log level
-STNFC_HAL_LOGLEVEL=3
-NFC_DEBUG_ENABLED=1
-###############################################################################
-# File used for NFA storage
-NFA_STORAGE="/data/nfc"
-
-###############################################################################
-# Keep the nfa storage file.
-PRESERVE_STORAGE=1
-
-###############################################################################
-# In Switch OFF mode (phone switched-off), specify the desired CE mode to
-# the controller.
-# 0: No card-emulation; DEFAULT
-# 1: Switch-off card-emulation enabled
-CE_ON_SWITCH_OFF_STATE=1
-
-###############################################################################
-# Choose the presence-check algorithm for type-4 tag.  If not defined,
-# the default value is 1.
-# 0  NFA_RW_PRES_CHK_DEFAULT; Let stack selects an algorithm
-# 1  NFA_RW_PRES_CHK_I_BLOCK; ISO-DEP protocol's empty I-block
-# 2  NFA_RW_PRES_CHK_RESET; Deactivate to Sleep, then re-activate
-# 3  NFA_RW_PRES_CHK_RB_CH0; Type-4 tag protocol's ReadBinary command on channel 0
-# 4  NFA_RW_PRES_CHK_RB_CH3; Type-4 tag protocol's ReadBinary command on channel 3
-# 5  NFA_RW_PRES_CHK_ISO_DEP_NAK; presence check command ISO-DEP NAK as per NCI2.0
-
-PRESENCE_CHECK_ALGORITHM=5
-
-###############################################################################
-# Name of the NCI HAL module to use
-# If unset, falls back to nfc_nci.bcm2079x
-NCI_HAL_MODULE="nfc_nci.st21nfc"
-
-###############################################################################
-# White list to be set at startup.
-DEVICE_HOST_WHITE_LIST={02:C0}
-
-###############################################################################
-# BAIL OUT value for P2P
-# Implements algorithm for NFC-DEP protocol priority over ISO-DEP protocol.
-POLL_BAIL_OUT_MODE=1
-
-###############################################################################
-# File used for NFA storage
-STNFC_FW_PATH_STORAGE="/vendor/firmware"
-
-
-###############################################################################
-# Vendor Specific Proprietary Protocol & Discovery Configuration
-# Set to 0xFF if unsupported
-#  byte[0] NCI_PROTOCOL_18092_ACTIVE
-#  byte[1] NCI_PROTOCOL_B_PRIME
-#  byte[2] NCI_PROTOCOL_DUAL
-#  byte[3] NCI_PROTOCOL_15693
-#  byte[4] NCI_PROTOCOL_KOVIO
-#  byte[5] NCI_PROTOCOL_MIFARE
-#  byte[6] NCI_DISCOVERY_TYPE_POLL_KOVIO
-#  byte[7] NCI_DISCOVERY_TYPE_POLL_B_PRIME
-#  byte[8] NCI_DISCOVERY_TYPE_LISTEN_B_PRIME
-NFA_PROPRIETARY_CFG={05:FF:FF:06:77:90:77:FF:FF}