Add variables in configuration file.

Add the capacity to modify the polling interval and the BGT in the config
file.

Bug: 160305311
Test: Manual
Change-Id: I583e2d05a6029d3bfa06d0d51d5f4333dec75733
(cherry picked from commit 43e98039178046a1ce81cebad3fb34e27dd0687a)
diff --git a/ese-spi-driver/SpiLayerComm.cc b/ese-spi-driver/SpiLayerComm.cc
index a6e4008..3139e80 100644
--- a/ese-spi-driver/SpiLayerComm.cc
+++ b/ese-spi-driver/SpiLayerComm.cc
@@ -35,6 +35,21 @@
 
 /*******************************************************************************
 **
+** Function         SpiLayerComm_init
+**
+** Description      Initialize
+**
+** Parameters       spiDevPath - Spi device path.
+**
+** Returns          null
+**
+*******************************************************************************/
+void SpiLayerComm_init(SpiDriver_config_t* tSpiDriver) {
+  pollInterval = tSpiDriver->polling_interval;
+  STLOG_HAL_D("SpiLayerDriver_init  pollInterval=  %d us", pollInterval);
+}
+/*******************************************************************************
+**
 ** Function         SpiLayerComm_waitForAtpLength
 **
 ** Description      Starts the polling mechanism to read the length of the ATP.
@@ -236,7 +251,8 @@
   // Start the polling mechanism
   while (true) {
     // Wait between each polling sequence
-    usleep(1000);
+    usleep(pollInterval);
+
     // Read the slave response by sending three null bytes
     if (SpiLayerDriver_read(&pollingRxByte, 1) != 1) {
       STLOG_HAL_E("Error reading a valid NAD from the slave.");
@@ -268,7 +284,6 @@
   if (SpiLayerDriver_read(buffer, 2) != 2) {
     return -1;
   }
-
   // Save the prologue read into the tpduRx
   respTpdu->nad = pollingRxByte;
   respTpdu->pcb = buffer[0];
diff --git a/ese-spi-driver/SpiLayerComm.h b/ese-spi-driver/SpiLayerComm.h
index 442f30f..fa06586 100644
--- a/ese-spi-driver/SpiLayerComm.h
+++ b/ese-spi-driver/SpiLayerComm.h
@@ -19,6 +19,7 @@
 #ifndef SPILAYERCOMM_H_
 #define SPILAYERCOMM_H_
 
+#include "SpiLayerInterface.h"
 #include "utils-lib/Tpdu.h"
 
 #define NAD_HOST_TO_SLAVE 0x21
@@ -29,6 +30,13 @@
 // Global variables
 
 /**
+ * Initialize internal variables.
+ *
+ * @return  null
+ */
+
+void SpiLayerComm_init(SpiDriver_config_t* tSpiDriver);
+/**
  * Starts the polling mechanism to read the length of the ATP.
  *
  * @returns 0 if everything is ok, -1 otherwise.
diff --git a/ese-spi-driver/SpiLayerDriver.cc b/ese-spi-driver/SpiLayerDriver.cc
index d2a1509..757fde8 100644
--- a/ese-spi-driver/SpiLayerDriver.cc
+++ b/ese-spi-driver/SpiLayerDriver.cc
@@ -28,9 +28,25 @@
 int currentMode;
 struct timeval lastRxTxTime;
 #define LINUX_DBGBUFFER_SIZE 300
+int BGT = 1000;  // in us
 
 /*******************************************************************************
 **
+** Function         SpiLayerDriver_init
+**
+** Description      Initialize
+**
+** Parameters       spiDevPath - Spi device path.
+**
+** Returns          null
+**
+*******************************************************************************/
+void SpiLayerDriver_init(SpiDriver_config_t* tSpiDriver) {
+  BGT = tSpiDriver->bgt;
+  STLOG_HAL_D("SpiLayerDriver_init  BGT=  %d us", BGT);
+}
+/*******************************************************************************
+**
 ** Function         SpiLayerDriver_open
 **
 ** Description      Open the spi device driver.
@@ -95,11 +111,12 @@
     struct timeval currentTime;
     gettimeofday(&currentTime, 0);
     STLOG_HAL_V("     Now: %ld,%ld", currentTime.tv_sec, currentTime.tv_usec);
-    int elapsedTime = Utils_getElapsedTimeInMs(lastRxTxTime, currentTime);
-    if (elapsedTime < MIN_TIME_BETWEEN_MODE_SWITCH) {
-      int waitTime = MIN_TIME_BETWEEN_MODE_SWITCH - elapsedTime;
-      STLOG_HAL_V("Waiting %d ms to switch from TX to RX", waitTime);
-      usleep(waitTime * 1000);
+
+    int elapsedTimeUs = Utils_getElapsedTimeInUs(lastRxTxTime, currentTime);
+    if (elapsedTimeUs < BGT) {
+      int waitTime = BGT - elapsedTimeUs;
+      STLOG_HAL_V("Waiting %d us to switch from TX to RX", waitTime);
+      usleep(waitTime);
     }
     gettimeofday(&currentTime, 0);
     STLOG_HAL_V("Start RX: %ld,%ld", currentTime.tv_sec, currentTime.tv_usec);
@@ -162,12 +179,12 @@
     STLOG_HAL_V(" Last RX: %ld,%ld", lastRxTxTime.tv_sec, lastRxTxTime.tv_usec);
     struct timeval currentTime;
     gettimeofday(&currentTime, 0);
-    STLOG_HAL_V("     Now: %ld,%ld", currentTime.tv_sec, currentTime.tv_usec);
-    int elapsedTime = Utils_getElapsedTimeInMs(lastRxTxTime, currentTime);
-    if (elapsedTime < MIN_TIME_BETWEEN_MODE_SWITCH) {
-      int waitTime = MIN_TIME_BETWEEN_MODE_SWITCH - elapsedTime;
-      STLOG_HAL_V("Waiting %d ms to switch from RX to TX", waitTime);
-      usleep(waitTime * 1000);
+
+    int elapsedTimeUs = Utils_getElapsedTimeInUs(lastRxTxTime, currentTime);
+    if (elapsedTimeUs < BGT) {
+      int waitTime = BGT - elapsedTimeUs;
+      STLOG_HAL_V("Waiting %d us to switch from RX to TX", waitTime);
+      usleep(waitTime);
     }
     gettimeofday(&currentTime, 0);
     STLOG_HAL_V("Start TX: %ld,%ld", currentTime.tv_sec, currentTime.tv_usec);
diff --git a/ese-spi-driver/SpiLayerDriver.h b/ese-spi-driver/SpiLayerDriver.h
index 5da1ab6..61b75cc 100644
--- a/ese-spi-driver/SpiLayerDriver.h
+++ b/ese-spi-driver/SpiLayerDriver.h
@@ -25,6 +25,7 @@
 #include <stdio.h>
 #include <sys/ioctl.h>
 #include <unistd.h>
+#include "SpiLayerInterface.h"
 
 #define ATP_FILE_PATH "/data/vendor/ese/atp.bin"
 
@@ -35,6 +36,14 @@
 #define ST54J_SE_PULSE_RESET _IOR(ST54J_SE_MAGIC, 0x01, unsigned int)
 
 /**
+ * Initialize internal variables.
+ *
+ * @return  null
+ */
+
+void SpiLayerDriver_init(SpiDriver_config_t* tSpiDriver);
+
+/**
  * Open the spi device driver.
  *
  * @return  -1 if an error occurred, file descriptor if success.
diff --git a/ese-spi-driver/SpiLayerInterface.cc b/ese-spi-driver/SpiLayerInterface.cc
index 11b9f65..11a7065 100644
--- a/ese-spi-driver/SpiLayerInterface.cc
+++ b/ese-spi-driver/SpiLayerInterface.cc
@@ -51,6 +51,9 @@
   // Configure the SPI before start the data exchange with the eSE
   char* spiDevPath = tSpiDriver->pDevName;
 
+  SpiLayerComm_init(tSpiDriver);
+  SpiLayerDriver_init(tSpiDriver);
+
   int DevHandle = SpiLayerDriver_open(spiDevPath);
   tSpiDriver->pDevHandle = (void*)((intptr_t)DevHandle);
   if (DevHandle == -1) {
diff --git a/ese-spi-driver/SpiLayerInterface.h b/ese-spi-driver/SpiLayerInterface.h
index 01c0cf9..c6ed047 100644
--- a/ese-spi-driver/SpiLayerInterface.h
+++ b/ese-spi-driver/SpiLayerInterface.h
@@ -30,6 +30,10 @@
    * e.g. On Linux based systems this would be /dev/p73
    */
 
+  int bgt;
+
+  int polling_interval;
+
   uint32_t devMaxFreq;
   /*!< Communication speed between DH and ESE
    *
diff --git a/ese-spi-driver/StEseApi.cc b/ese-spi-driver/StEseApi.cc
index 248be0d..f47ede6 100644
--- a/ese-spi-driver/StEseApi.cc
+++ b/ese-spi-driver/StEseApi.cc
@@ -18,11 +18,11 @@
  ******************************************************************************/
 #define LOG_TAG "StEse_HalApi"
 
-#include <pthread.h>
 #include "StEseApi.h"
-#include "SpiLayerComm.h"
 #include <cutils/properties.h>
 #include <ese_config.h>
+#include <pthread.h>
+#include "SpiLayerComm.h"
 #include "T1protocol.h"
 #include "android_logmsg.h"
 
@@ -31,10 +31,14 @@
 /* ESE Context structure */
 ese_Context_t ese_ctxt;
 
-const char* halVersion = "ST54-SE HAL1.0 Version 1.0.21";
+const char* halVersion = "ST54-SE HAL1.0 Version 1.0.22";
 
 pthread_mutex_t mutex;
 
+bool ConfRead = 0;
+unsigned int PollInt_confvalue = 1000;
+unsigned int BGT_confvalue = 1000;
+
 /******************************************************************************
  * Function         StEseLog_InitializeLogLevel
  *
@@ -84,6 +88,16 @@
   strcpy(ese_dev_node, ese_node.c_str());
   tSpiDriver.pDevName = ese_dev_node;
 
+  if (!ConfRead) {
+    PollInt_confvalue =
+        EseConfig::getUnsigned(NAME_ST_ESE_DEV_POLLING_INTERVAL, 1000);
+    BGT_confvalue = EseConfig::getUnsigned(NAME_ST_ESE_DEV_BGT, 1000);
+    ConfRead = true;
+  }
+
+  tSpiDriver.polling_interval = PollInt_confvalue;
+  tSpiDriver.bgt = BGT_confvalue;
+
   /* Initialize SPI Driver layer */
   if (T1protocol_init(&tSpiDriver) != ESESTATUS_SUCCESS) {
     STLOG_HAL_E("T1protocol_init Failed");
diff --git a/ese-spi-driver/utils-lib/Utils.cc b/ese-spi-driver/utils-lib/Utils.cc
index c444b2b..7c411cd 100644
--- a/ese-spi-driver/utils-lib/Utils.cc
+++ b/ese-spi-driver/utils-lib/Utils.cc
@@ -62,6 +62,22 @@
 

 /*******************************************************************************

 **

+** Function        Utils_getElapsedTimeInUs

+**

+** Description     Returns the difference of time (in Us) between t1 and t2.

+**

+** Parameters      t1  - initial time.

+**                 t2  - final time.

+**

+** Returns       The difference t2 - t1 in ms.

+**

+*******************************************************************************/

+int Utils_getElapsedTimeInUs(struct timeval t1, struct timeval t2) {

+  return (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);

+}

+

+/*******************************************************************************

+**

 ** Function        Utils_printCurrentTime

 **

 ** Description     Prints current time to standard log.

diff --git a/ese-spi-driver/utils-lib/Utils.h b/ese-spi-driver/utils-lib/Utils.h
index 16bded2..3c74beb 100644
--- a/ese-spi-driver/utils-lib/Utils.h
+++ b/ese-spi-driver/utils-lib/Utils.h
@@ -45,6 +45,16 @@
 int Utils_getElapsedTimeInMs(struct timeval t1, struct timeval t2);

 

 /**

+ * Returns the difference of time (in us) between t1 and t2.

+ *

+ * @param t1 The initial time.

+ * @param t2 The final time.

+ *

+ * @return The difference t2 - t1 in ms.

+ */

+int Utils_getElapsedTimeInUs(struct timeval t1, struct timeval t2);

+

+/**

  * Prints current time to standard log.

  *

  * @param prefix The prefix to be printed before the time.

diff --git a/ese-spi-driver/utils-lib/ese_config.h b/ese-spi-driver/utils-lib/ese_config.h
index 84624e5..5fd41a4 100644
--- a/ese-spi-driver/utils-lib/ese_config.h
+++ b/ese-spi-driver/utils-lib/ese_config.h
@@ -28,6 +28,8 @@
  * ########################## */
 #define NAME_STESE_HAL_LOGLEVEL "STESE_HAL_LOGLEVEL"
 #define NAME_ST_ESE_DEV_NODE "ST_ESE_DEV_NODE"
+#define NAME_ST_ESE_DEV_BGT "ST_ESE_DEV_BGT"
+#define NAME_ST_ESE_DEV_POLLING_INTERVAL "ST_ESE_DEV_POLLING_INTERVAL"
 
 class EseConfig {
  public:
diff --git a/libese-hal-st.conf b/libese-hal-st.conf
index a650760..c43e0e1 100644
--- a/libese-hal-st.conf
+++ b/libese-hal-st.conf
@@ -5,6 +5,15 @@
 # ST HAL trace log level
 STESE_HAL_LOGLEVEL=4
 
-ST_ESE_DEV_NODE="/dev/st54j"
+###############################################################################
+# Configure the device node
+ST_ESE_DEV_NODE="/dev/st54j_se"
 
+###############################################################################
+# Configure the polling interval (in us)
+ST_ESE_DEV_POLLING_INTERVAL=1500
+
+###############################################################################
+# Configure the BGT timing (in us)
+ST_ESE_DEV_BGT=1000