1 /* ****************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
26 #include "caedrinterface.h"
27 #include "caedrutils.h"
28 #include "caedrserver.h"
30 #include "oic_malloc.h"
31 #include "cathreadpool.h" /* for thread pool */
33 #include "uarraylist.h"
34 #include "caadapterutils.h"
35 #include "org_iotivity_ca_CaEdrInterface.h"
36 #include "oic_string.h"
39 #define TAG PCF("CA_EDR_SERVER")
40 #define MAX_PDU_BUFFER (1024)
42 static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
43 static const char CLASSPATH_BT_ADPATER[] = "android/bluetooth/BluetoothAdapter";
44 static const char CLASSPATH_BT_UUID[] = "java/util/UUID";
46 static ca_thread_pool_t g_threadPoolHandle = NULL;
51 * Mutex to synchronize socket list update.
53 static ca_mutex g_mutexSocketListManager;
56 * server socket instance.
58 static jobject g_serverSocketObject = NULL;
61 * Mutex to synchronize unicast server.
63 static ca_mutex g_mutexUnicastServer = NULL;
66 * Flag to control the Receive Unicast Data Thread.
68 static bool g_stopUnicast = false;
71 * Mutex to synchronize secure multicast server.
73 static ca_mutex g_mutexMulticastServer = NULL;
76 * Flag to control the Receive Multicast Data Thread.
78 static bool g_stopMulticast = false;
81 * Mutex to synchronize accept server.
83 static ca_mutex g_mutexAcceptServer = NULL;
86 * Flag to control the Accept Thread.
88 static bool g_stopAccept = false;
90 static jobject g_inputStream = NULL;
93 * Mutex to synchronize server socket.
95 static ca_mutex g_mutexServerSocket = NULL;
97 static jobject g_serverSocket = NULL;
100 * Mutex to synchronize device state list.
102 static ca_mutex g_mutexStateList = NULL;
105 * Mutex to synchronize device object list.
107 static ca_mutex g_mutexObjectList = NULL;
110 * Mutex to synchronize input stream.
112 static ca_mutex g_mutexInputStream = NULL;
114 typedef struct send_data
122 * Thread context information for unicast, multicast and secured unicast server.
127 CAAdapterServerType_t type;
128 } CAAdapterReceiveThreadContext_t;
133 } CAAdapterAcceptThreadContext_t;
136 * Maintains the callback to be notified when data received from remote
139 static CAEDRDataReceivedCallback g_edrPacketReceivedCallback = NULL;
141 static void CAReceiveHandler(void *data)
143 OIC_LOG(DEBUG, TAG, "IN - CAReceiveHandler..");
145 VERIFY_NON_NULL_VOID(data, TAG, "Invalid thread context");
147 bool isAttached = false;
149 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
152 OIC_LOG(DEBUG, TAG, "CAReceiveHandler - Could not get JNIEnv pointer");
153 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
157 OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
163 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) data;
165 while (true != *(ctx->stopFlag))
167 // if new socket object is added in socket list after below logic is ran.
168 // new socket will be started to read after next while loop
169 uint32_t length = CAEDRGetSocketListLength();
172 OIC_LOG(DEBUG, TAG, "socket list is empty");
177 for (idx = 0; idx < length; idx++)
179 OIC_LOG(DEBUG, TAG, "start CAEDRNativeReadData");
180 CAEDRNativeReadData(env, idx, ctx->type);
187 (*g_jvm)->DetachCurrentThread(g_jvm);
192 OIC_LOG(DEBUG, TAG, "OUT - CAReceiveHandler");
195 static void CAAcceptHandler(void *data)
199 OIC_LOG(ERROR, TAG, "[EDR] CAAcceptHandler: data is null");
203 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread start");
205 bool isAttached = false;
207 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
210 OIC_LOG(DEBUG, TAG, "CAAcceptHandler - Could not get JNIEnv pointer");
211 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
215 OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
221 jobject jni_obj_BTServerSocket = CAEDRNativeListen(env);
222 if (!jni_obj_BTServerSocket)
224 OIC_LOG(ERROR, TAG, "[EDR] AcceptThread: jni_obj_BTServerSocket is null");
228 (*g_jvm)->DetachCurrentThread(g_jvm);
234 ca_mutex_lock(g_mutexServerSocket);
235 g_serverSocket = (*env)->NewGlobalRef(env, jni_obj_BTServerSocket);
236 ca_mutex_unlock(g_mutexServerSocket);
238 CAAdapterAcceptThreadContext_t *ctx = (CAAdapterAcceptThreadContext_t *) data;
242 OIC_LOG(ERROR, TAG, "[EDR] AcceptThread: ctx is null");
245 (*g_jvm)->DetachCurrentThread(g_jvm);
250 // it should be initialized for restart accept thread
251 ca_mutex_lock(g_mutexAcceptServer);
252 g_stopAccept = false;
253 ca_mutex_unlock(g_mutexAcceptServer);
255 while (true != *(ctx->stopFlag))
257 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread running");
259 // when BT state is changed with Off. its thread will be stopped
260 if (!CAEDRNativeIsEnableBTAdapter(env))
262 OIC_LOG(DEBUG, TAG, "BT adpater is not enable");
263 ca_mutex_lock(g_mutexAcceptServer);
265 ca_mutex_unlock(g_mutexAcceptServer);
266 ca_mutex_lock(g_mutexServerSocket);
267 g_serverSocket = NULL;
268 ca_mutex_unlock(g_mutexServerSocket);
272 CAEDRNativeAccept(env, g_serverSocket);
278 (*g_jvm)->DetachCurrentThread(g_jvm);
283 OIC_LOG(DEBUG, TAG, "[EDR] AcceptThread finish");
289 * implement for adapter common method.
291 CAResult_t CAEDRServerStart(ca_thread_pool_t handle)
293 OIC_LOG(DEBUG, TAG, "IN");
294 CAEDRServerInitialize(handle);
295 CAResult_t res = CAEDRStartUnicastServer(false);
296 if (CA_STATUS_OK != res)
298 OIC_LOG(ERROR, TAG, "CAEDRStartUnicastServer failed");
299 return CA_STATUS_FAILED;
302 OIC_LOG(DEBUG, TAG, "OUT");
306 CAResult_t CAEDRServerStop()
308 OIC_LOG(DEBUG, TAG, "IN");
309 CAEDRStopUnicastServer();
310 CAEDRStopMulticastServer();
312 ca_mutex_lock(g_mutexAcceptServer);
314 ca_mutex_unlock(g_mutexAcceptServer);
318 OIC_LOG(DEBUG, TAG, "CAEDRServerStop - g_jvm is null");
319 return CA_STATUS_FAILED;
322 bool isAttached = false;
324 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
327 OIC_LOG(DEBUG, TAG, "CAEDRServerStop - Could not get JNIEnv pointer");
328 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
332 OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
333 return CA_STATUS_FAILED;
338 CAEDRNatvieCloseServerTask(env);
342 (*g_jvm)->DetachCurrentThread(g_jvm);
345 OIC_LOG(DEBUG, TAG, "OUT");
349 void CAEDRSetPacketReceivedCallback(CAEDRDataReceivedCallback packetReceivedCallback)
351 g_edrPacketReceivedCallback = packetReceivedCallback;
357 static void CAEDRServerDestroyMutex()
359 OIC_LOG(DEBUG, TAG, "IN");
361 if (g_mutexUnicastServer)
363 ca_mutex_free(g_mutexUnicastServer);
364 g_mutexUnicastServer = NULL;
367 if (g_mutexMulticastServer)
369 ca_mutex_free(g_mutexMulticastServer);
370 g_mutexMulticastServer = NULL;
373 if (g_mutexSocketListManager)
375 ca_mutex_free(g_mutexSocketListManager);
376 g_mutexSocketListManager = NULL;
379 if (g_mutexAcceptServer)
381 ca_mutex_free(g_mutexAcceptServer);
382 g_mutexAcceptServer = NULL;
385 if (g_mutexServerSocket)
387 ca_mutex_free(g_mutexServerSocket);
388 g_mutexServerSocket = NULL;
391 if (g_mutexStateList)
393 ca_mutex_free(g_mutexStateList);
394 g_mutexStateList = NULL;
397 if (g_mutexObjectList)
399 ca_mutex_free(g_mutexObjectList);
400 g_mutexObjectList = NULL;
403 if (g_mutexInputStream)
405 ca_mutex_free(g_mutexInputStream);
406 g_mutexInputStream = NULL;
409 OIC_LOG(DEBUG, TAG, "OUT");
415 static CAResult_t CAEDRServerCreateMutex()
417 OIC_LOG(DEBUG, TAG, "IN");
419 g_mutexUnicastServer = ca_mutex_new();
420 if (!g_mutexUnicastServer)
422 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
423 return CA_STATUS_FAILED;
426 g_mutexMulticastServer = ca_mutex_new();
427 if (!g_mutexMulticastServer)
429 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
431 CAEDRServerDestroyMutex();
432 return CA_STATUS_FAILED;
435 g_mutexSocketListManager = ca_mutex_new();
436 if (!g_mutexSocketListManager)
438 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
440 CAEDRServerDestroyMutex();
441 return CA_STATUS_FAILED;
444 g_mutexAcceptServer = ca_mutex_new();
445 if (!g_mutexAcceptServer)
447 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
449 CAEDRServerDestroyMutex();
450 return CA_STATUS_FAILED;
453 g_mutexServerSocket = ca_mutex_new();
454 if (!g_mutexServerSocket)
456 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
458 CAEDRServerDestroyMutex();
459 return CA_STATUS_FAILED;
462 g_mutexStateList = ca_mutex_new();
463 if (!g_mutexStateList)
465 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
467 CAEDRServerDestroyMutex();
468 return CA_STATUS_FAILED;
471 g_mutexObjectList = ca_mutex_new();
472 if (!g_mutexObjectList)
474 OIC_LOG(ERROR, TAG, "Failed to created mutex!");
476 CAEDRServerDestroyMutex();
477 return CA_STATUS_FAILED;
480 g_mutexInputStream = ca_mutex_new();
481 if (!g_mutexInputStream)
483 OIC_LOG(ERROR, TAG, "Failed to created g_mutexInputStream.");
485 CAEDRServerDestroyMutex();
486 return CA_STATUS_FAILED;
489 OIC_LOG(DEBUG, TAG, "OUT");
493 void CAEDRServerJniInit()
495 OIC_LOG(DEBUG, TAG, "CAEDRServerJniInit");
496 g_jvm = CANativeJNIGetJavaVM();
499 void CAEDRServerInitialize(ca_thread_pool_t handle)
501 OIC_LOG(DEBUG, TAG, "CAEDRServerInitialize");
503 g_threadPoolHandle = handle;
505 CAEDRServerStartAcceptThread();
507 OIC_LOG(DEBUG, TAG, "OUT");
510 void CAEDRServerStartAcceptThread()
512 CAEDRServerJniInit();
515 CAEDRServerCreateMutex();
517 bool isAttached = false;
519 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
522 OIC_LOG(DEBUG, TAG, "CAEDRServerInitialize - Could not get JNIEnv pointer");
523 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
527 OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
533 jstring jni_address = CAEDRNativeGetLocalDeviceAddress(env);
536 const char* localAddress = (*env)->GetStringUTFChars(env, jni_address, NULL);
537 OIC_LOG_V(DEBUG, TAG, "My BT Address is %s", localAddress);
538 (*env)->ReleaseStringUTFChars(env, jni_address, localAddress);
541 ca_mutex_lock(g_mutexStateList);
542 CAEDRNativeCreateDeviceStateList();
543 ca_mutex_unlock(g_mutexStateList);
545 ca_mutex_lock(g_mutexObjectList);
546 CAEDRNativeCreateDeviceSocketList();
547 ca_mutex_unlock(g_mutexObjectList);
551 (*g_jvm)->DetachCurrentThread(g_jvm);
554 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) OICMalloc(
555 sizeof(CAAdapterReceiveThreadContext_t));
558 OIC_LOG(ERROR, TAG, "Out of memory!");
562 ctx->stopFlag = &g_stopAccept;
563 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CAAcceptHandler, (void *) ctx))
565 OIC_LOG(ERROR, TAG, "Failed to create read thread!");
566 OICFree((void *) ctx);
570 OIC_LOG(DEBUG, TAG, "OUT");
573 void CAEDRServerTerminate()
575 OIC_LOG(DEBUG, TAG, "CAEDRServerTerminate");
581 bool isAttached = false;
583 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
586 OIC_LOG(DEBUG, TAG, "CAEDRServerTerminate - Could not get JNIEnv pointer");
587 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
591 OIC_LOG(ERROR, TAG, "AttachCurrentThread failed");
597 CAEDRNativeSocketCloseToAll(env);
601 (*g_jvm)->DetachCurrentThread(g_jvm);
604 CAEDRNativeRemoveAllDeviceState();
605 CAEDRNativeRemoveAllDeviceSocket(env);
608 CAEDRServerDestroyMutex();
611 CAResult_t CAEDRStartUnicastServer(bool isSecured)
613 OIC_LOG(DEBUG, TAG, "CAEDRStartUnicastServer");
615 ca_mutex_lock(g_mutexUnicastServer);
618 * The task to listen for data from unicast is added to the thread pool.
619 * This is a blocking call is made where we try to receive some data..
620 * We will keep waiting until some data is received.
621 * This task will be terminated when thread pool is freed on stopping the adapters.
622 * Thread context will be freed by thread on exit.
624 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) OICMalloc(
625 sizeof(CAAdapterReceiveThreadContext_t));
628 OIC_LOG(ERROR, TAG, "Out of memory!");
629 ca_mutex_unlock(g_mutexUnicastServer);
630 return CA_MEMORY_ALLOC_FAILED;
633 ctx->stopFlag = &g_stopUnicast;
634 ctx->type = isSecured ? CA_SECURED_UNICAST_SERVER : CA_UNICAST_SERVER;
635 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CAReceiveHandler, (void *) ctx))
637 OIC_LOG(ERROR, TAG, "Failed to create read thread!");
638 ca_mutex_unlock(g_mutexUnicastServer);
639 OICFree((void *) ctx);
640 return CA_STATUS_FAILED;
642 ca_mutex_unlock(g_mutexUnicastServer);
644 OIC_LOG(DEBUG, TAG, "OUT");
648 CAResult_t CAEDRStartMulticastServer()
650 OIC_LOG(DEBUG, TAG, "CAEDRStartMulticastServer");
652 ca_mutex_lock(g_mutexMulticastServer);
655 * The task to listen to data from multicast socket is added to the thread pool.
656 * This is a blocking call is made where we try to receive some data.
657 * We will keep waiting until some data is received.
658 * This task will be terminated when thread pool is freed on stopping the adapters.
659 * Thread context will be freed by thread on exit.
661 CAAdapterReceiveThreadContext_t *ctx = (CAAdapterReceiveThreadContext_t *) OICMalloc(
662 sizeof(CAAdapterReceiveThreadContext_t));
665 OIC_LOG(ERROR, TAG, "Out of memory!");
666 ca_mutex_unlock(g_mutexMulticastServer);
668 return CA_MEMORY_ALLOC_FAILED;
671 ctx->stopFlag = &g_stopMulticast;
672 ctx->type = CA_MULTICAST_SERVER;
674 g_stopMulticast = false;
675 if (CA_STATUS_OK != ca_thread_pool_add_task(g_threadPoolHandle, CAReceiveHandler, (void *) ctx))
677 OIC_LOG(ERROR, TAG, "thread_pool_add_task failed!");
679 g_stopMulticast = true;
680 ca_mutex_unlock(g_mutexMulticastServer);
681 OICFree((void *) ctx);
683 return CA_STATUS_FAILED;
685 ca_mutex_unlock(g_mutexMulticastServer);
687 OIC_LOG(DEBUG, TAG, "OUT");
691 CAResult_t CAEDRStopUnicastServer()
693 OIC_LOG(DEBUG, TAG, "CAEDRStopUnicastServer");
695 ca_mutex_lock(g_mutexUnicastServer);
696 g_stopUnicast = true;
697 ca_mutex_unlock(g_mutexUnicastServer);
702 CAResult_t CAEDRStopMulticastServer()
704 OIC_LOG(DEBUG, TAG, "CAEDRStopMulticastServer");
706 ca_mutex_lock(g_mutexMulticastServer);
707 g_stopMulticast = true;
708 ca_mutex_unlock(g_mutexMulticastServer);
710 OIC_LOG(INFO, TAG, "Multicast server stopped");
715 CAResult_t CAEDRNativeReadData(JNIEnv *env, uint32_t id, CAAdapterServerType_t type)
717 if (!CAEDRNativeIsEnableBTAdapter(env))
719 OIC_LOG(ERROR, TAG, "BT adpater is not enable");
720 return CA_STATUS_INVALID_PARAM;
723 if (!((*env)->ExceptionCheck(env)))
725 // check whether this socket object is connected or not.
726 jobject jni_obj_socket = CAEDRNativeGetDeviceSocket(id);
729 return CA_STATUS_INVALID_PARAM;
732 jstring jni_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_socket);
733 if (!jni_str_address)
735 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: jni_str_address is null");
736 return CA_STATUS_FAILED;
738 const char* address = (*env)->GetStringUTFChars(env, jni_str_address, NULL);
740 // check it whether is still connected or not through google api
741 jboolean ret = CAEDRIsConnectedForSocket(env, jni_obj_socket);
744 OIC_LOG(DEBUG, TAG, "[EDR][Native] this device is not connected now.let close socket");
747 // check it whether is still connected or not through socket state list
748 if (STATE_DISCONNECTED == CAEDRIsConnectedDevice(address))
750 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: it is not connected yet..");
752 // remove socket to list
753 // this code is related to below read fail exception code
754 CAEDRNativeRemoveDeviceSocket(env, jni_obj_socket);
755 (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
757 (*env)->DeleteLocalRef(env, jni_str_address);
759 return CA_STATUS_FAILED;
762 // start to read through InputStream
763 jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
764 if (!jni_cid_BTsocket)
766 (*env)->DeleteLocalRef(env, jni_str_address);
768 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: jni_cid_BTsocket is null");
769 return CA_STATUS_FAILED;
771 jmethodID jni_mid_getInputStream = (*env)->GetMethodID(env, jni_cid_BTsocket,
773 "()Ljava/io/InputStream;");
774 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: get InputStream..%d, %s", id, address);
778 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
779 (*env)->DeleteLocalRef(env, jni_str_address);
781 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_obj_socket is not available anymore..");
782 return CA_STATUS_FAILED;
785 jobject jni_obj_inputStream = (*env)->CallObjectMethod(env, jni_obj_socket,
786 jni_mid_getInputStream);
787 OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: ready inputStream..");
789 jclass jni_cid_InputStream = (*env)->FindClass(env, "java/io/InputStream");
790 if (!jni_cid_InputStream)
792 (*env)->DeleteLocalRef(env, jni_obj_inputStream);
793 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
794 (*env)->DeleteLocalRef(env, jni_str_address);
796 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: jni_cid_InputStream is null");
797 return CA_STATUS_FAILED;
799 jmethodID jni_mid_read = (*env)->GetMethodID(env, jni_cid_InputStream, "read", "([BII)I");
801 jbyteArray jbuf = (*env)->NewByteArray(env, MAX_PDU_BUFFER);
805 (*env)->DeleteLocalRef(env, jni_cid_InputStream);
806 (*env)->DeleteLocalRef(env, jni_obj_inputStream);
807 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
808 (*env)->DeleteLocalRef(env, jni_str_address);
810 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_obj_socket is not available anymore...");
811 return CA_STATUS_FAILED;
814 ca_mutex_lock(g_mutexInputStream);
817 g_inputStream = (*env)->NewGlobalRef(env, jni_obj_inputStream);
820 jint length = (*env)->CallIntMethod(env, g_inputStream, jni_mid_read, jbuf, (jint) 0,
822 ca_mutex_unlock(g_mutexInputStream);
824 OIC_LOG(DEBUG, TAG, "[EDR][Native] read something from InputStream");
828 (*env)->DeleteLocalRef(env, jni_cid_InputStream);
829 (*env)->DeleteLocalRef(env, jbuf);
830 (*env)->DeleteLocalRef(env, jni_obj_inputStream);
831 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
832 (*env)->DeleteLocalRef(env, jni_str_address);
834 OIC_LOG(ERROR, TAG, "[EDR][Native] read buffer is empty...");
835 return CA_STATUS_FAILED;
838 if ((*env)->ExceptionCheck(env))
840 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: read Error!!!");
841 (*env)->ExceptionDescribe(env);
842 (*env)->ExceptionClear(env);
844 // update state to disconnect
845 // the socket will be close next read thread routine
846 ca_mutex_lock(g_mutexStateList);
847 CAEDRUpdateDeviceState(STATE_DISCONNECTED, address);
848 ca_mutex_unlock(g_mutexStateList);
849 (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
851 (*env)->DeleteLocalRef(env, jbuf);
852 (*env)->DeleteLocalRef(env, jni_cid_InputStream);
853 (*env)->DeleteLocalRef(env, jni_obj_inputStream);
854 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
855 (*env)->DeleteLocalRef(env, jni_str_address);
857 return CA_STATUS_FAILED;
860 OIC_LOG(DEBUG, TAG, "[EDR][Native] btReadData: reading");
861 jbyte* buf = (*env)->GetByteArrayElements(env, jbuf, NULL);
864 (*env)->DeleteLocalRef(env, jni_cid_InputStream);
865 (*env)->DeleteLocalRef(env, jbuf);
866 (*env)->DeleteLocalRef(env, jni_obj_inputStream);
867 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
868 (*env)->DeleteLocalRef(env, jni_str_address);
870 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: buf is null");
871 return CA_STATUS_FAILED;
874 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btReadData: read %s, %d", buf, length);
876 char responseData[MAX_PDU_BUFFER] = { 0 };
877 memcpy(responseData, (const char*) buf, length);
881 case CA_UNICAST_SERVER:
882 case CA_MULTICAST_SERVER:
883 // Notify data to upper layer
884 if (g_edrPacketReceivedCallback)
886 uint32_t sentLength = 0;
887 OIC_LOG_V(DEBUG, TAG,"[EDR][Native] data will be sent to callback routine: \
888 %s, %d", responseData, length);
889 g_edrPacketReceivedCallback(address, (void*) responseData, length, &sentLength);
894 // Should never occur
895 OIC_LOG(ERROR, TAG, "Invalid server type");
896 return CA_STATUS_FAILED;
898 (*env)->ReleaseByteArrayElements(env, jbuf, buf, 0);
899 (*env)->ReleaseStringUTFChars(env, jni_str_address, address);
901 (*env)->DeleteLocalRef(env, jni_cid_InputStream);
902 (*env)->DeleteLocalRef(env, jbuf);
903 (*env)->DeleteLocalRef(env, jni_obj_inputStream);
904 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
905 (*env)->DeleteLocalRef(env, jni_str_address);
909 (*env)->ExceptionDescribe(env);
910 (*env)->ExceptionClear(env);
911 OIC_LOG(ERROR, TAG, "[EDR][Native] btReadData: env error!!");
912 return CA_STATUS_FAILED;
918 jboolean CAEDRIsConnectedForSocket(JNIEnv *env, jobject socket)
920 OIC_LOG(DEBUG, TAG, "[EDR][Native] CAEDRIsConnectedForSocket...");
922 if (!CAEDRNativeIsEnableBTAdapter(env))
924 OIC_LOG(ERROR, TAG, "BT adpater is not enable");
930 OIC_LOG(ERROR, TAG, "[EDR][Native] CAEDRIsConnectedForSocket - socket is null");
934 jclass jni_cid_BTsocket = (*env)->FindClass(env, "android/bluetooth/BluetoothSocket");
935 if (!jni_cid_BTsocket)
937 OIC_LOG(ERROR, TAG, "[EDR][Native] CAEDRIsConnectedForSocket - jni_cid_BTsocket is null");
941 jmethodID jni_mid_isConnected = (*env)->GetMethodID(env, jni_cid_BTsocket, "isConnected",
943 if (!jni_mid_isConnected)
945 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
947 OIC_LOG(ERROR, TAG, "[EDR][Native] CAEDRIsConnectedForSocket \
948 - jni_mid_isConnected is null.");
952 jboolean jni_isConnected = (*env)->CallBooleanMethod(env, socket, jni_mid_isConnected);
954 (*env)->DeleteLocalRef(env, jni_cid_BTsocket);
956 return jni_isConnected;
959 void CANativeStartListenTask(JNIEnv *env)
961 jobject jni_obj_BTServerSocket = CAEDRNativeListen(env);
962 if (!jni_obj_BTServerSocket)
964 OIC_LOG(ERROR, TAG, "[EDR] AcceptThread: jni_obj_BTServerSocket is null");
968 ca_mutex_lock(g_mutexServerSocket);
969 g_serverSocket = (*env)->NewGlobalRef(env, jni_obj_BTServerSocket);
970 ca_mutex_unlock(g_mutexServerSocket);
973 jobject CAEDRNativeListen(JNIEnv *env)
975 if (!CAEDRNativeIsEnableBTAdapter(env))
977 OIC_LOG(ERROR, TAG, "BT adpater is not enable");
981 OIC_LOG(DEBUG, TAG, "[EDR][Native] btListen");
983 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADPATER);
984 if (!jni_cid_BTAdapter)
986 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_cid_BTAdapter is null");
991 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
993 METHODID_OBJECTNONPARAM);
994 if (!jni_cid_BTAdapter)
996 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_cid_BTAdapter is null");
1000 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1001 jni_mid_getDefaultAdapter);
1002 if (!jni_obj_BTAdapter)
1004 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_obj_BTAdapter is null");
1008 // get listen method ID
1009 jmethodID jni_mid_listen = (*env)->GetMethodID(
1010 env, jni_cid_BTAdapter, "listenUsingInsecureRfcommWithServiceRecord",
1011 "(Ljava/lang/String;Ljava/util/UUID;)Landroid/bluetooth/BluetoothServerSocket;");
1012 if (!jni_mid_listen)
1014 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_mid_listen is null");
1017 // listenUsingInsecureRfcommWithServiceRecord / listenUsingRfcommWithServiceRecord
1019 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1022 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_mid_listen is null");
1026 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(
1027 env, jni_cid_uuid, "fromString", "(Ljava/lang/String;)Ljava/util/UUID;");
1028 if (!jni_mid_fromString)
1030 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_mid_fromString is null");
1034 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_EDR_SERVICE_ID);
1037 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_uuid is null");
1040 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
1044 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_obj_uuid is null");
1049 jstring jni_name = (*env)->NewStringUTF(env, "BluetoothTestSecure");
1052 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_name is null");
1055 jobject jni_obj_BTServerSocket = (*env)->CallObjectMethod(env, jni_obj_BTAdapter, jni_mid_listen,
1056 jni_name, jni_obj_uuid);
1057 if (!jni_obj_BTServerSocket)
1059 OIC_LOG(ERROR, TAG, "[EDR][Native] btListen: jni_obj_BTServerSocket is null");
1063 g_serverSocketObject = (*env)->NewGlobalRef(env, jni_obj_BTServerSocket);
1065 return jni_obj_BTServerSocket;
1068 void CAEDRNativeAccept(JNIEnv *env, jobject serverSocketObject)
1070 if (NULL != serverSocketObject)
1072 jclass jni_cid_BTServerSocket = (*env)->FindClass(
1073 env, "android/bluetooth/BluetoothServerSocket");
1074 if (!jni_cid_BTServerSocket)
1076 OIC_LOG(ERROR, TAG, "[EDR][Native] btAccept: jni_cid_BTServerSocket is null");
1080 jmethodID jni_mid_accept = (*env)->GetMethodID(env, jni_cid_BTServerSocket, "accept",
1081 "()Landroid/bluetooth/BluetoothSocket;");
1082 if (!jni_mid_accept)
1084 OIC_LOG(ERROR, TAG, "[EDR][Native] btAccept: jni_mid_accept is null");
1088 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: initiating accept...");
1090 jobject jni_obj_BTSocket = (*env)->CallObjectMethod(env, serverSocketObject,
1092 if (!jni_obj_BTSocket)
1094 OIC_LOG(ERROR, TAG, "[EDR][Native] btAccept: jni_obj_BTSocket is null");
1096 if ((*env)->ExceptionCheck(env))
1098 OIC_LOG(ERROR, TAG, "[EDR][Native] btAccept: socket might closed or timeout!!!");
1099 (*env)->ExceptionDescribe(env);
1100 (*env)->ExceptionClear(env);
1105 // get remote address
1106 jstring j_str_address = CAEDRNativeGetAddressFromDeviceSocket(env, jni_obj_BTSocket);
1109 OIC_LOG(ERROR, TAG, "[EDR][Native] btAccept: j_str_address is null");
1113 const char* address = (*env)->GetStringUTFChars(env, j_str_address, NULL);
1114 OIC_LOG_V(DEBUG, TAG, "[EDR][Native] btAccept: address is %s", address);
1116 // set socket to list
1117 jobject jni_socket = (*env)->NewGlobalRef(env, jni_obj_BTSocket);
1118 ca_mutex_lock(g_mutexObjectList);
1119 CAEDRNativeAddDeviceSocketToList(env, jni_socket);
1120 ca_mutex_unlock(g_mutexObjectList);
1122 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: Accepted!!");
1125 ca_mutex_lock(g_mutexStateList);
1126 CAEDRUpdateDeviceState(STATE_CONNECTED, address);
1127 ca_mutex_unlock(g_mutexStateList);
1128 (*env)->ReleaseStringUTFChars(env, j_str_address, address);
1132 OIC_LOG(DEBUG, TAG, "[EDR][Native] btAccept: serverSocket is close previously");
1137 * InputStream & BluetoothServerSocket will be close for Terminating.
1139 void CAEDRNatvieCloseServerTask(JNIEnv* env)
1143 OIC_LOG(DEBUG, TAG, "InputStream will be close");
1144 jclass jni_cid_InputStream = (*env)->FindClass(env, "java/io/InputStream");
1145 jmethodID jni_mid_close = (*env)->GetMethodID(env, jni_cid_InputStream, "close", "()V");
1146 (*env)->CallVoidMethod(env, g_inputStream, jni_mid_close);
1147 (*env)->DeleteGlobalRef(env, g_inputStream);
1152 OIC_LOG(DEBUG, TAG, "[EDR][Native] Accept Resource will be close");
1154 jclass jni_cid_BTServerSocket = (*env)->FindClass(
1155 env, "android/bluetooth/BluetoothServerSocket");
1156 if (!jni_cid_BTServerSocket)
1158 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_cid_BTServerSocket is null");
1162 jmethodID jni_mid_accept = (*env)->GetMethodID(env, jni_cid_BTServerSocket, "close", "()V");
1163 if (!jni_mid_accept)
1165 OIC_LOG(ERROR, TAG, "[EDR][Native] jni_mid_accept is null");
1168 (*env)->CallVoidMethod(env, g_serverSocket, jni_mid_accept);
1169 (*env)->DeleteGlobalRef(env, g_serverSocket);
1171 OIC_LOG(DEBUG, TAG, "[EDR][Native] close accept obj");