Implement Felica timeout handling (NFC service).
NFC service interface to set the Felica timeout from the API.
Refactored timeout resets to single method - assuming access
to the SE in wired mode is exclusive with normal tag access,
this should be no problem.
Change-Id: I4a4d1c2c6db12582e5ff6cca325fd14fcf286282
diff --git a/NfcNci/jni/com_android_nfc_NativeNfcManager.cpp b/NfcNci/jni/com_android_nfc_NativeNfcManager.cpp
index d0bd3b7..a1dd891 100644
--- a/NfcNci/jni/com_android_nfc_NativeNfcManager.cpp
+++ b/NfcNci/jni/com_android_nfc_NativeNfcManager.cpp
@@ -596,6 +596,7 @@
     REENTRANCE_LOCK();
     phLibNfc_SetIsoXchgTimeout(NXP_ISO_XCHG_TIMEOUT);
     phLibNfc_SetHciTimeout(NXP_NFC_HCI_TIMEOUT);
+    phLibNfc_SetFelicaTimeout(NXP_FELICA_XCHG_TIMEOUT);
     REENTRANCE_UNLOCK();
 }
 
@@ -1492,12 +1493,32 @@
    CONCURRENCY_UNLOCK();
 }
 
-static void com_android_nfc_NfcManager_doResetIsoDepTimeout( JNIEnv *e, jobject o) {
+static void com_android_nfc_NfcManager_doResetTimeouts( JNIEnv *e, jobject o) {
     CONCURRENCY_LOCK();
     nfc_jni_reset_timeout_values();
     CONCURRENCY_UNLOCK();
 }
 
+
+static void com_android_nfc_NfcManager_doSetFelicaTimeout( JNIEnv *e, jobject o,
+        jint timeout) {
+   CONCURRENCY_LOCK();
+   // The Felica timeout is configurable in the PN544 upto a maximum of 255 ms.
+   // It can be set to 0 to disable the timeout altogether, in which case we
+   // use the sw watchdog as a fallback.
+   if (timeout == 0) {
+       // Disable timeout altogether, not allowed
+       LOGE("It's not allowed to set the NFC Felica timeout to 0!");
+   } else if (timeout <= 255) {
+       phLibNfc_SetFelicaTimeout(timeout);
+   } else {
+       // Disable hw timeout, use sw watchdog for timeout
+       phLibNfc_SetFelicaTimeout(0);
+       phLibNfc_SetHciTimeout(timeout);
+   }
+
+   CONCURRENCY_UNLOCK();
+}
 // Calculates ceiling log2 of value
 static unsigned int log2(int value) {
     unsigned int ret = 0;
@@ -2394,8 +2415,11 @@
    {"doSetIsoDepTimeout", "(I)V",
       (void *)com_android_nfc_NfcManager_doSetIsoDepTimeout},
 
-   {"doResetIsoDepTimeout", "()V",
-      (void *)com_android_nfc_NfcManager_doResetIsoDepTimeout},
+   {"doResetTimeouts", "()V",
+      (void *)com_android_nfc_NfcManager_doResetTimeouts},
+
+   {"doSetFelicaTimeout", "(I)V",
+      (void *)com_android_nfc_NfcManager_doSetFelicaTimeout},
 };   
   
       
diff --git a/NfcNci/src/com/android/nfc/NativeNfcManager.java b/NfcNci/src/com/android/nfc/NativeNfcManager.java
index 7e4db32..3eec71c 100755
--- a/NfcNci/src/com/android/nfc/NativeNfcManager.java
+++ b/NfcNci/src/com/android/nfc/NativeNfcManager.java
@@ -93,9 +93,9 @@
     public native boolean doActivateLlcp();
 
 
-    public native void doResetIsoDepTimeout();
-    public void resetIsoDepTimeout() {
-        doResetIsoDepTimeout();
+    public native void doResetTimeouts();
+    public void resetTimeouts() {
+        doResetTimeouts();
     }
 
     public native void doSetIsoDepTimeout(int timeout);
@@ -103,6 +103,12 @@
         doSetIsoDepTimeout(timeout);
     }
 
+    public native void doSetFelicaTimeout(int timeout);
+    public void setFelicaTimeout(int timeout) {
+        doSetFelicaTimeout(timeout);
+    }
+
+
     /**
      * Notifies Ndef Message (TODO: rename into notifyTargetDiscovered)
      */
diff --git a/NfcNci/src/com/android/nfc/NfcService.java b/NfcNci/src/com/android/nfc/NfcService.java
index 169d9d3..9d767cb 100755
--- a/NfcNci/src/com/android/nfc/NfcService.java
+++ b/NfcNci/src/com/android/nfc/NfcService.java
@@ -1610,10 +1610,17 @@
         }
 
         @Override
-        public void resetIsoDepTimeout() throws RemoteException {
+        public void setFelicaTimeout(int timeout) throws RemoteException {
             mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
 
-            mManager.resetIsoDepTimeout();
+            mManager.setFelicaTimeout(timeout);
+        }
+
+        @Override
+        public void resetTimeouts() throws RemoteException {
+            mContext.enforceCallingOrSelfPermission(NFC_PERM, NFC_PERM_ERROR);
+
+            mManager.resetTimeouts();
         }
     };
 
@@ -1900,7 +1907,7 @@
                     throw new SecurityException("Wrong PID");
                 }
 
-                mManager.doResetIsoDepTimeout();
+                mManager.resetTimeouts();
                 mSecureElement.doDisconnect(mOpenEe.handle);
                 mOpenEe = null;