diff options
author | Elliott Hughes <enh@google.com> | 2012-12-18 10:25:42 -0800 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2012-12-18 12:46:17 -0800 |
commit | 2534d9a546f24435d9dab8168f9ea5bf8ca1eb9b (patch) | |
tree | f98f7ba48b7f4c520d004a2bb848a00e0beae68d /nxp | |
parent | b6b8e0edacf8cd0f1b619dc419143483ff47a48f (diff) | |
download | android_packages_apps_Nfc-2534d9a546f24435d9dab8168f9ea5bf8ca1eb9b.tar.gz android_packages_apps_Nfc-2534d9a546f24435d9dab8168f9ea5bf8ca1eb9b.tar.bz2 android_packages_apps_Nfc-2534d9a546f24435d9dab8168f9ea5bf8ca1eb9b.zip |
Fix NFC JNI local reference and pin table leaks.
Also turn compiler warnings up and fix the easy ones.
Bug: http://code.google.com/p/android/issues/detail?id=20441
Change-Id: I1043300f344b8296cfbb6bfad17388d5c3b314d1
Diffstat (limited to 'nxp')
-rw-r--r-- | nxp/jni/Android.mk | 3 | ||||
-rw-r--r-- | nxp/jni/com_android_nfc.cpp | 143 | ||||
-rw-r--r-- | nxp/jni/com_android_nfc.h | 19 | ||||
-rw-r--r-- | nxp/jni/com_android_nfc_NativeLlcpServiceSocket.cpp | 11 | ||||
-rw-r--r-- | nxp/jni/com_android_nfc_NativeNfcManager.cpp | 353 | ||||
-rwxr-xr-x | nxp/jni/com_android_nfc_NativeNfcSecureElement.cpp | 22 | ||||
-rw-r--r-- | nxp/jni/com_android_nfc_NativeNfcTag.cpp | 151 | ||||
-rw-r--r-- | nxp/jni/com_android_nfc_NativeP2pDevice.cpp | 7 |
8 files changed, 318 insertions, 391 deletions
diff --git a/nxp/jni/Android.mk b/nxp/jni/Android.mk index 8ae792a2..8b737096 100644 --- a/nxp/jni/Android.mk +++ b/nxp/jni/Android.mk @@ -18,7 +18,8 @@ LOCAL_SRC_FILES:= \ LOCAL_C_INCLUDES += \ $(JNI_H_INCLUDE) \ external/libnfc-nxp/src \ - external/libnfc-nxp/inc + external/libnfc-nxp/inc \ + libcore/include LOCAL_SHARED_LIBRARIES := \ libnativehelper \ diff --git a/nxp/jni/com_android_nfc.cpp b/nxp/jni/com_android_nfc.cpp index d794d6ec..2852706a 100644 --- a/nxp/jni/com_android_nfc.cpp +++ b/nxp/jni/com_android_nfc.cpp @@ -20,6 +20,7 @@ #include "com_android_nfc.h" #include "com_android_nfc_list.h" #include "phLibNfcStatus.h" +#include <ScopedLocalRef.h> /* * JNI Initialization @@ -28,7 +29,7 @@ jint JNI_OnLoad(JavaVM *jvm, void *reserved) { JNIEnv *e; - ALOGD("NFC Service : loading JNI\n"); + ALOGI("NFC Service: loading nxp JNI"); // Check JNI version if(jvm->GetEnv((void **)&e, JNI_VERSION_1_6)) @@ -128,50 +129,35 @@ void nfc_cb_data_releaseAll() int nfc_jni_cache_object(JNIEnv *e, const char *clsname, jobject *cached_obj) { - jclass cls; - jobject obj; - jmethodID ctor; - - cls = e->FindClass(clsname); - if(cls == NULL) - { - return -1; + ScopedLocalRef<jclass> cls(e, e->FindClass(clsname)); + if (cls.get() == NULL) { ALOGD("Find class error\n"); + return -1; } - - ctor = e->GetMethodID(cls, "<init>", "()V"); - - obj = e->NewObject(cls, ctor); - if(obj == NULL) - { - return -1; + jmethodID ctor = e->GetMethodID(cls.get(), "<init>", "()V"); + ScopedLocalRef<jobject> obj(e, e->NewObject(cls.get(), ctor)); + if (obj.get() == NULL) { ALOGD("Create object error\n"); + return -1; } - *cached_obj = e->NewGlobalRef(obj); - if(*cached_obj == NULL) - { - e->DeleteLocalRef(obj); + *cached_obj = e->NewGlobalRef(obj.get()); + if (*cached_obj == NULL) { ALOGD("Global ref error\n"); return -1; } - e->DeleteLocalRef(obj); - return 0; } struct nfc_jni_native_data* nfc_jni_get_nat(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; - /* Retrieve native structure address */ - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mNative", "I"); - return (struct nfc_jni_native_data*)e->GetIntField(o, f); + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mNative", "I"); + return (struct nfc_jni_native_data*) e->GetIntField(o, f); } struct nfc_jni_native_data* nfc_jni_get_nat_ext(JNIEnv *e) @@ -233,54 +219,39 @@ nfc_jni_native_monitor_t* nfc_jni_init_monitor(void) } return nfc_jni_native_monitor; -} +} nfc_jni_native_monitor_t* nfc_jni_get_monitor(void) { return nfc_jni_native_monitor; } - + phLibNfc_Handle nfc_jni_get_p2p_device_handle(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; - - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mHandle", "I"); - + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mHandle", "I"); return e->GetIntField(o, f); } jshort nfc_jni_get_p2p_device_mode(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; - - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mMode", "S"); - + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mMode", "S"); return e->GetShortField(o, f); } int nfc_jni_get_connected_tech_index(JNIEnv *e, jobject o) { - - jclass c; - jfieldID f; - - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mConnectedTechIndex", "I"); - + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mConnectedTechIndex", "I"); return e->GetIntField(o, f); } jint nfc_jni_get_connected_technology(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; int connectedTech = -1; int connectedTechIndex = nfc_jni_get_connected_tech_index(e,o); @@ -301,62 +272,43 @@ jint nfc_jni_get_connected_technology(JNIEnv *e, jobject o) jint nfc_jni_get_connected_technology_libnfc_type(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; jint connectedLibNfcType = -1; int connectedTechIndex = nfc_jni_get_connected_tech_index(e,o); - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mTechLibNfcTypes", "[I"); - jintArray libNfcTypes = (jintArray) e->GetObjectField(o, f); + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mTechLibNfcTypes", "[I"); + ScopedLocalRef<jintArray> libNfcTypes(e, (jintArray) e->GetObjectField(o, f)); - if ((connectedTechIndex != -1) && (libNfcTypes != NULL) && - (connectedTechIndex < e->GetArrayLength(libNfcTypes))) { - jint* types = e->GetIntArrayElements(libNfcTypes, 0); + if ((connectedTechIndex != -1) && (libNfcTypes.get() != NULL) && + (connectedTechIndex < e->GetArrayLength(libNfcTypes.get()))) { + jint* types = e->GetIntArrayElements(libNfcTypes.get(), 0); if (types != NULL) { connectedLibNfcType = types[connectedTechIndex]; - e->ReleaseIntArrayElements(libNfcTypes, types, JNI_ABORT); + e->ReleaseIntArrayElements(libNfcTypes.get(), types, JNI_ABORT); } } return connectedLibNfcType; - } phLibNfc_Handle nfc_jni_get_connected_handle(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; - - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mConnectedHandle", "I"); - + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mConnectedHandle", "I"); return e->GetIntField(o, f); } phLibNfc_Handle nfc_jni_get_nfc_socket_handle(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; - - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mHandle", "I"); - + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mHandle", "I"); return e->GetIntField(o, f); } jintArray nfc_jni_get_nfc_tag_type(JNIEnv *e, jobject o) { - jclass c; - jfieldID f; - jintArray techtypes; - - c = e->GetObjectClass(o); - f = e->GetFieldID(c, "mTechList","[I"); - - /* Read the techtypes */ - techtypes = (jintArray) e->GetObjectField(o, f); - - return techtypes; + ScopedLocalRef<jclass> c(e, e->GetObjectClass(o)); + jfieldID f = e->GetFieldID(c.get(), "mTechList","[I"); + return (jintArray) e->GetObjectField(o, f); } @@ -365,7 +317,7 @@ jintArray nfc_jni_get_nfc_tag_type(JNIEnv *e, jobject o) const char* nfc_jni_get_status_name(NFCSTATUS status) { #define STATUS_ENTRY(status) { status, #status } - + struct status_entry { NFCSTATUS code; const char *name; @@ -414,7 +366,7 @@ const char* nfc_jni_get_status_name(NFCSTATUS status) }; int i = sizeof(sNameTable)/sizeof(status_entry); - + while(i>0) { i--; @@ -453,9 +405,10 @@ int addTechIfNeeded(int *techList, int* handleList, int* typeList, int listSize, /* * Utility to get a technology tree and a corresponding handle list from a detected tag. */ -void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList, - uint8_t count, jintArray* techList, jintArray* handleList, - jintArray* libnfcTypeList) +void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList, uint8_t count, + ScopedLocalRef<jintArray>* techList, + ScopedLocalRef<jintArray>* handleList, + ScopedLocalRef<jintArray>* libnfcTypeList) { int technologies[MAX_NUM_TECHNOLOGIES]; int handles[MAX_NUM_TECHNOLOGIES]; @@ -546,18 +499,18 @@ void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList, // Build the Java arrays if (techList != NULL) { - *techList = e->NewIntArray(index); - e->SetIntArrayRegion(*techList, 0, index, technologies); + techList->reset(e->NewIntArray(index)); + e->SetIntArrayRegion(techList->get(), 0, index, technologies); } if (handleList != NULL) { - *handleList = e->NewIntArray(index); - e->SetIntArrayRegion(*handleList, 0, index, handles); + handleList->reset(e->NewIntArray(index)); + e->SetIntArrayRegion(handleList->get(), 0, index, handles); } if (libnfcTypeList != NULL) { - *libnfcTypeList = e->NewIntArray(index); - e->SetIntArrayRegion(*libnfcTypeList, 0, index, libnfctypes); + libnfcTypeList->reset(e->NewIntArray(index)); + e->SetIntArrayRegion(libnfcTypeList->get(), 0, index, libnfctypes); } } diff --git a/nxp/jni/com_android_nfc.h b/nxp/jni/com_android_nfc.h index b876dadf..56c229f1 100644 --- a/nxp/jni/com_android_nfc.h +++ b/nxp/jni/com_android_nfc.h @@ -17,10 +17,12 @@ #ifndef __COM_ANDROID_NFC_JNI_H__ #define __COM_ANDROID_NFC_JNI_H__ +#undef LOG_TAG #define LOG_TAG "NFCJNI" #include <JNIHelp.h> #include <jni.h> +#include <ScopedLocalRef.h> #include <pthread.h> #include <sys/queue.h> @@ -59,10 +61,10 @@ extern "C" { #define PROPERTY_LLCP_WKS 2 #define PROPERTY_LLCP_OPT 3 #define PROPERTY_NFC_DISCOVERY_A 4 -#define PROPERTY_NFC_DISCOVERY_B 5 +#define PROPERTY_NFC_DISCOVERY_B 5 #define PROPERTY_NFC_DISCOVERY_F 6 #define PROPERTY_NFC_DISCOVERY_15693 7 -#define PROPERTY_NFC_DISCOVERY_NCFIP 8 +#define PROPERTY_NFC_DISCOVERY_NCFIP 8 /* Error codes */ #define ERROR_BUFFER_TOO_SMALL -12 @@ -140,10 +142,10 @@ struct nfc_jni_native_data int discovery_modes_state[DISCOVERY_MODE_TABLE_SIZE]; phLibNfc_sADD_Cfg_t discovery_cfg; phLibNfc_Registry_Info_t registry_info; - + /* Secure Element selected */ int seId; - + /* LLCP params */ int lto; int miu; @@ -159,7 +161,7 @@ struct nfc_jni_native_data /* p2p modes */ int p2p_initiator_modes; int p2p_target_modes; - + }; typedef struct nfc_jni_native_monitor @@ -231,9 +233,10 @@ nfc_jni_native_monitor_t* nfc_jni_init_monitor(void); nfc_jni_native_monitor_t* nfc_jni_get_monitor(void); int get_technology_type(phNfc_eRemDevType_t type, uint8_t sak); -void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList, - uint8_t count, jintArray* techList, jintArray* handleList, - jintArray* typeList); +void nfc_jni_get_technology_tree(JNIEnv* e, phLibNfc_RemoteDevList_t* devList, uint8_t count, + ScopedLocalRef<jintArray>* techList, + ScopedLocalRef<jintArray>* handleList, + ScopedLocalRef<jintArray>* typeList); /* P2P */ phLibNfc_Handle nfc_jni_get_p2p_device_handle(JNIEnv *e, jobject o); diff --git a/nxp/jni/com_android_nfc_NativeLlcpServiceSocket.cpp b/nxp/jni/com_android_nfc_NativeLlcpServiceSocket.cpp index 2fccfc9c..406079c8 100644 --- a/nxp/jni/com_android_nfc_NativeLlcpServiceSocket.cpp +++ b/nxp/jni/com_android_nfc_NativeLlcpServiceSocket.cpp @@ -16,6 +16,7 @@ #include <semaphore.h> #include <errno.h> +#include <ScopedLocalRef.h> #include "com_android_nfc.h" @@ -73,7 +74,7 @@ static jobject com_NativeLlcpServiceSocket_doAccept(JNIEnv *e, jobject o, jint m phLibNfc_Llcp_sSocketOptions_t sOptions; phNfc_sData_t sWorkingBuffer; jfieldID f; - jclass clsNativeLlcpSocket; + ScopedLocalRef<jclass> clsNativeLlcpSocket(e, NULL); jobject clientSocket = NULL; struct nfc_jni_callback_data cb_data; phLibNfc_Handle hIncomingSocket, hServerSocket; @@ -146,7 +147,7 @@ static jobject com_NativeLlcpServiceSocket_doAccept(JNIEnv *e, jobject o, jint m } /* Get NativeConnectionOriented class object */ - clsNativeLlcpSocket = e->GetObjectClass(clientSocket); + clsNativeLlcpSocket.reset(e->GetObjectClass(clientSocket)); if(e->ExceptionCheck()) { ALOGD("LLCP Socket get class object error"); @@ -154,15 +155,15 @@ static jobject com_NativeLlcpServiceSocket_doAccept(JNIEnv *e, jobject o, jint m } /* Set socket handle */ - f = e->GetFieldID(clsNativeLlcpSocket, "mHandle", "I"); + f = e->GetFieldID(clsNativeLlcpSocket.get(), "mHandle", "I"); e->SetIntField(clientSocket, f,(jint)hIncomingSocket); /* Set socket MIU */ - f = e->GetFieldID(clsNativeLlcpSocket, "mLocalMiu", "I"); + f = e->GetFieldID(clsNativeLlcpSocket.get(), "mLocalMiu", "I"); e->SetIntField(clientSocket, f,(jint)miu); /* Set socket RW */ - f = e->GetFieldID(clsNativeLlcpSocket, "mLocalRw", "I"); + f = e->GetFieldID(clsNativeLlcpSocket.get(), "mLocalRw", "I"); e->SetIntField(clientSocket, f,(jint)rw); TRACE("socket handle 0x%02x: MIU = %d, RW = %d\n",hIncomingSocket, miu, rw); diff --git a/nxp/jni/com_android_nfc_NativeNfcManager.cpp b/nxp/jni/com_android_nfc_NativeNfcManager.cpp index d82291be..d6b6aa5e 100644 --- a/nxp/jni/com_android_nfc_NativeNfcManager.cpp +++ b/nxp/jni/com_android_nfc_NativeNfcManager.cpp @@ -24,6 +24,7 @@ #include <hardware/hardware.h> #include <hardware/nfc.h> #include <cutils/properties.h> +#include <ScopedLocalRef.h> #include "com_android_nfc.h" @@ -86,7 +87,7 @@ static bool performDownload(struct nfc_jni_native_data *nat, bool takeLock); static void client_kill_deferred_call(void* arg) { struct nfc_jni_native_data *nat = (struct nfc_jni_native_data *)arg; - + nat->running = FALSE; } @@ -94,15 +95,15 @@ static void kill_client(nfc_jni_native_data *nat) { phDal4Nfc_Message_Wrapper_t wrapper; phLibNfc_DeferredCall_t *pMsg; - + usleep(50000); ALOGD("Terminating client thread..."); - + pMsg = (phLibNfc_DeferredCall_t*)malloc(sizeof(phLibNfc_DeferredCall_t)); pMsg->pCallback = client_kill_deferred_call; pMsg->pParameter = (void*)nat; - + wrapper.msg.eMsgType = PH_LIBNFC_DEFERREDCALL_MSG; wrapper.msg.pMsgData = pMsg; wrapper.msg.Size = sizeof(phLibNfc_DeferredCall_t); @@ -359,7 +360,7 @@ static int nfc_jni_initialize(struct nfc_jni_native_data *nat) { goto force_download; } TRACE("phLibNfc_Mgt_Initialize returned 0x%04x[%s]", status, nfc_jni_get_status_name(status)); - + /* Wait for callback response */ if(sem_wait(&cb_data.sem)) { @@ -724,7 +725,7 @@ static void *nfc_jni_client_thread(void *arg) } } TRACE("NFC client stopped"); - + nat->vm->DetachCurrentThread(); return NULL; @@ -738,7 +739,7 @@ static phLibNfc_sNfcIPCfg_t nfc_jni_nfcip1_cfg = { 3, { 0x46, 0x66, 0x6D } -}; +}; /* * Callbacks @@ -762,7 +763,7 @@ static void nfc_jni_llcp_linkStatus_callback(void *pContext, TRACE("Callback: nfc_jni_llcp_linkStatus_callback()"); nat->vm->GetEnv( (void **)&e, nat->env_version); - + /* Update link status */ g_eLinkStatus = eLinkStatus; @@ -805,7 +806,7 @@ static void nfc_jni_llcp_linkStatus_callback(void *pContext, { ALOGE("Exception occured"); kill_client(nat); - } + } } } @@ -906,37 +907,29 @@ static void nfc_jni_Discovery_notification_callback(void *pContext, phLibNfc_RemoteDevList_t *psRemoteDevList, uint8_t uNofRemoteDev, NFCSTATUS status) { - JNIEnv *e; NFCSTATUS ret; - jclass tag_cls = NULL; - jobject target_array; - jobject tag; - jmethodID ctor; - jfieldID f; const char * typeName; - jbyteArray tagUid; - jbyteArray generalBytes = NULL; - struct nfc_jni_native_data *nat; struct timespec ts; phNfc_sData_t data; int i; int target_index = 0; // Target that will be reported (if multiple can be >0) - nat = (struct nfc_jni_native_data *)pContext; - + struct nfc_jni_native_data* nat = (struct nfc_jni_native_data *)pContext; + + JNIEnv *e; nat->vm->GetEnv( (void **)&e, nat->env_version); - + if(status == NFCSTATUS_DESELECTED) { - LOG_CALLBACK("nfc_jni_Discovery_notification_callback: Target deselected", status); - + LOG_CALLBACK("nfc_jni_Discovery_notification_callback: Target deselected", status); + /* Notify manager that a target was deselected */ e->CallVoidMethod(nat->manager, cached_NfcManager_notifyTargetDeselected); if(e->ExceptionCheck()) { - ALOGE("Exception occured"); + ALOGE("Exception occurred"); kill_client(nat); - } + } } else { @@ -945,6 +938,8 @@ static void nfc_jni_Discovery_notification_callback(void *pContext, target_index = find_preferred_target(psRemoteDevList, uNofRemoteDev); + ScopedLocalRef<jobject> tag(e, NULL); + /* Reset device connected flag */ device_connected_flag = 1; phLibNfc_sRemoteDevInformation_t *remDevInfo = psRemoteDevList[target_index].psRemoteDevInfo; @@ -952,61 +947,59 @@ static void nfc_jni_Discovery_notification_callback(void *pContext, if((remDevInfo->RemDevType == phNfc_eNfcIP1_Initiator) || (remDevInfo->RemDevType == phNfc_eNfcIP1_Target)) { - - tag_cls = e->GetObjectClass(nat->cached_P2pDevice); + ScopedLocalRef<jclass> tag_cls(e, e->GetObjectClass(nat->cached_P2pDevice)); if(e->ExceptionCheck()) { - ALOGE("Get Object Class Error"); + ALOGE("Get Object Class Error"); kill_client(nat); return; - } - + } + /* New target instance */ - ctor = e->GetMethodID(tag_cls, "<init>", "()V"); - tag = e->NewObject(tag_cls, ctor); - + jmethodID ctor = e->GetMethodID(tag_cls.get(), "<init>", "()V"); + tag.reset(e->NewObject(tag_cls.get(), ctor)); + /* Set P2P Target mode */ - f = e->GetFieldID(tag_cls, "mMode", "I"); - + jfieldID f = e->GetFieldID(tag_cls.get(), "mMode", "I"); + if(remDevInfo->RemDevType == phNfc_eNfcIP1_Initiator) { ALOGD("Discovered P2P Initiator"); - e->SetIntField(tag, f, (jint)MODE_P2P_INITIATOR); + e->SetIntField(tag.get(), f, (jint)MODE_P2P_INITIATOR); } else - { + { ALOGD("Discovered P2P Target"); - e->SetIntField(tag, f, (jint)MODE_P2P_TARGET); + e->SetIntField(tag.get(), f, (jint)MODE_P2P_TARGET); } - + if(remDevInfo->RemDevType == phNfc_eNfcIP1_Initiator) { /* Set General Bytes */ - f = e->GetFieldID(tag_cls, "mGeneralBytes", "[B"); - + f = e->GetFieldID(tag_cls.get(), "mGeneralBytes", "[B"); + TRACE("General Bytes length ="); for(i=0;i<remDevInfo->RemoteDevInfo.NfcIP_Info.ATRInfo_Length;i++) { ALOGD("%02x ", remDevInfo->RemoteDevInfo.NfcIP_Info.ATRInfo[i]); } - generalBytes = e->NewByteArray(remDevInfo->RemoteDevInfo.NfcIP_Info.ATRInfo_Length); - - e->SetByteArrayRegion(generalBytes, 0, + ScopedLocalRef<jbyteArray> generalBytes(e, e->NewByteArray(remDevInfo->RemoteDevInfo.NfcIP_Info.ATRInfo_Length)); + + e->SetByteArrayRegion(generalBytes.get(), 0, remDevInfo->RemoteDevInfo.NfcIP_Info.ATRInfo_Length, (jbyte *)remDevInfo->RemoteDevInfo.NfcIP_Info.ATRInfo); - - e->SetObjectField(tag, f, generalBytes); + e->SetObjectField(tag.get(), f, generalBytes.get()); } /* Set tag handle */ - f = e->GetFieldID(tag_cls, "mHandle", "I"); - e->SetIntField(tag, f,(jint)remDevHandle); + f = e->GetFieldID(tag_cls.get(), "mHandle", "I"); + e->SetIntField(tag.get(), f,(jint)remDevHandle); TRACE("Target handle = 0x%08x",remDevHandle); } else { - tag_cls = e->GetObjectClass(nat->cached_NfcTag); + ScopedLocalRef<jclass> tag_cls(e, e->GetObjectClass(nat->cached_NfcTag)); if(e->ExceptionCheck()) { kill_client(nat); @@ -1014,8 +1007,8 @@ static void nfc_jni_Discovery_notification_callback(void *pContext, } /* New tag instance */ - ctor = e->GetMethodID(tag_cls, "<init>", "()V"); - tag = e->NewObject(tag_cls, ctor); + jmethodID ctor = e->GetMethodID(tag_cls.get(), "<init>", "()V"); + tag.reset(e->NewObject(tag_cls.get(), ctor)); bool multi_protocol = false; @@ -1026,74 +1019,73 @@ static void nfc_jni_Discovery_notification_callback(void *pContext, } /* Set tag UID */ - f = e->GetFieldID(tag_cls, "mUid", "[B"); + jfieldID f = e->GetFieldID(tag_cls.get(), "mUid", "[B"); data = get_target_uid(remDevInfo); - tagUid = e->NewByteArray(data.length); + ScopedLocalRef<jbyteArray> tagUid(e, e->NewByteArray(data.length)); if(data.length > 0) { - e->SetByteArrayRegion(tagUid, 0, data.length, (jbyte *)data.buffer); + e->SetByteArrayRegion(tagUid.get(), 0, data.length, (jbyte *)data.buffer); } - e->SetObjectField(tag, f, tagUid); + e->SetObjectField(tag.get(), f, tagUid.get()); /* Generate technology list */ - jintArray techList; - jintArray handleList; - jintArray typeList; + ScopedLocalRef<jintArray> techList(e, NULL); + ScopedLocalRef<jintArray> handleList(e, NULL); + ScopedLocalRef<jintArray> typeList(e, NULL); nfc_jni_get_technology_tree(e, psRemoteDevList, multi_protocol ? uNofRemoteDev : 1, &techList, &handleList, &typeList); /* Push the technology list into the java object */ - f = e->GetFieldID(tag_cls, "mTechList", "[I"); - e->SetObjectField(tag, f, techList); + f = e->GetFieldID(tag_cls.get(), "mTechList", "[I"); + e->SetObjectField(tag.get(), f, techList.get()); - f = e->GetFieldID(tag_cls, "mTechHandles", "[I"); - e->SetObjectField(tag, f, handleList); + f = e->GetFieldID(tag_cls.get(), "mTechHandles", "[I"); + e->SetObjectField(tag.get(), f, handleList.get()); - f = e->GetFieldID(tag_cls, "mTechLibNfcTypes", "[I"); - e->SetObjectField(tag, f, typeList); + f = e->GetFieldID(tag_cls.get(), "mTechLibNfcTypes", "[I"); + e->SetObjectField(tag.get(), f, typeList.get()); - f = e->GetFieldID(tag_cls, "mConnectedTechIndex", "I"); - e->SetIntField(tag, f,(jint)-1); + f = e->GetFieldID(tag_cls.get(), "mConnectedTechIndex", "I"); + e->SetIntField(tag.get(), f,(jint)-1); - f = e->GetFieldID(tag_cls, "mConnectedHandle", "I"); - e->SetIntField(tag, f,(jint)-1); + f = e->GetFieldID(tag_cls.get(), "mConnectedHandle", "I"); + e->SetIntField(tag.get(), f,(jint)-1); } storedHandle = remDevHandle; if (nat->tag != NULL) { e->DeleteGlobalRef(nat->tag); } - nat->tag = e->NewGlobalRef(tag); + nat->tag = e->NewGlobalRef(tag.get()); - /* Notify the service */ + /* Notify the service */ TRACE("Notify Nfc Service"); if((remDevInfo->RemDevType == phNfc_eNfcIP1_Initiator) || (remDevInfo->RemDevType == phNfc_eNfcIP1_Target)) { - /* Store the hanlde of the P2P device */ + /* Store the handle of the P2P device */ hLlcpHandle = remDevHandle; - + /* Notify manager that new a P2P device was found */ - e->CallVoidMethod(nat->manager, cached_NfcManager_notifyLlcpLinkActivation, tag); + e->CallVoidMethod(nat->manager, cached_NfcManager_notifyLlcpLinkActivation, tag.get()); if(e->ExceptionCheck()) { - ALOGE("Exception occured"); + ALOGE("Exception occurred"); kill_client(nat); - } + } } else { /* Notify manager that new a tag was found */ - e->CallVoidMethod(nat->manager, cached_NfcManager_notifyNdefMessageListeners, tag); + e->CallVoidMethod(nat->manager, cached_NfcManager_notifyNdefMessageListeners, tag.get()); if(e->ExceptionCheck()) { - ALOGE("Exception occured"); + ALOGE("Exception occurred"); kill_client(nat); - } + } } - e->DeleteLocalRef(tag); - } + } } static void nfc_jni_init_callback(void *pContext, NFCSTATUS status) @@ -1379,7 +1371,7 @@ static void nfc_jni_stop_discovery_locked(struct nfc_jni_native_data *nat) discovery_cfg.NfcIP_Mode = phNfc_eDefaultP2PMode; discovery_cfg.NfcIP_Target_Mode = 0; discovery_cfg.NfcIP_Tgt_Disable = TRUE; - + /* Start Polling loop */ TRACE("****** Stop NFC Discovery ******"); REENTRANCE_LOCK(); @@ -1420,13 +1412,13 @@ static void com_android_nfc_NfcManager_disableDiscovery(JNIEnv *e, jobject o) /* Retrieve native structure address */ nat = nfc_jni_get_nat(e, o); - + nfc_jni_stop_discovery_locked(nat); CONCURRENCY_UNLOCK(); } - + static void com_android_nfc_NfcManager_enableDiscovery(JNIEnv *e, jobject o) { NFCSTATUS ret; struct nfc_jni_native_data *nat; @@ -1624,37 +1616,37 @@ static jboolean com_android_nfc_NfcManager_init_native_struc(JNIEnv *e, jobject jobject obj; jfieldID f; - TRACE("****** Init Native Structure ******"); + TRACE("****** Init Native Structure ******"); /* Initialize native structure */ nat = (nfc_jni_native_data*)malloc(sizeof(struct nfc_jni_native_data)); if(nat == NULL) { ALOGD("malloc of nfc_jni_native_data failed"); - return FALSE; + return FALSE; } memset(nat, 0, sizeof(*nat)); e->GetJavaVM(&(nat->vm)); nat->env_version = e->GetVersion(); nat->manager = e->NewGlobalRef(o); - + cls = e->GetObjectClass(o); f = e->GetFieldID(cls, "mNative", "I"); e->SetIntField(o, f, (jint)nat); - + /* Initialize native cached references */ cached_NfcManager_notifyNdefMessageListeners = e->GetMethodID(cls, "notifyNdefMessageListeners","(Lcom/android/nfc/dhimpl/NativeNfcTag;)V"); cached_NfcManager_notifyTransactionListeners = e->GetMethodID(cls, "notifyTransactionListeners", "([B)V"); - + cached_NfcManager_notifyLlcpLinkActivation = e->GetMethodID(cls, "notifyLlcpLinkActivation","(Lcom/android/nfc/dhimpl/NativeP2pDevice;)V"); - + cached_NfcManager_notifyLlcpLinkDeactivated = e->GetMethodID(cls, "notifyLlcpLinkDeactivated","(Lcom/android/nfc/dhimpl/NativeP2pDevice;)V"); - + cached_NfcManager_notifyTargetDeselected = e->GetMethodID(cls, "notifyTargetDeselected","()V"); @@ -1678,11 +1670,11 @@ static jboolean com_android_nfc_NfcManager_init_native_struc(JNIEnv *e, jobject ALOGD("Native Structure initialization failed"); return FALSE; } - + if(nfc_jni_cache_object(e,"com/android/nfc/dhimpl/NativeP2pDevice",&(nat->cached_P2pDevice)) == -1) { ALOGD("Native Structure initialization failed"); - return FALSE; + return FALSE; } TRACE("****** Init Native Structure OK ******"); return TRUE; @@ -1698,7 +1690,7 @@ static jboolean com_android_nfc_NfcManager_initialize(JNIEnv *e, jobject o) char value[PROPERTY_VALUE_MAX]; #endif jboolean result; - + CONCURRENCY_LOCK(); #ifdef TNFC_EMULATOR_ONLY @@ -1830,12 +1822,11 @@ static jboolean com_android_nfc_NfcManager_deinitialize(JNIEnv *e, jobject o) /* Secure Element methods */ static jintArray com_android_nfc_NfcManager_doGetSecureElementList(JNIEnv *e, jobject o) { NFCSTATUS ret; - jintArray list= NULL; phLibNfc_SE_List_t se_list[PHLIBNFC_MAXNO_OF_SE]; uint8_t i, se_count = PHLIBNFC_MAXNO_OF_SE; TRACE("****** Get Secure Element List ******"); - + TRACE("phLibNfc_SE_GetSecureElementList()"); REENTRANCE_LOCK(); ret = phLibNfc_SE_GetSecureElementList(se_list, &se_count); @@ -1843,13 +1834,13 @@ static jintArray com_android_nfc_NfcManager_doGetSecureElementList(JNIEnv *e, jo if (ret != NFCSTATUS_SUCCESS) { ALOGE("phLibNfc_SE_GetSecureElementList() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); - return list; + return NULL; } TRACE("phLibNfc_SE_GetSecureElementList() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); TRACE("Nb SE: %d", se_count); - list =e->NewIntArray(se_count); + jintArray result = e->NewIntArray(se_count); for (i = 0; i < se_count; i++) { if (se_list[i].eSE_Type == phLibNfc_SE_Type_SmartMX) { ALOGD("phLibNfc_SE_GetSecureElementList(): SMX detected"); @@ -1858,12 +1849,10 @@ static jintArray com_android_nfc_NfcManager_doGetSecureElementList(JNIEnv *e, jo ALOGD("phLibNfc_SE_GetSecureElementList(): UICC detected"); ALOGD("SE ID #%d: 0x%08x", i, se_list[i].hSecureElement); } - e->SetIntArrayRegion(list, i, 1, (jint*)&se_list[i].hSecureElement); + e->SetIntArrayRegion(result, i, 1, (jint*)&se_list[i].hSecureElement); } - e->DeleteLocalRef(list); - - return list; + return result; } static void com_android_nfc_NfcManager_doSelectSecureElement(JNIEnv *e, jobject o) { @@ -1929,7 +1918,7 @@ static void com_android_nfc_NfcManager_doDeselectSecureElement(JNIEnv *e, jobjec ret = phLibNfc_SE_SetMode(nat->seId, phLibNfc_SE_ActModeDefault, nfc_jni_se_set_mode_callback, (void *)&cb_data); REENTRANCE_UNLOCK(); - + TRACE("phLibNfc_SE_SetMode returned 0x%02x", ret); if (ret != NFCSTATUS_PENDING) { ALOGE("phLibNfc_SE_SetMode() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); @@ -2031,7 +2020,7 @@ static jboolean com_android_nfc_NfcManager_doActivateLlcp(JNIEnv *e, jobject o) else { ALOGE("phLibNfc_Llcp_Activate() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); - return JNI_FALSE; + return JNI_FALSE; } } @@ -2049,10 +2038,10 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpConnectionlessSocket(JNIEn phLibNfc_Llcp_sLinkParameters_t sParams; jclass clsNativeConnectionlessSocket; jfieldID f; - + /* Retrieve native structure address */ - nat = nfc_jni_get_nat(e, o); - + nat = nfc_jni_get_nat(e, o); + /* Allocate Working buffer length */ phLibNfc_Llcp_GetLocalInfo(hLlcpHandle, &sParams); sWorkingBuffer.length = sParams.miu + 1; // extra byte for SAP @@ -2068,7 +2057,7 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpConnectionlessSocket(JNIEn nfc_jni_llcp_transport_socket_err_callback, (void*)nat); REENTRANCE_UNLOCK(); - + if(ret != NFCSTATUS_SUCCESS) { lastErrorStatus = ret; @@ -2085,7 +2074,7 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpConnectionlessSocket(JNIEn serviceName.buffer = (uint8_t*)e->GetStringUTFChars(sn, NULL); serviceName.length = (uint32_t)e->GetStringUTFLength(sn); } - + /* Bind socket */ TRACE("phLibNfc_Llcp_Bind(hSocket=0x%08x, nSap=0x%02x)", hLlcpSocket, nSap); REENTRANCE_LOCK(); @@ -2097,41 +2086,41 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpConnectionlessSocket(JNIEn ALOGE("phLibNfc_Llcp_Bind() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); /* Close socket created */ REENTRANCE_LOCK(); - ret = phLibNfc_Llcp_Close(hLlcpSocket); + ret = phLibNfc_Llcp_Close(hLlcpSocket); REENTRANCE_UNLOCK(); goto error; } TRACE("phLibNfc_Llcp_Bind() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); - - + + /* Create new NativeLlcpConnectionlessSocket object */ if(nfc_jni_cache_object(e,"com/android/nfc/dhimpl/NativeLlcpConnectionlessSocket",&(connectionlessSocket)) == -1) { goto error; - } - + } + /* Get NativeConnectionless class object */ clsNativeConnectionlessSocket = e->GetObjectClass(connectionlessSocket); if(e->ExceptionCheck()) { goto error; } - + /* Set socket handle */ f = e->GetFieldID(clsNativeConnectionlessSocket, "mHandle", "I"); e->SetIntField(connectionlessSocket, f,(jint)hLlcpSocket); - TRACE("Connectionless socket Handle = %02x\n",hLlcpSocket); - + TRACE("Connectionless socket Handle = %02x\n",hLlcpSocket); + /* Set the miu link of the connectionless socket */ f = e->GetFieldID(clsNativeConnectionlessSocket, "mLinkMiu", "I"); e->SetIntField(connectionlessSocket, f,(jint)PHFRINFC_LLCP_MIU_DEFAULT); - TRACE("Connectionless socket Link MIU = %d\n",PHFRINFC_LLCP_MIU_DEFAULT); - + TRACE("Connectionless socket Link MIU = %d\n",PHFRINFC_LLCP_MIU_DEFAULT); + /* Set socket SAP */ f = e->GetFieldID(clsNativeConnectionlessSocket, "mSap", "I"); e->SetIntField(connectionlessSocket, f,(jint)nSap); - TRACE("Connectionless socket SAP = %d\n",nSap); - + TRACE("Connectionless socket SAP = %d\n",nSap); + return connectionlessSocket; error: if (serviceName.buffer != NULL) { @@ -2155,20 +2144,20 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpServiceSocket(JNIEnv *e, j struct nfc_jni_native_data *nat; jobject serviceSocket = NULL; jclass clsNativeLlcpServiceSocket; - jfieldID f; - + jfieldID f; + /* Retrieve native structure address */ - nat = nfc_jni_get_nat(e, o); - + nat = nfc_jni_get_nat(e, o); + /* Set Connection Oriented socket options */ sOptions.miu = miu; - sOptions.rw = rw; - + sOptions.rw = rw; + /* Allocate Working buffer length */ sWorkingBuffer.length = (miu*rw)+ miu + linearBufferLength; sWorkingBuffer.buffer = (uint8_t*)malloc(sWorkingBuffer.length); - + /* Create socket */ TRACE("phLibNfc_Llcp_Socket(hRemoteDevice=0x%08x, eType=phFriNfc_LlcpTransport_eConnectionOriented, ...)", hLlcpHandle); REENTRANCE_LOCK(); @@ -2179,7 +2168,7 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpServiceSocket(JNIEnv *e, j nfc_jni_llcp_transport_socket_err_callback, (void*)nat); REENTRANCE_UNLOCK(); - + if(ret != NFCSTATUS_SUCCESS) { ALOGE("phLibNfc_Llcp_Socket() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); @@ -2207,7 +2196,7 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpServiceSocket(JNIEnv *e, j lastErrorStatus = ret; ALOGE("phLibNfc_Llcp_Bind() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); /* Close socket created */ - ret = phLibNfc_Llcp_Close(hLlcpSocket); + ret = phLibNfc_Llcp_Close(hLlcpSocket); goto error; } TRACE("phLibNfc_Llcp_Bind() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); @@ -2218,53 +2207,53 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpServiceSocket(JNIEnv *e, j nfc_jni_llcp_transport_listen_socket_callback, (void*)hLlcpSocket); REENTRANCE_UNLOCK(); - + if(ret != NFCSTATUS_SUCCESS) { ALOGE("phLibNfc_Llcp_Listen() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); lastErrorStatus = ret; /* Close created socket */ REENTRANCE_LOCK(); - ret = phLibNfc_Llcp_Close(hLlcpSocket); + ret = phLibNfc_Llcp_Close(hLlcpSocket); REENTRANCE_UNLOCK(); goto error; - } + } TRACE("phLibNfc_Llcp_Listen() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); - + /* Create new NativeLlcpServiceSocket object */ if(nfc_jni_cache_object(e,"com/android/nfc/dhimpl/NativeLlcpServiceSocket",&(serviceSocket)) == -1) { ALOGE("Llcp Socket object creation error"); goto error; - } - + } + /* Get NativeLlcpServiceSocket class object */ clsNativeLlcpServiceSocket = e->GetObjectClass(serviceSocket); if(e->ExceptionCheck()) { ALOGE("Llcp Socket get object class error"); goto error; - } - + } + /* Set socket handle */ f = e->GetFieldID(clsNativeLlcpServiceSocket, "mHandle", "I"); e->SetIntField(serviceSocket, f,(jint)hLlcpSocket); - TRACE("Service socket Handle = %02x\n",hLlcpSocket); - + TRACE("Service socket Handle = %02x\n",hLlcpSocket); + /* Set socket linear buffer length */ f = e->GetFieldID(clsNativeLlcpServiceSocket, "mLocalLinearBufferLength", "I"); e->SetIntField(serviceSocket, f,(jint)linearBufferLength); - TRACE("Service socket Linear buffer length = %02x\n",linearBufferLength); - + TRACE("Service socket Linear buffer length = %02x\n",linearBufferLength); + /* Set socket MIU */ f = e->GetFieldID(clsNativeLlcpServiceSocket, "mLocalMiu", "I"); e->SetIntField(serviceSocket, f,(jint)miu); - TRACE("Service socket MIU = %d\n",miu); - + TRACE("Service socket MIU = %d\n",miu); + /* Set socket RW */ f = e->GetFieldID(clsNativeLlcpServiceSocket, "mLocalRw", "I"); e->SetIntField(serviceSocket, f,(jint)rw); - TRACE("Service socket RW = %d\n",rw); + TRACE("Service socket RW = %d\n",rw); return serviceSocket; error: @@ -2284,14 +2273,14 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpSocket(JNIEnv *e, jobject struct nfc_jni_native_data *nat; jclass clsNativeLlcpSocket; jfieldID f; - + /* Retrieve native structure address */ - nat = nfc_jni_get_nat(e, o); - + nat = nfc_jni_get_nat(e, o); + /* Set Connection Oriented socket options */ sOptions.miu = miu; sOptions.rw = rw; - + /* Allocate Working buffer length */ sWorkingBuffer.length = (miu*rw)+ miu + linearBufferLength; sWorkingBuffer.buffer = (uint8_t*)malloc(sWorkingBuffer.length); @@ -2314,22 +2303,22 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpSocket(JNIEnv *e, jobject return NULL; } TRACE("phLibNfc_Llcp_Socket() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); - + /* Create new NativeLlcpSocket object */ if(nfc_jni_cache_object(e,"com/android/nfc/dhimpl/NativeLlcpSocket",&(clientSocket)) == -1) { - ALOGE("Llcp socket object creation error"); - return NULL; - } - + ALOGE("Llcp socket object creation error"); + return NULL; + } + /* Get NativeConnectionless class object */ clsNativeLlcpSocket = e->GetObjectClass(clientSocket); if(e->ExceptionCheck()) { - ALOGE("Get class object error"); - return NULL; + ALOGE("Get class object error"); + return NULL; } - + /* Test if an SAP number is present */ if(nSap != 0) { @@ -2344,41 +2333,41 @@ static jobject com_android_nfc_NfcManager_doCreateLlcpSocket(JNIEnv *e, jobject ALOGE("phLibNfc_Llcp_Bind() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); /* Close socket created */ REENTRANCE_LOCK(); - ret = phLibNfc_Llcp_Close(hLlcpSocket); + ret = phLibNfc_Llcp_Close(hLlcpSocket); REENTRANCE_UNLOCK(); return NULL; } TRACE("phLibNfc_Llcp_Bind() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret)); - + /* Set socket SAP */ f = e->GetFieldID(clsNativeLlcpSocket, "mSap", "I"); e->SetIntField(clientSocket, f,(jint)nSap); - TRACE("socket SAP = %d\n",nSap); - } - + TRACE("socket SAP = %d\n",nSap); + } + /* Set socket handle */ f = e->GetFieldID(clsNativeLlcpSocket, "mHandle", "I"); e->SetIntField(clientSocket, f,(jint)hLlcpSocket); - TRACE("socket Handle = %02x\n",hLlcpSocket); - + TRACE("socket Handle = %02x\n",hLlcpSocket); + /* Set socket MIU */ f = e->GetFieldID(clsNativeLlcpSocket, "mLocalMiu", "I"); e->SetIntField(clientSocket, f,(jint)miu); - TRACE("socket MIU = %d\n",miu); - + TRACE("socket MIU = %d\n",miu); + /* Set socket RW */ f = e->GetFieldID(clsNativeLlcpSocket, "mLocalRw", "I"); e->SetIntField(clientSocket, f,(jint)rw); - TRACE("socket RW = %d\n",rw); - - + TRACE("socket RW = %d\n",rw); + + return clientSocket; } static jint com_android_nfc_NfcManager_doGetLastError(JNIEnv *e, jobject o) { TRACE("Last Error Status = 0x%02x",lastErrorStatus); - + if(lastErrorStatus == NFCSTATUS_BUFFER_TOO_SMALL) { return ERROR_BUFFER_TOO_SMALL; @@ -2544,37 +2533,37 @@ static JNINativeMethod gMethods[] = {"doInitialize", "()Z", (void *)com_android_nfc_NfcManager_initialize}, - + {"doDeinitialize", "()Z", (void *)com_android_nfc_NfcManager_deinitialize}, - + {"enableDiscovery", "()V", (void *)com_android_nfc_NfcManager_enableDiscovery}, {"doGetSecureElementList", "()[I", (void *)com_android_nfc_NfcManager_doGetSecureElementList}, - + {"doSelectSecureElement", "()V", (void *)com_android_nfc_NfcManager_doSelectSecureElement}, - + {"doDeselectSecureElement", "()V", (void *)com_android_nfc_NfcManager_doDeselectSecureElement}, - + {"doCheckLlcp", "()Z", (void *)com_android_nfc_NfcManager_doCheckLlcp}, - + {"doActivateLlcp", "()Z", (void *)com_android_nfc_NfcManager_doActivateLlcp}, - + {"doCreateLlcpConnectionlessSocket", "(ILjava/lang/String;)Lcom/android/nfc/dhimpl/NativeLlcpConnectionlessSocket;", (void *)com_android_nfc_NfcManager_doCreateLlcpConnectionlessSocket}, - + {"doCreateLlcpServiceSocket", "(ILjava/lang/String;III)Lcom/android/nfc/dhimpl/NativeLlcpServiceSocket;", (void *)com_android_nfc_NfcManager_doCreateLlcpServiceSocket}, - + {"doCreateLlcpSocket", "(IIII)Lcom/android/nfc/dhimpl/NativeLlcpSocket;", (void *)com_android_nfc_NfcManager_doCreateLlcpSocket}, - + {"doGetLastError", "()I", (void *)com_android_nfc_NfcManager_doGetLastError}, @@ -2601,9 +2590,9 @@ static JNINativeMethod gMethods[] = {"doDump", "()Ljava/lang/String;", (void *)com_android_nfc_NfcManager_doDump}, -}; - - +}; + + int register_com_android_nfc_NativeNfcManager(JNIEnv *e) { nfc_jni_native_monitor_t *nfc_jni_native_monitor; diff --git a/nxp/jni/com_android_nfc_NativeNfcSecureElement.cpp b/nxp/jni/com_android_nfc_NativeNfcSecureElement.cpp index bb1bb2a5..79887a0e 100755 --- a/nxp/jni/com_android_nfc_NativeNfcSecureElement.cpp +++ b/nxp/jni/com_android_nfc_NativeNfcSecureElement.cpp @@ -152,12 +152,12 @@ static void com_android_nfc_jni_open_secure_element_notification_callback(void * TRACE("Secure Element Handle: 0x%08x", secureElementHandle); /* Set type name */ - jintArray techList; + ScopedLocalRef<jintArray> techList(e, NULL); nfc_jni_get_technology_tree(e, psRemoteDevList,uNofRemoteDev, &techList, NULL, NULL); // TODO: Should use the "connected" technology, for now use the first - if ((techList != NULL) && e->GetArrayLength(techList) > 0) { - e->GetIntArrayRegion(techList, 0, 1, &SecureElementTech); + if ((techList.get() != NULL) && e->GetArrayLength(techList.get()) > 0) { + e->GetIntArrayRegion(techList.get(), 0, 1, &SecureElementTech); TRACE("Store Secure Element Info\n"); SecureElementInfo = psRemoteDevList->psRemoteDevInfo; @@ -167,10 +167,6 @@ static void com_android_nfc_jni_open_secure_element_notification_callback(void * ALOGE("Discovered secure element, but could not resolve tech"); status = NFCSTATUS_FAILED; } - - // This thread may not return to the virtual machine for a long time - // so make sure to delete the local refernce to the tech list. - e->DeleteLocalRef(techList); } clean_and_return: @@ -730,16 +726,12 @@ static jintArray com_android_nfc_NativeNfcSecureElement_doGetTechList(JNIEnv *e, jintArray techList; TRACE("Get Secure element Type function "); - if(handle == secureElementHandle) - { - techList = e->NewIntArray(1); - e->SetIntArrayRegion(techList, 0, 1, &SecureElementTech); - return techList; - } - else - { + if (handle != secureElementHandle) { return NULL; } + jintArray result = e->NewIntArray(1); + e->SetIntArrayRegion(result, 0, 1, &SecureElementTech); + return result; } diff --git a/nxp/jni/com_android_nfc_NativeNfcTag.cpp b/nxp/jni/com_android_nfc_NativeNfcTag.cpp index a621d2ac..1f469e4f 100644 --- a/nxp/jni/com_android_nfc_NativeNfcTag.cpp +++ b/nxp/jni/com_android_nfc_NativeNfcTag.cpp @@ -16,6 +16,8 @@ #include <semaphore.h> #include <errno.h> +#include <ScopedLocalRef.h> +#include <ScopedPrimitiveArray.h> #include "com_android_nfc.h" #include "phNfcHalTypes.h" @@ -133,7 +135,7 @@ static void nfc_jni_transceive_callback(void *pContext, { struct nfc_jni_callback_data * pCallbackData = (struct nfc_jni_callback_data *) pContext; LOG_CALLBACK("nfc_jni_transceive_callback", status); - + nfc_jni_transceive_buffer = pResBuffer; /* Report the callback status and wake up the caller */ @@ -196,8 +198,8 @@ static jbyteArray com_android_nfc_NativeNfcTag_doRead(JNIEnv *e, TRACE("phLibNfc_Ndef_Read()"); REENTRANCE_LOCK(); status = phLibNfc_Ndef_Read(handle, &nfc_jni_ndef_rw, - phLibNfc_Ndef_EBegin, - nfc_jni_tag_rw_callback, + phLibNfc_Ndef_EBegin, + nfc_jni_tag_rw_callback, (void *)&cb_data); REENTRANCE_UNLOCK(); if(status != NFCSTATUS_PENDING) @@ -206,7 +208,7 @@ static jbyteArray com_android_nfc_NativeNfcTag_doRead(JNIEnv *e, goto clean_and_return; } TRACE("phLibNfc_Ndef_Read() returned 0x%04x[%s]", status, nfc_jni_get_status_name(status)); - + /* Wait for callback response */ if(sem_wait(&cb_data.sem)) { @@ -229,7 +231,7 @@ clean_and_return: return buf; } - + static jboolean com_android_nfc_NativeNfcTag_doWrite(JNIEnv *e, jobject o, jbyteArray buf) @@ -292,26 +294,25 @@ clean_and_return: static void set_target_pollBytes(JNIEnv *e, jobject tag, phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo) { - jclass tag_cls = e->GetObjectClass(tag); - jfieldID f = e->GetFieldID(tag_cls, "mTechPollBytes", "[[B"); + ScopedLocalRef<jclass> tag_cls(e, e->GetObjectClass(tag)); + jfieldID f = e->GetFieldID(tag_cls.get(), "mTechPollBytes", "[[B"); - jobjectArray existingPollBytes = (jobjectArray) e->GetObjectField(tag, f); - - if (existingPollBytes != NULL) { + ScopedLocalRef<jobjectArray> existingPollBytes(e, (jobjectArray) e->GetObjectField(tag, f)); + if (existingPollBytes.get() != NULL) { return; } - jfieldID techListField = e->GetFieldID(tag_cls, "mTechList", "[I"); - jintArray techList = (jintArray) e->GetObjectField(tag, techListField); - jint *techId = e->GetIntArrayElements(techList, 0); - int techListLength = e->GetArrayLength(techList); + jfieldID techListField = e->GetFieldID(tag_cls.get(), "mTechList", "[I"); + ScopedLocalRef<jintArray> techList(e, (jintArray) e->GetObjectField(tag, techListField)); + ScopedIntArrayRO techIds(e, techList.get()); + int techListLength = techIds.size(); - jbyteArray pollBytes = e->NewByteArray(0); - jobjectArray techPollBytes = e->NewObjectArray(techListLength, - e->GetObjectClass(pollBytes), 0); + ScopedLocalRef<jbyteArray> pollBytes(e, e->NewByteArray(0)); + ScopedLocalRef<jclass> byteArrayClass(e, e->GetObjectClass(pollBytes.get())); + ScopedLocalRef<jobjectArray> techPollBytes(e, e->NewObjectArray(techListLength, byteArrayClass.get(), NULL)); for (int tech = 0; tech < techListLength; tech++) { - switch(techId[tech]) + switch(techIds[tech]) { /* ISO14443-3A: ATQA/SENS_RES */ case TARGET_TYPE_ISO14443_3A: @@ -320,56 +321,52 @@ static void set_target_pollBytes(JNIEnv *e, jobject tag, // at {0x00, 0x0C} in the spec. So eJewel can safely be // translated to {0x00, 0x0C}. const static jbyte JewelAtqA[2] = {0x00, 0x0C}; - pollBytes = e->NewByteArray(2); - e->SetByteArrayRegion(pollBytes, 0, 2, (jbyte*) JewelAtqA); - } - else { - pollBytes = e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA)); - e->SetByteArrayRegion(pollBytes, 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA), + pollBytes.reset(e->NewByteArray(2)); + e->SetByteArrayRegion(pollBytes.get(), 0, 2, (jbyte*) JewelAtqA); + } else { + pollBytes.reset(e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA))); + e->SetByteArrayRegion(pollBytes.get(), 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA), (jbyte *)psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AtqA); } break; /* ISO14443-3B: Application data (4 bytes) and Protocol Info (3 bytes) from ATQB/SENSB_RES */ case TARGET_TYPE_ISO14443_3B: - pollBytes = e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData) - + sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.ProtInfo)); - e->SetByteArrayRegion(pollBytes, 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData), + pollBytes.reset(e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData) + + sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.ProtInfo))); + e->SetByteArrayRegion(pollBytes.get(), 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData), (jbyte *)psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData); - e->SetByteArrayRegion(pollBytes, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData), + e->SetByteArrayRegion(pollBytes.get(), sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.AppData), sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.ProtInfo), (jbyte *)psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.AtqB.AtqResInfo.ProtInfo); break; /* JIS_X_6319_4: PAD0 (2 byte), PAD1 (2 byte), MRTI(2 byte), PAD2 (1 byte), RC (2 byte) */ case TARGET_TYPE_FELICA: - pollBytes = e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm) - + sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode)); - e->SetByteArrayRegion(pollBytes, 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm), + pollBytes.reset(e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm) + + sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode))); + e->SetByteArrayRegion(pollBytes.get(), 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm), (jbyte *)psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm); - e->SetByteArrayRegion(pollBytes, sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm), + e->SetByteArrayRegion(pollBytes.get(), sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.PMm), sizeof(psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode), (jbyte *)psRemoteDevInfo->RemoteDevInfo.Felica_Info.SystemCode); break; /* ISO15693: response flags (1 byte), DSFID (1 byte) */ case TARGET_TYPE_ISO15693: - pollBytes = e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags) - + sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid)); - e->SetByteArrayRegion(pollBytes, 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), + pollBytes.reset(e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags) + + sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid))); + e->SetByteArrayRegion(pollBytes.get(), 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), (jbyte *)&psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags); - e->SetByteArrayRegion(pollBytes, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), + e->SetByteArrayRegion(pollBytes.get(), sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid), (jbyte *)&psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid); break; default: - pollBytes = e->NewByteArray(0); + pollBytes.reset(e->NewByteArray(0)); break; } - e->SetObjectArrayElement(techPollBytes, tech, pollBytes); + e->SetObjectArrayElement(techPollBytes.get(), tech, pollBytes.get()); } - e->SetObjectField(tag, f, techPollBytes); - - e->ReleaseIntArrayElements(techList, techId, 0); - + e->SetObjectField(tag, f, techPollBytes.get()); } /* @@ -378,31 +375,30 @@ static void set_target_pollBytes(JNIEnv *e, jobject tag, static void set_target_activationBytes(JNIEnv *e, jobject tag, phLibNfc_sRemoteDevInformation_t *psRemoteDevInfo) { - jclass tag_cls = e->GetObjectClass(tag); - - jfieldID f = e->GetFieldID(tag_cls, "mTechActBytes", "[[B"); - jobjectArray existingActBytes = (jobjectArray) e->GetObjectField(tag, f); + ScopedLocalRef<jclass> tag_cls(e, e->GetObjectClass(tag)); - if (existingActBytes != NULL) { + jfieldID f = e->GetFieldID(tag_cls.get(), "mTechActBytes", "[[B"); + ScopedLocalRef<jobjectArray> existingActBytes(e, (jobjectArray) e->GetObjectField(tag, f)); + if (existingActBytes.get() != NULL) { return; } - jfieldID techListField = e->GetFieldID(tag_cls, "mTechList", "[I"); - jintArray techList = (jintArray) e->GetObjectField(tag, techListField); - int techListLength = e->GetArrayLength(techList); - jint *techId = e->GetIntArrayElements(techList, 0); + jfieldID techListField = e->GetFieldID(tag_cls.get(), "mTechList", "[I"); + ScopedLocalRef<jintArray> techList(e, (jintArray) e->GetObjectField(tag, techListField)); + ScopedIntArrayRO techIds(e, techList.get()); + int techListLength = techIds.size(); - jbyteArray actBytes = e->NewByteArray(0); - jobjectArray techActBytes = e->NewObjectArray(techListLength, - e->GetObjectClass(actBytes), 0); + ScopedLocalRef<jbyteArray> actBytes(e, e->NewByteArray(0)); + ScopedLocalRef<jclass> byteArrayClass(e, e->GetObjectClass(actBytes.get())); + ScopedLocalRef<jobjectArray> techActBytes(e, e->NewObjectArray(techListLength, byteArrayClass.get(), 0)); for (int tech = 0; tech < techListLength; tech++) { - switch(techId[tech]) { + switch(techIds[tech]) { /* ISO14443-3A: SAK/SEL_RES */ case TARGET_TYPE_ISO14443_3A: - actBytes = e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak)); - e->SetByteArrayRegion(actBytes, 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak), + actBytes.reset(e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak))); + e->SetByteArrayRegion(actBytes.get(), 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak), (jbyte *)&psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.Sak); break; /* ISO14443-3A & ISO14443-4: SAK/SEL_RES, historical bytes from ATS */ @@ -410,45 +406,40 @@ static void set_target_activationBytes(JNIEnv *e, jobject tag, case TARGET_TYPE_ISO14443_4: // Determine whether -A or -B if (psRemoteDevInfo->RemDevType == phNfc_eISO14443_B_PICC || - psRemoteDevInfo->RemDevType == phNfc_eISO14443_4B_PICC) { - actBytes = e->NewByteArray(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerRespLength); - e->SetByteArrayRegion(actBytes, 0, psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerRespLength, + psRemoteDevInfo->RemDevType == phNfc_eISO14443_4B_PICC) { + actBytes.reset(e->NewByteArray(psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerRespLength)); + e->SetByteArrayRegion(actBytes.get(), 0, psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerRespLength, (jbyte *)psRemoteDevInfo->RemoteDevInfo.Iso14443B_Info.HiLayerResp); - } - else if (psRemoteDevInfo->RemDevType == phNfc_eISO14443_A_PICC || - psRemoteDevInfo->RemDevType == phNfc_eISO14443_4A_PICC) { - actBytes = e->NewByteArray(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AppDataLength); - e->SetByteArrayRegion(actBytes, 0, + } else if (psRemoteDevInfo->RemDevType == phNfc_eISO14443_A_PICC || + psRemoteDevInfo->RemDevType == phNfc_eISO14443_4A_PICC) { + actBytes.reset(e->NewByteArray(psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AppDataLength)); + e->SetByteArrayRegion(actBytes.get(), 0, psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AppDataLength, (jbyte *)psRemoteDevInfo->RemoteDevInfo.Iso14443A_Info.AppData); } break; /* ISO15693: response flags (1 byte), DSFID (1 byte) */ case TARGET_TYPE_ISO15693: - actBytes = e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags) - + sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid)); - e->SetByteArrayRegion(actBytes, 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), + actBytes.reset(e->NewByteArray(sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags) + + sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid))); + e->SetByteArrayRegion(actBytes.get(), 0, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), (jbyte *)&psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags); - e->SetByteArrayRegion(actBytes, sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), + e->SetByteArrayRegion(actBytes.get(), sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Flags), sizeof(psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid), (jbyte *)&psRemoteDevInfo->RemoteDevInfo.Iso15693_Info.Dsfid); break; default: - actBytes = e->NewByteArray(0); + actBytes.reset(e->NewByteArray(0)); break; } - e->SetObjectArrayElement(techActBytes, tech, actBytes); + e->SetObjectArrayElement(techActBytes.get(), tech, actBytes.get()); } - e->SetObjectField(tag, f, techActBytes); - - e->ReleaseIntArrayElements(techList, techId, 0); + e->SetObjectField(tag, f, techActBytes.get()); } static jint com_android_nfc_NativeNfcTag_doConnect(JNIEnv *e, jobject o, phLibNfc_Handle handle) { - jclass cls; - jfieldID f; jint status; struct nfc_jni_callback_data cb_data; phLibNfc_sRemoteDevInformation_t* pRemDevInfo = NULL; @@ -504,8 +495,6 @@ clean_and_return: static jint com_android_nfc_NativeNfcTag_doHandleReconnect(JNIEnv *e, jobject o, phLibNfc_Handle handle) { - jclass cls; - jfieldID f; jint status; struct nfc_jni_callback_data cb_data; phLibNfc_sRemoteDevInformation_t* pRemDevInfo = NULL; @@ -578,8 +567,6 @@ static jint com_android_nfc_NativeNfcTag_doReconnect(JNIEnv *e, static jboolean com_android_nfc_NativeNfcTag_doDisconnect(JNIEnv *e, jobject o) { phLibNfc_Handle handle = 0; - jclass cls; - jfieldID f; NFCSTATUS status; jboolean result = JNI_FALSE; struct nfc_jni_callback_data cb_data; @@ -601,7 +588,7 @@ static jboolean com_android_nfc_NativeNfcTag_doDisconnect(JNIEnv *e, jobject o) /* Disconnect */ TRACE("Disconnecting from tag (%x)", handle); - + if (handle == -1) { // Was never connected to any tag, exit result = JNI_TRUE; @@ -636,7 +623,7 @@ static jboolean com_android_nfc_NativeNfcTag_doDisconnect(JNIEnv *e, jobject o) ALOGE("Failed to wait for semaphore (errno=0x%08x)", errno); goto clean_and_return; } - + /* Disconnect Status */ if(cb_data.status != NFCSTATUS_SUCCESS) { diff --git a/nxp/jni/com_android_nfc_NativeP2pDevice.cpp b/nxp/jni/com_android_nfc_NativeP2pDevice.cpp index fa460524..19f3ecc8 100644 --- a/nxp/jni/com_android_nfc_NativeP2pDevice.cpp +++ b/nxp/jni/com_android_nfc_NativeP2pDevice.cpp @@ -17,6 +17,7 @@ #include <semaphore.h> #include <errno.h> +#include <ScopedLocalRef.h> #include "com_android_nfc.h" @@ -122,7 +123,7 @@ static jboolean com_android_nfc_NativeP2pDevice_doConnect(JNIEnv *e, jobject o) jboolean result = JNI_FALSE; struct nfc_jni_callback_data cb_data; - jclass target_cls = NULL; + ScopedLocalRef<jclass> target_cls(e, NULL); jobject tag; jmethodID ctor; jfieldID f; @@ -164,9 +165,9 @@ static jboolean com_android_nfc_NativeP2pDevice_doConnect(JNIEnv *e, jobject o) } /* Set General Bytes */ - target_cls = e->GetObjectClass(o); + target_cls.reset(e->GetObjectClass(o)); - f = e->GetFieldID(target_cls, "mGeneralBytes", "[B"); + f = e->GetFieldID(target_cls.get(), "mGeneralBytes", "[B"); TRACE("General Bytes Length = %d", sGeneralBytes.length); TRACE("General Bytes ="); |