summaryrefslogtreecommitdiffstats
path: root/jni/com_android_nfc_NativeLlcpSocket.cpp
diff options
context:
space:
mode:
authorSylvain Fonteneau <sylvain.fonteneau@trusted-logic.com>2010-11-16 10:17:25 +0100
committerSylvain Fonteneau <sylvain.fonteneau@trusted-logic.com>2010-11-22 16:09:16 +0100
commitb78172c6c46957cc37786b690db9bc854b26b7f9 (patch)
treecfd76b459523672976bd3602f5b9da399a258b6a /jni/com_android_nfc_NativeLlcpSocket.cpp
parentff0bfe105e7cb39bd3b2e48a64da76521349fd73 (diff)
downloadpackages_apps_nfc-b78172c6c46957cc37786b690db9bc854b26b7f9.zip
packages_apps_nfc-b78172c6c46957cc37786b690db9bc854b26b7f9.tar.gz
packages_apps_nfc-b78172c6c46957cc37786b690db9bc854b26b7f9.tar.bz2
Use dynamic semaphores in JNI layer.
Change-Id: Iee394ace642302ee269e73203d3fc8427c69047e
Diffstat (limited to 'jni/com_android_nfc_NativeLlcpSocket.cpp')
-rw-r--r--jni/com_android_nfc_NativeLlcpSocket.cpp197
1 files changed, 116 insertions, 81 deletions
diff --git a/jni/com_android_nfc_NativeLlcpSocket.cpp b/jni/com_android_nfc_NativeLlcpSocket.cpp
index 5fb772f..c4df5c3 100644
--- a/jni/com_android_nfc_NativeLlcpSocket.cpp
+++ b/jni/com_android_nfc_NativeLlcpSocket.cpp
@@ -15,12 +15,10 @@
*/
#include <semaphore.h>
+#include <errno.h>
#include "com_android_nfc.h"
-static sem_t *nfc_jni_llcp_sem;
-static NFCSTATUS nfc_jni_cb_status = NFCSTATUS_FAILED;
-
namespace android {
/*
@@ -30,24 +28,20 @@ namespace android {
static void nfc_jni_disconnect_callback(void* pContext,
NFCSTATUS status)
{
- PHNFC_UNUSED_VARIABLE(pContext);
-
- LOG_CALLBACK("nfc_jni_llcp_disconnect_callback", status);
-
- nfc_jni_cb_status = status;
+ struct nfc_jni_callback_data * pCallbackData = (struct nfc_jni_callback_data *) pContext;
+ LOG_CALLBACK("nfc_jni_disconnect_callback", status);
- sem_post(nfc_jni_llcp_sem);
+ /* Report the callback status and wake up the caller */
+ pCallbackData->status = status;
+ sem_post(&pCallbackData->sem);
}
static void nfc_jni_connect_callback(void* pContext, uint8_t nErrCode, NFCSTATUS status)
{
- PHNFC_UNUSED_VARIABLE(pContext);
-
+ struct nfc_jni_callback_data * pCallbackData = (struct nfc_jni_callback_data *) pContext;
LOG_CALLBACK("nfc_jni_llcp_connect_callback", status);
- nfc_jni_cb_status = status;
-
if(status == NFCSTATUS_SUCCESS)
{
TRACE("Socket connected\n");
@@ -84,7 +78,9 @@ static void nfc_jni_connect_callback(void* pContext, uint8_t nErrCode, NFCSTATUS
}
}
- sem_post(nfc_jni_llcp_sem);
+ /* Report the callback status and wake up the caller */
+ pCallbackData->status = status;
+ sem_post(&pCallbackData->sem);
}
@@ -92,25 +88,22 @@ static void nfc_jni_connect_callback(void* pContext, uint8_t nErrCode, NFCSTATUS
static void nfc_jni_receive_callback(void* pContext, NFCSTATUS status)
{
- uint8_t i;
- PHNFC_UNUSED_VARIABLE(pContext);
-
+ struct nfc_jni_callback_data * pCallbackData = (struct nfc_jni_callback_data *) pContext;
LOG_CALLBACK("nfc_jni_llcp_receive_callback", status);
- nfc_jni_cb_status = status;
-
- sem_post(nfc_jni_llcp_sem);
+ /* Report the callback status and wake up the caller */
+ pCallbackData->status = status;
+ sem_post(&pCallbackData->sem);
}
static void nfc_jni_send_callback(void *pContext, NFCSTATUS status)
{
- PHNFC_UNUSED_VARIABLE(pContext);
-
+ struct nfc_jni_callback_data * pCallbackData = (struct nfc_jni_callback_data *) pContext;
LOG_CALLBACK("nfc_jni_llcp_send_callback", status);
- nfc_jni_cb_status = status;
-
- sem_post(nfc_jni_llcp_sem);
+ /* Report the callback status and wake up the caller */
+ pCallbackData->status = status;
+ sem_post(&pCallbackData->sem);
}
/*
@@ -121,50 +114,70 @@ static jboolean com_android_nfc_NativeLlcpSocket_doConnect(JNIEnv *e, jobject o,
NFCSTATUS ret;
struct timespec ts;
phLibNfc_Handle hLlcpSocket;
-
+ struct nfc_jni_callback_data cb_data;
+ jboolean result = JNI_FALSE;
+
/* Retrieve socket handle */
hLlcpSocket = nfc_jni_get_nfc_socket_handle(e,o);
+ /* Create the local semaphore */
+ if (!nfc_cb_data_init(&cb_data, NULL))
+ {
+ goto clean_and_return;
+ }
+
TRACE("phLibNfc_Llcp_Connect(%d)",nSap);
REENTRANCE_LOCK();
ret = phLibNfc_Llcp_Connect(hLlcpSocket,
nSap,
nfc_jni_connect_callback,
- (void*)hLlcpSocket);
+ (void*)&cb_data);
REENTRANCE_UNLOCK();
if(ret != NFCSTATUS_PENDING)
{
LOGE("phLibNfc_Llcp_Connect(%d) returned 0x%04x[%s]", nSap, ret, nfc_jni_get_status_name(ret));
- return FALSE;
+ goto clean_and_return;
}
TRACE("phLibNfc_Llcp_Connect(%d) returned 0x%04x[%s]", nSap, ret, nfc_jni_get_status_name(ret));
-
+
/* Wait for callback response */
- if(sem_wait(nfc_jni_llcp_sem) == -1)
- return FALSE;
-
- if(nfc_jni_cb_status == NFCSTATUS_SUCCESS)
+ if(sem_wait(&cb_data.sem))
{
- TRACE("LLCP Connect request OK");
- return TRUE;
+ LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ goto clean_and_return;
}
- else
+
+ if(cb_data.status != NFCSTATUS_SUCCESS)
{
- LOGD("LLCP Connect request KO");
- return FALSE;
+ LOGW("LLCP Connect request failed");
+ goto clean_and_return;
}
+
+ result = JNI_TRUE;
+
+clean_and_return:
+ nfc_cb_data_deinit(&cb_data);
+ return result;
}
static jboolean com_android_nfc_NativeLlcpSocket_doConnectBy(JNIEnv *e, jobject o, jstring sn)
{
NFCSTATUS ret;
struct timespec ts;
- phNfc_sData_t serviceName;
+ phNfc_sData_t serviceName;
phLibNfc_Handle hLlcpSocket;
-
+ struct nfc_jni_callback_data cb_data;
+ jboolean result = JNI_FALSE;
+
/* Retrieve socket handle */
hLlcpSocket = nfc_jni_get_nfc_socket_handle(e,o);
-
+
+ /* Create the local semaphore */
+ if (!nfc_cb_data_init(&cb_data, NULL))
+ {
+ goto clean_and_return;
+ }
+
/* Service socket */
serviceName.buffer = (uint8_t*)e->GetStringUTFChars(sn, NULL);
serviceName.length = (uint32_t)e->GetStringUTFLength(sn);
@@ -174,37 +187,42 @@ static jboolean com_android_nfc_NativeLlcpSocket_doConnectBy(JNIEnv *e, jobject
ret = phLibNfc_Llcp_ConnectByUri(hLlcpSocket,
&serviceName,
nfc_jni_connect_callback,
- (void*)hLlcpSocket);
+ (void*)&cb_data);
REENTRANCE_UNLOCK();
if(ret != NFCSTATUS_PENDING)
{
LOGE("phLibNfc_Llcp_ConnectByUri() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
- return FALSE;
- }
+ goto clean_and_return;
+ }
TRACE("phLibNfc_Llcp_ConnectByUri() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
-
+
/* Wait for callback response */
- if(sem_wait(nfc_jni_llcp_sem) == -1)
- return FALSE;
-
- if(nfc_jni_cb_status == NFCSTATUS_SUCCESS)
+ if(sem_wait(&cb_data.sem))
{
- return TRUE;
+ LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ goto clean_and_return;
}
- else
+
+ if(cb_data.status != NFCSTATUS_SUCCESS)
{
- return FALSE;
- }
+ goto clean_and_return;
+ }
+
+ result = JNI_TRUE;
+
+clean_and_return:
+ nfc_cb_data_deinit(&cb_data);
+ return result;
}
static jboolean com_android_nfc_NativeLlcpSocket_doClose(JNIEnv *e, jobject o)
{
NFCSTATUS ret;
phLibNfc_Handle hLlcpSocket;
-
+
/* Retrieve socket handle */
hLlcpSocket = nfc_jni_get_nfc_socket_handle(e,o);
-
+
TRACE("phLibNfc_Llcp_Close()");
REENTRANCE_LOCK();
ret = phLibNfc_Llcp_Close(hLlcpSocket);
@@ -224,10 +242,18 @@ static jboolean com_android_nfc_NativeLlcpSocket_doSend(JNIEnv *e, jobject o, jb
struct timespec ts;
phLibNfc_Handle hLlcpSocket;
phNfc_sData_t sSendBuffer;
+ struct nfc_jni_callback_data cb_data;
+ jboolean result = JNI_FALSE;
/* Retrieve socket handle */
hLlcpSocket = nfc_jni_get_nfc_socket_handle(e,o);
+ /* Create the local semaphore */
+ if (!nfc_cb_data_init(&cb_data, NULL))
+ {
+ goto clean_and_return;
+ }
+
sSendBuffer.buffer = (uint8_t*)e->GetByteArrayElements(data, NULL);
sSendBuffer.length = (uint32_t)e->GetArrayLength(data);
@@ -236,28 +262,32 @@ static jboolean com_android_nfc_NativeLlcpSocket_doSend(JNIEnv *e, jobject o, jb
ret = phLibNfc_Llcp_Send(hLlcpSocket,
&sSendBuffer,
nfc_jni_send_callback,
- (void*)hLlcpSocket);
+ (void*)&cb_data);
REENTRANCE_UNLOCK();
if(ret != NFCSTATUS_PENDING)
{
LOGE("phLibNfc_Llcp_Send() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
- return FALSE;
+ goto clean_and_return;
}
TRACE("phLibNfc_Llcp_Send() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
-
- /* Wait for callback response */
- if(sem_wait(nfc_jni_llcp_sem) == -1)
- return FALSE;
-
- if(nfc_jni_cb_status == NFCSTATUS_SUCCESS)
+ /* Wait for callback response */
+ if(sem_wait(&cb_data.sem))
{
- return TRUE;
+ LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ goto clean_and_return;
}
- else
+
+ if(cb_data.status != NFCSTATUS_SUCCESS)
{
- return FALSE;
- }
+ goto clean_and_return;
+ }
+
+ result = JNI_TRUE;
+
+clean_and_return:
+ nfc_cb_data_deinit(&cb_data);
+ return result;
}
static jint com_android_nfc_NativeLlcpSocket_doReceive(JNIEnv *e, jobject o, jbyteArray buffer)
@@ -266,10 +296,18 @@ static jint com_android_nfc_NativeLlcpSocket_doReceive(JNIEnv *e, jobject o, jby
struct timespec ts;
phLibNfc_Handle hLlcpSocket;
phNfc_sData_t sReceiveBuffer;
+ struct nfc_jni_callback_data cb_data;
+ jint result = 0;
/* Retrieve socket handle */
hLlcpSocket = nfc_jni_get_nfc_socket_handle(e,o);
+ /* Create the local semaphore */
+ if (!nfc_cb_data_init(&cb_data, NULL))
+ {
+ goto clean_and_return;
+ }
+
sReceiveBuffer.buffer = (uint8_t*)e->GetByteArrayElements(buffer, NULL);
sReceiveBuffer.length = (uint32_t)e->GetArrayLength(buffer);
@@ -278,7 +316,7 @@ static jint com_android_nfc_NativeLlcpSocket_doReceive(JNIEnv *e, jobject o, jby
ret = phLibNfc_Llcp_Recv(hLlcpSocket,
&sReceiveBuffer,
nfc_jni_receive_callback,
- (void*)hLlcpSocket);
+ (void*)&cb_data);
REENTRANCE_UNLOCK();
if((ret != NFCSTATUS_SUCCESS) && (ret != NFCSTATUS_PENDING))
{
@@ -288,20 +326,21 @@ static jint com_android_nfc_NativeLlcpSocket_doReceive(JNIEnv *e, jobject o, jby
}
TRACE("phLibNfc_Llcp_Recv() returned 0x%04x[%s]", ret, nfc_jni_get_status_name(ret));
- /* Wait for callback response (happen if status is either SUCCESS or PENDING) */
- if(sem_wait(nfc_jni_llcp_sem) == -1)
+ /* Wait for callback response */
+ if(sem_wait(&cb_data.sem))
{
- return 0;
+ LOGE("Failed to wait for semaphore (errno=0x%08x)", errno);
+ goto clean_and_return;
}
- if(nfc_jni_cb_status == NFCSTATUS_SUCCESS)
- {
- return sReceiveBuffer.length;
- }
- else
+ if(cb_data.status == NFCSTATUS_SUCCESS)
{
- return 0;
+ result = sReceiveBuffer.length;
}
+
+clean_and_return:
+ nfc_cb_data_deinit(&cb_data);
+ return result;
}
static jint com_android_nfc_NativeLlcpSocket_doGetRemoteSocketMIU(JNIEnv *e, jobject o)
@@ -387,10 +426,6 @@ static JNINativeMethod gMethods[] =
int register_com_android_nfc_NativeLlcpSocket(JNIEnv *e)
{
- nfc_jni_llcp_sem = (sem_t *)malloc(sizeof(sem_t));
- if(sem_init(nfc_jni_llcp_sem, 0, 0) == -1)
- return -1;
-
return jniRegisterNativeMethods(e,
"com/android/nfc/NativeLlcpSocket",gMethods, NELEM(gMethods));
}