Avoid P2P/SMX race conditions in NXP stack.

Google Wallet can trigger off field P2P field events
and initiate communication with the SMX. The NXP stack
appears to contain some tricky race conditions around
opening/closing the SMX close to P2P interactions. For now,
do not allow NFCEE access while a P2P link is still active.

Bug: 8552749
Change-Id: I7acae39bc22cebb5d79a484990dcd3fc5f3dad23
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index dc01eaf..306b706 100755
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -1392,6 +1392,16 @@
                 if (!isNfcEnabled()) {
                     return EE_ERROR_NFC_DISABLED;
                 }
+                if (mInProvisionMode) {
+                    // Deny access to the NFCEE as long as the device is being setup
+                    return EE_ERROR_IO;
+                }
+                if (mDeviceHost.enablePN544Quirks() && mP2pLinkManager.isLlcpActive()) {
+                    // Don't allow PN544-based devices to open the SE while the LLCP
+                    // link is still up or in a debounce state. This avoids race
+                    // conditions in the NXP stack around P2P/SMX switching.
+                    return EE_ERROR_EXT_FIELD;
+                }
                 if (mOpenEe != null) {
                     return EE_ERROR_ALREADY_OPEN;
                 }
diff --git a/src/com/android/nfc/P2pLinkManager.java b/src/com/android/nfc/P2pLinkManager.java
index 4205c7a..29afe95 100755
--- a/src/com/android/nfc/P2pLinkManager.java
+++ b/src/com/android/nfc/P2pLinkManager.java
@@ -265,6 +265,16 @@
     }
 
     /**
+     * May be called from any thread.
+     * @return whether the LLCP link is in an active or debounce state
+     */
+    public boolean isLlcpActive() {
+        synchronized (this) {
+            return mLinkState != LINK_STATE_DOWN;
+        }
+    }
+
+    /**
      * Set NDEF callback for sending.
      * May be called from any thread.
      * NDEF callbacks may be set at any time (even if NFC is