summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNick Pelly <npelly@google.com>2011-07-26 14:43:43 -0700
committerNick Pelly <npelly@google.com>2011-07-26 16:51:13 -0700
commit70bbea61637e3f9eb7202efd243b9d2f9516a06a (patch)
treef08ccfaf34727be8aa8d56f5023e20b562dbb1b0
parent742eacb628af1f2e008c76462cca3c651a6433b7 (diff)
parent5057a59181dc0e814a219fe18fc356b7c72772cc (diff)
downloadpackages_apps_nfc-70bbea61637e3f9eb7202efd243b9d2f9516a06a.zip
packages_apps_nfc-70bbea61637e3f9eb7202efd243b9d2f9516a06a.tar.gz
packages_apps_nfc-70bbea61637e3f9eb7202efd243b9d2f9516a06a.tar.bz2
resolved conflicts for merge of 5057a591 to master
While I was there, rework of NativeNfcTag.findAndReadNdef() Change-Id: I971403d880f281c8f64d2f9ed60756f6e692e9ae
-rw-r--r--jni/com_android_nfc_NativeNfcTag.cpp63
-rwxr-xr-xsrc/com/android/nfc/NfcService.java12
-rwxr-xr-xsrc/com/android/nfc/nxp/NativeNfcTag.java201
3 files changed, 147 insertions, 129 deletions
diff --git a/jni/com_android_nfc_NativeNfcTag.cpp b/jni/com_android_nfc_NativeNfcTag.cpp
index e0b63cb..18e71df 100644
--- a/jni/com_android_nfc_NativeNfcTag.cpp
+++ b/jni/com_android_nfc_NativeNfcTag.cpp
@@ -444,13 +444,12 @@ static void set_target_activationBytes(JNIEnv *e, jobject tag,
e->ReleaseIntArrayElements(techList, techId, 0);
}
-static jboolean com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e,
+static jint com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e,
jobject o, phLibNfc_Handle handle)
{
jclass cls;
jfieldID f;
- NFCSTATUS status;
- jboolean result = JNI_FALSE;
+ jint status;
struct nfc_jni_callback_data cb_data;
phLibNfc_sRemoteDevInformation_t* pRemDevInfo = NULL;
@@ -459,6 +458,7 @@ static jboolean com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e,
/* Create the local semaphore */
if (!nfc_cb_data_init(&cb_data, &pRemDevInfo))
{
+ status = NFCSTATUS_NOT_ENOUGH_MEMORY;
goto clean_and_return;
}
@@ -478,11 +478,15 @@ static jboolean com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e,
if(sem_wait(&cb_data.sem))
{
LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ status = NFCSTATUS_ABORTED;
goto clean_and_return;
}
-
+
+ status = cb_data.status;
+ TRACE("phLibNfc_RemoteDev_Connect() - Status code = %d", status);
+
/* Connect Status */
- if(cb_data.status != NFCSTATUS_SUCCESS)
+ if(status != NFCSTATUS_SUCCESS)
{
goto clean_and_return;
}
@@ -491,21 +495,18 @@ static jboolean com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e,
set_target_pollBytes(e, o, pRemDevInfo);
set_target_activationBytes(e, o, pRemDevInfo);
- result = JNI_TRUE;
-
clean_and_return:
nfc_cb_data_deinit(&cb_data);
CONCURRENCY_UNLOCK();
- return result;
+ return status;
}
-static jboolean com_android_nfc_NativeNfcTag_doHandleReconnect(JNIEnv *e,
+static jint com_android_nfc_NativeNfcTag_doHandleReconnect(JNIEnv *e,
jobject o, phLibNfc_Handle handle)
{
jclass cls;
jfieldID f;
- NFCSTATUS status;
- jboolean result = JNI_FALSE;
+ jint status;
struct nfc_jni_callback_data cb_data;
phLibNfc_sRemoteDevInformation_t* pRemDevInfo = NULL;
CONCURRENCY_LOCK();
@@ -513,6 +514,7 @@ static jboolean com_android_nfc_NativeNfcTag_doHandleReconnect(JNIEnv *e,
/* Create the local semaphore */
if (!nfc_cb_data_init(&cb_data, &pRemDevInfo))
{
+ status = NFCSTATUS_NOT_ENOUGH_MEMORY;
goto clean_and_return;
}
@@ -532,24 +534,25 @@ static jboolean com_android_nfc_NativeNfcTag_doHandleReconnect(JNIEnv *e,
if(sem_wait(&cb_data.sem))
{
LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ status = NFCSTATUS_ABORTED;
goto clean_and_return;
}
+ status = cb_data.status;
+
/* Connect Status */
- if(cb_data.status != NFCSTATUS_SUCCESS)
+ if(status != NFCSTATUS_SUCCESS)
{
goto clean_and_return;
}
- result = JNI_TRUE;
-
clean_and_return:
nfc_cb_data_deinit(&cb_data);
CONCURRENCY_UNLOCK();
- return result;
+ return status;
}
-static jboolean com_android_nfc_NativeNfcTag_doReconnect(JNIEnv *e,
+static jint com_android_nfc_NativeNfcTag_doReconnect(JNIEnv *e,
jobject o)
{
// Reconnect is provided by libnfc by just calling connect again
@@ -563,11 +566,11 @@ static jboolean com_android_nfc_NativeNfcTag_doReconnect(JNIEnv *e,
return com_android_nfc_NativeNfcTag_doConnect(e, o, handle);
}
else {
- return JNI_TRUE;
+ return NFCSTATUS_SUCCESS;
}
}
else {
- return JNI_FALSE;
+ return NFCSTATUS_REJECTED;
}
}
@@ -940,11 +943,10 @@ static jint com_android_nfc_NativeNfcTag_doGetNdefType(JNIEnv *e, jobject o,
return ndefType;
}
-static bool com_android_nfc_NativeNfcTag_doCheckNdef(JNIEnv *e, jobject o, jintArray ndefinfo)
+static jint com_android_nfc_NativeNfcTag_doCheckNdef(JNIEnv *e, jobject o, jintArray ndefinfo)
{
phLibNfc_Handle handle = 0;
- NFCSTATUS status;
- bool result = JNI_FALSE;
+ jint status;
phLibNfc_ChkNdef_Info_t sNdefInfo;
struct nfc_jni_callback_data cb_data;
jint *ndef = e->GetIntArrayElements(ndefinfo, 0);
@@ -955,6 +957,7 @@ static bool com_android_nfc_NativeNfcTag_doCheckNdef(JNIEnv *e, jobject o, jintA
/* Create the local semaphore */
if (!nfc_cb_data_init(&cb_data, NULL))
{
+ status = NFCSTATUS_NOT_ENOUGH_MEMORY;
goto clean_and_return;
}
cb_data.pContext = &sNdefInfo;
@@ -976,16 +979,18 @@ static bool com_android_nfc_NativeNfcTag_doCheckNdef(JNIEnv *e, jobject o, jintA
if(sem_wait(&cb_data.sem))
{
LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ status = NFCSTATUS_ABORTED;
goto clean_and_return;
}
- if (cb_data.status != NFCSTATUS_SUCCESS)
+ status = cb_data.status;
+ TRACE("phLibNfc_Ndef_CheckNdef() - Status code = %d", status);
+
+ if (status != NFCSTATUS_SUCCESS)
{
goto clean_and_return;
}
- result = JNI_TRUE;
-
ndef[0] = sNdefInfo.MaxNdefMsgLength;
// Translate the card state to know values for the NFC API
switch (sNdefInfo.NdefCardState) {
@@ -1008,7 +1013,7 @@ clean_and_return:
e->ReleaseIntArrayElements(ndefinfo, ndef, 0);
nfc_cb_data_deinit(&cb_data);
CONCURRENCY_UNLOCK();
- return result;
+ return status;
}
static jboolean com_android_nfc_NativeNfcTag_doPresenceCheck(JNIEnv *e, jobject o)
@@ -1221,19 +1226,19 @@ clean_and_return:
*/
static JNINativeMethod gMethods[] =
{
- {"doConnect", "(I)Z",
+ {"doConnect", "(I)I",
(void *)com_android_nfc_NativeNfcTag_doConnect},
{"doDisconnect", "()Z",
(void *)com_android_nfc_NativeNfcTag_doDisconnect},
- {"doReconnect", "()Z",
+ {"doReconnect", "()I",
(void *)com_android_nfc_NativeNfcTag_doReconnect},
- {"doHandleReconnect", "(I)Z",
+ {"doHandleReconnect", "(I)I",
(void *)com_android_nfc_NativeNfcTag_doHandleReconnect},
{"doTransceive", "([BZ[I)[B",
(void *)com_android_nfc_NativeNfcTag_doTransceive},
{"doGetNdefType", "(II)I",
(void *)com_android_nfc_NativeNfcTag_doGetNdefType},
- {"doCheckNdef", "([I)Z",
+ {"doCheckNdef", "([I)I",
(void *)com_android_nfc_NativeNfcTag_doCheckNdef},
{"doRead", "()[B",
(void *)com_android_nfc_NativeNfcTag_doRead},
diff --git a/src/com/android/nfc/NfcService.java b/src/com/android/nfc/NfcService.java
index efe5f8b..64f862c 100755
--- a/src/com/android/nfc/NfcService.java
+++ b/src/com/android/nfc/NfcService.java
@@ -1189,20 +1189,19 @@ public class NfcService extends Application implements DeviceHostListener {
@Override
public boolean isNdef(int nativeHandle) throws RemoteException {
TagEndpoint tag = null;
- boolean isSuccess = false;
// Check if NFC is enabled
if (!mIsNfcEnabled) {
- return isSuccess;
+ return false;
}
/* find the tag in the hmap */
tag = (TagEndpoint) findObject(nativeHandle);
int[] ndefInfo = new int[2];
- if (tag != null) {
- isSuccess = tag.checkNdef(ndefInfo);
+ if (tag == null) {
+ return false;
}
- return isSuccess;
+ return tag.checkNdef(ndefInfo);
}
@Override
@@ -2116,7 +2115,6 @@ public class NfcService extends Application implements DeviceHostListener {
}
final class NfcServiceHandler extends Handler {
-
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
@@ -2151,12 +2149,10 @@ public class NfcService extends Application implements DeviceHostListener {
tag.startPresenceChecking();
dispatchTagEndpoint(tag, ndefMsgs);
} else {
- // No ndef found or connect failed, just try to reconnect and dispatch
if (tag.reconnect()) {
tag.startPresenceChecking();
dispatchTagEndpoint(tag, null);
} else {
- Log.w(TAG, "Failed to connect to tag");
tag.disconnect();
}
}
diff --git a/src/com/android/nfc/nxp/NativeNfcTag.java b/src/com/android/nfc/nxp/NativeNfcTag.java
index 0e1a4e6..f0e123e 100755
--- a/src/com/android/nfc/nxp/NativeNfcTag.java
+++ b/src/com/android/nfc/nxp/NativeNfcTag.java
@@ -37,6 +37,8 @@ import android.util.Log;
public class NativeNfcTag implements TagEndpoint {
static final boolean DBG = false;
+ static final int STATUS_CODE_TARGET_LOST = 146;
+
private int[] mTechList;
private int[] mTechHandles;
private int[] mTechLibNfcTypes;
@@ -131,13 +133,12 @@ public class NativeNfcTag implements TagEndpoint {
}
}
- private native boolean doConnect(int handle);
- @Override
- public synchronized boolean connect(int technology) {
+ private native int doConnect(int handle);
+ public synchronized int connectWithStatus(int technology) {
if (mWatchdog != null) {
mWatchdog.pause();
}
- boolean isSuccess = false;
+ int status = -1;
for (int i = 0; i < mTechList.length; i++) {
if (mTechList[i] == technology) {
// Get the handle and connect, if not already connected
@@ -150,12 +151,12 @@ public class NativeNfcTag implements TagEndpoint {
// switching to that.
if (mConnectedHandle == -1) {
// Not connected yet
- isSuccess = doConnect(mTechHandles[i]);
+ status = doConnect(mTechHandles[i]);
} else {
// Connect to a tech with a different handle
- isSuccess = reconnect(mTechHandles[i]);
+ status = reconnectWithStatus(mTechHandles[i]);
}
- if (isSuccess) {
+ if (status == 0) {
mConnectedHandle = mTechHandles[i];
mConnectedTechIndex = i;
}
@@ -168,19 +169,19 @@ public class NativeNfcTag implements TagEndpoint {
// allowed.
if ((technology == TagTechnology.NDEF) ||
(technology == TagTechnology.NDEF_FORMATABLE)) {
- isSuccess = true;
+ status = 0;
} else {
if ((technology != TagTechnology.ISO_DEP) &&
(hasTechOnHandle(TagTechnology.ISO_DEP, mTechHandles[i]))) {
// Don't allow to connect a -4 tag at a different level
// than IsoDep, as this is not supported by
// libNFC.
- isSuccess = false;
+ status = -1;
} else {
- isSuccess = true;
+ status = 0;
}
}
- if (isSuccess) {
+ if (status == 0) {
mConnectedTechIndex = i;
// Handle was already identical
}
@@ -191,7 +192,11 @@ public class NativeNfcTag implements TagEndpoint {
if (mWatchdog != null) {
mWatchdog.doResume();
}
- return isSuccess;
+ return status;
+ }
+ @Override
+ public synchronized boolean connect(int technology) {
+ return connectWithStatus(technology) == 0;
}
@Override
@@ -211,7 +216,6 @@ public class NativeNfcTag implements TagEndpoint {
// of our knowledge.
return mIsPresent;
}
-
native boolean doDisconnect();
@Override
public synchronized boolean disconnect() {
@@ -237,29 +241,32 @@ public class NativeNfcTag implements TagEndpoint {
return result;
}
- native boolean doReconnect();
- @Override
- public synchronized boolean reconnect() {
+ native int doReconnect();
+ public synchronized int reconnectWithStatus() {
if (mWatchdog != null) {
mWatchdog.pause();
}
- boolean result = doReconnect();
+ int status = doReconnect();
if (mWatchdog != null) {
mWatchdog.doResume();
}
- return result;
+ return status;
+ }
+ @Override
+ public synchronized boolean reconnect() {
+ return reconnectWithStatus() == 0;
}
- native boolean doHandleReconnect(int handle);
- public synchronized boolean reconnect(int handle) {
+ native int doHandleReconnect(int handle);
+ public synchronized int reconnectWithStatus(int handle) {
if (mWatchdog != null) {
mWatchdog.pause();
}
- boolean result = doHandleReconnect(handle);
+ int status = doHandleReconnect(handle);
if (mWatchdog != null) {
mWatchdog.doResume();
}
- return result;
+ return status;
}
private native byte[] doTransceive(byte[] data, boolean raw, int[] returnCode);
@@ -275,17 +282,20 @@ public class NativeNfcTag implements TagEndpoint {
return result;
}
- private native boolean doCheckNdef(int[] ndefinfo);
- @Override
- public synchronized boolean checkNdef(int[] ndefinfo) {
+ private native int doCheckNdef(int[] ndefinfo);
+ private synchronized int checkNdefWithStatus(int[] ndefinfo) {
if (mWatchdog != null) {
mWatchdog.pause();
}
- boolean result = doCheckNdef(ndefinfo);
+ int status = doCheckNdef(ndefinfo);
if (mWatchdog != null) {
mWatchdog.doResume();
}
- return result;
+ return status;
+ }
+ @Override
+ public synchronized boolean checkNdef(int[] ndefinfo) {
+ return checkNdefWithStatus(ndefinfo) == 0;
}
private native byte[] doRead();
@@ -393,10 +403,6 @@ public class NativeNfcTag implements TagEndpoint {
return mTechList;
}
- private int[] getHandleList() {
- return mTechHandles;
- }
-
private int getConnectedHandle() {
return mConnectedHandle;
}
@@ -683,75 +689,86 @@ public class NativeNfcTag implements TagEndpoint {
public NdefMessage[] findAndReadNdef() {
// Try to find NDEF on any of the technologies.
int[] technologies = getTechList();
- int[] handles = getHandleList();
- int techIndex = 0;
- int lastHandleScanned = 0;
- boolean ndefFoundAndConnected = false;
+ int[] handles = mTechHandles;
NdefMessage[] ndefMsgs = null;
boolean foundFormattable = false;
int formattableHandle = 0;
int formattableLibNfcType = 0;
+ int status;
- while ((!ndefFoundAndConnected) && (techIndex < technologies.length)) {
- if (handles[techIndex] != lastHandleScanned) {
- // We haven't seen this handle yet, connect and checkndef
- if (connect(technologies[techIndex])) {
- // Check if this type is NDEF formatable
- if (!foundFormattable) {
- if (isNdefFormatable()) {
- foundFormattable = true;
- formattableHandle = getConnectedHandle();
- formattableLibNfcType = getConnectedLibNfcType();
- // We'll only add formattable tech if no ndef is
- // found - this is because libNFC refuses to format
- // an already NDEF formatted tag.
- }
- reconnect();
- } // else, already found formattable technology
-
- int[] ndefinfo = new int[2];
- if (checkNdef(ndefinfo)) {
- ndefFoundAndConnected = true;
- boolean generateEmptyNdef = false;
-
- int supportedNdefLength = ndefinfo[0];
- int cardState = ndefinfo[1];
- byte[] buff = readNdef();
- if (buff != null) {
- ndefMsgs = new NdefMessage[1];
- try {
- ndefMsgs[0] = new NdefMessage(buff);
- addNdefTechnology(ndefMsgs[0],
- getConnectedHandle(),
- getConnectedLibNfcType(),
- getConnectedTechnology(),
- supportedNdefLength, cardState);
- reconnect();
- } catch (FormatException e) {
- // Create an intent anyway, without NDEF messages
- generateEmptyNdef = true;
- }
- } else {
- generateEmptyNdef = true;
- }
+ for (int techIndex = 0; techIndex < technologies.length; techIndex++) {
+ // have we seen this handle before?
+ for (int i = 0; i < techIndex; i++) {
+ if (handles[i] == handles[techIndex]) {
+ continue; // don't check duplicate handles
+ }
+ }
- if (generateEmptyNdef) {
- ndefMsgs = new NdefMessage[] { };
- addNdefTechnology(null,
- getConnectedHandle(),
- getConnectedLibNfcType(),
- getConnectedTechnology(),
- supportedNdefLength, cardState);
- reconnect();
- }
- } // else, no NDEF on this tech, continue loop
- } else {
- // Connect failed, tag maybe lost. Try next handle
- // anyway.
+ status = connectWithStatus(technologies[techIndex]);
+ if (status != 0) {
+ Log.d(TAG, "Connect Failed - status = "+ status);
+ if (status == STATUS_CODE_TARGET_LOST) {
+ break;
}
+ continue; // try next handle
+ }
+ // Check if this type is NDEF formatable
+ if (!foundFormattable) {
+ if (isNdefFormatable()) {
+ foundFormattable = true;
+ formattableHandle = getConnectedHandle();
+ formattableLibNfcType = getConnectedLibNfcType();
+ // We'll only add formattable tech if no ndef is
+ // found - this is because libNFC refuses to format
+ // an already NDEF formatted tag.
+ }
+ reconnect();
+ }
+
+ int[] ndefinfo = new int[2];
+ status = checkNdefWithStatus(ndefinfo);
+ if (status != 0) {
+ Log.d(TAG, "Check NDEF Failed - status = " + status);
+ if (status == STATUS_CODE_TARGET_LOST) {
+ break;
+ }
+ continue; // try next handle
+ }
+
+ // found our NDEF handle
+ boolean generateEmptyNdef = false;
+
+ int supportedNdefLength = ndefinfo[0];
+ int cardState = ndefinfo[1];
+ byte[] buff = readNdef();
+ if (buff != null) {
+ ndefMsgs = new NdefMessage[1];
+ try {
+ ndefMsgs[0] = new NdefMessage(buff);
+ addNdefTechnology(ndefMsgs[0],
+ getConnectedHandle(),
+ getConnectedLibNfcType(),
+ getConnectedTechnology(),
+ supportedNdefLength, cardState);
+ reconnect();
+ } catch (FormatException e) {
+ // Create an intent anyway, without NDEF messages
+ generateEmptyNdef = true;
+ }
+ } else {
+ generateEmptyNdef = true;
+ }
+
+ if (generateEmptyNdef) {
+ ndefMsgs = new NdefMessage[] { };
+ addNdefTechnology(null,
+ getConnectedHandle(),
+ getConnectedLibNfcType(),
+ getConnectedTechnology(),
+ supportedNdefLength, cardState);
+ reconnect();
}
- lastHandleScanned = handles[techIndex];
- techIndex++;
+ break;
}
if (ndefMsgs == null && foundFormattable) {