Prevent SE from being opened while target is in field.

This fixes an issue where an attempt to open the SE while the NFC
Controller is in initiator (reader/writer or P2P Initiator) mode
would cause a strange error response, and all further attempts to
open the SE fail.

Change-Id: I6401f644c73a993433cb73fee2eff8c11ededa1d
diff --git a/jni/com_android_nfc_NativeNfcManager.cpp b/jni/com_android_nfc_NativeNfcManager.cpp
index cdbfa82..dc233ca 100644
--- a/jni/com_android_nfc_NativeNfcManager.cpp
+++ b/jni/com_android_nfc_NativeNfcManager.cpp
@@ -33,6 +33,8 @@
 static phNfc_sData_t gInputParam;
 static phNfc_sData_t gOutputParam;
 
+uint8_t device_connected_flag;
+
 static phLibNfc_Handle              hLlcpHandle;
 static NFCSTATUS                    lastErrorStatus = NFCSTATUS_FAILED;
 static phLibNfc_Llcp_eLinkStatus_t  g_eLinkStatus = phFriNfc_LlcpMac_eLinkDefault;
@@ -371,6 +373,9 @@
       goto clean_and_return;
    }
 
+   /* Reset device connected handle */
+   device_connected_flag = 0;
+
    /* Reset stored handle */
    storedHandle = 0;
 
@@ -662,6 +667,9 @@
    /* Reset the PN544 ISO XCHG / sw watchdog timeouts */
    nfc_jni_reset_timeout_values();
 
+   /* Reset device connected flag */
+   device_connected_flag = 0;
+
    /* Restart Polling loop */
    TRACE("******  Start NFC Discovery ******");
    REENTRANCE_LOCK();
@@ -846,12 +854,15 @@
                                                                                   sLinkParams.miu,
                                                                                   sLinkParams.option,
                                                                                   sLinkParams.wks);
+           device_connected_flag = 1;
       }
    }
    else if(eLinkStatus == phFriNfc_LlcpMac_eLinkDeactivated)
    {
       LOGI("LLCP Link deactivated");
       free(pContextData);
+      /* Reset device connected flag */
+      device_connected_flag = 0;
 
       /* Reset incoming socket list */
       while (!LIST_EMPTY(&pMonitor->incoming_socket_head))
@@ -989,7 +1000,10 @@
    {
       LOG_CALLBACK("nfc_jni_Discovery_notification_callback", status);
       TRACE("Discovered %d tags", uNofRemoteDev);
-      
+
+      /* Reset device connected flag */
+      device_connected_flag = 1;
+
       if((psRemoteDevList->psRemoteDevInfo->RemDevType == phNfc_eNfcIP1_Initiator)
           || (psRemoteDevList->psRemoteDevInfo->RemDevType == phNfc_eNfcIP1_Target))
       {
@@ -1413,6 +1427,9 @@
    /* Reset the PN544 ISO XCHG / sw watchdog timeouts */
    nfc_jni_reset_timeout_values();
 
+   /* Reset device connected flag */
+   device_connected_flag = 0;
+
 #if 0
    nat->discovery_cfg.PollDevInfo.PollCfgInfo.EnableIso14443A = TRUE;
    nat->discovery_cfg.PollDevInfo.PollCfgInfo.EnableIso14443B = TRUE;
@@ -1760,7 +1777,7 @@
    struct nfc_jni_callback_data cb_data;
 
    /* Retrieve native structure address */
-   nat = nfc_jni_get_nat(e, o); 
+   nat = nfc_jni_get_nat(e, o);
 
    /* Clear previous configuration */
    memset(&nat->discovery_cfg, 0, sizeof(phLibNfc_sADD_Cfg_t));
diff --git a/jni/com_android_nfc_NativeNfcSecureElement.cpp b/jni/com_android_nfc_NativeNfcSecureElement.cpp
index f309122..f4a402b 100755
--- a/jni/com_android_nfc_NativeNfcSecureElement.cpp
+++ b/jni/com_android_nfc_NativeNfcSecureElement.cpp
@@ -24,6 +24,7 @@
 static int secureElementHandle;
 extern void                 *gHWRef;
 static int SecureElementTech;
+extern uint8_t device_connected_flag;
 
 namespace android {
 
@@ -204,6 +205,13 @@
    
    TRACE("Open Secure Element");
    
+   /* Check if NFC device is already connected to a tag or P2P peer */
+   if (device_connected_flag == 1)
+   {
+       LOGD("Unable to open SE connection, device already connected to a P2P peer or a Tag");
+       goto clean_and_return;
+   }
+
    /* Test if External RF field is detected */
    InParam.buffer = ExternalRFDetected;
    InParam.length = 3;
@@ -215,6 +223,7 @@
    if(ret!=NFCSTATUS_PENDING)
    {
       LOGE("IOCTL status error");
+      goto clean_and_return;
    }
 
    /* Wait for callback response */
diff --git a/jni/com_android_nfc_NativeNfcTag.cpp b/jni/com_android_nfc_NativeNfcTag.cpp
index 566b28e..ade5307 100644
--- a/jni/com_android_nfc_NativeNfcTag.cpp
+++ b/jni/com_android_nfc_NativeNfcTag.cpp
@@ -25,7 +25,7 @@
 uint8_t *nfc_jni_ndef_buf = NULL;
 uint32_t nfc_jni_ndef_buf_len = 0;
 
-
+extern uint8_t device_connected_flag;
 
 namespace android {
 
@@ -639,9 +639,12 @@
     {
         goto clean_and_return;
     }
+
     result = JNI_TRUE;
 
 clean_and_return:
+    /* Reset device connected flag */
+    device_connected_flag = 0;
    nfc_cb_data_deinit(&cb_data);
    CONCURRENCY_UNLOCK();
    return result;
@@ -1051,6 +1054,7 @@
 
 clean_and_return:
    nfc_cb_data_deinit(&cb_data);
+
    CONCURRENCY_UNLOCK();
 
    return result;
diff --git a/jni/com_android_nfc_NativeP2pDevice.cpp b/jni/com_android_nfc_NativeP2pDevice.cpp
index fd9af3e..ed85620 100644
--- a/jni/com_android_nfc_NativeP2pDevice.cpp
+++ b/jni/com_android_nfc_NativeP2pDevice.cpp
@@ -20,6 +20,8 @@
 
 #include "com_android_nfc.h"
 
+extern uint8_t device_connected_flag;
+
 namespace android {
 
 extern void nfc_jni_restart_discovery_locked(struct nfc_jni_native_data *nat);
@@ -252,9 +254,12 @@
     {
         goto clean_and_return;
     }
+
     result = JNI_TRUE;
 
 clean_and_return:
+    /* Reset device connected flag */
+    device_connected_flag = 0;
     nfc_cb_data_deinit(&cb_data);
     CONCURRENCY_UNLOCK();
     return result;