5 #include "caedrinterface.h"
6 #include "caedrutils.h"
7 #include "caedrserver.h"
9 #include "oic_malloc.h"
10 #include "uthreadpool.h" /* for thread pool */
12 #include "uarraylist.h"
13 #include "caadapterutils.h"
16 #define TAG PCF("CA_EDR_SERVER")
18 static const char *METHODID_OBJECTNONPARAM = "()Landroid/bluetooth/BluetoothAdapter;";
19 static const char *METHODID_STRINGNONPARAM = "()Ljava/lang/String;";
20 static const char *CLASSPATH_BT_ADPATER = "android/bluetooth/BluetoothAdapter";
21 static const char *CLASSPATH_BT_UUID = "java/util/UUID";
23 static const uint32_t STATE_CONNECTED = 1;
24 static const uint32_t STATE_DISCONNECTED = 0;
26 static const uint32_t MAX_PDU_BUFFER = 1024;
28 static u_arraylist_t *gdeviceStateList = NULL;
29 static u_arraylist_t *gdeviceObjectList = NULL;
31 static u_thread_pool_t gThreadPoolHandle = NULL;
34 static jobject gContext;
36 static jbyteArray gSendBuffer;
39 * @var gMutexSocketListManager
40 * @brief Mutex to synchronize socket list update
42 static u_mutex gMutexSocketListManager;
44 // server socket instance
45 static jobject gServerSocketObject = NULL;
48 * @var gMutexUnicastServer
49 * @brief Mutex to synchronize unicast server
51 static u_mutex gMutexUnicastServer = NULL;
55 * @brief Flag to control the Receive Unicast Data Thread
57 static bool gStopUnicast = FALSE;
60 * @var gMutexMulticastServer
61 * @brief Mutex to synchronize secure multicast server
63 static u_mutex gMutexMulticastServer = NULL;
67 * @brief Flag to control the Receive Multicast Data Thread
69 static bool gStopMulticast = FALSE;
73 * @brief Flag to control the Accept Thread
75 static bool gStopAccept = FALSE;
77 typedef struct send_data {
84 @brief Thread context information for unicast, multicast and secured unicast server
89 CAAdapterServerType_t type;
90 } CAAdapterReceiveThreadContext_t;
95 } CAAdapterAcceptThreadContext_t;
100 * @var gEDRNetworkChangeCallback
101 * @brief Maintains the callback to be notified when data received from remote Bluetooth device
103 static CAEDRDataReceivedCallback gEDRPacketReceivedCallback = NULL;
105 static void CAReceiveHandler(void *data)
107 OIC_LOG_V(DEBUG, TAG, "start CAReceiveHandler..");
108 OIC_LOG(DEBUG, TAG, "IN");
110 VERIFY_NON_NULL_VOID(data, TAG, "Invalid thread context");
112 jboolean isAttached = FALSE;
114 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
117 OIC_LOG_V(DEBUG, TAG, "CAReceiveHandler - Could not get JNIEnv pointer");
118 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
122 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
128 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)data;
130 while (TRUE != *(ctx->stopFlag))
134 // if new socket object is added in socket list after below logic is ran.
135 // new socket will be started to read after next while loop
136 uint32_t length = CAEDRServerGetSocketListLength();
139 OIC_LOG_V(DEBUG, TAG, "socket list is empty");
143 // for(idx = 0 ; idx < length ; idx++)
145 // OIC_LOG_V(DEBUG, TAG, "start CAEDRNativeReadData");
146 CAEDRNativeReadData(env, idx, ctx->type);
151 (*g_jvm)->DetachCurrentThread(g_jvm);
156 OIC_LOG(DEBUG, TAG, "OUT");
159 static void CAAcceptHandler(void *data)
163 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread start");
165 jboolean isAttached = FALSE;
167 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
170 OIC_LOG_V(DEBUG, TAG, "CAAcceptHandler - Could not get JNIEnv pointer");
171 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
175 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
181 jobject jni_obj_BTSeverSocket = CAEDRNativeListen(env);
182 if(!jni_obj_BTSeverSocket) {
183 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread: jni_obj_BTSeverSocket is null");
186 (*g_jvm)->DetachCurrentThread(g_jvm);
191 CAAdapterAcceptThreadContext_t *ctx = (CAAdapterAcceptThreadContext_t *)data;
193 while (TRUE != *(ctx->stopFlag))
195 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread running");
196 CAEDRNativeAccept(env, jni_obj_BTSeverSocket);
200 (*g_jvm)->DetachCurrentThread(g_jvm);
205 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread finish");
211 // adapter common method
213 CAResult_t CAEDRServerStart(const char *serviceUUID, int32_t *serverFD, u_thread_pool_t handle)
215 OIC_LOG_V(DEBUG, TAG, "IN");
216 CAEDRServerInitialize(handle);
218 CAEDRStartUnicastServer(NULL, FALSE);
219 OIC_LOG_V(DEBUG, TAG, "OUT");
223 CAResult_t CAEDRServerStop(const int32_t serverFD)
225 OIC_LOG_V(DEBUG, TAG, "IN");
226 CAEDRStopUnicastServer(-1);
227 CAEDRStopMulticastServer(-1);
228 CAEDRServerTerminate();
229 OIC_LOG_V(DEBUG, TAG, "OUT");
233 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
235 gEDRPacketReceivedCallback = packetReceivedCallback;
241 static void CAEDRServerDestroyMutex()
243 OIC_LOG(DEBUG, TAG, "IN");
245 if (gMutexUnicastServer)
247 u_mutex_free(gMutexUnicastServer);
248 gMutexUnicastServer = NULL;
255 if (gMutexMulticastServer)
257 u_mutex_free(gMutexMulticastServer);
258 gMutexMulticastServer = NULL;
261 if(gMutexSocketListManager)
263 u_mutex_free(gMutexSocketListManager);
264 gMutexSocketListManager = NULL;
267 OIC_LOG(DEBUG, TAG, "OUT");
274 static CAResult_t CAEDRServerCreateMutex()
276 OIC_LOG(DEBUG, TAG, "IN");
278 gMutexUnicastServer = u_mutex_new();
279 if (!gMutexUnicastServer)
281 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
282 return CA_STATUS_FAILED;
289 gMutexMulticastServer = u_mutex_new();
290 if (!gMutexMulticastServer)
292 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
294 CAEDRServerDestroyMutex();
295 return CA_STATUS_FAILED;
298 gMutexSocketListManager = u_mutex_new();
299 if (!gMutexSocketListManager)
301 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
303 CAEDRServerDestroyMutex();
304 return CA_STATUS_FAILED;
307 OIC_LOG(DEBUG, TAG, "OUT");
311 void CAEDRServerJniInit(JNIEnv *env, JavaVM* jvm)
313 OIC_LOG_V(DEBUG, TAG, "CAEDRServerJniInit");
317 void CAEDRServerInitialize(u_thread_pool_t handle)
319 OIC_LOG(DEBUG, TAG, "CAEDRServerInitialize");
321 gThreadPoolHandle = handle;
324 CAEDRServerCreateMutex();
326 jboolean isAttached = FALSE;
328 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
331 OIC_LOG_V(DEBUG, TAG, "CAEDRServerInitialize - Could not get JNIEnv pointer");
332 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
336 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
342 jstring jni_address = CAEDRNativeGetLocalDeviceAddress(env);
345 const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
346 OIC_LOG_V(DEBUG, TAG, "My BT Address is %s", localAddress);
349 CAEDRServerNativeCreateDeviceStateList();
350 CAEDRServerNativeCreateDeviceSocketList();
353 (*g_jvm)->DetachCurrentThread(g_jvm);
355 // CAEDRCreateJNIInterfaceObject(gContext); /* create java CAEDRInterface instance*/
357 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
358 OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
361 OIC_LOG(ERROR, TAG, "Out of memory!");
365 ctx->stopFlag = &gStopAccept;
366 if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, CAAcceptHandler, (void *)ctx))
368 OIC_LOG(ERROR, TAG, "Failed to create read thread!");
369 OICFree((void *)ctx);
373 OIC_LOG(DEBUG, TAG, "OUT");
376 void CAEDRServerTerminate()
378 OIC_LOG(DEBUG, TAG, "CAEDRServerTerminate");
380 jboolean isAttached = FALSE;
382 jint res = (*g_jvm)->GetEnv(g_jvm, (void**)&env, JNI_VERSION_1_6);
385 OIC_LOG_V(DEBUG, TAG, "CAEDRServerTerminate - Could not get JNIEnv pointer");
386 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
390 OIC_LOG_V(DEBUG, TAG, "AttachCurrentThread failed");
397 gStopMulticast = TRUE;
401 (*g_jvm)->DetachCurrentThread(g_jvm);
404 CAEDRServerDestroyMutex();
406 CAEDRServerNativeRemoveAllDeviceState();
407 CAEDRServerNativeRemoveAllDeviceSocket(env);
410 int32_t CAEDRStartUnicastServer(const char* address, bool isSecured)
412 OIC_LOG_V(DEBUG, TAG, "CAEDRStartUnicastServer(%s)", address);
414 u_mutex_lock(gMutexUnicastServer);
417 * The task to listen for data from unicast is added to the thread pool.
418 * This is a blocking call is made where we try to receive some data..
419 * We will keep waiting until some data is received.
420 * This task will be terminated when thread pool is freed on stopping the adapters.
421 * Thread context will be freed by thread on exit.
423 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
424 OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
427 OIC_LOG(ERROR, TAG, "Out of memory!");
428 u_mutex_unlock(gMutexUnicastServer);
429 return CA_MEMORY_ALLOC_FAILED;
432 ctx->stopFlag = &gStopUnicast;
433 ctx->type = isSecured ? CA_SECURED_UNICAST_SERVER : CA_UNICAST_SERVER;
434 if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, CAReceiveHandler, (void *)ctx))
436 OIC_LOG(ERROR, TAG, "Failed to create read thread!");
437 u_mutex_unlock(gMutexUnicastServer);
438 OICFree((void *)ctx);
439 return CA_STATUS_FAILED;
441 u_mutex_unlock(gMutexUnicastServer);
443 OIC_LOG(DEBUG, TAG, "OUT");
447 int32_t CAEDRStartMulticastServer(bool isSecured)
449 OIC_LOG_V(DEBUG, TAG, "CAEDRStartMulticastServer");
451 u_mutex_lock(gMutexMulticastServer);
454 * The task to listen to data from multicast socket is added to the thread pool.
455 * This is a blocking call is made where we try to receive some data.
456 * We will keep waiting until some data is received.
457 * This task will be terminated when thread pool is freed on stopping the adapters.
458 * Thread context will be freed by thread on exit.
460 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *)
461 OICMalloc(sizeof(CAAdapterReceiveThreadContext_t));
464 OIC_LOG(ERROR, TAG, "Out of memory!");
465 u_mutex_unlock(gMutexMulticastServer);
467 return CA_MEMORY_ALLOC_FAILED;
470 ctx->stopFlag = &gStopMulticast;
471 ctx->type = CA_MULTICAST_SERVER;
473 gStopMulticast = FALSE;
474 if (CA_STATUS_OK != u_thread_pool_add_task(gThreadPoolHandle, CAReceiveHandler, (void *)ctx))
476 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed!");
478 gStopMulticast = TRUE;
479 u_mutex_unlock(gMutexMulticastServer);
481 return CA_STATUS_FAILED;
483 u_mutex_unlock(gMutexMulticastServer);
485 OIC_LOG(DEBUG, TAG, "OUT");
489 int32_t CAEDRStopUnicastServer(int32_t serverID)
491 OIC_LOG(DEBUG, TAG, "CAEDRStopUnicastServer");
493 u_mutex_lock(gMutexUnicastServer);
495 u_mutex_unlock(gMutexUnicastServer);
500 int32_t CAEDRStopMulticastServer(int32_t serverID)
502 OIC_LOG(DEBUG, TAG, "CAEDRStopMulticastServer");
504 u_mutex_lock(gMutexMulticastServer);
505 gStopMulticast = true;
506 u_mutex_unlock(gMutexMulticastServer);
508 OIC_LOG_V(INFO, TAG, "Multicast server stopped");
513 uint32_t CAEDRNativeReadData(JNIEnv *env, uint32_t id, CAAdapterServerType_t type)
515 if(!((*env)->ExceptionCheck(env)))
517 // check whether this socket object is connected or not.
518 jobject jni_obj_socket = CAEDRServerNativeGetDeviceSocket(id);
524 jstring jni_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_socket);
525 const char* address = (*env)->GetStringUTFChars(env, jni_str_address, NULL);
526 if(STATE_DISCONNECTED == CAEDRServerIsConnectedDevice(address))
528 OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: it is not connected yet..");
532 // start to read through InputStream
533 jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
534 jmethodID jni_mid_getInputStream = (*env)->GetMethodID(env, jni_cid_BTsocket, "getInputStream", "()Ljava/io/InputStream;");
535 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: get InputStream..%d, %s", id, address);
539 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_socket is not available anymore..");
543 jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, jni_obj_socket, jni_mid_getInputStream);
544 OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: ready inputStream..");
546 jclass jni_cid_InputStream = (*env)->FindClass(env, "java/io/InputStream");
547 jmethodID jni_mid_read = (*env)->GetMethodID(env, jni_cid_InputStream, "read", "([BII)I");
549 jbyteArray jbuf = (*env)->NewByteArray(env, MAX_PDU_BUFFER);
554 OIC_LOG(DEBUG, TAG, "[EDR][Native] jni_obj_socket is not available anymore...");
558 while ((nread = (*env)->CallIntMethod(env, jni_obj_inputStream, jni_mid_read, jbuf, (jint)0, MAX_PDU_BUFFER)) != -1)
560 if((*env)->ExceptionCheck(env))
562 OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: read Error!!!");
563 (*env)->ExceptionDescribe(env);
564 (*env)->ExceptionClear(env);
567 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: reading");
568 jbyte* buf = (*env)->GetByteArrayElements(env, jbuf, NULL);
569 jint length = strlen((char*)buf);
570 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: read %s", buf);
578 case CA_UNICAST_SERVER:
579 case CA_MULTICAST_SERVER:
580 // Notify data to upper layer
581 if (gEDRPacketReceivedCallback)
583 uint32_t sentLength = 0;
584 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] data will be sent to callback routine: %s, %d", buf, length);
585 gEDRPacketReceivedCallback(address, (void*)buf, length, &sentLength);
589 case CA_SECURED_UNICAST_SERVER:
591 #endif //__WITH_DTLS__
593 // Should never occur
594 OIC_LOG_V(DEBUG, TAG, "Invalid server type");
597 (*env)->ReleaseByteArrayElements(env, jbuf, buf, 0);
599 // close socket after data was sent
600 CAEDRNativeServerSocketClose(env, address, id);
606 (*env)->ExceptionDescribe(env);
607 (*env)->ExceptionClear(env);
608 OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: env error!!");
615 jobject CAEDRNativeListen(JNIEnv *env)
617 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen");
619 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
620 if(!jni_cid_BTAdapter)
622 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_cid_BTAdapter is null");
627 jmethodID jni_mid_getDefaultAdapter =(*env)->GetStaticMethodID(env, jni_cid_BTAdapter, "getDefaultAdapter", METHODID_OBJECTNONPARAM);
628 if(!jni_cid_BTAdapter)
630 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_cid_BTAdapter is null");
634 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter, jni_mid_getDefaultAdapter);
635 if(!jni_obj_BTAdapter)
637 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_obj_BTAdapter is null");
641 // get listen method ID
642 jmethodID jni_mid_listen = (*env)->GetMethodID(env, jni_cid_BTAdapter,
643 "listenUsingInsecureRfcommWithServiceRecord","(Ljava/lang/String;Ljava/util/UUID;)Landroid/bluetooth/BluetoothServerSocket;");
646 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_mid_listen is null");
649 // listenUsingInsecureRfcommWithServiceRecord / listenUsingRfcommWithServiceRecord
651 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
654 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_mid_listen is null");
658 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
659 if(!jni_mid_fromString)
661 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_mid_fromString is null");
665 // jstring uuid = (*env)->NewStringUTF(env, "0000111f-0000-1000-8000-00805f9b8033");
666 jstring jni_uuid = (*env)->NewStringUTF(env, "00000000-0000-0000-0000-0000cdab0000");
667 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString, jni_uuid);
670 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_obj_uuid is null");
675 jstring jni_name = (*env)->NewStringUTF(env, "BluetoothTestSecure");
676 jobject jni_obj_BTSeverSocket = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_listen, jni_name, jni_obj_uuid);
677 if(!jni_obj_BTSeverSocket)
679 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen: jni_obj_BTSeverSocket is null");
683 gServerSocketObject = (*env)->NewGlobalRef(env, jni_obj_BTSeverSocket);
685 return jni_obj_BTSeverSocket;
688 void CAEDRNativeAccept(JNIEnv *env, jobject severSocketObject)
690 if(severSocketObject != NULL)
692 jclass jni_cid_BTServerSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothServerSocket");
693 if(!jni_cid_BTServerSocket)
695 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: jni_cid_BTServerSocket is null");
699 jmethodID jni_mid_accept = (*env)->GetMethodID(env, jni_cid_BTServerSocket, "accept", "()Landroid/bluetooth/BluetoothSocket;");
702 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: jni_mid_accept is null");
706 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: initiating accept...");
708 jobject jni_obj_BTSocket = NULL;
709 jni_obj_BTSocket = (*env)->CallObjectMethod(env, severSocketObject, jni_mid_accept);
710 if(!jni_obj_BTSocket)
712 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: jni_obj_BTSocket is null");
716 // get remote address
717 jstring j_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_BTSocket);
719 const char* address = (*env)->GetStringUTFChars(env, j_str_address, NULL);
720 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btAccept: address is %s", address);
722 // set socket to list
723 jobject jni_socket = (*env)->NewGlobalRef(env, jni_obj_BTSocket);
724 CAEDRServerNativeAddDeviceSocketToList(env, jni_socket);
726 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: Accepted!!");
729 CAEDRServerUpdateDeviceState(STATE_CONNECTED, address);
733 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: severSocket is close previously");
737 void CAEDRNativeServerSocketClose(JNIEnv *env, const char* address, uint32_t id)
740 jclass jni_cid_BTSocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
741 if(!jni_cid_BTSocket)
743 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_cid_BTSocket is null");
747 jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_BTSocket, "close", "()V");
750 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_mid_close is null");
754 jobject jni_obj_socket = CAEDRServerNativeGetDeviceSocket(id);
757 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: jni_obj_socket is not available");
761 (*env)->CallVoidMethod(env, jni_obj_socket, jni_mid_close);
763 if((*env)->ExceptionCheck(env))
765 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: close is Failed!!!");
766 (*env)->ExceptionDescribe(env);
767 (*env)->ExceptionClear(env);
771 // remove socket to list
772 CAEDRServerNativeRemoveDeviceSocket(env, jni_obj_socket);
775 CAEDRServerUpdateDeviceState(STATE_DISCONNECTED, address);
777 OIC_LOG(DEBUG, TAG, "[EDR][Native] close: disconnected");
783 void CAEDRServerNativeCreateDeviceStateList()
785 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRNativeCreateDeviceStateList");
787 // create new object array
788 if (gdeviceStateList == NULL)
790 OIC_LOG_V(DEBUG, TAG, "Create device list");
792 gdeviceStateList = u_arraylist_create();
796 void CAEDRServerUpdateDeviceState(uint32_t state, const char* address)
798 state_t *newstate = (state_t*) OICMalloc( sizeof(state_t) );
799 memset(newstate->address, 0, strlen(newstate->address));
800 strcpy(newstate->address, address);
801 newstate->state = state;
803 CAEDRServerNativeAddDeviceStateToList(newstate);
806 void CAEDRServerNativeAddDeviceStateToList(state_t* state)
810 OIC_LOG(DEBUG, TAG, "[EDR][Native] device is null");
814 if(!gdeviceStateList)
816 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdevice_list is null");
820 if(!CAEDRServerNativeIsDeviceInList(state->address)) {
821 CAEDRServerNativeRemoveDevice(state->address); // delete previous state for update new state
822 u_arraylist_add(gdeviceStateList, state); // update new state
823 OIC_LOG_V(DEBUG, TAG, "Set State to Connected State List");
827 jboolean CAEDRServerNativeIsDeviceInList(const char* remoteAddress){
830 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
832 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
835 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
839 if(!strcmp(remoteAddress, state->address))
841 OIC_LOG_V(DEBUG, TAG, "the device is already set");
850 OIC_LOG_V(DEBUG, TAG, "there are no the device in list.");
854 void CAEDRServerNativeRemoveAllDeviceState()
856 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveAllDevices");
858 if(!gdeviceStateList)
860 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
865 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
867 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
870 OIC_LOG(DEBUG, TAG, "[EDR][Native] jarrayObj is null");
876 OICFree(gdeviceStateList);
877 gdeviceStateList = NULL;
881 void CAEDRServerNativeRemoveDevice(const char* remoteAddress)
883 OIC_LOG_V(DEBUG, TAG, "CAEDRNativeRemoveDevice");
885 if(!gdeviceStateList)
887 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
892 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
894 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
897 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
901 if(!strcmp(state->address, remoteAddress))
903 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] remove object : %s", remoteAddress);
906 CAEDRServerReorderingDeviceList(index);
910 OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
914 jboolean CAEDRServerIsConnectedDevice(const char* remoteAddress)
916 OIC_LOG_V(DEBUG, TAG, "CAEDRServerIsConnectedDevice");
918 if(!gdeviceStateList)
920 OIC_LOG(DEBUG, TAG, "[EDR][Native] gdeviceStateList is null");
925 for (index = 0; index < u_arraylist_length(gdeviceStateList); index++)
927 state_t* state = (state_t*) u_arraylist_get(gdeviceStateList, index);
930 OIC_LOG(DEBUG, TAG, "[EDR][Native] state_t object is null");
934 if(!strcmp(state->address, remoteAddress))
936 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] check whether it is connected or not");
941 OIC_LOG(DEBUG, TAG, "[EDR][Native] there are no target object");
945 void CAEDRServerReorderingDeviceList(uint32_t index)
947 if (index >= gdeviceStateList->length)
952 if (index < gdeviceStateList->length - 1)
954 memmove(&gdeviceStateList->data[index], &gdeviceStateList->data[index + 1],
955 (gdeviceStateList->length - index - 1) * sizeof(void *));
958 gdeviceStateList->size--;
959 gdeviceStateList->length--;
963 * Device Socket Object List
965 void CAEDRServerNativeCreateDeviceSocketList()
967 OIC_LOG(DEBUG, TAG, "[BLE][Native] CAEDRServerNativeCreateDeviceSocketList");
969 // create new object array
970 if (gdeviceObjectList == NULL)
972 OIC_LOG_V(DEBUG, TAG, "Create Device object list");
974 gdeviceObjectList = u_arraylist_create();
978 void CAEDRServerNativeAddDeviceSocketToList(JNIEnv *env, jobject deviceSocket)
980 OIC_LOG(DEBUG, TAG, "[BLE][Native] CAEDRServerNativeAddDeviceSocketToList");
984 OIC_LOG(DEBUG, TAG, "[BLE][Native] Device is null");
988 if(!gdeviceObjectList)
990 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
994 jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
995 if(!jni_remoteAddress)
997 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1001 u_mutex_lock(gMutexSocketListManager);
1003 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1005 if(!CAEDRServerNativeIsDeviceSocketInList(env, remoteAddress))
1007 jobject gDeviceSocker = (*env)->NewGlobalRef(env, deviceSocket);
1008 u_arraylist_add(gdeviceObjectList, gDeviceSocker);
1009 OIC_LOG_V(DEBUG, TAG, "Set Object to Array as Element");
1012 u_mutex_unlock(gMutexSocketListManager);
1015 jboolean CAEDRServerNativeIsDeviceSocketInList(JNIEnv *env, const char* remoteAddress)
1017 OIC_LOG(DEBUG, TAG, "[BLE][Native] CANativeIsDeviceObjInList");
1020 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
1023 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
1026 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1030 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
1033 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1037 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1039 if(!strcmp(remoteAddress, setAddress))
1041 OIC_LOG_V(DEBUG, TAG, "the device is already set");
1050 OIC_LOG_V(DEBUG, TAG, "there are no the Device obejct in list. we can add");
1054 void CAEDRServerNativeRemoveAllDeviceSocket(JNIEnv *env)
1056 OIC_LOG_V(DEBUG, TAG, "CANativeRemoveAllDeviceObjsList");
1058 if(!gdeviceObjectList)
1060 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1065 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
1067 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
1070 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1073 (*env)->DeleteGlobalRef(env, jarrayObj);
1076 OICFree(gdeviceObjectList);
1077 gdeviceObjectList = NULL;
1081 void CAEDRServerNativeRemoveDeviceSocket(JNIEnv *env, jobject deviceSocket)
1083 OIC_LOG_V(DEBUG, TAG, "CAEDRServerNativeRemoveDeviceSocket");
1085 if(!gdeviceObjectList)
1087 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1091 u_mutex_lock(gMutexSocketListManager);
1094 for (index = 0; index < u_arraylist_length(gdeviceObjectList); index++)
1096 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, index);
1099 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is null");
1103 jstring jni_setAddress = CAEDRNativeGetAddressFromDeviceSocket(env, jarrayObj);
1106 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_setAddress is null");
1109 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
1111 jstring jni_remoteAddress = CAEDRNativeGetAddressFromDeviceSocket(env, deviceSocket);
1112 if(!jni_remoteAddress)
1114 OIC_LOG(DEBUG, TAG, "[BLE][Native] jni_remoteAddress is null");
1117 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
1119 if(!strcmp(setAddress, remoteAddress))
1121 OIC_LOG_V(DEBUG, TAG, "[BLE][Native] remove object : %s", remoteAddress);
1122 (*env)->DeleteGlobalRef(env, jarrayObj);
1124 CAEDRServerReorderingDeviceSocketList(index);
1128 u_mutex_unlock(gMutexSocketListManager);
1130 OIC_LOG(DEBUG, TAG, "[BLE][Native] there are no target object");
1134 jobject CAEDRServerNativeGetDeviceSocket(uint32_t idx)
1136 OIC_LOG_V(DEBUG, TAG, "CAEDRServerNativeGetDeviceSocket");
1140 OIC_LOG(DEBUG, TAG, "[BLE][Native] index is not available");
1144 if(!gdeviceObjectList)
1146 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1150 jobject jarrayObj = (jobject) u_arraylist_get(gdeviceObjectList, idx);
1153 OIC_LOG(DEBUG, TAG, "[BLE][Native] jarrayObj is not available");
1159 uint32_t CAEDRServerGetSocketListLength()
1161 if(!gdeviceObjectList)
1163 OIC_LOG(DEBUG, TAG, "[BLE][Native] gdeviceObjectList is null");
1167 uint32_t length = u_arraylist_length(gdeviceObjectList);
1172 void CAEDRServerReorderingDeviceSocketList(uint32_t index)
1174 if (index >= gdeviceObjectList->length)
1179 if (index < gdeviceObjectList->length - 1)
1181 memmove(&gdeviceObjectList->data[index], &gdeviceObjectList->data[index + 1],
1182 (gdeviceObjectList->length - index - 1) * sizeof(void *));
1185 gdeviceObjectList->size--;
1186 gdeviceObjectList->length--;
1189 void CAEDRServerSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
1191 gEDRPacketReceivedCallback = packetReceivedCallback;