Skip to content

Commit

Permalink
Patch to manage TARGET_LOST status in case of Multiple protocol tag
Browse files Browse the repository at this point in the history
Plumb status codes up through the stack.

Change-Id: Id30e69fff3bb56082ab29d42f166cb12c3061857
  • Loading branch information
Daniel Tomas authored and Nick Pelly committed Jul 25, 2011
1 parent 04a29c8 commit 2c3f9be
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 72 deletions.
63 changes: 34 additions & 29 deletions jni/com_android_nfc_NativeNfcTag.cpp
Expand Up @@ -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;

Expand All @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -491,28 +495,26 @@ 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();

/* Create the local semaphore */
if (!nfc_cb_data_init(&cb_data, &pRemDevInfo))
{
status = NFCSTATUS_NOT_ENOUGH_MEMORY;
goto clean_and_return;
}

Expand All @@ -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
Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -1008,7 +1013,7 @@ static bool com_android_nfc_NativeNfcTag_doCheckNdef(JNIEnv *e, jobject o, jintA
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)
Expand Down Expand Up @@ -1221,19 +1226,19 @@ static jboolean com_android_nfc_NativeNfcTag_doMakeReadonly(JNIEnv *e, jobject o
*/
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},
Expand Down
53 changes: 30 additions & 23 deletions src/com/android/nfc/NativeNfcTag.java
Expand Up @@ -43,6 +43,8 @@ public class NativeNfcTag {

private int mConnectedTechnology; // Index in mTechList

private int mLastStatusCode;

private final String TAG = "NativeNfcTag";

private boolean mIsPresent; // Whether the tag is known to be still present
Expand Down Expand Up @@ -115,12 +117,12 @@ public synchronized void run() {
}
}

private native boolean doConnect(int handle);
public synchronized boolean connect(int technology) {
private native int doConnect(int handle);
public synchronized int connect(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
Expand All @@ -139,45 +141,45 @@ public synchronized boolean connect(int technology) {
// allowed.
if (mConnectedTechnology == -1) {
// Not connected yet
isSuccess = doConnect(mTechHandles[i]);
status = doConnect(mTechHandles[i]);
}
else if ((mConnectedTechnology != -1) &&
(mTechHandles[mConnectedTechnology] != mTechHandles[i])) {
// Connect to a tech with a different handle
isSuccess = reconnect(mTechHandles[i]);
status = reconnect(mTechHandles[i]);
}
else {
// Already connected to a tech with the same handle
// Only allow Ndef / NdefFormatable techs to return
// success
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;
} else {
isSuccess = true;
status = 0;
}
}
}
if (isSuccess) {
if (status == 0) {
mConnectedTechnology = i;
}
} else {
isSuccess = true; // Already connect to this tech
status = 0; // Already connect to this tech
}
break;
}
}
if (mWatchdog != null) {
mWatchdog.doResume();
}
return isSuccess;
mLastStatusCode = status;
return status;
}

public synchronized void startPresenceChecking() {
Expand Down Expand Up @@ -219,28 +221,28 @@ public synchronized boolean disconnect() {
return result;
}

native boolean doReconnect();
public synchronized boolean reconnect() {
native int doReconnect();
public synchronized int reconnect() {
if (mWatchdog != null) {
mWatchdog.pause();
}
boolean result = doReconnect();
int status = doReconnect();
if (mWatchdog != null) {
mWatchdog.doResume();
}
return result;
return status;
}

native boolean doHandleReconnect(int handle);
public synchronized boolean reconnect(int handle) {
native int doHandleReconnect(int handle);
public synchronized int reconnect(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);
Expand All @@ -255,16 +257,17 @@ public synchronized byte[] transceive(byte[] data, boolean raw, int[] returnCode
return result;
}

private native boolean doCheckNdef(int[] ndefinfo);
public synchronized boolean checkNdef(int[] ndefinfo) {
private native int doCheckNdef(int[] ndefinfo);
public synchronized int checkNdef(int[] ndefinfo) {
if (mWatchdog != null) {
mWatchdog.pause();
}
boolean result = doCheckNdef(ndefinfo);
int status = doCheckNdef(ndefinfo);
if (mWatchdog != null) {
mWatchdog.doResume();
}
return result;
mLastStatusCode = status;
return status;
}

private native byte[] doRead();
Expand Down Expand Up @@ -348,6 +351,10 @@ public synchronized boolean isNdefFormatable() {
private NativeNfcTag() {
}

public int getLastStatusCode() {
return mLastStatusCode;
}

public int getHandle() {
// This is just a handle for the clients; it can simply use the first
// technology handle we have.
Expand Down

0 comments on commit 2c3f9be

Please sign in to comment.