+/* ****************************************************************
+ *
+ * Copyright 2014 Samsung Electronics All Rights Reserved.
+ *
+ *
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ ******************************************************************/
+
#include <stdio.h>
#include <string.h>
#include <jni.h>
#include "caedrserver.h"
#include "logger.h"
#include "oic_malloc.h"
-#include "uthreadpool.h" /* for thread pool */
-#include "umutex.h"
+#include "cathreadpool.h" /* for thread pool */
+#include "camutex.h"
#include "uarraylist.h"
#include "caadapterutils.h"
-#include "com_iotivity_jar_CAEDRInterface.h"
-
-//#define DEBUG_MODE
-#define TAG PCF("CA_EDR_SERVER")
+#include "org_iotivity_ca_CaEdrInterface.h"
+#include "oic_string.h"
-static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
-static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
-static const char *CLASSPATH_BT_UUID = "java/util/UUID";
+#define TAG PCF("OIC_CA_EDR_SERVER")
-static const uint32_t STATE_CONNECTED = 1;
-static const uint32_t STATE_DISCONNECTED = 0;
+static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
+static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
+static const char CLASSPATH_BT_UUID[] = "java/util/UUID";
+static const char CLASSPATH_BT_SERVER_SOCKET[] = "android/bluetooth/BluetoothServerSocket";
-static const uint32_t MAX_PDU_BUFFER = 1024;
-static const uint32_t MAX_CONNECTED_BT_SUPPORT_CNT = 7;
-
-static u_thread_pool_t gThreadPoolHandle = NULL;
+static ca_thread_pool_t g_threadPoolHandle = NULL;
static JavaVM *g_jvm;
-static jobject gContext;
/**
- * @var gMutexSocketListManager
- * @brief Mutex to synchronize socket list update
+ * Maximum CoAP over TCP header length
+ * to know the total data length.
*/
-static u_mutex gMutexSocketListManager;
-
-// server socket instance
-static jobject gServerSocketObject = NULL;
+#define EDR_MAX_HEADER_LEN 6
/**
- * @var gMutexUnicastServer
- * @brief Mutex to synchronize unicast server
+ * Mutex to synchronize receive server.
*/
-static u_mutex gMutexUnicastServer = NULL;
+static ca_mutex g_mutexReceiveServer = NULL;
/**
- * @var gStopUnicast
- * @brief Flag to control the Receive Unicast Data Thread
+ * Flag to control the Receive Unicast Data Thread.
*/
-static bool gStopUnicast = FALSE;
+static bool g_stopUnicast = false;
/**
- * @var gMutexMulticastServer
- * @brief Mutex to synchronize secure multicast server
+ * Mutex to synchronize accept server.
*/
-static u_mutex gMutexMulticastServer = NULL;
+static ca_mutex g_mutexAcceptServer = NULL;
/**
- * @var gMutexMulticastServer
- * @brief Mutex to synchronize multicast receiver
+ * Flag to control the Accept Thread.
*/
-static u_mutex gMutexReceiver = NULL;
+static bool g_stopAccept = false;
/**
- * @var gStopMulticast
- * @brief Flag to control the Receive Multicast Data Thread
+ * Mutex to synchronize server socket.
*/
-static bool gStopMulticast = FALSE;
+static ca_mutex g_mutexServerSocket = NULL;
/**
- * @var gStopAccept
- * @brief Flag to control the Accept Thread
+ * Flag to control the server socket.
*/
-static bool gStopAccept = FALSE;
+static jobject g_serverSocket = NULL;
+/**
+ * Mutex to synchronize device state list.
+ */
+static ca_mutex g_mutexStateList = NULL;
-static jobject gInputStream = NULL;
-
-static jobject gSeverSocket = NULL;
-
-typedef struct send_data {
- char* address;
- char* data;
- uint32_t id;
-} data_t;
+/**
+ * Mutex to synchronize device object list.
+ */
+static ca_mutex g_mutexObjectList = NULL;
/**
- @brief Thread context information for unicast, multicast and secured unicast server
+ * Thread context information for unicast, multicast and secured unicast server.
*/
typedef struct
{
} CAAdapterAcceptThreadContext_t;
/**
- * @var gEDRNetworkChangeCallback
- * @brief Maintains the callback to be notified when data received from remote Bluetooth device
+ * Maintains the callback to be notified when data received from remote
+ * Bluetooth device.
*/
-static CAEDRDataReceivedCallback gEDRPacketReceivedCallback = NULL;
+static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
static void CAReceiveHandler(void *data)
{
// Input validation
VERIFY_NON_NULL_VOID(data, TAG, "Invalid thread context");
- jboolean isAttached = FALSE;
+ bool isAttached = false;
JNIEnv* env;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
- if(res != JNI_OK)
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "CAReceiveHandler - Could not get JNIEnv pointer");
+ OIC_LOG(DEBUG, TAG, "CAReceiveHandler - Could not get JNIEnv pointer");
res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if(res != JNI_OK)
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
return;
}
- isAttached = TRUE;
+ isAttached = true;
}
- CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)data;
+ CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) data;
- while (TRUE != *(ctx->stopFlag))
+ while (true != *(ctx->stopFlag))
{
- uint32_t idx;
+ if (!CAEDRNativeIsEnableBTAdapter(env))
+ {
+ OIC_LOG(INFO, TAG, "BT adapter is not enabled");
+ break;
+ }
// if new socket object is added in socket list after below logic is ran.
// new socket will be started to read after next while loop
uint32_t length = CAEDRGetSocketListLength();
- if(0 == length)
+ if (0 != length)
{
- OIC_LOG_V(DEBUG, TAG, "socket list is empty");
- sleep(1);
- }
-
- for(idx = 0 ; idx < length ; idx++)
- {
- OIC_LOG_V(DEBUG, TAG, "start CAEDRNativeReadData");
- CAEDRNativeReadData(env, idx, ctx->type);
- sleep(1);
+ for (uint32_t idx = 0; idx < length; idx++)
+ {
+ CAEDRNativeReadData(env, idx);
+ }
}
}
- if(isAttached)
+ if (isAttached)
+ {
(*g_jvm)->DetachCurrentThread(g_jvm);
+ }
- if(ctx)
- OICFree(ctx);
+ OICFree(ctx);
OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler");
}
static void CAAcceptHandler(void *data)
{
- int status;
+ OIC_LOG(DEBUG, TAG, "AcceptThread start");
- OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread start");
+ VERIFY_NON_NULL_VOID(data, TAG, "data is null");
- jboolean isAttached = FALSE;
+ bool isAttached = false;
JNIEnv* env;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
- if(res != JNI_OK)
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "CAAcceptHandler - Could not get JNIEnv pointer");
+ OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if(res != JNI_OK)
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
return;
}
- isAttached = TRUE;
+ isAttached = true;
}
- jobject jni_obj_BTSeverSocket = CAEDRNativeListen(env);
- if(!jni_obj_BTSeverSocket) {
- OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread: jni_obj_BTSeverSocket is null");
+ jobject jni_obj_BTServerSocket = CAEDRNativeListen(env);
+ if (!jni_obj_BTServerSocket)
+ {
+ OIC_LOG(ERROR, TAG, "jni_obj_BTServerSocket is null");
- if(isAttached)
+ if (isAttached)
+ {
(*g_jvm)->DetachCurrentThread(g_jvm);
+ }
return;
}
- gSeverSocket = (*env)->NewGlobalRef(env, jni_obj_BTSeverSocket);
+ ca_mutex_lock(g_mutexServerSocket);
+ g_serverSocket = (*env)->NewGlobalRef(env, jni_obj_BTServerSocket);
+ ca_mutex_unlock(g_mutexServerSocket);
- CAAdapterAcceptThreadContext_t *ctx = (CAAdapterAcceptThreadContext_t *)data;
-
- if(NULL == ctx)
- {
- OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread: ctx is null");
- return;
- }
+ CAAdapterAcceptThreadContext_t *ctx = (CAAdapterAcceptThreadContext_t *) data;
// it should be initialized for restart accept thread
- gStopAccept = FALSE;
+ ca_mutex_lock(g_mutexAcceptServer);
+ g_stopAccept = false;
+ ca_mutex_unlock(g_mutexAcceptServer);
- while (TRUE != *(ctx->stopFlag))
+ while (true != *(ctx->stopFlag))
{
- OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread running");
-
// when BT state is changed with Off. its thread will be stopped
- if(!CAEDRNativeIsEnableBTAdapter(env))
+ if (!CAEDRNativeIsEnableBTAdapter(env))
{
- OIC_LOG(DEBUG, TAG, "BT adpater is not enable");
- gStopAccept = TRUE;
- gSeverSocket = NULL;
+ OIC_LOG(INFO, TAG, "BT adapter is not enabled");
+ ca_mutex_lock(g_mutexAcceptServer);
+ g_stopAccept = true;
+ ca_mutex_unlock(g_mutexAcceptServer);
+ ca_mutex_lock(g_mutexServerSocket);
+ g_serverSocket = NULL;
+ ca_mutex_unlock(g_mutexServerSocket);
}
else
{
- CAEDRNativeAccept(env, gSeverSocket);
+ CAEDRNativeAccept(env, g_serverSocket);
}
}
- if(isAttached)
+ if (isAttached)
+ {
(*g_jvm)->DetachCurrentThread(g_jvm);
+ }
- if(ctx)
- OICFree(ctx);
+ OICFree(ctx);
- OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread finish");
+ OIC_LOG(DEBUG, TAG, "AcceptThread finish");
return;
}
/**
- * implement for adapter common method
+ * implement for adapter common method.
*/
-CAResult_t CAEDRServerStart(const char *serviceUUID, int32_t *serverFD, u_thread_pool_t handle)
+CAResult_t CAEDRServerStart()
{
- OIC_LOG_V(DEBUG, TAG, "IN");
- CAEDRServerInitialize(handle);
- // FIXME
- CAEDRStartUnicastServer(NULL, FALSE);
+ if (!g_threadPoolHandle)
+ {
+ return CA_STATUS_NOT_INITIALIZED;
+ }
- *serverFD = 1;
- OIC_LOG_V(DEBUG, TAG, "OUT");
- return CA_STATUS_OK;
+ CAResult_t res = CAEDRServerStartAcceptThread();
+ if (CA_STATUS_OK == res)
+ {
+ res = CAEDRStartReceiveThread(false);
+ if (CA_STATUS_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "failed to start receive thread");
+ CAEDRServerStop();
+ return CA_STATUS_FAILED;
+ }
+ }
+
+ return res;
}
-CAResult_t CAEDRServerStop(const int32_t serverFD)
+CAResult_t CAEDRServerStop()
{
- OIC_LOG_V(DEBUG, TAG, "IN");
- CAEDRStopUnicastServer(-1);
- CAEDRStopMulticastServer(-1);
- CAEDRServerTerminate();
- OIC_LOG_V(DEBUG, TAG, "OUT");
+ CAEDRStopReceiveThread();
+
+ ca_mutex_lock(g_mutexAcceptServer);
+ g_stopAccept = true;
+ ca_mutex_unlock(g_mutexAcceptServer);
+
+ if (!g_jvm)
+ {
+ OIC_LOG(DEBUG, TAG, "g_jvm is null");
+ return CA_STATUS_FAILED;
+ }
+
+ bool isAttached = false;
+ JNIEnv* env;
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
+ {
+ OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
+ res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
+
+ if (JNI_OK != res)
+ {
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
+ return CA_STATUS_FAILED;
+ }
+ isAttached = true;
+ }
+
+ CAEDRNatvieCloseServerTask(env);
+
+ if (isAttached)
+ {
+ (*g_jvm)->DetachCurrentThread(g_jvm);
+ }
+
return CA_STATUS_OK;
}
void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
{
- gEDRPacketReceivedCallback = packetReceivedCallback;
+ g_edrPacketReceivedCallback = packetReceivedCallback;
}
-/**
- * Destroy Mutex
- */
static void CAEDRServerDestroyMutex()
{
- OIC_LOG(DEBUG, TAG, "IN");
-
- if (gMutexUnicastServer)
+ if (g_mutexReceiveServer)
{
- u_mutex_free(gMutexUnicastServer);
- gMutexUnicastServer = NULL;
+ ca_mutex_free(g_mutexReceiveServer);
+ g_mutexReceiveServer = NULL;
}
-#ifdef __WITH_DTLS__
-
-#endif
+ if (g_mutexAcceptServer)
+ {
+ ca_mutex_free(g_mutexAcceptServer);
+ g_mutexAcceptServer = NULL;
+ }
- if (gMutexMulticastServer)
+ if (g_mutexServerSocket)
{
- u_mutex_free(gMutexMulticastServer);
- gMutexMulticastServer = NULL;
+ ca_mutex_free(g_mutexServerSocket);
+ g_mutexServerSocket = NULL;
}
- if(gMutexSocketListManager)
+ if (g_mutexStateList)
{
- u_mutex_free(gMutexSocketListManager);
- gMutexSocketListManager = NULL;
+ ca_mutex_free(g_mutexStateList);
+ g_mutexStateList = NULL;
}
- OIC_LOG(DEBUG, TAG, "OUT");
+ if (g_mutexObjectList)
+ {
+ ca_mutex_free(g_mutexObjectList);
+ g_mutexObjectList = NULL;
+ }
}
-
-/*
- * Create Mutex
- */
static CAResult_t CAEDRServerCreateMutex()
{
- OIC_LOG(DEBUG, TAG, "IN");
+ g_mutexReceiveServer = ca_mutex_new();
+ if (!g_mutexReceiveServer)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to created mutex!");
+ return CA_STATUS_FAILED;
+ }
- gMutexUnicastServer = u_mutex_new();
- if (!gMutexUnicastServer)
+ g_mutexAcceptServer = ca_mutex_new();
+ if (!g_mutexAcceptServer)
{
OIC_LOG(ERROR, TAG, "Failed to created mutex!");
+
+ CAEDRServerDestroyMutex();
return CA_STATUS_FAILED;
}
-#ifdef __WITH_DTLS__
+ g_mutexServerSocket = ca_mutex_new();
+ if (!g_mutexServerSocket)
+ {
+ OIC_LOG(ERROR, TAG, "Failed to created mutex!");
-#endif
+ CAEDRServerDestroyMutex();
+ return CA_STATUS_FAILED;
+ }
- gMutexMulticastServer = u_mutex_new();
- if (!gMutexMulticastServer)
+ g_mutexStateList = ca_mutex_new();
+ if (!g_mutexStateList)
{
OIC_LOG(ERROR, TAG, "Failed to created mutex!");
return CA_STATUS_FAILED;
}
- gMutexSocketListManager = u_mutex_new();
- if (!gMutexSocketListManager)
+ g_mutexObjectList = ca_mutex_new();
+ if (!g_mutexObjectList)
{
OIC_LOG(ERROR, TAG, "Failed to created mutex!");
return CA_STATUS_FAILED;
}
- OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
void CAEDRServerJniInit()
{
- OIC_LOG_V(DEBUG, TAG, "CAEDRServerJniInit");
+ OIC_LOG(DEBUG, TAG, "CAEDRServerJniInit");
g_jvm = CANativeJNIGetJavaVM();
}
-void CAEDRServerInitialize(u_thread_pool_t handle)
+CAResult_t CAEDRServerInitialize(ca_thread_pool_t handle)
{
OIC_LOG(DEBUG, TAG, "CAEDRServerInitialize");
+ VERIFY_NON_NULL(handle, TAG, "handle is NULL");
- gThreadPoolHandle = handle;
-
- CAEDRServerStartAccpetThread();
+ g_threadPoolHandle = handle;
+ CAEDRServerJniInit();
- OIC_LOG(DEBUG, TAG, "OUT");
+ return CAEDRServerCreateMutex();
}
-void CAEDRServerStartAccpetThread()
+CAResult_t CAEDRServerStartAcceptThread()
{
- CAEDRServerJniInit();
-
- // init mutex
- CAEDRServerCreateMutex();
-
- jboolean isAttached = FALSE;
+ bool isAttached = false;
JNIEnv* env;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
- if(res != JNI_OK)
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "CAEDRServerInitialize - Could not get JNIEnv pointer");
+ OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if(res != JNI_OK)
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
- return;
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
+ return CA_STATUS_FAILED;
}
- isAttached = TRUE;
+ isAttached = true;
}
jstring jni_address = CAEDRNativeGetLocalDeviceAddress(env);
- if(jni_address)
+ if (jni_address)
{
const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
OIC_LOG_V(DEBUG, TAG, "My BT Address is %s", localAddress);
+ (*env)->ReleaseStringUTFChars(env, jni_address, localAddress);
}
+ ca_mutex_lock(g_mutexStateList);
CAEDRNativeCreateDeviceStateList();
+ ca_mutex_unlock(g_mutexStateList);
+
+ ca_mutex_lock(g_mutexObjectList);
CAEDRNativeCreateDeviceSocketList();
+ ca_mutex_unlock(g_mutexObjectList);
- if(isAttached)
+ if (isAttached)
+ {
(*g_jvm)->DetachCurrentThread(g_jvm);
+ }
- CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
- OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
+ CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) OICMalloc(
+ sizeof(CAAdapterReceiveThreadContext_t));
if (!ctx)
{
OIC_LOG(ERROR, TAG, "Out of memory!");
- return;
+ return CA_MEMORY_ALLOC_FAILED;
}
- ctx->stopFlag = &gStopAccept;
- if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, CAAcceptHandler, (void *)ctx))
+ ctx->stopFlag = &g_stopAccept;
+ if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CAAcceptHandler, (void *) ctx))
{
OIC_LOG(ERROR, TAG, "Failed to create read thread!");
- OICFree((void *)ctx);
- return;
+ OICFree((void *) ctx);
+ return CA_STATUS_FAILED;
}
+
+ return CA_STATUS_OK;
}
void CAEDRServerTerminate()
{
OIC_LOG(DEBUG, TAG, "CAEDRServerTerminate");
- jboolean isAttached = FALSE;
+ if (!g_jvm)
+ {
+ return;
+ }
+ bool isAttached = false;
JNIEnv* env;
- jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
- if(res != JNI_OK)
+ jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "CAEDRServerTerminate - Could not get JNIEnv pointer");
+ OIC_LOG(DEBUG, TAG, "Could not get JNIEnv pointer");
res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
- if(res != JNI_OK)
+ if (JNI_OK != res)
{
- OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
+ OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
return;
}
- isAttached = TRUE;
+ isAttached = true;
}
- gStopAccept = TRUE;
- gStopMulticast = TRUE;
- gStopUnicast = TRUE;
-
- CAEDRNatvieCloseServerTask(env);
-
CAEDRNativeSocketCloseToAll(env);
- // delete mutex
- CAEDRServerDestroyMutex();
+ if (isAttached)
+ {
+ (*g_jvm)->DetachCurrentThread(g_jvm);
+ }
CAEDRNativeRemoveAllDeviceState();
CAEDRNativeRemoveAllDeviceSocket(env);
- if(isAttached)
- (*g_jvm)->DetachCurrentThread(g_jvm);
+ // delete mutex
+ CAEDRServerDestroyMutex();
}
-int32_t CAEDRStartUnicastServer(const char* address, bool isSecured)
+CAResult_t CAEDRStartReceiveThread(bool isSecured)
{
- OIC_LOG_V(DEBUG, TAG, "CAEDRStartUnicastServer(%s)", address);
+ OIC_LOG(DEBUG, TAG, "CAEDRStartReceiveThread");
- u_mutex_lock(gMutexUnicastServer);
+ ca_mutex_lock(g_mutexReceiveServer);
/**
- * The task to listen for data from unicast is added to the thread pool.
- * This is a blocking call is made where we try to receive some data..
- * We will keep waiting until some data is received.
- * This task will be terminated when thread pool is freed on stopping the adapters.
- * Thread context will be freed by thread on exit.
- */
- CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
- OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
+ * The task to listen for data from unicast is added to the thread pool.
+ * This is a blocking call is made where we try to receive some data..
+ * We will keep waiting until some data is received.
+ * This task will be terminated when thread pool is freed on stopping the adapters.
+ * Thread context will be freed by thread on exit.
+ */
+ CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) OICMalloc(
+ sizeof(CAAdapterReceiveThreadContext_t));
if (!ctx)
{
OIC_LOG(ERROR, TAG, "Out of memory!");
- u_mutex_unlock(gMutexUnicastServer);
+ ca_mutex_unlock(g_mutexReceiveServer);
return CA_MEMORY_ALLOC_FAILED;
}
- ctx->stopFlag = &gStopUnicast;
+ g_stopUnicast = false;
+ ctx->stopFlag = &g_stopUnicast;
ctx->type = isSecured ? CA_SECURED_UNICAST_SERVER : CA_UNICAST_SERVER;
- if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, CAReceiveHandler, (void *)ctx))
+ if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CAReceiveHandler, (void *) ctx))
{
OIC_LOG(ERROR, TAG, "Failed to create read thread!");
- u_mutex_unlock(gMutexUnicastServer);
- OICFree((void *)ctx);
+ ca_mutex_unlock(g_mutexReceiveServer);
+ OICFree((void *) ctx);
return CA_STATUS_FAILED;
}
- u_mutex_unlock(gMutexUnicastServer);
+ ca_mutex_unlock(g_mutexReceiveServer);
OIC_LOG(DEBUG, TAG, "OUT");
return CA_STATUS_OK;
}
-int32_t CAEDRStartMulticastServer(bool isSecured)
+CAResult_t CAEDRStopReceiveThread()
{
- OIC_LOG_V(DEBUG, TAG, "CAEDRStartMulticastServer");
+ OIC_LOG(DEBUG, TAG, "CAEDRStopReceiveThread");
- u_mutex_lock(gMutexMulticastServer);
+ ca_mutex_lock(g_mutexReceiveServer);
+ g_stopUnicast = true;
+ ca_mutex_unlock(g_mutexReceiveServer);
- /**
- * The task to listen to data from multicast socket is added to the thread pool.
- * This is a blocking call is made where we try to receive some data.
- * We will keep waiting until some data is received.
- * This task will be terminated when thread pool is freed on stopping the adapters.
- * Thread context will be freed by thread on exit.
- */
- CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
- OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
- if (!ctx)
- {
- OIC_LOG(ERROR, TAG, "Out of memory!");
- u_mutex_unlock(gMutexMulticastServer);
+ return CA_STATUS_OK;
+}
- return CA_MEMORY_ALLOC_FAILED;
+CAResult_t CAEDRNativeReadData(JNIEnv *env, uint32_t idx)
+{
+ if ((*env)->ExceptionCheck(env))
+ {
+ (*env)->ExceptionDescribe(env);
+ (*env)->ExceptionClear(env);
+ OIC_LOG(ERROR, TAG, "env error!!");
+ return CA_STATUS_FAILED;
}
- ctx->stopFlag = &gStopMulticast;
- ctx->type = CA_MULTICAST_SERVER;
-
- gStopMulticast = FALSE;
- if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, CAReceiveHandler, (void *)ctx))
+ jobject jni_obj_inputStream = CAEDRNativeGetInputStream(idx);
+ if (!jni_obj_inputStream)
{
- OIC_LOG(ERROR, TAG, "thread_pool_add_task failed!");
-
- gStopMulticast = TRUE;
- u_mutex_unlock(gMutexMulticastServer);
-
+ OIC_LOG(ERROR, TAG, "jni_obj_inputStream is null");
return CA_STATUS_FAILED;
}
- u_mutex_unlock(gMutexMulticastServer);
-
- OIC_LOG(DEBUG, TAG, "OUT");
- return CA_STATUS_OK;
-}
-
-int32_t CAEDRStopUnicastServer(int32_t serverID)
-{
- OIC_LOG(DEBUG, TAG, "CAEDRStopUnicastServer");
-
- u_mutex_lock(gMutexUnicastServer);
- gStopUnicast = TRUE;
- u_mutex_unlock(gMutexUnicastServer);
-
- return 0;
-}
-
-int32_t CAEDRStopMulticastServer(int32_t serverID)
-{
- OIC_LOG(DEBUG, TAG, "CAEDRStopMulticastServer");
-
- u_mutex_lock(gMutexMulticastServer);
- gStopMulticast = true;
- u_mutex_unlock(gMutexMulticastServer);
-
- OIC_LOG_V(INFO, TAG, "Multicast server stopped");
-
- return 0;
-}
-uint32_t CAEDRNativeReadData(JNIEnv *env, uint32_t id, CAAdapterServerType_t type)
-{
- if(!CAEDRNativeIsEnableBTAdapter(env))
+ jmethodID jni_mid_available = CAGetJNIMethodID(env, "java/io/InputStream", "available", "()I");
+ if (!jni_mid_available)
{
- OIC_LOG(DEBUG, TAG, "BT adpater is not enable");
- return 0;
+ OIC_LOG(ERROR, TAG, "jni_mid_available is null");
+ return CA_STATUS_FAILED;
}
- if(!((*env)->ExceptionCheck(env)))
+ jint available = (*env)->CallIntMethod(env, jni_obj_inputStream, jni_mid_available);
+
+ if (0 < available)
{
- // check whether this socket object is connected or not.
- jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
- if(!jni_obj_socket)
+ jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(idx);
+ if (!jni_obj_socket)
{
- return 0;
+ OIC_LOG(ERROR, TAG, "jni_obj_socket is null");
+ return CA_STATUS_FAILED;
}
jstring jni_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_socket);
- const char* address = (*env)->GetStringUTFChars(env, jni_str_address, NULL);
-
- // check it whether is still connected or not through google api
- jboolean ret = CAEDRIsConnectedForSocket(env, jni_obj_socket);
- if(!ret)
+ if (!jni_str_address)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] this device is not connected now...let close socket");
+ OIC_LOG(ERROR, TAG, "jni_str_address is null");
+ return CA_STATUS_FAILED;
}
- // check it whether is still connected or not through socket state list
- if(STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
+ const char* address = (*env)->GetStringUTFChars(env, jni_str_address, NULL);
+ if (!address)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: it is not connected yet..");
-
- // remove socket to list
- // this code is related to below read fail exception code
- CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);
-
- return 0;
+ OIC_LOG(ERROR, TAG, "address is null");
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
}
- // start to read through InputStream
- jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
- jmethodID jni_mid_getInputStream = (*env)->GetMethodID(env, jni_cid_BTsocket, "getInputStream", "()Ljava/io/InputStream;");
- OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: get InputStream..%d, %s", id, address);
-
- if(!jni_obj_socket)
+ CAConnectedDeviceInfo_t *deviceInfo =
+ (CAConnectedDeviceInfo_t *) CAEDRGetDeviceInfoFromAddress(address);
+ if (!deviceInfo)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_socket is not available anymore..");
- return 0;
+ OIC_LOG(ERROR, TAG, "failed to get device info from list");
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
}
- jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, jni_obj_socket, jni_mid_getInputStream);
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: ready inputStream...");
-
- gInputStream = (*env)->NewGlobalRef(env, jni_obj_inputStream);
-
- jclass jni_cid_InputStream = (*env)->FindClass(env, "java/io/InputStream");
- jmethodID jni_mid_read = (*env)->GetMethodID(env, jni_cid_InputStream, "read", "([BII)I");
-
- jbyteArray jbuf = (*env)->NewByteArray(env, MAX_PDU_BUFFER);
- jlong nread;
-
- if(!jni_obj_socket)
+ jint bufSize = (deviceInfo->totalDataLen == 0) ? EDR_MAX_HEADER_LEN
+ : deviceInfo->totalDataLen;
+ if (!deviceInfo->recvData)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_socket is not available anymore...");
- return 0;
+ deviceInfo->recvData = OICCalloc(1, bufSize);
+ if (!deviceInfo->recvData)
+ {
+ OIC_LOG(ERROR, TAG, "out of memory");
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
+ }
}
- nread = (*env)->CallIntMethod(env, gInputStream, jni_mid_read, jbuf, (jint)0, MAX_PDU_BUFFER);
+ jint remainSize = (jint) bufSize - deviceInfo->recvDataLen;
+ if (0 >= remainSize)
+ {
+ OIC_LOG(ERROR, TAG, "remainSize value is invalid.");
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
+ }
- OIC_LOG(DEBUG, TAG, "[EDR][Native] read something from InputStream");
+ jbyteArray jbuf = (*env)->NewByteArray(env, remainSize);
+ if (!jbuf)
+ {
+ OIC_LOG(ERROR, TAG, "jbuf is null");
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
+ }
- if(-1 == nread)
+ jmethodID jni_mid_read = CAGetJNIMethodID(env, "java/io/InputStream", "read", "([BII)I");
+ if (!jni_mid_read)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] read buffer is empty...");
- return 0;
+ OIC_LOG(ERROR, TAG, "jni_mid_read is null");
+ (*env)->DeleteLocalRef(env, jbuf);
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
}
- if((*env)->ExceptionCheck(env))
+ OIC_LOG_V(DEBUG, TAG, "read InputStream (idx:%d, addr:%s)", idx, address);
+ jint recvLen = (*env)->CallIntMethod(env, jni_obj_inputStream, jni_mid_read,
+ jbuf, (jint) 0, remainSize);
+ if (-1 == recvLen)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: read Error!!!");
- (*env)->ExceptionDescribe(env);
- (*env)->ExceptionClear(env);
-
- // update state to disconnect
- // the socket will be close next read thread routine
- CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
- return 0;
+ OIC_LOG(ERROR, TAG, "recvLen is -1");
+ (*env)->DeleteLocalRef(env, jbuf);
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
}
+ OIC_LOG_V(DEBUG, TAG, "read success (length: %d bytes)", recvLen);
- OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: reading");
jbyte* buf = (*env)->GetByteArrayElements(env, jbuf, NULL);
- if (buf == NULL)
+ if (!buf)
{
- return 0;
+ OIC_LOG(ERROR, TAG, "buf is null");
+ (*env)->DeleteLocalRef(env, jbuf);
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
}
+ memcpy(deviceInfo->recvData + deviceInfo->recvDataLen, (const char*) buf, recvLen);
+ deviceInfo->recvDataLen += recvLen;
- jint length = strlen((char*)buf);
- OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: read %s, %d", buf, length);
-
- char responseData[MAX_PDU_BUFFER];
- memset(responseData, 0, MAX_PDU_BUFFER);
- strncpy(responseData, (char*)buf, length);
- responseData[length] = '\0';
+ (*env)->ReleaseByteArrayElements(env, jbuf, buf, 0);
+ (*env)->DeleteLocalRef(env, jbuf);
- switch (type)
+ if (!deviceInfo->totalDataLen)
{
- case CA_UNICAST_SERVER:
- case CA_MULTICAST_SERVER:
- // Notify data to upper layer
- if (gEDRPacketReceivedCallback)
+ coap_transport_type transport = coap_get_tcp_header_type_from_initbyte(
+ ((unsigned char *) deviceInfo->recvData)[0] >> 4);
+ size_t headerLen = coap_get_tcp_header_length_for_transport(transport);
+ if (deviceInfo->recvData && deviceInfo->recvDataLen >= headerLen)
+ {
+ deviceInfo->totalDataLen = coap_get_total_message_length(deviceInfo->recvData,
+ deviceInfo->recvDataLen);
+ OIC_LOG_V(DEBUG, TAG, "total data length [%d] bytes", deviceInfo->totalDataLen);
+
+ uint8_t *newBuf = OICRealloc(deviceInfo->recvData, deviceInfo->totalDataLen);
+ if (!newBuf)
{
- // u_mutex_lock(gMutexSocketListManager);
- uint32_t sentLength = 0;
- OIC_LOG_V(DEBUG, TAG, "[EDR][Native] data will be sent to callback routine: %s, %d", responseData, length);
-// sleep(3);
- gEDRPacketReceivedCallback(address, (void*)responseData, length, &sentLength);
- // u_mutex_unlock(gMutexSocketListManager);
+ OIC_LOG(ERROR, TAG, "out of memory");
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
}
- break;
-#ifdef __WITH_DTLS__
- case CA_SECURED_UNICAST_SERVER:
- break;
-#endif //__WITH_DTLS__
- default:
- // Should never occur
- OIC_LOG_V(DEBUG, TAG, "Invalid server type");
- return 0;
+ deviceInfo->recvData = newBuf;
+ }
}
- (*env)->ReleaseByteArrayElements(env, jbuf, buf, 0);
- }
- else
- {
- (*env)->ExceptionDescribe(env);
- (*env)->ExceptionClear(env);
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: env error!!");
- return 0;
- }
- return 1;
-}
+ if (deviceInfo->totalDataLen == deviceInfo->recvDataLen)
+ {
+ if ((*env)->ExceptionCheck(env))
+ {
+ OIC_LOG(ERROR, TAG, "read Error!!!");
+ (*env)->ExceptionDescribe(env);
+ (*env)->ExceptionClear(env);
-jboolean CAEDRIsConnectedForSocket(JNIEnv *env, jobject socket)
-{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRIsConnectedForSocket...");
+ // update state to disconnect
+ // the socket will be close next read thread routine
+ ca_mutex_lock(g_mutexStateList);
+ CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
+ ca_mutex_unlock(g_mutexStateList);
- if(!CAEDRNativeIsEnableBTAdapter(env))
- {
- OIC_LOG(DEBUG, TAG, "BT adpater is not enable");
- return FALSE;
- }
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ return CA_STATUS_FAILED;
+ }
- if(!socket)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRIsConnectedForSocket - socket is null");
- return FALSE;
- }
+ if (g_edrPacketReceivedCallback)
+ {
+ OIC_LOG_V(DEBUG, TAG, "data will be sent to callback routine: %s, %d",
+ deviceInfo->recvData, deviceInfo->recvDataLen);
- jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
- if(!jni_cid_BTsocket)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRIsConnectedForSocket - jni_cid_BTsocket is null");
- return FALSE;
- }
+ uint32_t sentLength = 0;
+ g_edrPacketReceivedCallback(address, (void*) deviceInfo->recvData,
+ deviceInfo->recvDataLen, &sentLength);
- jmethodID jni_mid_isConnected = (*env)->GetMethodID(env, jni_cid_BTsocket, "isConnected", "()Z");
- if(!jni_mid_isConnected)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRIsConnectedForSocket - jni_mid_isConnected is null.");
- return FALSE;
- }
+ OICFree(deviceInfo->recvData);
+ deviceInfo->recvData = NULL;
+ deviceInfo->recvDataLen = 0;
+ deviceInfo->totalDataLen = 0;
+ }
+ }
- jboolean jni_isConnected = (*env)->CallBooleanMethod(env, socket, jni_mid_isConnected);
+ (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
+ (*env)->DeleteLocalRef(env, jni_str_address);
+ }
- return jni_isConnected;
+ return CA_STATUS_OK;
}
void CANativeStartListenTask(JNIEnv *env)
{
- jobject jni_obj_BTSeverSocket = CAEDRNativeListen(env);
- if(!jni_obj_BTSeverSocket) {
- OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread: jni_obj_BTSeverSocket is null");
+ jobject jni_obj_BTServerSocket = CAEDRNativeListen(env);
+ if (!jni_obj_BTServerSocket)
+ {
+ OIC_LOG(ERROR, TAG, "jni_obj_BTServerSocket is null");
return;
}
- gSeverSocket = (*env)->NewGlobalRef(env, jni_obj_BTSeverSocket);
+ ca_mutex_lock(g_mutexServerSocket);
+ g_serverSocket = (*env)->NewGlobalRef(env, jni_obj_BTServerSocket);
+ ca_mutex_unlock(g_mutexServerSocket);
}
jobject CAEDRNativeListen(JNIEnv *env)
{
- if(!CAEDRNativeIsEnableBTAdapter(env))
+ OIC_LOG(DEBUG, TAG, "CAEDRNativeListen - IN");
+
+ if (!CAEDRNativeIsEnableBTAdapter(env))
{
- OIC_LOG(DEBUG, TAG, "BT adpater is not enable");
+ OIC_LOG(INFO, TAG, "BT adapter is not enabled");
return NULL;
}
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen");
-
- jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
- if(!jni_cid_BTAdapter)
+ jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
+ if (!jni_cid_BTAdapter)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_cid_BTAdapter is null");
+ OIC_LOG(ERROR, TAG, "jni_cid_BTAdapter is null");
return NULL;
}
// get BTadpater
- jmethodID jni_mid_getDefaultAdapter =(*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
- if(!jni_cid_BTAdapter)
+ jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
+ "getDefaultAdapter",
+ METHODID_OBJECTNONPARAM);
+ if (!jni_mid_getDefaultAdapter)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_cid_BTAdapter is null");
+ OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
return NULL;
}
- jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
- if(!jni_obj_BTAdapter)
+ jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
+ jni_mid_getDefaultAdapter);
+ if (!jni_obj_BTAdapter)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_obj_BTAdapter is null");
+ OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
return NULL;
}
// get listen method ID
- jmethodID jni_mid_listen = (*env)->GetMethodID(env, jni_cid_BTAdapter,
- "listenUsingInsecureRfcommWithServiceRecord","(Ljava/lang/String;Ljava/util/UUID;)Landroid/bluetooth/BluetoothServerSocket;");
- if(!jni_mid_listen)
+ jmethodID jni_mid_listen = (*env)->GetMethodID(
+ env, jni_cid_BTAdapter, "listenUsingInsecureRfcommWithServiceRecord",
+ "(Ljava/lang/String;Ljava/util/UUID;)Landroid/bluetooth/BluetoothServerSocket;");
+ if (!jni_mid_listen)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_mid_listen is null");
+ OIC_LOG(ERROR, TAG, "jni_mid_listen is null");
return NULL;
}
// listenUsingInsecureRfcommWithServiceRecord / listenUsingRfcommWithServiceRecord
// setting UUID
jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
- if(!jni_cid_uuid)
+ if (!jni_cid_uuid)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_mid_listen is null");
+ OIC_LOG(ERROR, TAG, "jni_mid_listen is null");
return NULL;
}
- jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
- if(!jni_mid_fromString)
+ jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(
+ env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
+ if (!jni_mid_fromString)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_mid_fromString is null");
+ OIC_LOG(ERROR, TAG, "jni_mid_fromString is null");
return NULL;
}
-// jstring uuid = (*env)->NewStringUTF(env, "0000111f-0000-1000-8000-00805f9b8033");
- jstring jni_uuid = (*env)->NewStringUTF(env, "00000000-0000-0000-0000-0000cdab0000");
- jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
- if(!jni_obj_uuid)
+ jstring jni_uuid = (*env)->NewStringUTF(env, OIC_EDR_SERVICE_ID);
+ if (!jni_uuid)
+ {
+ OIC_LOG(ERROR, TAG, "jni_uuid is null");
+ return NULL;
+ }
+ jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
+ jni_uuid);
+ if (!jni_obj_uuid)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_obj_uuid is null");
+ OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
return NULL;
}
// create socket
jstring jni_name = (*env)->NewStringUTF(env, "BluetoothTestSecure");
- jobject jni_obj_BTSeverSocket = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_listen, jni_name, jni_obj_uuid);
- if(!jni_obj_BTSeverSocket)
+ if (!jni_name)
+ {
+ OIC_LOG(ERROR, TAG, "jni_name is null");
+ return NULL;
+ }
+ jobject jni_obj_BTServerSocket = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_listen,
+ jni_name, jni_obj_uuid);
+ if (!jni_obj_BTServerSocket)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_obj_BTSeverSocket is null");
+ OIC_LOG(ERROR, TAG, "jni_obj_BTServerSocket is null");
return NULL;
}
- gServerSocketObject = (*env)->NewGlobalRef(env, jni_obj_BTSeverSocket);
+ OIC_LOG(DEBUG, TAG, "CAEDRNativeListen - OUT");
- return jni_obj_BTSeverSocket;
+ return jni_obj_BTServerSocket;
}
-void CAEDRNativeAccept(JNIEnv *env, jobject severSocketObject)
+void CAEDRNativeAccept(JNIEnv *env, jobject serverSocketObject)
{
- if(severSocketObject != NULL)
- {
- jclass jni_cid_BTServerSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothServerSocket");
- if(!jni_cid_BTServerSocket)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: jni_cid_BTServerSocket is null");
- return;
- }
+ OIC_LOG(DEBUG, TAG, "CAEDRNativeAccept - IN");
- jmethodID jni_mid_accept = (*env)->GetMethodID(env, jni_cid_BTServerSocket, "accept", "()Landroid/bluetooth/BluetoothSocket;");
- if(!jni_mid_accept)
+ if (NULL != serverSocketObject)
+ {
+ jmethodID jni_mid_accept = CAGetJNIMethodID(env, CLASSPATH_BT_SERVER_SOCKET,
+ "accept",
+ "()Landroid/bluetooth/BluetoothSocket;");
+ if (!jni_mid_accept)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: jni_mid_accept is null");
+ OIC_LOG(ERROR, TAG, "jni_mid_accept is null");
return;
}
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: initiating accept...");
+ OIC_LOG(DEBUG, TAG, "waiting for the new connection request...");
- jobject jni_obj_BTSocket = NULL;
- jni_obj_BTSocket = (*env)->CallObjectMethod(env, severSocketObject, jni_mid_accept);
- if(!jni_obj_BTSocket)
+ jobject jni_obj_BTSocket = (*env)->CallObjectMethod(env, serverSocketObject,
+ jni_mid_accept);
+ if (!jni_obj_BTSocket)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: jni_obj_BTSocket is null");
+ OIC_LOG(ERROR, TAG, "jni_obj_BTSocket is null");
- if((*env)->ExceptionCheck(env))
+ if ((*env)->ExceptionCheck(env))
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: socket might closed or timeout!!!");
+ OIC_LOG(ERROR, TAG, "socket might closed or timeout!!!");
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
return;
// get remote address
jstring j_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_BTSocket);
+ if (!j_str_address)
+ {
+ OIC_LOG(ERROR, TAG, "j_str_address is null");
+ return;
+ }
const char* address = (*env)->GetStringUTFChars(env, j_str_address, NULL);
- OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btAccept: address is %s", address);
-
- // set socket to list
- jobject jni_socket = (*env)->NewGlobalRef(env, jni_obj_BTSocket);
- CAEDRNativeAddDeviceSocketToList(env, jni_socket);
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: Accepted!!");
+ OIC_LOG_V(INFO, TAG, "accept a new connection from [%s]", address);
// update state
+ ca_mutex_lock(g_mutexStateList);
CAEDRUpdateDeviceState(STATE_CONNECTED, address);
- }
- else
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: severSocket is close previously");
- }
-}
-
-void CAEDRNativeServerSocketClose(JNIEnv *env, const char* address, uint32_t id)
-{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeServerSocketClose");
+ ca_mutex_unlock(g_mutexStateList);
- if(!CAEDRNativeIsEnableBTAdapter(env))
- {
- OIC_LOG(DEBUG, TAG, "BT adpater is not enable");
- return;
- }
+ (*env)->ReleaseStringUTFChars(env, j_str_address, address);
+ (*env)->DeleteLocalRef(env, j_str_address);
- jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
- if(!jni_cid_BTSocket)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
- return;
- }
+ // set socket to list
+ jobject jni_socket = (*env)->NewGlobalRef(env, jni_obj_BTSocket);
+ if (!jni_socket)
+ {
+ OIC_LOG(ERROR, TAG, "jni_socket is null");
+ (*env)->DeleteLocalRef(env, jni_obj_BTSocket);
+ return;
+ }
- jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
- if(!jni_mid_close)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_mid_close is null");
- return;
- }
+ ca_mutex_lock(g_mutexObjectList);
+ CAEDRNativeAddDeviceSocketToList(env, jni_socket);
+ ca_mutex_unlock(g_mutexObjectList);
- jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
- if(!jni_obj_socket)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_obj_socket is not available");
- return;
+ (*env)->DeleteGlobalRef(env, jni_socket);
+ (*env)->DeleteLocalRef(env, jni_obj_BTSocket);
}
-
- (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
-
- if((*env)->ExceptionCheck(env))
+ else
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
- (*env)->ExceptionDescribe(env);
- (*env)->ExceptionClear(env);
- return;
+ OIC_LOG(DEBUG, TAG, "serverSocket is close previously");
}
- // remove socket to list
- CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);
-
- // update state
- CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
-
- OIC_LOG(DEBUG, TAG, "[EDR][Native] close: disconnected");
+ OIC_LOG(DEBUG, TAG, "CAEDRNativeAccept - OUT");
}
/**
- * InputStream & BluetoothServerSocket will be close for Terminating
+ * InputStream & BluetoothServerSocket will be close for Terminating.
*/
void CAEDRNatvieCloseServerTask(JNIEnv* env)
{
- if(gInputStream)
- {
- OIC_LOG_V(DEBUG, TAG, "InputStream will be close");
- jclass jni_cid_InputStream = (*env)->FindClass(env, "java/io/InputStream");
- jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_InputStream, "close", "()V");
- (*env)->CallVoidMethod(env, gInputStream, jni_mid_close);
- (*env)->DeleteGlobalRef(env, gInputStream);
- }
-
- if(gSeverSocket)
+ if (g_serverSocket)
{
- OIC_LOG_V(DEBUG, TAG, "[EDR][Native] Accept Resource will be close");
+ OIC_LOG(DEBUG, TAG, "Accept Resource will be close");
- jclass jni_cid_BTServerSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothServerSocket");
- if(!jni_cid_BTServerSocket)
+ jmethodID jni_mid_close = CAGetJNIMethodID(env, CLASSPATH_BT_SERVER_SOCKET,
+ "close", "()V");
+ if (!jni_mid_close)
{
- OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_cid_BTServerSocket is null");
+ OIC_LOG(ERROR, TAG, "jni_mid_close is null");
return;
}
- jmethodID jni_mid_accept = (*env)->GetMethodID(env, jni_cid_BTServerSocket, "close", "()V");
- if(!jni_mid_accept)
- {
- OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_mid_accept is null");
- return;
- }
- (*env)->CallVoidMethod(env, gSeverSocket, jni_mid_accept);
- (*env)->DeleteGlobalRef(env, gSeverSocket);
+ (*env)->CallVoidMethod(env, g_serverSocket, jni_mid_close);
+ (*env)->DeleteGlobalRef(env, g_serverSocket);
+ g_serverSocket = NULL;
- OIC_LOG(DEBUG, TAG, "[EDR][Native] close accept obj");
+ OIC_LOG(DEBUG, TAG, "close accept obj");
}
}
-
-void CAEDRServerSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
-{
- gEDRPacketReceivedCallback = packetReceivedCallback;
-}
-