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 "caleclient.h"
27 #include "caleserver.h"
28 #include "caleutils.h"
29 #include "caleinterface.h"
30 #include "caadapterutils.h"
33 #include "oic_malloc.h"
34 #include "cathreadpool.h" /* for thread pool */
36 #include "uarraylist.h"
37 #include "org_iotivity_ca_CaLeClientInterface.h"
39 #define TAG PCF("CA_LE_CLIENT")
41 static const char METHODID_OBJECTNONPARAM[] = "()Landroid/bluetooth/BluetoothAdapter;";
42 static const char CLASSPATH_BT_ADAPTER[] = "android/bluetooth/BluetoothAdapter";
43 static const char CLASSPATH_BT_UUID[] = "java/util/UUID";
44 static const char CLASSPATH_BT_GATT[] = "android/bluetooth/BluetoothGatt";
47 static u_arraylist_t *g_deviceList = NULL; // device list to have same UUID
48 static u_arraylist_t *g_gattObjectList = NULL;
49 static u_arraylist_t *g_deviceStateList = NULL;
51 static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
52 static CABLEErrorHandleCallback g_clientErrorCallback;
53 static ca_thread_pool_t g_threadPoolHandle = NULL;
54 static jobject g_leScanCallback = NULL;
55 static jobject g_leGattCallback = NULL;
56 static jobject g_context = NULL;
57 static jobjectArray g_uuidList = NULL;
59 // it will be prevent to start send logic when adapter has stopped.
60 static bool g_isStartedLEClient = false;
61 static bool g_isStartedMulticastServer = false;
62 static bool g_isStartedScan = false;
64 static jbyteArray g_sendBuffer = NULL;
65 static uint32_t g_targetCnt = 0;
66 static uint32_t g_currentSentCnt = 0;
67 static bool g_isFinishedSendData = false;
68 static ca_mutex g_SendFinishMutex = false;
69 static ca_mutex g_threadMutex = NULL;
70 static ca_cond g_threadCond = NULL;
72 static bool g_isRequestedSend = false;
73 static bool g_isReceivedWriteCB = false;
74 static ca_mutex g_writeCharacteristicCBMutex = false;
75 static ca_mutex g_theSendRequestMutex = false;
76 static ca_mutex g_threadSendCBMutex = NULL;
77 static ca_cond g_threadSendCBCond = NULL;
79 static ca_mutex g_threadSendMutex = NULL;
80 static ca_cond g_threadSendCond = NULL;
82 static ca_mutex g_bleReqRespClientCbMutex = NULL;
83 static ca_mutex g_bleServerBDAddressMutex = NULL;
85 static ca_mutex g_deviceListMutex = NULL;
86 static ca_mutex g_gattObjectMutex = NULL;
87 static ca_mutex g_deviceStateListMutex = NULL;
89 static CABLEClientDataReceivedCallback g_CABLEClientDataReceivedCallback = NULL;
92 void CALEClientJniInit()
94 OIC_LOG(DEBUG, TAG, "CALEClientJniInit");
95 g_jvm = (JavaVM*) CANativeJNIGetJavaVM();
98 void CALEClientJNISetContext()
100 OIC_LOG(DEBUG, TAG, "CALEClientJNISetContext");
101 g_context = (jobject) CANativeJNIGetContext();
104 CAResult_t CALECreateJniInterfaceObject()
106 OIC_LOG(DEBUG, TAG, "CALECreateJniInterfaceObject");
110 OIC_LOG(ERROR, TAG, "g_context is null");
111 return CA_STATUS_FAILED;
116 OIC_LOG(ERROR, TAG, "g_jvm is null");
117 return CA_STATUS_FAILED;
120 bool isAttached = false;
122 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
125 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
126 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
130 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
131 return CA_STATUS_FAILED;
136 jclass jni_LEInterface = (*env)->FindClass(env, "org/iotivity/ca/CaLeClientInterface");
137 if (!jni_LEInterface)
139 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface class");
143 jmethodID LeInterfaceConstructorMethod = (*env)->GetMethodID(env, jni_LEInterface, "<init>",
144 "(Landroid/content/Context;)V");
145 if (!LeInterfaceConstructorMethod)
147 OIC_LOG(ERROR, TAG, "Could not get CaLeClientInterface constructor method");
151 (*env)->NewObject(env, jni_LEInterface, LeInterfaceConstructorMethod, g_context);
152 OIC_LOG(DEBUG, TAG, "Create instance for CaLeClientInterface");
156 (*g_jvm)->DetachCurrentThread(g_jvm);
165 (*g_jvm)->DetachCurrentThread(g_jvm);
168 return CA_STATUS_FAILED;
171 CAResult_t CALEClientInitialize(ca_thread_pool_t handle)
173 OIC_LOG(DEBUG, TAG, "CALEClientInitialize");
179 OIC_LOG(ERROR, TAG, "g_jvm is null");
180 return CA_STATUS_FAILED;
183 bool isAttached = false;
185 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
188 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
189 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
193 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
194 return CA_STATUS_FAILED;
199 CAResult_t ret = CALECheckPlatformVersion(env, 18);
200 if (CA_STATUS_OK != ret)
202 OIC_LOG(ERROR, TAG, "it is not supported");
206 (*g_jvm)->DetachCurrentThread(g_jvm);
212 g_threadPoolHandle = handle;
214 ret = CALEClientInitGattMutexVaraibles();
215 if (CA_STATUS_OK != ret)
217 OIC_LOG(ERROR, TAG, "CALEClientInitGattMutexVaraibles has failed!");
218 CALEClientTerminateGattMutexVariables();
222 (*g_jvm)->DetachCurrentThread(g_jvm);
228 // init mutex for send logic
229 g_threadCond = ca_cond_new();
230 g_threadSendCond = ca_cond_new();
231 g_threadSendCBCond = ca_cond_new();
233 CALEClientCreateDeviceList();
234 CALEClientJNISetContext();
236 ret = CALEClientCreateUUIDList();
237 if (CA_STATUS_OK != ret)
239 OIC_LOG(ERROR, TAG, "CALEClientCreateUUIDList has failed");
243 (*g_jvm)->DetachCurrentThread(g_jvm);
249 ret = CALECreateJniInterfaceObject(); /* create java caleinterface instance*/
250 if (CA_STATUS_OK != ret)
252 OIC_LOG(ERROR, TAG, "CALECreateJniInterfaceObject has failed");
256 (*g_jvm)->DetachCurrentThread(g_jvm);
261 g_isStartedLEClient = true;
266 void CALEClientTerminate()
268 OIC_LOG(DEBUG, TAG, "CALEClientTerminate");
272 OIC_LOG(ERROR, TAG, "g_jvm is null");
276 bool isAttached = false;
278 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
281 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
282 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
286 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
292 if (g_leScanCallback)
294 (*env)->DeleteGlobalRef(env, g_leScanCallback);
297 if (g_leGattCallback)
299 (*env)->DeleteGlobalRef(env, g_leGattCallback);
304 (*env)->DeleteGlobalRef(env, g_sendBuffer);
309 (*env)->DeleteGlobalRef(env, g_uuidList);
312 CAResult_t ret = CALEClientRemoveAllDeviceState();
313 if (CA_STATUS_OK != ret)
315 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllDeviceState has failed");
318 ret = CALEClientRemoveAllScanDevices(env);
319 if (CA_STATUS_OK != ret)
321 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllScanDevices has failed");
324 ret = CALEClientRemoveAllGattObjs(env);
325 if (CA_STATUS_OK != ret)
327 OIC_LOG(ERROR, TAG, "CALEClientRemoveAllGattObjs has failed");
330 g_isStartedMulticastServer = false;
331 g_isStartedScan = false;
332 CALEClientSetSendFinishFlag(false);
333 CALEClientSetTheSendRequestFlag(false);
334 CALEClientSetWriteCharacteristicCBFlag(false);
336 CALEClientTerminateGattMutexVariables();
338 ca_cond_free(g_threadCond);
339 ca_cond_free(g_threadSendCond);
340 ca_cond_free(g_threadSendCBCond);
344 (*g_jvm)->DetachCurrentThread(g_jvm);
348 void CALEClientSendFinish(JNIEnv *env, jobject gatt)
350 OIC_LOG(DEBUG, TAG, "CALEClientSendFinish");
351 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
355 CAResult_t res = CALEClientDisconnect(env, gatt);
356 if (CA_STATUS_OK != res)
358 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
361 CALEClientUpdateSendCnt(env);
364 CAResult_t CALEClientSendUnicastMessage(const char* address, const char* data,
365 const uint32_t dataLen)
367 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessage(%s, %s)", address, data);
368 VERIFY_NON_NULL(address, TAG, "address is null");
369 VERIFY_NON_NULL(data, TAG, "data is null");
371 return CALEClientSendUnicastMessageImpl(address, data, dataLen);
374 CAResult_t CALEClientSendMulticastMessage(const char* data, const uint32_t dataLen)
376 OIC_LOG_V(DEBUG, TAG, "CALEClientSendMulticastMessage(%s)", data);
377 VERIFY_NON_NULL(data, TAG, "data is null");
381 OIC_LOG(ERROR, TAG, "g_jvm is null");
382 return CA_STATUS_FAILED;
385 bool isAttached = false;
387 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
390 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
391 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
395 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
396 return CA_STATUS_FAILED;
401 CAResult_t ret = CALEClientSendMulticastMessageImpl(env, data, dataLen);
402 if (CA_STATUS_OK != ret)
404 OIC_LOG(ERROR, TAG, "CALEClientSendMulticastMessageImpl has failed");
409 (*g_jvm)->DetachCurrentThread(g_jvm);
415 CAResult_t CALEClientStartUnicastServer(const char* address)
417 OIC_LOG_V(DEBUG, TAG, "it is not needed in this platform (%s)", address);
419 return CA_NOT_SUPPORTED;
422 CAResult_t CALEClientStartMulticastServer()
424 OIC_LOG(DEBUG, TAG, "CALEClientStartMulticastServer");
426 if (g_isStartedMulticastServer)
428 OIC_LOG(ERROR, TAG, "server is already started..it will be skipped");
429 return CA_STATUS_FAILED;
434 OIC_LOG(ERROR, TAG, "g_jvm is null");
435 return CA_STATUS_FAILED;
438 bool isAttached = false;
440 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
443 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
444 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
448 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
449 return CA_STATUS_FAILED;
454 g_isStartedMulticastServer = true;
455 CAResult_t ret = CALEClientStartScan();
456 if (CA_STATUS_OK != ret)
458 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
463 (*g_jvm)->DetachCurrentThread(g_jvm);
469 void CALEClientStopUnicastServer()
471 OIC_LOG(DEBUG, TAG, "CALEClientStopUnicastServer");
474 void CALEClientStopMulticastServer()
476 OIC_LOG(DEBUG, TAG, "CALEClientStopMulticastServer");
477 g_isStartedMulticastServer = false;
478 CAResult_t res = CALEClientStopScan();
479 if (CA_STATUS_OK != res)
481 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
486 void CALEClientSetCallback(CAPacketReceiveCallback callback)
488 g_packetReceiveCallback = callback;
491 void CASetBLEClientErrorHandleCallback(CABLEErrorHandleCallback callback)
493 g_clientErrorCallback = callback;
496 CAResult_t CALEClientGetInterfaceInfo(char **address)
498 OIC_LOG(INFO, TAG, "CALEClientGetInterfaceInfo is not supported");
499 return CA_NOT_SUPPORTED;
502 CAResult_t CALEClientSendUnicastMessageImpl(const char* address, const char* data,
503 const uint32_t dataLen)
505 OIC_LOG_V(DEBUG, TAG, "CALEClientSendUnicastMessageImpl, address: %s, data: %s", address,
507 VERIFY_NON_NULL(address, TAG, "address is null");
508 VERIFY_NON_NULL(data, TAG, "data is null");
512 OIC_LOG(ERROR, TAG, "g_jvm is null");
513 return CA_STATUS_FAILED;
516 bool isAttached = false;
518 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
521 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
522 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
525 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
526 return CA_STATUS_FAILED;
531 ca_mutex_lock(g_threadSendMutex);
533 CAResult_t ret = CA_STATUS_OK;
534 if (g_context && g_deviceList)
536 uint32_t length = u_arraylist_length(g_deviceList);
537 for (uint32_t index = 0; index < length; index++)
539 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
542 OIC_LOG(ERROR, TAG, "jarrayObj is null");
546 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
549 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
553 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
556 OIC_LOG(ERROR, TAG, "setAddress is null");
560 OIC_LOG_V(DEBUG, TAG, "remote device address is %s", setAddress);
562 if (!strcmp(setAddress, address))
564 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
566 // connect to gatt server
567 ret = CALEClientStopScan();
568 if (CA_STATUS_OK != ret)
570 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
576 (*env)->DeleteGlobalRef(env, g_sendBuffer);
578 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
579 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
580 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
582 ret = CALEClientSendData(env, jarrayObj);
583 if (CA_STATUS_OK != ret)
585 OIC_LOG(ERROR, TAG, "CALEClientSendData in unicast is failed");
590 CALEClientSetTheSendRequestFlag(true);
591 ca_cond_signal(g_threadSendCBCond);
593 if (!g_isReceivedWriteCB)
595 OIC_LOG(INFO, TAG, "wait..(unicast)");
596 ca_cond_wait(g_threadSendCond, g_threadSendMutex);
600 CALEClientSetWriteCharacteristicCBFlag(false);
604 OIC_LOG(INFO, TAG, "wake up");
607 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
613 (*g_jvm)->DetachCurrentThread(g_jvm);
616 ret = CALECheckSendState(address);
617 if(CA_STATUS_OK != ret)
619 OIC_LOG(ERROR, TAG, "send has failed");
623 // start LE Scan again
624 ret = CALEClientStartScan();
625 if (CA_STATUS_OK != ret)
627 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
628 ca_mutex_unlock(g_threadSendMutex);
632 ca_mutex_unlock(g_threadSendMutex);
633 OIC_LOG(INFO, TAG, "unicast - send success");
639 // start LE Scan again
640 ret = CALEClientStartScan();
641 if (CA_STATUS_OK != ret)
643 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
644 ca_mutex_unlock(g_threadSendMutex);
650 (*g_jvm)->DetachCurrentThread(g_jvm);
652 ca_mutex_unlock(g_threadSendMutex);
653 return CA_SEND_FAILED;
656 CAResult_t CALEClientSendMulticastMessageImpl(JNIEnv *env, const char* data,
657 const uint32_t dataLen)
659 OIC_LOG_V(DEBUG, TAG, "CASendMulticastMessageImpl, send to, data: %s, %d", data, dataLen);
660 VERIFY_NON_NULL(data, TAG, "data is null");
661 VERIFY_NON_NULL(env, TAG, "env is null");
665 OIC_LOG(ERROR, TAG, "g_deviceList is null");
666 return CA_STATUS_FAILED;
669 ca_mutex_lock(g_threadSendMutex);
671 CALEClientSetSendFinishFlag(false);
673 OIC_LOG(DEBUG, TAG, "set byteArray for data");
676 (*env)->DeleteGlobalRef(env, g_sendBuffer);
678 jbyteArray jni_arr = (*env)->NewByteArray(env, dataLen);
679 (*env)->SetByteArrayRegion(env, jni_arr, 0, dataLen, (jbyte*) data);
680 g_sendBuffer = (jbyteArray)(*env)->NewGlobalRef(env, jni_arr);
682 // connect to gatt server
683 CAResult_t res = CALEClientStopScan();
684 if (CA_STATUS_OK != res)
686 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
687 ca_mutex_unlock(g_threadSendMutex);
691 uint32_t length = u_arraylist_length(g_deviceList);
692 g_targetCnt = length;
698 for (uint32_t index = 0; index < length; index++)
700 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
703 OIC_LOG(ERROR, TAG, "jarrayObj is not available");
707 res = CALEClientSendData(env, jarrayObj);
708 if (res != CA_STATUS_OK)
710 OIC_LOG(ERROR, TAG, "BT device[%d] - send has failed");
714 CALEClientSetTheSendRequestFlag(true);
715 ca_cond_signal(g_threadSendCBCond);
717 if (!g_isReceivedWriteCB)
719 OIC_LOG(INFO, TAG, "wait..(multicast)");
720 ca_cond_wait(g_threadSendCond, g_threadSendMutex);
724 CALEClientSetWriteCharacteristicCBFlag(false);
728 jstring jni_address = CALEGetAddressFromBTDevice(env, jarrayObj);
731 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
735 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
738 OIC_LOG(ERROR, TAG, "address is not available");
742 res = CALECheckSendState(address);
743 if (CA_STATUS_OK != res)
745 OIC_LOG_V(INFO, TAG, "multicast : send has failed for this device[%s]", address);
746 g_clientErrorCallback(address, data, dataLen, res);
747 (*env)->ReleaseStringUTFChars(env, jni_address, address);
751 (*env)->ReleaseStringUTFChars(env, jni_address, address);
754 OIC_LOG(DEBUG, TAG, "connection routine is finished");
756 // wait for finish to send data through "CALeGattServicesDiscoveredCallback"
757 if (!g_isFinishedSendData)
759 ca_mutex_lock(g_threadMutex);
760 ca_cond_wait(g_threadCond, g_threadMutex);
761 OIC_LOG(DEBUG, TAG, "the data was sent for All devices");
762 ca_mutex_unlock(g_threadMutex);
765 // start LE Scan again
766 res = CALEClientStartScan();
767 if (CA_STATUS_OK != res)
769 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
770 ca_mutex_unlock(g_threadSendMutex);
774 ca_mutex_unlock(g_threadSendMutex);
775 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
779 res = CALEClientStartScan();
780 if (CA_STATUS_OK != res)
782 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
783 ca_mutex_unlock(g_threadSendMutex);
787 ca_mutex_unlock(g_threadSendMutex);
788 OIC_LOG(DEBUG, TAG, "OUT - CALEClientSendMulticastMessageImpl");
789 return CA_SEND_FAILED;
792 CAResult_t CALECheckSendState(const char* address)
794 VERIFY_NON_NULL(address, TAG, "address is null");
796 ca_mutex_lock(g_deviceStateListMutex);
797 CALEState_t* state = CALEClientGetStateInfo(address);
800 OIC_LOG(ERROR, TAG, "state is null");
801 ca_mutex_unlock(g_deviceStateListMutex);
802 return CA_SEND_FAILED;
805 if (STATE_SEND_SUCCESS != state->sendState)
807 OIC_LOG(ERROR, TAG, "sendstate is not STATE_SEND_SUCCESS");
808 ca_mutex_unlock(g_deviceStateListMutex);
809 return CA_SEND_FAILED;
811 ca_mutex_unlock(g_deviceStateListMutex);
815 CAResult_t CALEClientSendData(JNIEnv *env, jobject device)
817 OIC_LOG(DEBUG, TAG, "IN - CALEClientSendData");
818 VERIFY_NON_NULL(device, TAG, "device is null");
819 VERIFY_NON_NULL(env, TAG, "env is null");
821 jstring jni_address = CALEGetAddressFromBTDevice(env, device);
824 OIC_LOG(ERROR, TAG, "CALEGetAddressFromBTDevice has failed");
825 return CA_STATUS_FAILED;
828 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
831 OIC_LOG(ERROR, TAG, "address is not available");
832 return CA_STATUS_FAILED;
835 ca_mutex_lock(g_deviceStateListMutex);
836 CALEState_t* state = CALEClientGetStateInfo(address);
837 ca_mutex_unlock(g_deviceStateListMutex);
840 OIC_LOG(DEBUG, TAG, "state is empty..start to connect LE");
841 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
842 if (CA_STATUS_OK != ret)
844 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
845 (*env)->ReleaseStringUTFChars(env, jni_address, address);
851 if (STATE_CONNECTED == state->connectedState)
853 OIC_LOG(INFO, TAG, "GATT has already connected");
854 jobject gatt = CALEClientGetGattObjInList(env, address);
857 OIC_LOG(ERROR, TAG, "CALEClientGetGattObjInList has failed");
858 (*env)->ReleaseStringUTFChars(env, jni_address, address);
859 return CA_STATUS_FAILED;
862 CAResult_t ret = CALEClientWriteCharacteristic(env, gatt);
863 if (CA_STATUS_OK != ret)
865 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
866 (*env)->ReleaseStringUTFChars(env, jni_address, address);
872 OIC_LOG(DEBUG, TAG, "start to connect LE");
873 CAResult_t ret = CALEClientConnect(env, device, JNI_FALSE, g_leGattCallback);
874 if (CA_STATUS_OK != ret)
876 OIC_LOG(ERROR, TAG, "CALEClientConnect has failed");
877 (*env)->ReleaseStringUTFChars(env, jni_address, address);
883 (*env)->ReleaseStringUTFChars(env, jni_address, address);
887 jstring CALEClientGetAddressFromGattObj(JNIEnv *env, jobject gatt)
889 VERIFY_NON_NULL_RET(gatt, TAG, "gatt is null", NULL);
890 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
892 jclass jni_cid_gattdevice_list = (*env)->FindClass(env, CLASSPATH_BT_GATT);
893 if (!jni_cid_gattdevice_list)
895 OIC_LOG(ERROR, TAG, "jni_cid_gattdevice_list is null");
899 jmethodID jni_mid_getDevice = (*env)->GetMethodID(env, jni_cid_gattdevice_list, "getDevice",
900 "()Landroid/bluetooth/BluetoothDevice;");
901 if (!jni_mid_getDevice)
903 OIC_LOG(ERROR, TAG, "jni_mid_getDevice is null");
907 jobject jni_obj_device = (*env)->CallObjectMethod(env, gatt, jni_mid_getDevice);
910 OIC_LOG(ERROR, TAG, "jni_obj_device is null");
914 jstring jni_address = CALEGetAddressFromBTDevice(env, jni_obj_device);
917 OIC_LOG(ERROR, TAG, "jni_address is null");
927 CAResult_t CALEClientGattClose(JNIEnv *env, jobject bluetoothGatt)
930 OIC_LOG(DEBUG, TAG, "Gatt Close");
931 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
932 VERIFY_NON_NULL(env, TAG, "env is null");
934 // get BluetoothGatt class
935 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
936 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
937 if (!jni_cid_BluetoothGatt)
939 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
940 return CA_STATUS_FAILED;
943 jmethodID jni_mid_closeGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt, "close", "()V");
944 if (!jni_mid_closeGatt)
946 OIC_LOG(ERROR, TAG, "jni_mid_closeGatt is null");
950 // call disconnect gatt method
951 OIC_LOG(DEBUG, TAG, "request to close GATT");
952 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_closeGatt);
954 if ((*env)->ExceptionCheck(env))
956 OIC_LOG(ERROR, TAG, "closeGATT has failed");
957 (*env)->ExceptionDescribe(env);
958 (*env)->ExceptionClear(env);
959 return CA_STATUS_FAILED;
965 CAResult_t CALEClientStartScan()
967 if (!g_isStartedMulticastServer)
969 OIC_LOG(ERROR, TAG, "server is not started yet..scan will be passed");
970 return CA_STATUS_FAILED;
973 if (!g_isStartedLEClient)
975 OIC_LOG(ERROR, TAG, "LE client is not started");
976 return CA_STATUS_FAILED;
981 OIC_LOG(ERROR, TAG, "g_jvm is null");
982 return CA_STATUS_FAILED;
987 OIC_LOG(INFO, TAG, "scanning is already started");
991 bool isAttached = false;
993 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
996 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
998 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1001 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1002 return CA_STATUS_FAILED;
1007 OIC_LOG(DEBUG, TAG, "CALEClientStartScan");
1009 CAResult_t ret = CA_STATUS_OK;
1010 // scan gatt server with UUID
1011 if (g_leScanCallback && g_uuidList)
1014 ret = CALEClientStartScanWithUUIDImpl(env, g_uuidList, g_leScanCallback);
1015 if(CA_STATUS_OK != ret)
1017 OIC_LOG(ERROR, TAG, "CALEClientStartScanWithUUIDImpl has failed");
1020 ret = CALEClientStartScanImpl(env, g_leScanCallback);
1021 if (CA_STATUS_OK != ret)
1023 OIC_LOG(ERROR, TAG, "CALEClientStartScanImpl has failed");
1030 (*g_jvm)->DetachCurrentThread(g_jvm);
1036 CAResult_t CALEClientStartScanImpl(JNIEnv *env, jobject callback)
1038 VERIFY_NON_NULL(callback, TAG, "callback is null");
1039 VERIFY_NON_NULL(env, TAG, "env is null");
1041 if (!CALEIsEnableBTAdapter(env))
1043 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1044 return CA_ADAPTER_NOT_ENABLED;
1047 // get default bt adapter class
1048 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1049 if (!jni_cid_BTAdapter)
1051 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1052 return CA_STATUS_FAILED;
1055 // get remote bt adapter method
1056 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1057 "getDefaultAdapter",
1058 METHODID_OBJECTNONPARAM);
1059 if (!jni_mid_getDefaultAdapter)
1061 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1062 return CA_STATUS_FAILED;
1065 // get start le scan method
1066 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1067 "(Landroid/bluetooth/BluetoothAdapter$"
1068 "LeScanCallback;)Z");
1069 if (!jni_mid_startLeScan)
1071 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1072 return CA_STATUS_FAILED;
1075 // gat bt adapter object
1076 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1077 jni_mid_getDefaultAdapter);
1078 if (!jni_obj_BTAdapter)
1080 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1081 return CA_STATUS_FAILED;
1084 // call start le scan method
1085 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1086 jni_mid_startLeScan, callback);
1087 if (!jni_obj_startLeScan)
1089 OIC_LOG(ERROR, TAG, "startLeScan is failed");
1090 return CA_STATUS_FAILED;
1094 OIC_LOG(DEBUG, TAG, "startLeScan is started");
1095 g_isStartedScan = true;
1098 return CA_STATUS_OK;
1101 CAResult_t CALEClientStartScanWithUUIDImpl(JNIEnv *env, jobjectArray uuids, jobject callback)
1103 VERIFY_NON_NULL(callback, TAG, "callback is null");
1104 VERIFY_NON_NULL(uuids, TAG, "uuids is null");
1105 VERIFY_NON_NULL(env, TAG, "env is null");
1107 if (!CALEIsEnableBTAdapter(env))
1109 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1110 return CA_ADAPTER_NOT_ENABLED;
1113 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1114 if (!jni_cid_BTAdapter)
1116 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1117 return CA_STATUS_FAILED;
1120 // get remote bt adapter method
1121 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1122 "getDefaultAdapter",
1123 METHODID_OBJECTNONPARAM);
1124 if (!jni_mid_getDefaultAdapter)
1126 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1127 return CA_STATUS_FAILED;
1130 // get start le scan method
1131 jmethodID jni_mid_startLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "startLeScan",
1132 "([Ljava/util/UUID;Landroid/bluetooth/"
1133 "BluetoothAdapter$LeScanCallback;)Z");
1134 if (!jni_mid_startLeScan)
1136 OIC_LOG(ERROR, TAG, "startLeScan: jni_mid_startLeScan is null");
1137 return CA_STATUS_FAILED;
1140 // get bt adapter object
1141 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1142 jni_mid_getDefaultAdapter);
1143 if (!jni_obj_BTAdapter)
1145 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_obj_BTAdapter is null");
1146 return CA_STATUS_FAILED;
1149 // call start le scan method
1150 jboolean jni_obj_startLeScan = (*env)->CallBooleanMethod(env, jni_obj_BTAdapter,
1151 jni_mid_startLeScan, uuids, callback);
1152 if (!jni_obj_startLeScan)
1154 OIC_LOG(ERROR, TAG, "startLeScan With UUID is failed");
1155 return CA_STATUS_FAILED;
1159 OIC_LOG(DEBUG, TAG, "startLeScan With UUID is started");
1160 g_isStartedScan = true;
1163 return CA_STATUS_OK;
1166 jobject CALEClientGetUUIDObject(JNIEnv *env, const char* uuid)
1168 VERIFY_NON_NULL_RET(uuid, TAG, "uuid is null", NULL);
1169 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1172 jclass jni_cid_uuid = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1175 OIC_LOG(ERROR, TAG, "jni_cid_uuid is null");
1179 jmethodID jni_mid_fromString = (*env)->GetStaticMethodID(env, jni_cid_uuid, "fromString",
1180 "(Ljava/lang/String;)"
1181 "Ljava/util/UUID;");
1182 if (!jni_mid_fromString)
1184 OIC_LOG(ERROR, TAG, "jni_mid_fromString is null");
1188 jstring jni_uuid = (*env)->NewStringUTF(env, uuid);
1189 jobject jni_obj_uuid = (*env)->CallStaticObjectMethod(env, jni_cid_uuid, jni_mid_fromString,
1193 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1197 return jni_obj_uuid;
1200 CAResult_t CALEClientStopScan()
1204 OIC_LOG(ERROR, TAG, "g_jvm is null");
1205 return CA_STATUS_FAILED;
1208 if (!g_isStartedScan)
1210 OIC_LOG(INFO, TAG, "scanning is already stopped");
1211 return CA_STATUS_OK;
1214 bool isAttached = false;
1216 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1219 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1220 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1223 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1224 return CA_STATUS_FAILED;
1229 CAResult_t ret = CALEClientStopScanImpl(env, g_leScanCallback);
1230 if (CA_STATUS_OK != ret)
1232 OIC_LOG(ERROR, TAG, "CALEClientStopScanImpl has failed");
1236 g_isStartedScan = false;
1241 (*g_jvm)->DetachCurrentThread(g_jvm);
1247 CAResult_t CALEClientStopScanImpl(JNIEnv *env, jobject callback)
1249 OIC_LOG(DEBUG, TAG, "CALEClientStopScanImpl");
1250 VERIFY_NON_NULL(callback, TAG, "callback is null");
1251 VERIFY_NON_NULL(env, TAG, "env is null");
1253 if (!CALEIsEnableBTAdapter(env))
1255 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1256 return CA_ADAPTER_NOT_ENABLED;
1259 // get default bt adapter class
1260 jclass jni_cid_BTAdapter = (*env)->FindClass(env, CLASSPATH_BT_ADAPTER);
1261 if (!jni_cid_BTAdapter)
1263 OIC_LOG(ERROR, TAG, "getState From BTAdapter: jni_cid_BTAdapter is null");
1264 return CA_STATUS_FAILED;
1267 // get remote bt adapter method
1268 jmethodID jni_mid_getDefaultAdapter = (*env)->GetStaticMethodID(env, jni_cid_BTAdapter,
1269 "getDefaultAdapter",
1270 METHODID_OBJECTNONPARAM);
1271 if (!jni_mid_getDefaultAdapter)
1273 OIC_LOG(ERROR, TAG, "jni_mid_getDefaultAdapter is null");
1274 return CA_STATUS_FAILED;
1277 // get start le scan method
1278 jmethodID jni_mid_stopLeScan = (*env)->GetMethodID(env, jni_cid_BTAdapter, "stopLeScan",
1279 "(Landroid/bluetooth/"
1280 "BluetoothAdapter$LeScanCallback;)V");
1281 if (!jni_mid_stopLeScan)
1283 OIC_LOG(ERROR, TAG, "stopLeScan: jni_mid_stopLeScan is null");
1284 return CA_STATUS_FAILED;
1287 // gat bt adapter object
1288 jobject jni_obj_BTAdapter = (*env)->CallStaticObjectMethod(env, jni_cid_BTAdapter,
1289 jni_mid_getDefaultAdapter);
1290 if (!jni_obj_BTAdapter)
1292 OIC_LOG(ERROR, TAG, "jni_obj_BTAdapter is null");
1293 return CA_STATUS_FAILED;
1296 OIC_LOG(DEBUG, TAG, "CALL API - request to stop LE Scan");
1297 // call start le scan method
1298 (*env)->CallVoidMethod(env, jni_obj_BTAdapter, jni_mid_stopLeScan, callback);
1299 if ((*env)->ExceptionCheck(env))
1301 OIC_LOG(ERROR, TAG, "stopLeScan has failed");
1302 (*env)->ExceptionDescribe(env);
1303 (*env)->ExceptionClear(env);
1304 return CA_STATUS_FAILED;
1307 return CA_STATUS_OK;
1310 CAResult_t CALEClientConnect(JNIEnv *env, jobject bluetoothDevice, jboolean autoconnect,
1313 OIC_LOG(DEBUG, TAG, "GATT CONNECT");
1314 VERIFY_NON_NULL(env, TAG, "env is null");
1315 VERIFY_NON_NULL(bluetoothDevice, TAG, "bluetoothDevice is null");
1316 VERIFY_NON_NULL(callback, TAG, "callback is null");
1318 if (!CALEIsEnableBTAdapter(env))
1320 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1321 return CA_ADAPTER_NOT_ENABLED;
1324 jstring jni_address = CALEGetAddressFromBTDevice(env, bluetoothDevice);
1327 OIC_LOG(ERROR, TAG, "bleConnect: CALEGetAddressFromBTDevice is null");
1328 return CA_STATUS_FAILED;
1331 // get BluetoothDevice class
1332 OIC_LOG(DEBUG, TAG, "get BluetoothDevice class");
1333 jclass jni_cid_BluetoothDevice = (*env)->FindClass(env, "android/bluetooth/BluetoothDevice");
1334 if (!jni_cid_BluetoothDevice)
1336 OIC_LOG(ERROR, TAG, "bleConnect: jni_cid_BluetoothDevice is null");
1337 return CA_STATUS_FAILED;
1340 // get connectGatt method
1341 OIC_LOG(DEBUG, TAG, "get connectGatt method");
1342 jmethodID jni_mid_connectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothDevice, "connectGatt",
1343 "(Landroid/content/Context;ZLandroid/"
1344 "bluetooth/BluetoothGattCallback;)"
1345 "Landroid/bluetooth/BluetoothGatt;");
1346 if (!jni_mid_connectGatt)
1348 OIC_LOG(ERROR, TAG, "bleConnect: jni_mid_connectGatt is null");
1349 return CA_STATUS_FAILED;
1352 OIC_LOG(DEBUG, TAG, "Call object method - connectGatt");
1353 jobject jni_obj_connectGatt = (*env)->CallObjectMethod(env, bluetoothDevice,
1354 jni_mid_connectGatt,
1356 autoconnect, callback);
1357 if (!jni_obj_connectGatt)
1359 OIC_LOG(ERROR, TAG, "CALL API - connectGatt was failed..it will be removed");
1360 CALEClientRemoveDeviceInScanDeviceList(env, jni_address);
1361 CALEClientUpdateSendCnt(env);
1362 return CA_STATUS_FAILED;
1366 OIC_LOG(DEBUG, TAG, "le connecting..please wait..");
1368 return CA_STATUS_OK;
1371 CAResult_t CALEClientDisconnect(JNIEnv *env, jobject bluetoothGatt)
1373 OIC_LOG(DEBUG, TAG, "GATT DISCONNECT");
1374 VERIFY_NON_NULL(env, TAG, "env is null");
1375 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1377 if (!CALEIsEnableBTAdapter(env))
1379 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1380 return CA_ADAPTER_NOT_ENABLED;
1383 // get BluetoothGatt class
1384 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1385 if (!jni_cid_BluetoothGatt)
1387 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1388 return CA_STATUS_FAILED;
1391 OIC_LOG(DEBUG, TAG, "get gatt disconnect method");
1392 jmethodID jni_mid_disconnectGatt = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1393 "disconnect", "()V");
1394 if (!jni_mid_disconnectGatt)
1396 OIC_LOG(ERROR, TAG, "jni_mid_disconnectGatt is null");
1397 return CA_STATUS_FAILED;
1400 // call disconnect gatt method
1401 (*env)->CallVoidMethod(env, bluetoothGatt, jni_mid_disconnectGatt);
1402 if ((*env)->ExceptionCheck(env))
1404 OIC_LOG(ERROR, TAG, "disconnect has failed");
1405 (*env)->ExceptionDescribe(env);
1406 (*env)->ExceptionClear(env);
1407 return CA_STATUS_FAILED;
1410 OIC_LOG(DEBUG, TAG, "disconnecting Gatt...");
1412 return CA_STATUS_OK;
1415 CAResult_t CALEClientDisconnectAll(JNIEnv *env)
1417 OIC_LOG(DEBUG, TAG, "CALEClientDisconnectAll");
1418 VERIFY_NON_NULL(env, TAG, "env is null");
1420 if (!g_gattObjectList)
1422 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
1423 return CA_STATUS_FAILED;
1426 uint32_t length = u_arraylist_length(g_gattObjectList);
1427 for (uint32_t index = 0; index < length; index++)
1429 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
1432 OIC_LOG(ERROR, TAG, "jarrayObj is null");
1435 CAResult_t res = CALEClientDisconnect(env, jarrayObj);
1436 if (CA_STATUS_OK != res)
1438 OIC_LOG(ERROR, TAG, "CALEClientDisconnect has failed");
1443 OICFree(g_gattObjectList);
1444 g_gattObjectList = NULL;
1446 return CA_STATUS_OK;
1449 CAResult_t CALEClientDiscoverServices(JNIEnv *env, jobject bluetoothGatt)
1451 VERIFY_NON_NULL(env, TAG, "env is null");
1452 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1454 if (!CALEIsEnableBTAdapter(env))
1456 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1457 return CA_ADAPTER_NOT_ENABLED;
1460 // get BluetoothGatt class
1461 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1462 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1463 if (!jni_cid_BluetoothGatt)
1465 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1466 return CA_STATUS_FAILED;
1469 OIC_LOG(DEBUG, TAG, "discovery gatt services method");
1470 jmethodID jni_mid_discoverServices = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1471 "discoverServices", "()Z");
1472 if (!jni_mid_discoverServices)
1474 OIC_LOG(ERROR, TAG, "jni_mid_discoverServices is null");
1475 return CA_STATUS_FAILED;
1477 // call disconnect gatt method
1478 OIC_LOG(DEBUG, TAG, "CALL API - request discovery gatt services");
1479 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_discoverServices);
1482 OIC_LOG(ERROR, TAG, "discoverServices has not been started");
1483 return CA_STATUS_FAILED;
1486 return CA_STATUS_OK;
1489 CAResult_t CALEClientWriteCharacteristic(JNIEnv *env, jobject gatt)
1491 VERIFY_NON_NULL(env, TAG, "env is null");
1492 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
1495 jobject jni_obj_character = CALEClientCreateGattCharacteristic(env, gatt, g_sendBuffer);
1496 if (!jni_obj_character)
1498 CALEClientSendFinish(env, gatt);
1499 ca_cond_signal(g_threadSendCond);
1500 return CA_STATUS_FAILED;
1503 CAResult_t ret = CALEClientWriteCharacteristicImpl(env, gatt, jni_obj_character);
1504 if (CA_STATUS_OK != ret)
1506 CALEClientSendFinish(env, gatt);
1507 ca_cond_signal(g_threadSendCond);
1511 return CA_STATUS_OK;
1514 CAResult_t CALEClientWriteCharacteristicImpl(JNIEnv *env, jobject bluetoothGatt,
1515 jobject gattCharacteristic)
1517 OIC_LOG(DEBUG, TAG, "WRITE GATT CHARACTERISTIC");
1518 VERIFY_NON_NULL(env, TAG, "env is null");
1519 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1520 VERIFY_NON_NULL(gattCharacteristic, TAG, "gattCharacteristic is null");
1522 if (!CALEIsEnableBTAdapter(env))
1524 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1525 return CA_STATUS_FAILED;
1528 // get BluetoothGatt class
1529 OIC_LOG(DEBUG, TAG, "get BluetoothGatt class");
1530 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1531 if (!jni_cid_BluetoothGatt)
1533 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1534 return CA_STATUS_FAILED;
1537 OIC_LOG(DEBUG, TAG, "write characteristic method");
1538 jmethodID jni_mid_writeCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1539 "writeCharacteristic",
1540 "(Landroid/bluetooth/"
1541 "BluetoothGattCharacteristic;)Z");
1542 if (!jni_mid_writeCharacteristic)
1544 OIC_LOG(ERROR, TAG, "jni_mid_writeCharacteristic is null");
1545 return CA_STATUS_FAILED;
1548 // call disconnect gatt method
1549 OIC_LOG(DEBUG, TAG, "CALL API - request to write gatt characteristic");
1550 jboolean ret = (jboolean)(*env)->CallBooleanMethod(env, bluetoothGatt,
1551 jni_mid_writeCharacteristic,
1552 gattCharacteristic);
1555 OIC_LOG(DEBUG, TAG, "writeCharacteristic success");
1559 OIC_LOG(ERROR, TAG, "writeCharacteristic has failed");
1560 return CA_STATUS_FAILED;
1563 return CA_STATUS_OK;
1566 CAResult_t CALEClientReadCharacteristic(JNIEnv *env, jobject bluetoothGatt)
1568 VERIFY_NON_NULL(env, TAG, "env is null");
1569 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1571 if (!CALEIsEnableBTAdapter(env))
1573 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1574 return CA_STATUS_FAILED;
1577 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1578 if (!jni_cid_BluetoothGatt)
1580 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1581 return CA_STATUS_FAILED;
1584 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
1587 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1588 return CA_STATUS_FAILED;
1591 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1592 if (!jni_obj_GattCharacteristic)
1594 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1595 return CA_STATUS_FAILED;
1598 OIC_LOG(DEBUG, TAG, "read characteristic method");
1599 jmethodID jni_mid_readCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1600 "readCharacteristic",
1601 "(Landroid/bluetooth/"
1602 "BluetoothGattCharacteristic;)Z");
1603 if (!jni_mid_readCharacteristic)
1605 OIC_LOG(ERROR, TAG, "jni_mid_readCharacteristic is null");
1606 return CA_STATUS_FAILED;
1609 // call disconnect gatt method
1610 OIC_LOG(DEBUG, TAG, "CALL API - request to read gatt characteristic");
1611 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_readCharacteristic,
1612 jni_obj_GattCharacteristic);
1615 OIC_LOG(DEBUG, TAG, "readCharacteristic success");
1619 OIC_LOG(ERROR, TAG, "readCharacteristic has failed");
1620 return CA_STATUS_FAILED;
1623 return CA_STATUS_OK;
1626 CAResult_t CALEClientSetCharacteristicNotification(JNIEnv *env, jobject bluetoothGatt,
1627 jobject characteristic)
1629 VERIFY_NON_NULL(env, TAG, "env is null");
1630 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1631 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1633 if (!CALEIsEnableBTAdapter(env))
1635 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1636 return CA_ADAPTER_NOT_ENABLED;
1639 // get BluetoothGatt class
1640 OIC_LOG(DEBUG, TAG, "CALEClientSetCharacteristicNotification");
1641 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1642 if (!jni_cid_BluetoothGatt)
1644 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1645 return CA_STATUS_FAILED;
1648 // set Characteristic Notification
1649 jmethodID jni_mid_setNotification = (*env)->GetMethodID(env, jni_cid_BluetoothGatt,
1650 "setCharacteristicNotification",
1651 "(Landroid/bluetooth/"
1652 "BluetoothGattCharacteristic;Z)Z");
1653 if (!jni_mid_setNotification)
1655 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1656 return CA_STATUS_FAILED;
1659 jboolean ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_setNotification,
1660 characteristic, JNI_TRUE);
1661 if (JNI_TRUE == ret)
1663 OIC_LOG(DEBUG, TAG, "CALL API - setCharacteristicNotification success");
1667 OIC_LOG(ERROR, TAG, "CALL API - setCharacteristicNotification has failed");
1668 return CA_STATUS_FAILED;
1671 return CA_STATUS_OK;
1674 jobject CALEClientGetGattService(JNIEnv *env, jobject bluetoothGatt, jstring characterUUID)
1676 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1677 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1678 VERIFY_NON_NULL_RET(characterUUID, TAG, "characterUUID is null", NULL);
1680 if (!CALEIsEnableBTAdapter(env))
1682 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1686 // get BluetoothGatt class
1687 OIC_LOG(DEBUG, TAG, "CALEClientGetGattService");
1688 jclass jni_cid_BluetoothGatt = (*env)->FindClass(env, CLASSPATH_BT_GATT);
1689 if (!jni_cid_BluetoothGatt)
1691 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGatt is null");
1695 jmethodID jni_mid_getService = (*env)->GetMethodID(
1696 env, jni_cid_BluetoothGatt, "getService",
1697 "(Ljava/util/UUID;)Landroid/bluetooth/BluetoothGattService;");
1698 if (!jni_mid_getService)
1700 OIC_LOG(ERROR, TAG, "jni_mid_getService is null");
1704 jobject jni_obj_service_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1705 if (!jni_obj_service_uuid)
1707 OIC_LOG(ERROR, TAG, "jni_obj_service_uuid is null");
1711 // get bluetooth gatt service
1712 OIC_LOG(DEBUG, TAG, "request to get service");
1713 jobject jni_obj_gattService = (*env)->CallObjectMethod(env, bluetoothGatt, jni_mid_getService,
1714 jni_obj_service_uuid);
1715 if (!jni_obj_gattService)
1717 OIC_LOG(ERROR, TAG, "jni_obj_gattService is null");
1721 // get bluetooth gatt service class
1722 jclass jni_cid_BluetoothGattService = (*env)->FindClass(
1723 env, "android/bluetooth/BluetoothGattService");
1724 if (!jni_cid_BluetoothGattService)
1726 OIC_LOG(ERROR, TAG, "jni_cid_BluetoothGattService is null");
1730 OIC_LOG(DEBUG, TAG, "get gatt getCharacteristic method");
1731 jmethodID jni_mid_getCharacteristic = (*env)->GetMethodID(env, jni_cid_BluetoothGattService,
1732 "getCharacteristic",
1733 "(Ljava/util/UUID;)"
1734 "Landroid/bluetooth/"
1735 "BluetoothGattCharacteristic;");
1736 if (!jni_mid_getCharacteristic)
1738 OIC_LOG(ERROR, TAG, "jni_mid_getCharacteristic is null");
1742 const char* uuid = (*env)->GetStringUTFChars(env, characterUUID, NULL);
1745 OIC_LOG(ERROR, TAG, "uuid is null");
1749 jobject jni_obj_tx_uuid = CALEClientGetUUIDObject(env, uuid);
1750 if (!jni_obj_tx_uuid)
1752 OIC_LOG(ERROR, TAG, "jni_obj_tx_uuid is null");
1753 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1757 OIC_LOG(DEBUG, TAG, "request to get Characteristic");
1758 jobject jni_obj_GattCharacteristic = (*env)->CallObjectMethod(env, jni_obj_gattService,
1759 jni_mid_getCharacteristic,
1762 (*env)->ReleaseStringUTFChars(env, characterUUID, uuid);
1763 return jni_obj_GattCharacteristic;
1766 jobject CALEClientCreateGattCharacteristic(JNIEnv *env, jobject bluetoothGatt, jbyteArray data)
1768 OIC_LOG(DEBUG, TAG, "CALEClientCreateGattCharacteristic");
1769 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1770 VERIFY_NON_NULL_RET(bluetoothGatt, TAG, "bluetoothGatt is null", NULL);
1771 VERIFY_NON_NULL_RET(data, TAG, "data is null", NULL);
1773 if (!CALEIsEnableBTAdapter(env))
1775 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1779 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_REQUEST_UUID);
1782 OIC_LOG(ERROR, TAG, "jni_uuid is null");
1786 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, bluetoothGatt, jni_uuid);
1787 if (!jni_obj_GattCharacteristic)
1789 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
1793 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth"
1794 "/BluetoothGattCharacteristic");
1795 if (!jni_cid_BTGattCharacteristic)
1797 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1801 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1802 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "setValue",
1804 if (!jni_mid_setValue)
1806 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
1810 jboolean ret = (*env)->CallBooleanMethod(env, jni_obj_GattCharacteristic, jni_mid_setValue,
1812 if (JNI_TRUE == ret)
1814 OIC_LOG(DEBUG, TAG, "the locally stored value has been set");
1818 OIC_LOG(ERROR, TAG, "the locally stored value hasn't been set");
1823 jmethodID jni_mid_setWriteType = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1824 "setWriteType", "(I)V");
1825 if (!jni_mid_setWriteType)
1827 OIC_LOG(ERROR, TAG, "jni_mid_setWriteType is null");
1831 jfieldID jni_fid_no_response = (*env)->GetStaticFieldID(env, jni_cid_BTGattCharacteristic,
1832 "WRITE_TYPE_NO_RESPONSE", "I");
1833 if (!jni_fid_no_response)
1835 OIC_LOG(ERROR, TAG, "jni_fid_no_response is not available");
1839 jint jni_int_val = (*env)->GetStaticIntField(env, jni_cid_BTGattCharacteristic,
1840 jni_fid_no_response);
1842 (*env)->CallVoidMethod(env, jni_obj_GattCharacteristic, jni_mid_setWriteType, jni_int_val);
1844 return jni_obj_GattCharacteristic;
1847 jbyteArray CALEClientGetValueFromCharacteristic(JNIEnv *env, jobject characteristic)
1849 VERIFY_NON_NULL_RET(characteristic, TAG, "characteristic is null", NULL);
1850 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
1852 if (!CALEIsEnableBTAdapter(env))
1854 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1858 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1859 "BluetoothGattCharacteristic");
1860 if (!jni_cid_BTGattCharacteristic)
1862 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1866 OIC_LOG(DEBUG, TAG, "get value in Characteristic");
1867 jmethodID jni_mid_getValue = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic, "getValue",
1869 if (!jni_mid_getValue)
1871 OIC_LOG(ERROR, TAG, "jni_mid_getValue is null");
1875 jbyteArray jni_obj_data_array = (*env)->CallObjectMethod(env, characteristic,
1877 return jni_obj_data_array;
1880 CAResult_t CALEClientCreateUUIDList()
1884 OIC_LOG(ERROR, TAG, "g_jvm is null");
1885 return CA_STATUS_FAILED;
1888 bool isAttached = false;
1890 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
1893 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
1894 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
1898 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
1899 return CA_STATUS_FAILED;
1904 // create new object array
1905 jclass jni_cid_uuid_list = (*env)->FindClass(env, CLASSPATH_BT_UUID);
1906 if (!jni_cid_uuid_list)
1908 OIC_LOG(ERROR, TAG, "jni_cid_uuid_list is null");
1912 jobjectArray jni_obj_uuid_list = (jobjectArray)(*env)->NewObjectArray(env, 1,
1913 jni_cid_uuid_list, NULL);
1914 if (!jni_obj_uuid_list)
1916 OIC_LOG(ERROR, TAG, "jni_obj_uuid_list is null");
1921 jobject jni_obj_uuid = CALEClientGetUUIDObject(env, OIC_GATT_SERVICE_UUID);
1924 OIC_LOG(ERROR, TAG, "jni_obj_uuid is null");
1927 (*env)->SetObjectArrayElement(env, jni_obj_uuid_list, 0, jni_obj_uuid);
1929 g_uuidList = (jobjectArray)(*env)->NewGlobalRef(env, jni_obj_uuid_list);
1933 (*g_jvm)->DetachCurrentThread(g_jvm);
1936 return CA_STATUS_OK;
1943 (*g_jvm)->DetachCurrentThread(g_jvm);
1945 return CA_STATUS_FAILED;
1948 CAResult_t CALEClientSetUUIDToDescriptor(JNIEnv *env, jobject bluetoothGatt,
1949 jobject characteristic)
1951 VERIFY_NON_NULL(env, TAG, "env is null");
1952 VERIFY_NON_NULL(bluetoothGatt, TAG, "bluetoothGatt is null");
1953 VERIFY_NON_NULL(characteristic, TAG, "characteristic is null");
1955 if (!CALEIsEnableBTAdapter(env))
1957 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
1958 return CA_ADAPTER_NOT_ENABLED;
1961 OIC_LOG(DEBUG, TAG, "CALEClientSetUUIDToDescriptor");
1962 jclass jni_cid_BTGattCharacteristic = (*env)->FindClass(env, "android/bluetooth/"
1963 "BluetoothGattCharacteristic");
1964 if (!jni_cid_BTGattCharacteristic)
1966 OIC_LOG(ERROR, TAG, "jni_cid_BTGattCharacteristic is null");
1967 return CA_STATUS_FAILED;
1970 OIC_LOG(DEBUG, TAG, "set value in Characteristic");
1971 jmethodID jni_mid_getDescriptor = (*env)->GetMethodID(env, jni_cid_BTGattCharacteristic,
1973 "(Ljava/util/UUID;)Landroid/bluetooth/"
1974 "BluetoothGattDescriptor;");
1975 if (!jni_mid_getDescriptor)
1977 OIC_LOG(ERROR, TAG, "jni_mid_getDescriptor is null");
1978 return CA_STATUS_FAILED;
1981 jobject jni_obj_cc_uuid = CALEClientGetUUIDObject(env, OIC_GATT_CHARACTERISTIC_CONFIG_UUID);
1982 if (!jni_obj_cc_uuid)
1984 OIC_LOG(ERROR, TAG, "jni_obj_cc_uuid is null");
1985 return CA_STATUS_FAILED;
1988 OIC_LOG(DEBUG, TAG, "request to get descriptor");
1989 jobject jni_obj_descriptor = (*env)->CallObjectMethod(env, characteristic,
1990 jni_mid_getDescriptor, jni_obj_cc_uuid);
1991 if (!jni_obj_descriptor)
1993 OIC_LOG(ERROR, TAG, "jni_obj_descriptor is null");
1994 return CA_STATUS_FAILED;
1997 OIC_LOG(DEBUG, TAG, "set value in descriptor");
1998 jclass jni_cid_descriptor = (*env)->FindClass(env,
1999 "android/bluetooth/BluetoothGattDescriptor");
2000 if (!jni_cid_descriptor)
2002 OIC_LOG(ERROR, TAG, "jni_cid_descriptor is null");
2003 return CA_STATUS_FAILED;
2006 jmethodID jni_mid_setValue = (*env)->GetMethodID(env, jni_cid_descriptor, "setValue", "([B)Z");
2007 if (!jni_mid_setValue)
2009 OIC_LOG(ERROR, TAG, "jni_mid_setValue is null");
2010 return CA_STATUS_FAILED;
2013 jfieldID jni_fid_NotiValue = (*env)->GetStaticFieldID(env, jni_cid_descriptor,
2014 "ENABLE_NOTIFICATION_VALUE", "[B");
2015 if (!jni_fid_NotiValue)
2017 OIC_LOG(ERROR, TAG, "jni_fid_NotiValue is null");
2018 return CA_STATUS_FAILED;
2021 OIC_LOG(DEBUG, TAG, "get ENABLE_NOTIFICATION_VALUE");
2023 jboolean jni_setvalue = (*env)->CallBooleanMethod(
2024 env, jni_obj_descriptor, jni_mid_setValue,
2025 (jbyteArray)(*env)->GetStaticObjectField(env, jni_cid_descriptor, jni_fid_NotiValue));
2028 OIC_LOG(DEBUG, TAG, "setValue success");
2032 OIC_LOG(ERROR, TAG, "setValue has failed");
2033 return CA_STATUS_FAILED;
2036 jclass jni_cid_gatt = (*env)->FindClass(env, "android/bluetooth/BluetoothGatt");
2039 OIC_LOG(ERROR, TAG, "jni_cid_gatt is null");
2040 return CA_STATUS_FAILED;
2043 OIC_LOG(DEBUG, TAG, "write Descriptor in gatt object");
2044 jmethodID jni_mid_writeDescriptor = (*env)->GetMethodID(env, jni_cid_gatt, "writeDescriptor",
2045 "(Landroid/bluetooth/"
2046 "BluetoothGattDescriptor;)Z");
2047 if (!jni_mid_writeDescriptor)
2049 OIC_LOG(ERROR, TAG, "jni_mid_writeDescriptor is null");
2050 return CA_STATUS_FAILED;
2053 OIC_LOG(DEBUG, TAG, "request to write descriptor");
2054 jboolean jni_ret = (*env)->CallBooleanMethod(env, bluetoothGatt, jni_mid_writeDescriptor,
2055 jni_obj_descriptor);
2058 OIC_LOG(DEBUG, TAG, "writeDescriptor success");
2062 OIC_LOG(ERROR, TAG, "writeDescriptor has failed");
2063 return CA_STATUS_FAILED;
2066 return CA_STATUS_OK;
2069 void CALEClientCreateScanDeviceList(JNIEnv *env)
2071 OIC_LOG(DEBUG, TAG, "CALEClientCreateScanDeviceList");
2072 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2074 ca_mutex_lock(g_deviceListMutex);
2075 // create new object array
2076 if (g_deviceList == NULL)
2078 OIC_LOG(DEBUG, TAG, "Create device list");
2080 g_deviceList = u_arraylist_create();
2082 ca_mutex_unlock(g_deviceListMutex);
2085 CAResult_t CALEClientAddScanDeviceToList(JNIEnv *env, jobject device)
2087 OIC_LOG(DEBUG, TAG, "IN - CALEClientAddScanDeviceToList");
2088 VERIFY_NON_NULL(device, TAG, "device is null");
2089 VERIFY_NON_NULL(env, TAG, "env is null");
2091 ca_mutex_lock(g_deviceListMutex);
2095 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2096 ca_mutex_unlock(g_deviceListMutex);
2097 return CA_STATUS_FAILED;
2100 jstring jni_remoteAddress = CALEGetAddressFromBTDevice(env, device);
2101 if (!jni_remoteAddress)
2103 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2104 ca_mutex_unlock(g_deviceListMutex);
2105 return CA_STATUS_FAILED;
2108 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2111 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2112 ca_mutex_unlock(g_deviceListMutex);
2113 return CA_STATUS_FAILED;
2116 if (!CALEClientIsDeviceInScanDeviceList(env, remoteAddress))
2118 jobject gdevice = (*env)->NewGlobalRef(env, device);
2119 u_arraylist_add(g_deviceList, gdevice);
2120 OIC_LOG(DEBUG, TAG, "Set Object to Array as Element");
2122 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2124 ca_mutex_unlock(g_deviceListMutex);
2126 OIC_LOG(DEBUG, TAG, "OUT - CALEClientAddScanDeviceToList");
2127 return CA_STATUS_OK;
2130 bool CALEClientIsDeviceInScanDeviceList(JNIEnv *env, const char* remoteAddress)
2132 OIC_LOG(DEBUG, TAG, "IN - CALEClientIsDeviceInScanDeviceList");
2133 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2134 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2138 OIC_LOG(DEBUG, TAG, "g_deviceList is null");
2142 uint32_t length = u_arraylist_length(g_deviceList);
2143 for (uint32_t index = 0; index < length; index++)
2145 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2148 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2152 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2153 if (!jni_setAddress)
2155 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2159 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2162 OIC_LOG(ERROR, TAG, "setAddress is null");
2166 if (!strcmp(remoteAddress, setAddress))
2168 OIC_LOG(DEBUG, TAG, "the device is already set");
2169 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2173 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2176 OIC_LOG(DEBUG, TAG, "OUT - CALEClientIsDeviceInScanDeviceList");
2177 OIC_LOG(DEBUG, TAG, "there are no the device in list. we can add");
2182 CAResult_t CALEClientRemoveAllScanDevices(JNIEnv *env)
2184 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllScanDevices");
2185 VERIFY_NON_NULL(env, TAG, "env is null");
2187 ca_mutex_lock(g_deviceListMutex);
2191 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2192 ca_mutex_unlock(g_deviceListMutex);
2193 return CA_STATUS_FAILED;
2196 uint32_t length = u_arraylist_length(g_deviceList);
2197 for (uint32_t index = 0; index < length; index++)
2199 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2202 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2205 (*env)->DeleteGlobalRef(env, jarrayObj);
2208 OICFree(g_deviceList);
2209 g_deviceList = NULL;
2211 ca_mutex_unlock(g_deviceListMutex);
2212 return CA_STATUS_OK;
2215 CAResult_t CALEClientRemoveDeviceInScanDeviceList(JNIEnv *env, jstring address)
2217 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceInScanDeviceList");
2218 VERIFY_NON_NULL(address, TAG, "address is null");
2219 VERIFY_NON_NULL(env, TAG, "env is null");
2221 ca_mutex_lock(g_deviceListMutex);
2225 OIC_LOG(ERROR, TAG, "g_deviceList is null");
2226 ca_mutex_unlock(g_deviceListMutex);
2227 return CA_STATUS_FAILED;
2230 uint32_t length = u_arraylist_length(g_deviceList);
2231 for (uint32_t index = 0; index < length; index++)
2233 jobject jarrayObj = (jobject) u_arraylist_get(g_deviceList, index);
2236 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2237 ca_mutex_unlock(g_deviceListMutex);
2238 return CA_STATUS_FAILED;
2241 jstring jni_setAddress = CALEGetAddressFromBTDevice(env, jarrayObj);
2242 if (!jni_setAddress)
2244 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2245 ca_mutex_unlock(g_deviceListMutex);
2246 return CA_STATUS_FAILED;
2249 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2252 OIC_LOG(ERROR, TAG, "setAddress is null");
2253 ca_mutex_unlock(g_deviceListMutex);
2254 return CA_STATUS_FAILED;
2257 const char* remoteAddress = (*env)->GetStringUTFChars(env, address, NULL);
2260 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2261 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2262 ca_mutex_unlock(g_deviceListMutex);
2263 return CA_STATUS_FAILED;
2266 if (!strcmp(setAddress, remoteAddress))
2268 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2269 (*env)->DeleteGlobalRef(env, jarrayObj);
2270 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2271 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2273 CALEClientReorderingList(index, g_deviceList);
2274 ca_mutex_unlock(g_deviceListMutex);
2275 return CA_STATUS_OK;
2277 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2278 (*env)->ReleaseStringUTFChars(env, address, remoteAddress);
2281 ca_mutex_unlock(g_deviceListMutex);
2282 OIC_LOG(DEBUG, TAG, "There are no object in the device list");
2284 return CA_STATUS_OK;
2291 CAResult_t CALEClientAddGattobjToList(JNIEnv *env, jobject gatt)
2293 OIC_LOG(DEBUG, TAG, "CALEClientAddGattobjToList");
2294 VERIFY_NON_NULL(env, TAG, "env is null");
2295 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2297 ca_mutex_lock(g_gattObjectMutex);
2299 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2300 if (!jni_remoteAddress)
2302 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2303 ca_mutex_unlock(g_gattObjectMutex);
2304 return CA_STATUS_FAILED;
2307 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2310 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2311 ca_mutex_unlock(g_gattObjectMutex);
2312 return CA_STATUS_FAILED;
2315 if (!CALEClientIsGattObjInList(env, remoteAddress))
2317 jobject newGatt = (*env)->NewGlobalRef(env, gatt);
2318 u_arraylist_add(g_gattObjectList, newGatt);
2319 OIC_LOG(DEBUG, TAG, "Set GATT Object to Array as Element");
2322 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2323 ca_mutex_unlock(g_gattObjectMutex);
2324 return CA_STATUS_OK;
2327 bool CALEClientIsGattObjInList(JNIEnv *env, const char* remoteAddress)
2329 OIC_LOG(DEBUG, TAG, "CALEClientIsGattObjInList");
2330 VERIFY_NON_NULL(env, TAG, "env is null");
2331 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", true);
2333 uint32_t length = u_arraylist_length(g_gattObjectList);
2334 for (uint32_t index = 0; index < length; index++)
2337 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2340 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2344 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2345 if (!jni_setAddress)
2347 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2351 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2354 OIC_LOG(ERROR, TAG, "setAddress is null");
2358 if (!strcmp(remoteAddress, setAddress))
2360 OIC_LOG(DEBUG, TAG, "the device is already set");
2361 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2366 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2371 OIC_LOG(DEBUG, TAG, "There are no GATT object in list. it can be added");
2375 jobject CALEClientGetGattObjInList(JNIEnv *env, const char* remoteAddress)
2377 OIC_LOG(DEBUG, TAG, "CALEClientGetGattObjInList");
2378 VERIFY_NON_NULL_RET(env, TAG, "env is null", NULL);
2379 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2381 ca_mutex_lock(g_gattObjectMutex);
2382 uint32_t length = u_arraylist_length(g_gattObjectList);
2383 for (uint32_t index = 0; index < length; index++)
2385 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2388 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2389 ca_mutex_unlock(g_gattObjectMutex);
2393 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2394 if (!jni_setAddress)
2396 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2397 ca_mutex_unlock(g_gattObjectMutex);
2401 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2404 OIC_LOG(ERROR, TAG, "setAddress is null");
2405 ca_mutex_unlock(g_gattObjectMutex);
2409 if (!strcmp(remoteAddress, setAddress))
2411 OIC_LOG(DEBUG, TAG, "the device is already set");
2412 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2413 ca_mutex_unlock(g_gattObjectMutex);
2416 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2419 ca_mutex_unlock(g_gattObjectMutex);
2420 OIC_LOG(DEBUG, TAG, "There are no the gatt object in list");
2424 CAResult_t CALEClientRemoveAllGattObjs(JNIEnv *env)
2426 OIC_LOG(DEBUG, TAG, "CALEClientRemoveAllGattObjs");
2427 VERIFY_NON_NULL(env, TAG, "env is null");
2429 ca_mutex_lock(g_gattObjectMutex);
2430 if (!g_gattObjectList)
2432 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2433 ca_mutex_unlock(g_gattObjectMutex);
2434 return CA_STATUS_FAILED;
2437 uint32_t length = u_arraylist_length(g_gattObjectList);
2438 for (uint32_t index = 0; index < length; index++)
2440 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2443 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2446 (*env)->DeleteGlobalRef(env, jarrayObj);
2449 OICFree(g_gattObjectList);
2450 g_gattObjectList = NULL;
2451 ca_mutex_unlock(g_gattObjectMutex);
2452 return CA_STATUS_OK;
2455 CAResult_t CALEClientRemoveGattObj(JNIEnv *env, jobject gatt)
2457 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObj");
2458 VERIFY_NON_NULL(gatt, TAG, "gatt is null");
2459 VERIFY_NON_NULL(env, TAG, "env is null");
2461 ca_mutex_lock(g_gattObjectMutex);
2462 if (!g_gattObjectList)
2464 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2465 ca_mutex_unlock(g_gattObjectMutex);
2466 return CA_STATUS_FAILED;
2469 uint32_t length = u_arraylist_length(g_gattObjectList);
2470 for (uint32_t index = 0; index < length; index++)
2472 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2475 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2476 ca_mutex_unlock(g_gattObjectMutex);
2477 return CA_STATUS_FAILED;
2480 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2481 if (!jni_setAddress)
2483 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2484 ca_mutex_unlock(g_gattObjectMutex);
2485 return CA_STATUS_FAILED;
2488 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2491 OIC_LOG(ERROR, TAG, "setAddress is null");
2492 ca_mutex_unlock(g_gattObjectMutex);
2493 return CA_STATUS_FAILED;
2496 jstring jni_remoteAddress = CALEClientGetAddressFromGattObj(env, gatt);
2497 if (!jni_remoteAddress)
2499 OIC_LOG(ERROR, TAG, "jni_remoteAddress is null");
2500 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2501 ca_mutex_unlock(g_gattObjectMutex);
2502 return CA_STATUS_FAILED;
2505 const char* remoteAddress = (*env)->GetStringUTFChars(env, jni_remoteAddress, NULL);
2508 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2509 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2510 ca_mutex_unlock(g_gattObjectMutex);
2511 return CA_STATUS_FAILED;
2514 if (!strcmp(setAddress, remoteAddress))
2516 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2517 (*env)->DeleteGlobalRef(env, jarrayObj);
2518 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2519 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2520 ca_mutex_unlock(g_gattObjectMutex);
2521 return CALEClientReorderingList(index, g_gattObjectList);
2523 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2524 (*env)->ReleaseStringUTFChars(env, jni_remoteAddress, remoteAddress);
2527 ca_mutex_unlock(g_gattObjectMutex);
2528 OIC_LOG(DEBUG, TAG, "there are no target object");
2529 return CA_STATUS_OK;
2532 CAResult_t CALEClientRemoveGattObjForAddr(JNIEnv *env, jstring addr)
2534 OIC_LOG(DEBUG, TAG, "CALEClientRemoveGattObjForAddr");
2535 VERIFY_NON_NULL(addr, TAG, "addr is null");
2536 VERIFY_NON_NULL(env, TAG, "env is null");
2538 ca_mutex_lock(g_gattObjectMutex);
2539 if (!g_gattObjectList)
2541 OIC_LOG(ERROR, TAG, "g_gattObjectList is null");
2542 ca_mutex_unlock(g_gattObjectMutex);
2543 return CA_STATUS_FAILED;
2546 uint32_t length = u_arraylist_length(g_gattObjectList);
2547 for (uint32_t index = 0; index < length; index++)
2549 jobject jarrayObj = (jobject) u_arraylist_get(g_gattObjectList, index);
2552 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2553 ca_mutex_unlock(g_gattObjectMutex);
2554 return CA_STATUS_FAILED;
2557 jstring jni_setAddress = CALEClientGetAddressFromGattObj(env, jarrayObj);
2558 if (!jni_setAddress)
2560 OIC_LOG(ERROR, TAG, "jni_setAddress is null");
2561 ca_mutex_unlock(g_gattObjectMutex);
2562 return CA_STATUS_FAILED;
2565 const char* setAddress = (*env)->GetStringUTFChars(env, jni_setAddress, NULL);
2568 OIC_LOG(ERROR, TAG, "setAddress is null");
2569 ca_mutex_unlock(g_gattObjectMutex);
2570 return CA_STATUS_FAILED;
2573 const char* remoteAddress = (*env)->GetStringUTFChars(env, addr, NULL);
2576 OIC_LOG(ERROR, TAG, "remoteAddress is null");
2577 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2578 ca_mutex_unlock(g_gattObjectMutex);
2579 return CA_STATUS_FAILED;
2582 if (!strcmp(setAddress, remoteAddress))
2584 OIC_LOG_V(DEBUG, TAG, "remove object : %s", remoteAddress);
2585 (*env)->DeleteGlobalRef(env, jarrayObj);
2587 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2588 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2589 ca_mutex_unlock(g_gattObjectMutex);
2590 return CALEClientReorderingList(index, g_gattObjectList);
2592 (*env)->ReleaseStringUTFChars(env, jni_setAddress, setAddress);
2593 (*env)->ReleaseStringUTFChars(env, addr, remoteAddress);
2596 ca_mutex_unlock(g_gattObjectMutex);
2597 OIC_LOG(DEBUG, TAG, "there are no target object");
2598 return CA_STATUS_FAILED;
2605 CAResult_t CALEClientUpdateDeviceState(const char* address, uint32_t connectedState,
2606 uint16_t notificationState, uint16_t sendState)
2608 VERIFY_NON_NULL(address, TAG, "address is null");
2610 CALEState_t *newstate = (CALEState_t*) OICMalloc(sizeof(CALEState_t));
2613 OIC_LOG(ERROR, TAG, "out of memory");
2614 return CA_MEMORY_ALLOC_FAILED;
2617 if (strlen(address) > CA_MACADDR_SIZE)
2619 OIC_LOG(ERROR, TAG, "address is not proper");
2621 return CA_STATUS_FAILED;
2624 OICStrcpy(newstate->address, sizeof(newstate->address), address);
2625 newstate->connectedState = connectedState;
2626 newstate->notificationState = notificationState;
2627 newstate->sendState = sendState;
2628 return CALEClientAddDeviceStateToList(newstate);
2631 CAResult_t CALEClientAddDeviceStateToList(CALEState_t* state)
2633 VERIFY_NON_NULL(state, TAG, "state is null");
2635 ca_mutex_lock(g_deviceStateListMutex);
2637 if (!g_deviceStateList)
2639 OIC_LOG(ERROR, TAG, "gdevice_list is null");
2640 ca_mutex_unlock(g_deviceStateListMutex);
2641 return CA_STATUS_FAILED;
2644 if (CALEClientIsDeviceInList(state->address))
2646 CALEState_t* curState = CALEClientGetStateInfo(state->address);
2649 OIC_LOG(ERROR, TAG, "curState is null");
2650 ca_mutex_unlock(g_deviceStateListMutex);
2651 return CA_STATUS_FAILED;
2654 if (STATE_CHARACTER_NO_CHANGE == state->notificationState)
2656 state->notificationState = curState->notificationState;
2659 // delete previous state for update new state
2660 CAResult_t res = CALEClientRemoveDeviceState(state->address);
2661 if (CA_STATUS_OK != res)
2663 OIC_LOG(ERROR, TAG, "CALEClientRemoveDeviceState has failed");
2664 ca_mutex_unlock(g_deviceStateListMutex);
2668 u_arraylist_add(g_deviceStateList, state); // update new state
2669 OIC_LOG_V(DEBUG, TAG, "Set State Info to List : %d, %d",
2670 state->connectedState, state->notificationState);
2672 ca_mutex_unlock(g_deviceStateListMutex);
2673 return CA_STATUS_OK;
2676 bool CALEClientIsDeviceInList(const char* remoteAddress)
2678 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2680 if (!g_deviceStateList)
2682 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2686 uint32_t length = u_arraylist_length(g_deviceStateList);
2687 for (uint32_t index = 0; index < length; index++)
2689 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2692 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2696 if (!strcmp(remoteAddress, state->address))
2698 OIC_LOG(DEBUG, TAG, "the device is already set");
2707 OIC_LOG(DEBUG, TAG, "there are no the device in list.");
2711 CAResult_t CALEClientRemoveAllDeviceState()
2713 OIC_LOG(DEBUG, TAG, "CALENativeRemoveAllDevices");
2715 ca_mutex_lock(g_deviceStateListMutex);
2716 if (!g_deviceStateList)
2718 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2719 ca_mutex_unlock(g_deviceStateListMutex);
2720 return CA_STATUS_FAILED;
2723 uint32_t length = u_arraylist_length(g_deviceStateList);
2724 for (uint32_t index = 0; index < length; index++)
2726 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2729 OIC_LOG(ERROR, TAG, "jarrayObj is null");
2735 OICFree(g_deviceStateList);
2736 g_deviceStateList = NULL;
2737 ca_mutex_unlock(g_deviceStateListMutex);
2739 return CA_STATUS_OK;
2742 CAResult_t CALEClientRemoveDeviceState(const char* remoteAddress)
2744 OIC_LOG(DEBUG, TAG, "CALEClientRemoveDeviceState");
2745 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
2747 if (!g_deviceStateList)
2749 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2750 return CA_STATUS_FAILED;
2753 uint32_t length = u_arraylist_length(g_deviceStateList);
2754 for (uint32_t index = 0; index < length; index++)
2756 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2759 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2763 if (!strcmp(state->address, remoteAddress))
2765 OIC_LOG_V(DEBUG, TAG, "remove state : %s", remoteAddress);
2768 CAResult_t res = CALEClientReorderingList(index, g_deviceStateList);
2769 if(CA_STATUS_OK != res)
2771 OIC_LOG(ERROR, TAG, "CALEClientReorderingList has failed");
2774 return CA_STATUS_OK;
2778 return CA_STATUS_FAILED;
2781 CALEState_t* CALEClientGetStateInfo(const char* remoteAddress)
2783 OIC_LOG(DEBUG, TAG, "CALEClientGetStateInfo");
2784 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", NULL);
2786 if (!g_deviceStateList)
2788 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2792 uint32_t length = u_arraylist_length(g_deviceStateList);
2793 for (uint32_t index = 0; index < length; index++)
2795 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2798 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2802 if (!strcmp(state->address, remoteAddress))
2804 OIC_LOG_V(DEBUG, TAG, "get state : %s", remoteAddress);
2811 bool CALEClientIsConnectedDevice(const char* remoteAddress)
2813 OIC_LOG(DEBUG, TAG, "CALEClientIsConnectedDevice");
2814 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2816 ca_mutex_lock(g_deviceStateListMutex);
2817 if (!g_deviceStateList)
2819 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2820 ca_mutex_unlock(g_deviceStateListMutex);
2824 uint32_t length = u_arraylist_length(g_deviceStateList);
2825 for (uint32_t index = 0; index < length; index++)
2827 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2830 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2834 if (!strcmp(state->address, remoteAddress))
2836 OIC_LOG(DEBUG, TAG, "check whether it is connected or not");
2838 if (STATE_CONNECTED == state->connectedState)
2840 ca_mutex_unlock(g_deviceStateListMutex);
2845 ca_mutex_unlock(g_deviceStateListMutex);
2850 ca_mutex_unlock(g_deviceStateListMutex);
2854 bool CALEClientIsSetCharacteristic(const char* remoteAddress)
2856 OIC_LOG(DEBUG, TAG, "CALEClientIsSetCharacteristic");
2857 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
2859 ca_mutex_lock(g_deviceStateListMutex);
2860 if (!g_deviceStateList)
2862 OIC_LOG(ERROR, TAG, "g_deviceStateList is null");
2863 ca_mutex_unlock(g_deviceStateListMutex);
2867 uint32_t length = u_arraylist_length(g_deviceStateList);
2868 for (uint32_t index = 0; index < length; index++)
2870 CALEState_t* state = (CALEState_t*) u_arraylist_get(g_deviceStateList, index);
2873 OIC_LOG(ERROR, TAG, "CALEState_t object is null");
2877 if (!strcmp(state->address, remoteAddress))
2879 OIC_LOG_V(DEBUG, TAG, "check whether it was set or not:%d", state->notificationState);
2881 if (STATE_CHARACTER_SET == state->notificationState)
2883 ca_mutex_unlock(g_deviceStateListMutex);
2888 ca_mutex_unlock(g_deviceStateListMutex);
2894 ca_mutex_unlock(g_deviceStateListMutex);
2898 void CALEClientCreateDeviceList()
2900 OIC_LOG(DEBUG, TAG, "CALEClientCreateDeviceList");
2902 // create new object array
2903 if (!g_gattObjectList)
2905 OIC_LOG(DEBUG, TAG, "Create g_gattObjectList");
2907 g_gattObjectList = u_arraylist_create();
2910 if (!g_deviceStateList)
2912 OIC_LOG(DEBUG, TAG, "Create g_deviceStateList");
2914 g_deviceStateList = u_arraylist_create();
2919 OIC_LOG(DEBUG, TAG, "Create g_deviceList");
2921 g_deviceList = u_arraylist_create();
2925 CAResult_t CALEClientReorderingList(uint32_t index, u_arraylist_t *list)
2929 OIC_LOG(ERROR, TAG, "list is null");
2930 return CA_STATUS_FAILED;
2933 if (index >= list->length)
2935 OIC_LOG(ERROR, TAG, "index is not available");
2936 return CA_STATUS_FAILED;
2939 if (index < list->length - 1)
2941 memmove(&list->data[index], &list->data[index + 1],
2942 (list->length - index - 1) * sizeof(void *));
2948 return CA_STATUS_OK;
2952 * Check Sent Count for remove g_sendBuffer
2954 void CALEClientUpdateSendCnt(JNIEnv *env)
2956 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
2958 ca_mutex_lock(g_threadMutex);
2962 if (g_targetCnt <= g_currentSentCnt)
2965 g_currentSentCnt = 0;
2969 (*env)->DeleteGlobalRef(env, g_sendBuffer);
2970 g_sendBuffer = NULL;
2972 // notity the thread
2973 ca_cond_signal(g_threadCond);
2974 CALEClientSetSendFinishFlag(true);
2975 OIC_LOG(DEBUG, TAG, "set signal for send data");
2978 ca_mutex_unlock(g_threadMutex);
2981 CAResult_t CALEClientInitGattMutexVaraibles()
2983 OIC_LOG(DEBUG, TAG, "IN");
2985 if (NULL == g_bleReqRespClientCbMutex)
2987 g_bleReqRespClientCbMutex = ca_mutex_new();
2988 if (NULL == g_bleReqRespClientCbMutex)
2990 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
2991 return CA_STATUS_FAILED;
2995 if (NULL == g_bleServerBDAddressMutex)
2997 g_bleServerBDAddressMutex = ca_mutex_new();
2998 if (NULL == g_bleServerBDAddressMutex)
3000 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3001 return CA_STATUS_FAILED;
3005 if (NULL == g_threadMutex)
3007 g_threadMutex = ca_mutex_new();
3008 if (NULL == g_threadMutex)
3010 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3011 return CA_STATUS_FAILED;
3015 if (NULL == g_threadSendMutex)
3017 g_threadSendMutex = ca_mutex_new();
3018 if (NULL == g_threadSendMutex)
3020 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3021 return CA_STATUS_FAILED;
3025 if (NULL == g_threadSendCBMutex)
3027 g_threadSendCBMutex = ca_mutex_new();
3028 if (NULL == g_threadSendCBMutex)
3030 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3031 return CA_STATUS_FAILED;
3035 if (NULL == g_deviceListMutex)
3037 g_deviceListMutex = ca_mutex_new();
3038 if (NULL == g_deviceListMutex)
3040 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3041 return CA_STATUS_FAILED;
3045 if (NULL == g_gattObjectMutex)
3047 g_gattObjectMutex = ca_mutex_new();
3048 if (NULL == g_gattObjectMutex)
3050 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3051 return CA_STATUS_FAILED;
3055 if (NULL == g_deviceStateListMutex)
3057 g_deviceStateListMutex = ca_mutex_new();
3058 if (NULL == g_deviceStateListMutex)
3060 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3061 return CA_STATUS_FAILED;
3065 if (NULL == g_SendFinishMutex)
3067 g_SendFinishMutex = ca_mutex_new();
3068 if (NULL == g_SendFinishMutex)
3070 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3071 return CA_STATUS_FAILED;
3075 if (NULL == g_writeCharacteristicCBMutex)
3077 g_writeCharacteristicCBMutex = ca_mutex_new();
3078 if (NULL == g_writeCharacteristicCBMutex)
3080 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3081 return CA_STATUS_FAILED;
3085 if (NULL == g_theSendRequestMutex)
3087 g_theSendRequestMutex = ca_mutex_new();
3088 if (NULL == g_theSendRequestMutex)
3090 OIC_LOG(ERROR, TAG, "ca_mutex_new has failed");
3091 return CA_STATUS_FAILED;
3095 OIC_LOG(DEBUG, TAG, "OUT");
3096 return CA_STATUS_OK;
3099 void CALEClientTerminateGattMutexVariables()
3101 OIC_LOG(DEBUG, TAG, "IN");
3103 ca_mutex_free(g_bleReqRespClientCbMutex);
3104 g_bleReqRespClientCbMutex = NULL;
3106 ca_mutex_free(g_bleServerBDAddressMutex);
3107 g_bleServerBDAddressMutex = NULL;
3109 ca_mutex_free(g_threadMutex);
3110 g_threadMutex = NULL;
3112 ca_mutex_free(g_threadSendMutex);
3113 g_threadSendMutex = NULL;
3115 ca_mutex_free(g_threadSendCBMutex);
3116 g_threadSendCBMutex = NULL;
3118 ca_mutex_free(g_deviceListMutex);
3119 g_deviceListMutex = NULL;
3121 ca_mutex_free(g_SendFinishMutex);
3122 g_SendFinishMutex = NULL;
3124 ca_mutex_free(g_writeCharacteristicCBMutex);
3125 g_writeCharacteristicCBMutex = NULL;
3127 ca_mutex_free(g_theSendRequestMutex);
3128 g_theSendRequestMutex = NULL;
3130 OIC_LOG(DEBUG, TAG, "OUT");
3133 void CALEClientSetSendFinishFlag(bool flag)
3135 OIC_LOG_V(DEBUG, TAG, "g_isFinishedSendData is %d", flag);
3137 ca_mutex_lock(g_SendFinishMutex);
3138 g_isFinishedSendData = flag;
3139 ca_mutex_unlock(g_SendFinishMutex);
3142 void CALEClientSetWriteCharacteristicCBFlag(bool flag)
3144 OIC_LOG_V(DEBUG, TAG, "g_isReceivedWriteCB is %d", flag);
3146 ca_mutex_lock(g_writeCharacteristicCBMutex);
3147 g_isReceivedWriteCB = flag;
3148 ca_mutex_unlock(g_writeCharacteristicCBMutex);
3151 void CALEClientSetTheSendRequestFlag(bool flag)
3153 OIC_LOG_V(DEBUG, TAG, "g_isRequestedSend is %d", flag);
3155 ca_mutex_lock(g_theSendRequestMutex);
3156 g_isRequestedSend = flag;
3157 ca_mutex_unlock(g_theSendRequestMutex);
3164 CAResult_t CAStartLEGattClient()
3166 CAResult_t res = CALEClientStartMulticastServer();
3167 if (CA_STATUS_OK != res)
3169 OIC_LOG(ERROR, TAG, "CALEClientStartMulticastServer has failed");
3173 g_isStartedLEClient = true;
3179 void CAStopLEGattClient()
3181 OIC_LOG(DEBUG, TAG, "CAStopBLEGattClient");
3185 OIC_LOG(ERROR, TAG, "g_jvm is null");
3189 bool isAttached = false;
3191 jint res = (*g_jvm)->GetEnv(g_jvm, (void**) &env, JNI_VERSION_1_6);
3194 OIC_LOG(INFO, TAG, "Could not get JNIEnv pointer");
3195 res = (*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL);
3199 OIC_LOG(ERROR, TAG, "AttachCurrentThread has failed");
3205 CAResult_t ret = CALEClientDisconnectAll(env);
3206 if (CA_STATUS_OK != ret)
3208 OIC_LOG(ERROR, TAG, "CALEClientDisconnectAll has failed");
3211 ret = CALEClientStopScan();
3212 if(CA_STATUS_OK != ret)
3214 OIC_LOG(ERROR, TAG, "CALEClientStopScan has failed");
3217 ca_cond_signal(g_threadCond);
3218 ca_cond_signal(g_threadSendCond);
3219 g_isStartedLEClient = false;
3223 (*g_jvm)->DetachCurrentThread(g_jvm);
3228 void CATerminateLEGattClient()
3230 OIC_LOG(DEBUG, TAG, "Terminate GATT Client");
3231 CALEClientTerminate();
3234 CAResult_t CAUpdateCharacteristicsToGattServer(const char *remoteAddress, const char *data,
3235 const uint32_t dataLen, CALETransferType_t type,
3236 const int32_t position)
3238 OIC_LOG(DEBUG, TAG, "call CALEClientSendUnicastMessage");
3239 VERIFY_NON_NULL(data, TAG, "data is null");
3240 VERIFY_NON_NULL(remoteAddress, TAG, "remoteAddress is null");
3242 return CALEClientSendUnicastMessage(remoteAddress, data, dataLen);
3245 CAResult_t CAUpdateCharacteristicsToAllGattServers(const char *data, uint32_t dataLen)
3247 OIC_LOG(DEBUG, TAG, "call CALEClientSendMulticastMessage");
3248 VERIFY_NON_NULL(data, TAG, "data is null");
3250 return CALEClientSendMulticastMessage(data, dataLen);
3253 void CASetLEReqRespClientCallback(CABLEClientDataReceivedCallback callback)
3255 OIC_LOG(DEBUG, TAG, "IN");
3257 ca_mutex_lock(g_bleReqRespClientCbMutex);
3258 g_CABLEClientDataReceivedCallback = callback;
3259 ca_mutex_unlock(g_bleReqRespClientCbMutex);
3261 OIC_LOG(DEBUG, TAG, "OUT");
3264 void CASetLEClientThreadPoolHandle(ca_thread_pool_t handle)
3266 OIC_LOG(DEBUG, TAG, "IN");
3268 CALEClientInitialize(handle);
3270 OIC_LOG(DEBUG, TAG, "OUT");
3273 CAResult_t CAGetLEAddress(char **local_address)
3275 OIC_LOG(DEBUG, TAG, "IN");
3277 VERIFY_NON_NULL(local_address, TAG, "local_address is null");
3278 CAResult_t res = CALEClientGetInterfaceInfo(local_address);
3279 if (CA_STATUS_OK != res)
3281 OIC_LOG(INFO, TAG, "it didn't get local address");
3284 OIC_LOG(DEBUG, TAG, "OUT");
3285 return CA_STATUS_OK;
3288 JNIEXPORT void JNICALL
3289 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterLeScanCallback(JNIEnv *env, jobject obj,
3292 OIC_LOG(DEBUG, TAG, "CaLeRegisterLeScanCallback");
3293 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3294 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3296 g_leScanCallback = (*env)->NewGlobalRef(env, callback);
3299 JNIEXPORT void JNICALL
3300 Java_org_iotivity_ca_CaLeClientInterface_caLeRegisterGattCallback(JNIEnv *env, jobject obj,
3303 OIC_LOG(DEBUG, TAG, "CaLeRegisterGattCallback");
3304 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3305 VERIFY_NON_NULL_VOID(callback, TAG, "callback is null");
3307 g_leGattCallback = (*env)->NewGlobalRef(env, callback);
3310 JNIEXPORT void JNICALL
3311 Java_org_iotivity_ca_CaLeClientInterface_caLeScanCallback(JNIEnv *env, jobject obj,
3312 jobject device, jint rssi,
3313 jbyteArray scanRecord)
3315 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3316 VERIFY_NON_NULL_VOID(device, TAG, "device is null");
3318 CAResult_t res = CALEClientAddScanDeviceToList(env, device);
3319 if (CA_STATUS_OK != res)
3321 OIC_LOG_V(ERROR, TAG, "CALEClientAddScanDeviceToList has failed : %d", res);
3326 * Class: org_iotivity_ca_jar_caleinterface
3327 * Method: CALeGattConnectionStateChangeCallback
3328 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3330 JNIEXPORT void JNICALL
3331 Java_org_iotivity_ca_CaLeClientInterface_caLeGattConnectionStateChangeCallback(JNIEnv *env,
3337 OIC_LOG_V(DEBUG, TAG, "CALeGattConnectionStateChangeCallback - status %d, newstate %d", status,
3339 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3340 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3342 if (GATT_SUCCESS == status && STATE_CONNECTED == newstate) // le connected
3344 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3350 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3353 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED,
3354 STATE_CHARACTER_NO_CHANGE,
3356 if (CA_STATUS_OK != res)
3358 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3359 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3362 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3365 CAResult_t res = CALEClientAddGattobjToList(env, gatt);
3366 if (CA_STATUS_OK != res)
3368 OIC_LOG(ERROR, TAG, "CALEClientAddGattobjToList has failed");
3372 res = CALEClientDiscoverServices(env, gatt);
3373 if (CA_STATUS_OK != res)
3375 OIC_LOG(ERROR, TAG, "CALEClientDiscoverServices has failed");
3379 else if (GATT_SUCCESS == status && STATE_DISCONNECTED == newstate) // le disconnected
3381 CAResult_t res = CALEClientStartScan();
3382 if (CA_STATUS_OK != res)
3384 OIC_LOG(ERROR, TAG, "CALEClientStartScan has failed");
3387 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3390 OIC_LOG(ERROR, TAG, "CALEClientGetAddressFromGattObj has failed");
3393 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3396 res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3397 STATE_CHARACTER_NO_CHANGE,
3399 if (CA_STATUS_OK != res)
3401 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3403 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3406 res = CALEClientGattClose(env, gatt);
3407 if (CA_STATUS_OK != res)
3409 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3412 ca_cond_signal(g_threadSendCond);
3417 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3420 OIC_LOG(ERROR, TAG, "jni_address is null");
3425 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3428 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_DISCONNECTED,
3429 STATE_CHARACTER_NO_CHANGE,
3431 if (CA_STATUS_OK != res)
3433 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3436 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3438 CAResult_t res = CALEClientGattClose(env, gatt);
3439 if (CA_STATUS_OK != res)
3441 OIC_LOG(ERROR, TAG, "CALEClientGattClose has failed");
3451 CALEClientSendFinish(env, gatt);
3452 ca_cond_signal(g_threadSendCond);
3457 * Class: org_iotivity_ca_jar_caleinterface
3458 * Method: CALeGattServicesDiscoveredCallback
3459 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3461 JNIEXPORT void JNICALL
3462 Java_org_iotivity_ca_CaLeClientInterface_caLeGattServicesDiscoveredCallback(JNIEnv *env,
3467 OIC_LOG_V(DEBUG, TAG, "CALeGattServicesDiscoveredCallback - status %d: ", status);
3468 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3469 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3471 if (0 != status) // discovery error
3473 CALEClientSendFinish(env, gatt);
3474 ca_cond_signal(g_threadSendCond);
3478 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3481 CALEClientSendFinish(env, gatt);
3482 ca_cond_signal(g_threadSendCond);
3486 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3489 CALEClientSendFinish(env, gatt);
3490 ca_cond_signal(g_threadSendCond);
3494 if (!CALEClientIsSetCharacteristic(address))
3496 jstring jni_uuid = (*env)->NewStringUTF(env, OIC_GATT_CHARACTERISTIC_RESPONSE_UUID);
3499 OIC_LOG(ERROR, TAG, "jni_uuid is null");
3503 jobject jni_obj_GattCharacteristic = CALEClientGetGattService(env, gatt, jni_uuid);
3504 if (!jni_obj_GattCharacteristic)
3506 OIC_LOG(ERROR, TAG, "jni_obj_GattCharacteristic is null");
3510 CAResult_t res = CALEClientSetCharacteristicNotification(env, gatt,
3511 jni_obj_GattCharacteristic);
3512 if (CA_STATUS_OK != res)
3514 OIC_LOG(ERROR, TAG, "CALEClientSetCharacteristicNotification has failed");
3518 res = CALEClientSetUUIDToDescriptor(env, gatt, jni_obj_GattCharacteristic);
3519 if (CA_STATUS_OK != res)
3521 OIC_LOG(ERROR, TAG, "CALEClientSetUUIDToDescriptor has failed");
3525 res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3527 if (CA_STATUS_OK != res)
3529 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3535 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3536 if (CA_STATUS_OK != res)
3538 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3543 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3548 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3549 CALEClientSendFinish(env, gatt);
3550 ca_cond_signal(g_threadSendCond);
3555 * Class: org_iotivity_ca_jar_caleinterface
3556 * Method: CALeGattCharacteristicReadCallback
3557 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3559 JNIEXPORT void JNICALL
3560 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicReadCallback(JNIEnv *env,
3563 jobject characteristic,
3567 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicReadCallback - status : %d", status);
3571 * Class: org_iotivity_ca_jar_caleinterface
3572 * Method: CALeGattCharacteristicWritjclasseCallback
3573 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;I)V
3575 JNIEXPORT void JNICALL
3576 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicWriteCallback(
3577 JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data,
3580 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - status : %d", status);
3581 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3582 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3584 ca_mutex_lock(g_threadSendCBMutex);
3585 if (!g_isRequestedSend)
3587 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - waiting");
3588 ca_cond_wait(g_threadSendCBCond, g_threadSendCBMutex);
3590 ca_mutex_unlock(g_threadSendCBMutex);
3593 char* wroteData = (char*) (*env)->GetByteArrayElements(env, data, &isCopy);
3595 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicWriteCallback - write data : %s", wroteData);
3597 // send success & signal
3598 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3604 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3610 if (GATT_SUCCESS != status) // error case
3612 OIC_LOG(ERROR, TAG, "send failure");
3613 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3615 if (CA_STATUS_OK != res)
3617 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3619 CALEClientSendFinish(env, gatt);
3623 OIC_LOG(DEBUG, TAG, "send success");
3624 CAResult_t res = CALEClientUpdateDeviceState(address, STATE_CONNECTED, STATE_CHARACTER_SET,
3625 STATE_SEND_SUCCESS);
3626 if (CA_STATUS_OK != res)
3628 OIC_LOG(ERROR, TAG, "CALEClientUpdateDeviceState has failed");
3630 CALEClientUpdateSendCnt(env);
3633 CALEClientSetWriteCharacteristicCBFlag(true);
3634 CALEClientSetTheSendRequestFlag(false);
3635 ca_cond_signal(g_threadSendCond);
3636 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3642 CALEClientSetWriteCharacteristicCBFlag(true);
3643 CALEClientSetTheSendRequestFlag(false);
3644 CALEClientSendFinish(env, gatt);
3645 ca_cond_signal(g_threadSendCond);
3650 * Class: org_iotivity_ca_jar_caleinterface
3651 * Method: CALeGattCharacteristicChangedCallback
3652 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattCharacteristic;)V
3654 JNIEXPORT void JNICALL
3655 Java_org_iotivity_ca_CaLeClientInterface_caLeGattCharacteristicChangedCallback(
3656 JNIEnv *env, jobject obj, jobject gatt, jobject characteristic, jbyteArray data)
3658 OIC_LOG(DEBUG, TAG, "CALeGattCharacteristicChangedCallback");
3659 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3660 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3661 VERIFY_NON_NULL_VOID(data, TAG, "data is null");
3663 // get Byte Array and covert to char*
3664 jint length = (*env)->GetArrayLength(env, data);
3667 jbyte *jni_byte_responseData = (jbyte*) (*env)->GetByteArrayElements(env, data, &isCopy);
3669 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - raw data received : %s",
3670 jni_byte_responseData);
3672 char* receivedData = (char*) OICMalloc(sizeof(char) * length + 1);
3675 OIC_LOG(ERROR, TAG, "recevicedData is null");
3679 memcpy(receivedData, (const char*) jni_byte_responseData, length);
3680 receivedData[length] = '\0';
3681 (*env)->ReleaseByteArrayElements(env, data, jni_byte_responseData, JNI_ABORT);
3683 jstring jni_address = CALEClientGetAddressFromGattObj(env, gatt);
3686 OIC_LOG(ERROR, TAG, "jni_address is null");
3687 OICFree(receivedData);
3691 const char* address = (*env)->GetStringUTFChars(env, jni_address, NULL);
3694 OIC_LOG(ERROR, TAG, "address is null");
3695 OICFree(receivedData);
3699 OIC_LOG_V(DEBUG, TAG, "CALeGattCharacteristicChangedCallback - data. : %s, %d",
3700 receivedData, length);
3702 ca_mutex_lock(g_bleServerBDAddressMutex);
3703 uint32_t sentLength = 0;
3704 g_CABLEClientDataReceivedCallback(address, OIC_GATT_SERVICE_UUID, receivedData, length,
3706 ca_mutex_unlock(g_bleServerBDAddressMutex);
3708 (*env)->ReleaseStringUTFChars(env, jni_address, address);
3712 * Class: org_iotivity_ca_jar_caleinterface
3713 * Method: CALeGattDescriptorReadCallback
3714 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3716 JNIEXPORT void JNICALL
3717 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorReadCallback(JNIEnv *env, jobject obj,
3722 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorReadCallback - status %d: ", status);
3726 * Class: org_iotivity_ca_jar_caleinterface
3727 * Method: CALeGattDescriptorWriteCallback
3728 * Signature: (Landroid/bluetooth/BluetoothGatt;Landroid/bluetooth/BluetoothGattDescriptor;I)V
3730 JNIEXPORT void JNICALL
3731 Java_org_iotivity_ca_CaLeClientInterface_caLeGattDescriptorWriteCallback(JNIEnv *env, jobject obj,
3736 OIC_LOG_V(DEBUG, TAG, "CALeGattDescriptorWriteCallback - status %d: ", status);
3737 VERIFY_NON_NULL_VOID(env, TAG, "env is null");
3738 VERIFY_NON_NULL_VOID(gatt, TAG, "gatt is null");
3740 CAResult_t res = CALEClientWriteCharacteristic(env, gatt);
3741 if (CA_STATUS_OK != res)
3743 OIC_LOG(ERROR, TAG, "CALEClientWriteCharacteristic has failed");
3751 CALEClientSendFinish(env, gatt);
3752 ca_cond_signal(g_threadSendCond);
3757 * Class: org_iotivity_ca_jar_caleinterface
3758 * Method: CALeGattReliableWriteCompletedCallback
3759 * Signature: (Landroid/bluetooth/BluetoothGatt;I)V
3761 JNIEXPORT void JNICALL
3762 Java_org_iotivity_ca_CaLeClientInterface_caLeGattReliableWriteCompletedCallback(JNIEnv *env,
3767 OIC_LOG_V(DEBUG, TAG, "CALeGattReliableWriteCompletedCallback - status %d: ", status);
3771 * Class: org_iotivity_ca_jar_caleinterface
3772 * Method: CALeGattReadRemoteRssiCallback
3773 * Signature: (Landroid/bluetooth/BluetoothGatt;II)V
3775 JNIEXPORT void JNICALL
3776 Java_org_iotivity_ca_CaLeClientInterface_caLeGattReadRemoteRssiCallback(JNIEnv *env, jobject obj,
3777 jobject gatt, jint rssi,
3780 OIC_LOG_V(DEBUG, TAG, "CALeGattReadRemoteRssiCallback - rssi %d, status %d: ", rssi, status);